| Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame^] | 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 | namespace android { | 
|  | 30 | namespace uirenderer { | 
|  | 31 |  | 
|  | 32 | class FontRenderer; | 
|  | 33 |  | 
|  | 34 | class Font { | 
|  | 35 | public: | 
|  | 36 | ~Font(); | 
|  | 37 |  | 
|  | 38 | // Pointer to the utf data, length of data, where to start, number of glyphs ot read | 
|  | 39 | // (each glyph may be longer than a char because we are dealing with utf data) | 
|  | 40 | // Last two variables are the initial pen position | 
|  | 41 | void renderUTF(SkPaint* paint, const char *text, uint32_t len, uint32_t start, | 
|  | 42 | int numGlyphs, int x, int y); | 
|  | 43 |  | 
|  | 44 | static Font* create(FontRenderer* state, uint32_t fontId, float fontSize); | 
|  | 45 |  | 
|  | 46 | protected: | 
|  | 47 |  | 
|  | 48 | friend class FontRenderer; | 
|  | 49 |  | 
|  | 50 | void invalidateTextureCache(); | 
|  | 51 | struct CachedGlyphInfo { | 
|  | 52 | // Has the cache been invalidated? | 
|  | 53 | bool mIsValid; | 
|  | 54 | // Location of the cached glyph in the bitmap | 
|  | 55 | // in case we need to resize the texture | 
|  | 56 | uint32_t mBitmapWidth; | 
|  | 57 | uint32_t mBitmapHeight; | 
|  | 58 | // Also cache texture coords for the quad | 
|  | 59 | float mBitmapMinU; | 
|  | 60 | float mBitmapMinV; | 
|  | 61 | float mBitmapMaxU; | 
|  | 62 | float mBitmapMaxV; | 
|  | 63 | // Minimize how much we call freetype | 
|  | 64 | uint32_t mGlyphIndex; | 
|  | 65 | uint32_t mAdvanceX; | 
|  | 66 | uint32_t mAdvanceY; | 
|  | 67 | // Values below contain a glyph's origin in the bitmap | 
|  | 68 | uint32_t mBitmapLeft; | 
|  | 69 | uint32_t mBitmapTop; | 
|  | 70 | }; | 
|  | 71 |  | 
|  | 72 | FontRenderer* mState; | 
|  | 73 | uint32_t mFontId; | 
|  | 74 | float mFontSize; | 
|  | 75 |  | 
|  | 76 | Font(FontRenderer* state, uint32_t fontId, float fontSize); | 
|  | 77 |  | 
|  | 78 | DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs; | 
|  | 79 |  | 
|  | 80 | CachedGlyphInfo *cacheGlyph(SkPaint* paint, int32_t glyph); | 
|  | 81 | void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph); | 
|  | 82 | void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); | 
|  | 83 | }; | 
|  | 84 |  | 
|  | 85 | class FontRenderer { | 
|  | 86 | public: | 
|  | 87 | FontRenderer(); | 
|  | 88 | ~FontRenderer(); | 
|  | 89 |  | 
|  | 90 | void init(); | 
|  | 91 | void deinit(); | 
|  | 92 |  | 
|  | 93 | void setFont(uint32_t fontId, float fontSize); | 
|  | 94 | void renderText(SkPaint* paint, const char *text, uint32_t len, uint32_t startIndex, | 
|  | 95 | int numGlyphs, int x, int y); | 
|  | 96 | void renderText(SkPaint* paint, const char *text, int x, int y); | 
|  | 97 |  | 
|  | 98 | GLuint getTexture() { | 
|  | 99 | checkInit(); | 
|  | 100 | return mTextureId; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | protected: | 
|  | 104 | friend class Font; | 
|  | 105 |  | 
|  | 106 | struct CacheTextureLine { | 
|  | 107 | uint16_t mMaxHeight; | 
|  | 108 | uint16_t mMaxWidth; | 
|  | 109 | uint32_t mCurrentRow; | 
|  | 110 | uint32_t mCurrentCol; | 
|  | 111 |  | 
|  | 112 | CacheTextureLine(uint16_t maxHeight, uint16_t maxWidth, uint32_t currentRow, | 
|  | 113 | uint32_t currentCol): | 
|  | 114 | mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow), | 
|  | 115 | mCurrentCol(currentCol) { | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) { | 
|  | 119 | if (glyph.fHeight > mMaxHeight) { | 
|  | 120 | return false; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | if (mCurrentCol + glyph.fWidth < mMaxWidth) { | 
|  | 124 | *retOriginX = mCurrentCol; | 
|  | 125 | *retOriginY = mCurrentRow; | 
|  | 126 | mCurrentCol += glyph.fWidth; | 
|  | 127 | return true; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | return false; | 
|  | 131 | } | 
|  | 132 | }; | 
|  | 133 |  | 
|  | 134 | uint32_t getCacheWidth() const { | 
|  | 135 | return mCacheWidth; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | uint32_t getCacheHeight() const { | 
|  | 139 | return mCacheHeight; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | void initTextTexture(); | 
|  | 143 |  | 
|  | 144 | bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); | 
|  | 145 |  | 
|  | 146 | void flushAllAndInvalidate(); | 
|  | 147 | void initVertexArrayBuffers(); | 
|  | 148 |  | 
|  | 149 | void checkInit(); | 
|  | 150 |  | 
|  | 151 | void issueDrawCommand(); | 
|  | 152 |  | 
|  | 153 | void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2, | 
|  | 154 | float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, | 
|  | 155 | float x4, float y4, float z4, float u4, float v4); | 
|  | 156 |  | 
|  | 157 | uint32_t mCacheWidth; | 
|  | 158 | uint32_t mCacheHeight; | 
|  | 159 |  | 
|  | 160 | Font* mCurrentFont; | 
|  | 161 |  | 
|  | 162 | Vector<CacheTextureLine*> mCacheLines; | 
|  | 163 |  | 
|  | 164 | Vector<Font*> mActiveFonts; | 
|  | 165 |  | 
|  | 166 | // Texture to cache glyph bitmaps | 
|  | 167 | unsigned char* mTextTexture; | 
|  | 168 | GLuint mTextureId; | 
|  | 169 | bool mUploadTexture; | 
|  | 170 |  | 
|  | 171 | // Pointer to vertex data to speed up frame to frame work | 
|  | 172 | float *mTextMeshPtr; | 
|  | 173 | uint32_t mCurrentQuadIndex; | 
|  | 174 | uint32_t mMaxNumberOfQuads; | 
|  | 175 |  | 
|  | 176 | uint32_t mIndexBufferID; | 
|  | 177 |  | 
|  | 178 | bool mInitialized; | 
|  | 179 | }; | 
|  | 180 |  | 
|  | 181 | }; // namespace uirenderer | 
|  | 182 | }; // namespace android | 
|  | 183 |  | 
|  | 184 | #endif // ANDROID_UI_FONT_RENDERER_H |