MinikinFont.h revision 6c4d167bff33c24c239d77ddb1044b18d180766a
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
97class MinikinFont : public MinikinRefCounted {
98public:
99    virtual bool GetGlyph(uint32_t codepoint, uint32_t *glyph) const = 0;
100
101    virtual float GetHorizontalAdvance(uint32_t glyph_id,
102        const MinikinPaint &paint) const = 0;
103
104    virtual void GetBounds(MinikinRect* bounds, uint32_t glyph_id,
105        const MinikinPaint &paint) const = 0;
106
107    // If buf is NULL, just update size
108    virtual bool GetTable(uint32_t tag, uint8_t *buf, size_t *size) = 0;
109
110    virtual int32_t GetUniqueId() const = 0;
111
112    static uint32_t MakeTag(char c1, char c2, char c3, char c4) {
113        return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) |
114            ((uint32_t)c3 << 8) | (uint32_t)c4;
115    }
116};
117
118}  // namespace android
119
120#endif  // MINIKIN_FONT_H
121