| 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 <memory> |
| 21 | #include <string> |
| 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 |
| 35 | // skipCache() |
| 36 | struct MinikinPaint { |
| 37 | MinikinPaint() |
| 38 | : font(nullptr), |
| 39 | size(0), |
| 40 | scaleX(0), |
| 41 | skewX(0), |
| 42 | letterSpacing(0), |
| 43 | wordSpacing(0), |
| 44 | paintFlags(0), |
| 45 | fakery(), |
| 46 | hyphenEdit(), |
| 47 | fontFeatureSettings() {} |
| 48 | |
| 49 | bool skipCache() const { return !fontFeatureSettings.empty(); } |
| 50 | |
| 51 | MinikinFont* font; |
| 52 | float size; |
| 53 | float scaleX; |
| 54 | float skewX; |
| 55 | float letterSpacing; |
| 56 | float wordSpacing; |
| 57 | uint32_t paintFlags; |
| 58 | FontFakery fakery; |
| 59 | HyphenEdit hyphenEdit; |
| 60 | std::string fontFeatureSettings; |
| 61 | }; |
| 62 | |
| 63 | // Only a few flags affect layout, but those that do should have values |
| 64 | // consistent with Android's paint flags. |
| 65 | enum MinikinPaintFlags { |
| 66 | LinearTextFlag = 0x40, |
| 67 | }; |
| 68 | |
| 69 | struct MinikinRect { |
| 70 | float mLeft, mTop, mRight, mBottom; |
| 71 | bool isEmpty() const { return mLeft == mRight || mTop == mBottom; } |
| 72 | void set(const MinikinRect& r) { |
| 73 | mLeft = r.mLeft; |
| 74 | mTop = r.mTop; |
| 75 | mRight = r.mRight; |
| 76 | mBottom = r.mBottom; |
| 77 | } |
| 78 | void offset(float dx, float dy) { |
| 79 | mLeft += dx; |
| 80 | mTop += dy; |
| 81 | mRight += dx; |
| 82 | mBottom += dy; |
| 83 | } |
| 84 | void setEmpty() { mLeft = mTop = mRight = mBottom = 0; } |
| 85 | void join(const MinikinRect& r); |
| 86 | }; |
| 87 | |
| 88 | // Callback for freeing data |
| 89 | typedef void (*MinikinDestroyFunc)(void* data); |
| 90 | |
| 91 | class MinikinFont { |
| 92 | public: |
| 93 | explicit MinikinFont(int32_t uniqueId) : mUniqueId(uniqueId) {} |
| 94 | |
| 95 | virtual ~MinikinFont(); |
| 96 | |
| 97 | virtual float GetHorizontalAdvance(uint32_t glyph_id, |
| 98 | const MinikinPaint& paint) const = 0; |
| 99 | |
| 100 | virtual void GetBounds(MinikinRect* bounds, |
| 101 | uint32_t glyph_id, |
| 102 | const MinikinPaint& paint) const = 0; |
| 103 | |
| 104 | virtual hb_face_t* CreateHarfBuzzFace() const { return nullptr; } |
| 105 | |
| 106 | virtual const std::vector<minikin::FontVariation>& GetAxes() const = 0; |
| 107 | |
| 108 | virtual std::shared_ptr<MinikinFont> createFontWithVariation( |
| 109 | const std::vector<FontVariation>&) const { |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
| 113 | static uint32_t MakeTag(char c1, char c2, char c3, char c4) { |
| 114 | return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) | ((uint32_t)c3 << 8) | |
| 115 | (uint32_t)c4; |
| 116 | } |
| 117 | |
| 118 | int32_t GetUniqueId() const { return mUniqueId; } |
| 119 | |
| 120 | private: |
| 121 | const int32_t mUniqueId; |
| 122 | }; |
| 123 | |
| 124 | } // namespace minikin |
| 125 | |
| 126 | #endif // MINIKIN_FONT_H |
| 127 | |