1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "Minikin"
18 #define ATRACE_TAG ATRACE_TAG_VIEW
19 
20 #include "minikin/LayoutCore.h"
21 
22 #include <hb-icu.h>
23 #include <hb-ot.h>
24 #include <log/log.h>
25 #include <unicode/ubidi.h>
26 #include <unicode/utf16.h>
27 #include <utils/LruCache.h>
28 #include <utils/Trace.h>
29 
30 #include <cmath>
31 #include <iostream>
32 #include <mutex>
33 #include <set>
34 #include <string>
35 #include <vector>
36 
37 #include "BidiUtils.h"
38 #include "LayoutUtils.h"
39 #include "LetterSpacingUtils.h"
40 #include "LocaleListCache.h"
41 #include "MinikinInternal.h"
42 #include "ScriptUtils.h"
43 #include "minikin/Emoji.h"
44 #include "minikin/FontFeature.h"
45 #include "minikin/HbUtils.h"
46 #include "minikin/LayoutCache.h"
47 #include "minikin/LayoutPieces.h"
48 #include "minikin/Macros.h"
49 
50 namespace minikin {
51 
52 namespace {
53 
54 struct SkiaArguments {
55     const MinikinFont* font;
56     const MinikinPaint* paint;
57     FontFakery fakery;
58 };
59 
60 // Returns true if the character needs to be excluded for the line spacing.
isLineSpaceExcludeChar(uint16_t c)61 inline bool isLineSpaceExcludeChar(uint16_t c) {
62     return c == CHAR_LINE_FEED || c == CHAR_CARRIAGE_RETURN;
63 }
64 
harfbuzzGetGlyphHorizontalAdvance(hb_font_t *,void * fontData,hb_codepoint_t glyph,void *)65 static hb_position_t harfbuzzGetGlyphHorizontalAdvance(hb_font_t* /* hbFont */, void* fontData,
66                                                        hb_codepoint_t glyph, void* /* userData */) {
67     SkiaArguments* args = reinterpret_cast<SkiaArguments*>(fontData);
68     float advance = args->font->GetHorizontalAdvance(glyph, *args->paint, args->fakery);
69     return 256 * advance + 0.5;
70 }
71 
harfbuzzGetGlyphHorizontalAdvances(hb_font_t *,void * fontData,unsigned int count,const hb_codepoint_t * first_glyph,unsigned glyph_stride,hb_position_t * first_advance,unsigned advance_stride,void *)72 static void harfbuzzGetGlyphHorizontalAdvances(hb_font_t* /* hbFont */, void* fontData,
73                                                unsigned int count,
74                                                const hb_codepoint_t* first_glyph,
75                                                unsigned glyph_stride, hb_position_t* first_advance,
76                                                unsigned advance_stride, void* /* userData */) {
77     SkiaArguments* args = reinterpret_cast<SkiaArguments*>(fontData);
78     std::vector<uint16_t> glyphVec(count);
79     std::vector<float> advVec(count);
80 
81     const hb_codepoint_t* glyph = first_glyph;
82     for (uint32_t i = 0; i < count; ++i) {
83         glyphVec[i] = *glyph;
84         glyph = reinterpret_cast<const hb_codepoint_t*>(reinterpret_cast<const uint8_t*>(glyph) +
85                                                         glyph_stride);
86     }
87 
88     args->font->GetHorizontalAdvances(glyphVec.data(), count, *args->paint, args->fakery,
89                                       advVec.data());
90 
91     hb_position_t* advances = first_advance;
92     for (uint32_t i = 0; i < count; ++i) {
93         *advances = HBFloatToFixed(advVec[i]);
94         advances = reinterpret_cast<hb_position_t*>(reinterpret_cast<uint8_t*>(advances) +
95                                                     advance_stride);
96     }
97 }
98 
harfbuzzGetGlyphHorizontalOrigin(hb_font_t *,void *,hb_codepoint_t,hb_position_t *,hb_position_t *,void *)99 static hb_bool_t harfbuzzGetGlyphHorizontalOrigin(hb_font_t* /* hbFont */, void* /* fontData */,
100                                                   hb_codepoint_t /* glyph */,
101                                                   hb_position_t* /* x */, hb_position_t* /* y */,
102                                                   void* /* userData */) {
103     // Just return true, following the way that Harfbuzz-FreeType implementation does.
104     return true;
105 }
106 
getFontFuncs()107 hb_font_funcs_t* getFontFuncs() {
108     static hb_font_funcs_t* fontFuncs = nullptr;
109     static std::once_flag once;
110     std::call_once(once, [&]() {
111         fontFuncs = hb_font_funcs_create();
112         // Override the h_advance function since we can't use HarfBuzz's implemenation. It may
113         // return the wrong value if the font uses hinting aggressively.
114         hb_font_funcs_set_glyph_h_advance_func(fontFuncs, harfbuzzGetGlyphHorizontalAdvance, 0, 0);
115         hb_font_funcs_set_glyph_h_advances_func(fontFuncs, harfbuzzGetGlyphHorizontalAdvances, 0,
116                                                 0);
117         hb_font_funcs_set_glyph_h_origin_func(fontFuncs, harfbuzzGetGlyphHorizontalOrigin, 0, 0);
118         hb_font_funcs_make_immutable(fontFuncs);
119     });
120     return fontFuncs;
121 }
122 
getFontFuncsForEmoji()123 hb_font_funcs_t* getFontFuncsForEmoji() {
124     static hb_font_funcs_t* fontFuncs = nullptr;
125     static std::once_flag once;
126     std::call_once(once, [&]() {
127         fontFuncs = hb_font_funcs_create();
128         // Don't override the h_advance function since we use HarfBuzz's implementation for emoji
129         // for performance reasons.
130         // Note that it is technically possible for a TrueType font to have outline and embedded
131         // bitmap at the same time. We ignore modified advances of hinted outline glyphs in that
132         // case.
133         hb_font_funcs_set_glyph_h_origin_func(fontFuncs, harfbuzzGetGlyphHorizontalOrigin, 0, 0);
134         hb_font_funcs_make_immutable(fontFuncs);
135     });
136     return fontFuncs;
137 }
138 
isColorBitmapFont(const HbFontUniquePtr & font)139 static bool isColorBitmapFont(const HbFontUniquePtr& font) {
140     HbBlob cbdt(font, HB_TAG('C', 'B', 'D', 'T'));
141     return cbdt;
142 }
143 
determineHyphenChar(hb_codepoint_t preferredHyphen,hb_font_t * font)144 static inline hb_codepoint_t determineHyphenChar(hb_codepoint_t preferredHyphen, hb_font_t* font) {
145     hb_codepoint_t glyph;
146     if (preferredHyphen == 0x058A    /* ARMENIAN_HYPHEN */
147         || preferredHyphen == 0x05BE /* HEBREW PUNCTUATION MAQAF */
148         || preferredHyphen == 0x1400 /* CANADIAN SYLLABIC HYPHEN */) {
149         if (hb_font_get_nominal_glyph(font, preferredHyphen, &glyph)) {
150             return preferredHyphen;
151         } else {
152             // The original hyphen requested was not supported. Let's try and see if the
153             // Unicode hyphen is supported.
154             preferredHyphen = CHAR_HYPHEN;
155         }
156     }
157     if (preferredHyphen == CHAR_HYPHEN) { /* HYPHEN */
158         // Fallback to ASCII HYPHEN-MINUS if the font didn't have a glyph for the preferred hyphen.
159         // Note that we intentionally don't do anything special if the font doesn't have a
160         // HYPHEN-MINUS either, so a tofu could be shown, hinting towards something missing.
161         if (!hb_font_get_nominal_glyph(font, preferredHyphen, &glyph)) {
162             return 0x002D;  // HYPHEN-MINUS
163         }
164     }
165     return preferredHyphen;
166 }
167 
168 template <typename HyphenEdit>
addHyphenToHbBuffer(const HbBufferUniquePtr & buffer,const HbFontUniquePtr & font,HyphenEdit hyphen,uint32_t cluster)169 static inline void addHyphenToHbBuffer(const HbBufferUniquePtr& buffer, const HbFontUniquePtr& font,
170                                        HyphenEdit hyphen, uint32_t cluster) {
171     auto [chars, size] = getHyphenString(hyphen);
172     for (size_t i = 0; i < size; i++) {
173         hb_buffer_add(buffer.get(), determineHyphenChar(chars[i], font.get()), cluster);
174     }
175 }
176 
177 // Returns the cluster value assigned to the first codepoint added to the buffer, which can be used
178 // to translate cluster values returned by HarfBuzz to input indices.
addToHbBuffer(const HbBufferUniquePtr & buffer,const uint16_t * buf,size_t start,size_t count,size_t bufSize,ssize_t scriptRunStart,ssize_t scriptRunEnd,StartHyphenEdit inStartHyphen,EndHyphenEdit inEndHyphen,const HbFontUniquePtr & hbFont)179 static inline uint32_t addToHbBuffer(const HbBufferUniquePtr& buffer, const uint16_t* buf,
180                                      size_t start, size_t count, size_t bufSize,
181                                      ssize_t scriptRunStart, ssize_t scriptRunEnd,
182                                      StartHyphenEdit inStartHyphen, EndHyphenEdit inEndHyphen,
183                                      const HbFontUniquePtr& hbFont) {
184     // Only hyphenate the very first script run for starting hyphens.
185     const StartHyphenEdit startHyphen =
186             (scriptRunStart == 0) ? inStartHyphen : StartHyphenEdit::NO_EDIT;
187     // Only hyphenate the very last script run for ending hyphens.
188     const EndHyphenEdit endHyphen =
189             (static_cast<size_t>(scriptRunEnd) == count) ? inEndHyphen : EndHyphenEdit::NO_EDIT;
190 
191     // In the following code, we drop the pre-context and/or post-context if there is a
192     // hyphen edit at that end. This is not absolutely necessary, since HarfBuzz uses
193     // contexts only for joining scripts at the moment, e.g. to determine if the first or
194     // last letter of a text range to shape should take a joining form based on an
195     // adjacent letter or joiner (that comes from the context).
196     //
197     // TODO: Revisit this for:
198     // 1. Desperate breaks for joining scripts like Arabic (where it may be better to keep
199     //    the context);
200     // 2. Special features like start-of-word font features (not implemented in HarfBuzz
201     //    yet).
202 
203     // We don't have any start-of-line replacement edit yet, so we don't need to check for
204     // those.
205     if (isInsertion(startHyphen)) {
206         // A cluster value of zero guarantees that the inserted hyphen will be in the same
207         // cluster with the next codepoint, since there is no pre-context.
208         addHyphenToHbBuffer(buffer, hbFont, startHyphen, 0 /* cluster */);
209     }
210 
211     const uint16_t* hbText;
212     int hbTextLength;
213     unsigned int hbItemOffset;
214     unsigned int hbItemLength = scriptRunEnd - scriptRunStart;  // This is >= 1.
215 
216     const bool hasEndInsertion = isInsertion(endHyphen);
217     const bool hasEndReplacement = isReplacement(endHyphen);
218     if (hasEndReplacement) {
219         // Skip the last code unit while copying the buffer for HarfBuzz if it's a replacement. We
220         // don't need to worry about non-BMP characters yet since replacements are only done for
221         // code units at the moment.
222         hbItemLength -= 1;
223     }
224 
225     if (startHyphen == StartHyphenEdit::NO_EDIT) {
226         // No edit at the beginning. Use the whole pre-context.
227         hbText = buf;
228         hbItemOffset = start + scriptRunStart;
229     } else {
230         // There's an edit at the beginning. Drop the pre-context and start the buffer at where we
231         // want to start shaping.
232         hbText = buf + start + scriptRunStart;
233         hbItemOffset = 0;
234     }
235 
236     if (endHyphen == EndHyphenEdit::NO_EDIT) {
237         // No edit at the end, use the whole post-context.
238         hbTextLength = (buf + bufSize) - hbText;
239     } else {
240         // There is an edit at the end. Drop the post-context.
241         hbTextLength = hbItemOffset + hbItemLength;
242     }
243 
244     hb_buffer_add_utf16(buffer.get(), hbText, hbTextLength, hbItemOffset, hbItemLength);
245 
246     unsigned int numCodepoints;
247     hb_glyph_info_t* cpInfo = hb_buffer_get_glyph_infos(buffer.get(), &numCodepoints);
248 
249     // Add the hyphen at the end, if there's any.
250     if (hasEndInsertion || hasEndReplacement) {
251         // When a hyphen is inserted, by assigning the added hyphen and the last
252         // codepoint added to the HarfBuzz buffer to the same cluster, we can make sure
253         // that they always remain in the same cluster, even if the last codepoint gets
254         // merged into another cluster (for example when it's a combining mark).
255         //
256         // When a replacement happens instead, we want it to get the cluster value of
257         // the character it's replacing, which is one "codepoint length" larger than
258         // the last cluster. But since the character replaced is always just one
259         // code unit, we can just add 1.
260         uint32_t hyphenCluster;
261         if (numCodepoints == 0) {
262             // Nothing was added to the HarfBuzz buffer. This can only happen if
263             // we have a replacement that is replacing a one-code unit script run.
264             hyphenCluster = 0;
265         } else {
266             hyphenCluster = cpInfo[numCodepoints - 1].cluster + (uint32_t)hasEndReplacement;
267         }
268         addHyphenToHbBuffer(buffer, hbFont, endHyphen, hyphenCluster);
269         // Since we have just added to the buffer, cpInfo no longer necessarily points to
270         // the right place. Refresh it.
271         cpInfo = hb_buffer_get_glyph_infos(buffer.get(), nullptr /* we don't need the size */);
272     }
273     return cpInfo[0].cluster;
274 }
275 
276 }  // namespace
277 
LayoutPiece(const U16StringPiece & textBuf,const Range & range,bool isRtl,const MinikinPaint & paint,StartHyphenEdit startHyphen,EndHyphenEdit endHyphen)278 LayoutPiece::LayoutPiece(const U16StringPiece& textBuf, const Range& range, bool isRtl,
279                          const MinikinPaint& paint, StartHyphenEdit startHyphen,
280                          EndHyphenEdit endHyphen) {
281     const uint16_t* buf = textBuf.data();
282     const size_t start = range.getStart();
283     const size_t count = range.getLength();
284     const size_t bufSize = textBuf.size();
285 
286     mAdvances.resize(count, 0);  // Need zero filling.
287 
288     // Usually the number of glyphs are less than number of code units.
289     mFontIndices.reserve(count);
290     mGlyphIds.reserve(count);
291     mPoints.reserve(count);
292     mClusters.reserve(count);
293 
294     HbBufferUniquePtr buffer(hb_buffer_create());
295     U16StringPiece substr = textBuf.substr(range);
296     std::vector<FontCollection::Run> items =
297             paint.font->itemize(substr, paint.fontStyle, paint.localeListId, paint.familyVariant);
298 
299     std::vector<hb_feature_t> features = cleanAndAddDefaultFontFeatures(paint);
300 
301     std::vector<HbFontUniquePtr> hbFonts;
302     double size = paint.size;
303     double scaleX = paint.scaleX;
304 
305     std::unordered_map<const MinikinFont*, uint32_t> fontMap;
306 
307     float x = 0;
308     float y = 0;
309 
310     constexpr uint32_t MAX_LENGTH_FOR_BITSET = 256;  // std::bit_ceil(CHAR_LIMIT_FOR_CACHE);
311     std::bitset<MAX_LENGTH_FOR_BITSET> clusterSet;
312     std::set<uint32_t> clusterSetForLarge;
313     const bool useLargeSet = count >= MAX_LENGTH_FOR_BITSET;
314 
315     for (int run_ix = isRtl ? items.size() - 1 : 0;
316          isRtl ? run_ix >= 0 : run_ix < static_cast<int>(items.size());
317          isRtl ? --run_ix : ++run_ix) {
318         FontCollection::Run& run = items[run_ix];
319         FakedFont fakedFont = paint.font->getBestFont(substr, run, paint.fontStyle);
320         const std::shared_ptr<MinikinFont>& typeface = fakedFont.typeface();
321         auto it = fontMap.find(typeface.get());
322         uint8_t font_ix;
323         if (it == fontMap.end()) {
324             // First time to see this font.
325             font_ix = mFonts.size();
326             mFonts.push_back(fakedFont);
327             fontMap.insert(std::make_pair(typeface.get(), font_ix));
328 
329             // We override some functions which are not thread safe.
330             HbFontUniquePtr font(hb_font_create_sub_font(fakedFont.hbFont().get()));
331             hb_font_set_funcs(
332                     font.get(), isColorBitmapFont(font) ? getFontFuncsForEmoji() : getFontFuncs(),
333                     new SkiaArguments({fakedFont.typeface().get(), &paint, fakedFont.fakery}),
334                     [](void* data) { delete reinterpret_cast<SkiaArguments*>(data); });
335             hbFonts.push_back(std::move(font));
336         } else {
337             font_ix = it->second;
338         }
339         const HbFontUniquePtr& hbFont = hbFonts[font_ix];
340 
341         bool needExtent = false;
342         for (int i = run.start; i < run.end; ++i) {
343             if (!isLineSpaceExcludeChar(buf[i])) {
344                 needExtent = true;
345                 break;
346             }
347         }
348         if (needExtent) {
349             MinikinExtent verticalExtent;
350             typeface->GetFontExtent(&verticalExtent, paint, fakedFont.fakery);
351             mExtent.extendBy(verticalExtent);
352         }
353 
354         hb_font_set_ppem(hbFont.get(), size * scaleX, size);
355         hb_font_set_scale(hbFont.get(), HBFloatToFixed(size * scaleX), HBFloatToFixed(size));
356 
357         // TODO: if there are multiple scripts within a font in an RTL run,
358         // we need to reorder those runs. This is unlikely with our current
359         // font stack, but should be done for correctness.
360 
361         // Note: scriptRunStart and scriptRunEnd, as well as run.start and run.end, run between 0
362         // and count.
363         for (const auto [range, script] : ScriptText(textBuf, run.start, run.end)) {
364             ssize_t scriptRunStart = range.getStart();
365             ssize_t scriptRunEnd = range.getEnd();
366 
367             // After the last line, scriptRunEnd is guaranteed to have increased, since the only
368             // time getScriptRun does not increase its iterator is when it has already reached the
369             // end of the buffer. But that can't happen, since if we have already reached the end
370             // of the buffer, we should have had (scriptRunEnd == run.end), which means
371             // (scriptRunStart == run.end) which is impossible due to the exit condition of the for
372             // loop. So we can be sure that scriptRunEnd > scriptRunStart.
373 
374             double letterSpace = 0.0;
375             double letterSpaceHalf = 0.0;
376 
377             if (paint.letterSpacing != 0.0 && isLetterSpacingCapableScript(script)) {
378                 letterSpace = paint.letterSpacing * size * scaleX;
379                 letterSpaceHalf = letterSpace * 0.5;
380             }
381 
382             hb_buffer_clear_contents(buffer.get());
383             hb_buffer_set_script(buffer.get(), script);
384             hb_buffer_set_direction(buffer.get(), isRtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
385             const LocaleList& localeList = LocaleListCache::getById(paint.localeListId);
386             if (localeList.size() != 0) {
387                 hb_language_t hbLanguage = localeList.getHbLanguage(0);
388                 for (size_t i = 0; i < localeList.size(); ++i) {
389                     if (localeList[i].supportsScript(hb_script_to_iso15924_tag(script))) {
390                         hbLanguage = localeList.getHbLanguage(i);
391                         break;
392                     }
393                 }
394                 hb_buffer_set_language(buffer.get(), hbLanguage);
395             }
396 
397             const uint32_t clusterStart =
398                     addToHbBuffer(buffer, buf, start, count, bufSize, scriptRunStart, scriptRunEnd,
399                                   startHyphen, endHyphen, hbFont);
400 
401             hb_shape(hbFont.get(), buffer.get(), features.empty() ? NULL : &features[0],
402                      features.size());
403             unsigned int numGlyphs;
404             hb_glyph_info_t* info = hb_buffer_get_glyph_infos(buffer.get(), &numGlyphs);
405             hb_glyph_position_t* positions = hb_buffer_get_glyph_positions(buffer.get(), NULL);
406 
407             // At this point in the code, the cluster values in the info buffer correspond to the
408             // input characters with some shift. The cluster value clusterStart corresponds to the
409             // first character passed to HarfBuzz, which is at buf[start + scriptRunStart] whose
410             // advance needs to be saved into mAdvances[scriptRunStart]. So cluster values need to
411             // be reduced by (clusterStart - scriptRunStart) to get converted to indices of
412             // mAdvances.
413             const ssize_t clusterOffset = clusterStart - scriptRunStart;
414 
415             if (numGlyphs && letterSpace != 0) {
416                 const uint32_t advIndex = info[0].cluster - clusterOffset;
417                 const uint32_t cp = textBuf.codePointAt(advIndex + start);
418                 if (!u_iscntrl(cp)) {
419                     mAdvances[advIndex] += letterSpaceHalf;
420                     x += letterSpaceHalf;
421                 }
422             }
423             for (unsigned int i = 0; i < numGlyphs; i++) {
424                 const size_t clusterBaseIndex = info[i].cluster - clusterOffset;
425                 if (letterSpace != 0 && i > 0 && info[i - 1].cluster != info[i].cluster) {
426                     const uint32_t prevAdvIndex = info[i - 1].cluster - clusterOffset;
427                     const uint32_t prevCp = textBuf.codePointAt(prevAdvIndex + start);
428                     const uint32_t cp = textBuf.codePointAt(clusterBaseIndex + start);
429 
430                     const bool isCtrl = u_iscntrl(cp);
431                     const bool isPrevCtrl = u_iscntrl(prevCp);
432                     if (!isPrevCtrl) {
433                         mAdvances[prevAdvIndex] += letterSpaceHalf;
434                     }
435 
436                     if (!isCtrl) {
437                         mAdvances[clusterBaseIndex] += letterSpaceHalf;
438                     }
439 
440                     // To avoid rounding error, add full letter spacing when the both prev and
441                     // current code point are non-control characters.
442                     if (!isCtrl && !isPrevCtrl) {
443                         x += letterSpace;
444                     } else if (!isCtrl || !isPrevCtrl) {
445                         x += letterSpaceHalf;
446                     }
447                 }
448 
449                 hb_codepoint_t glyph_ix = info[i].codepoint;
450                 float xoff = HBFixedToFloat(positions[i].x_offset);
451                 float yoff = -HBFixedToFloat(positions[i].y_offset);
452                 xoff += yoff * paint.skewX;
453                 mFontIndices.push_back(font_ix);
454                 mGlyphIds.push_back(glyph_ix);
455                 mPoints.emplace_back(x + xoff, y + yoff);
456                 float xAdvance = HBFixedToFloat(positions[i].x_advance);
457                 mClusters.push_back(clusterBaseIndex);
458                 if (useLargeSet) {
459                     clusterSetForLarge.insert(clusterBaseIndex);
460                 } else {
461                     clusterSet.set(clusterBaseIndex);
462                 }
463 
464                 if (clusterBaseIndex < count) {
465                     mAdvances[clusterBaseIndex] += xAdvance;
466                 } else {
467                     ALOGE("cluster %zu (start %zu) out of bounds of count %zu", clusterBaseIndex,
468                           start, count);
469                 }
470                 x += xAdvance;
471             }
472             if (numGlyphs && letterSpace != 0) {
473                 const uint32_t lastAdvIndex = info[numGlyphs - 1].cluster - clusterOffset;
474                 const uint32_t lastCp = textBuf.codePointAt(lastAdvIndex + start);
475                 if (!u_iscntrl(lastCp)) {
476                     mAdvances[lastAdvIndex] += letterSpaceHalf;
477                     x += letterSpaceHalf;
478                 }
479             }
480         }
481     }
482     mFontIndices.shrink_to_fit();
483     mGlyphIds.shrink_to_fit();
484     mPoints.shrink_to_fit();
485     mClusters.shrink_to_fit();
486     mAdvance = x;
487     if (useLargeSet) {
488         mClusterCount = clusterSetForLarge.size();
489     } else {
490         mClusterCount = clusterSet.count();
491     }
492 }
493 
494 // static
calculateBounds(const LayoutPiece & layout,const MinikinPaint & paint)495 MinikinRect LayoutPiece::calculateBounds(const LayoutPiece& layout, const MinikinPaint& paint) {
496     ATRACE_CALL();
497     MinikinRect out;
498     for (uint32_t i = 0; i < layout.glyphCount(); ++i) {
499         MinikinRect bounds;
500         uint32_t glyphId = layout.glyphIdAt(i);
501         const FakedFont& fakedFont = layout.fontAt(i);
502         const Point& pos = layout.pointAt(i);
503 
504         fakedFont.typeface()->GetBounds(&bounds, glyphId, paint, fakedFont.fakery);
505         out.join(bounds, pos);
506     }
507     return out;
508 }
509 
~LayoutPiece()510 LayoutPiece::~LayoutPiece() {}
511 
512 }  // namespace minikin
513