Layout.h revision 01f526614431e3a0a6e1a48039e00b8a9b7d6fbf
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef MINIKIN_LAYOUT_H
18#define MINIKIN_LAYOUT_H
19
20#include <hb.h>
21
22#include <vector>
23
24#include <minikin/FontCollection.h>
25#include <minikin/MinikinFontFreeType.h>
26
27namespace android {
28
29// The Bitmap class is for debugging. We'll probably move it out
30// of here into a separate lightweight software rendering module
31// (optional, as we'd hope most clients would do their own)
32class Bitmap {
33public:
34    Bitmap(int width, int height);
35    ~Bitmap();
36    void writePnm(std::ofstream& o) const;
37    void drawGlyph(const GlyphBitmap& bitmap, int x, int y);
38private:
39    int width;
40    int height;
41    uint8_t* buf;
42};
43
44struct LayoutGlyph {
45    // index into mFaces and mHbFonts vectors. We could imagine
46    // moving this into a run length representation, because it's
47    // more efficient for long strings, and we'll probably need
48    // something like that for paint attributes (color, underline,
49    // fake b/i, etc), as having those per-glyph is bloated.
50    int font_ix;
51
52    unsigned int glyph_id;
53    float x;
54    float y;
55};
56
57// Internal state used during layout operation
58class LayoutContext;
59
60enum {
61    kBidi_LTR = 0,
62    kBidi_RTL = 1,
63    kBidi_Default_LTR = 2,
64    kBidi_Default_RTL = 3,
65    kBidi_Force_LTR = 4,
66    kBidi_Force_RTL = 5,
67
68    kBidi_Mask = 0x7
69};
70
71// Lifecycle and threading assumptions for Layout:
72// The object is assumed to be owned by a single thread; multiple threads
73// may not mutate it at the same time.
74// The lifetime of the FontCollection set through setFontCollection must
75// extend through the lifetime of the Layout object.
76class Layout {
77public:
78
79    Layout() : mGlyphs(), mAdvances(), mCollection(0), mFaces(), mAdvance(0), mBounds() {
80        mBounds.setEmpty();
81    }
82
83    // Clears layout, ready to be used again
84    void reset();
85
86    void dump() const;
87    void setFontCollection(const FontCollection* collection);
88
89    void doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
90        int bidiFlags, const FontStyle &style, const MinikinPaint &paint);
91
92    void draw(Bitmap*, int x0, int y0, float size) const;
93
94    // Deprecated. Nont needed. Remove when callers are removed.
95    static void init();
96
97    // public accessors
98    size_t nGlyphs() const;
99    // Does not bump reference; ownership is still layout
100    MinikinFont *getFont(int i) const;
101    FontFakery getFakery(int i) const;
102    unsigned int getGlyphId(int i) const;
103    float getX(int i) const;
104    float getY(int i) const;
105
106    float getAdvance() const;
107
108    // Get advances, copying into caller-provided buffer. The size of this
109    // buffer must match the length of the string (nchars arg to doLayout).
110    void getAdvances(float* advances);
111
112    void getBounds(MinikinRect* rect);
113
114    // Purge all caches, useful in low memory conditions
115    static void purgeCaches();
116
117private:
118    friend class LayoutCacheKey;
119
120    // Find a face in the mFaces vector, or create a new entry
121    int findFace(FakedFont face, LayoutContext* ctx);
122
123    // Lay out a single bidi run
124    void doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
125        bool isRtl, LayoutContext* ctx, size_t dstStart);
126
127    // Lay out a single word
128    void doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
129        bool isRtl, LayoutContext* ctx, size_t bufStart);
130
131    // Lay out a single bidi run
132    void doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
133        bool isRtl, LayoutContext* ctx);
134
135    // Append another layout (for example, cached value) into this one
136    void appendLayout(Layout* src, size_t start);
137
138    std::vector<LayoutGlyph> mGlyphs;
139    std::vector<float> mAdvances;
140
141    const FontCollection* mCollection;
142    std::vector<FakedFont> mFaces;
143    float mAdvance;
144    MinikinRect mBounds;
145};
146
147}  // namespace android
148
149#endif  // MINIKIN_LAYOUT_H
150