Skip to content

Commit de7e074

Browse files
fmalitaSkia Commit-Bot
authored andcommitted
[skottie] Shaper::VAlign::kResizeToFit should also scale line height
When fitting the shape result to a box, scale both the font size and line height by the same factor. This produces more intuitive results. Bug: skia:9129 Change-Id: I742a952b9615216a2b68c0432b41026751099cbc Reviewed-on: https://skia-review.googlesource.com/c/skia/+/216220 Reviewed-by: Mike Reed <[email protected]> Commit-Queue: Florin Malita <[email protected]>
1 parent f06b6d5 commit de7e074

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

modules/skottie/src/text/SkottieShaper.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ Shaper::Result ShapeToFit(const SkString& txt, const Shaper::TextDesc& orig_desc
219219
auto desc = orig_desc;
220220
desc.fVAlign = Shaper::VAlign::kCenter;
221221

222-
float in_size = 0, // maximum size that fits inside
223-
out_size = std::numeric_limits<float>::max(), // minimum size that doesn't fit
224-
try_size = desc.fTextSize; // current probe
222+
float in_scale = 0, // maximum scale that fits inside
223+
out_scale = std::numeric_limits<float>::max(), // minimum scale that doesn't fit
224+
try_scale = 1; // current probe
225225

226226
// Perform a binary search for the best vertical fit (SkShaper already handles
227227
// horizontal fitting), starting with the specified text size.
@@ -230,17 +230,18 @@ Shaper::Result ShapeToFit(const SkString& txt, const Shaper::TextDesc& orig_desc
230230
// exponential search for the extremes.
231231
static constexpr size_t kMaxIter = 16;
232232
for (size_t i = 0; i < kMaxIter; ++i) {
233-
SkASSERT(try_size >= in_size && try_size <= out_size);
234-
desc.fTextSize = try_size;
233+
SkASSERT(try_scale >= in_scale && try_scale <= out_scale);
234+
desc.fTextSize = try_scale * orig_desc.fTextSize;
235+
desc.fLineHeight = try_scale * orig_desc.fLineHeight;
235236

236237
auto res = ShapeImpl(txt, desc, box);
237238
auto res_height = res.computeBounds().height();
238239

239240
if (res_height > box.height()) {
240-
out_size = try_size;
241-
try_size = (in_size == 0)
242-
? try_size * 0.5f // initial in_size not found yet - search exponentially
243-
: (in_size + out_size) * 0.5f; // in_size found - binary search
241+
out_scale = try_scale;
242+
try_scale = (in_scale == 0)
243+
? try_scale * 0.5f // initial in_scale not found yet - search exponentially
244+
: (in_scale + out_scale) * 0.5f; // in_scale found - binary search
244245
} else {
245246
// It fits - so it's a candidate.
246247
best_result = res;
@@ -250,10 +251,10 @@ Shaper::Result ShapeToFit(const SkString& txt, const Shaper::TextDesc& orig_desc
250251
break;
251252
}
252253

253-
in_size = try_size;
254-
try_size = (out_size == std::numeric_limits<float>::max())
255-
? try_size * 2 // initial out_size not found yet - search exponentially
256-
: (in_size + out_size) * 0.5f; // out_size found - binary search
254+
in_scale = try_scale;
255+
try_scale = (out_scale == std::numeric_limits<float>::max())
256+
? try_scale * 2 // initial out_scale not found yet - search exponentially
257+
: (in_scale + out_scale) * 0.5f; // out_scale found - binary search
257258
}
258259
}
259260

0 commit comments

Comments
 (0)