MCPcopy Create free account
hub / github.com/PageBot/PageBot / BabelString

Class BabelString

Lib/pagebot/contexts/basecontext/babelstring.py:38–1411  ·  view source on GitHub ↗

BabelString is a generic string format, that stores a string or text as a list of BabelRun instances. String behaviour can differ between various output contexts and platforms. A BabelString should not know anything about contexts, except that it can execute the text, textBox, textS

Source from the content-addressed store, hash-verified

36from pagebot.toolbox.color import color
37
38class BabelString:
39 """BabelString is a generic string format, that stores a string or text as
40 a list of BabelRun instances. String behaviour can differ between various
41 output contexts and platforms.
42
43 A BabelString should not know anything about contexts, except that it can
44 execute the text, textBox, textSize and textLines fuctions. Styles in runs
45 are not cascading.
46
47 ?? self.leading returns the value of the last run, because
48 that is the attribute value if plain text gets appended.
49
50 For platform-specific doctests, see
51 doctests/strings-*.txt in the repository root.
52
53 NOTE:
54 - The styles values of sequential runs are *not* cascading. This is
55 similar to the behavior of the DrawBot FormattedString attributes.
56 - Plain numbers are by default converted to points.
57 - Attribute properties refer to the style of the last run.
58
59 >>> from pagebot.toolbox.units import pt, mm
60 >>> from pagebot.contexts import getContext
61 >>> context = getContext()
62 >>> bs = BabelString('ABCD', style=dict(fontSize=12), context=context)
63 >>> bs.fontSize
64 12pt
65 >>>
66 >>> bs.fontSize = mm(24)
67 >>> bs.fontSize, bs.runs[0].style['fontSize']
68 (24mm, 24mm)
69 >>> bs.runs # Without context a BabelString can do all that does not need rendering.
70 [<BabelRun "ABCD">]
71
72 """
73 def __init__(self, s=None, style=None, w=None, h=None, context=None):
74 """Constructor of BabelString. `s` is a plain string, style is a
75 dictionary compatible with the document root style keys to add one
76 PageBotRun as default. Otherwise self.runs is created as empty list.
77 @s should be a plain string, but gets cast by `str(s)` otherwise.
78 Optional `w` and `h` make the difference if this BabelString behaves as
79 a plain string (answering it&#x27;s own size) or a text (answering the
80 defined size and overflow).
81
82 Some methods only work if context is defined (e.g. self.textSize,
83 self.lines and self.overflow)
84
85 >>> from pagebot.contexts import getContext
86 >>> context = getContext()
87 >>> bs = BabelString(context=context)
88 >>> len(bs.runs)
89 0
90 >>> bs.add('ABCD')
91 >>> len(bs), len(bs.runs)
92 (4, 1)
93 >>> bs.add('EFGH') # Without style, adding to the last run
94 >>> len(bs), len(bs.runs)
95 (8, 1)

Callers 5

newStringMethod · 0.90
_set_bsMethod · 0.90
appendMethod · 0.90
__getitem__Method · 0.85
__add__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected