MinikinFont.h revision 8e7a3dae37e9a22b2c054aec852615843d71caf6
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 <minikin/MinikinRefCounted.h>
21#include <minikin/FontFamily.h>
22
23// An abstraction for platform fonts, allowing Minikin to be used with
24// multiple actual implementations of fonts.
25
26namespace android {
27
28class MinikinFont;
29
30// Possibly move into own .h file?
31// Note: if you add a field here, also update LayoutCacheKey
32struct MinikinPaint {
33    MinikinFont *font;
34    float size;
35    float scaleX;
36    float skewX;
37    float letterSpacing;
38    uint32_t paintFlags;
39    FontFakery fakery;
40};
41
42struct MinikinRect {
43    float mLeft, mTop, mRight, mBottom;
44    bool isEmpty() const {
45        return mLeft == mRight || mTop == mBottom;
46    }
47    void set(const MinikinRect& r) {
48        mLeft = r.mLeft;
49        mTop = r.mTop;
50        mRight = r.mRight;
51        mBottom = r.mBottom;
52    }
53    void offset(float dx, float dy) {
54        mLeft += dx;
55        mTop += dy;
56        mRight += dx;
57        mBottom += dy;
58    }
59    void setEmpty() {
60        mLeft = mTop = mRight = mBottom = 0;
61    }
62    void join(const MinikinRect& r);
63};
64
65class MinikinFontFreeType;
66
67class MinikinFont : public MinikinRefCounted {
68public:
69    virtual bool GetGlyph(uint32_t codepoint, uint32_t *glyph) const = 0;
70
71    virtual float GetHorizontalAdvance(uint32_t glyph_id,
72        const MinikinPaint &paint) const = 0;
73
74    virtual void GetBounds(MinikinRect* bounds, uint32_t glyph_id,
75        const MinikinPaint &paint) const = 0;
76
77    // If buf is NULL, just update size
78    virtual bool GetTable(uint32_t tag, uint8_t *buf, size_t *size) = 0;
79
80    virtual int32_t GetUniqueId() const = 0;
81
82    static uint32_t MakeTag(char c1, char c2, char c3, char c4) {
83        return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) |
84            ((uint32_t)c3 << 8) | (uint32_t)c4;
85    }
86};
87
88}  // namespace android
89
90#endif  // MINIKIN_FONT_H
91