FontRenderer.h revision 9b9902ddbb01548f4a0199087b7035e7c10b2ae7
1/* 2 * Copyright (C) 2010 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_UI_FONT_RENDERER_H 18#define ANDROID_UI_FONT_RENDERER_H 19 20#include <utils/String8.h> 21#include <utils/Vector.h> 22#include <utils/KeyedVector.h> 23 24#include <SkScalerContext.h> 25#include <SkPaint.h> 26 27#include <GLES2/gl2.h> 28 29#include "Rect.h" 30#include "Properties.h" 31 32namespace android { 33namespace uirenderer { 34 35class FontRenderer; 36 37/** 38 * Represents a font, defined by a Skia font id and a font size. A font is used 39 * to generate glyphs and cache them in the FontState. 40 */ 41class Font { 42public: 43 ~Font(); 44 45 /** 46 * Renders the specified string of text. 47 */ 48 void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len, 49 int numGlyphs, int x, int y); 50 /** 51 * Creates a new font associated with the specified font state. 52 */ 53 static Font* create(FontRenderer* state, uint32_t fontId, float fontSize); 54 55protected: 56 friend class FontRenderer; 57 58 struct CachedGlyphInfo { 59 // Has the cache been invalidated? 60 bool mIsValid; 61 // Location of the cached glyph in the bitmap 62 // in case we need to resize the texture 63 uint32_t mBitmapWidth; 64 uint32_t mBitmapHeight; 65 // Also cache texture coords for the quad 66 float mBitmapMinU; 67 float mBitmapMinV; 68 float mBitmapMaxU; 69 float mBitmapMaxV; 70 // Minimize how much we call freetype 71 uint32_t mGlyphIndex; 72 uint32_t mAdvanceX; 73 uint32_t mAdvanceY; 74 // Values below contain a glyph's origin in the bitmap 75 uint32_t mBitmapLeft; 76 uint32_t mBitmapTop; 77 }; 78 79 Font(FontRenderer* state, uint32_t fontId, float fontSize); 80 81 DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs; 82 83 void invalidateTextureCache(); 84 85 CachedGlyphInfo *cacheGlyph(SkPaint* paint, int32_t glyph); 86 void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph); 87 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); 88 89 FontRenderer* mState; 90 uint32_t mFontId; 91 float mFontSize; 92}; 93 94class FontRenderer { 95public: 96 FontRenderer(); 97 ~FontRenderer(); 98 99 void init(); 100 void deinit(); 101 102 void setFont(uint32_t fontId, float fontSize); 103 void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, 104 uint32_t len, int numGlyphs, int x, int y); 105 106 GLuint getTexture() { 107 checkInit(); 108 return mTextureId; 109 } 110 111protected: 112 friend class Font; 113 114 struct CacheTextureLine { 115 uint16_t mMaxHeight; 116 uint16_t mMaxWidth; 117 uint32_t mCurrentRow; 118 uint32_t mCurrentCol; 119 bool mDirty; 120 121 CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow, 122 uint32_t currentCol): 123 mMaxHeight(maxHeight), 124 mMaxWidth(maxWidth), 125 mCurrentRow(currentRow), 126 mCurrentCol(currentCol), 127 mDirty(false) { 128 } 129 130 bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) { 131 if (glyph.fHeight > mMaxHeight) { 132 return false; 133 } 134 135 if (mCurrentCol + glyph.fWidth < mMaxWidth) { 136 *retOriginX = mCurrentCol; 137 *retOriginY = mCurrentRow; 138 mCurrentCol += glyph.fWidth; 139 mDirty = true; 140 return true; 141 } 142 143 return false; 144 } 145 }; 146 147 uint32_t getCacheWidth() const { 148 return mCacheWidth; 149 } 150 151 uint32_t getCacheHeight() const { 152 return mCacheHeight; 153 } 154 155 void initTextTexture(); 156 bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); 157 158 void flushAllAndInvalidate(); 159 void initVertexArrayBuffers(); 160 161 void checkInit(); 162 163 void issueDrawCommand(); 164 void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2, 165 float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, 166 float x4, float y4, float z4, float u4, float v4); 167 168 uint32_t mCacheWidth; 169 uint32_t mCacheHeight; 170 171 Vector<CacheTextureLine*> mCacheLines; 172 173 Font* mCurrentFont; 174 Vector<Font*> mActiveFonts; 175 176 // Texture to cache glyph bitmaps 177 unsigned char* mTextTexture; 178 GLuint mTextureId; 179 void checkTextureUpdate(); 180 bool mUploadTexture; 181 182 // Pointer to vertex data to speed up frame to frame work 183 float *mTextMeshPtr; 184 uint32_t mCurrentQuadIndex; 185 uint32_t mMaxNumberOfQuads; 186 187 uint32_t mIndexBufferID; 188 189 const Rect* mClip; 190 191 bool mInitialized; 192}; 193 194}; // namespace uirenderer 195}; // namespace android 196 197#endif // ANDROID_UI_FONT_RENDERER_H 198