Layout.h revision 22e41754f6470ff1f4c0e0a56d01f7f555b59e21
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/CssParse.h>
25#include <minikin/FontCollection.h>
26#include <minikin/MinikinFontFreeType.h>
27
28namespace android {
29
30// The Bitmap class is for debugging. We'll probably move it out
31// of here into a separate lightweight software rendering module
32// (optional, as we'd hope most clients would do their own)
33class Bitmap {
34public:
35    Bitmap(int width, int height);
36    ~Bitmap();
37    void writePnm(std::ofstream& o) const;
38    void drawGlyph(const GlyphBitmap& bitmap, int x, int y);
39private:
40    int width;
41    int height;
42    uint8_t* buf;
43};
44
45struct LayoutGlyph {
46    // index into mFaces and mHbFonts vectors. We could imagine
47    // moving this into a run length representation, because it's
48    // more efficient for long strings, and we'll probably need
49    // something like that for paint attributes (color, underline,
50    // fake b/i, etc), as having those per-glyph is bloated.
51    int font_ix;
52
53    unsigned int glyph_id;
54    float x;
55    float y;
56};
57
58// Internal state used during layout operation
59class LayoutContext;
60
61// Lifecycle and threading assumptions for Layout:
62// The object is assumed to be owned by a single thread; multiple threads
63// may not mutate it at the same time.
64// The lifetime of the FontCollection set through setFontCollection must
65// extend through the lifetime of the Layout object.
66class Layout {
67public:
68    void dump() const;
69    void setFontCollection(const FontCollection* collection);
70
71    // deprecated - missing functionality
72    void doLayout(const uint16_t* buf, size_t nchars);
73
74    void doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
75        const std::string& css);
76
77    void draw(Bitmap*, int x0, int y0, float size) const;
78
79    // deprecated - pass as argument to doLayout instead
80    void setProperties(const std::string& css);
81
82    // This must be called before any invocations.
83	// TODO: probably have a factory instead
84    static void init();
85
86    // public accessors
87    size_t nGlyphs() const;
88    // Does not bump reference; ownership is still layout
89    MinikinFont *getFont(int i) const;
90    FontFakery getFakery(int i) const;
91    unsigned int getGlyphId(int i) const;
92    float getX(int i) const;
93    float getY(int i) const;
94
95    float getAdvance() const;
96
97    // Get advances, copying into caller-provided buffer. The size of this
98    // buffer must match the length of the string (nchars arg to doLayout).
99    void getAdvances(float* advances);
100
101    void getBounds(MinikinRect* rect);
102
103    // Purge all caches, useful in low memory conditions
104    static void purgeCaches();
105
106private:
107    // Find a face in the mFaces vector, or create a new entry
108    int findFace(FakedFont face, LayoutContext* ctx);
109
110    // Lay out a single bidi run
111    void doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
112        bool isRtl, LayoutContext* ctx, size_t dstStart);
113
114    // Lay out a single word
115    void doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
116        bool isRtl, LayoutContext* ctx, size_t bufStart);
117
118    // Lay out a single bidi run
119    void doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
120        bool isRtl, LayoutContext* ctx);
121
122    // Append another layout (for example, cached value) into this one
123    void appendLayout(Layout* src, size_t start);
124
125    // deprecated - remove when setProperties is removed
126    std::string mCssString;
127
128    std::vector<LayoutGlyph> mGlyphs;
129    std::vector<float> mAdvances;
130
131    const FontCollection* mCollection;
132    std::vector<FakedFont> mFaces;
133    float mAdvance;
134    MinikinRect mBounds;
135};
136
137}  // namespace android
138
139#endif  // MINIKIN_LAYOUT_H
140