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