MinikinFont.h revision aaa4e3470270496e6eb80704eadecb2cb7c56bf0
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 android { 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) { } 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), paintFlags(0), 49 fakery(), 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 uint32_t paintFlags; 61 FontFakery fakery; 62 HyphenEdit hyphenEdit; 63 std::string fontFeatureSettings; 64}; 65 66// Only a few flags affect layout, but those that do should have values 67// consistent with Android's paint flags. 68enum MinikinPaintFlags { 69 LinearTextFlag = 0x40, 70}; 71 72struct MinikinRect { 73 float mLeft, mTop, mRight, mBottom; 74 bool isEmpty() const { 75 return mLeft == mRight || mTop == mBottom; 76 } 77 void set(const MinikinRect& r) { 78 mLeft = r.mLeft; 79 mTop = r.mTop; 80 mRight = r.mRight; 81 mBottom = r.mBottom; 82 } 83 void offset(float dx, float dy) { 84 mLeft += dx; 85 mTop += dy; 86 mRight += dx; 87 mBottom += dy; 88 } 89 void setEmpty() { 90 mLeft = mTop = mRight = mBottom = 0; 91 } 92 void join(const MinikinRect& r); 93}; 94 95class MinikinFontFreeType; 96 97// Callback for freeing data 98typedef void (*MinikinDestroyFunc) (void* data); 99 100class MinikinFont : public MinikinRefCounted { 101public: 102 virtual ~MinikinFont(); 103 104 virtual float GetHorizontalAdvance(uint32_t glyph_id, 105 const MinikinPaint &paint) const = 0; 106 107 virtual void GetBounds(MinikinRect* bounds, uint32_t glyph_id, 108 const MinikinPaint &paint) const = 0; 109 110 virtual const void* GetTable(uint32_t tag, size_t* size, MinikinDestroyFunc* destroy) = 0; 111 112 // Override if font can provide access to raw data 113 virtual const void* GetFontData() const { 114 return nullptr; 115 } 116 117 // Override if font can provide access to raw data 118 virtual size_t GetFontSize() const { 119 return 0; 120 } 121 122 // Override if font can provide access to raw data. 123 // Returns index within OpenType collection 124 virtual int GetFontIndex() const { 125 return 0; 126 } 127 128 virtual int32_t GetUniqueId() const = 0; 129 130 static uint32_t MakeTag(char c1, char c2, char c3, char c4) { 131 return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) | 132 ((uint32_t)c3 << 8) | (uint32_t)c4; 133 } 134}; 135 136} // namespace android 137 138#endif // MINIKIN_FONT_H 139