Layout.h revision 9cc9bbe1461f359f0b27c5e7645c17dda001ab1d
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 <ft2build.h> 21#include FT_FREETYPE_H 22 23#include <hb.h> 24 25#include <vector> 26 27#include <minikin/CssParse.h> 28#include <minikin/FontCollection.h> 29 30namespace android { 31 32// The Bitmap class is for debugging. We'll probably move it out 33// of here into a separate lightweight software rendering module 34// (optional, as we'd hope most clients would do their own) 35class Bitmap { 36public: 37 Bitmap(int width, int height); 38 ~Bitmap(); 39 void writePnm(std::ofstream& o) const; 40 void drawGlyph(const FT_Bitmap& bitmap, int x, int y); 41private: 42 int width; 43 int height; 44 uint8_t* buf; 45}; 46 47struct LayoutGlyph { 48 // index into mFaces and mHbFonts vectors. We could imagine 49 // moving this into a run length representation, because it's 50 // more efficient for long strings, and we'll probably need 51 // something like that for paint attributes (color, underline, 52 // fake b/i, etc), as having those per-glyph is bloated. 53 int font_ix; 54 55 unsigned int glyph_id; 56 float x; 57 float y; 58}; 59 60class Layout { 61public: 62 void dump() const; 63 void setFontCollection(const FontCollection *collection); 64 void doLayout(const uint16_t* buf, size_t nchars); 65 void draw(Bitmap*, int x0, int y0) const; 66 void setProperties(const std::string css); 67 68 // This must be called before any invocations. 69 // TODO: probably have a factory instead 70 static void init(); 71private: 72 // Find a face in the mFaces vector, or create a new entry 73 int findFace(FT_Face face); 74 75 CssProperties mProps; // TODO: want spans 76 std::vector<LayoutGlyph> mGlyphs; 77 78 // In future, this will be some kind of mapping from the 79 // identifier used to represent font-family to a font collection. 80 // But for the time being, it should be ok to have just one 81 // per layout. 82 const FontCollection *mCollection; 83 std::vector<FT_Face> mFaces; 84 std::vector<hb_font_t *> mHbFonts; 85}; 86 87} // namespace android 88 89#endif // MINIKIN_LAYOUT_H 90