MinikinFont.h revision b80c1f19c58b927820a8a24bf2218e5645724608
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?
30struct MinikinPaint {
31    MinikinFont *font;
32    float size;
33    // todo: skew, stretch, hinting
34};
35
36struct MinikinRect {
37    float mLeft, mTop, mRight, mBottom;
38    bool isEmpty() const {
39        return mLeft == mRight || mTop == mBottom;
40    }
41    void set(const MinikinRect& r) {
42        mLeft = r.mLeft;
43        mTop = r.mTop;
44        mRight = r.mRight;
45        mBottom = r.mBottom;
46    }
47    void offset(float dx, float dy) {
48        mLeft += dx;
49        mTop += dy;
50        mRight += dx;
51        mBottom += dy;
52    }
53    void setEmpty() {
54        mLeft = mTop = mRight = mBottom = 0;
55    }
56    void join(const MinikinRect& r);
57};
58
59class MinikinFontFreeType;
60
61class MinikinFont : public MinikinRefCounted {
62public:
63    virtual bool GetGlyph(uint32_t codepoint, uint32_t *glyph) const = 0;
64
65    virtual float GetHorizontalAdvance(uint32_t glyph_id,
66        const MinikinPaint &paint) const = 0;
67
68    virtual void GetBounds(MinikinRect* bounds, uint32_t glyph_id,
69        const MinikinPaint &paint) const = 0;
70
71    // If buf is NULL, just update size
72    virtual bool GetTable(uint32_t tag, uint8_t *buf, size_t *size) = 0;
73
74    virtual int32_t GetUniqueId() const = 0;
75
76    static uint32_t MakeTag(char c1, char c2, char c3, char c4) {
77        return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) |
78            ((uint32_t)c3 << 8) | (uint32_t)c4;
79    }
80};
81
82}  // namespace android
83
84#endif  // MINIKIN_FONT_H
85