File: text_layout_engine_usage.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (361 lines) | stat: -rw-r--r-- 12,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#  Copyright (c) 2021-2023, Manfred Moitzi
#  License: MIT License
from typing import Iterable
import pathlib
import random
import ezdxf
from ezdxf import zoom, print_config
from ezdxf.math import Matrix44
from ezdxf.fonts import fonts
from ezdxf.tools import text_layout as tl
from ezdxf.enums import TextEntityAlignment

""" 
This example shows the usage of the internal text_layout module to render 
complex text layouts. The module is designed to render MText like entities,
but could be used for other tasks too. The layout engine supports a multi 
column setup, each column contains paragraphs, and these paragraphs can 
automatically flow across the columns. All locations are relative to each other, 
absolute locations are not supported - tabulators are not supported.

The layout engine knows nothing about the content itself, it just manages 
content boxes of a fixed given width and height and "glue" spaces in between. 
The engine does not alter the size of the content boxes, but resizes the glue 
if necessary. The actual rendering is done by a rendering object associated to 
each content box. 

The only text styling manged by the layout engine is underline, overline and 
strike through multiple content boxes.

Features:

- layout alignment like MText: top-middle-bottom combined with left-center-right
- paragraph alignments: left, right, center, justified
- paragraph indentation: left, right, special first line
- cell alignments: top, center, bottom
- fraction cells: over, slanted, tolerance style
- columns have a fixed height or grows automatically, paragraphs which do not 
  fit "flow" into the following column.
- pass through of transformation matrix to the rendering object

TODO: 

- bullet- and numbered lists
- refinements to replicate MText features as good as possible

Used for:

- drawing add-on to render MTEXT with columns
- explode MTEXT into DXF primitives (TEXT, LINE)

"""
# Type alias:
Content = Iterable[tl.Cell]

DIR = pathlib.Path("~/Desktop/Outbox").expanduser()
STYLE = "Style0"
FONT = "OpenSans-Regular.ttf"
COLUMN_HEIGHT: float = 12

print_config()

doc = ezdxf.new()
msp = doc.modelspace()
style = doc.styles.new(STYLE, dxfattribs={"font": FONT})


def measure_space(font):
    return font.text_width(" X") - font.text_width("X")


class SizedFont:
    def __init__(self, height: float):
        self.height = float(height)
        self.font = fonts.make_font(FONT, self.height)
        self.space = measure_space(self.font)

    def text_width(self, text: str):
        return self.font.text_width(text)


fix_sized_fonts = [
    SizedFont(0.18),
    SizedFont(0.35),
    SizedFont(0.50),
    SizedFont(0.70),
    SizedFont(1.00),
]


class FrameRenderer(tl.ContentRenderer):
    """Render object to render a frame around a content collection.

    This renderer can be used by collections which just manages content
    but do not represent a content by itself (Layout, Column, Paragraph).

    """

    def __init__(self, color):
        self.color = color

    def render(
        self,
        left: float,
        bottom: float,
        right: float,
        top: float,
        m: Matrix44 = None,
    ) -> None:
        """Render a frame as LWPOLYLINE."""
        pline = msp.add_lwpolyline(
            [(left, top), (right, top), (right, bottom), (left, bottom)],
            close=True,
            dxfattribs={"color": self.color},
        )
        if m:
            pline.transform(m)

    def line(
        self, x1: float, y1: float, x2: float, y2: float, m: Matrix44 = None
    ) -> None:
        """Line renderer used to create underline, overline, strike through
        and fraction dividers.

        """
        line = msp.add_line((x1, y1), (x2, y2), dxfattribs={"color": self.color})
        if m:
            line.transform(m)


class TextRenderer(tl.ContentRenderer):
    """Text content renderer."""

    def __init__(self, text, attribs):
        self.text = text
        self.attribs = attribs
        self.line_attribs = {"color": attribs["color"]}

    def render(
        self,
        left: float,
        bottom: float,
        right: float,
        top: float,
        m: Matrix44 = None,
    ):
        """Create/render the text content"""
        text = msp.add_text(self.text, dxfattribs=self.attribs)
        text.set_placement((left, bottom), align=TextEntityAlignment.LEFT)
        if m:
            text.transform(m)

    def line(
        self, x1: float, y1: float, x2: float, y2: float, m: Matrix44 = None
    ) -> None:
        """Line renderer used to create underline, overline, strike through
        and fraction dividers.

        """
        line = msp.add_line((x1, y1), (x2, y2), dxfattribs=self.line_attribs)
        if m:
            line.transform(m)


class Word(tl.Text):
    """Represent a word as content box for the layout engine."""

    def __init__(self, text: str, font: SizedFont, stroke: int = 0):
        # Each content box can have individual properties:
        attribs = {
            "color": random.choice((1, 2, 3, 4, 6, 7, 7)),
            "height": font.height,
            "style": STYLE,
        }
        super().__init__(
            # Width and height of the content are fixed given values and will
            # not be changed by the layout engine:
            width=font.text_width(text),
            height=font.height,
            stroke=stroke,
            # Each content box can have it's own rendering object:
            renderer=TextRenderer(text, attribs),
        )


def uniform_content(count: int, size: int = 1) -> Content:
    """Create content with one text size."""
    font = fix_sized_fonts[size]
    for word in tl.lorem_ipsum(count):
        yield Word(word, font)
        yield tl.Space(font.space)


