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 minikin { 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 android::GlyphBitmap& bitmap, int x, int y); 38 private: 39 int width; 40 int height; 41 uint8_t* buf; 42 }; 43 44 } // namespace minikin 45 46 namespace android { 47 48 struct LayoutGlyph { 49 // index into mFaces and mHbFonts vectors. We could imagine 50 // moving this into a run length representation, because it's 51 // more efficient for long strings, and we'll probably need 52 // something like that for paint attributes (color, underline, 53 // fake b/i, etc), as having those per-glyph is bloated. 54 int font_ix; 55 56 unsigned int glyph_id; 57 float x; 58 float y; 59 }; 60 61 // Internal state used during layout operation 62 class LayoutContext; 63 64 enum { 65 kBidi_LTR = 0, 66 kBidi_RTL = 1, 67 kBidi_Default_LTR = 2, 68 kBidi_Default_RTL = 3, 69 kBidi_Force_LTR = 4, 70 kBidi_Force_RTL = 5, 71 72 kBidi_Mask = 0x7 73 }; 74 75 // Lifecycle and threading assumptions for Layout: 76 // The object is assumed to be owned by a single thread; multiple threads 77 // may not mutate it at the same time. 78 // The lifetime of the FontCollection set through setFontCollection must 79 // extend through the lifetime of the Layout object. 80 class Layout { 81 public: 82 Layout()83 Layout() : mGlyphs(), mAdvances(), mCollection(0), mFaces(), mAdvance(0), mBounds() { 84 mBounds.setEmpty(); 85 } 86 87 // Clears layout, ready to be used again 88 void reset(); 89 90 void dump() const; 91 void setFontCollection(const FontCollection* collection); 92 93 void doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize, 94 int bidiFlags, const FontStyle &style, const MinikinPaint &paint); 95 96 void draw(minikin::Bitmap*, int x0, int y0, float size) const; 97 98 // Deprecated. Nont needed. Remove when callers are removed. 99 static void init(); 100 101 // public accessors 102 size_t nGlyphs() const; 103 // Does not bump reference; ownership is still layout 104 MinikinFont *getFont(int i) const; 105 FontFakery getFakery(int i) const; 106 unsigned int getGlyphId(int i) const; 107 float getX(int i) const; 108 float getY(int i) const; 109 110 float getAdvance() const; 111 112 // Get advances, copying into caller-provided buffer. The size of this 113 // buffer must match the length of the string (count arg to doLayout). 114 void getAdvances(float* advances); 115 116 // The i parameter is an offset within the buf relative to start, it is < count, where 117 // start and count are the parameters to doLayout getCharAdvance(size_t i)118 float getCharAdvance(size_t i) const { return mAdvances[i]; } 119 120 void getBounds(MinikinRect* rect); 121 122 // Purge all caches, useful in low memory conditions 123 static void purgeCaches(); 124 125 private: 126 friend class LayoutCacheKey; 127 128 // Find a face in the mFaces vector, or create a new entry 129 int findFace(FakedFont face, LayoutContext* ctx); 130 131 // Lay out a single bidi run 132 void doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize, 133 bool isRtl, LayoutContext* ctx, size_t dstStart); 134 135 // Lay out a single word 136 void doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize, 137 bool isRtl, LayoutContext* ctx, size_t bufStart); 138 139 // Lay out a single bidi run 140 void doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize, 141 bool isRtl, LayoutContext* ctx); 142 143 // Append another layout (for example, cached value) into this one 144 void appendLayout(Layout* src, size_t start); 145 146 std::vector<LayoutGlyph> mGlyphs; 147 std::vector<float> mAdvances; 148 149 const FontCollection* mCollection; 150 std::vector<FakedFont> mFaces; 151 float mAdvance; 152 MinikinRect mBounds; 153 }; 154 155 } // namespace android 156 157 #endif // MINIKIN_LAYOUT_H 158