Determines text size based on placed text value.
(self, bs, w=None, h=None, align=None, ascDesc=True)
| 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. |