MinikinFont.h revision acd401d02981af51419f4b740abb2c41e4980fdb
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    virtual const void* GetTable(uint32_t tag, size_t* size, MinikinDestroyFunc* destroy) = 0;
114
115    // Override if font can provide access to raw data
116    virtual const void* GetFontData() const {
117        return nullptr;
118    }
119
120    // Override if font can provide access to raw data
121    virtual size_t GetFontSize() const {
122        return 0;
123    }
124
125    // Override if font can provide access to raw data.
126    // Returns index within OpenType collection
127    virtual int GetFontIndex() const {
128        return 0;
129    }
130
131    static uint32_t MakeTag(char c1, char c2, char c3, char c4) {
132        return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) |
133            ((uint32_t)c3 << 8) | (uint32_t)c4;
134    }
135
136    int32_t GetUniqueId() const { return mUniqueId; }
137private:
138    const int32_t mUniqueId;
139};
140
141}  // namespace minikin
142
143#endif  // MINIKIN_FONT_H
144