Layout.cpp 35.4 KB
Newer Older
R
Raph Levien 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
#define LOG_TAG "Minikin"
18

R
Raph Levien 已提交
19
#include <algorithm>
R
Raph Levien 已提交
20 21
#include <fstream>
#include <iostream>  // for debugging
22
#include <math.h>
23
#include <string>
24
#include <unicode/ubidi.h>
25
#include <vector>
R
Raph Levien 已提交
26

27
#include <log/log.h>
28 29 30 31 32
#include <utils/JenkinsHash.h>
#include <utils/LruCache.h>
#include <utils/Singleton.h>
#include <utils/String16.h>

33
#include <hb-icu.h>
34
#include <hb-ot.h>
35

36
#include "FontLanguage.h"
S
Seigo Nonaka 已提交
37
#include "FontLanguageListCache.h"
38
#include "HbFontCache.h"
39
#include "LayoutUtils.h"
R
Raph Levien 已提交
40
#include "MinikinInternal.h"
R
Raph Levien 已提交
41
#include <minikin/MinikinFontFreeType.h>
R
Raph Levien 已提交
42 43 44 45 46
#include <minikin/Layout.h>

using std::string;
using std::vector;

J
John Reck 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
namespace minikin {

Bitmap::Bitmap(int width, int height) : width(width), height(height) {
    buf = new uint8_t[width * height]();
}

Bitmap::~Bitmap() {
    delete[] buf;
}

void Bitmap::writePnm(std::ofstream &o) const {
    o << "P5" << std::endl;
    o << width << " " << height << std::endl;
    o << "255" << std::endl;
    o.write((const char *)buf, width * height);
    o.close();
}

S
Seigo Nonaka 已提交
65
void Bitmap::drawGlyph(const GlyphBitmap& bitmap, int x, int y) {
J
John Reck 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    int bmw = bitmap.width;
    int bmh = bitmap.height;
    x += bitmap.left;
    y -= bitmap.top;
    int x0 = std::max(0, x);
    int x1 = std::min(width, x + bmw);
    int y0 = std::max(0, y);
    int y1 = std::min(height, y + bmh);
    const unsigned char* src = bitmap.buffer + (y0 - y) * bmw + (x0 - x);
    uint8_t* dst = buf + y0 * width;
    for (int yy = y0; yy < y1; yy++) {
        for (int xx = x0; xx < x1; xx++) {
            int pixel = (int)dst[xx] + (int)src[xx - x];
            pixel = pixel > 0xff ? 0xff : pixel;
            dst[xx] = pixel;
        }
        src += bmw;
        dst += width;
    }
}

R
Raph Levien 已提交
87 88
const int kDirection_Mask = 0x1;

B
Behdad Esfahbod 已提交
89 90 91 92 93 94 95
struct LayoutContext {
    MinikinPaint paint;
    FontStyle style;
    std::vector<hb_font_t*> hbFonts;  // parallel to mFaces

    void clearHbFonts() {
        for (size_t i = 0; i < hbFonts.size(); i++) {
96
            hb_font_set_funcs(hbFonts[i], nullptr, nullptr, nullptr);
R
Raph Levien 已提交
97
            hb_font_destroy(hbFonts[i]);
B
Behdad Esfahbod 已提交
98 99 100 101 102
        }
        hbFonts.clear();
    }
};

103 104 105 106 107 108
// Layout cache datatypes

class LayoutCacheKey {
public:
    LayoutCacheKey(const FontCollection* collection, const MinikinPaint& paint, FontStyle style,
            const uint16_t* chars, size_t start, size_t count, size_t nchars, bool dir)
109 110
            : mChars(chars), mNchars(nchars),
            mStart(start), mCount(count), mId(collection->getId()), mStyle(style),
R
Raph Levien 已提交
111
            mSize(paint.size), mScaleX(paint.scaleX), mSkewX(paint.skewX),
B
Behdad Esfahbod 已提交
112
            mLetterSpacing(paint.letterSpacing),
113 114
            mPaintFlags(paint.paintFlags), mHyphenEdit(paint.hyphenEdit), mIsRtl(dir),
            mHash(computeHash()) {
115 116
    }
    bool operator==(const LayoutCacheKey &other) const;
117

S
Seigo Nonaka 已提交
118
    android::hash_t hash() const {
119 120
        return mHash;
    }
121

B
Behdad Esfahbod 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    void copyText() {
        uint16_t* charsCopy = new uint16_t[mNchars];
        memcpy(charsCopy, mChars, mNchars * sizeof(uint16_t));
        mChars = charsCopy;
    }
    void freeText() {
        delete[] mChars;
        mChars = NULL;
    }

    void doLayout(Layout* layout, LayoutContext* ctx, const FontCollection* collection) const {
        layout->setFontCollection(collection);
        layout->mAdvances.resize(mCount, 0);
        ctx->clearHbFonts();
        layout->doLayoutRun(mChars, mStart, mCount, mNchars, mIsRtl, ctx);
    }

139
private:
B
Behdad Esfahbod 已提交
140 141
    const uint16_t* mChars;
    size_t mNchars;
142 143 144 145 146
    size_t mStart;
    size_t mCount;
    uint32_t mId;  // for the font collection
    FontStyle mStyle;
    float mSize;
R
Raph Levien 已提交
147 148
    float mScaleX;
    float mSkewX;
B
Behdad Esfahbod 已提交
149
    float mLetterSpacing;
R
Raph Levien 已提交
150
    int32_t mPaintFlags;
R
Raph Levien 已提交
151
    HyphenEdit mHyphenEdit;
152 153 154
    bool mIsRtl;
    // Note: any fields added to MinikinPaint must also be reflected here.
    // TODO: language matching (possibly integrate into style)
S
Seigo Nonaka 已提交
155
    android::hash_t mHash;
156

S
Seigo Nonaka 已提交
157
    android::hash_t computeHash() const;
158 159
};

S
Seigo Nonaka 已提交
160
class LayoutCache : private android::OnEntryRemoved<LayoutCacheKey, Layout*> {
161 162 163 164 165
public:
    LayoutCache() : mCache(kMaxEntries) {
        mCache.setOnEntryRemovedListener(this);
    }

B
Behdad Esfahbod 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    void clear() {
        mCache.clear();
    }

