Layout.cpp 14.0 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 18 19
#define LOG_TAG "Minikin"
#include <cutils/log.h>

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

R
Raph Levien 已提交
27
#include <unicode/ubidi.h>
28 29
#include <hb-icu.h>

R
Raph Levien 已提交
30
#include "MinikinInternal.h"
R
Raph Levien 已提交
31
#include <minikin/MinikinFontFreeType.h>
R
Raph Levien 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <minikin/Layout.h>

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

namespace android {

// TODO: globals are not cool, move to a factory-ish object
hb_buffer_t* buffer = 0;

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();
}

R
Raph Levien 已提交
58
void Bitmap::drawGlyph(const GlyphBitmap& bitmap, int x, int y) {
R
Raph Levien 已提交
59
    int bmw = bitmap.width;
R
Raph Levien 已提交
60 61 62
    int bmh = bitmap.height;
    x += bitmap.left;
    y -= bitmap.top;
R
Raph Levien 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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;
    }
}

80 81 82 83 84 85 86 87 88 89 90 91
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);
    }
}

// TODO: the actual initialization is deferred, maybe make this explicit
R
Raph Levien 已提交
92 93 94 95 96 97 98 99
void Layout::init() {
}

void Layout::setFontCollection(const FontCollection *collection) {
    mCollection = collection;
}

hb_blob_t* referenceTable(hb_face_t* face, hb_tag_t tag, void* userData)  {
R
Raph Levien 已提交
100 101 102 103
    MinikinFont* font = reinterpret_cast<MinikinFont *>(userData);
    size_t length = 0;
    bool ok = font->GetTable(tag, NULL, &length);
    if (!ok) {
R
Raph Levien 已提交
104 105 106 107 108 109
        return 0;
    }
    char *buffer = reinterpret_cast<char*>(malloc(length));
    if (!buffer) {
        return 0;
    }
R
Raph Levien 已提交
110 111 112 113
    ok = font->GetTable(tag, reinterpret_cast<uint8_t*>(buffer), &length);
    printf("referenceTable %c%c%c%c length=%d %d\n",
        (tag >>24) & 0xff, (tag>>16)&0xff, (tag>>8)&0xff, tag&0xff, length, ok);
    if (!ok) {
R
Raph Levien 已提交
114 115 116 117 118 119 120 121 122
        free(buffer);
        return 0;
    }
    return hb_blob_create(const_cast<char*>(buffer), length,
        HB_MEMORY_MODE_WRITABLE, buffer, free);
}

static hb_bool_t harfbuzzGetGlyph(hb_font_t* hbFont, void* fontData, hb_codepoint_t unicode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* userData)
{
R
Raph Levien 已提交
123 124 125 126 127 128 129 130
    MinikinPaint* paint = reinterpret_cast<MinikinPaint *>(fontData);
    MinikinFont* font = paint->font;
    uint32_t glyph_id;
    bool ok = font->GetGlyph(unicode, &glyph_id);
    if (ok) {
        *glyph = glyph_id;
    }
    return ok;
R
Raph Levien 已提交
131 132 133 134
}

static hb_position_t harfbuzzGetGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, void* userData)
{
R
Raph Levien 已提交
135 136 137 138
    MinikinPaint* paint = reinterpret_cast<MinikinPaint *>(fontData);
    MinikinFont* font = paint->font;
    float advance = font->GetHorizontalAdvance(glyph, *paint);
    return 256 * advance + 0.5;
R
Raph Levien 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
}

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)
{
    // Just return true, following the way that Harfbuzz-FreeType
    // implementation does.
    return true;
}

hb_font_funcs_t* getHbFontFuncs() {
    static hb_font_funcs_t* hbFontFuncs = 0;

    if (hbFontFuncs == 0) {
        hbFontFuncs = hb_font_funcs_create();
        hb_font_funcs_set_glyph_func(hbFontFuncs, harfbuzzGetGlyph, 0, 0);
        hb_font_funcs_set_glyph_h_advance_func(hbFontFuncs, harfbuzzGetGlyphHorizontalAdvance, 0, 0);
        hb_font_funcs_set_glyph_h_origin_func(hbFontFuncs, harfbuzzGetGlyphHorizontalOrigin, 0, 0);
        hb_font_funcs_make_immutable(hbFontFuncs);
    }
    return hbFontFuncs;
}

