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 
28 namespace 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.
33 class HyphenEdit {
34 public:
HyphenEdit()35     HyphenEdit() : hyphen(0) { }
HyphenEdit(uint32_t hyphenInt)36     HyphenEdit(uint32_t hyphenInt) : hyphen(hyphenInt) { }
hasHyphen()37     bool hasHyphen() const { return hyphen != 0; }
38     bool operator==(const HyphenEdit &other) const { return hyphen == other.hyphen; }
39 private:
40     uint32_t hyphen;
41 };
42 
43 class 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()
47 struct MinikinPaint {
MinikinPaintMinikinPaint48     MinikinPaint() : font(0), size(0), scaleX(0), skewX(0), letterSpacing(0), paintFlags(0),
49             fakery(), fontFeatureSettings() { }
50 
skipCacheMinikinPaint51     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.
68 enum MinikinPaintFlags {
69     LinearTextFlag = 0x40,
70 };
71 
72 struct MinikinRect {
73     float mLeft, mTop, mRight, mBottom;
isEmptyMinikinRect74     bool isEmpty() const {
75         return mLeft == mRight || mTop == mBottom;
76     }
setMinikinRect77     void set(const MinikinRect& r) {
78         mLeft = r.mLeft;
79         mTop = r.mTop;
80         mRight = r.mRight;
81         mBottom = r.mBottom;
82     }
offsetMinikinRect83     void offset(float dx, float dy) {
84         mLeft += dx;
85         mTop += dy;
86         mRight += dx;
87         mBottom += dy;
88     }
setEmptyMinikinRect89     void setEmpty() {
90         mLeft = mTop = mRight = mBottom = 0;
91     }
92     void join(const MinikinRect& r);
93 };
94 
95 class MinikinFontFreeType;
96 
97 // Callback for freeing data
98 typedef void (*MinikinDestroyFunc) (void* data);
99 
100 class MinikinFont : public MinikinRefCounted {
101 public:
MinikinFont(int32_t uniqueId)102     MinikinFont(int32_t uniqueId) : mUniqueId(uniqueId) {}
103 
104     virtual ~MinikinFont();
105 
106     virtual float GetHorizontalAdvance(uint32_t glyph_id,
107         const MinikinPaint &paint) const = 0;
108 
109     virtual void GetBounds(MinikinRect* bounds, uint32_t glyph_id,
110         const MinikinPaint &paint) const = 0;
111 
112     virtual const void* GetTable(uint32_t tag, size_t* size, MinikinDestroyFunc* destroy) = 0;
113 
114     // Override if font can provide access to raw data
GetFontData()115     virtual const void* GetFontData() const {
116         return nullptr;
117     }
118 
119     // Override if font can provide access to raw data
GetFontSize()120     virtual size_t GetFontSize() const {
121         return 0;
122     }
123 
124     // Override if font can provide access to raw data.
125     // Returns index within OpenType collection
GetFontIndex()126     virtual int GetFontIndex() const {
127         return 0;
128     }
129 
MakeTag(char c1,char c2,char c3,char c4)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 
GetUniqueId()135     int32_t GetUniqueId() const { return mUniqueId; }
136 private:
137     const int32_t mUniqueId;
138 };
139 
140 }  // namespace android
141 
142 #endif  // MINIKIN_FONT_H
143