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