    Layout* get(LayoutCacheKey& key, LayoutContext* ctx, const FontCollection* collection) {
        Layout* layout = mCache.get(key);
        if (layout == NULL) {
            key.copyText();
            layout = new Layout();
            key.doLayout(layout, ctx, collection);
            mCache.put(key, layout);
        }
        return layout;
    }

private:
182 183
    // callback for OnEntryRemoved
    void operator()(LayoutCacheKey& key, Layout*& value) {
B
Behdad Esfahbod 已提交
184
        key.freeText();
185 186 187
        delete value;
    }

S
Seigo Nonaka 已提交
188
    android::LruCache<LayoutCacheKey, Layout*> mCache;
B
Behdad Esfahbod 已提交
189

190 191 192 193 194 195 196
    //static const size_t kMaxEntries = LruCache<LayoutCacheKey, Layout*>::kUnlimitedCapacity;

    // TODO: eviction based on memory footprint; for now, we just use a constant
    // number of strings
    static const size_t kMaxEntries = 5000;
};

197 198 199 200 201
static unsigned int disabledDecomposeCompatibility(hb_unicode_funcs_t*, hb_codepoint_t,
                                                   hb_codepoint_t*, void*) {
    return 0;
}

S
Seigo Nonaka 已提交
202
class LayoutEngine : public ::android::Singleton<LayoutEngine> {
203 204
public:
    LayoutEngine() {
205 206 207 208
        unicodeFunctions = hb_unicode_funcs_create(hb_icu_get_unicode_funcs());
        /* Disable the function used for compatibility decomposition */
        hb_unicode_funcs_set_decompose_compatibility_func(
                unicodeFunctions, disabledDecomposeCompatibility, NULL, NULL);
209
        hbBuffer = hb_buffer_create();
210
        hb_buffer_set_unicode_funcs(hbBuffer, unicodeFunctions);
211 212 213
    }

