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 
27 namespace 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)
32 class Bitmap {
33 public:
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);
38 private:
39     int width;
40     int height;
41     uint8_t* buf;
42 };
43 
44 struct 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
58 class 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.
65 class Layout {
66 public:
67 
Layout()68     Layout() : mGlyphs(), mAdvances(), mCollection(0), mFaces(), mAdvance(0), mBounds() {
69         mBounds.setEmpty();
70     }
71 
72     // Clears layout, ready to be used again
73     void reset();
74 
75     void dump() const;
76     void setFontCollection(const FontCollection* collection);
77 
78     void doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
79         int bidiFlags, const FontStyle &style, const MinikinPaint &paint);
80 
81     void draw(Bitmap*, int x0, int y0, float size) const;
82 
83     // Deprecated. Nont needed. Remove when callers are removed.
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 
106 private:
107     friend class LayoutCacheKey;
108 
109     // Find a face in the mFaces vector, or create a new entry
110     int findFace(FakedFont face, LayoutContext* ctx);
111 
112     // Lay out a single bidi run
113     void doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
114         bool isRtl, LayoutContext* ctx, size_t dstStart);
115 
116     // Lay out a single word
117     void doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
118         bool isRtl, LayoutContext* ctx, size_t bufStart);
119 
120     // Lay out a single bidi run
121     void doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
122         bool isRtl, LayoutContext* ctx);
123 
124     // Append another layout (for example, cached value) into this one
125     void appendLayout(Layout* src, size_t start);
126 
127     std::vector<LayoutGlyph> mGlyphs;
128     std::vector<float> mAdvances;
129 
130     const FontCollection* mCollection;
131     std::vector<FakedFont> mFaces;
132     float mAdvance;
133     MinikinRect mBounds;
134 };
135 
136 }  // namespace android
137 
138 #endif  // MINIKIN_LAYOUT_H
139