def random_sized_content(count: int) -> Content:
    """Create content with randomized text size."""

    def size():
        return random.choice([0, 1, 1, 1, 1, 1, 2, 3])

    for word in tl.lorem_ipsum(count):
        font = fix_sized_fonts[size()]
        yield Word(word, font)
        yield tl.Space(font.space)


def stroke_groups(words: Iterable[str]):
    group = []
    count = 0
    stroke = 0
    for word in words:
        if count == 0:
            if group:
                yield group, stroke
            count = random.randint(1, 4)
            group = [word]
            stroke = random.choice([0, 0, 0, 0, 1, 1, 1, 2, 2, 4])
        else:
            count -= 1
            group.append(word)

    if group:
        yield group, stroke


def stroked_content(count: int, size: int = 1) -> Content:
    """Create content with one text size and groups of words with or without
    strokes.

    """
    font = fix_sized_fonts[size]
    groups = stroke_groups(tl.lorem_ipsum(count))
    for group, stroke in groups:
        # strokes should span across spaces in between words:
        # Spaces between words are bound to the preceding content box renderer,
        # MText is more flexible, but this implementation is easy and good
        # enough, otherwise spaces would need a distinct height and a rendering
        # object, both are not implemented for glue objects.
        continue_stroke = stroke + 8 if stroke else 0
        for word in group[:-1]:
            yield Word(word, font=font, stroke=continue_stroke)
            yield tl.Space(font.space)
        # strokes end at the last word, without continue stroke:
        yield Word(group[-1], font=font, stroke=stroke)
        yield tl.Space(font.space)


class Fraction(tl.Fraction):
    """Represents a fraction for the layout engine, which consist of a top-
    and bottom content box, divided by horizontal or slanted line.
    The "tolerance style" has no line between the stacked content boxes.

    This implementation is more flexible than MText, the content boxes can be
    words but also fractions or cell groups.

    """

    def __init__(self, t1: str, t2: str, stacking: tl.Stacking, font: SizedFont):
        top = Word(t1, font)
        bottom = Word(t2, font)
        super().__init__(
            top=top,
            bottom=bottom,
            stacking=stacking,
            # Uses only the generic line renderer to render the divider line,
            # the top- and bottom content boxes use their own render objects.
            renderer=FrameRenderer(color=7),
        )


def fraction_content() -> Content:
    """Create content with one text size and place random fractions between
    words.

    """
    words = list(uniform_content(120))
    for word in words:
        word.valign = tl.CellAlignment.BOTTOM

    stacking_options = list(tl.Stacking)
    font = SizedFont(0.25)  # fraction font
    for _ in range(10):
        stacking = random.choice(stacking_options)
        top = str(random.randint(1, 1000))
        bottom = str(random.randint(1, 1000))
        pos = random.randint(0, len(words) - 1)
        if isinstance(words[pos], tl.Space):
            pos += 1
        words.insert(pos, Fraction(top, bottom, stacking, font))
        words.insert(pos + 1, tl.Space(font.space))
    return words


def create_layout(align: tl.ParagraphAlignment, content: Content) -> tl.Layout:
    # Create a flow text paragraph for the content:
    paragraph = tl.Paragraph(align=align)
    paragraph.append_content(content)

    # Start the layout engine and set default column width:
    layout = tl.Layout(
        width=8,  # default column width for columns without define width
        margins=(0.5,),  # space around the layout
        # The render object of collections like Layout, Column or Paragraph is
        # called before the render objects of the content managed by the
        # collection.
        # This could be used to render a frame or a background:
        renderer=FrameRenderer(color=2),
    )

    # Append the first column with default width and a content height of 12 drawing
    # units. At least the first column has to be created by the client.
    layout.append_column(height=COLUMN_HEIGHT, gutter=1)

    # Append the content. The content will be distributed across the available
    # columns and automatically overflow into adjacent columns if necessary.
    # The layout engine creates new columns automatically if required by
    # cloning the last column.
    layout.append_paragraphs([paragraph])

    # Content- and total size is always up to date, only the final location
    # has to be updated by calling Layout.place().
    print()
    print(f"Layout has {len(layout)} columns.")
    print(f"Layout total width: {layout.total_width}")
    print(f"Layout total height: {layout.total_height}")

    for n, column in enumerate(layout, start=1):
        print()
        print(f"  {n}. column has {len(column)} paragraph(s)")
        print(f"  Column total width: {column.total_width}")
        print(f"  Column total height: {column.total_height}")

    # It is recommended to place the layout at origin (0, 0) and use a
    # transformation matrix to move the layout to the final location in
    # the DXF target layout - the model space in this example.
    # Set final layout location in the xy-plane with alignment:
    layout.place(align=tl.LayoutAlignment.BOTTOM_LEFT)

    # It is possible to add content after calling place(), but place has to be
    # called again before calling the render() method of the layout.
    return layout


def create(content: Content, y: float) -> None:
    x: float = 0
    for align in list(tl.ParagraphAlignment):
        # Build and place the layout at (0, 0):
        layout = create_layout(align, content)
        # Render and move the layout to the final location:
        m = Matrix44.translate(x, y, 0)
        layout.render(m)
        x += layout.total_width + 2


dy: float = COLUMN_HEIGHT + 3
create(list(uniform_content(200)), 0)
create(list(random_sized_content(200)), dy)
create(list(stroked_content(200)), 2 * dy)
create(fraction_content(), 3 * dy)

# zooming needs the longest time:
zoom.extents(msp, factor=1.1)
doc.saveas(str(DIR / "text_layout.dxf"))