rsFont.h revision 2e8665de7c0eb4514c67baf8693d61c892e5303d
1/*
2 * Copyright (C) 2009 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_RS_FONT_H
18#define ANDROID_RS_FONT_H
19
20#include "RenderScript.h"
21#include "rsStream.h"
22#include <utils/String8.h>
23#include <utils/Vector.h>
24#include <utils/KeyedVector.h>
25
26#include <ft2build.h>
27#include FT_FREETYPE_H
28
29// ---------------------------------------------------------------------------
30namespace android {
31
32namespace renderscript {
33
34// Gamma (>= 1.0, <= 10.0)
35#define PROPERTY_TEXT_GAMMA "ro.text_gamma"
36#define PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD "ro.text_gamma.black_threshold"
37#define PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD "ro.text_gamma.white_threshold"
38
39#define DEFAULT_TEXT_GAMMA 1.4f
40#define DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD 64
41#define DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD 192
42
43class FontState;
44
45class Font : public ObjectBase {
46public:
47    enum RenderMode {
48        FRAMEBUFFER,
49        BITMAP,
50        MEASURE,
51    };
52
53    struct Rect {
54        int32_t left;
55        int32_t top;
56        int32_t right;
57        int32_t bottom;
58        void set(int32_t l, int32_t r, int32_t t, int32_t b) {
59            left = l;
60            right = r;
61            top = t;
62            bottom = b;
63        }
64    };
65
66    ~Font();
67
68    // Currently files do not get serialized,
69    // but we need to inherit from ObjectBase for ref tracking
70    virtual void serialize(OStream *stream) const {
71    }
72    virtual RsA3DClassID getClassId() const {
73        return RS_A3D_CLASS_ID_UNKNOWN;
74    }
75
76    static Font * create(Context *rsc, const char *name, float fontSize, uint32_t dpi,
77                         const void *data = NULL, uint32_t dataLen = 0);
78
79protected:
80
81    friend class FontState;
82
83    // Pointer to the utf data, length of data, where to start, number of glyphs ot read
84    // (each glyph may be longer than a char because we are dealing with utf data)
85    // Last two variables are the initial pen position
86    void renderUTF(const char *text, uint32_t len, int32_t x, int32_t y,
87                   uint32_t start, int32_t numGlyphs,
88                   RenderMode mode = FRAMEBUFFER, Rect *bounds = NULL,
89                   uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
90
91    void invalidateTextureCache();
92    struct CachedGlyphInfo
93    {
94        // Has the cache been invalidated?
95        bool mIsValid;
96        // Location of the cached glyph in the bitmap
97        // in case we need to resize the texture
98        uint32_t mBitmapMinX;
99        uint32_t mBitmapMinY;
100        uint32_t mBitmapWidth;
101        uint32_t mBitmapHeight;
102        // Also cache texture coords for the quad
103        float mBitmapMinU;
104        float mBitmapMinV;
105        float mBitmapMaxU;
106        float mBitmapMaxV;
107        // Minimize how much we call freetype
108        FT_UInt mGlyphIndex;
109        FT_Vector mAdvance;
110        // Values below contain a glyph's origin in the bitmap
111        FT_Int mBitmapLeft;
112        FT_Int mBitmapTop;
113    };
114
115    String8 mFontName;
116    float mFontSize;
117    uint32_t mDpi;
118
119    Font(Context *rsc);
120    bool init(const char *name, float fontSize, uint32_t dpi, const void *data = NULL, uint32_t dataLen = 0);
121
122    virtual void preDestroy() const;
123    FT_Face mFace;
124    bool mInitialized;
125    bool mHasKerning;
126
127    DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs;
128    CachedGlyphInfo* getCachedUTFChar(int32_t utfChar);
129
130    CachedGlyphInfo *cacheGlyph(uint32_t glyph);
131    void updateGlyphCache(CachedGlyphInfo *glyph);
132    void measureCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y, Rect *bounds);
133    void drawCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y);
134    void drawCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y,
135                         uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH);
136};
137
138class FontState {
139public:
140    FontState();
141    ~FontState();
142
143    void init(Context *rsc);
144    void deinit(Context *rsc);
145
146    ObjectBaseRef<Font> mDefault;
147    ObjectBaseRef<Font> mLast;
148
149    void renderText(const char *text, uint32_t len, int32_t x, int32_t y,
150                    uint32_t startIndex = 0, int numGlyphs = -1,
151                    Font::RenderMode mode = Font::FRAMEBUFFER,
152                    Font::Rect *bounds = NULL,
153                    uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
154
155    void measureText(const char *text, uint32_t len, Font::Rect *bounds);
156
157    void setFontColor(float r, float g, float b, float a);
158    void getFontColor(float *r, float *g, float *b, float *a) const;
159
160protected:
161
162    friend class Font;
163
164    struct CacheTextureLine {
165        uint32_t mMaxHeight;
166        uint32_t mMaxWidth;
167        uint32_t mCurrentRow;
168        uint32_t mCurrentCol;
169        bool mDirty;
170
171        CacheTextureLine(uint32_t maxHeight, uint32_t maxWidth, uint32_t currentRow, uint32_t currentCol)
172            : mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow),
173              mCurrentCol(currentCol), mDirty(false)  {
174        }
175
176        bool fitBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) {
177            if ((uint32_t)bitmap->rows > mMaxHeight) {
178                return false;
179            }
180
181            if (mCurrentCol + (uint32_t)bitmap->width < mMaxWidth) {
182                *retOriginX = mCurrentCol;
183                *retOriginY = mCurrentRow;
184                mCurrentCol += bitmap->width;
185                mDirty = true;
186               return true;
187            }
188
189            return false;
190        }
191    };
192
193    Vector<CacheTextureLine*> mCacheLines;
194    uint32_t getRemainingCacheCapacity();
195
196    void precacheLatin(Font *font);
197    String8 mLatinPrecache;
198
199    Context *mRSC;
200
201    struct {
202        float mFontColor[4];
203        float mGamma;
204    } mConstants;
205    bool mConstantsDirty;
206
207    float mBlackGamma;
208    float mWhiteGamma;
209
210    float mBlackThreshold;
211    float mWhiteThreshold;
212
213    // Free type library, we only need one copy
214    FT_Library mLibrary;
215    FT_Library getLib();
216    Vector<Font*> mActiveFonts;
217
218    // Render state for the font
219    ObjectBaseRef<Allocation> mFontShaderFConstant;
220    ObjectBaseRef<ProgramFragment> mFontShaderF;
221    ObjectBaseRef<Sampler> mFontSampler;
222    ObjectBaseRef<ProgramStore> mFontProgramStore;
223    void initRenderState();
224
225    // Texture to cache glyph bitmaps
226    ObjectBaseRef<Allocation> mTextTexture;
227    void initTextTexture();
228    const uint8_t* getTextTextureData() const {
229        return (uint8_t*)mTextTexture->getPtr();
230    }
231
232    bool cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY);
233    const Type* getCacheTextureType() {
234        return mTextTexture->getType();
235    }
236
237    void flushAllAndInvalidate();
238
239    // Pointer to vertex data to speed up frame to frame work
240    float *mTextMeshPtr;
241    uint32_t mCurrentQuadIndex;
242    uint32_t mMaxNumberOfQuads;
243
244    void initVertexArrayBuffers();
245    ObjectBaseRef<Allocation> mIndexBuffer;
246    ObjectBaseRef<Allocation> mVertexArray;
247
248
249    bool mInitialized;
250
251    void checkInit();
252
253    void issueDrawCommand();
254
255    void appendMeshQuad(float x1, float y1, float z1,
256                        float u1, float v1,
257                        float x2, float y2, float z2,
258                        float u2, float v2,
259                        float x3, float y3, float z3,
260                        float u3, float v3,
261                        float x4, float y4, float z4,
262                        float u4, float v4);
263};
264
265}
266}
267
268#endif
269