Layout.h revision c924dd126db5d029af407ae6dd1a41c3652a6a64
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
60// Lifecycle and threading assumptions for Layout:
61// The object is assumed to be owned by a single thread; multiple threads
62// may not mutate it at the same time.
63// The lifetime of the FontCollection set through setFontCollection must
64// extend through the lifetime of the Layout object.
65class Layout {
66public:
67    void dump() const;
68    void setFontCollection(const FontCollection* collection);
69
70    void doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
71        int bidiFlags, const FontStyle &style, const MinikinPaint &paint);
72
73    void draw(Bitmap*, int x0, int y0, float size) const;
74
75    // This must be called before any invocations.
76    // TODO: probably have a factory instead
77    static void init();
78
79    // public accessors
80    size_t nGlyphs() const;
81    // Does not bump reference; ownership is still layout
82    MinikinFont *getFont(int i) const;
83    FontFakery getFakery(int i) const;
84    unsigned int getGlyphId(int i) const;
85    float getX(int i) const;
86    float getY(int i) const;
87
88    float getAdvance() const;
89
90    // Get advances, copying into caller-provided buffer. The size of this
91    // buffer must match the length of the string (nchars arg to doLayout).
92    void getAdvances(float* advances);
93
94    void getBounds(MinikinRect* rect);
95
96    // Purge all caches, useful in low memory conditions
97    static void purgeCaches();
98
99private:
100    // Find a face in the mFaces vector, or create a new entry
101    int findFace(FakedFont face, LayoutContext* ctx);
102
103    // Lay out a single bidi run
104    void doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
105        bool isRtl, LayoutContext* ctx, size_t dstStart);
106
107    // Lay out a single word
108    void doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
109        bool isRtl, LayoutContext* ctx, size_t bufStart);
110
111    // Lay out a single bidi run
112    void doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
113        bool isRtl, LayoutContext* ctx);
114
115    // Append another layout (for example, cached value) into this one
116    void appendLayout(Layout* src, size_t start);
117
118    std::vector<LayoutGlyph> mGlyphs;
119    std::vector<float> mAdvances;
120
121    const FontCollection* mCollection;
122    std::vector<FakedFont> mFaces;
123    float mAdvance;
124    MinikinRect mBounds;
125};
126
127}  // namespace android
128
129#endif  // MINIKIN_LAYOUT_H
130