Skip to content

Commit 90bfd1c

Browse files
RusinoSkia Commit-Bot
authored andcommitted
Small changes
Change-Id: Ic9c41a2dd11b4df8ab24037df0109e36536ec6c3 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/257892 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Julia Lavrova <jlavrova@google.com>
1 parent 3f7c49c commit 90bfd1c

File tree

8 files changed

+206
-81
lines changed

8 files changed

+206
-81
lines changed

modules/skparagraph/include/TextStyle.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,13 @@ struct Block {
243243
Block(size_t start, size_t end, const TextStyle& style) : fRange(start, end), fStyle(style) {}
244244
Block(TextRange textRange, const TextStyle& style) : fRange(textRange), fStyle(style) {}
245245

246-
Block(const Block& other) {
247-
fRange = other.fRange;
248-
fStyle = other.fStyle;
249-
}
246+
Block(const Block& other) : fRange(other.fRange), fStyle(other.fStyle) {}
250247

251248
void add(TextRange tail) {
252249
SkASSERT(fRange.end == tail.start);
253250
fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width());
254251
}
252+
255253
TextRange fRange;
256254
TextStyle fStyle;
257255
};
@@ -273,13 +271,12 @@ struct Placeholder {
273271
, fBlocksBefore(blocksBefore)
274272
, fTextBefore(textBefore) {}
275273

276-
Placeholder(const Placeholder& other) {
277-
fRange = other.fRange;
278-
fStyle = other.fStyle;
279-
fTextStyle = other.fTextStyle;
280-
fBlocksBefore = other.fBlocksBefore;
281-
fTextBefore = other.fTextBefore;
282-
}
274+
Placeholder(const Placeholder& other)
275+
: fRange(other.fRange)
276+
, fStyle(other.fStyle)
277+
, fTextStyle(other.fTextStyle)
278+
, fBlocksBefore(other.fBlocksBefore)
279+
, fTextBefore(other.fTextBefore) {}
283280

284281
TextRange fRange;
285282
PlaceholderStyle fStyle;

modules/skparagraph/src/OneLineShaper.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ void OneLineShaper::sortOutGlyphs(std::function<void(GlyphRange)>&& sortOutUnres
339339
void OneLineShaper::iterateThroughFontStyles(SkSpan<Block> styleSpan,
340340
const ShapeSingleFontVisitor& visitor) {
341341
Block combinedBlock;
342+
342343
for (auto& block : styleSpan) {
343344
SkASSERT(combinedBlock.fRange.width() == 0 ||
344345
combinedBlock.fRange.end == block.fRange.start);
@@ -460,11 +461,12 @@ bool OneLineShaper::shape() {
460461
}
461462

462463
iterateThroughFontStyles(styleSpan,
463-
[this, &shaper, textDirection, limitlessWidth, &advanceX](Block block) {
464+
[this, &shaper, textDirection, limitlessWidth, &advanceX]
465+
(Block block) {
464466
auto blockSpan = SkSpan<Block>(&block, 1);
465467

466468
// Start from the beginning (hoping that it's a simple case one block - one run)
467-
fHeight = block.fStyle.getHeight();
469+
fHeight = block.fStyle.getHeightOverride() ? block.fStyle.getHeight() : 0;
468470
fAdvance = SkVector::Make(advanceX, 0);
469471
fCurrentText = block.fRange;
470472
fUnresolvedBlocks.emplace(RunBlock(block.fRange));
@@ -495,8 +497,9 @@ bool OneLineShaper::shape() {
495497
}
496498

497499
fCurrentText = unresolvedRange;
498-
shaper->shape(unresolvedText.begin(), unresolvedText.size(), fontIter, *bidi,
499-
*script, lang, limitlessWidth, this);
500+
shaper->shape(unresolvedText.begin(), unresolvedText.size(),
501+
fontIter, *bidi,*script, lang,
502+
limitlessWidth, this);
500503

501504
// Take off the queue the block we tried to resolved -
502505
// whatever happened, we have now smaller pieces of it to deal with
@@ -507,7 +510,7 @@ bool OneLineShaper::shape() {
507510
return !fUnresolvedBlocks.empty();
508511
});
509512

510-
this->finish(block.fRange, block.fStyle.getHeight(), advanceX);
513+
this->finish(block.fRange, fHeight, advanceX);
511514
});
512515

513516
return true;

modules/skparagraph/src/ParagraphImpl.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ PositionWithAffinity ParagraphImpl::getGlyphPositionAtCoordinate(SkScalar dx, Sk
887887
return false;
888888
}
889889

890-
if (dx >= context.clip.fRight) {
890+
if (dx >= context.clip.fRight + offsetX) {
891891
// We have to keep looking but just in case keep the last one as the closes
892892
// so far
893893
auto index = context.pos + context.size;
@@ -912,7 +912,7 @@ PositionWithAffinity ParagraphImpl::getGlyphPositionAtCoordinate(SkScalar dx, Sk
912912
}
913913
found = i;
914914
}
915-
auto glyphStart = context.run->positionX(found);
915+
auto glyphStart = context.run->positionX(found) + context.fTextShift + offsetX;
916916
auto glyphWidth = context.run->positionX(found + 1) - context.run->positionX(found);
917917
auto clusterIndex8 = context.run->fClusterIndexes[found];
918918

@@ -932,14 +932,11 @@ PositionWithAffinity ParagraphImpl::getGlyphPositionAtCoordinate(SkScalar dx, Sk
932932
auto averageCodepoint = glyphWidth / graphemeSize;
933933
auto codepointStart = glyphStart + averageCodepoint * (codepointIndex - codepoints.start);
934934
auto codepointEnd = codepointStart + averageCodepoint;
935-
center = (codepointStart + codepointEnd) / 2 + context.fTextShift;
935+
center = (codepointStart + codepointEnd) / 2;
936936
} else {
937937
SkASSERT(graphemeSize == 1);
938-
auto codepointStart = glyphStart;
939-
auto codepointEnd = codepointStart + glyphWidth;
940-
center = (codepointStart + codepointEnd) / 2 + context.fTextShift;
938+
center = glyphStart + glyphWidth / 2;
941939
}
942-
943940
if ((dx < center) == context.run->leftToRight()) {
944941
result = { SkToS32(codepointIndex), kDownstream };
945942
} else {
@@ -1092,15 +1089,25 @@ void ParagraphImpl::setState(InternalState state) {
10921089
}
10931090

10941091
void ParagraphImpl::computeEmptyMetrics() {
1095-
auto defaultTextStyle = paragraphStyle().getTextStyle();
1092+
auto defaultTextStyle = paragraphStyle().getTextStyle();
10961093

1097-
auto typefaces = fontCollection()->findTypefaces(
1094+
auto typefaces = fontCollection()->findTypefaces(
10981095
defaultTextStyle.getFontFamilies(), defaultTextStyle.getFontStyle());
1099-
auto typeface = typefaces.size() ? typefaces.front() : nullptr;
1096+
auto typeface = typefaces.size() ? typefaces.front() : nullptr;
1097+
1098+
SkFont font(typeface, defaultTextStyle.getFontSize());
1099+
1100+
fEmptyMetrics = InternalLineMetrics(font, paragraphStyle().getStrutStyle().getForceStrutHeight());
1101+
if (!paragraphStyle().getStrutStyle().getForceStrutHeight() &&
1102+
defaultTextStyle.getHeightOverride()) {
1103+
auto multiplier =
1104+
defaultTextStyle.getHeight() * defaultTextStyle.getFontSize() / fEmptyMetrics.height();
1105+
fEmptyMetrics = InternalLineMetrics(fEmptyMetrics.ascent() * multiplier,
1106+
fEmptyMetrics.descent() * multiplier,
1107+
fEmptyMetrics.leading() * multiplier);
1108+
}
11001109

1101-
SkFont font(typeface, defaultTextStyle.getFontSize());
1102-
fEmptyMetrics = InternalLineMetrics(font, paragraphStyle().getStrutStyle().getForceStrutHeight());
1103-
fStrutMetrics.updateLineMetrics(fEmptyMetrics);
1110+
fStrutMetrics.updateLineMetrics(fEmptyMetrics);
11041111
}
11051112

11061113
void ParagraphImpl::updateText(size_t from, SkString text) {

modules/skparagraph/src/ParagraphImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ class ParagraphImpl final : public Paragraph {
199199
InternalLineMetrics getEmptyMetrics() const { return fEmptyMetrics; }
200200
InternalLineMetrics getStrutMetrics() const { return fStrutMetrics; }
201201

202+
BlockRange findAllBlocks(TextRange textRange);
203+
202204
private:
203205
friend class ParagraphBuilder;
204206
friend class ParagraphCacheKey;
@@ -209,7 +211,6 @@ class ParagraphImpl final : public Paragraph {
209211
friend class OneLineShaper;
210212

211213
void calculateBoundaries(ClusterRange clusters, SkVector offset, SkVector advance);
212-
BlockRange findAllBlocks(TextRange textRange);
213214
void extractStyles();
214215

215216
void markGraphemes16();

modules/skparagraph/src/TextStyle.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ bool TextStyle::equals(const TextStyle& other) const {
8484
if (fTextShadows.size() != other.fTextShadows.size()) {
8585
return false;
8686
}
87-
88-
for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) {
87+
for (size_t i = 0; i < fTextShadows.size(); ++i) {
8988
if (fTextShadows[i] != other.fTextShadows[i]) {
9089
return false;
9190
}
@@ -134,7 +133,6 @@ bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) c
134133
// TODO: should not we take typefaces in account?
135134
return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
136135
fFontSize == other.fFontSize && fHeight == other.fHeight;
137-
138136
default:
139137
SkASSERT(false);
140138
return false;

modules/skparagraph/src/TextWrapper.cpp

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters) {
3535
break;
3636
}
3737
if (cluster->width() > maxWidth) {
38-
// Break the cluster into parts by glyph position
39-
auto delta = maxWidth - (fWords.width() + fClusters.width());
40-
fClip.extend(cluster, cluster->roundPos(delta));
38+
fClusters.extend(cluster);
4139
fTooLongCluster = true;
4240
fTooLongWord = true;
4341
break;
@@ -53,11 +51,9 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters) {
5351
}
5452
if (nextWordLength > maxWidth) {
5553
// If the word is too long we can break it right now and hope it's enough
54+
fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, nextWordLength);
5655
fTooLongWord = true;
5756
}
58-
59-
// TODO: this is the place when we use hyphenation
60-
fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, fTooLongWord ? maxWidth : nextWordLength);
6157
break;
6258
}
6359

@@ -83,8 +79,10 @@ void TextWrapper::moveForward() {
8379
} else if (fClusters.width() > 0) {
8480
fEndLine.extend(fClusters);
8581
fTooLongWord = false;
82+
fTooLongCluster = false;
8683
} else if (fClip.width() > 0 || (fTooLongWord && fTooLongCluster)) {
87-
fEndLine.extend(fClip);
84+
// Flutter: forget the clipped cluster but keep the metrics
85+
fEndLine.metrics().add(fClip.metrics());
8886
fTooLongWord = false;
8987
fTooLongCluster = false;
9088
} else {
@@ -159,8 +157,8 @@ void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
159157
SkScalar maxWidth,
160158
const AddLineToParagraph& addLine) {
161159
fHeight = 0;
162-
fMinIntrinsicWidth = 0;
163-
fMaxIntrinsicWidth = 0;
160+
fMinIntrinsicWidth = std::numeric_limits<SkScalar>::min();
161+
fMaxIntrinsicWidth = std::numeric_limits<SkScalar>::min();
164162

165163
auto span = parent->clusters();
166164
if (span.size() == 0) {
@@ -268,16 +266,35 @@ void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
268266
}
269267

270268
// We finished formatting the text but we need to scan the rest for some numbers
271-
auto cluster = fEndLine.endCluster();
272-
while (cluster != end) {
273-
fExceededMaxLines = true;
274-
if (cluster->isHardBreak()) {
275-
softLineMaxIntrinsicWidth = 0;
276-
} else {
277-
softLineMaxIntrinsicWidth += cluster->width();
278-
fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
269+
if (fEndLine.breakCluster() != nullptr) {
270+
auto lastWordLength = 0.0f;
271+
auto cluster = fEndLine.breakCluster();
272+
if (cluster != end) {
273+
++cluster;
279274
}
280-
++cluster;
275+
while (cluster != end || cluster->endPos() < end->endPos()) {
276+
fExceededMaxLines = true;
277+
if (cluster->isHardBreak()) {
278+
fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
279+
softLineMaxIntrinsicWidth = 0;
280+
281+
fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
282+
lastWordLength = 0;
283+
} else if (cluster->isWhitespaces()) {
284+
SkASSERT(cluster->isWhitespaces());
285+
softLineMaxIntrinsicWidth += cluster->width();
286+
fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
287+
lastWordLength = 0;
288+
} else {
289+
softLineMaxIntrinsicWidth += cluster->width();
290+
lastWordLength += cluster->width();
291+
}
292+
++cluster;
293+
}
294+
fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
295+
fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
296+
// In case we could not place a single cluster on the line
297+
fHeight = SkTMax(fHeight, fEndLine.metrics().height());
281298
}
282299

283300
if (fHardLineBreak) {

samplecode/SampleParagraph.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,15 +1647,13 @@ class ParagraphView19 : public ParagraphView_Base {
16471647

16481648
auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false, true);
16491649

1650-
const char* text = "World domination is such an ugly phrase - I prefer to call it world optimisation";
1650+
std::u16string text = u"\u0068\u0301\u0350\u0312\u0357\u030C\u0369\u0305\u036C\u0304\u0310\u033F\u0366\u0350\u0343\u0364\u0369\u0311\u0309\u030E\u0365\u031B\u0340\u0337\u0335\u035E\u0334\u0328\u0360\u0360\u0315\u035F\u0340\u0340\u0362\u0360\u0322\u031B\u031B\u0337\u0340\u031E\u031F\u032A\u0331\u0345\u032F\u0332\u032E\u0333\u0353\u0320\u0345\u031C\u031F\u033C\u0325\u0355\u032C\u0325\u033Aa\u0307\u0312\u034B\u0308\u0312\u0346\u0313\u0346\u0304\u0307\u0344\u0305\u0342\u0368\u0346\u036A\u035B\u030F\u0365\u0307\u0340\u0328\u0322\u0361\u0489\u034F\u0328\u0334\u035F\u0335\u0362\u0489\u0360\u0358\u035E\u0360\u035D\u0341\u0337\u0337\u032E\u0326\u032D\u0359\u0318\u033C\u032F\u0333\u035A\u034D\u0319\u031C\u0353\u033C\u0345\u0359\u0331\u033B\u0331\u033C";
16511651
ParagraphStyle paragraph_style;
1652-
paragraph_style.setMaxLines(7);
1653-
paragraph_style.setEllipsis(u"\u2026");
16541652
ParagraphBuilderImpl builder(paragraph_style, fontCollection);
16551653
TextStyle text_style;
16561654
text_style.setColor(SK_ColorBLACK);
16571655
text_style.setFontFamilies({SkString("Roboto")});
1658-
text_style.setFontSize(40);
1656+
text_style.setFontSize(20);
16591657
builder.pushStyle(text_style);
16601658
builder.addText(text);
16611659
auto paragraph = builder.Build();
@@ -1677,19 +1675,20 @@ class ParagraphView20 : public ParagraphView_Base {
16771675

16781676
auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false, true);
16791677

1680-
const char* text = "";
1678+
const char* text = "0";
16811679
ParagraphStyle paragraph_style;
16821680
paragraph_style.setMaxLines(std::numeric_limits<size_t>::max());
1683-
//paragraph_style.setEllipsis(u"\u2026");
16841681
ParagraphBuilderImpl builder(paragraph_style, fontCollection);
16851682
TextStyle text_style;
16861683
text_style.setColor(SK_ColorBLACK);
1687-
text_style.setFontFamilies({SkString("Roboto")});
1688-
text_style.setFontSize(40);
1684+
text_style.setFontFamilies({SkString("Google Sans Display")});
1685+
text_style.setFontSize(160);
1686+
//text_style.setHeightOverride(true);
1687+
//text_style.setHeight(1.75);
16891688
builder.pushStyle(text_style);
16901689
builder.addText(text);
16911690
auto paragraph = builder.Build();
1692-
paragraph->layout(this->width());
1691+
paragraph->layout(94);
16931692

16941693
paragraph->paint(canvas, 0, 0);
16951694
}

0 commit comments

Comments
 (0)