def draw_rect(points: List[Vec3], color: Color, out: Backend): from ezdxf.addons.drawing import Properties props = Properties() props.color = color for a, b in zip(points, points[1:]): out.draw_line(a, b, props)
def simplified_text_chunks( text: AnyText, out: Backend, *, font: fonts.Font = None, debug_draw_rect: bool = False ) -> Iterable[Tuple[str, Matrix44, float]]: """ Splits a complex text entity into simple chunks of text which can all be rendered the same way: render the string (which will not contain any newlines) with the given cap_height with (left, baseline) at (0, 0) then transform it with the given matrix to move it into place. """ alignment = _get_alignment(text) box_width = _get_text_width(text) cap_height = _get_cap_height(text) lines = _split_into_lines( text, box_width, lambda s: out.get_text_line_width(s, cap_height, font=font)) line_spacing = _get_line_spacing(text, cap_height) line_widths = [ out.get_text_line_width(line, cap_height, font=font) for line in lines ] font_measurements = out.get_font_measurements(cap_height, font=font) anchor, line_xs, line_ys = \ _apply_alignment(alignment, line_widths, line_spacing, box_width, font_measurements) rotation = _get_rotation(text) # first_line_width is used for TEXT, ATTRIB and ATTDEF stretching if line_widths: first_line_width = line_widths[0] else: # no text lines -> no output, value is not important first_line_width = 1.0 extra_transform = _get_extra_transform(text, first_line_width) insert = _get_wcs_insert(text) whole_text_transform = ( Matrix44.translate(-anchor[0], -anchor[1], 0) @ extra_transform @ rotation @ Matrix44.translate(*insert.xyz)) for i, (line, line_x, line_y) in enumerate(zip(lines, line_xs, line_ys)): transform = Matrix44.translate(line_x, line_y, 0) @ whole_text_transform yield line, transform, cap_height if debug_draw_rect: width = out.get_text_line_width(line, cap_height, font) ps = list( transform.transform_vertices([ Vec3(0, 0, 0), Vec3(width, 0, 0), Vec3(width, cap_height, 0), Vec3(0, cap_height, 0), Vec3(0, 0, 0) ])) draw_rect(ps, '#ff0000', out)
def simplified_text_chunks(text: AnyText, out: Backend, *, debug_draw_rect: bool = False) -> Iterable[Tuple[str, Matrix44, float]]: """ Splits a complex text entity into simple chunks of text which can all be rendered the same way: render the string (which will not contain any newlines) with the given cap_height with (left, baseline) at (0, 0) then transform it with the given matrix to move it into place. """ alignment = _get_alignment(text) box_width = _get_text_width(text) cap_height = _get_cap_height(text) lines = _split_into_lines(text, box_width, lambda s: out.get_text_line_width(s, cap_height)) line_spacing = _get_line_spacing(text, cap_height) line_widths = [out.get_text_line_width(line, cap_height) for line in lines] font_measurements = out.get_font_measurements(cap_height) anchor, line_xs, line_ys = \ _apply_alignment(alignment, line_widths, cap_height, line_spacing, box_width, font_measurements) rotation = _get_rotation(text) extra_transform = _get_extra_transform(text) insert = _get_wcs_insert(text) whole_text_transform = ( Matrix44.translate(-anchor[0], -anchor[1], 0) @ extra_transform @ rotation @ Matrix44.translate(*insert.xyz) ) for i, (line, line_x, line_y) in enumerate(zip(lines, line_xs, line_ys)): transform = Matrix44.translate(line_x, line_y, 0) @ whole_text_transform yield line, transform, cap_height if debug_draw_rect: width = out.get_text_line_width(line, cap_height) ps = list(transform.transform_vertices([Vector(0, 0, 0), Vector(width, 0, 0), Vector(width, cap_height, 0), Vector(0, cap_height, 0), Vector(0, 0, 0)])) for a, b in zip(ps, ps[1:]): out.draw_line(a, b, '#ff0000')