Calculate the bounding box of the text. Unlike :meth:`matplotlib.text.Text.get_extents` method, The bbox size of the text before the rotation is calculated.
(text, renderer)
| 69 | |
| 70 | |
| 71 | def _get_textbox(text, renderer): |
| 72 | """ |
| 73 | Calculate the bounding box of the text. Unlike |
| 74 | :meth:`matplotlib.text.Text.get_extents` method, The bbox size of |
| 75 | the text before the rotation is calculated. |
| 76 | """ |
| 77 | # TODO : This function may move into the Text class as a method. As a |
| 78 | # matter of fact, The information from the _get_textbox function |
| 79 | # should be available during the Text._get_layout() call, which is |
| 80 | # called within the _get_textbox. So, it would better to move this |
| 81 | # function as a method with some refactoring of _get_layout method. |
| 82 | |
| 83 | projected_xs = [] |
| 84 | projected_ys = [] |
| 85 | |
| 86 | theta = np.deg2rad(text.get_rotation()) |
| 87 | tr = Affine2D().rotate(-theta) |
| 88 | |
| 89 | _, parts, d = text._get_layout(renderer) |
| 90 | |
| 91 | for t, wh, x, y in parts: |
| 92 | w, h = wh |
| 93 | |
| 94 | xt1, yt1 = tr.transform_point((x, y)) |
| 95 | yt1 -= d |
| 96 | xt2, yt2 = xt1 + w, yt1 + h |
| 97 | |
| 98 | projected_xs.extend([xt1, xt2]) |
| 99 | projected_ys.extend([yt1, yt2]) |
| 100 | |
| 101 | xt_box, yt_box = min(projected_xs), min(projected_ys) |
| 102 | w_box, h_box = max(projected_xs) - xt_box, max(projected_ys) - yt_box |
| 103 | |
| 104 | x_box, y_box = Affine2D().rotate(theta).transform_point((xt_box, yt_box)) |
| 105 | |
| 106 | return x_box, y_box, w_box, h_box |
| 107 | |
| 108 | |
| 109 | @cbook._define_aliases({ |
no test coverage detected