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