Layout.h revision c31e3883456e018d742e9f29815ba5ff8b315ea1
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// Lifecycle and threading assumptions for Layout: 59// The object is assumed to be owned by a single thread; multiple threads 60// may not mutate it at the same time. 61// The lifetime of the FontCollection set through setFontCollection must 62// extend through the lifetime of the Layout object. 63class Layout { 64public: 65 ~Layout(); 66 67 void dump() const; 68 void setFontCollection(const FontCollection* collection); 69 void doLayout(const uint16_t* buf, size_t nchars); 70 void draw(Bitmap*, int x0, int y0) const; 71 void setProperties(const std::string css); 72 73 // This must be called before any invocations. 74 // TODO: probably have a factory instead 75 static void init(); 76 77 // public accessors 78 size_t nGlyphs() const; 79 // Does not bump reference; ownership is still layout 80 MinikinFont *getFont(int i) const; 81 unsigned int getGlyphId(int i) const; 82 float getX(int i) const; 83 float getY(int i) const; 84 85 float getAdvance() const; 86 87 // Get advances, copying into caller-provided buffer. The size of this 88 // buffer must match the length of the string (nchars arg to doLayout). 89 void getAdvances(float* advances); 90 91 void getBounds(MinikinRect* rect); 92 93private: 94 // Find a face in the mFaces vector, or create a new entry 95 int findFace(MinikinFont* face, MinikinPaint* paint); 96 97 CssProperties mProps; // TODO: want spans 98 std::vector<LayoutGlyph> mGlyphs; 99 std::vector<float> mAdvances; 100 101 // In future, this will be some kind of mapping from the 102 // identifier used to represent font-family to a font collection. 103 // But for the time being, it should be ok to have just one 104 // per layout. 105 const FontCollection* mCollection; 106 std::vector<MinikinFont *> mFaces; 107 std::vector<hb_font_t *> mHbFonts; 108 float mAdvance; 109 MinikinRect mBounds; 110}; 111 112} // namespace android 113 114#endif // MINIKIN_LAYOUT_H 115