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

Method _place

Lib/pagebot/contexts/flatcontext/flatcontext.py:551–599  ·  view source on GitHub ↗

Places the styled Flat text on a page, transform vertical position to position on baseline. Vertical alignment should to be handled by the calling function, already calculated in the `y`. Horizontal alignment is taken from the bs.xTextAlign value, as answered by the style of

(self, bs, x, y, w=None, h=None)

Source from the content-addressed store, hash-verified

549 return self._flatFonts[font.path], font
550
551 def _place(self, bs, x, y, w=None, h=None):
552 """Places the styled Flat text on a page, transform vertical position
553 to position on baseline. Vertical alignment should to be handled by the
554 calling function, already calculated in the `y`. Horizontal alignment is taken
555 from the bs.xTextAlign value, as answered by the style of the first
556 run: bs.runs[0].style.get('xTextAlign')
557
558 TODO: implement runs instead of native context string.
559
560 for run in bs.runs:
561 flatFont, font = self._getFlatFont(run.style.get('font'))
562 fontSize = run.style.get('fontSize', DEFAULT_FONT_SIZE)
563 ascender = fontSize * font.info.typoAscender / font.info.unitsPerEm
564 leading = upt(run.style.get('leading',em(1.2)), base=fontSize)
565 textColor = color(run.style.get('textFill', blackColor))
566 flatColor = rgb(*self._asFlatColor(textColor))
567 strike = self.b.strike(flatFont).size(fontSize, leading).color(flatColor)
568 spans.append(strike.span(run.s))
569 maxAscender = max(maxAscender, ascender)
570 maxFontSize = max(maxFontSize, fontSize)
571
572 #paragraphs = [self.b.paragraph(spans)]
573 #placedText = self.page.place(self.b.text(paragraphs))
574 #placedText.frame(x, y-maxAscender+200, placedText.width, fontSize)
575 """
576 spans = []
577 maxAscender = 0
578 maxFontSize = 0
579 placedText = self.page.place(bs.cs.txt)
580
581 # If the style of the first BabelString run, contains a xTextAlign
582 # for CENTER or RIGHT, the shift the x position accordingly.
583 xTextAlign = bs.xTextAlign
584
585 if xTextAlign == CENTER:
586 x -= bs.tw / 2
587 elif xTextAlign == RIGHT:
588 x -= bs.tw
589
590 # Vertical alignment is handled by the calling function, already calculated in
591 # the `y`.
592
593 x = x.pt
594 y = y.pt
595 tw, th = self.textSize(bs, w=w, h=h, ascDesc=True)
596 w = w or tw
597 h = h or th
598 r = 1.01 # Counter rounding error.
599 placedText.frame(x, y - h, w * r, h * r)
600
601 def _asFlatColor(self, pbColor):
602 # Make this dependent on type of export.

Callers 2

textMethod · 0.95
textBoxMethod · 0.95

Calls 3

textSizeMethod · 0.95
placeMethod · 0.80
frameMethod · 0.80

Tested by

no test coverage detected