bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 to 2020 TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 19 | #include <stdbool.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <stdio.h> |
| 25 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 26 | #include <pthread.h> |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 27 | #include <algorithm> |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 28 | #include <string> |
| 29 | #include "truetype.hpp" |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 30 | |
| 31 | static FontData font_data = { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 32 | .ft_library = NULL, |
| 33 | .mutex = PTHREAD_MUTEX_INITIALIZER |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 36 | twrpTruetype::twrpTruetype(void) { |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 37 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 38 | } |
| 39 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 40 | int twrpTruetype::utf8_to_unicode(const char* pIn, unsigned int *pOut) { |
| 41 | int utf_bytes = 1; |
| 42 | unsigned int unicode = 0; |
| 43 | unsigned char tmp; |
| 44 | tmp = (unsigned char)*pIn++; |
| 45 | if (tmp < 0x80) |
| 46 | { |
| 47 | *pOut = tmp; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | unsigned int high_bit_mask = 0x3F; |
| 52 | unsigned int high_bit_shift = 0; |
| 53 | int total_bits = 0; |
| 54 | while((tmp & 0xC0) == 0xC0) |
| 55 | { |
| 56 | utf_bytes ++; |
| 57 | if(utf_bytes > 6) |
| 58 | { |
| 59 | *pOut = tmp; |
| 60 | return 1; |
| 61 | } |
| 62 | tmp = 0xFF & (tmp << 1); |
| 63 | total_bits += 6; |
| 64 | high_bit_mask >>= 1; |
| 65 | high_bit_shift++; |
| 66 | unicode <<= 6; |
| 67 | unicode |= (*pIn++) & 0x3F; |
| 68 | } |
| 69 | unicode |= ((tmp >> high_bit_shift) & high_bit_mask) << total_bits; |
| 70 | *pOut = unicode; |
| 71 | } |
| 72 | |
| 73 | return utf_bytes; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 74 | } |
| 75 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 76 | void* twrpTruetype::gr_ttf_loadFont(const char *filename, int size, int dpi) { |
| 77 | int error; |
| 78 | TrueTypeFont* res = nullptr; |
| 79 | TrueTypeFontKey* key; |
soyu | d5a7c0e | 2014-11-28 14:33:53 +0800 | [diff] [blame] | 80 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 81 | pthread_mutex_lock(&font_data.mutex); |
soyu | d5a7c0e | 2014-11-28 14:33:53 +0800 | [diff] [blame] | 82 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 83 | TrueTypeFontKey k = { |
| 84 | .size = size, |
| 85 | .dpi = dpi, |
| 86 | .path = (char*)filename |
| 87 | }; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 88 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 89 | TrueTypeFontMap::iterator ttfIter = font_data.fonts.find(k); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 90 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 91 | if (ttfIter != font_data.fonts.end()) |
| 92 | { |
| 93 | res = ttfIter->second; |
| 94 | ++res->refcount; |
| 95 | goto exit; |
| 96 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 97 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 98 | if(!font_data.ft_library) |
| 99 | { |
| 100 | error = FT_Init_FreeType(&font_data.ft_library); |
| 101 | if(error) |
| 102 | { |
| 103 | fprintf(stderr, "Failed to init libfreetype! %d\n", error); |
| 104 | goto exit; |
| 105 | } |
| 106 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 107 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 108 | FT_Face face; |
| 109 | error = FT_New_Face(font_data.ft_library, filename, 0, &face); |
| 110 | if(error) |
| 111 | { |
| 112 | fprintf(stderr, "Failed to load truetype face %s: %d\n", filename, error); |
| 113 | goto exit; |
| 114 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 115 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 116 | error = FT_Set_Char_Size(face, 0, size*16, dpi, dpi); |
| 117 | if(error) |
| 118 | { |
| 119 | fprintf(stderr, "Failed to set truetype face size to %d, dpi %d: %d\n", size, dpi, error); |
| 120 | FT_Done_Face(face); |
| 121 | goto exit; |
| 122 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 123 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 124 | res = new TrueTypeFont; |
| 125 | res->type = FONT_TYPE_TTF; |
| 126 | res->size = size; |
| 127 | res->dpi = dpi; |
| 128 | res->face = face; |
| 129 | res->max_height = -1; |
| 130 | res->base = -1; |
| 131 | res->refcount = 1; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 132 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 133 | pthread_mutex_init(&res->mutex, 0); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 134 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 135 | key = new TrueTypeFontKey; |
| 136 | key->path = strdup(filename); |
| 137 | key->size = size; |
| 138 | key->dpi = dpi; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 139 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 140 | res->key = key; |
| 141 | font_data.fonts[*key] = res; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 142 | |
| 143 | exit: |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 144 | pthread_mutex_unlock(&font_data.mutex); |
| 145 | return res; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 146 | } |
| 147 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 148 | void* twrpTruetype::gr_ttf_scaleFont(void *font, int max_width, int measured_width) { |
| 149 | if (!font) |
| 150 | return nullptr; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 151 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 152 | TrueTypeFont *f = (TrueTypeFont *)font; |
| 153 | float scale_value = (float)(max_width) / (float)(measured_width); |
| 154 | int new_size = ((int)((float)f->size * scale_value)) - 1; |
| 155 | if (new_size < 1) |
| 156 | new_size = 1; |
| 157 | const char* file = f->key->path; |
| 158 | int dpi = f->dpi; |
| 159 | return gr_ttf_loadFont(file, new_size, dpi); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 160 | } |
| 161 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 162 | static bool gr_ttf_freeFontCache(void *value, void *context __unused) |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 163 | { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 164 | TrueTypeCacheEntry *e = (TrueTypeCacheEntry *)value; |
| 165 | FT_Done_Glyph((FT_Glyph)e->glyph); |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 166 | delete e; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 167 | return true; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 168 | } |
| 169 | |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 170 | void twrpTruetype::gr_ttf_freeStringCache(void *key, void *value, void *context __unused) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 171 | StringCacheKey *k = (StringCacheKey *)key; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 172 | delete k; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 173 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 174 | StringCacheEntry *e = (StringCacheEntry *)value; |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 175 | free(e->surface.data); |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 176 | delete e; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 177 | } |
| 178 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 179 | void twrpTruetype::gr_ttf_freeFont(void *font) { |
| 180 | pthread_mutex_lock(&font_data.mutex); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 181 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 182 | TrueTypeFont *d = (TrueTypeFont *)font; |
| 183 | if(--d->refcount == 0) |
| 184 | { |
| 185 | delete d->key->path; |
| 186 | delete d->key; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 187 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 188 | FT_Done_Face(d->face); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 189 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 190 | StringCacheMap::iterator stringCacheEntryIt = d->string_cache.begin(); |
| 191 | while (stringCacheEntryIt != d->string_cache.end()) { |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 192 | gr_ttf_freeStringCache(stringCacheEntryIt->second->key, stringCacheEntryIt->second, nullptr); |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 193 | stringCacheEntryIt = d->string_cache.erase(stringCacheEntryIt); |
| 194 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 195 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 196 | TrueTypeCacheEntryMap::iterator ttcIt = d->glyph_cache.begin(); |
| 197 | while(ttcIt != d->glyph_cache.end()) { |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 198 | gr_ttf_freeFontCache(ttcIt->second, nullptr); |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 199 | ttcIt = d->glyph_cache.erase(ttcIt); |
| 200 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 201 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 202 | pthread_mutex_destroy(&d->mutex); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 203 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 204 | TrueTypeFontMap::iterator trueTypeFontIt = font_data.fonts.find(*(d->key)); |
| 205 | delete d; |
| 206 | font_data.fonts.erase(trueTypeFontIt); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | pthread_mutex_unlock(&font_data.mutex); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 211 | } |
| 212 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 213 | TrueTypeCacheEntry* twrpTruetype::gr_ttf_glyph_cache_peek(TrueTypeFont *font, int char_index) { |
| 214 | TrueTypeCacheEntryMap::iterator glyphCacheItr = font->glyph_cache.find(char_index); |
| 215 | |
| 216 | if(glyphCacheItr != font->glyph_cache.end()) { |
| 217 | return font->glyph_cache[char_index]; |
| 218 | } |
| 219 | return nullptr; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 220 | } |
| 221 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 222 | TrueTypeCacheEntry* twrpTruetype::gr_ttf_glyph_cache_get(TrueTypeFont *font, int char_index) { |
| 223 | TrueTypeCacheEntryMap::iterator glyphCacheItr = font->glyph_cache.find(char_index); |
| 224 | TrueTypeCacheEntry* res = nullptr; |
| 225 | if(glyphCacheItr == font->glyph_cache.end()) |
| 226 | { |
| 227 | int error = FT_Load_Glyph(font->face, char_index, FT_LOAD_RENDER); |
| 228 | if(error) |
| 229 | { |
| 230 | fprintf(stderr, "Failed to load glyph idx %d: %d\n", char_index, error); |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 231 | return nullptr; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 232 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 233 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 234 | FT_BitmapGlyph glyph; |
| 235 | error = FT_Get_Glyph(font->face->glyph, (FT_Glyph*)&glyph); |
| 236 | if(error) |
| 237 | { |
| 238 | fprintf(stderr, "Failed to copy glyph %d: %d\n", char_index, error); |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 239 | return nullptr; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 240 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 241 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 242 | res = new TrueTypeCacheEntry; |
| 243 | res->glyph = glyph; |
| 244 | FT_Glyph_Get_CBox((FT_Glyph)glyph, FT_GLYPH_BBOX_PIXELS, &res->bbox); |
| 245 | font->glyph_cache[char_index] = res; |
| 246 | } |
| 247 | else { |
| 248 | res = glyphCacheItr->second; |
| 249 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 250 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 251 | return res; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 252 | } |
| 253 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 254 | int twrpTruetype::gr_ttf_copy_glyph_to_surface(GGLSurface *dest, FT_BitmapGlyph glyph, int offX, int offY, int base) { |
| 255 | unsigned y; |
| 256 | uint8_t *src_itr = glyph->bitmap.buffer; |
| 257 | uint8_t *dest_itr = dest->data; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 258 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 259 | if(glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY) |
| 260 | { |
| 261 | fprintf(stderr, "Unsupported pixel mode in FT_BitmapGlyph %d\n", glyph->bitmap.pixel_mode); |
| 262 | return -1; |
| 263 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 264 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 265 | dest_itr += (offY + base - glyph->top)*dest->stride + (offX + glyph->left); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 266 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 267 | // FIXME: if glyph->left is negative and everything else is 0 (e.g. letter 'j' in Roboto-Regular), |
| 268 | // the result might end up being before the buffer - I'm not sure how to properly handle this. |
| 269 | if(dest_itr < dest->data) |
| 270 | dest_itr = dest->data; |
Vojtech Bocek | 54cf108 | 2015-03-15 16:50:26 +0100 | [diff] [blame] | 271 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 272 | for(y = 0; y < glyph->bitmap.rows; ++y) |
| 273 | { |
| 274 | memcpy(dest_itr, src_itr, glyph->bitmap.width); |
| 275 | src_itr += glyph->bitmap.pitch; |
| 276 | dest_itr += dest->stride; |
| 277 | } |
| 278 | return 0; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 279 | } |
| 280 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 281 | void twrpTruetype::gr_ttf_calcMaxFontHeight(TrueTypeFont *f) { |
| 282 | char c; |
| 283 | int char_idx; |
| 284 | int error; |
| 285 | FT_Glyph glyph; |
| 286 | FT_BBox bbox; |
| 287 | FT_BBox bbox_glyph; |
| 288 | TrueTypeCacheEntry *ent; |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 289 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 290 | bbox.yMin = bbox_glyph.yMin = LONG_MAX; |
| 291 | bbox.yMax = bbox_glyph.yMax = LONG_MIN; |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 292 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 293 | for(c = '!'; c <= '~'; ++c) |
| 294 | { |
| 295 | char_idx = FT_Get_Char_Index(f->face, c); |
| 296 | ent = gr_ttf_glyph_cache_peek(f, char_idx); |
| 297 | if(ent) |
| 298 | { |
| 299 | bbox.yMin = MIN(bbox.yMin, ent->bbox.yMin); |
| 300 | bbox.yMax = MAX(bbox.yMax, ent->bbox.yMax); |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | error = FT_Load_Glyph(f->face, char_idx, 0); |
| 305 | if(error) |
| 306 | continue; |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 307 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 308 | error = FT_Get_Glyph(f->face->glyph, &glyph); |
| 309 | if(error) |
| 310 | continue; |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 311 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 312 | FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox_glyph); |
| 313 | bbox.yMin = MIN(bbox.yMin, bbox_glyph.yMin); |
| 314 | bbox.yMax = MAX(bbox.yMax, bbox_glyph.yMax); |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 315 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 316 | FT_Done_Glyph(glyph); |
| 317 | } |
| 318 | } |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 319 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 320 | if(bbox.yMin > bbox.yMax) |
| 321 | bbox.yMin = bbox.yMax = 0; |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 322 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 323 | f->max_height = bbox.yMax - bbox.yMin; |
| 324 | f->base = bbox.yMax; |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 325 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 326 | // FIXME: twrp fonts have some padding on top, I'll add it here |
| 327 | // Should be fixed in the themes |
| 328 | f->max_height += f->size / 4; |
| 329 | f->base += f->size / 4; |
Vojtech Bocek | a482f25 | 2015-03-15 17:03:50 +0100 | [diff] [blame] | 330 | } |
| 331 | |
xiaolu | e738da5 | 2015-02-22 20:49:35 +0800 | [diff] [blame] | 332 | // returns number of bytes from const char *text rendered to fit max_width, not number of UTF8 characters! |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 333 | int twrpTruetype::gr_ttf_render_text(TrueTypeFont *font, GGLSurface *surface, const std::string text, int max_width) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 334 | TrueTypeFont *f = font; |
| 335 | TrueTypeCacheEntry *ent; |
| 336 | int bytes_rendered = 0, total_w = 0; |
| 337 | int utf_bytes = 0; |
| 338 | unsigned int unicode = 0; |
| 339 | int i, x, diff, char_idx, prev_idx = 0; |
| 340 | int height; |
| 341 | FT_Vector delta; |
| 342 | uint8_t *data = NULL; |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 343 | const char *text_itr = text.c_str(); |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 344 | int *char_idxs; |
| 345 | int char_idxs_len = 0; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 346 | |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 347 | char_idxs = new int[text.length()]; |
xiaolu | e738da5 | 2015-02-22 20:49:35 +0800 | [diff] [blame] | 348 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 349 | while(*text_itr) |
| 350 | { |
| 351 | utf_bytes = utf8_to_unicode(text_itr, &unicode); |
| 352 | text_itr += utf_bytes; |
| 353 | bytes_rendered += utf_bytes; |
xiaolu | e738da5 | 2015-02-22 20:49:35 +0800 | [diff] [blame] | 354 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 355 | char_idx = FT_Get_Char_Index(f->face, unicode); |
| 356 | char_idxs[char_idxs_len] = char_idx; |
| 357 | ent = gr_ttf_glyph_cache_get(f, char_idx); |
| 358 | if(ent) |
| 359 | { |
| 360 | diff = ent->glyph->root.advance.x >> 16; |
xiaolu | e738da5 | 2015-02-22 20:49:35 +0800 | [diff] [blame] | 361 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 362 | if(FT_HAS_KERNING(f->face) && prev_idx && char_idx) |
| 363 | { |
| 364 | FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta); |
| 365 | diff += delta.x >> 6; |
| 366 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 367 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 368 | if(max_width != -1 && total_w + diff > max_width) |
| 369 | break; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 370 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 371 | total_w += diff; |
| 372 | } |
| 373 | prev_idx = char_idx; |
| 374 | ++char_idxs_len; |
| 375 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 376 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 377 | if(font->max_height == -1) |
| 378 | gr_ttf_calcMaxFontHeight(font); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 379 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 380 | if(font->max_height == -1) |
| 381 | { |
| 382 | delete [] char_idxs; |
| 383 | return -1; |
| 384 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 385 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 386 | height = font->max_height; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 387 | |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 388 | data = (uint8_t *)malloc(total_w*height); |
| 389 | memset(data, 0, total_w*height); x = 0; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 390 | prev_idx = 0; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 391 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 392 | surface->version = sizeof(*surface); |
| 393 | surface->width = total_w; |
| 394 | surface->height = height; |
| 395 | surface->stride = total_w; |
| 396 | surface->data = (GGLubyte*)data; |
| 397 | surface->format = GGL_PIXEL_FORMAT_A_8; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 398 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 399 | for(i = 0; i < char_idxs_len; ++i) |
| 400 | { |
| 401 | char_idx = char_idxs[i]; |
| 402 | if(FT_HAS_KERNING(f->face) && prev_idx && char_idx) |
| 403 | { |
| 404 | FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta); |
| 405 | x += delta.x >> 6; |
| 406 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 407 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 408 | ent = gr_ttf_glyph_cache_get(f, char_idx); |
| 409 | if(ent) |
| 410 | { |
| 411 | gr_ttf_copy_glyph_to_surface(surface, ent->glyph, x, 0, font->base); |
| 412 | x += ent->glyph->root.advance.x >> 16; |
| 413 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 414 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 415 | prev_idx = char_idx; |
| 416 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 417 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 418 | delete [] char_idxs; |
| 419 | return bytes_rendered; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 420 | } |
| 421 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 422 | StringCacheEntry* twrpTruetype::gr_ttf_string_cache_peek(TrueTypeFont *font, |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 423 | const std::string text, |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 424 | __attribute__((unused)) int max_width) { |
| 425 | StringCacheKey k = { |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 426 | .text = text, |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 427 | .max_width = max_width |
| 428 | }; |
| 429 | StringCacheMap::iterator stringCacheItr = font->string_cache.find(k); |
| 430 | if (stringCacheItr != font->string_cache.end()) { |
| 431 | return stringCacheItr->second; |
| 432 | } |
| 433 | else { |
| 434 | return nullptr; |
| 435 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 436 | } |
| 437 | |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 438 | void twrpTruetype::gr_ttf_string_cache_truncate(TrueTypeFont *font) { |
| 439 | StringCacheMap::iterator stringCacheItr; |
| 440 | |
| 441 | if (font->string_cache.size() == STRING_CACHE_MAX_ENTRIES) { |
| 442 | StringCacheEntry *truncateEntry = nullptr; |
| 443 | stringCacheItr = font->string_cache.begin(); |
| 444 | int deleteCtr = 0; |
| 445 | while (stringCacheItr != font->string_cache.end() || deleteCtr == (STRING_CACHE_MAX_ENTRIES - 1)) { |
| 446 | truncateEntry = stringCacheItr->second; |
| 447 | gr_ttf_freeStringCache(truncateEntry->key, truncateEntry, nullptr); |
| 448 | stringCacheItr = font->string_cache.erase(stringCacheItr); |
| 449 | deleteCtr++; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | StringCacheEntry* twrpTruetype::gr_ttf_string_cache_get(TrueTypeFont *font, const std::string text, int max_width) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 455 | StringCacheEntry *res = nullptr; |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 456 | StringCacheMap::iterator stringCacheItr; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 457 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 458 | StringCacheKey k = { |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 459 | .text = text, |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 460 | .max_width = max_width |
| 461 | }; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 462 | |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 463 | stringCacheItr = font->string_cache.find(k); |
| 464 | if (stringCacheItr == font->string_cache.end()) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 465 | res = new StringCacheEntry; |
| 466 | res->rendered_bytes = gr_ttf_render_text(font, &res->surface, text, max_width); |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 467 | if(res->rendered_bytes < 0) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 468 | delete res; |
| 469 | return nullptr; |
| 470 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 471 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 472 | StringCacheKey *new_key = new StringCacheKey; |
| 473 | new_key->max_width = max_width; |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 474 | new_key->text = text; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 475 | res->key = new_key; |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 476 | font->string_cache[*new_key] = res; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 477 | } |
| 478 | else |
| 479 | { |
| 480 | res = stringCacheItr->second; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 481 | } |
| 482 | return res; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 483 | } |
| 484 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 485 | int twrpTruetype::gr_ttf_measureEx(const char *s, void *font) { |
| 486 | TrueTypeFont *f = (TrueTypeFont *)font; |
| 487 | int res = -1; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 488 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 489 | pthread_mutex_lock(&f->mutex); |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 490 | gr_ttf_string_cache_truncate(f); |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 491 | StringCacheEntry *e = gr_ttf_string_cache_get(f, s, -1); |
| 492 | if(e) |
| 493 | res = e->surface.width; |
| 494 | pthread_mutex_unlock(&f->mutex); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 495 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 496 | return res; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 497 | } |
| 498 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 499 | int twrpTruetype::gr_ttf_maxExW(const char *s, void *font, int max_width) { |
| 500 | TrueTypeFont *f = (TrueTypeFont *)font; |
| 501 | TrueTypeCacheEntry *ent; |
| 502 | int max_bytes = 0, total_w = 0; |
| 503 | int utf_bytes, prev_utf_bytes = 0; |
| 504 | unsigned int unicode = 0; |
| 505 | int char_idx, prev_idx = 0; |
| 506 | FT_Vector delta; |
| 507 | StringCacheEntry *e; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 508 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 509 | pthread_mutex_lock(&f->mutex); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 510 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 511 | e = gr_ttf_string_cache_peek(f, s, max_width); |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 512 | if (e) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 513 | max_bytes = e->rendered_bytes; |
| 514 | pthread_mutex_unlock(&f->mutex); |
| 515 | return max_bytes; |
| 516 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 517 | |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 518 | while(*s) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 519 | utf_bytes = utf8_to_unicode(s, &unicode); |
| 520 | s += utf_bytes; |
xiaolu | e738da5 | 2015-02-22 20:49:35 +0800 | [diff] [blame] | 521 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 522 | char_idx = FT_Get_Char_Index(f->face, unicode); |
| 523 | if(FT_HAS_KERNING(f->face) && prev_idx && char_idx) |
| 524 | { |
| 525 | FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta); |
| 526 | total_w += delta.x >> 6; |
| 527 | } |
| 528 | prev_idx = char_idx; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 529 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 530 | if(total_w > max_width) |
| 531 | { |
| 532 | max_bytes -= prev_utf_bytes; |
| 533 | break; |
| 534 | } |
| 535 | prev_utf_bytes = utf_bytes; |
| 536 | ent = gr_ttf_glyph_cache_get(f, char_idx); |
| 537 | if(!ent) |
| 538 | continue; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 539 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 540 | total_w += ent->glyph->root.advance.x >> 16; |
| 541 | max_bytes += utf_bytes; |
| 542 | } |
| 543 | pthread_mutex_unlock(&f->mutex); |
| 544 | return max_bytes; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 545 | } |
| 546 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 547 | int twrpTruetype::gr_ttf_textExWH(void *context, int x, int y, |
| 548 | const char *s, void *pFont, |
| 549 | int max_width, int max_height, |
| 550 | const gr_surface gr_draw_surface) { |
| 551 | GGLContext *gl = (GGLContext *)context; |
| 552 | TrueTypeFont *font = (TrueTypeFont *)pFont; |
| 553 | const GRSurface *gr_draw = (const GRSurface*) gr_draw_surface; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 554 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 555 | // not actualy max width, but max_width + x |
| 556 | if(max_width != -1) |
| 557 | { |
| 558 | max_width -= x; |
| 559 | if(max_width <= 0) |
| 560 | return 0; |
| 561 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 562 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 563 | pthread_mutex_lock(&font->mutex); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 564 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 565 | StringCacheEntry *e = gr_ttf_string_cache_get(font, s, max_width); |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 566 | if (!e) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 567 | pthread_mutex_unlock(&font->mutex); |
| 568 | return -1; |
| 569 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 570 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 571 | #if TW_ROTATION != 0 |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 572 | // Do not perform relatively expensive operation if not needed |
| 573 | GGLSurface string_surface_rotated; |
| 574 | string_surface_rotated.version = sizeof(string_surface_rotated); |
| 575 | // Skip the **(TW_ROTATION == 0)** || (TW_ROTATION == 180) check |
| 576 | // because we are under a TW_ROTATION != 0 conditional compilation statement |
| 577 | string_surface_rotated.width = (TW_ROTATION == 180) ? e->surface.width : e->surface.height; |
| 578 | string_surface_rotated.height = (TW_ROTATION == 180) ? e->surface.height : e->surface.width; |
| 579 | string_surface_rotated.stride = string_surface_rotated.width; |
| 580 | string_surface_rotated.format = e->surface.format; |
| 581 | // e->surface.format is GGL_PIXEL_FORMAT_A_8 (grayscale) |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 582 | string_surface_rotated.data = (GGLubyte*) malloc(string_surface_rotated.stride * string_surface_rotated.height * 1); |
| 583 | surface_ROTATION_transform((gr_surface) &string_surface_rotated, (const gr_surface) &e->surface, 1); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 584 | #endif |
| 585 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 586 | int y_bottom = y + e->surface.height; |
| 587 | int res = e->rendered_bytes; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 588 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 589 | if(max_height != -1 && max_height < y_bottom) |
| 590 | { |
| 591 | y_bottom = max_height; |
| 592 | if(y_bottom <= y) |
| 593 | { |
| 594 | pthread_mutex_unlock(&font->mutex); |
| 595 | return 0; |
| 596 | } |
| 597 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 598 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 599 | // Figuring out display coordinates works for TW_ROTATION == 0 too, |
| 600 | // and isn't as expensive as allocating and rotating another surface, |
| 601 | // so we do this anyway. |
| 602 | int x0_disp, y0_disp, x1_disp, y1_disp; |
| 603 | int l_disp, r_disp, t_disp, b_disp; |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 604 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 605 | x0_disp = ROTATION_X_DISP(x, y, gr_draw); |
| 606 | y0_disp = ROTATION_Y_DISP(x, y, gr_draw); |
| 607 | x1_disp = ROTATION_X_DISP(x + e->surface.width, y_bottom, gr_draw); |
| 608 | y1_disp = ROTATION_Y_DISP(x + e->surface.width, y_bottom, gr_draw); |
| 609 | l_disp = std::min(x0_disp, x1_disp); |
| 610 | r_disp = std::max(x0_disp, x1_disp); |
| 611 | t_disp = std::min(y0_disp, y1_disp); |
| 612 | b_disp = std::max(y0_disp, y1_disp); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 613 | |
| 614 | #if TW_ROTATION != 0 |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 615 | gl->bindTexture(gl, &string_surface_rotated); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 616 | #else |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 617 | gl->bindTexture(gl, &e->surface); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 618 | #endif |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 619 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 620 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 621 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 622 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 623 | gl->enable(gl, GGL_TEXTURE_2D); |
| 624 | gl->texCoord2i(gl, -l_disp, -t_disp); |
| 625 | gl->recti(gl, l_disp, t_disp, r_disp, b_disp); |
| 626 | gl->disable(gl, GGL_TEXTURE_2D); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 627 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 628 | #if TW_ROTATION != 0 |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 629 | free(string_surface_rotated.data); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 630 | #endif |
bigbiff bigbiff | ac6cc08 | 2019-11-01 19:00:27 -0400 | [diff] [blame] | 631 | pthread_mutex_unlock(&font->mutex);; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 632 | return res; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 633 | } |
| 634 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 635 | int twrpTruetype::gr_ttf_getMaxFontHeight(void *font) { |
| 636 | int res; |
| 637 | TrueTypeFont *f = (TrueTypeFont *)font; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 638 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 639 | pthread_mutex_lock(&f->mutex); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 640 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 641 | if(f->max_height == -1) |
| 642 | gr_ttf_calcMaxFontHeight(f); |
| 643 | res = f->max_height; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 644 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 645 | pthread_mutex_unlock(&f->mutex); |
| 646 | return res; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 647 | } |