1/*
2 * Copyright (C) 2012 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 ANDROID_HWUI_FONT_H
18#define ANDROID_HWUI_FONT_H
19
20#include <utils/KeyedVector.h>
21
22#include <SkScalerContext.h>
23#include <SkPaint.h>
24#include <SkPathMeasure.h>
25
26#include "CachedGlyphInfo.h"
27#include "../Rect.h"
28
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Font
34///////////////////////////////////////////////////////////////////////////////
35
36class FontRenderer;
37
38/**
39 * Represents a font, defined by a Skia font id and a font size. A font is used
40 * to generate glyphs and cache them in the FontState.
41 */
42class Font {
43public:
44    enum Style {
45        kFakeBold = 1
46    };
47
48    ~Font();
49
50    /**
51     * Renders the specified string of text.
52     * If bitmap is specified, it will be used as the render target
53     */
54    void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
55            int numGlyphs, int x, int y, uint8_t *bitmap = NULL,
56            uint32_t bitmapW = 0, uint32_t bitmapH = 0);
57
58    void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
59            int numGlyphs, int x, int y, const float* positions);
60
61    void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
62            int numGlyphs, SkPath* path, float hOffset, float vOffset);
63
64    /**
65     * Creates a new font associated with the specified font state.
66     */
67    static Font* create(FontRenderer* state, uint32_t fontId, float fontSize,
68            int flags, uint32_t italicStyle, uint32_t scaleX, SkPaint::Style style,
69            uint32_t strokeWidth);
70
71private:
72    friend class FontRenderer;
73    typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*,
74            uint32_t, uint32_t, Rect*, const float*);
75
76    enum RenderMode {
77        FRAMEBUFFER,
78        BITMAP,
79        MEASURE,
80    };
81
82    void precache(SkPaint* paint, const char* text, int numGlyphs);
83
84    void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
85            int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
86            uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions);
87
88    void measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
89            int numGlyphs, Rect *bounds, const float* positions);
90
91    Font(FontRenderer* state, uint32_t fontId, float fontSize, int flags, uint32_t italicStyle,
92            uint32_t scaleX, SkPaint::Style style, uint32_t strokeWidth);
93
94    // Cache of glyphs
95    DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
96
97    void invalidateTextureCache(CacheTexture* cacheTexture = NULL);
98
99    CachedGlyphInfo* cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching);
100    void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
101            bool precaching);
102
103    void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
104            uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
105            Rect* bounds, const float* pos);
106    void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
107            uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
108            Rect* bounds, const float* pos);
109    void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
110            uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
111            Rect* bounds, const float* pos);
112    void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
113            SkPathMeasure& measure, SkPoint* position, SkVector* tangent);
114
115    CachedGlyphInfo* getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching = false);
116
117    FontRenderer* mState;
118    uint32_t mFontId;
119    float mFontSize;
120    int mFlags;
121    uint32_t mItalicStyle;
122    uint32_t mScaleX;
123    SkPaint::Style mStyle;
124    uint32_t mStrokeWidth;
125};
126
127}; // namespace uirenderer
128}; // namespace android
129
130#endif // ANDROID_HWUI_FONT_H
131