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