Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2009 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 18 | #include "rsContext.h" |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 19 | |
| 20 | #include "rsFont.h" |
| 21 | #include "rsProgramFragment.h" |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Alex Sakhartchouk | ebd65bb | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 23 | |
| 24 | #include <ft2build.h> |
| 25 | #include FT_FREETYPE_H |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 26 | #include FT_BITMAP_H |
| 27 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 28 | using namespace android; |
| 29 | using namespace android::renderscript; |
| 30 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 31 | Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 32 | mInitialized = false; |
| 33 | mHasKerning = false; |
Alex Sakhartchouk | b6b3489 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 34 | mFace = NULL; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 37 | bool Font::init(const char *name, float fontSize, uint32_t dpi, const void *data, uint32_t dataLen) { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 38 | if (mInitialized) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 39 | LOGE("Reinitialization of fonts not supported"); |
| 40 | return false; |
| 41 | } |
| 42 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 43 | FT_Error error = 0; |
| 44 | if (data != NULL && dataLen > 0) { |
| 45 | error = FT_New_Memory_Face(mRSC->mStateFont.getLib(), (const FT_Byte*)data, dataLen, 0, &mFace); |
| 46 | } else { |
| 47 | error = FT_New_Face(mRSC->mStateFont.getLib(), name, 0, &mFace); |
| 48 | } |
| 49 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 50 | if (error) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 51 | LOGE("Unable to initialize font %s", name); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
| 55 | mFontName = name; |
| 56 | mFontSize = fontSize; |
| 57 | mDpi = dpi; |
| 58 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 59 | error = FT_Set_Char_Size(mFace, (FT_F26Dot6)(fontSize * 64.0f), 0, dpi, 0); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 60 | if (error) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 61 | LOGE("Unable to set font size on %s", name); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
| 65 | mHasKerning = FT_HAS_KERNING(mFace); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 66 | |
| 67 | mInitialized = true; |
| 68 | return true; |
| 69 | } |
| 70 | |
Jason Sams | 38f8d9d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 71 | void Font::preDestroy() const { |
| 72 | for (uint32_t ct = 0; ct < mRSC->mStateFont.mActiveFonts.size(); ct++) { |
| 73 | if (mRSC->mStateFont.mActiveFonts[ct] == this) { |
| 74 | mRSC->mStateFont.mActiveFonts.removeAt(ct); |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 80 | void Font::invalidateTextureCache() { |
| 81 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 82 | mCachedGlyphs.valueAt(i)->mIsValid = false; |
| 83 | } |
| 84 | } |
| 85 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | void Font::drawCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 87 | FontState *state = &mRSC->mStateFont; |
| 88 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 89 | int32_t nPenX = x + glyph->mBitmapLeft; |
| 90 | int32_t nPenY = y - glyph->mBitmapTop + glyph->mBitmapHeight; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 91 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 92 | float u1 = glyph->mBitmapMinU; |
| 93 | float u2 = glyph->mBitmapMaxU; |
| 94 | float v1 = glyph->mBitmapMinV; |
| 95 | float v2 = glyph->mBitmapMaxV; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 96 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 97 | int32_t width = (int32_t) glyph->mBitmapWidth; |
| 98 | int32_t height = (int32_t) glyph->mBitmapHeight; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 99 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 100 | state->appendMeshQuad(nPenX, nPenY, 0, u1, v2, |
| 101 | nPenX + width, nPenY, 0, u2, v2, |
| 102 | nPenX + width, nPenY - height, 0, u2, v1, |
| 103 | nPenX, nPenY - height, 0, u1, v1); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 106 | void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int32_t x, int32_t y, |
| 107 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH) { |
| 108 | int32_t nPenX = x + glyph->mBitmapLeft; |
| 109 | int32_t nPenY = y + glyph->mBitmapTop; |
| 110 | |
| 111 | uint32_t endX = glyph->mBitmapMinX + glyph->mBitmapWidth; |
| 112 | uint32_t endY = glyph->mBitmapMinY + glyph->mBitmapHeight; |
| 113 | |
| 114 | FontState *state = &mRSC->mStateFont; |
| 115 | uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); |
| 116 | const uint8_t* cacheBuffer = state->getTextTextureData(); |
| 117 | |
| 118 | uint32_t cacheX = 0, cacheY = 0; |
| 119 | int32_t bX = 0, bY = 0; |
| 120 | for (cacheX = glyph->mBitmapMinX, bX = nPenX; cacheX < endX; cacheX++, bX++) { |
| 121 | for (cacheY = glyph->mBitmapMinY, bY = nPenY; cacheY < endY; cacheY++, bY++) { |
| 122 | if (bX < 0 || bY < 0 || bX >= (int32_t) bitmapW || bY >= (int32_t) bitmapH) { |
| 123 | LOGE("Skipping invalid index"); |
| 124 | continue; |
| 125 | } |
| 126 | uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX]; |
| 127 | bitmap[bY * bitmapW + bX] = tempCol; |
| 128 | } |
| 129 | } |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y, Rect *bounds) { |
| 133 | int32_t nPenX = x + glyph->mBitmapLeft; |
| 134 | int32_t nPenY = y - glyph->mBitmapTop + glyph->mBitmapHeight; |
| 135 | |
| 136 | int32_t width = (int32_t) glyph->mBitmapWidth; |
| 137 | int32_t height = (int32_t) glyph->mBitmapHeight; |
| 138 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 139 | // 0, 0 is top left, so bottom is a positive number |
| 140 | if (bounds->bottom < nPenY) { |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 141 | bounds->bottom = nPenY; |
| 142 | } |
| 143 | if (bounds->left > nPenX) { |
| 144 | bounds->left = nPenX; |
| 145 | } |
| 146 | if (bounds->right < nPenX + width) { |
| 147 | bounds->right = nPenX + width; |
| 148 | } |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 149 | if (bounds->top > nPenY - height) { |
| 150 | bounds->top = nPenY - height; |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | void Font::renderUTF(const char *text, uint32_t len, int32_t x, int32_t y, |
| 155 | uint32_t start, int32_t numGlyphs, |
| 156 | RenderMode mode, Rect *bounds, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 157 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH) { |
| 158 | if (!mInitialized || numGlyphs == 0 || text == NULL || len == 0) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 162 | if (mode == Font::MEASURE) { |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 163 | if (bounds == NULL) { |
| 164 | LOGE("No return rectangle provided to measure text"); |
| 165 | return; |
| 166 | } |
| 167 | // Reset min and max of the bounding box to something large |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 168 | bounds->set(1e6, -1e6, 1e6, -1e6); |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | int32_t penX = x, penY = y; |
| 172 | int32_t glyphsLeft = 1; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 173 | if (numGlyphs > 0) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 174 | glyphsLeft = numGlyphs; |
| 175 | } |
| 176 | |
| 177 | size_t index = start; |
| 178 | size_t nextIndex = 0; |
| 179 | |
| 180 | while (glyphsLeft > 0) { |
| 181 | |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 182 | int32_t utfChar = utf32_from_utf8_at(text, len, index, &nextIndex); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 183 | |
| 184 | // Reached the end of the string or encountered |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 185 | if (utfChar < 0) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | |
| 189 | // Move to the next character in the array |
| 190 | index = nextIndex; |
| 191 | |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 192 | CachedGlyphInfo *cachedGlyph = getCachedUTFChar(utfChar); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 193 | |
| 194 | // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 195 | if (cachedGlyph->mIsValid) { |
| 196 | switch (mode) { |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 197 | case FRAMEBUFFER: |
| 198 | drawCachedGlyph(cachedGlyph, penX, penY); |
| 199 | break; |
| 200 | case BITMAP: |
| 201 | drawCachedGlyph(cachedGlyph, penX, penY, bitmap, bitmapW, bitmapH); |
| 202 | break; |
| 203 | case MEASURE: |
| 204 | measureCachedGlyph(cachedGlyph, penX, penY, bounds); |
| 205 | break; |
| 206 | } |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Alex Sakhartchouk | ebd65bb | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 209 | penX += (cachedGlyph->mAdvanceX >> 6); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 210 | |
| 211 | // If we were given a specific number of glyphs, decrement |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 212 | if (numGlyphs > 0) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 213 | glyphsLeft --; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 218 | Font::CachedGlyphInfo* Font::getCachedUTFChar(int32_t utfChar) { |
| 219 | |
| 220 | CachedGlyphInfo *cachedGlyph = mCachedGlyphs.valueFor((uint32_t)utfChar); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 221 | if (cachedGlyph == NULL) { |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 222 | cachedGlyph = cacheGlyph((uint32_t)utfChar); |
| 223 | } |
| 224 | // Is the glyph still in texture cache? |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 225 | if (!cachedGlyph->mIsValid) { |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 226 | updateGlyphCache(cachedGlyph); |
| 227 | } |
| 228 | |
| 229 | return cachedGlyph; |
| 230 | } |
| 231 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 232 | void Font::updateGlyphCache(CachedGlyphInfo *glyph) { |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 233 | FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER ); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 234 | if (error) { |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 235 | LOGE("Couldn't load glyph."); |
| 236 | return; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Alex Sakhartchouk | ebd65bb | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 239 | glyph->mAdvanceX = mFace->glyph->advance.x; |
| 240 | glyph->mAdvanceY = mFace->glyph->advance.y; |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 241 | glyph->mBitmapLeft = mFace->glyph->bitmap_left; |
| 242 | glyph->mBitmapTop = mFace->glyph->bitmap_top; |
| 243 | |
| 244 | FT_Bitmap *bitmap = &mFace->glyph->bitmap; |
| 245 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 246 | // Now copy the bitmap into the cache texture |
| 247 | uint32_t startX = 0; |
| 248 | uint32_t startY = 0; |
| 249 | |
| 250 | // Let the font state figure out where to put the bitmap |
| 251 | FontState *state = &mRSC->mStateFont; |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 252 | glyph->mIsValid = state->cacheBitmap(bitmap, &startX, &startY); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 253 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 254 | if (!glyph->mIsValid) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 255 | return; |
| 256 | } |
| 257 | |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 258 | uint32_t endX = startX + bitmap->width; |
| 259 | uint32_t endY = startY + bitmap->rows; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 260 | |
| 261 | glyph->mBitmapMinX = startX; |
| 262 | glyph->mBitmapMinY = startY; |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 263 | glyph->mBitmapWidth = bitmap->width; |
| 264 | glyph->mBitmapHeight = bitmap->rows; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 265 | |
| 266 | uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); |
| 267 | uint32_t cacheHeight = state->getCacheTextureType()->getDimY(); |
| 268 | |
| 269 | glyph->mBitmapMinU = (float)startX / (float)cacheWidth; |
| 270 | glyph->mBitmapMinV = (float)startY / (float)cacheHeight; |
| 271 | glyph->mBitmapMaxU = (float)endX / (float)cacheWidth; |
| 272 | glyph->mBitmapMaxV = (float)endY / (float)cacheHeight; |
| 273 | } |
| 274 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 275 | Font::CachedGlyphInfo *Font::cacheGlyph(uint32_t glyph) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 276 | CachedGlyphInfo *newGlyph = new CachedGlyphInfo(); |
| 277 | mCachedGlyphs.add(glyph, newGlyph); |
| 278 | |
| 279 | newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph); |
| 280 | newGlyph->mIsValid = false; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 281 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 282 | updateGlyphCache(newGlyph); |
| 283 | |
| 284 | return newGlyph; |
| 285 | } |
| 286 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 287 | Font * Font::create(Context *rsc, const char *name, float fontSize, uint32_t dpi, |
| 288 | const void *data, uint32_t dataLen) { |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 289 | rsc->mStateFont.checkInit(); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 290 | Vector<Font*> &activeFonts = rsc->mStateFont.mActiveFonts; |
| 291 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 292 | for (uint32_t i = 0; i < activeFonts.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 293 | Font *ithFont = activeFonts[i]; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 294 | if (ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 295 | return ithFont; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | Font *newFont = new Font(rsc); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 300 | bool isInitialized = newFont->init(name, fontSize, dpi, data, dataLen); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 301 | if (isInitialized) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 302 | activeFonts.push(newFont); |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 303 | rsc->mStateFont.precacheLatin(newFont); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 304 | return newFont; |
| 305 | } |
| 306 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 307 | ObjectBase::checkDelete(newFont); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 308 | return NULL; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 311 | Font::~Font() { |
| 312 | if (mFace) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 313 | FT_Done_Face(mFace); |
| 314 | } |
| 315 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 316 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 317 | CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 318 | delete glyph; |
| 319 | } |
| 320 | } |
| 321 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 322 | FontState::FontState() { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 323 | mInitialized = false; |
| 324 | mMaxNumberOfQuads = 1024; |
| 325 | mCurrentQuadIndex = 0; |
| 326 | mRSC = NULL; |
Alex Sakhartchouk | b6b3489 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 327 | mLibrary = NULL; |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 328 | |
| 329 | // Get the renderer properties |
| 330 | char property[PROPERTY_VALUE_MAX]; |
| 331 | |
| 332 | // Get the gamma |
| 333 | float gamma = DEFAULT_TEXT_GAMMA; |
| 334 | if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) { |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 335 | gamma = atof(property); |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // Get the black gamma threshold |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 339 | int32_t blackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD; |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 340 | if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, NULL) > 0) { |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 341 | blackThreshold = atoi(property); |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 342 | } |
| 343 | mBlackThreshold = (float)(blackThreshold) / 255.0f; |
| 344 | |
| 345 | // Get the white gamma threshold |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 346 | int32_t whiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD; |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 347 | if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, NULL) > 0) { |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 348 | whiteThreshold = atoi(property); |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 349 | } |
| 350 | mWhiteThreshold = (float)(whiteThreshold) / 255.0f; |
| 351 | |
| 352 | // Compute the gamma tables |
| 353 | mBlackGamma = gamma; |
| 354 | mWhiteGamma = 1.0f / gamma; |
Alex Sakhartchouk | 960ae15 | 2010-10-12 14:15:17 -0700 | [diff] [blame] | 355 | |
| 356 | setFontColor(0.1f, 0.1f, 0.1f, 1.0f); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 359 | FontState::~FontState() { |
| 360 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 361 | delete mCacheLines[i]; |
| 362 | } |
| 363 | |
| 364 | rsAssert(!mActiveFonts.size()); |
| 365 | } |
| 366 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 367 | FT_Library FontState::getLib() { |
| 368 | if (!mLibrary) { |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 369 | FT_Error error = FT_Init_FreeType(&mLibrary); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 370 | if (error) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 371 | LOGE("Unable to initialize freetype"); |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 372 | return NULL; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
Alex Sakhartchouk | b6b3489 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 375 | |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 376 | return mLibrary; |
| 377 | } |
| 378 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 379 | void FontState::init(Context *rsc) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 380 | mRSC = rsc; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 383 | void FontState::flushAllAndInvalidate() { |
| 384 | if (mCurrentQuadIndex != 0) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 385 | issueDrawCommand(); |
| 386 | mCurrentQuadIndex = 0; |
| 387 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 388 | for (uint32_t i = 0; i < mActiveFonts.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 389 | mActiveFonts[i]->invalidateTextureCache(); |
| 390 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 391 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 392 | mCacheLines[i]->mCurrentCol = 0; |
| 393 | } |
| 394 | } |
| 395 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 396 | bool FontState::cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 397 | // If the glyph is too tall, don't cache it |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 398 | if ((uint32_t)bitmap->rows > mCacheLines[mCacheLines.size()-1]->mMaxHeight) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 399 | LOGE("Font size to large to fit in cache. width, height = %i, %i", (int)bitmap->width, (int)bitmap->rows); |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | // Now copy the bitmap into the cache texture |
| 404 | uint32_t startX = 0; |
| 405 | uint32_t startY = 0; |
| 406 | |
| 407 | bool bitmapFit = false; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 408 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 409 | bitmapFit = mCacheLines[i]->fitBitmap(bitmap, &startX, &startY); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 410 | if (bitmapFit) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // If the new glyph didn't fit, flush the state so far and invalidate everything |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 416 | if (!bitmapFit) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 417 | flushAllAndInvalidate(); |
| 418 | |
| 419 | // Try to fit it again |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 420 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 421 | bitmapFit = mCacheLines[i]->fitBitmap(bitmap, &startX, &startY); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 422 | if (bitmapFit) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 423 | break; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // if we still don't fit, something is wrong and we shouldn't draw |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 428 | if (!bitmapFit) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 429 | LOGE("Bitmap doesn't fit in cache. width, height = %i, %i", (int)bitmap->width, (int)bitmap->rows); |
| 430 | return false; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | *retOriginX = startX; |
| 435 | *retOriginY = startY; |
| 436 | |
| 437 | uint32_t endX = startX + bitmap->width; |
| 438 | uint32_t endY = startY + bitmap->rows; |
| 439 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 440 | uint32_t cacheWidth = getCacheTextureType()->getDimX(); |
| 441 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 442 | uint8_t *cacheBuffer = (uint8_t*)mTextTexture->getPtr(); |
| 443 | uint8_t *bitmapBuffer = bitmap->buffer; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 444 | |
| 445 | uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 446 | for (cacheX = startX, bX = 0; cacheX < endX; cacheX ++, bX ++) { |
| 447 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY ++, bY ++) { |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 448 | uint8_t tempCol = bitmapBuffer[bY * bitmap->width + bX]; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 449 | cacheBuffer[cacheY*cacheWidth + cacheX] = tempCol; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // This will dirty the texture and the shader so next time |
| 454 | // we draw it will upload the data |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 455 | mTextTexture->deferredUploadToTexture(mRSC); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 456 | mFontShaderF->bindTexture(mRSC, 0, mTextTexture.get()); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 457 | |
| 458 | // Some debug code |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 459 | /*for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 460 | LOGE("Cache Line: H: %u Empty Space: %f", |
| 461 | mCacheLines[i]->mMaxHeight, |
| 462 | (1.0f - (float)mCacheLines[i]->mCurrentCol/(float)mCacheLines[i]->mMaxWidth)*100.0f); |
| 463 | |
| 464 | }*/ |
| 465 | |
| 466 | return true; |
| 467 | } |
| 468 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 469 | void FontState::initRenderState() { |
Alex Sakhartchouk | d209163 | 2010-10-06 11:15:01 -0700 | [diff] [blame] | 470 | String8 shaderString("varying vec2 varTex0;\n"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 471 | shaderString.append("void main() {\n"); |
| 472 | shaderString.append(" lowp vec4 col = UNI_Color;\n"); |
| 473 | shaderString.append(" col.a = texture2D(UNI_Tex0, varTex0.xy).a;\n"); |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 474 | shaderString.append(" col.a = pow(col.a, UNI_Gamma);\n"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 475 | shaderString.append(" gl_FragColor = col;\n"); |
| 476 | shaderString.append("}\n"); |
| 477 | |
| 478 | const Element *colorElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4); |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 479 | const Element *gammaElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 1); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 480 | mRSC->mStateElement.elementBuilderBegin(); |
| 481 | mRSC->mStateElement.elementBuilderAdd(colorElem, "Color", 1); |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 482 | mRSC->mStateElement.elementBuilderAdd(gammaElem, "Gamma", 1); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 483 | const Element *constInput = mRSC->mStateElement.elementBuilderCreate(mRSC); |
| 484 | |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 485 | Type *inputType = Type::getType(mRSC, constInput, 1, 0, 0, false, false); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 486 | |
| 487 | uint32_t tmp[4]; |
| 488 | tmp[0] = RS_PROGRAM_PARAM_CONSTANT; |
| 489 | tmp[1] = (uint32_t)inputType; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 490 | tmp[2] = RS_PROGRAM_PARAM_TEXTURE_TYPE; |
| 491 | tmp[3] = RS_TEXTURE_2D; |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 492 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 493 | mFontShaderFConstant.set(new Allocation(mRSC, inputType, |
| 494 | RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS)); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 495 | ProgramFragment *pf = new ProgramFragment(mRSC, shaderString.string(), |
| 496 | shaderString.length(), tmp, 4); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 497 | mFontShaderF.set(pf); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 498 | mFontShaderF->bindAllocation(mRSC, mFontShaderFConstant.get(), 0); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 499 | |
| 500 | Sampler *sampler = new Sampler(mRSC, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST, |
| 501 | RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP); |
| 502 | mFontSampler.set(sampler); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 503 | mFontShaderF->bindSampler(mRSC, 0, sampler); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 504 | |
Jason Sams | 331bf9b | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 505 | ProgramStore *fontStore = new ProgramStore(mRSC, true, true, true, true, |
| 506 | false, false, |
Jason Sams | bd184c5 | 2011-04-06 11:44:47 -0700 | [diff] [blame] | 507 | RS_BLEND_SRC_SRC_ALPHA, |
| 508 | RS_BLEND_DST_ONE_MINUS_SRC_ALPHA, |
Jason Sams | 331bf9b | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 509 | RS_DEPTH_FUNC_ALWAYS); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 510 | mFontProgramStore.set(fontStore); |
Jason Sams | 48f5056 | 2011-03-18 15:03:25 -0700 | [diff] [blame] | 511 | mFontProgramStore->init(); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 514 | void FontState::initTextTexture() { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 515 | const Element *alphaElem = Element::create(mRSC, RS_TYPE_UNSIGNED_8, RS_KIND_PIXEL_A, true, 1); |
| 516 | |
| 517 | // We will allocate a texture to initially hold 32 character bitmaps |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 518 | Type *texType = Type::getType(mRSC, alphaElem, 1024, 256, 0, false, false); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 519 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 520 | Allocation *cacheAlloc = new Allocation(mRSC, texType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 521 | mTextTexture.set(cacheAlloc); |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 522 | mTextTexture->syncAll(mRSC, RS_ALLOCATION_USAGE_SCRIPT); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 523 | |
| 524 | // Split up our cache texture into lines of certain widths |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 525 | int32_t nextLine = 0; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 526 | mCacheLines.push(new CacheTextureLine(16, texType->getDimX(), nextLine, 0)); |
| 527 | nextLine += mCacheLines.top()->mMaxHeight; |
| 528 | mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0)); |
| 529 | nextLine += mCacheLines.top()->mMaxHeight; |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 530 | mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0)); |
| 531 | nextLine += mCacheLines.top()->mMaxHeight; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 532 | mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0)); |
| 533 | nextLine += mCacheLines.top()->mMaxHeight; |
| 534 | mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0)); |
| 535 | nextLine += mCacheLines.top()->mMaxHeight; |
| 536 | mCacheLines.push(new CacheTextureLine(40, texType->getDimX(), nextLine, 0)); |
| 537 | nextLine += mCacheLines.top()->mMaxHeight; |
| 538 | mCacheLines.push(new CacheTextureLine(texType->getDimY() - nextLine, texType->getDimX(), nextLine, 0)); |
| 539 | } |
| 540 | |
| 541 | // Avoid having to reallocate memory and render quad by quad |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 542 | void FontState::initVertexArrayBuffers() { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 543 | // Now lets write index data |
| 544 | const Element *indexElem = Element::create(mRSC, RS_TYPE_UNSIGNED_16, RS_KIND_USER, false, 1); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 545 | uint32_t numIndicies = mMaxNumberOfQuads * 6; |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 546 | Type *indexType = Type::getType(mRSC, indexElem, numIndicies, 0, 0, false, false); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 547 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 548 | Allocation *indexAlloc = new Allocation(mRSC, indexType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_VERTEX); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 549 | uint16_t *indexPtr = (uint16_t*)indexAlloc->getPtr(); |
| 550 | |
| 551 | // Four verts, two triangles , six indices per quad |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 552 | for (uint32_t i = 0; i < mMaxNumberOfQuads; i ++) { |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 553 | int32_t i6 = i * 6; |
| 554 | int32_t i4 = i * 4; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 555 | |
| 556 | indexPtr[i6 + 0] = i4 + 0; |
| 557 | indexPtr[i6 + 1] = i4 + 1; |
| 558 | indexPtr[i6 + 2] = i4 + 2; |
| 559 | |
| 560 | indexPtr[i6 + 3] = i4 + 0; |
| 561 | indexPtr[i6 + 4] = i4 + 2; |
| 562 | indexPtr[i6 + 5] = i4 + 3; |
| 563 | } |
| 564 | |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 565 | indexAlloc->deferredUploadToBufferObject(mRSC); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 566 | |
| 567 | const Element *posElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 3); |
| 568 | const Element *texElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 2); |
| 569 | |
Alex Sakhartchouk | 98bfe5d | 2010-10-18 17:18:50 -0700 | [diff] [blame] | 570 | mRSC->mStateElement.elementBuilderBegin(); |
| 571 | mRSC->mStateElement.elementBuilderAdd(posElem, "position", 1); |
| 572 | mRSC->mStateElement.elementBuilderAdd(texElem, "texture0", 1); |
| 573 | const Element *vertexDataElem = mRSC->mStateElement.elementBuilderCreate(mRSC); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 574 | |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 575 | Type *vertexDataType = Type::getType(mRSC, vertexDataElem, |
| 576 | mMaxNumberOfQuads * 4, |
| 577 | 0, 0, false, false); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 578 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 579 | Allocation *vertexAlloc = new Allocation(mRSC, vertexDataType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_VERTEX); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 580 | mTextMeshPtr = (float*)vertexAlloc->getPtr(); |
| 581 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 582 | mMesh.set(new Mesh(mRSC, 1, 1)); |
| 583 | mMesh->setVertexBuffer(vertexAlloc, 0); |
| 584 | mMesh->setPrimitive(indexAlloc, RS_PRIMITIVE_TRIANGLE, 0); |
| 585 | mMesh->init(); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // We don't want to allocate anything unless we actually draw text |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 589 | void FontState::checkInit() { |
| 590 | if (mInitialized) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 591 | return; |
| 592 | } |
| 593 | |
| 594 | initTextTexture(); |
| 595 | initRenderState(); |
| 596 | |
| 597 | initVertexArrayBuffers(); |
| 598 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 599 | // We store a string with letters in a rough frequency of occurrence |
| 600 | mLatinPrecache = String8(" eisarntolcdugpmhbyfvkwzxjq"); |
| 601 | mLatinPrecache += String8("EISARNTOLCDUGPMHBYFVKWZXJQ"); |
| 602 | mLatinPrecache += String8(",.?!()-+@;:`'"); |
| 603 | mLatinPrecache += String8("0123456789"); |
| 604 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 605 | mInitialized = true; |
| 606 | } |
| 607 | |
| 608 | void FontState::issueDrawCommand() { |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 609 | Context::PushState ps(mRSC); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 610 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 611 | mRSC->setProgramVertex(mRSC->getDefaultProgramVertex()); |
| 612 | mRSC->setProgramRaster(mRSC->getDefaultProgramRaster()); |
| 613 | mRSC->setProgramFragment(mFontShaderF.get()); |
| 614 | mRSC->setProgramStore(mFontProgramStore.get()); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 615 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 616 | if (mConstantsDirty) { |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 617 | mFontShaderFConstant->data(mRSC, 0, 0, 1, &mConstants, sizeof(mConstants)); |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 618 | mConstantsDirty = false; |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 621 | if (!mRSC->setupCheck()) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 622 | return; |
| 623 | } |
| 624 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 625 | mMesh->renderPrimitiveRange(mRSC, 0, 0, mCurrentQuadIndex * 6); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | void FontState::appendMeshQuad(float x1, float y1, float z1, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 629 | float u1, float v1, |
| 630 | float x2, float y2, float z2, |
| 631 | float u2, float v2, |
| 632 | float x3, float y3, float z3, |
| 633 | float u3, float v3, |
| 634 | float x4, float y4, float z4, |
| 635 | float u4, float v4) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 636 | const uint32_t vertsPerQuad = 4; |
| 637 | const uint32_t floatsPerVert = 5; |
| 638 | float *currentPos = mTextMeshPtr + mCurrentQuadIndex * vertsPerQuad * floatsPerVert; |
| 639 | |
| 640 | // Cull things that are off the screen |
| 641 | float width = (float)mRSC->getWidth(); |
| 642 | float height = (float)mRSC->getHeight(); |
| 643 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 644 | if (x1 > width || y1 < 0.0f || x2 < 0 || y4 > height) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 645 | return; |
| 646 | } |
| 647 | |
| 648 | /*LOGE("V0 x: %f y: %f z: %f", x1, y1, z1); |
| 649 | LOGE("V1 x: %f y: %f z: %f", x2, y2, z2); |
| 650 | LOGE("V2 x: %f y: %f z: %f", x3, y3, z3); |
| 651 | LOGE("V3 x: %f y: %f z: %f", x4, y4, z4);*/ |
| 652 | |
| 653 | (*currentPos++) = x1; |
| 654 | (*currentPos++) = y1; |
| 655 | (*currentPos++) = z1; |
| 656 | (*currentPos++) = u1; |
| 657 | (*currentPos++) = v1; |
| 658 | |
| 659 | (*currentPos++) = x2; |
| 660 | (*currentPos++) = y2; |
| 661 | (*currentPos++) = z2; |
| 662 | (*currentPos++) = u2; |
| 663 | (*currentPos++) = v2; |
| 664 | |
| 665 | (*currentPos++) = x3; |
| 666 | (*currentPos++) = y3; |
| 667 | (*currentPos++) = z3; |
| 668 | (*currentPos++) = u3; |
| 669 | (*currentPos++) = v3; |
| 670 | |
| 671 | (*currentPos++) = x4; |
| 672 | (*currentPos++) = y4; |
| 673 | (*currentPos++) = z4; |
| 674 | (*currentPos++) = u4; |
| 675 | (*currentPos++) = v4; |
| 676 | |
| 677 | mCurrentQuadIndex ++; |
| 678 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 679 | if (mCurrentQuadIndex == mMaxNumberOfQuads) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 680 | issueDrawCommand(); |
| 681 | mCurrentQuadIndex = 0; |
| 682 | } |
| 683 | } |
| 684 | |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 685 | uint32_t FontState::getRemainingCacheCapacity() { |
| 686 | uint32_t remainingCapacity = 0; |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 687 | uint32_t totalPixels = 0; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 688 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 689 | remainingCapacity += (mCacheLines[i]->mMaxWidth - mCacheLines[i]->mCurrentCol); |
| 690 | totalPixels += mCacheLines[i]->mMaxWidth; |
| 691 | } |
| 692 | remainingCapacity = (remainingCapacity * 100) / totalPixels; |
| 693 | return remainingCapacity; |
| 694 | } |
| 695 | |
| 696 | void FontState::precacheLatin(Font *font) { |
| 697 | // Remaining capacity is measured in % |
| 698 | uint32_t remainingCapacity = getRemainingCacheCapacity(); |
| 699 | uint32_t precacheIdx = 0; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 700 | while (remainingCapacity > 25 && precacheIdx < mLatinPrecache.size()) { |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 701 | font->getCachedUTFChar((int32_t)mLatinPrecache[precacheIdx]); |
| 702 | remainingCapacity = getRemainingCacheCapacity(); |
| 703 | precacheIdx ++; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 708 | void FontState::renderText(const char *text, uint32_t len, int32_t x, int32_t y, |
| 709 | uint32_t startIndex, int32_t numGlyphs, |
| 710 | Font::RenderMode mode, |
| 711 | Font::Rect *bounds, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 712 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 713 | checkInit(); |
| 714 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 715 | // Render code here |
| 716 | Font *currentFont = mRSC->getFont(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 717 | if (!currentFont) { |
| 718 | if (!mDefault.get()) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 719 | String8 fontsDir("/fonts/DroidSans.ttf"); |
| 720 | String8 fullPath(getenv("ANDROID_ROOT")); |
| 721 | fullPath += fontsDir; |
| 722 | |
Alex Sakhartchouk | 2c74ad9 | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 723 | mDefault.set(Font::create(mRSC, fullPath.string(), 8, mRSC->getDPI())); |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 724 | } |
| 725 | currentFont = mDefault.get(); |
| 726 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 727 | if (!currentFont) { |
Alex Sakhartchouk | b6b3489 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 728 | LOGE("Unable to initialize any fonts"); |
| 729 | return; |
| 730 | } |
| 731 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 732 | currentFont->renderUTF(text, len, x, y, startIndex, numGlyphs, |
| 733 | mode, bounds, bitmap, bitmapW, bitmapH); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 734 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 735 | if (mCurrentQuadIndex != 0) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 736 | issueDrawCommand(); |
| 737 | mCurrentQuadIndex = 0; |
| 738 | } |
| 739 | } |
| 740 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 741 | void FontState::measureText(const char *text, uint32_t len, Font::Rect *bounds) { |
| 742 | renderText(text, len, 0, 0, 0, -1, Font::MEASURE, bounds); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 743 | bounds->bottom = - bounds->bottom; |
| 744 | bounds->top = - bounds->top; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Alex Sakhartchouk | fb10c16 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 747 | void FontState::setFontColor(float r, float g, float b, float a) { |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 748 | mConstants.mFontColor[0] = r; |
| 749 | mConstants.mFontColor[1] = g; |
| 750 | mConstants.mFontColor[2] = b; |
| 751 | mConstants.mFontColor[3] = a; |
| 752 | |
| 753 | mConstants.mGamma = 1.0f; |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 754 | const float luminance = (r * 2.0f + g * 5.0f + b) / 8.0f; |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 755 | if (luminance <= mBlackThreshold) { |
| 756 | mConstants.mGamma = mBlackGamma; |
| 757 | } else if (luminance >= mWhiteThreshold) { |
| 758 | mConstants.mGamma = mWhiteGamma; |
| 759 | } |
Alex Sakhartchouk | 960ae15 | 2010-10-12 14:15:17 -0700 | [diff] [blame] | 760 | |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 761 | mConstantsDirty = true; |
Alex Sakhartchouk | fb10c16 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 764 | void FontState::getFontColor(float *r, float *g, float *b, float *a) const { |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 765 | *r = mConstants.mFontColor[0]; |
| 766 | *g = mConstants.mFontColor[1]; |
| 767 | *b = mConstants.mFontColor[2]; |
| 768 | *a = mConstants.mFontColor[3]; |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 771 | void FontState::deinit(Context *rsc) { |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 772 | mInitialized = false; |
| 773 | |
Stephen Hines | 01f0ad7 | 2010-09-28 15:45:45 -0700 | [diff] [blame] | 774 | mFontShaderFConstant.clear(); |
| 775 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 776 | mMesh.clear(); |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 777 | |
| 778 | mFontShaderF.clear(); |
| 779 | mFontSampler.clear(); |
| 780 | mFontProgramStore.clear(); |
| 781 | |
| 782 | mTextTexture.clear(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 783 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 784 | delete mCacheLines[i]; |
| 785 | } |
| 786 | mCacheLines.clear(); |
| 787 | |
| 788 | mDefault.clear(); |
| 789 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 790 | if (mLibrary) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 791 | FT_Done_FreeType( mLibrary ); |
Alex Sakhartchouk | b6b3489 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 792 | mLibrary = NULL; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 793 | } |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Alex Sakhartchouk | ebd65bb | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 796 | bool FontState::CacheTextureLine::fitBitmap(FT_Bitmap_ *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) { |
| 797 | if ((uint32_t)bitmap->rows > mMaxHeight) { |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | if (mCurrentCol + (uint32_t)bitmap->width < mMaxWidth) { |
| 802 | *retOriginX = mCurrentCol; |
| 803 | *retOriginY = mCurrentRow; |
| 804 | mCurrentCol += bitmap->width; |
| 805 | mDirty = true; |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | return false; |
| 810 | } |
| 811 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 812 | namespace android { |
| 813 | namespace renderscript { |
| 814 | |
Alex Sakhartchouk | e7c4a75 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 815 | RsFont rsi_FontCreateFromFile(Context *rsc, |
| 816 | char const *name, size_t name_length, |
| 817 | float fontSize, uint32_t dpi) { |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 818 | Font *newFont = Font::create(rsc, name, fontSize, dpi); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 819 | if (newFont) { |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 820 | newFont->incUserRef(); |
| 821 | } |
| 822 | return newFont; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Alex Sakhartchouk | e7c4a75 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 825 | RsFont rsi_FontCreateFromMemory(Context *rsc, |
| 826 | char const *name, size_t name_length, |
| 827 | float fontSize, uint32_t dpi, |
| 828 | const void *data, size_t data_length) { |
| 829 | Font *newFont = Font::create(rsc, name, fontSize, dpi, data, data_length); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 830 | if (newFont) { |
| 831 | newFont->incUserRef(); |
| 832 | } |
| 833 | return newFont; |
| 834 | } |
| 835 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 836 | } // renderscript |
| 837 | } // android |