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

Method textSize

Lib/pagebot/contexts/flatcontext/flatcontext.py:718–756  ·  view source on GitHub ↗

Determines text size based on placed text value.

(self, bs, w=None, h=None, align=None, ascDesc=True)

Source from the content-addressed store, hash-verified

716 return s
717
718 def textSize(self, bs, w=None, h=None, align=None, ascDesc=True):
719 """Determines text size based on placed text value."""
720 textWidth = 0.0
721 textHeight = 0.0
722 lastDescender = 0.0
723 placedText = bs.cs.pt
724
725 # Reflow to new (w, h). Otherwise use the layout.runs as already stored
726 # by placedText.
727 if placedText.width != w or placedText.height != h:
728 w = w or bs.w or math.inf
729 h = h or bs.h or math.inf
730 assert w, h
731 placedText.frame(0, 0, w, h)
732
733 flatRuns = placedText.layout.runs()
734 style = None
735
736 for rIndex, (height, run) in enumerate(placedText.layout.runs()):
737 runWidth = 0
738
739 for style, s in run:
740 runWidth += style.width(s)
741
742 textWidth = max(textWidth, runWidth)
743
744 if ascDesc:
745 if rIndex == 0 and style:
746 textHeight += style.ascender()
747 else:
748 textHeight += height
749 elif style:
750 textHeight += style.leading
751
752 if ascDesc and style:
753 # NOTE: Descender is a negative value.
754 textHeight -= style.descender()
755
756 return pt(textWidth, textHeight)
757
758 def getTextLines(self, bs, w=None, h=None, ascDesc=False):
759 """Answer a list of BabeLineInfo instances.

Callers 1

_placeMethod · 0.95

Calls 4

ptFunction · 0.90
frameMethod · 0.80
ascenderMethod · 0.80
descenderMethod · 0.80

Tested by

no test coverage detected