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