R
Raph Levien 已提交
161 162
hb_font_t* create_hb_font(MinikinFont* minikinFont, MinikinPaint* minikinPaint) {
    hb_face_t* face = hb_face_create_for_tables(referenceTable, minikinFont, NULL);
R
Raph Levien 已提交
163
    hb_font_t* font = hb_font_create(face);
R
Raph Levien 已提交
164
    hb_font_set_funcs(font, getHbFontFuncs(), minikinPaint, 0);
R
Raph Levien 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    // TODO: manage ownership of face
    return font;
}

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;
    }
}

// A couple of things probably need to change:
// 1. Deal with multiple sizes in a layout
// 2. We'll probably store FT_Face as primary and then use a cache
// for the hb fonts
R
Raph Levien 已提交
190
int Layout::findFace(MinikinFont* face, MinikinPaint* paint) {
R
Raph Levien 已提交
191 192 193 194 195 196 197
    unsigned int ix;
    for (ix = 0; ix < mFaces.size(); ix++) {
        if (mFaces[ix] == face) {
            return ix;
        }
    }
    mFaces.push_back(face);
R
Raph Levien 已提交
198
    hb_font_t *font = create_hb_font(face, paint);
R
Raph Levien 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    mHbFonts.push_back(font);
    return ix;
}

static FontStyle styleFromCss(const CssProperties &props) {
    int weight = 4;
    if (props.hasTag(fontWeight)) {
        weight = props.value(fontWeight).getIntValue() / 100;
    }
    bool italic = false;
    if (props.hasTag(fontStyle)) {
        italic = props.value(fontStyle).getIntValue() != 0;
    }
    return FontStyle(weight, italic);
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
static hb_script_t codePointToScript(hb_codepoint_t codepoint) {
    static hb_unicode_funcs_t *u = 0;
    if (!u) {
        u = hb_icu_get_unicode_funcs();
    }
    return hb_unicode_script(u, codepoint);
}

static hb_codepoint_t decodeUtf16(const uint16_t *chars, size_t len, ssize_t *iter) {
    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;
            }
            (*iter) -= 2;
            return ~0u;
        } else {
            (*iter)--;
            return ~0u;
        }
    } else {
        return v;
    }
}

static hb_script_t getScriptRun(const uint16_t *chars, size_t len, ssize_t *iter) {
    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;
}

