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

Method __init__

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

Constructor of BabelString. `s` is a plain string, style is a dictionary compatible with the document root style keys to add one PageBotRun as default. Otherwise self.runs is created as empty list. @s should be a plain string, but gets cast by `str(s)` otherwise. Opti

(self, s=None, style=None, w=None, h=None, context=None)

Source from the content-addressed store, hash-verified

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'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)
96 >>> bs = context.newString('ABCD')
97 >>> bs
98 $ABCD$
99 """
100 # Context instance @context is used for text size rendering methods.
101 self.runs = [] # List of BabelRun instances.
102 if s is not None or style is not None:
103 self.runs.append(BabelRun(s, style))
104 self.context = context # Store optional as weakref property. Clears cache.
105 assert self.context
106 self._w = units(w) # Set source value of the properties, not need clear cache again.
107 self._h = units(h) # Set to points, if not already a Unit instance.
108
109 # Cache is initialize by the self.context-->self.reset() property call.
110 # In case of overflow for a given width and height, the overflow indices
111 # store the slice in self.lines for the current overflow render by the context.
112 # _overflowStart Line index where overflow starts.
113 # _overflowEnd Line (non-inclusive)
114 # _cs Cache of native context string (e.g. DrawBot.FormattedString or FlatStringData)
115 # _lines Cache of calculated meta info after line wrapping BabelLine
116 # _twh Cached tuple of calculated text width (self.tw, self.th)
117 # _pwh Cached tuple of calculated pixel width (self.pw, self.ph)
118 #self.reset() # Clear cache of previous context
119
120 def _get_context(self):
121 """Answers the weakref context if it is defined.

Callers

nothing calls this directly

Calls 3

unitsFunction · 0.90
BabelRunClass · 0.85
appendMethod · 0.45

Tested by

no test coverage detected