MinikinFont.h revision 39654bb6441b55f99341b613fc1194121b1e740b
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_FONT_H 18#define MINIKIN_FONT_H 19 20#include <string> 21 22#include <minikin/MinikinRefCounted.h> 23#include <minikin/FontFamily.h> 24 25// An abstraction for platform fonts, allowing Minikin to be used with 26// multiple actual implementations of fonts. 27 28namespace minikin { 29 30// The hyphen edit represents an edit to the string when a word is 31// hyphenated. The most common hyphen edit is adding a "-" at the end 32// of a syllable, but nonstandard hyphenation allows for more choices. 33class HyphenEdit { 34public: 35 HyphenEdit() : hyphen(0) { } 36 HyphenEdit(uint32_t hyphenInt) : hyphen(hyphenInt) { } // NOLINT(implicit) 37 bool hasHyphen() const { return hyphen != 0; } 38 bool operator==(const HyphenEdit &other) const { return hyphen == other.hyphen; } 39private: 40 uint32_t hyphen; 41}; 42 43class MinikinFont; 44 45// Possibly move into own .h file? 46// Note: if you add a field here, either add it to LayoutCacheKey or to skipCache() 47struct MinikinPaint { 48 MinikinPaint() : font(0), size(0), scaleX(0), skewX(0), letterSpacing(0), wordSpacing(0), 49 paintFlags(0), fakery(), hyphenEdit(), fontFeatureSettings() { } 50 51 bool skipCache() const { 52 return !fontFeatureSettings.empty(); 53 } 54 55 MinikinFont *font; 56 float size; 57 float scaleX; 58 float skewX; 59 float letterSpacing; 60 float wordSpacing; 61 uint32_t paintFlags; 62 FontFakery fakery; 63 HyphenEdit hyphenEdit; 64 std::string fontFeatureSettings; 65}; 66 67// Only a few flags affect layout, but those that do should have values 68// consistent with Android's paint flags. 69enum MinikinPaintFlags { 70 LinearTextFlag = 0x40, 71}; 72 73struct MinikinRect { 74 float mLeft, mTop, mRight, mBottom; 75 bool isEmpty() const { 76 return mLeft == mRight || mTop == mBottom; 77 } 78 void set(const MinikinRect& r) { 79 mLeft = r.mLeft; 80 mTop = r.mTop; 81 mRight = r.mRight; 82 mBottom = r.mBottom; 83 } 84 void offset(float dx, float dy) { 85 mLeft += dx; 86 mTop += dy; 87 mRight += dx; 88 mBottom += dy; 89 } 90 void setEmpty() { 91 mLeft = mTop = mRight = mBottom = 0; 92 } 93 void join(const MinikinRect& r); 94}; 95 96class MinikinFontFreeType; 97 98// Callback for freeing data 99typedef void (*MinikinDestroyFunc) (void* data); 100 101class MinikinFont : public MinikinRefCounted { 102public: 103 explicit MinikinFont(int32_t uniqueId) : mUniqueId(uniqueId) {} 104 105 virtual ~MinikinFont(); 106 107 virtual float GetHorizontalAdvance(uint32_t glyph_id, 108 const MinikinPaint &paint) const = 0; 109 110 virtual void GetBounds(MinikinRect* bounds, uint32_t glyph_id, 111 const MinikinPaint &paint) const = 0; 112 113 // Override if font can provide access to raw data 114 virtual const void* GetFontData() const { 115 return nullptr; 116 } 117 118 // Override if font can provide access to raw data 119 virtual size_t GetFontSize() const { 120 return 0; 121 } 122 123 // Override if font can provide access to raw data. 124 // Returns index within OpenType collection 125 virtual int GetFontIndex() const { 126 return 0; 127 } 128 129 virtual const std::vector<minikin::FontVariation>& GetAxes() const = 0; 130 131 virtual MinikinFont* createFontWithVariation(const std::vector<FontVariation>&) const { 132 return nullptr; 133 } 134 135 static uint32_t MakeTag(char c1, char c2, char c3, char c4) { 136 return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) | 137 ((uint32_t)c3 << 8) | (uint32_t)c4; 138 } 139 140 int32_t GetUniqueId() const { return mUniqueId; } 141private: 142 const int32_t mUniqueId; 143}; 144 145} // namespace minikin 146 147#endif // MINIKIN_FONT_H 148