    hb_buffer_t* hbBuffer;
214
    hb_unicode_funcs_t* unicodeFunctions;
215 216 217 218
    LayoutCache layoutCache;
};

bool LayoutCacheKey::operator==(const LayoutCacheKey& other) const {
R
Raph Levien 已提交
219 220 221 222 223 224 225
    return mId == other.mId
            && mStart == other.mStart
            && mCount == other.mCount
            && mStyle == other.mStyle
            && mSize == other.mSize
            && mScaleX == other.mScaleX
            && mSkewX == other.mSkewX
B
Behdad Esfahbod 已提交
226
            && mLetterSpacing == other.mLetterSpacing
R
Raph Levien 已提交
227
            && mPaintFlags == other.mPaintFlags
R
Raph Levien 已提交
228
            && mHyphenEdit == other.mHyphenEdit
R
Raph Levien 已提交
229
            && mIsRtl == other.mIsRtl
B
Behdad Esfahbod 已提交
230 231
            && mNchars == other.mNchars
            && !memcmp(mChars, other.mChars, mNchars * sizeof(uint16_t));
232 233
}

S
Seigo Nonaka 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247
android::hash_t LayoutCacheKey::computeHash() const {
    uint32_t hash = android::JenkinsHashMix(0, mId);
    hash = android::JenkinsHashMix(hash, mStart);
    hash = android::JenkinsHashMix(hash, mCount);
    hash = android::JenkinsHashMix(hash, hash_type(mStyle));
    hash = android::JenkinsHashMix(hash, hash_type(mSize));
    hash = android::JenkinsHashMix(hash, hash_type(mScaleX));
    hash = android::JenkinsHashMix(hash, hash_type(mSkewX));
    hash = android::JenkinsHashMix(hash, hash_type(mLetterSpacing));
    hash = android::JenkinsHashMix(hash, hash_type(mPaintFlags));
    hash = android::JenkinsHashMix(hash, hash_type(mHyphenEdit.hasHyphen()));
    hash = android::JenkinsHashMix(hash, hash_type(mIsRtl));
    hash = android::JenkinsHashMixShorts(hash, mChars, mNchars);
    return android::JenkinsHashWhiten(hash);
248 249
}

S
Seigo Nonaka 已提交
250
android::hash_t hash_type(const LayoutCacheKey& key) {
251 252 253
    return key.hash();
}

254 255 256 257 258 259 260 261 262 263 264
void MinikinRect::join(const MinikinRect& r) {
    if (isEmpty()) {
        set(r);
    } else if (!r.isEmpty()) {
        mLeft = std::min(mLeft, r.mLeft);
        mTop = std::min(mTop, r.mTop);
        mRight = std::max(mRight, r.mRight);
        mBottom = std::max(mBottom, r.mBottom);
    }
}

265
// Deprecated. Remove when callers are removed.
R
Raph Levien 已提交
266 267 268
void Layout::init() {
}

269 270 271 272 273 274 275 276
void Layout::reset() {
    mGlyphs.clear();
    mFaces.clear();
    mBounds.setEmpty();
    mAdvances.clear();
    mAdvance = 0;
}

277
void Layout::setFontCollection(const FontCollection* collection) {
R
Raph Levien 已提交
278 279 280
    mCollection = collection;
}

281 282
static hb_position_t harfbuzzGetGlyphHorizontalAdvance(hb_font_t* /* hbFont */, void* fontData,
        hb_codepoint_t glyph, void* /* userData */) {
283
    MinikinPaint* paint = reinterpret_cast<MinikinPaint*>(fontData);
R
Raph Levien 已提交
284 285 286
    MinikinFont* font = paint->font;
    float advance = font->GetHorizontalAdvance(glyph, *paint);
    return 256 * advance + 0.5;
R
Raph Levien 已提交
287 288
}

289 290 291
static hb_bool_t harfbuzzGetGlyphHorizontalOrigin(hb_font_t* /* hbFont */, void* /* fontData */,
        hb_codepoint_t /* glyph */, hb_position_t* /* x */, hb_position_t* /* y */,
        void* /* userData */) {
R
Raph Levien 已提交
292 293 294 295 296
    // Just return true, following the way that Harfbuzz-FreeType
    // implementation does.
    return true;
}

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
hb_font_funcs_t* getHbFontFuncs(bool forColorBitmapFont) {
    assertMinikinLocked();

    static hb_font_funcs_t* hbFuncs = nullptr;
    static hb_font_funcs_t* hbFuncsForColorBitmap = nullptr;

    hb_font_funcs_t** funcs = forColorBitmapFont ? &hbFuncs : &hbFuncsForColorBitmap;
    if (*funcs == nullptr) {
        *funcs = hb_font_funcs_create();
        if (forColorBitmapFont) {
            // Override the h_advance function since we can't use HarfBuzz's implemenation. It may
            // return the wrong value if the font uses hinting aggressively.
            hb_font_funcs_set_glyph_h_advance_func(*funcs, harfbuzzGetGlyphHorizontalAdvance, 0, 0);
        } else {
            // Don't override the h_advance function since we use HarfBuzz's implementation for
            // emoji for performance reasons.
            // Note that it is technically possible for a TrueType font to have outline and embedded
            // bitmap at the same time. We ignore modified advances of hinted outline glyphs in that
            // case.
        }
        hb_font_funcs_set_glyph_h_origin_func(*funcs, harfbuzzGetGlyphHorizontalOrigin, 0, 0);
        hb_font_funcs_make_immutable(*funcs);
R
Raph Levien 已提交
319
    }
320 321 322 323 324 325 326
    return *funcs;
}

static bool isColorBitmapFont(hb_font_t* font) {
    hb_face_t* face = hb_font_get_face(font);
    HbBlob cbdt(hb_face_reference_table(face, HB_TAG('C', 'B', 'D', 'T')));
    return cbdt.size() > 0;
R
Raph Levien 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
}

static float HBFixedToFloat(hb_position_t v)
{
    return scalbnf (v, -8);
}

static hb_position_t HBFloatToFixed(float v)
{
    return scalbnf (v, +8);
}

void Layout::dump() const {
    for (size_t i = 0; i < mGlyphs.size(); i++) {
        const LayoutGlyph& glyph = mGlyphs[i];
        std::cout << glyph.glyph_id << ": " << glyph.x << ", " << glyph.y << std::endl;
    }
}

R
Raph Levien 已提交
346
int Layout::findFace(FakedFont face, LayoutContext* ctx) {
R
Raph Levien 已提交
347 348
    unsigned int ix;
    for (ix = 0; ix < mFaces.size(); ix++) {
R
Raph Levien 已提交
349
        if (mFaces[ix].font == face.font) {
R
Raph Levien 已提交
350 351 352 353
            return ix;
        }
    }
    mFaces.push_back(face);
354 355 356
    // Note: ctx == NULL means we're copying from the cache, no need to create
    // corresponding hb_font object.
    if (ctx != NULL) {
357
        hb_font_t* font = getHbFontLocked(face.font);
358
        hb_font_set_funcs(font, getHbFontFuncs(isColorBitmapFont(font)), &ctx->paint, 0);
359 360
        ctx->hbFonts.push_back(font);
    }
R
Raph Levien 已提交
361 362 363
    return ix;
}

364
static hb_script_t codePointToScript(hb_codepoint_t codepoint) {
365
    static hb_unicode_funcs_t* u = 0;
366
    if (!u) {
367
        u = LayoutEngine::getInstance().unicodeFunctions;
368 369 370 371
    }
    return hb_unicode_script(u, codepoint);
}

372
static hb_codepoint_t decodeUtf16(const uint16_t* chars, size_t len, ssize_t* iter) {
373 374 375 376 377 378 379 380 381 382 383 384
    const uint16_t v = chars[(*iter)++];
    // test whether v in (0xd800..0xdfff), lead or trail surrogate
    if ((v & 0xf800) == 0xd800) {
        // test whether v in (0xd800..0xdbff), lead surrogate
        if (size_t(*iter) < len && (v & 0xfc00) == 0xd800) {
            const uint16_t v2 = chars[(*iter)++];
            // test whether v2 in (0xdc00..0xdfff), trail surrogate
            if ((v2 & 0xfc00) == 0xdc00) {
                // (0xd800 0xdc00) in utf-16 maps to 0x10000 in ucs-32
                const hb_codepoint_t delta = (0xd800 << 10) + 0xdc00 - 0x10000;
                return (((hb_codepoint_t)v) << 10) + v2 - delta;
            }
385 386
            (*iter) -= 1;
            return 0xFFFDu;
387
        } else {
388
            return 0xFFFDu;
389 390 391 392 393 394
        }
    } else {
        return v;
    }
}

395
static hb_script_t getScriptRun(const uint16_t* chars, size_t len, ssize_t* iter) {
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    if (size_t(*iter) == len) {
        return HB_SCRIPT_UNKNOWN;
    }
    uint32_t cp = decodeUtf16(chars, len, iter);
    hb_script_t current_script = codePointToScript(cp);
    for (;;) {
        if (size_t(*iter) == len)
            break;
        const ssize_t prev_iter = *iter;
        cp = decodeUtf16(chars, len, iter);
        const hb_script_t script = codePointToScript(cp);
        if (script != current_script) {
            if (current_script == HB_SCRIPT_INHERITED ||
                current_script == HB_SCRIPT_COMMON) {
                current_script = script;
            } else if (script == HB_SCRIPT_INHERITED ||
                script == HB_SCRIPT_COMMON) {
                continue;
            } else {
                *iter = prev_iter;
                break;
            }
        }
    }
    if (current_script == HB_SCRIPT_INHERITED) {
        current_script = HB_SCRIPT_COMMON;
    }

    return current_script;
}

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
/**
 * Disable certain scripts (mostly those with cursive connection) from having letterspacing
 * applied. See https://github.com/behdad/harfbuzz/issues/64 for more details.
 */
static bool isScriptOkForLetterspacing(hb_script_t script) {
    return !(
            script == HB_SCRIPT_ARABIC ||
            script == HB_SCRIPT_NKO ||
            script == HB_SCRIPT_PSALTER_PAHLAVI ||
            script == HB_SCRIPT_MANDAIC ||
            script == HB_SCRIPT_MONGOLIAN ||
            script == HB_SCRIPT_PHAGS_PA ||
            script == HB_SCRIPT_DEVANAGARI ||
            script == HB_SCRIPT_BENGALI ||
            script == HB_SCRIPT_GURMUKHI ||
            script == HB_SCRIPT_MODI ||
            script == HB_SCRIPT_SHARADA ||
            script == HB_SCRIPT_SYLOTI_NAGRI ||
            script == HB_SCRIPT_TIRHUTA ||
            script == HB_SCRIPT_OGHAM
            );
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
class BidiText {
public:
    class Iter {
    public:
        struct RunInfo {
            int32_t mRunStart;
            int32_t mRunLength;
            bool mIsRtl;
        };

        Iter(UBiDi* bidi, size_t start, size_t end, size_t runIndex, size_t runCount, bool isRtl);

        bool operator!= (const Iter& other) const {
            return mIsEnd != other.mIsEnd || mNextRunIndex != other.mNextRunIndex
                    || mBidi != other.mBidi;
        }

        const RunInfo& operator* () const {
            return mRunInfo;
        }

        const Iter& operator++ () {
            updateRunInfo();
            return *this;
        }

    private:
        UBiDi* const mBidi;
        bool mIsEnd;
        size_t mNextRunIndex;
        const size_t mRunCount;
        const int32_t mStart;
        const int32_t mEnd;
        RunInfo mRunInfo;

        void updateRunInfo();
    };

    BidiText(const uint16_t* buf, size_t start, size_t count, size_t bufSize, int bidiFlags);

    ~BidiText() {
        if (mBidi) {
            ubidi_close(mBidi);
        }
    }

    Iter begin () const {
        return Iter(mBidi, mStart, mEnd, 0, mRunCount, mIsRtl);
    }

    Iter end() const {
        return Iter(mBidi, mStart, mEnd, mRunCount, mRunCount, mIsRtl);
    }

private:
    const size_t mStart;
    const size_t mEnd;
    const size_t mBufSize;
    UBiDi* mBidi;
    size_t mRunCount;
    bool mIsRtl;

512 513
    BidiText(const BidiText&) = delete;
    void operator=(const BidiText&) = delete;
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
};

BidiText::Iter::Iter(UBiDi* bidi, size_t start, size_t end, size_t runIndex, size_t runCount,
        bool isRtl)
    : mBidi(bidi), mIsEnd(runIndex == runCount), mNextRunIndex(runIndex), mRunCount(runCount),
      mStart(start), mEnd(end), mRunInfo() {
    if (mRunCount == 1) {
        mRunInfo.mRunStart = start;
        mRunInfo.mRunLength = end - start;
        mRunInfo.mIsRtl = isRtl;
        mNextRunIndex = mRunCount;
        return;
    }
    updateRunInfo();
}

void BidiText::Iter::updateRunInfo() {
    if (mNextRunIndex == mRunCount) {
        // All runs have been iterated.
        mIsEnd = true;
        return;
    }
    int32_t startRun = -1;
    int32_t lengthRun = -1;
    const UBiDiDirection runDir = ubidi_getVisualRun(mBidi, mNextRunIndex, &startRun, &lengthRun);
    mNextRunIndex++;
    if (startRun == -1 || lengthRun == -1) {
        ALOGE("invalid visual run");
        // skip the invalid run.
        updateRunInfo();
        return;
    }
    const int32_t runEnd = std::min(startRun + lengthRun, mEnd);
    mRunInfo.mRunStart = std::max(startRun, mStart);
    mRunInfo.mRunLength = runEnd - mRunInfo.mRunStart;
    if (mRunInfo.mRunLength <= 0) {
        // skip the empty run.
        updateRunInfo();
        return;
    }
    mRunInfo.mIsRtl = (runDir == UBIDI_RTL);
}

BidiText::BidiText(const uint16_t* buf, size_t start, size_t count, size_t bufSize, int bidiFlags)
    : mStart(start), mEnd(start + count), mBufSize(bufSize), mBidi(NULL), mRunCount(1),
      mIsRtl((bidiFlags & kDirection_Mask) != 0) {
    if (bidiFlags == kBidi_Force_LTR || bidiFlags == kBidi_Force_RTL) {
        // force single run.
        return;
    }
    mBidi = ubidi_open();
    if (!mBidi) {
        ALOGE("error creating bidi object");
        return;
    }
    UErrorCode status = U_ZERO_ERROR;
    UBiDiLevel bidiReq = bidiFlags;
    if (bidiFlags == kBidi_Default_LTR) {
        bidiReq = UBIDI_DEFAULT_LTR;
    } else if (bidiFlags == kBidi_Default_RTL) {
        bidiReq = UBIDI_DEFAULT_RTL;
    }
    ubidi_setPara(mBidi, buf, mBufSize, bidiReq, NULL, &status);
    if (!U_SUCCESS(status)) {
        ALOGE("error calling ubidi_setPara, status = %d", status);
        return;
    }
    const int paraDir = ubidi_getParaLevel(mBidi) & kDirection_Mask;
    const ssize_t rc = ubidi_countRuns(mBidi, &status);
    if (!U_SUCCESS(status) || rc < 0) {
        ALOGW("error counting bidi runs, status = %d", status);
    }
    if (!U_SUCCESS(status) || rc <= 1) {
        mIsRtl = (paraDir == kBidi_RTL);
        return;
    }
    mRunCount = rc;
}

593 594
void Layout::doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
        int bidiFlags, const FontStyle &style, const MinikinPaint &paint) {
S
Seigo Nonaka 已提交
595
    android::AutoMutex _l(gMinikinLock);
596 597 598 599 600

    LayoutContext ctx;
    ctx.style = style;
    ctx.paint = paint;

601
    reset();
602
    mAdvances.resize(count, 0);
603

604 605
    for (const BidiText::Iter::RunInfo& runInfo : BidiText(buf, start, count, bufSize, bidiFlags)) {
        doLayoutRunCached(buf, runInfo.mRunStart, runInfo.mRunLength, bufSize, runInfo.mIsRtl, &ctx,
606
                start, mCollection, this, NULL);
607
    }
B
Behdad Esfahbod 已提交
608
    ctx.clearHbFonts();
609 610
}

611 612 613
float Layout::measureText(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
        int bidiFlags, const FontStyle &style, const MinikinPaint &paint,
        const FontCollection* collection, float* advances) {
S
Seigo Nonaka 已提交
614
    android::AutoMutex _l(gMinikinLock);
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

    LayoutContext ctx;
    ctx.style = style;
    ctx.paint = paint;

    float advance = 0;
    for (const BidiText::Iter::RunInfo& runInfo : BidiText(buf, start, count, bufSize, bidiFlags)) {
        float* advancesForRun = advances ? advances + (runInfo.mRunStart - start) : advances;
        advance += doLayoutRunCached(buf, runInfo.mRunStart, runInfo.mRunLength, bufSize,
                runInfo.mIsRtl, &ctx, 0, collection, NULL, advancesForRun);
    }

    ctx.clearHbFonts();
    return advance;
}

float Layout::doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
        bool isRtl, LayoutContext* ctx, size_t dstStart, const FontCollection* collection,
        Layout* layout, float* advances) {
634
    HyphenEdit hyphen = ctx->paint.hyphenEdit;
635
    float advance = 0;
636 637
    if (!isRtl) {
        // left to right
638 639
        size_t wordstart =
                start == bufSize ? start : getPrevWordBreakForCache(buf, start + 1, bufSize);
640 641
        size_t wordend;
        for (size_t iter = start; iter < start + count; iter = wordend) {
642
            wordend = getNextWordBreakForCache(buf, iter, bufSize);
643 644
            // Only apply hyphen to the last word in the string.
            ctx->paint.hyphenEdit = wordend >= start + count ? hyphen : HyphenEdit();
R
Raph Levien 已提交
645
            size_t wordcount = std::min(start + count, wordend) - iter;
646 647 648
            advance += doLayoutWord(buf + wordstart, iter - wordstart, wordcount,
                    wordend - wordstart, isRtl, ctx, iter - dstStart, collection, layout,
                    advances ? advances + (iter - start) : advances);
649 650 651 652 653 654
            wordstart = wordend;
        }
    } else {
        // right to left
        size_t wordstart;
        size_t end = start + count;
655
        size_t wordend = end == 0 ? 0 : getNextWordBreakForCache(buf, end - 1, bufSize);
656
        for (size_t iter = end; iter > start; iter = wordstart) {
657
            wordstart = getPrevWordBreakForCache(buf, iter, bufSize);
658 659
            // Only apply hyphen to the last (leftmost) word in the string.
            ctx->paint.hyphenEdit = iter == end ? hyphen : HyphenEdit();
R
Raph Levien 已提交
660
            size_t bufStart = std::max(start, wordstart);
661 662 663
            advance += doLayoutWord(buf + wordstart, bufStart - wordstart, iter - bufStart,
                    wordend - wordstart, isRtl, ctx, bufStart - dstStart, collection, layout,
                    advances ? advances + (bufStart - start) : advances);
664 665
            wordend = wordstart;
        }
R
Raph Levien 已提交
666
    }
667
    return advance;
R
Raph Levien 已提交
668 669
}

