Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f700549

Browse files
RusinoSkia Commit-Bot
authored andcommitted
Keeping run metrics rather than calculating them every time
Change-Id: Ic0257859317cf46dbb90277573f184d565fdb58d Reviewed-on: https://skia-review.googlesource.com/c/skia/+/295322 Reviewed-by: Ben Wagner <[email protected]> Reviewed-by: Mike Reed <[email protected]> Commit-Queue: Julia Lavrova <[email protected]>
1 parent fb5ede5 commit f700549

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

modules/skparagraph/src/ParagraphImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ void ParagraphImpl::buildClusterTable() {
540540
run.setClusterRange(runStart, fClusters.size());
541541
fMaxIntrinsicWidth += run.advance().fX;
542542
}
543-
fClustersIndexFromCodeUnit[fText.size()] = fClusters.size();
543+
fClustersIndexFromCodeUnit[fText.size()] = fClusters.size();
544544
fClusters.emplace_back(this, EMPTY_RUN, 0, 0, this->text({fText.size(), fText.size()}), 0, 0);
545545
}
546546

modules/skparagraph/src/Run.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ namespace textlayout {
1717
Run::Run(ParagraphImpl* master,
1818
const SkShaper::RunHandler::RunInfo& info,
1919
size_t firstChar,
20-
SkScalar lineHeight,
20+
SkScalar heightMultiplier,
2121
size_t index,
2222
SkScalar offsetX)
2323
: fMaster(master)
2424
, fTextRange(firstChar + info.utf8Range.begin(), firstChar + info.utf8Range.end())
2525
, fClusterRange(EMPTY_CLUSTERS)
2626
, fFont(info.fFont)
2727
, fClusterStart(firstChar)
28-
, fHeightMultiplier(lineHeight)
28+
, fHeightMultiplier(heightMultiplier)
2929
{
3030
fBidiLevel = info.fBidiLevel;
3131
fAdvance = info.fAdvance;
@@ -38,6 +38,9 @@ Run::Run(ParagraphImpl* master,
3838
fClusterIndexes.push_back_n(info.glyphCount + 1);
3939
fShifts.push_back_n(info.glyphCount + 1, 0.0);
4040
info.fFont.getMetrics(&fFontMetrics);
41+
42+
this->calculateMetrics();
43+
4144
fSpaced = false;
4245
// To make edge cases easier:
4346
fPositions[info.glyphCount] = fOffset + fAdvance;
@@ -46,6 +49,18 @@ Run::Run(ParagraphImpl* master,
4649
fPlaceholderIndex = std::numeric_limits<size_t>::max();
4750
}
4851

52+
void Run::calculateMetrics() {
53+
fCorrectAscent = fFontMetrics.fAscent - fFontMetrics.fLeading * 0.5;
54+
fCorrectDescent = fFontMetrics.fDescent + fFontMetrics.fLeading * 0.5;
55+
fCorrectLeading = 0;
56+
if (!SkScalarNearlyZero(fHeightMultiplier)) {
57+
auto multiplier = fHeightMultiplier * fFont.getSize() /
58+
(fFontMetrics.fDescent - fFontMetrics.fAscent + fFontMetrics.fLeading);
59+
fCorrectAscent *= multiplier;
60+
fCorrectDescent *= multiplier;
61+
}
62+
}
63+
4964
SkShaper::RunHandler::Buffer Run::newRunBuffer() {
5065
return {fGlyphs.data(), fPositions.data(), nullptr, fClusterIndexes.data(), fOffset};
5166
}
@@ -267,6 +282,8 @@ void Run::updateMetrics(InternalLineMetrics* endlineMetrics) {
267282
break;
268283
}
269284

285+
this->calculateMetrics();
286+
270287
// Make sure the placeholder can fit the line
271288
endlineMetrics->add(this);
272289
}

modules/skparagraph/src/Run.h

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Run {
6060
Run(ParagraphImpl* master,
6161
const SkShaper::RunHandler::RunInfo& info,
6262
size_t firstChar,
63-
SkScalar lineHeight,
63+
SkScalar heightMultiplier,
6464
size_t index,
6565
SkScalar shiftX);
6666
Run(const Run&) = default;
@@ -84,36 +84,15 @@ class Run {
8484
fOffset.fY += shiftY;
8585
}
8686
SkVector advance() const {
87-
return SkVector::Make(fAdvance.fX, fFontMetrics.fDescent - fFontMetrics.fAscent);
87+
return SkVector::Make(fAdvance.fX, fFontMetrics.fDescent - fFontMetrics.fAscent + fFontMetrics.fLeading);
8888
}
8989
SkVector offset() const { return fOffset; }
9090
SkScalar ascent() const { return fFontMetrics.fAscent; }
9191
SkScalar descent() const { return fFontMetrics.fDescent; }
9292
SkScalar leading() const { return fFontMetrics.fLeading; }
93-
SkScalar correctAscent() const {
94-
95-
if (fHeightMultiplier == 0) {
96-
return fFontMetrics.fAscent - fFontMetrics.fLeading / 2;
97-
}
98-
return fFontMetrics.fAscent * fHeightMultiplier * fFont.getSize() /
99-
(fFontMetrics.fDescent - fFontMetrics.fAscent + fFontMetrics.fLeading / 2);
100-
}
101-
SkScalar correctDescent() const {
102-
103-
if (fHeightMultiplier == 0) {
104-
return fFontMetrics.fDescent + fFontMetrics.fLeading / 2;
105-
}
106-
return fFontMetrics.fDescent * fHeightMultiplier * fFont.getSize() /
107-
(fFontMetrics.fDescent - fFontMetrics.fAscent + fFontMetrics.fLeading / 2);
108-
}
109-
SkScalar correctLeading() const {
110-
111-
if (fHeightMultiplier == 0) {
112-
return fFontMetrics.fAscent;
113-
}
114-
return fFontMetrics.fLeading * fHeightMultiplier * fFont.getSize() /
115-
(fFontMetrics.fDescent - fFontMetrics.fAscent + fFontMetrics.fLeading);
116-
}
93+
SkScalar correctAscent() const { return fCorrectAscent; }
94+
SkScalar correctDescent() const { return fCorrectDescent; }
95+
SkScalar correctLeading() const { return fCorrectLeading; }
11796
const SkFont& font() const { return fFont; }
11897
bool leftToRight() const { return fBidiLevel % 2 == 0; }
11998
TextDirection getTextDirection() const { return leftToRight() ? TextDirection::kLtr : TextDirection::kRtl; }
@@ -132,6 +111,7 @@ class Run {
132111

133112
bool isEllipsis() const { return fEllipsis; }
134113

114+
void calculateMetrics();
135115
void updateMetrics(InternalLineMetrics* endlineMetrics);
136116

137117
void setClusterRange(size_t from, size_t to) { fClusterRange = ClusterRange(from, to); }
@@ -217,6 +197,9 @@ class Run {
217197

218198
SkFontMetrics fFontMetrics;
219199
const SkScalar fHeightMultiplier;
200+
SkScalar fCorrectAscent;
201+
SkScalar fCorrectDescent;
202+
SkScalar fCorrectLeading;
220203

221204
bool fSpaced;
222205
bool fEllipsis;

modules/skparagraph/src/TextWrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class TextWrapper {
7575
fStart = ClusterPos(cluster, cluster->startPos());
7676
}
7777
fEnd = ClusterPos(cluster, cluster->endPos());
78+
// TODO: Make sure all the checks are correct and there are no unnecessary checks
7879
if (!cluster->run()->isPlaceholder()) {
7980
fMetrics.add(cluster->run());
8081
}

0 commit comments

Comments
 (0)