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

Method build

Lib/pagebot/elements/text.py:296–375  ·  view source on GitHub ↗

Draws the text on position (x, y). Draws a background rectangle and / or frame if fill and / or stroke are defined. >>> from pagebot.document import Document >>> from pagebot.elements import * >>> from pagebot.contexts import getContext >>> from pagebot.toolb

(self, view, origin=ORIGIN, **kwargs)

Source from the content-addressed store, hash-verified

294 return s+'>'
295
296 def build(self, view, origin=ORIGIN, **kwargs):
297 """Draws the text on position (x, y). Draws a background rectangle and
298 / or frame if fill and / or stroke are defined.
299
300 >>> from pagebot.document import Document
301 >>> from pagebot.elements import *
302 >>> from pagebot.contexts import getContext
303 >>> from pagebot.toolbox.units import pt, em
304 >>> context = getContext()
305 >>> W, H = 500, 400
306 >>> doc = Document(w=W, h=H, context=context)
307 >>> page = doc[1]
308 >>> fontSize = pt(100)
309 >>> style = dict(font='PageBot-Regular', fontSize=fontSize, leading=fontSize, textFill=(1, 0, 0), xAlign=CENTER)
310 >>> bs = context.newString('Hkpx\\nHkpx', style)
311 >>> t = Text(bs, x=W/2, y=H/2, parent=page, showOrigin=True, fill=0.9, yAlign=MIDDLE)
312 >>> l = Line(x=t.x-t.w/2, y=t.y, w=t.w, h=0, stroke=(0, 0, 0.5), parent=page)
313 >>> l = Line(x=t.x-t.w/2, y=t.y+t.bs.capHeight, w=t.w, h=0, stroke=(0, 0, 0.5), parent=page)
314 >>> l = Line(x=t.x-t.w/2, y=t.y+t.bs.ascender, w=t.w, h=0, stroke=(0, 0, 0.5), parent=page)
315 >>> l = Line(x=t.x-t.w/2, y=t.y+t.bs.xHeight, w=t.w, h=0, stroke=(0, 0, 0.5), parent=page)
316 >>> l = Line(x=t.x-t.w/2, y=t.y+t.bs.descender, w=t.w, h=0, stroke=(0, 0, 0.5), parent=page)
317 >>> doc.export('_export/Text-build1.pdf')
318
319 >>> W, H = 700, 100
320 >>> doc = Document(w=W, h=H, context=context)
321 >>> view = doc.view
322 >>> view.showOrigin = True
323 >>> view.padding = pt(30)
324 >>> view.showCropMarks = True
325 >>> view.showFrame = True
326 >>> style = dict(font='PageBot-Regular', leading=em(1), fontSize=pt(18), textFill=(0, 0, 0.5), xAlign=CENTER)
327 >>> txt = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit valim mecto trambor.'
328 >>> bs = context.newString(txt, style) # Creates a BabelString with context reference.
329 >>> bs.context is context
330 True
331 >>> t = Text(bs, x=W/2, y=H/2, parent=doc[1], yAlign=MIDDLE_X)
332 >>> t.context is context
333 True
334 >>> doc.export('_export/Text-build2.pdf')
335 """
336 if not self.bs:
337 return
338
339 context = view.context # Get current context
340
341 # Stores scaled x-value first; text is already aligned.
342 p = pointOffset(self.origin, origin)
343 x, _, _, = self._applyScale(view, p)
344
345 # Determines frame position.
346 p = self.getPosition(view, origin)
347 px, py, _ = p
348
349 # TODO: Add Element clipping stuff here.
350 # FIXME: needs some restructuring, text box width and BabelString width
351 # can be different now.
352
353 # Let the view draw frame info for debugging, in case view.showFrame ==

Callers

nothing calls this directly

Calls 15

pointOffsetFunction · 0.90
_applyScaleMethod · 0.80
getPositionMethod · 0.80
buildFrameMethod · 0.80
drawMarginMethod · 0.80
_restoreRotationMethod · 0.80
_restoreScaleMethod · 0.80
drawMetaMethod · 0.80
drawElementFrameMethod · 0.45
fillMethod · 0.45
cssMethod · 0.45
strokeMethod · 0.45

Tested by

no test coverage detected