Layout.h 4.2 KB
Newer Older
R
Raph Levien 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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.
 */

#ifndef MINIKIN_LAYOUT_H
#define MINIKIN_LAYOUT_H

#include <hb.h>

#include <vector>

#include <minikin/CssParse.h>
#include <minikin/FontCollection.h>
R
Raph Levien 已提交
26
#include <minikin/MinikinFontFreeType.h>
R
Raph Levien 已提交
27 28 29 30 31 32 33 34 35 36 37

namespace android {

// The Bitmap class is for debugging. We'll probably move it out
// of here into a separate lightweight software rendering module
// (optional, as we'd hope most clients would do their own)
class Bitmap {
public:
    Bitmap(int width, int height);
    ~Bitmap();
    void writePnm(std::ofstream& o) const;
R
Raph Levien 已提交
38
    void drawGlyph(const GlyphBitmap& bitmap, int x, int y);
R
Raph Levien 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
private:
    int width;
    int height;
    uint8_t* buf;
};

struct LayoutGlyph {
    // index into mFaces and mHbFonts vectors. We could imagine
    // moving this into a run length representation, because it's
    // more efficient for long strings, and we'll probably need
    // something like that for paint attributes (color, underline,
    // fake b/i, etc), as having those per-glyph is bloated.
    int font_ix;

    unsigned int glyph_id;
    float x;
    float y;
};

58 59 60
// Internal state used during layout operation
class LayoutContext;

61 62 63 64 65
// Lifecycle and threading assumptions for Layout:
// The object is assumed to be owned by a single thread; multiple threads
// may not mutate it at the same time.
// The lifetime of the FontCollection set through setFontCollection must
// extend through the lifetime of the Layout object.
R
Raph Levien 已提交
66 67 68
class Layout {
public:
    void dump() const;
69
    void setFontCollection(const FontCollection* collection);
70

71
    // Deprecated.  Will be removed.
72 73 74
    void doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
        const std::string& css);

75 76 77
    void doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
        int bidiFlags, const FontStyle &style, const MinikinPaint &paint);

78 79
    void draw(Bitmap*, int x0, int y0, float size) const;

R
Raph Levien 已提交
80
    // This must be called before any invocations.
B
Behdad Esfahbod 已提交
81
    // TODO: probably have a factory instead
R
Raph Levien 已提交
82
    static void init();
83 84 85 86 87

    // public accessors
    size_t nGlyphs() const;
    // Does not bump reference; ownership is still layout
    MinikinFont *getFont(int i) const;
R
Raph Levien 已提交
88
    FontFakery getFakery(int i) const;
89 90 91 92 93 94 95 96 97 98 99 100
    unsigned int getGlyphId(int i) const;
    float getX(int i) const;
    float getY(int i) const;

    float getAdvance() const;

    // Get advances, copying into caller-provided buffer. The size of this
    // buffer must match the length of the string (nchars arg to doLayout).
    void getAdvances(float* advances);

    void getBounds(MinikinRect* rect);

R
Raph Levien 已提交
101 102 103
    // Purge all caches, useful in low memory conditions
    static void purgeCaches();

R
Raph Levien 已提交
104 105
private:
    // Find a face in the mFaces vector, or create a new entry
R
Raph Levien 已提交
106
    int findFace(FakedFont face, LayoutContext* ctx);
107 108 109

    // Lay out a single bidi run
    void doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
R
Raph Levien 已提交
110
        bool isRtl, LayoutContext* ctx, size_t dstStart);
111 112 113 114

    // Lay out a single word
    void doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
        bool isRtl, LayoutContext* ctx, size_t bufStart);
R
Raph Levien 已提交
115

R
Raph Levien 已提交
116 117
    // Lay out a single bidi run
    void doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
118 119 120 121 122
        bool isRtl, LayoutContext* ctx);

    // Append another layout (for example, cached value) into this one
    void appendLayout(Layout* src, size_t start);

R
Raph Levien 已提交
123
    std::vector<LayoutGlyph> mGlyphs;
124
    std::vector<float> mAdvances;
R
Raph Levien 已提交
125

126
    const FontCollection* mCollection;
R
Raph Levien 已提交
127
    std::vector<FakedFont> mFaces;
R
Raph Levien 已提交
128
    float mAdvance;
129
    MinikinRect mBounds;
R
Raph Levien 已提交
130 131 132 133 134
};

}  // namespace android

#endif  // MINIKIN_LAYOUT_H