blob: defe38b9a781a56341847a9d8f5517d4016d7e30 [file] [log] [blame]
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -07001/*
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
34class FontState;
35
36class Font : public ObjectBase
37{
38public:
39 ~Font();
40
41 // Pointer to the utf data, length of data, where to start, number of glyphs ot read
42 // (each glyph may be longer than a char because we are dealing with utf data)
43 // Last two variables are the initial pen position
44 void renderUTF(const char *text, uint32_t len, uint32_t start, int numGlyphs, int x, int y);
45
46 // Currently files do not get serialized,
47 // but we need to inherit from ObjectBase for ref tracking
48 virtual void serialize(OStream *stream) const {
49 }
50 virtual RsA3DClassID getClassId() const {
51 return RS_A3D_CLASS_ID_UNKNOWN;
52 }
53
54 static Font * create(Context *rsc, const char *name, uint32_t fontSize, uint32_t dpi);
55
56protected:
57
58 friend class FontState;
59
60 void invalidateTextureCache();
61 struct CachedGlyphInfo
62 {
63 // Has the cache been invalidated?
64 bool mIsValid;
65 // Location of the cached glyph in the bitmap
66 // in case we need to resize the texture
67 uint32_t mBitmapMinX;
68 uint32_t mBitmapMinY;
69 uint32_t mBitmapWidth;
70 uint32_t mBitmapHeight;
71 // Also cache texture coords for the quad
72 float mBitmapMinU;
73 float mBitmapMinV;
74 float mBitmapMaxU;
75 float mBitmapMaxV;
76 // Minimize how much we call freetype
77 FT_UInt mGlyphIndex;
78 FT_Vector mAdvance;
79 // Values below contain a glyph's origin in the bitmap
80 FT_Int mBitmapLeft;
81 FT_Int mBitmapTop;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070082 };
83
84 String8 mFontName;
85 uint32_t mFontSize;
86 uint32_t mDpi;
87
88 Font(Context *rsc);
89 bool init(const char *name, uint32_t fontSize, uint32_t dpi);
90
91 FT_Face mFace;
92 bool mInitialized;
93 bool mHasKerning;
94
95 DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -070096 CachedGlyphInfo* getCachedUTFChar(int32_t utfChar);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070097
98 CachedGlyphInfo *cacheGlyph(uint32_t glyph);
99 void updateGlyphCache(CachedGlyphInfo *glyph);
100 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y);
101};
102
103class FontState
104{
105public:
106 FontState();
107 ~FontState();
108
109 void init(Context *rsc);
110 void deinit(Context *rsc);
111
112 ObjectBaseRef<Font> mDefault;
113 ObjectBaseRef<Font> mLast;
114
115 void renderText(const char *text, uint32_t len, uint32_t startIndex, int numGlyphs, int x, int y);
116 void renderText(const char *text, int x, int y);
117 void renderText(Allocation *alloc, int x, int y);
118 void renderText(Allocation *alloc, uint32_t start, int len, int x, int y);
119
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700120 void setFontColor(float r, float g, float b, float a);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700121 void getFontColor(float *r, float *g, float *b, float *a) const;
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700122
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700123protected:
124
125 friend class Font;
126
127 struct CacheTextureLine
128 {
129 uint32_t mMaxHeight;
130 uint32_t mMaxWidth;
131 uint32_t mCurrentRow;
132 uint32_t mCurrentCol;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700133 bool mDirty;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700134
135 CacheTextureLine(uint32_t maxHeight, uint32_t maxWidth, uint32_t currentRow, uint32_t currentCol) :
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700136 mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow), mCurrentCol(currentCol),
137 mDirty(false) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700138 }
139
140 bool fitBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) {
141 if((uint32_t)bitmap->rows > mMaxHeight) {
142 return false;
143 }
144
145 if(mCurrentCol + (uint32_t)bitmap->width < mMaxWidth) {
146 *retOriginX = mCurrentCol;
147 *retOriginY = mCurrentRow;
148 mCurrentCol += bitmap->width;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700149 mDirty = true;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700150 return true;
151 }
152
153 return false;
154 }
155 };
156
157 Vector<CacheTextureLine*> mCacheLines;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700158 uint32_t getRemainingCacheCapacity();
159
160 void precacheLatin(Font *font);
161 String8 mLatinPrecache;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700162
163 Context *mRSC;
164
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700165 float mFontColor[4];
166 bool mFontColorDirty;
167
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700168 // Free type library, we only need one copy
169 FT_Library mLibrary;
Alex Sakhartchouka1ccecd2010-06-30 12:49:27 -0700170 FT_Library getLib();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700171 Vector<Font*> mActiveFonts;
172
173 // Render state for the font
174 ObjectBaseRef<ProgramFragment> mFontShaderF;
175 ObjectBaseRef<Sampler> mFontSampler;
176 ObjectBaseRef<ProgramStore> mFontProgramStore;
177 void initRenderState();
178
179 // Texture to cache glyph bitmaps
180 ObjectBaseRef<Allocation> mTextTexture;
181 void initTextTexture();
182
183 bool cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY);
184 const Type* getCacheTextureType() {
185 return mTextTexture->getType();
186 }
187
188 void flushAllAndInvalidate();
189
190 // Pointer to vertex data to speed up frame to frame work
191 float *mTextMeshPtr;
192 uint32_t mCurrentQuadIndex;
193 uint32_t mMaxNumberOfQuads;
194
195 void initVertexArrayBuffers();
196 ObjectBaseRef<Allocation> mIndexBuffer;
197 ObjectBaseRef<Allocation> mVertexArray;
198
199
200 bool mInitialized;
201
202 void checkInit();
203
204 void issueDrawCommand();
205
206 void appendMeshQuad(float x1, float y1, float z1,
207 float u1, float v1,
208 float x2, float y2, float z2,
209 float u2, float v2,
210 float x3, float y3, float z3,
211 float u3, float v3,
212 float x4, float y4, float z4,
213 float u4, float v4);
214
215};
216
217
218}
219}
220
221#endif