rsFont.h revision d3e0ad43dc758c409fc23d1893dab67b18520c24
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 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; 82 // Hold on to the bitmap in case cache is invalidated 83 FT_Bitmap mBitmap; 84 bool mBitmapValid; 85 }; 86 87 String8 mFontName; 88 uint32_t mFontSize; 89 uint32_t mDpi; 90 91 Font(Context *rsc); 92 bool init(const char *name, uint32_t fontSize, uint32_t dpi); 93 94 FT_Face mFace; 95 bool mInitialized; 96 bool mHasKerning; 97 98 DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs; 99 100 CachedGlyphInfo *cacheGlyph(uint32_t glyph); 101 void updateGlyphCache(CachedGlyphInfo *glyph); 102 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); 103}; 104 105class FontState 106{ 107public: 108 FontState(); 109 ~FontState(); 110 111 void init(Context *rsc); 112 void deinit(Context *rsc); 113 114 ObjectBaseRef<Font> mDefault; 115 ObjectBaseRef<Font> mLast; 116 117 void renderText(const char *text, uint32_t len, uint32_t startIndex, int numGlyphs, int x, int y); 118 void renderText(const char *text, int x, int y); 119 void renderText(Allocation *alloc, int x, int y); 120 void renderText(Allocation *alloc, uint32_t start, int len, int x, int y); 121 122protected: 123 124 friend class Font; 125 126 struct CacheTextureLine 127 { 128 uint32_t mMaxHeight; 129 uint32_t mMaxWidth; 130 uint32_t mCurrentRow; 131 uint32_t mCurrentCol; 132 133 CacheTextureLine(uint32_t maxHeight, uint32_t maxWidth, uint32_t currentRow, uint32_t currentCol) : 134 mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow), mCurrentCol(currentCol) { 135 } 136 137 bool fitBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) { 138 if((uint32_t)bitmap->rows > mMaxHeight) { 139 return false; 140 } 141 142 if(mCurrentCol + (uint32_t)bitmap->width < mMaxWidth) { 143 *retOriginX = mCurrentCol; 144 *retOriginY = mCurrentRow; 145 mCurrentCol += bitmap->width; 146 return true; 147 } 148 149 return false; 150 } 151 }; 152 153 Vector<CacheTextureLine*> mCacheLines; 154 155 Context *mRSC; 156 157 // Free type library, we only need one copy 158 FT_Library mLibrary; 159 Vector<Font*> mActiveFonts; 160 161 // Render state for the font 162 ObjectBaseRef<ProgramFragment> mFontShaderF; 163 ObjectBaseRef<Sampler> mFontSampler; 164 ObjectBaseRef<ProgramStore> mFontProgramStore; 165 void initRenderState(); 166 167 // Texture to cache glyph bitmaps 168 ObjectBaseRef<Allocation> mTextTexture; 169 void initTextTexture(); 170 171 bool cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY); 172 const Type* getCacheTextureType() { 173 return mTextTexture->getType(); 174 } 175 176 void flushAllAndInvalidate(); 177 178 // Pointer to vertex data to speed up frame to frame work 179 float *mTextMeshPtr; 180 uint32_t mCurrentQuadIndex; 181 uint32_t mMaxNumberOfQuads; 182 183 void initVertexArrayBuffers(); 184 ObjectBaseRef<Allocation> mIndexBuffer; 185 ObjectBaseRef<Allocation> mVertexArray; 186 187 188 bool mInitialized; 189 190 void checkInit(); 191 192 void issueDrawCommand(); 193 194 void appendMeshQuad(float x1, float y1, float z1, 195 float u1, float v1, 196 float x2, float y2, float z2, 197 float u2, float v2, 198 float x3, float y3, float z3, 199 float u3, float v3, 200 float x4, float y4, float z4, 201 float u4, float v4); 202 203}; 204 205 206} 207} 208 209#endif 210