| 72 | } |
| 73 | |
| 74 | TextLayout CreateTextLayout(const TextDocument* textDocument, |
| 75 | const std::vector<GlyphHandle>& glyphList) { |
| 76 | TextLayout layout = {}; |
| 77 | auto isVertical = textDocument->direction == TextDirection::Vertical; |
| 78 | if (!textDocument->boxText) { |
| 79 | layout.boxRect = tgfx::Rect::MakeEmpty(); |
| 80 | if (isVertical) { |
| 81 | layout.coordinateMatrix.setRotate(90); |
| 82 | } |
| 83 | } else { |
| 84 | if (isVertical) { |
| 85 | auto boxRight = textDocument->boxTextPos.x + textDocument->boxTextSize.x; |
| 86 | layout.boxRect = tgfx::Rect::MakeWH(textDocument->boxTextSize.y, textDocument->boxTextSize.x); |
| 87 | layout.firstBaseLine = boxRight - textDocument->firstBaseLine; |
| 88 | layout.coordinateMatrix.setRotate(90); |
| 89 | layout.coordinateMatrix.postTranslate(boxRight, textDocument->boxTextPos.y); |
| 90 | } else { |
| 91 | layout.boxRect = tgfx::Rect::MakeWH(textDocument->boxTextSize.x, textDocument->boxTextSize.y); |
| 92 | layout.firstBaseLine = textDocument->firstBaseLine - textDocument->boxTextPos.y; |
| 93 | layout.coordinateMatrix.setTranslate(textDocument->boxTextPos.x, textDocument->boxTextPos.y); |
| 94 | } |
| 95 | } |
| 96 | layout.lineGap = textDocument->leading == 0 ? roundf(textDocument->fontSize * LINE_GAP_FACTOR) |
| 97 | : textDocument->leading; |
| 98 | layout.tracking = roundf(textDocument->tracking * textDocument->fontSize * 0.001f); |
| 99 | layout.baselineShift = textDocument->baselineShift; |
| 100 | layout.justification = textDocument->justification; |
| 101 | |
| 102 | float minAscent = 0, maxDescent = 0; |
| 103 | for (auto& glyph : glyphList) { |
| 104 | minAscent = std::min(minAscent, glyph->getAscent()); |
| 105 | maxDescent = std::max(maxDescent, glyph->getDescent()); |
| 106 | } |
| 107 | // 以 ascent 和 descent 比例计算出行高的上下位置。 |
| 108 | auto lineHeight = textDocument->fontSize * LINE_GAP_FACTOR; |
| 109 | layout.fontBottom = (maxDescent / (maxDescent - minAscent)) * lineHeight; |
| 110 | layout.fontTop = layout.fontBottom - lineHeight; |
| 111 | return layout; |
| 112 | } |
| 113 | |
| 114 | static size_t CalculateNextLineIndex(const std::vector<GlyphInfo>& glyphList, size_t index, |
| 115 | float scaleFactor, float maxWidth, float tracking, |
no test coverage detected