670 671 672
float Layout::doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
        bool isRtl, LayoutContext* ctx, size_t bufStart, const FontCollection* collection,
        Layout* layout, float* advances) {
673
    LayoutCache& cache = LayoutEngine::getInstance().layoutCache;
674
    LayoutCacheKey key(collection, ctx->paint, ctx->style, buf, start, count, bufSize, isRtl);
S
Seigo Nonaka 已提交
675 676 677 678 679

    float wordSpacing = count == 1 && isWordSpace(buf[start]) ? ctx->paint.wordSpacing : 0;

    float advance;
    if (ctx->paint.skipCache()) {
680 681 682
        Layout layoutForWord;
        key.doLayout(&layoutForWord, ctx, collection);
        if (layout) {
S
Seigo Nonaka 已提交
683
            layout->appendLayout(&layoutForWord, bufStart, wordSpacing);
684 685 686 687
        }
        if (advances) {
            layoutForWord.getAdvances(advances);
        }
S
Seigo Nonaka 已提交
688
        advance = layoutForWord.getAdvance();
B
Behdad Esfahbod 已提交
689
    } else {
690 691
        Layout* layoutForWord = cache.get(key, ctx, collection);
        if (layout) {
S
Seigo Nonaka 已提交
692
            layout->appendLayout(layoutForWord, bufStart, wordSpacing);
693 694 695 696
        }
        if (advances) {
            layoutForWord->getAdvances(advances);
        }
S
Seigo Nonaka 已提交
697
        advance = layoutForWord->getAdvance();
698
    }
S
Seigo Nonaka 已提交
699 700 701 702 703 704 705 706

    if (wordSpacing != 0) {
        advance += wordSpacing;
        if (advances) {
            advances[0] += wordSpacing;
        }
    }
    return advance;
707 708
}

B
Behdad Esfahbod 已提交
709 710 711
static void addFeatures(const string &str, vector<hb_feature_t>* features) {
    if (!str.size())
        return;
R
Raph Levien 已提交
712

B
Behdad Esfahbod 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    const char* start = str.c_str();
    const char* end = start + str.size();

    while (start < end) {
        static hb_feature_t feature;
        const char* p = strchr(start, ',');
        if (!p)
            p = end;
        /* We do not allow setting features on ranges.  As such, reject any
         * setting that has non-universal range. */
        if (hb_feature_from_string (start, p - start, &feature)
                && feature.start == 0 && feature.end == (unsigned int) -1)
            features->push_back(feature);
        start = p + 1;
    }
728 729
}

R
Raph Levien 已提交
730
void Layout::doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
731 732
        bool isRtl, LayoutContext* ctx) {
    hb_buffer_t* buffer = LayoutEngine::getInstance().hbBuffer;
R
Raph Levien 已提交
733
    vector<FontCollection::Run> items;
734
    mCollection->itemize(buf + start, count, ctx->style, &items);
R
Raph Levien 已提交
735 736 737 738
    if (isRtl) {
        std::reverse(items.begin(), items.end());
    }

739
    vector<hb_feature_t> features;
B
Behdad Esfahbod 已提交
740 741 742 743 744 745 746 747 748 749 750 751
    // Disable default-on non-required ligature features if letter-spacing
    // See http://dev.w3.org/csswg/css-text-3/#letter-spacing-property
    // "When the effective spacing between two characters is not zero (due to
    // either justification or a non-zero value of letter-spacing), user agents
    // should not apply optional ligatures."
    if (fabs(ctx->paint.letterSpacing) > 0.03)
    {
        static const hb_feature_t no_liga = { HB_TAG('l', 'i', 'g', 'a'), 0, 0, ~0u };
        static const hb_feature_t no_clig = { HB_TAG('c', 'l', 'i', 'g'), 0, 0, ~0u };
        features.push_back(no_liga);
        features.push_back(no_clig);
    }
B
Behdad Esfahbod 已提交
752
    addFeatures(ctx->paint.fontFeatureSettings, &features);
753

B
Behdad Esfahbod 已提交
754 755 756
    double size = ctx->paint.size;
    double scaleX = ctx->paint.scaleX;

R
Raph Levien 已提交
757
    float x = mAdvance;
R
Raph Levien 已提交
758 759 760
    float y = 0;
    for (size_t run_ix = 0; run_ix < items.size(); run_ix++) {
        FontCollection::Run &run = items[run_ix];
R
Raph Levien 已提交
761
        if (run.fakedFont.font == NULL) {
762 763 764
            ALOGE("no font for run starting u+%04x length %d", buf[run.start], run.end - run.start);
            continue;
        }
R
Raph Levien 已提交
765 766 767
        int font_ix = findFace(run.fakedFont, ctx);
        ctx->paint.font = mFaces[font_ix].font;
        ctx->paint.fakery = mFaces[font_ix].fakery;
768
        hb_font_t* hbFont = ctx->hbFonts[font_ix];
769
#ifdef VERBOSE_DEBUG
770
        ALOGD("Run %zu, font %d [%d:%d]", run_ix, font_ix, run.start, run.end);
R
Raph Levien 已提交
771
#endif
B
Behdad Esfahbod 已提交
772

R
Raph Levien 已提交
773 774
        hb_font_set_ppem(hbFont, size * scaleX, size);
        hb_font_set_scale(hbFont, HBFloatToFixed(size * scaleX), HBFloatToFixed(size));
R
Raph Levien 已提交
775

776 777
        const bool is_color_bitmap_font = isColorBitmapFont(hbFont);

R
Raph Levien 已提交
778 779 780
        // TODO: if there are multiple scripts within a font in an RTL run,
        // we need to reorder those runs. This is unlikely with our current
        // font stack, but should be done for correctness.
R
Raph Levien 已提交
781 782
        ssize_t srunend;
        for (ssize_t srunstart = run.start; srunstart < run.end; srunstart = srunend) {
783
            srunend = srunstart;
R
Raph Levien 已提交
784
            hb_script_t script = getScriptRun(buf + start, run.end, &srunend);
785

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
            double letterSpace = 0.0;
            double letterSpaceHalfLeft = 0.0;
            double letterSpaceHalfRight = 0.0;

            if (ctx->paint.letterSpacing != 0.0 && isScriptOkForLetterspacing(script)) {
                letterSpace = ctx->paint.letterSpacing * size * scaleX;
                if ((ctx->paint.paintFlags & LinearTextFlag) == 0) {
                    letterSpace = round(letterSpace);
                    letterSpaceHalfLeft = floor(letterSpace * 0.5);
                } else {
                    letterSpaceHalfLeft = letterSpace * 0.5;
                }
                letterSpaceHalfRight = letterSpace - letterSpaceHalfLeft;
            }

801
            hb_buffer_clear_contents(buffer);
802
            hb_buffer_set_script(buffer, script);
R
Raph Levien 已提交
803
            hb_buffer_set_direction(buffer, isRtl? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
S
Seigo Nonaka 已提交
804 805 806
            const FontLanguages& langList =
                    FontLanguageListCache::getById(ctx->style.getLanguageListId());
            if (langList.size() != 0) {
807 808 809 810 811 812 813 814 815
                const FontLanguage* hbLanguage = &langList[0];
                for (size_t i = 0; i < langList.size(); ++i) {
                    if (langList[i].supportsHbScript(script)) {
                        hbLanguage = &langList[i];
                        break;
                    }
                }
                hb_buffer_set_language(buffer,
                        hb_language_from_string(hbLanguage->getString().c_str(), -1));
R
Raph Levien 已提交
816
            }
R
Raph Levien 已提交
817
            hb_buffer_add_utf16(buffer, buf, bufSize, srunstart + start, srunend - srunstart);
818 819 820
            if (ctx->paint.hyphenEdit.hasHyphen() && srunend > srunstart) {
                // TODO: check whether this is really the desired semantics. It could have the
                // effect of assigning the hyphen width to a nonspacing mark
821
                unsigned int lastCluster = start + srunend - 1;
822 823 824 825 826 827 828 829 830 831 832

                hb_codepoint_t hyphenChar = 0x2010; // HYPHEN
                hb_codepoint_t glyph;
                // Fallback to ASCII HYPHEN-MINUS if the font didn't have a glyph for HYPHEN. Note
                // that we intentionally don't do anything special if the font doesn't have a
                // HYPHEN-MINUS either, so a tofu could be shown, hinting towards something
                // missing.
                if (!hb_font_get_glyph(hbFont, hyphenChar, 0, &glyph)) {
                    hyphenChar = 0x002D; // HYPHEN-MINUS
                }
                hb_buffer_add(buffer, hyphenChar, lastCluster);
833
            }
834
            hb_shape(hbFont, buffer, features.empty() ? NULL : &features[0], features.size());
835
            unsigned int numGlyphs;
836 837
            hb_glyph_info_t* info = hb_buffer_get_glyph_infos(buffer, &numGlyphs);
            hb_glyph_position_t* positions = hb_buffer_get_glyph_positions(buffer, NULL);
B
Behdad Esfahbod 已提交
838 839
            if (numGlyphs)
            {
R
Raph Levien 已提交
840 841
                mAdvances[info[0].cluster - start] += letterSpaceHalfLeft;
                x += letterSpaceHalfLeft;
B
Behdad Esfahbod 已提交
842
            }
843
            for (unsigned int i = 0; i < numGlyphs; i++) {
844 845 846 847 848 849 850 851
#ifdef VERBOSE_DEBUG
                ALOGD("%d %d %d %d",
                        positions[i].x_advance, positions[i].y_advance,
                        positions[i].x_offset, positions[i].y_offset);
                ALOGD("DoLayout %u: %f; %d, %d",
                        info[i].codepoint, HBFixedToFloat(positions[i].x_advance),
                        positions[i].x_offset, positions[i].y_offset);
#endif
B
Behdad Esfahbod 已提交
852
                if (i > 0 && info[i - 1].cluster != info[i].cluster) {
R
Raph Levien 已提交
853 854 855
                    mAdvances[info[i - 1].cluster - start] += letterSpaceHalfRight;
                    mAdvances[info[i].cluster - start] += letterSpaceHalfLeft;
                    x += letterSpace;
B
Behdad Esfahbod 已提交
856 857
                }

858 859
                hb_codepoint_t glyph_ix = info[i].codepoint;
                float xoff = HBFixedToFloat(positions[i].x_offset);
R
Raph Levien 已提交
860 861
                float yoff = -HBFixedToFloat(positions[i].y_offset);
                xoff += yoff * ctx->paint.skewX;
862 863 864
                LayoutGlyph glyph = {font_ix, glyph_ix, x + xoff, y + yoff};
                mGlyphs.push_back(glyph);
                float xAdvance = HBFixedToFloat(positions[i].x_advance);
R
Raph Levien 已提交
865 866 867
                if ((ctx->paint.paintFlags & LinearTextFlag) == 0) {
                    xAdvance = roundf(xAdvance);
                }
868
                MinikinRect glyphBounds;
869 870 871 872 873 874 875 876 877 878 879 880 881
                hb_glyph_extents_t extents = {};
                if (is_color_bitmap_font && hb_font_get_glyph_extents(hbFont, glyph_ix, &extents)) {
                    // Note that it is technically possible for a TrueType font to have outline and
                    // embedded bitmap at the same time. We ignore modified bbox of hinted outline
                    // glyphs in that case.
                    glyphBounds.mLeft = roundf(HBFixedToFloat(extents.x_bearing));
                    glyphBounds.mTop = roundf(HBFixedToFloat(-extents.y_bearing));
                    glyphBounds.mRight = roundf(HBFixedToFloat(extents.x_bearing + extents.width));
                    glyphBounds.mBottom =
                            roundf(HBFixedToFloat(-extents.y_bearing - extents.height));
                } else {
                    ctx->paint.font->GetBounds(&glyphBounds, glyph_ix, ctx->paint);
                }
882 883
                glyphBounds.offset(x + xoff, y + yoff);
                mBounds.join(glyphBounds);
884 885 886
                if (info[i].cluster - start < count) {
                    mAdvances[info[i].cluster - start] += xAdvance;
                } else {
887
                    ALOGE("cluster %zu (start %zu) out of bounds of count %zu",
888 889
                        info[i].cluster - start, start, count);
                }
890 891
                x += xAdvance;
            }
B
Behdad Esfahbod 已提交
892 893
            if (numGlyphs)
            {
R
Raph Levien 已提交
894 895
                mAdvances[info[numGlyphs - 1].cluster - start] += letterSpaceHalfRight;
                x += letterSpaceHalfRight;
B
Behdad Esfahbod 已提交
896
            }
R
Raph Levien 已提交
897 898
        }
    }
R
Raph Levien 已提交
899
    mAdvance = x;
R
Raph Levien 已提交
900 901
}

S
Seigo Nonaka 已提交
902
void Layout::appendLayout(Layout* src, size_t start, float extraAdvance) {
B
Behdad Esfahbod 已提交
903 904 905 906 907 908 909
    int fontMapStack[16];
    int* fontMap;
    if (src->mFaces.size() < sizeof(fontMapStack) / sizeof(fontMapStack[0])) {
        fontMap = fontMapStack;
    } else {
        fontMap = new int[src->mFaces.size()];
    }
910 911
    for (size_t i = 0; i < src->mFaces.size(); i++) {
        int font_ix = findFace(src->mFaces[i], NULL);
B
Behdad Esfahbod 已提交
912
        fontMap[i] = font_ix;
913 914 915 916 917 918 919 920 921 922 923 924 925
    }
    int x0 = mAdvance;
    for (size_t i = 0; i < src->mGlyphs.size(); i++) {
        LayoutGlyph& srcGlyph = src->mGlyphs[i];
        int font_ix = fontMap[srcGlyph.font_ix];
        unsigned int glyph_id = srcGlyph.glyph_id;
        float x = x0 + srcGlyph.x;
        float y = srcGlyph.y;
        LayoutGlyph glyph = {font_ix, glyph_id, x, y};
        mGlyphs.push_back(glyph);
    }
    for (size_t i = 0; i < src->mAdvances.size(); i++) {
        mAdvances[i + start] = src->mAdvances[i];
S
Seigo Nonaka 已提交
926 927
        if (i == 0)
          mAdvances[i + start] += extraAdvance;
928 929 930 931
    }
    MinikinRect srcBounds(src->mBounds);
    srcBounds.offset(x0, 0);
    mBounds.join(srcBounds);
S
Seigo Nonaka 已提交
932
    mAdvance += src->mAdvance + extraAdvance;
B
Behdad Esfahbod 已提交
933 934 935 936

    if (fontMap != fontMapStack) {
        delete[] fontMap;
    }
937 938
}

J
John Reck 已提交
939
void Layout::draw(minikin::Bitmap* surface, int x0, int y0, float size) const {
R
Raph Levien 已提交
940 941
    /*
    TODO: redo as MinikinPaint settings
R
Raph Levien 已提交
942 943 944 945 946
    if (mProps.hasTag(minikinHinting)) {
        int hintflags = mProps.value(minikinHinting).getIntValue();
        if (hintflags & 1) load_flags |= FT_LOAD_NO_HINTING;
        if (hintflags & 2) load_flags |= FT_LOAD_NO_AUTOHINT;
    }
R
Raph Levien 已提交
947
    */
R
Raph Levien 已提交
948 949
    for (size_t i = 0; i < mGlyphs.size(); i++) {
        const LayoutGlyph& glyph = mGlyphs[i];
R
Raph Levien 已提交
950
        MinikinFont* mf = mFaces[glyph.font_ix].font;
951
        MinikinFontFreeType* face = static_cast<MinikinFontFreeType*>(mf);
R
Raph Levien 已提交
952 953
        GlyphBitmap glyphBitmap;
        MinikinPaint paint;
954
        paint.size = size;
R
Raph Levien 已提交
955
        bool ok = face->Render(glyph.glyph_id, paint, &glyphBitmap);
956 957
#ifdef VERBOSE_DEBUG
        ALOGD("glyphBitmap.width=%d, glyphBitmap.height=%d (%d, %d) x=%f, y=%f, ok=%d",
R
Raph Levien 已提交
958
            glyphBitmap.width, glyphBitmap.height, glyphBitmap.left, glyphBitmap.top, glyph.x, glyph.y, ok);
959
#endif
R
Raph Levien 已提交
960 961 962 963
        if (ok) {
            surface->drawGlyph(glyphBitmap,
                x0 + int(floor(glyph.x + 0.5)), y0 + int(floor(glyph.y + 0.5)));
        }
R
Raph Levien 已提交
964 965 966
    }
}

967 968 969 970
size_t Layout::nGlyphs() const {
    return mGlyphs.size();
}

971
MinikinFont* Layout::getFont(int i) const {
972
    const LayoutGlyph& glyph = mGlyphs[i];
R
Raph Levien 已提交
973 974 975 976 977 978
    return mFaces[glyph.font_ix].font;
}

FontFakery Layout::getFakery(int i) const {
    const LayoutGlyph& glyph = mGlyphs[i];
    return mFaces[glyph.font_ix].fakery;
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
}

unsigned int Layout::getGlyphId(int i) const {
    const LayoutGlyph& glyph = mGlyphs[i];
    return glyph.glyph_id;
}

float Layout::getX(int i) const {
    const LayoutGlyph& glyph = mGlyphs[i];
    return glyph.x;
}

float Layout::getY(int i) const {
    const LayoutGlyph& glyph = mGlyphs[i];
    return glyph.y;
}

R
Raph Levien 已提交
996 997 998 999
float Layout::getAdvance() const {
    return mAdvance;
}

1000 1001 1002 1003 1004 1005 1006 1007
void Layout::getAdvances(float* advances) {
    memcpy(advances, &mAdvances[0], mAdvances.size() * sizeof(float));
}

void Layout::getBounds(MinikinRect* bounds) {
    bounds->set(mBounds);
}

R
Raph Levien 已提交
1008
void Layout::purgeCaches() {
S
Seigo Nonaka 已提交
1009
    android::AutoMutex _l(gMinikinLock);
R
Raph Levien 已提交
1010
    LayoutCache& layoutCache = LayoutEngine::getInstance().layoutCache;
B
Behdad Esfahbod 已提交
1011
    layoutCache.clear();
1012
    purgeHbFontCacheLocked();
R
Raph Levien 已提交
1013 1014
}

S
Seigo Nonaka 已提交
1015 1016 1017 1018 1019 1020
}  // namespace minikin

// Unable to define the static data member outside of android.
// TODO: introduce our own Singleton to drop android namespace.
namespace android {
ANDROID_SINGLETON_STATIC_INSTANCE(minikin::LayoutEngine);
R
Raph Levien 已提交
1021
}  // namespace android
S
Seigo Nonaka 已提交
1022