19cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien/*
29cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Copyright (C) 2013 The Android Open Source Project
39cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
49cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Licensed under the Apache License, Version 2.0 (the "License");
59cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * you may not use this file except in compliance with the License.
69cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * You may obtain a copy of the License at
79cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
89cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *      http://www.apache.org/licenses/LICENSE-2.0
99cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
109cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Unless required by applicable law or agreed to in writing, software
119cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * distributed under the License is distributed on an "AS IS" BASIS,
129cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * See the License for the specific language governing permissions and
149cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * limitations under the License.
159cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien */
169cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
17ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien#define LOG_TAG "Minikin"
18ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien#include <cutils/log.h>
19ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
2069d4fba2f2b7bb2c248cc0e78cf277a6e44665f8Dan Albert#include <math.h>
2169d4fba2f2b7bb2c248cc0e78cf277a6e44665f8Dan Albert
22d231a4b0b1d482c7ae7717b048112e1fe5d0f5a9Raph Levien#include <algorithm>
239cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <fstream>
249cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <iostream>  // for debugging
2569d4fba2f2b7bb2c248cc0e78cf277a6e44665f8Dan Albert#include <string>
2669d4fba2f2b7bb2c248cc0e78cf277a6e44665f8Dan Albert#include <vector>
279cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
284d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien#include <utils/JenkinsHash.h>
294d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien#include <utils/LruCache.h>
304d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien#include <utils/Singleton.h>
314d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien#include <utils/String16.h>
324d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
33d231a4b0b1d482c7ae7717b048112e1fe5d0f5a9Raph Levien#include <unicode/ubidi.h>
34ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien#include <hb-icu.h>
352a79f59e73294e43f32cc0138e23fcde34eec28aSeigo Nonaka#include <hb-ot.h>
36ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
37198b46f1fea3f47ef8eb6317799c0d77aaec52f6Seigo Nonaka#include "FontLanguage.h"
386d9dcd2cf3d3ed26a886e02d94c907311e7b1f83Seigo Nonaka#include "FontLanguageListCache.h"
394e3adc6fb2073d5b466b88b8f5329d281038aba1Seigo Nonaka#include "LayoutUtils.h"
4089e80237bc27af084c9ff316d4f47abf426eced8Seigo Nonaka#include "HbFontCache.h"
41b80c1f19c58b927820a8a24bf2218e5645724608Raph Levien#include "MinikinInternal.h"
42bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien#include <minikin/MinikinFontFreeType.h>
439cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <minikin/Layout.h>
449cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
459cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienusing std::string;
469cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienusing std::vector;
479cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
488920e81717c6e51b92ff8f4479a1f959af260556John Recknamespace minikin {
498920e81717c6e51b92ff8f4479a1f959af260556John Reck
508920e81717c6e51b92ff8f4479a1f959af260556John ReckBitmap::Bitmap(int width, int height) : width(width), height(height) {
518920e81717c6e51b92ff8f4479a1f959af260556John Reck    buf = new uint8_t[width * height]();
528920e81717c6e51b92ff8f4479a1f959af260556John Reck}
538920e81717c6e51b92ff8f4479a1f959af260556John Reck
548920e81717c6e51b92ff8f4479a1f959af260556John ReckBitmap::~Bitmap() {
558920e81717c6e51b92ff8f4479a1f959af260556John Reck    delete[] buf;
568920e81717c6e51b92ff8f4479a1f959af260556John Reck}
578920e81717c6e51b92ff8f4479a1f959af260556John Reck
588920e81717c6e51b92ff8f4479a1f959af260556John Reckvoid Bitmap::writePnm(std::ofstream &o) const {
598920e81717c6e51b92ff8f4479a1f959af260556John Reck    o << "P5" << std::endl;
608920e81717c6e51b92ff8f4479a1f959af260556John Reck    o << width << " " << height << std::endl;
618920e81717c6e51b92ff8f4479a1f959af260556John Reck    o << "255" << std::endl;
628920e81717c6e51b92ff8f4479a1f959af260556John Reck    o.write((const char *)buf, width * height);
638920e81717c6e51b92ff8f4479a1f959af260556John Reck    o.close();
648920e81717c6e51b92ff8f4479a1f959af260556John Reck}
658920e81717c6e51b92ff8f4479a1f959af260556John Reck
668920e81717c6e51b92ff8f4479a1f959af260556John Reckvoid Bitmap::drawGlyph(const android::GlyphBitmap& bitmap, int x, int y) {
678920e81717c6e51b92ff8f4479a1f959af260556John Reck    int bmw = bitmap.width;
688920e81717c6e51b92ff8f4479a1f959af260556John Reck    int bmh = bitmap.height;
698920e81717c6e51b92ff8f4479a1f959af260556John Reck    x += bitmap.left;
708920e81717c6e51b92ff8f4479a1f959af260556John Reck    y -= bitmap.top;
718920e81717c6e51b92ff8f4479a1f959af260556John Reck    int x0 = std::max(0, x);
728920e81717c6e51b92ff8f4479a1f959af260556John Reck    int x1 = std::min(width, x + bmw);
738920e81717c6e51b92ff8f4479a1f959af260556John Reck    int y0 = std::max(0, y);
748920e81717c6e51b92ff8f4479a1f959af260556John Reck    int y1 = std::min(height, y + bmh);
758920e81717c6e51b92ff8f4479a1f959af260556John Reck    const unsigned char* src = bitmap.buffer + (y0 - y) * bmw + (x0 - x);
768920e81717c6e51b92ff8f4479a1f959af260556John Reck    uint8_t* dst = buf + y0 * width;
778920e81717c6e51b92ff8f4479a1f959af260556John Reck    for (int yy = y0; yy < y1; yy++) {
788920e81717c6e51b92ff8f4479a1f959af260556John Reck        for (int xx = x0; xx < x1; xx++) {
798920e81717c6e51b92ff8f4479a1f959af260556John Reck            int pixel = (int)dst[xx] + (int)src[xx - x];
808920e81717c6e51b92ff8f4479a1f959af260556John Reck            pixel = pixel > 0xff ? 0xff : pixel;
818920e81717c6e51b92ff8f4479a1f959af260556John Reck            dst[xx] = pixel;
828920e81717c6e51b92ff8f4479a1f959af260556John Reck        }
838920e81717c6e51b92ff8f4479a1f959af260556John Reck        src += bmw;
848920e81717c6e51b92ff8f4479a1f959af260556John Reck        dst += width;
858920e81717c6e51b92ff8f4479a1f959af260556John Reck    }
868920e81717c6e51b92ff8f4479a1f959af260556John Reck}
878920e81717c6e51b92ff8f4479a1f959af260556John Reck
888920e81717c6e51b92ff8f4479a1f959af260556John Reck} // namespace minikin
898920e81717c6e51b92ff8f4479a1f959af260556John Reck
909cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Leviennamespace android {
919cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
9286fa46c5ebb0d2c3319e08f4fbf487d8c2abbbfcRaph Levienconst int kDirection_Mask = 0x1;
9386fa46c5ebb0d2c3319e08f4fbf487d8c2abbbfcRaph Levien
94288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbodstruct LayoutContext {
95288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    MinikinPaint paint;
96288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    FontStyle style;
97288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    std::vector<hb_font_t*> hbFonts;  // parallel to mFaces
98288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod
99288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    void clearHbFonts() {
100288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        for (size_t i = 0; i < hbFonts.size(); i++) {
10189e80237bc27af084c9ff316d4f47abf426eced8Seigo Nonaka            hb_font_set_funcs(hbFonts[i], nullptr, nullptr, nullptr);
102aaa4e3470270496e6eb80704eadecb2cb7c56bf0Raph Levien            hb_font_destroy(hbFonts[i]);
103288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        }
104288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        hbFonts.clear();
105288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    }
106288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod};
107288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod
1084d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien// Layout cache datatypes
1094d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1104d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienclass LayoutCacheKey {
1114d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienpublic:
1124d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    LayoutCacheKey(const FontCollection* collection, const MinikinPaint& paint, FontStyle style,
1134d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien            const uint16_t* chars, size_t start, size_t count, size_t nchars, bool dir)
114cb20a2f0b366bfc16db3a489a60156dec7a9fe21Andreas Gampe            : mChars(chars), mNchars(nchars),
115cb20a2f0b366bfc16db3a489a60156dec7a9fe21Andreas Gampe            mStart(start), mCount(count), mId(collection->getId()), mStyle(style),
116448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            mSize(paint.size), mScaleX(paint.scaleX), mSkewX(paint.skewX),
1178e7a3dae37e9a22b2c054aec852615843d71caf6Behdad Esfahbod            mLetterSpacing(paint.letterSpacing),
1189d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi            mPaintFlags(paint.paintFlags), mHyphenEdit(paint.hyphenEdit), mIsRtl(dir),
1199d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi            mHash(computeHash()) {
1204d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    }
1214d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    bool operator==(const LayoutCacheKey &other) const;
1229d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi
1239d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi    hash_t hash() const {
1249d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi        return mHash;
1259d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi    }
1264d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
127288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    void copyText() {
128288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        uint16_t* charsCopy = new uint16_t[mNchars];
129288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        memcpy(charsCopy, mChars, mNchars * sizeof(uint16_t));
130288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        mChars = charsCopy;
131288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    }
132288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    void freeText() {
133288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        delete[] mChars;
134288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        mChars = NULL;
135288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    }
136288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod
137288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    void doLayout(Layout* layout, LayoutContext* ctx, const FontCollection* collection) const {
138288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        layout->setFontCollection(collection);
139288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        layout->mAdvances.resize(mCount, 0);
140288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        ctx->clearHbFonts();
141288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        layout->doLayoutRun(mChars, mStart, mCount, mNchars, mIsRtl, ctx);
142288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    }
143288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod
1444d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienprivate:
145288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    const uint16_t* mChars;
146288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    size_t mNchars;
1474d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    size_t mStart;
1484d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    size_t mCount;
1494d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    uint32_t mId;  // for the font collection
1504d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    FontStyle mStyle;
1514d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    float mSize;
152448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien    float mScaleX;
153448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien    float mSkewX;
1548e7a3dae37e9a22b2c054aec852615843d71caf6Behdad Esfahbod    float mLetterSpacing;
155893399732b64203133c22ad3c1f3535e46bf35a7Raph Levien    int32_t mPaintFlags;
1566c4d167bff33c24c239d77ddb1044b18d180766aRaph Levien    HyphenEdit mHyphenEdit;
1574d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    bool mIsRtl;
1584d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // Note: any fields added to MinikinPaint must also be reflected here.
1594d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // TODO: language matching (possibly integrate into style)
1609d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi    hash_t mHash;
1619d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi
1629d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagi    hash_t computeHash() const;
1634d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien};
1644d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1654d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienclass LayoutCache : private OnEntryRemoved<LayoutCacheKey, Layout*> {
1664d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienpublic:
1674d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    LayoutCache() : mCache(kMaxEntries) {
1684d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien        mCache.setOnEntryRemovedListener(this);
1694d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    }
1704d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
171288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    void clear() {
172288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        mCache.clear();
173288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    }
174288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod
175288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    Layout* get(LayoutCacheKey& key, LayoutContext* ctx, const FontCollection* collection) {
176288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        Layout* layout = mCache.get(key);
177288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        if (layout == NULL) {
178288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod            key.copyText();
179288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod            layout = new Layout();
180288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod            key.doLayout(layout, ctx, collection);
181288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod            mCache.put(key, layout);
182288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        }
183288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        return layout;
184288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    }
185288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod
186288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbodprivate:
1874d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // callback for OnEntryRemoved
1884d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    void operator()(LayoutCacheKey& key, Layout*& value) {
189288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod        key.freeText();
1904d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien        delete value;
1914d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    }
1924d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1934d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    LruCache<LayoutCacheKey, Layout*> mCache;
194288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod
1954d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    //static const size_t kMaxEntries = LruCache<LayoutCacheKey, Layout*>::kUnlimitedCapacity;
1964d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1974d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // TODO: eviction based on memory footprint; for now, we just use a constant
1984d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // number of strings
1994d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    static const size_t kMaxEntries = 5000;
2004d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien};
2014d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
2020bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournaderstatic unsigned int disabledDecomposeCompatibility(hb_unicode_funcs_t*, hb_codepoint_t,
2030bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader                                                   hb_codepoint_t*, void*) {
2040bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader    return 0;
2050bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader}
2060bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader
2074d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienclass LayoutEngine : public Singleton<LayoutEngine> {
2084d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienpublic:
2094d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    LayoutEngine() {
2100bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader        unicodeFunctions = hb_unicode_funcs_create(hb_icu_get_unicode_funcs());
2110bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader        /* Disable the function used for compatibility decomposition */
2120bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader        hb_unicode_funcs_set_decompose_compatibility_func(
2130bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader                unicodeFunctions, disabledDecomposeCompatibility, NULL, NULL);
2144d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien        hbBuffer = hb_buffer_create();
2150bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader        hb_buffer_set_unicode_funcs(hbBuffer, unicodeFunctions);
2164d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    }
2174d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
2184d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    hb_buffer_t* hbBuffer;
2190bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader    hb_unicode_funcs_t* unicodeFunctions;
2204d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    LayoutCache layoutCache;
2214d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien};
2224d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
2234d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph LevienANDROID_SINGLETON_STATIC_INSTANCE(LayoutEngine);
2244d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
2254d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienbool LayoutCacheKey::operator==(const LayoutCacheKey& other) const {
226448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien    return mId == other.mId
227448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            && mStart == other.mStart
228448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            && mCount == other.mCount
229448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            && mStyle == other.mStyle
230448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            && mSize == other.mSize
231448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            && mScaleX == other.mScaleX
232448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            && mSkewX == other.mSkewX
2338e7a3dae37e9a22b2c054aec852615843d71caf6Behdad Esfahbod            && mLetterSpacing == other.mLetterSpacing
234448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            && mPaintFlags == other.mPaintFlags
2356c4d167bff33c24c239d77ddb1044b18d180766aRaph Levien            && mHyphenEdit == other.mHyphenEdit
236448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien            && mIsRtl == other.mIsRtl
237288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod            && mNchars == other.mNchars
238288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod            && !memcmp(mChars, other.mChars, mNchars * sizeof(uint16_t));
2394d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien}
2404d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
2419d48271c0480c27402cfba359f45964637927a63Keisuke Kuroyanagihash_t LayoutCacheKey::computeHash() const {
2424d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    uint32_t hash = JenkinsHashMix(0, mId);
2434d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    hash = JenkinsHashMix(hash, mStart);
2444d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    hash = JenkinsHashMix(hash, mCount);
2454d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    hash = JenkinsHashMix(hash, hash_type(mStyle));
2464d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    hash = JenkinsHashMix(hash, hash_type(mSize));
247448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien    hash = JenkinsHashMix(hash, hash_type(mScaleX));
248448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien    hash = JenkinsHashMix(hash, hash_type(mSkewX));
2498e7a3dae37e9a22b2c054aec852615843d71caf6Behdad Esfahbod    hash = JenkinsHashMix(hash, hash_type(mLetterSpacing));
250448b0fd720d7ba902b9be224a287d08abe3ebea8Raph Levien    hash = JenkinsHashMix(hash, hash_type(mPaintFlags));
2516c4d167bff33c24c239d77ddb1044b18d180766aRaph Levien    hash = JenkinsHashMix(hash, hash_type(mHyphenEdit.hasHyphen()));
2524d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    hash = JenkinsHashMix(hash, hash_type(mIsRtl));
253288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    hash = JenkinsHashMixShorts(hash, mChars, mNchars);
2544d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    return JenkinsHashWhiten(hash);
2554d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien}
2564d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
2574d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienhash_t hash_type(const LayoutCacheKey& key) {
2584d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    return key.hash();
2594d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien}
2604d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
261ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levienvoid MinikinRect::join(const MinikinRect& r) {
262ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    if (isEmpty()) {
263ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        set(r);
264ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    } else if (!r.isEmpty()) {
265ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        mLeft = std::min(mLeft, r.mLeft);
266ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        mTop = std::min(mTop, r.mTop);
267ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        mRight = std::max(mRight, r.mRight);
268ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        mBottom = std::max(mBottom, r.mBottom);
269ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    }
270ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien}
271ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
2726da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod// Deprecated. Remove when callers are removed.
2739cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienvoid Layout::init() {
2749cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
2759cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
2766da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbodvoid Layout::reset() {
2776da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    mGlyphs.clear();
2786da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    mFaces.clear();
2796da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    mBounds.setEmpty();
2806da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    mAdvances.clear();
2816da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    mAdvance = 0;
2826da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod}
2836da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod
2844d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienvoid Layout::setFontCollection(const FontCollection* collection) {
2859cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    mCollection = collection;
2869cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
2879cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
288bae347682989d2627081310129a5b60541ed6ad0Seigo Nonakastatic hb_position_t harfbuzzGetGlyphHorizontalAdvance(hb_font_t* /* hbFont */, void* fontData,
289bae347682989d2627081310129a5b60541ed6ad0Seigo Nonaka        hb_codepoint_t glyph, void* /* userData */) {
2904d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    MinikinPaint* paint = reinterpret_cast<MinikinPaint*>(fontData);
291bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    MinikinFont* font = paint->font;
292bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    float advance = font->GetHorizontalAdvance(glyph, *paint);
293bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    return 256 * advance + 0.5;
2949cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
2959cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
296bae347682989d2627081310129a5b60541ed6ad0Seigo Nonakastatic hb_bool_t harfbuzzGetGlyphHorizontalOrigin(hb_font_t* /* hbFont */, void* /* fontData */,
297bae347682989d2627081310129a5b60541ed6ad0Seigo Nonaka        hb_codepoint_t /* glyph */, hb_position_t* /* x */, hb_position_t* /* y */,
298bae347682989d2627081310129a5b60541ed6ad0Seigo Nonaka        void* /* userData */) {
2999cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    // Just return true, following the way that Harfbuzz-FreeType
3009cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    // implementation does.
3019cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    return true;
3029cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
3039cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
3049cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienhb_font_funcs_t* getHbFontFuncs() {
3059cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    static hb_font_funcs_t* hbFontFuncs = 0;
3069cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
3079cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    if (hbFontFuncs == 0) {
3089cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        hbFontFuncs = hb_font_funcs_create();
3099cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        hb_font_funcs_set_glyph_h_advance_func(hbFontFuncs, harfbuzzGetGlyphHorizontalAdvance, 0, 0);
3109cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        hb_font_funcs_set_glyph_h_origin_func(hbFontFuncs, harfbuzzGetGlyphHorizontalOrigin, 0, 0);
3119cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        hb_font_funcs_make_immutable(hbFontFuncs);
3129cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    }
3139cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    return hbFontFuncs;
3149cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
3159cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
3169cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic float HBFixedToFloat(hb_position_t v)
3179cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien{
3189cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    return scalbnf (v, -8);
3199cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
3209cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
3219cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic hb_position_t HBFloatToFixed(float v)
3229cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien{
3239cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    return scalbnf (v, +8);
3249cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
3259cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
3269cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienvoid Layout::dump() const {
3279cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    for (size_t i = 0; i < mGlyphs.size(); i++) {
3289cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        const LayoutGlyph& glyph = mGlyphs[i];
3299cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        std::cout << glyph.glyph_id << ": " << glyph.x << ", " << glyph.y << std::endl;
3309cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    }
3319cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
3329cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
3339a5f713add8cfb91ac2c9ed5c917309053201ab6Raph Levienint Layout::findFace(FakedFont face, LayoutContext* ctx) {
3349cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    unsigned int ix;
3359cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    for (ix = 0; ix < mFaces.size(); ix++) {
3369a5f713add8cfb91ac2c9ed5c917309053201ab6Raph Levien        if (mFaces[ix].font == face.font) {
3379cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien            return ix;
3389cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        }
3399cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    }
3409cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    mFaces.push_back(face);
3414d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // Note: ctx == NULL means we're copying from the cache, no need to create
3424d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // corresponding hb_font object.
3434d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    if (ctx != NULL) {
34489e80237bc27af084c9ff316d4f47abf426eced8Seigo Nonaka        hb_font_t* font = getHbFontLocked(face.font);
34589e80237bc27af084c9ff316d4f47abf426eced8Seigo Nonaka        hb_font_set_funcs(font, getHbFontFuncs(), &ctx->paint, 0);
3464d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien        ctx->hbFonts.push_back(font);
3474d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    }
3489cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    return ix;
3499cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
3509cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
351ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levienstatic hb_script_t codePointToScript(hb_codepoint_t codepoint) {
3524d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    static hb_unicode_funcs_t* u = 0;
353ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    if (!u) {
3540bbff3a96d3836079371cdd4398c21afad3c5234Roozbeh Pournader        u = LayoutEngine::getInstance().unicodeFunctions;
355ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    }
356ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    return hb_unicode_script(u, codepoint);
357ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien}
358ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
3594d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienstatic hb_codepoint_t decodeUtf16(const uint16_t* chars, size_t len, ssize_t* iter) {
360ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    const uint16_t v = chars[(*iter)++];
361ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    // test whether v in (0xd800..0xdfff), lead or trail surrogate
362ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    if ((v & 0xf800) == 0xd800) {
363ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        // test whether v in (0xd800..0xdbff), lead surrogate
364ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        if (size_t(*iter) < len && (v & 0xfc00) == 0xd800) {
365ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            const uint16_t v2 = chars[(*iter)++];
366ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            // test whether v2 in (0xdc00..0xdfff), trail surrogate
367ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            if ((v2 & 0xfc00) == 0xdc00) {
368ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                // (0xd800 0xdc00) in utf-16 maps to 0x10000 in ucs-32
369ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                const hb_codepoint_t delta = (0xd800 << 10) + 0xdc00 - 0x10000;
370ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                return (((hb_codepoint_t)v) << 10) + v2 - delta;
371ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            }
372b8208bc19c178b73175f76323381a1eeb8059807Behdad Esfahbod            (*iter) -= 1;
373b8208bc19c178b73175f76323381a1eeb8059807Behdad Esfahbod            return 0xFFFDu;
374ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        } else {
375b8208bc19c178b73175f76323381a1eeb8059807Behdad Esfahbod            return 0xFFFDu;
376ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        }
377ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    } else {
378ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        return v;
379ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    }
380ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien}
381ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
3824d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienstatic hb_script_t getScriptRun(const uint16_t* chars, size_t len, ssize_t* iter) {
383ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    if (size_t(*iter) == len) {
384ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        return HB_SCRIPT_UNKNOWN;
385ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    }
386ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    uint32_t cp = decodeUtf16(chars, len, iter);
387ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    hb_script_t current_script = codePointToScript(cp);
388ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    for (;;) {
389ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        if (size_t(*iter) == len)
390ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            break;
391ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        const ssize_t prev_iter = *iter;
392ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        cp = decodeUtf16(chars, len, iter);
393ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        const hb_script_t script = codePointToScript(cp);
394ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        if (script != current_script) {
395ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            if (current_script == HB_SCRIPT_INHERITED ||
396ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                current_script == HB_SCRIPT_COMMON) {
397ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                current_script = script;
398ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            } else if (script == HB_SCRIPT_INHERITED ||
399ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                script == HB_SCRIPT_COMMON) {
400ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                continue;
401ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            } else {
402ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                *iter = prev_iter;
403ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien                break;
404ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien            }
405ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        }
406ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    }
407ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    if (current_script == HB_SCRIPT_INHERITED) {
408ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien        current_script = HB_SCRIPT_COMMON;
409ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    }
410ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
411ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    return current_script;
412ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien}
413ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
4144d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien/**
415e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien * Disable certain scripts (mostly those with cursive connection) from having letterspacing
416e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien * applied. See https://github.com/behdad/harfbuzz/issues/64 for more details.
417e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien */
418e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levienstatic bool isScriptOkForLetterspacing(hb_script_t script) {
419e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien    return !(
420e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_ARABIC ||
421e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_NKO ||
422e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_PSALTER_PAHLAVI ||
423e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_MANDAIC ||
424e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_MONGOLIAN ||
425e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_PHAGS_PA ||
426e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_DEVANAGARI ||
427e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_BENGALI ||
428e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_GURMUKHI ||
429e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_MODI ||
430e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_SHARADA ||
431e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_SYLOTI_NAGRI ||
432e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_TIRHUTA ||
433e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            script == HB_SCRIPT_OGHAM
434e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien            );
435e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien}
436e8721b2d293ba1a0e5d92a066aa9aa15807357ccRaph Levien
4376292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagiclass BidiText {
4386292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagipublic:
4396292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    class Iter {
4406292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    public:
4416292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        struct RunInfo {
4426292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi            int32_t mRunStart;
4436292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi            int32_t mRunLength;
4446292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi            bool mIsRtl;
4456292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        };
4466292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4476292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        Iter(UBiDi* bidi, size_t start, size_t end, size_t runIndex, size_t runCount, bool isRtl);
4486292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4496292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        bool operator!= (const Iter& other) const {
4506292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi            return mIsEnd != other.mIsEnd || mNextRunIndex != other.mNextRunIndex
4516292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi                    || mBidi != other.mBidi;
4526292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        }
4536292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4546292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        const RunInfo& operator* () const {
4556292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi            return mRunInfo;
4566292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        }
4576292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4586292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        const Iter& operator++ () {
4596292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi            updateRunInfo();
4606292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi            return *this;
4616292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        }
4626292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4636292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    private:
4646292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        UBiDi* const mBidi;
4656292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        bool mIsEnd;
4666292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        size_t mNextRunIndex;
4676292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        const size_t mRunCount;
4686292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        const int32_t mStart;
4696292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        const int32_t mEnd;
4706292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        RunInfo mRunInfo;
4716292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4726292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        void updateRunInfo();
4736292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    };
4746292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4756292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    BidiText(const uint16_t* buf, size_t start, size_t count, size_t bufSize, int bidiFlags);
4766292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4776292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    ~BidiText() {
4786292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        if (mBidi) {
4796292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi            ubidi_close(mBidi);
4806292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        }
4816292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
4826292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4836292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    Iter begin () const {
4846292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return Iter(mBidi, mStart, mEnd, 0, mRunCount, mIsRtl);
4856292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
4866292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4876292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    Iter end() const {
4886292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return Iter(mBidi, mStart, mEnd, mRunCount, mRunCount, mIsRtl);
4896292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
4906292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4916292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagiprivate:
4926292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    const size_t mStart;
4936292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    const size_t mEnd;
4946292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    const size_t mBufSize;
4956292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    UBiDi* mBidi;
4966292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    size_t mRunCount;
4976292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    bool mIsRtl;
4986292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
4996292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    DISALLOW_COPY_AND_ASSIGN(BidiText);
5006292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi};
5016292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
5026292e1a966da86af7045c356fcad6ab8864089b8Keisuke KuroyanagiBidiText::Iter::Iter(UBiDi* bidi, size_t start, size_t end, size_t runIndex, size_t runCount,
5036292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        bool isRtl)
5046292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    : mBidi(bidi), mIsEnd(runIndex == runCount), mNextRunIndex(runIndex), mRunCount(runCount),
5056292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi      mStart(start), mEnd(end), mRunInfo() {
5066292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (mRunCount == 1) {
5076292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        mRunInfo.mRunStart = start;
5086292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        mRunInfo.mRunLength = end - start;
5096292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        mRunInfo.mIsRtl = isRtl;
5106292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        mNextRunIndex = mRunCount;
5116292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return;
5126292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5136292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    updateRunInfo();
5146292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi}
5156292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
5166292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagivoid BidiText::Iter::updateRunInfo() {
5176292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (mNextRunIndex == mRunCount) {
5186292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        // All runs have been iterated.
5196292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        mIsEnd = true;
5206292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return;
5216292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5226292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    int32_t startRun = -1;
5236292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    int32_t lengthRun = -1;
5246292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    const UBiDiDirection runDir = ubidi_getVisualRun(mBidi, mNextRunIndex, &startRun, &lengthRun);
5256292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    mNextRunIndex++;
5266292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (startRun == -1 || lengthRun == -1) {
5276292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        ALOGE("invalid visual run");
5286292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        // skip the invalid run.
5296292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        updateRunInfo();
5306292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return;
5316292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5326292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    const int32_t runEnd = std::min(startRun + lengthRun, mEnd);
5336292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    mRunInfo.mRunStart = std::max(startRun, mStart);
5346292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    mRunInfo.mRunLength = runEnd - mRunInfo.mRunStart;
5356292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (mRunInfo.mRunLength <= 0) {
5366292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        // skip the empty run.
5376292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        updateRunInfo();
5386292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return;
5396292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5406292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    mRunInfo.mIsRtl = (runDir == UBIDI_RTL);
5416292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi}
5426292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
5436292e1a966da86af7045c356fcad6ab8864089b8Keisuke KuroyanagiBidiText::BidiText(const uint16_t* buf, size_t start, size_t count, size_t bufSize, int bidiFlags)
5446292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    : mStart(start), mEnd(start + count), mBufSize(bufSize), mBidi(NULL), mRunCount(1),
5456292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi      mIsRtl((bidiFlags & kDirection_Mask) != 0) {
5466292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (bidiFlags == kBidi_Force_LTR || bidiFlags == kBidi_Force_RTL) {
5476292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        // force single run.
5486292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return;
5496292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5506292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    mBidi = ubidi_open();
5516292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (!mBidi) {
5526292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        ALOGE("error creating bidi object");
5536292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return;
5546292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5556292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    UErrorCode status = U_ZERO_ERROR;
5566292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    UBiDiLevel bidiReq = bidiFlags;
5576292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (bidiFlags == kBidi_Default_LTR) {
5586292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        bidiReq = UBIDI_DEFAULT_LTR;
5596292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    } else if (bidiFlags == kBidi_Default_RTL) {
5606292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        bidiReq = UBIDI_DEFAULT_RTL;
5616292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5626292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    ubidi_setPara(mBidi, buf, mBufSize, bidiReq, NULL, &status);
5636292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (!U_SUCCESS(status)) {
5646292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        ALOGE("error calling ubidi_setPara, status = %d", status);
5656292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return;
5666292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5676292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    const int paraDir = ubidi_getParaLevel(mBidi) & kDirection_Mask;
5686292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    const ssize_t rc = ubidi_countRuns(mBidi, &status);
5696292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (!U_SUCCESS(status) || rc < 0) {
5706292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        ALOGW("error counting bidi runs, status = %d", status);
5716292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5726292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    if (!U_SUCCESS(status) || rc <= 1) {
5736292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        mIsRtl = (paraDir == kBidi_RTL);
5746292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        return;
5756292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    }
5766292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    mRunCount = rc;
5776292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi}
5786292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi
5794d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levienvoid Layout::doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
58009f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod        int bidiFlags, const FontStyle &style, const MinikinPaint &paint) {
58109f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod    AutoMutex _l(gMinikinLock);
58209f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod
58309f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod    LayoutContext ctx;
58409f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod    ctx.style = style;
58509f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod    ctx.paint = paint;
58609f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod
5876da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    reset();
5884d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    mAdvances.resize(count, 0);
5896da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod
5906292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi    for (const BidiText::Iter::RunInfo& runInfo : BidiText(buf, start, count, bufSize, bidiFlags)) {
5916292e1a966da86af7045c356fcad6ab8864089b8Keisuke Kuroyanagi        doLayoutRunCached(buf, runInfo.mRunStart, runInfo.mRunLength, bufSize, runInfo.mIsRtl, &ctx,
5926344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi                start, mCollection, this, NULL);
5934d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    }
594288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    ctx.clearHbFonts();
5954d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien}
5964d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
5976344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagifloat Layout::measureText(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
5986344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        int bidiFlags, const FontStyle &style, const MinikinPaint &paint,
5996344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        const FontCollection* collection, float* advances) {
6006344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    AutoMutex _l(gMinikinLock);
6016344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi
6026344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    LayoutContext ctx;
6036344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    ctx.style = style;
6046344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    ctx.paint = paint;
6056344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi
6066344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    float advance = 0;
6076344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    for (const BidiText::Iter::RunInfo& runInfo : BidiText(buf, start, count, bufSize, bidiFlags)) {
6086344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        float* advancesForRun = advances ? advances + (runInfo.mRunStart - start) : advances;
6096344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        advance += doLayoutRunCached(buf, runInfo.mRunStart, runInfo.mRunLength, bufSize,
6106344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi                runInfo.mIsRtl, &ctx, 0, collection, NULL, advancesForRun);
6116344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    }
6126344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi
6136344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    ctx.clearHbFonts();
6146344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    return advance;
6156344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi}
6166344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi
6176344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagifloat Layout::doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
6186344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        bool isRtl, LayoutContext* ctx, size_t dstStart, const FontCollection* collection,
6196344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        Layout* layout, float* advances) {
620d692d6a9791145d41d7778cdf6b40b20c2be8cb4Raph Levien    HyphenEdit hyphen = ctx->paint.hyphenEdit;
6216344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    float advance = 0;
6224d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    if (!isRtl) {
6234d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien        // left to right
6244e3adc6fb2073d5b466b88b8f5329d281038aba1Seigo Nonaka        size_t wordstart =
6254e3adc6fb2073d5b466b88b8f5329d281038aba1Seigo Nonaka                start == bufSize ? start : getPrevWordBreakForCache(buf, start + 1, bufSize);
6264d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien        size_t wordend;
6274d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien        for (size_t iter = start; iter < start + count; iter = wordend) {
6284e3adc6fb2073d5b466b88b8f5329d281038aba1Seigo Nonaka            wordend = getNextWordBreakForCache(buf, iter, bufSize);
629d692d6a9791145d41d7778cdf6b40b20c2be8cb4Raph Levien            // Only apply hyphen to the last word in the string.
630d692d6a9791145d41d7778cdf6b40b20c2be8cb4Raph Levien            ctx->paint.hyphenEdit = wordend >= start + count ? hyphen : HyphenEdit();
6313f1ea5da2ee12b0d95c17c56928c3e553d4eeda0Raph Levien            size_t wordcount = std::min(start + count, wordend) - iter;
6326344de60fa37c4e4246cb37d82215564826c2b8b