R
Raph Levien 已提交
279 280
// TODO: API should probably take context
void Layout::doLayout(const uint16_t* buf, size_t nchars) {
R
Raph Levien 已提交
281
    AutoMutex _l(gMinikinLock);
282 283 284
    if (buffer == 0) {
        buffer = hb_buffer_create();
    }
R
Raph Levien 已提交
285 286 287 288 289 290
    FT_Error error;

    vector<FontCollection::Run> items;
    FontStyle style = styleFromCss(mProps);
    mCollection->itemize(buf, nchars, style, &items);

R
Raph Levien 已提交
291 292 293
    MinikinPaint paint;
    double size = mProps.value(fontSize).getFloatValue();
    paint.size = size;
R
Raph Levien 已提交
294 295 296 297 298
    int bidiFlags = mProps.hasTag(minikinBidi) ? mProps.value(minikinBidi).getIntValue() : 0;
    bool isRtl = (bidiFlags & 1) != 0;  // TODO: do real bidi algo
    if (isRtl) {
        std::reverse(items.begin(), items.end());
    }
R
Raph Levien 已提交
299

R
Raph Levien 已提交
300 301 302
    mGlyphs.clear();
    mFaces.clear();
    mHbFonts.clear();
303 304 305
    mBounds.setEmpty();
    mAdvances.clear();
    mAdvances.resize(nchars, 0);
R
Raph Levien 已提交
306 307 308 309
    float x = 0;
    float y = 0;
    for (size_t run_ix = 0; run_ix < items.size(); run_ix++) {
        FontCollection::Run &run = items[run_ix];
R
Raph Levien 已提交
310 311
        int font_ix = findFace(run.font, &paint);
        paint.font = mFaces[font_ix];
R
Raph Levien 已提交
312
        hb_font_t *hbFont = mHbFonts[font_ix];
313 314 315 316
        if (paint.font == NULL) {
            // TODO: should log what went wrong
            continue;
        }
R
Raph Levien 已提交
317 318 319 320
#ifdef VERBOSE
        std::cout << "Run " << run_ix << ", font " << font_ix <<
            " [" << run.start << ":" << run.end << "]" << std::endl;
#endif
R
Raph Levien 已提交
321 322
        hb_font_set_ppem(hbFont, size, size);
        hb_font_set_scale(hbFont, HBFloatToFixed(size), HBFloatToFixed(size));
R
Raph Levien 已提交
323

R
Raph Levien 已提交
324 325 326
        // 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 已提交
327 328
        ssize_t srunend;
        for (ssize_t srunstart = run.start; srunstart < run.end; srunstart = srunend) {
329 330 331 332 333
            srunend = srunstart;
            hb_script_t script = getScriptRun(buf, run.end, &srunend);

            hb_buffer_reset(buffer);
            hb_buffer_set_script(buffer, script);
R
Raph Levien 已提交
334
            hb_buffer_set_direction(buffer, isRtl? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
            hb_buffer_add_utf16(buffer, buf, nchars, srunstart, srunend - srunstart);
            hb_shape(hbFont, buffer, NULL, 0);
            unsigned int numGlyphs;
            hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, &numGlyphs);
            hb_glyph_position_t *positions = hb_buffer_get_glyph_positions(buffer, NULL);
            for (unsigned int i = 0; i < numGlyphs; i++) {
    #ifdef VERBOSE
                std::cout << positions[i].x_advance << " " << positions[i].y_advance << " " << positions[i].x_offset << " " << positions[i].y_offset << std::endl;            std::cout << "DoLayout " << info[i].codepoint <<
                ": " << HBFixedToFloat(positions[i].x_advance) << "; " << positions[i].x_offset << ", " << positions[i].y_offset << std::endl;
    #endif
                hb_codepoint_t glyph_ix = info[i].codepoint;
                float xoff = HBFixedToFloat(positions[i].x_offset);
                float yoff = HBFixedToFloat(positions[i].y_offset);
                LayoutGlyph glyph = {font_ix, glyph_ix, x + xoff, y + yoff};
                mGlyphs.push_back(glyph);
                float xAdvance = HBFixedToFloat(positions[i].x_advance);
                MinikinRect glyphBounds;
                paint.font->GetBounds(&glyphBounds, glyph_ix, paint);
                glyphBounds.offset(x + xoff, y + yoff);
                mBounds.join(glyphBounds);
                size_t cluster = info[i].cluster;
                mAdvances[cluster] += xAdvance;
                x += xAdvance;
            }
R
Raph Levien 已提交
359 360
        }
    }
R
Raph Levien 已提交
361
    mAdvance = x;
R
Raph Levien 已提交
362 363 364
}

void Layout::draw(Bitmap* surface, int x0, int y0) const {
R
Raph Levien 已提交
365 366
    /*
    TODO: redo as MinikinPaint settings
R
Raph Levien 已提交
367 368 369 370 371
    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 已提交
372
    */
R
Raph Levien 已提交
373 374
    for (size_t i = 0; i < mGlyphs.size(); i++) {
        const LayoutGlyph& glyph = mGlyphs[i];
R
Raph Levien 已提交
375 376 377 378 379 380 381 382 383 384 385 386
        MinikinFont *mf = mFaces[glyph.font_ix];
        MinikinFontFreeType *face = static_cast<MinikinFontFreeType *>(mf);
        GlyphBitmap glyphBitmap;
        MinikinPaint paint;
        paint.size = mProps.value(fontSize).getFloatValue();
        bool ok = face->Render(glyph.glyph_id, paint, &glyphBitmap);
        printf("glyphBitmap.width=%d, glyphBitmap.height=%d (%d, %d) x=%f, y=%f, ok=%d\n",
            glyphBitmap.width, glyphBitmap.height, glyphBitmap.left, glyphBitmap.top, glyph.x, glyph.y, ok);
        if (ok) {
            surface->drawGlyph(glyphBitmap,
                x0 + int(floor(glyph.x + 0.5)), y0 + int(floor(glyph.y + 0.5)));
        }
R
Raph Levien 已提交
387 388 389 390 391 392 393
    }
}

void Layout::setProperties(string css) {
    mProps.parse(css);
}

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
size_t Layout::nGlyphs() const {
    return mGlyphs.size();
}

MinikinFont *Layout::getFont(int i) const {
    const LayoutGlyph& glyph = mGlyphs[i];
    return mFaces[glyph.font_ix];
}

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 已提交
418 419 420 421
float Layout::getAdvance() const {
    return mAdvance;
}

422 423 424 425 426 427 428 429
void Layout::getAdvances(float* advances) {
    memcpy(advances, &mAdvances[0], mAdvances.size() * sizeof(float));
}

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

R
Raph Levien 已提交
430
}  // namespace android