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

Method getTextLines

Lib/pagebot/contexts/flatcontext/flatcontext.py:758–801  ·  view source on GitHub ↗

Answer a list of BabeLineInfo instances. >>> from pagebot.toolbox.units import pt >>> context = FlatContext() >>> style = dict(font='PageBot-Regular', fontSize=pt(20)) >>> bs = context.newString('ABCD ' * 100, style, w=pt(200)) >>> lines = context.getTextLine

(self, bs, w=None, h=None, ascDesc=False)

Source from the content-addressed store, hash-verified

756 return pt(textWidth, textHeight)
757
758 def getTextLines(self, bs, w=None, h=None, ascDesc=False):
759 """Answer a list of BabeLineInfo instances.
760
761 >>> from pagebot.toolbox.units import pt
762 >>> context = FlatContext()
763 >>> style = dict(font='PageBot-Regular', fontSize=pt(20))
764 >>> bs = context.newString('ABCD ' * 100, style, w=pt(200))
765 >>> lines = context.getTextLines(bs)
766 >>> len(lines)
767 34
768 >>> line = lines[10]
769 >>> #line
770 #<BabelLineInfo y=246pt>
771 """
772 placedText = bs.cs.pt
773 lines = []
774
775 if placedText.width != w or placedText.height != h:
776 # Make reflow on this new (w, h)
777 placedText.frame(0, 0, w or bs.w or math.inf, h or bs.h or math.inf)
778 x = pt(0) # Relative vertical position
779 y = None
780
781 # In Flat "run" is native data for line.
782 flatRuns = placedText.layout.runs()
783
784 for height, flatLine in flatRuns:
785 height = round(height)
786 if y is None:
787 y = height
788 else:
789 y += height
790
791 babelLineInfo = BabelLineInfo(x, y, context=self, cLine=flatLine)
792
793 for fst, s in flatLine:
794 font = findFont(fst.font.name.decode("utf-8"))
795 style = dict(font=font, fontSize=pt(fst.size), leading=pt(fst.leading),
796 textFill=color(fst.color), tracking=pt(fst.tracking * fst.size))
797 babelRunInfo = BabelRunInfo(s, style, self, cRun=fst) # Cache the flatStyle, just in case.
798 babelLineInfo.runs.append(babelRunInfo)
799 lines.append(babelLineInfo)
800
801 return lines
802
803 def fromBabelString(self, bs):
804 """Convert the "public" data in BabelString to FlatStringData instance

Callers

nothing calls this directly

Calls 7

ptFunction · 0.90
BabelLineInfoClass · 0.90
findFontFunction · 0.90
colorFunction · 0.90
BabelRunInfoClass · 0.90
frameMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected