| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file font.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created font.c |
| 10 | * - 3rd February 2009: Renamed font.c to font.cpp |
| 11 | * |
| 12 | * @par Licence: |
| 13 | * Copyright (c) 2005-2017 AJ Thomson |
| 14 | * |
| 15 | * OpenJazz is distributed under the terms of |
| 16 | * the GNU General Public License, version 2.0 |
| 17 | * |
| 18 | * @par Description: |
| 19 | * Deals with the loading, displaying and freeing of screen fonts. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "io/file.h"cxx . . $ `<file>/src/io/file.h`/ |
| 24 | #include "font.h"cxx . . $ `<file>/src/io/gfx/font.h`/ |
| 25 | #include "video.h"cxx . . $ `<file>/src/io/gfx/video.h`/ |
| 26 | #include "io/log.h"cxx . . $ `<file>/src/io/log.h`/ |
| 27 | |
| 28 | #include <stb_rect_pack.h>cxx . . $ `<file>/ext/stb/stb_rect_pack.h`/ |
| 29 | #include <string.h> |
| 30 | |
| 31 | namespacecxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/ { |
| 32 | constexpr unsigned int INVALID_FONT_CHARcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/INVALID_FONT_CHAR. = -1; |
| 33 | |
| 34 | constexpr int normalPaddingcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/normalPadding. = 2; |
| 35 | constexpr int sceneStringPaddingcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/sceneStringPadding. = 1; |
| 36 | } |
| 37 | |
| 38 | void Fontcxx . . $ Font#::commonSetupcxx . . $ Font#commonSetup(49f6e7a06ebc5aa8).() { |
| 39 | isOkcxx . . $ Font#isOk. = false; |
| 40 | lineHeightcxx . . $ Font#lineHeight. = 0; |
| 41 | spaceWidthcxx . . $ Font#spaceWidth. = 0; |
| 42 | nCharacterscxx . . $ Font#nCharacters. = MAX_FONT_CHARScxx . . $ `src/io/gfx/font.h:31:9`!; |
| 43 | memset(atlasRectscxx . . $ Font#atlasRects., 0, sizeof(atlasRectscxx . . $ Font#atlasRects.)); |
| 44 | memset(mapcxx . . $ Font#map., INVALID_FONT_CHARcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/INVALID_FONT_CHAR., sizeof(mapcxx . . $ Font#map.)); |
| 45 | } |
| 46 | |
| 47 | void Fontcxx . . $ Font#::cleanMappingcxx . . $ Font#cleanMapping(49f6e7a06ebc5aa8).() { |
| 48 | // remove empty |
| 49 | for (int i = 0; i < MAX_FONT_CHARScxx . . $ `src/io/gfx/font.h:31:9`!; i++) { |
| 50 | if (mapcxx . . $ Font#map.[i] >= static_cast<unsigned int>(nCharacterscxx . . $ Font#nCharacters.)) |
| 51 | mapcxx . . $ Font#map.[i] = INVALID_FONT_CHARcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/INVALID_FONT_CHAR.; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Load a font from the given .0FN file. |
| 57 | * |
| 58 | * @param fileName Name of an .0FN file |
| 59 | */ |
| 60 | Fontcxx . . $ Font#::Fontcxx . . $ Font#Font(85c52e162fed56f9). (const char* fileName) { |
| 61 | commonSetupcxx . . $ Font#commonSetup(49f6e7a06ebc5aa8).(); |
| 62 | |
| 63 | // Load font from a font file |
| 64 | Filecxx . . $ File#* file = new Filecxx . . $ File#File(e99a0a23efd851c6).(fileName, PATH_TYPE_GAMEcxx . . $ PATH_TYPE_GAME.); |
| 65 | int fileSize = file->getSizecxx . . $ File#getSize(b126dc7c1de90089).(); |
| 66 | |
| 67 | // Checking font file |
| 68 | char *identifier1 = file->loadStringcxx . . $ File#loadString(ff224f080444ee45).(18); |
| 69 | char identifier2 = file->loadCharcxx . . $ File#loadChar(7c4be072098b358f).(); |
| 70 | if (strncmp(identifier1, "Digital Dimensions", 18) != 0 || identifier2 != 0x1A) { |
| 71 | LOG_ERRORcxx . . $ `src/io/log.h:78:9`!("Font not valid!"); |
| 72 | delete[] identifier1; |
| 73 | return; |
| 74 | } |
| 75 | delete[] identifier1; |
| 76 | |
| 77 | spaceWidthcxx . . $ Font#spaceWidth. = file->loadCharcxx . . $ File#loadChar(7c4be072098b358f).(); |
| 78 | lineHeightcxx . . $ Font#lineHeight. = file->loadCharcxx . . $ File#loadChar(7c4be072098b358f).(); |
| 79 | LOG_MAXcxx . . $ `src/io/log.h:73:9`!("spaceWidth: %d, lineHeight: %d", spaceWidth, lineHeight); |
| 80 | |
| 81 | // temporary character data |
| 82 | SDL_Surface *chars[MAX_FONT_CHARScxx . . $ `src/io/gfx/font.h:31:9`!]; |
| 83 | stbrp_rect rects[MAX_FONT_CHARScxx . . $ `src/io/gfx/font.h:31:9`!]; |
| 84 | |
| 85 | // Load characters |
| 86 | for (int i = 0; i < MAX_FONT_CHARScxx . . $ `src/io/gfx/font.h:31:9`!; i++) { |
| 87 | if (file->tellcxx . . $ File#tell(b126dc7c1de90089).() >= fileSize) { |
| 88 | nCharacterscxx . . $ Font#nCharacters. = i; |
| 89 | LOG_TRACEcxx . . $ `src/io/log.h:74:9`!("Loaded %d characters from font.", nCharacters); |
| 90 | |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | int size = file->loadShortcxx . . $ File#loadShort(32a7769d58713265).(); |
| 95 | int w = 0; |
| 96 | int h = 0; |
| 97 | |
| 98 | if (size > 4) { |
| 99 | unsigned char* pixels = file->loadRLEcxx . . $ File#loadRLE(bf8b529691269f26).(size); |
| 100 | |
| 101 | int width = pixels[0]|pixels[1] << 8; |
| 102 | int height = pixels[2]|pixels[3] << 8; |
| 103 | |
| 104 | if (size - 4 >= width * height) { |
| 105 | chars[i] = videocxx . . $ video..createSurfacecxx . . $ Video#createSurface(5ec4649223a2dfef).(pixels + 4, width, height); |
| 106 | |
| 107 | w = width; |
| 108 | h = height; |
| 109 | } |
| 110 | |
| 111 | delete[] pixels; |
| 112 | } |
| 113 | |
| 114 | // setup sizes for packing |
| 115 | rects[i] =cxx . . $ stbrp_rect#`operator=`(3de49e50708b7b8d). { i, w, h, 0, 0, 0 }; |
| 116 | |
| 117 | // save size |
| 118 | atlasRectscxx . . $ Font#atlasRects.[i] = { -1, -1, w, h }; |
| 119 | } |
| 120 | |
| 121 | delete file; |
| 122 | |
| 123 | // Pack all characters in a 128x128 pixels surface |
| 124 | int aW = 128; |
| 125 | int aH = 128; |
| 126 | characterAtlascxx . . $ Font#characterAtlas. = videocxx . . $ video..createSurfacecxx . . $ Video#createSurface(5ec4649223a2dfef).(nullptr, aW, aH); |
| 127 | videocxx . . $ video..enableColorKeycxx . . $ Video#enableColorKey(905504f46e4e09c1).(characterAtlascxx . . $ Font#characterAtlas., 0); |
| 128 | |
| 129 | stbrp_context ctx; |
| 130 | stbrp_node nodes[aW]; |
| 131 | stbrp_init_target(&ctx, aW, aH, nodes, aW); |
| 132 | |
| 133 | bool res = stbrp_pack_rects(&ctx, rects, nCharacterscxx . . $ Font#nCharacters.); |
| 134 | if(res) { |
| 135 | for (int i = 0; i < nCharacterscxx . . $ Font#nCharacters.; i++) { |
| 136 | // save position |
| 137 | atlasRectscxx . . $ Font#atlasRects.[i].x = rects[i].xcxx . . $ stbrp_rect#x.; |
| 138 | atlasRectscxx . . $ Font#atlasRects.[i].y = rects[i].ycxx . . $ stbrp_rect#y.; |
| 139 | |
| 140 | // copy char to atlas |
| 141 | if (rects[i].wcxx . . $ stbrp_rect#w. > 0 && rects[i].hcxx . . $ stbrp_rect#h. > 0) |
| 142 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(chars[i], nullptr, characterAtlascxx . . $ Font#characterAtlas., &atlasRectscxx . . $ Font#atlasRects.[i]); |
| 143 | } |
| 144 | } else |
| 145 | LOG_WARNcxx . . $ `src/io/log.h:77:9`!("Could not pack font atlas!"); |
| 146 | |
| 147 | // Delete single char surfaces |
| 148 | for (int i = 0; i < nCharacterscxx . . $ Font#nCharacters.; i++) { |
| 149 | if(rects[i].wcxx . . $ stbrp_rect#w. > 0 && rects[i].hcxx . . $ stbrp_rect#h. > 0) |
| 150 | videocxx . . $ video..destroySurfacecxx . . $ Video#destroySurface(60a0b30a00b4fc6d).(chars[i]); |
| 151 | } |
| 152 | |
| 153 | // Create ASCII->font map |
| 154 | mapcxx . . $ Font#map.[33] = 107; // ! |
| 155 | mapcxx . . $ Font#map.[34] = 116; // " |
| 156 | mapcxx . . $ Font#map.[36] = 63; // $ |
| 157 | mapcxx . . $ Font#map.[39] = 115; // ' |
| 158 | mapcxx . . $ Font#map.[40] = 111; // ( |
| 159 | mapcxx . . $ Font#map.[41] = 112; // ) |
| 160 | mapcxx . . $ Font#map.[43] = 105; // + |
| 161 | mapcxx . . $ Font#map.[44] = 101; // , |
| 162 | mapcxx . . $ Font#map.[45] = 104; // - |
| 163 | mapcxx . . $ Font#map.[46] = 102; // . |
| 164 | mapcxx . . $ Font#map.[47] = 108; // / |
| 165 | for (int i = 48; i < 58; i++) |
| 166 | mapcxx . . $ Font#map.[i] = i + 5; // Numbers |
| 167 | mapcxx . . $ Font#map.[58] = 114; // : |
| 168 | mapcxx . . $ Font#map.[59] = 113; // ; |
| 169 | mapcxx . . $ Font#map.[61] = 106; // = |
| 170 | mapcxx . . $ Font#map.[63] = 103; // ? |
| 171 | for (int i = 65; i < 91; i++) |
| 172 | mapcxx . . $ Font#map.[i] = i - 38; // Upper-case letters |
| 173 | for (int i = 97; i < 123; i++) |
| 174 | mapcxx . . $ Font#map.[i] = i - 96; // Lower-case letters |
| 175 | |
| 176 | cleanMappingcxx . . $ Font#cleanMapping(49f6e7a06ebc5aa8).(); |
| 177 | |
| 178 | isOkcxx . . $ Font#isOk. = res; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * Create a font from the panel pixel data. |
| 184 | * |
| 185 | * @param pixels Panel pixel data |
| 186 | * @param big Whether to use the small or the big font |
| 187 | */ |
| 188 | Fontcxx . . $ Font#::Fontcxx . . $ Font#Font(ff9db7b1d71f489c). (unsigned char* pixels, bool big) { |
| 189 | commonSetupcxx . . $ Font#commonSetup(49f6e7a06ebc5aa8).(); |
| 190 | |
| 191 | int charWidth = 8; |
| 192 | spaceWidthcxx . . $ Font#spaceWidth. = 5; |
| 193 | if (big) { |
| 194 | nCharacterscxx . . $ Font#nCharacters. = 40; |
| 195 | lineHeightcxx . . $ Font#lineHeight. = 8; |
| 196 | } else { |
| 197 | nCharacterscxx . . $ Font#nCharacters. = 13; |
| 198 | lineHeightcxx . . $ Font#lineHeight. = 7; |
| 199 | } |
| 200 | |
| 201 | // temporary character data |
| 202 | SDL_Surface *chars[nCharacterscxx . . $ Font#nCharacters.]; |
| 203 | stbrp_rect rects[MAX_FONT_CHARScxx . . $ `src/io/gfx/font.h:31:9`!]; |
| 204 | |
| 205 | unsigned char* chrPixels = new unsigned char[charWidth * lineHeightcxx . . $ Font#lineHeight.]; |
| 206 | |
| 207 | for (int i = 0; i < nCharacterscxx . . $ Font#nCharacters.; i++) { |
| 208 | // copy to atlas |
| 209 | for (int y = 0; y < lineHeightcxx . . $ Font#lineHeight.; y++) { |
| 210 | memcpy(chrPixels + (y * charWidth), |
| 211 | pixels + (i * charWidth) + (y * SWcxx . . $ `src/io/gfx/video.h:38:9`!), charWidth); |
| 212 | } |
| 213 | chars[i] = videocxx . . $ video..createSurfacecxx . . $ Video#createSurface(5ec4649223a2dfef).(chrPixels, charWidth, lineHeightcxx . . $ Font#lineHeight.); |
| 214 | |
| 215 | // setup sizes for packing |
| 216 | rects[i] =cxx . . $ stbrp_rect#`operator=`(3de49e50708b7b8d). { i, charWidth, lineHeightcxx . . $ Font#lineHeight., 0, 0, 0 }; |
| 217 | |
| 218 | // save size |
| 219 | atlasRectscxx . . $ Font#atlasRects.[i] = { -1, -1, charWidth, lineHeightcxx . . $ Font#lineHeight. }; |
| 220 | } |
| 221 | |
| 222 | delete[] chrPixels; |
| 223 | |
| 224 | // Pack all characters in a surface |
| 225 | int aW, aH; |
| 226 | if(big) { |
| 227 | // 56x48 |
| 228 | aW = 7 * charWidth; |
| 229 | aH = 6 * lineHeightcxx . . $ Font#lineHeight.; |
| 230 | } else { |
| 231 | // 32x28 |
| 232 | aW = 4 * charWidth; |
| 233 | aH = 4 * lineHeightcxx . . $ Font#lineHeight.; |
| 234 | } |
| 235 | |
| 236 | characterAtlascxx . . $ Font#characterAtlas. = videocxx . . $ video..createSurfacecxx . . $ Video#createSurface(5ec4649223a2dfef).(nullptr, aW, aH); |
| 237 | if (big) videocxx . . $ video..enableColorKeycxx . . $ Video#enableColorKey(905504f46e4e09c1).(characterAtlascxx . . $ Font#characterAtlas., 31); |
| 238 | |
| 239 | stbrp_context ctx; |
| 240 | stbrp_node nodes[aW]; |
| 241 | stbrp_init_target(&ctx, aW, aH, nodes, aW); |
| 242 | |
| 243 | bool res = stbrp_pack_rects(&ctx, rects, nCharacterscxx . . $ Font#nCharacters.); |
| 244 | if(res) { |
| 245 | for (int i = 0; i < nCharacterscxx . . $ Font#nCharacters.; i++) { |
| 246 | // save position |
| 247 | atlasRectscxx . . $ Font#atlasRects.[i].x = rects[i].xcxx . . $ stbrp_rect#x.; |
| 248 | atlasRectscxx . . $ Font#atlasRects.[i].y = rects[i].ycxx . . $ stbrp_rect#y.; |
| 249 | |
| 250 | // copy char to atlas |
| 251 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(chars[i], nullptr, characterAtlascxx . . $ Font#characterAtlas., &atlasRectscxx . . $ Font#atlasRects.[i]); |
| 252 | } |
| 253 | } else |
| 254 | LOG_WARNcxx . . $ `src/io/log.h:77:9`!("Could not pack font atlas!"); |
| 255 | |
| 256 | // Delete single char surfaces |
| 257 | for (int i = 0; i < nCharacterscxx . . $ Font#nCharacters.; i++) |
| 258 | videocxx . . $ video..destroySurfacecxx . . $ Video#destroySurface(60a0b30a00b4fc6d).(chars[i]); |
| 259 | |
| 260 | // Create ASCII->font map |
| 261 | if (big) { |
| 262 | // Goes " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-:." |
| 263 | mapcxx . . $ Font#map.[45] = 37; // - |
| 264 | mapcxx . . $ Font#map.[46] = 39; // . |
| 265 | mapcxx . . $ Font#map.[58] = 38; // : |
| 266 | for (int i = 47; i < 58; i++) |
| 267 | mapcxx . . $ Font#map.[i] = i - 47; // Numbers |
| 268 | for (int i = 64; i < 91; i++) { |
| 269 | mapcxx . . $ Font#map.[i] = i - 54; // Upper-case letters |
| 270 | mapcxx . . $ Font#map.[i+32] = mapcxx . . $ Font#map.[i]; // Lower-case letters (copy) |
| 271 | } |
| 272 | } else { |
| 273 | // Goes " 0123456789oo" (where oo = infinity) |
| 274 | // Use :; to represent the infinity symbol |
| 275 | for (int i = 47; i < 60; i++) |
| 276 | mapcxx . . $ Font#map.[i] = i - 47; // Numbers and :; |
| 277 | } |
| 278 | |
| 279 | isOkcxx . . $ Font#isOk. = res; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /** |
| 284 | * Load a font from a .000 file. |
| 285 | * |
| 286 | * @param bonus whether to use FONTS.000 or BONUS.000 |
| 287 | */ |
| 288 | Fontcxx . . $ Font#::Fontcxx . . $ Font#Font(b45ddcbc8423892c). (bool bonus) { |
| 289 | commonSetupcxx . . $ Font#commonSetup(49f6e7a06ebc5aa8).(); |
| 290 | |
| 291 | // Load font from FONTS.000 or BONUS.000 |
| 292 | Filecxx . . $ File#* file = new Filecxx . . $ File#File(e99a0a23efd851c6).(bonus? "BONUS.000": "FONTS.000", PATH_TYPE_GAMEcxx . . $ PATH_TYPE_GAME.); |
| 293 | |
| 294 | int fileSize = file->getSizecxx . . $ File#getSize(b126dc7c1de90089).(); |
| 295 | nCharacterscxx . . $ Font#nCharacters. = file->loadShortcxx . . $ File#loadShort(a18c05e4017afb3d).(256); |
| 296 | |
| 297 | if (bonus) { |
| 298 | int nSprites = file->loadShortcxx . . $ File#loadShort(32a7769d58713265).(); |
| 299 | nCharacterscxx . . $ Font#nCharacters. -= nSprites; |
| 300 | |
| 301 | // Skip sprites |
| 302 | for (int i = 0; i < nSprites; i++) { |
| 303 | file->seekcxx . . $ File#seek(918ebd27a9ddbad3).(4, false); |
| 304 | |
| 305 | int width = file->loadShortcxx . . $ File#loadShort(32a7769d58713265).(); |
| 306 | if (width == 0xFFFF) width = 0; |
| 307 | |
| 308 | file->seekcxx . . $ File#seek(918ebd27a9ddbad3).((width << 2) + file->loadShortcxx . . $ File#loadShort(32a7769d58713265).(), false); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // temporary character data |
| 313 | SDL_Surface *chars[MAX_FONT_CHARScxx . . $ `src/io/gfx/font.h:31:9`!]; |
| 314 | stbrp_rect rects[MAX_FONT_CHARScxx . . $ `src/io/gfx/font.h:31:9`!]; |
| 315 | |
| 316 | // Load characters |
| 317 | for (int i = 0; i < nCharacterscxx . . $ Font#nCharacters.; i++) { |
| 318 | if (file->tellcxx . . $ File#tell(b126dc7c1de90089).() >= fileSize) { |
| 319 | nCharacterscxx . . $ Font#nCharacters. = i; |
| 320 | LOG_TRACEcxx . . $ `src/io/log.h:74:9`!("Loaded %d characters from font.", nCharacters); |
| 321 | |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | int width = file->loadShortcxx . . $ File#loadShort(a18c05e4017afb3d).(SWcxx . . $ `src/io/gfx/video.h:38:9`!); |
| 326 | int height = file->loadShortcxx . . $ File#loadShort(a18c05e4017afb3d).(SHcxx . . $ `src/io/gfx/video.h:39:9`!); |
| 327 | |
| 328 | // adjust |
| 329 | if (bonus) width = (width + 3) & ~3; |
| 330 | else width <<= 2; |
| 331 | |
| 332 | file->seekcxx . . $ File#seek(918ebd27a9ddbad3).(4, false); |
| 333 | |
| 334 | unsigned char *pixels = file->loadPixelscxx . . $ File#loadPixels(f18dc31a6ecbac56).(width * height); |
| 335 | |
| 336 | chars[i] = videocxx . . $ video..createSurfacecxx . . $ Video#createSurface(5ec4649223a2dfef).(pixels, width, height); |
| 337 | |
| 338 | delete[] pixels; |
| 339 | |
| 340 | // setup sizes for packing |
| 341 | rects[i] =cxx . . $ stbrp_rect#`operator=`(3de49e50708b7b8d). { i, width, height, 0, 0, 0 }; |
| 342 | |
| 343 | // save size |
| 344 | atlasRectscxx . . $ Font#atlasRects.[i] = { -1, -1, width, height }; |
| 345 | } |
| 346 | |
| 347 | delete file; |
| 348 | |
| 349 | spaceWidthcxx . . $ Font#spaceWidth. = 5; |
| 350 | // use "A" as reference |
| 351 | lineHeightcxx . . $ Font#lineHeight. = atlasRectscxx . . $ Font#atlasRects.[0].h; |
| 352 | LOG_MAXcxx . . $ `src/io/log.h:73:9`!("spaceWidth: %d, lineHeight: %d", spaceWidth, lineHeight); |
| 353 | |
| 354 | // Pack all characters in a 160x160 pixels surface |
| 355 | int aW = 160; |
| 356 | int aH = 160; |
| 357 | characterAtlascxx . . $ Font#characterAtlas. = videocxx . . $ video..createSurfacecxx . . $ Video#createSurface(5ec4649223a2dfef).(nullptr, aW, aH); |
| 358 | videocxx . . $ video..enableColorKeycxx . . $ Video#enableColorKey(905504f46e4e09c1).(characterAtlascxx . . $ Font#characterAtlas., 254); |
| 359 | |
| 360 | stbrp_context ctx; |
| 361 | stbrp_node nodes[aW]; |
| 362 | stbrp_init_target(&ctx, aW, aH, nodes, aW); |
| 363 | |
| 364 | bool res = stbrp_pack_rects(&ctx, rects, nCharacterscxx . . $ Font#nCharacters.); |
| 365 | if(res) { |
| 366 | for (int i = 0; i < nCharacterscxx . . $ Font#nCharacters.; i++) { |
| 367 | // save position |
| 368 | atlasRectscxx . . $ Font#atlasRects.[i].x = rects[i].xcxx . . $ stbrp_rect#x.; |
| 369 | atlasRectscxx . . $ Font#atlasRects.[i].y = rects[i].ycxx . . $ stbrp_rect#y.; |
| 370 | |
| 371 | // copy char to atlas |
| 372 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(chars[i], nullptr, characterAtlascxx . . $ Font#characterAtlas., &atlasRectscxx . . $ Font#atlasRects.[i]); |
| 373 | } |
| 374 | } else |
| 375 | LOG_WARNcxx . . $ `src/io/log.h:77:9`!("Could not pack font atlas!"); |
| 376 | |
| 377 | // Delete single char surfaces |
| 378 | for (int i = 0; i < nCharacterscxx . . $ Font#nCharacters.; i++) |
| 379 | videocxx . . $ video..destroySurfacecxx . . $ Video#destroySurface(60a0b30a00b4fc6d).(chars[i]); |
| 380 | |
| 381 | // Create ASCII->font map |
| 382 | if (bonus) { |
| 383 | mapcxx . . $ Font#map.[42] = 37; // * |
| 384 | mapcxx . . $ Font#map.[46] = 39; // . |
| 385 | mapcxx . . $ Font#map.[47] = 38; // / |
| 386 | mapcxx . . $ Font#map.[58] = 36; // : |
| 387 | } else { |
| 388 | mapcxx . . $ Font#map.[37] = 36; // % |
| 389 | } |
| 390 | for (int i = 48; i < 58; i++) |
| 391 | mapcxx . . $ Font#map.[i] = i - 22; // Numbers |
| 392 | for (int i = 65; i < 91; i++) { |
| 393 | mapcxx . . $ Font#map.[i] = i - 65; // Upper-case letters |
| 394 | mapcxx . . $ Font#map.[i+32] = mapcxx . . $ Font#map.[i]; // Lower-case letters (copy) |
| 395 | } |
| 396 | |
| 397 | cleanMappingcxx . . $ Font#cleanMapping(49f6e7a06ebc5aa8).(); |
| 398 | |
| 399 | isOkcxx . . $ Font#isOk. = res; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | /** |
| 404 | * Delete the font. |
| 405 | */ |
| 406 | Fontcxx . . $ Font#::~cxx . . $ Font#`~Font`(49f6e7a06ebc5aa8).Fontcxx . . $ Font# () { |
| 407 | videocxx . . $ video..destroySurfacecxx . . $ Video#destroySurface(60a0b30a00b4fc6d).(characterAtlascxx . . $ Font#characterAtlas.); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | /** |
| 412 | * Draw a string using the font. |
| 413 | * |
| 414 | * @param string The string to draw |
| 415 | * @param x The x-coordinate at which to draw the string |
| 416 | * @param y The y-coordinate at which to draw the string |
| 417 | * @param xAlign The horizontal alignment |
| 418 | * @param yAlign The vertical alignment |
| 419 | * |
| 420 | * @return The x and y coordinates of the end of the string |
| 421 | */ |
| 422 | Pointcxx . . $ Point# Fontcxx . . $ Font#::showStringcxx . . $ Font#showString(7e50a619d5a117f1). (const char* string, int x, int y, |
| 423 | alignXcxx . . $ alignX# xAlign, alignYcxx . . $ alignY# yAlign) { |
| 424 | |
| 425 | if (!isOkcxx . . $ Font#isOk.) return Pointcxx . . $ Point#Point(3e454b8ccf442868).(x, y); |
| 426 | |
| 427 | // Determine the position at which to draw the first character |
| 428 | int xOffsetBase, xOffset, yOffset; |
| 429 | switch(xAlign) { |
| 430 | default: |
| 431 | case alignXcxx . . $ alignX#::Leftcxx . . $ alignX#Left.: |
| 432 | xOffset = x; |
| 433 | break; |
| 434 | |
| 435 | case alignXcxx . . $ alignX#::Centercxx . . $ alignX#Center.: |
| 436 | xOffset = x - (getStringWidthcxx . . $ Font#getStringWidth(5c4306d8553b3696).(string) >> 1); |
| 437 | break; |
| 438 | |
| 439 | case alignXcxx . . $ alignX#::Rightcxx . . $ alignX#Right.: |
| 440 | xOffset = x - getStringWidthcxx . . $ Font#getStringWidth(5c4306d8553b3696).(string); |
| 441 | break; |
| 442 | } |
| 443 | xOffsetBase = xOffset; |
| 444 | |
| 445 | switch(yAlign) { |
| 446 | default: |
| 447 | case alignYcxx . . $ alignY#::Topcxx . . $ alignY#Top.: |
| 448 | yOffset = y; |
| 449 | break; |
| 450 | |
| 451 | case alignYcxx . . $ alignY#::Centercxx . . $ alignY#Center.: |
| 452 | yOffset = y - (getStringHeightcxx . . $ Font#getStringHeight(5c4306d8553b3696).(string) >> 1); |
| 453 | break; |
| 454 | |
| 455 | case alignYcxx . . $ alignY#::Bottomcxx . . $ alignY#Bottom.: |
| 456 | yOffset = y - getStringHeightcxx . . $ Font#getStringHeight(5c4306d8553b3696).(string); |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | // Go through each character of the string |
| 461 | for (int i = 0; string[i]; i++) { |
| 462 | if (string[i] == '\n') { |
| 463 | // reset after line break |
| 464 | xOffset = xOffsetBase; |
| 465 | yOffset += lineHeightcxx . . $ Font#lineHeight.; |
| 466 | } else { |
| 467 | unsigned int c = mapcxx . . $ Font#map.[int(string[i])]; |
| 468 | |
| 469 | // skip spaces and invalid |
| 470 | if(c == INVALID_FONT_CHARcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/INVALID_FONT_CHAR.) { |
| 471 | xOffset += spaceWidthcxx . . $ Font#spaceWidth.; |
| 472 | #if DEBUG_FONTS |
| 473 | if (string[i] != ' ') // log invalid |
| 474 | LOG_MAX("Skipping char %d in %s at index %d", string[i], string, i); |
| 475 | #endif |
| 476 | continue; |
| 477 | } |
| 478 | |
| 479 | // Determine the character's position on the screen |
| 480 | SDL_Rect dst = { xOffset, yOffset, 0, 0 }; |
| 481 | |
| 482 | // Draw the character to the screen |
| 483 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(characterAtlascxx . . $ Font#characterAtlas., &atlasRectscxx . . $ Font#atlasRects.[c], canvascxx . . $ canvas., &dst); |
| 484 | |
| 485 | xOffset += atlasRectscxx . . $ Font#atlasRects.[c].w + normalPaddingcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/normalPadding.; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return Pointcxx . . $ Point#Point(3e454b8ccf442868).(xOffset, yOffset); |
| 490 | } |
| 491 | |
| 492 | Pointcxx . . $ Point# Fontcxx . . $ Font#::showStringCenteredcxx . . $ Font#showStringCentered(dffc78c69bf84bb8). (const char *s) { |
| 493 | return showStringcxx . . $ Font#showString(7e50a619d5a117f1).(s, (canvasWcxx . . $ canvasW. >> 1), (canvasHcxx . . $ canvasH. >> 1), |
| 494 | alignXcxx . . $ alignX#::Centercxx . . $ alignX#Center., alignYcxx . . $ alignY#::Centercxx . . $ alignY#Center.); |
| 495 | } |
| 496 | |
| 497 | |
| 498 | /** |
| 499 | * Draw a JJ1 cutscene string using the font. |
| 500 | * |
| 501 | * @param string The JJ1 cutstring to draw |
| 502 | * @param x The x-coordinate at which to draw the string |
| 503 | * @param y The y-coordinate at which to draw the string |
| 504 | * |
| 505 | * @return The x-coordinate of the end of the string |
| 506 | */ |
| 507 | int Fontcxx . . $ Font#::showSceneStringcxx . . $ Font#showSceneString(98fdf16c90fffe93). (const unsigned char* string, int x, int y) { |
| 508 | if (!isOkcxx . . $ Font#isOk.) return x; |
| 509 | |
| 510 | // Determine the position at which to draw the first character |
| 511 | int offset = x; |
| 512 | |
| 513 | // Go through each character of the string |
| 514 | for (int i = 0; string[i]; i++) { |
| 515 | |
| 516 | // Determine the character's position on the screen |
| 517 | SDL_Rect dst = { offset, y, 0, 0 }; |
| 518 | |
| 519 | // use space for invalid characters |
| 520 | if (string[i] >= nCharacterscxx . . $ Font#nCharacters.) { |
| 521 | offset += spaceWidthcxx . . $ Font#spaceWidth.; |
| 522 | continue; |
| 523 | } |
| 524 | |
| 525 | int c = string[i]; |
| 526 | |
| 527 | // Draw the character to the screen |
| 528 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(characterAtlascxx . . $ Font#characterAtlas., &atlasRectscxx . . $ Font#atlasRects.[c], canvascxx . . $ canvas., &dst); |
| 529 | |
| 530 | offset += atlasRectscxx . . $ Font#atlasRects.[c].w + sceneStringPaddingcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/sceneStringPadding.; |
| 531 | } |
| 532 | |
| 533 | return offset; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | /** |
| 538 | * Draw a number using the font. |
| 539 | * |
| 540 | * @param n The number to draw |
| 541 | * @param x The x-coordinate at which to draw the number |
| 542 | * @param y The y-coordinate at which to draw the number |
| 543 | * |
| 544 | * @return The x-coordinate of the end of the number |
| 545 | */ |
| 546 | void Fontcxx . . $ Font#::showNumbercxx . . $ Font#showNumber(b1146eedae1e1d0b). (int n, int x, int y) { |
| 547 | if (!isOkcxx . . $ Font#isOk.) return; |
| 548 | |
| 549 | SDL_Rect dst; |
| 550 | |
| 551 | // n being 0 is a special case. It must not be considered to be a trailing |
| 552 | // zero, as these are not displayed. |
| 553 | if (!n) { |
| 554 | unsigned int c = mapcxx . . $ Font#map.[int('0')]; |
| 555 | |
| 556 | // Determine 0's position on the screen |
| 557 | dst.y = y; |
| 558 | dst.x = x - atlasRectscxx . . $ Font#atlasRects.[c].w; |
| 559 | |
| 560 | // Draw 0 to the screen |
| 561 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(characterAtlascxx . . $ Font#characterAtlas., &atlasRectscxx . . $ Font#atlasRects.[c], canvascxx . . $ canvas., &dst); |
| 562 | |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | // Determine the length of the number to be drawn |
| 567 | int count; |
| 568 | if (n > 0) count = n; |
| 569 | else count = -n; |
| 570 | |
| 571 | // Determine the position at which to draw the lowest digit |
| 572 | int offset = x; |
| 573 | |
| 574 | while (count) { |
| 575 | unsigned int c = mapcxx . . $ Font#map.[int('0' + (count % 10))]; |
| 576 | |
| 577 | offset -= atlasRectscxx . . $ Font#atlasRects.[c].w; |
| 578 | |
| 579 | // Determine the digit's position on the screen |
| 580 | dst.y = y; |
| 581 | dst.x = offset; |
| 582 | |
| 583 | // Draw the digit to the screen |
| 584 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(characterAtlascxx . . $ Font#characterAtlas., &atlasRectscxx . . $ Font#atlasRects.[c], canvascxx . . $ canvas., &dst); |
| 585 | |
| 586 | count /= 10; |
| 587 | } |
| 588 | |
| 589 | // If needed, draw the negative sign |
| 590 | if (n < 0) { |
| 591 | unsigned int c = mapcxx . . $ Font#map.[int('-')]; |
| 592 | |
| 593 | // Determine the negative sign's position on the screen |
| 594 | dst.y = y; |
| 595 | dst.x = offset - atlasRectscxx . . $ Font#atlasRects.[c].w; |
| 596 | |
| 597 | // Draw the negative sign to the screen |
| 598 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(characterAtlascxx . . $ Font#characterAtlas., &atlasRectscxx . . $ Font#atlasRects.[c], canvascxx . . $ canvas., &dst); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | |
| 603 | /** |
| 604 | * Map a range of palette indices to another range |
| 605 | * |
| 606 | * @param start Start of original range |
| 607 | * @param length Span of original range |
| 608 | * @param newStart Start of new range |
| 609 | * @param newLength Span of new range |
| 610 | */ |
| 611 | void Fontcxx . . $ Font#::mapPalettecxx . . $ Font#mapPalette(748afa4755bc9595). (int start, int length, int newStart, int newLength) { |
| 612 | if (!isOkcxx . . $ Font#isOk.) return; |
| 613 | |
| 614 | SDL_Color palette[MAX_PALETTE_COLORScxx . . $ `src/OpenJazz.h:71:9`!]; |
| 615 | |
| 616 | for (int i = 0; i < length; i++) { |
| 617 | palette[i].rcxx . . $ SDL_Color#r. = palette[i].gcxx . . $ SDL_Color#g. = palette[i].bcxx . . $ SDL_Color#b. = (i * newLength / length) + newStart; |
| 618 | #if OJ_SDL3 || OJ_SDL3 |
| 619 | palette[i].a = 0xFF; |
| 620 | #endif |
| 621 | } |
| 622 | |
| 623 | videocxx . . $ video..setSurfacePalettecxx . . $ Video#setSurfacePalette(b59f7f041c83eb39).(characterAtlascxx . . $ Font#characterAtlas., palette, start, length); |
| 624 | } |
| 625 | |
| 626 | |
| 627 | /** |
| 628 | * Restore a palette to its original state. |
| 629 | */ |
| 630 | void Fontcxx . . $ Font#::restorePalettecxx . . $ Font#restorePalette(49f6e7a06ebc5aa8). () { |
| 631 | if (!isOkcxx . . $ Font#isOk.) return; |
| 632 | |
| 633 | videocxx . . $ video..restoreSurfacePalettecxx . . $ Video#restoreSurfacePalette(60a0b30a00b4fc6d).(characterAtlascxx . . $ Font#characterAtlas.); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | /** |
| 638 | * Get the width of a given string. |
| 639 | * |
| 640 | * @param string The string to measure |
| 641 | * |
| 642 | * @return The width |
| 643 | */ |
| 644 | int Fontcxx . . $ Font#::getStringWidthcxx . . $ Font#getStringWidth(5c4306d8553b3696). (const char *string) { |
| 645 | if (!isOkcxx . . $ Font#isOk.) return 0; |
| 646 | |
| 647 | int stringWidth = 0, tmpWidth = 0; |
| 648 | |
| 649 | // Go through each character of the string |
| 650 | for (int i = 0; string[i]; i++) { |
| 651 | // check each line individually for maximum width |
| 652 | if (string[i] == '\n') { |
| 653 | if(tmpWidth > stringWidth) stringWidth = tmpWidth; |
| 654 | tmpWidth = 0; |
| 655 | } |
| 656 | |
| 657 | unsigned int c = mapcxx . . $ Font#map.[int(string[i])]; |
| 658 | |
| 659 | // skip spaces |
| 660 | if(c == INVALID_FONT_CHARcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/INVALID_FONT_CHAR.) { |
| 661 | tmpWidth += spaceWidthcxx . . $ Font#spaceWidth.; |
| 662 | continue; |
| 663 | } |
| 664 | |
| 665 | tmpWidth += atlasRectscxx . . $ Font#atlasRects.[c].w + normalPaddingcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/normalPadding.; |
| 666 | } |
| 667 | |
| 668 | // check last or single line |
| 669 | if(tmpWidth > stringWidth) |
| 670 | return tmpWidth; |
| 671 | |
| 672 | return stringWidth; |
| 673 | } |
| 674 | |
| 675 | |
| 676 | /** |
| 677 | * Get the height of a given string. |
| 678 | * |
| 679 | * @param string The string to measure |
| 680 | * |
| 681 | * @return The height |
| 682 | */ |
| 683 | int Fontcxx . . $ Font#::getStringHeightcxx . . $ Font#getStringHeight(5c4306d8553b3696). (const char *string) { |
| 684 | if (!isOkcxx . . $ Font#isOk.) return 0; |
| 685 | |
| 686 | int stringHeight = lineHeightcxx . . $ Font#lineHeight.; // start with one |
| 687 | |
| 688 | // Go through each character of the string |
| 689 | for (int i = 0; string[i]; i++) { |
| 690 | // each line break adds to it |
| 691 | if (string[i] == '\n') stringHeight += lineHeightcxx . . $ Font#lineHeight.; |
| 692 | } |
| 693 | |
| 694 | return stringHeight; |
| 695 | } |
| 696 | |
| 697 | |
| 698 | /** |
| 699 | * Get the width of a single line of a given JJ1 cutscene string. |
| 700 | * |
| 701 | * @param string The string to measure |
| 702 | * |
| 703 | * @return The width |
| 704 | */ |
| 705 | int Fontcxx . . $ Font#::getSceneStringWidthcxx . . $ Font#getSceneStringWidth(b84ccf4e0bb14b06). (const unsigned char *string) { |
| 706 | if (!isOkcxx . . $ Font#isOk.) return 0; |
| 707 | |
| 708 | int stringWidth = 0; |
| 709 | |
| 710 | // Go through each character of the string |
| 711 | for (int i = 0; string[i]; i++) { |
| 712 | |
| 713 | // use space for invalid characters |
| 714 | if (string[i] >= nCharacterscxx . . $ Font#nCharacters.) { |
| 715 | stringWidth += spaceWidthcxx . . $ Font#spaceWidth.; |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | stringWidth += atlasRectscxx . . $ Font#atlasRects.[string[i]].w + sceneStringPaddingcxx . . $ `$anonymous_namespace_src/io/gfx/font.cpp`/sceneStringPadding.; |
| 720 | } |
| 721 | |
| 722 | return stringWidth; |
| 723 | } |
| 724 | |
| 725 | #ifdef DEBUG_FONTS |
| 726 | |
| 727 | /** |
| 728 | * Save the generated font atlas to a BMP image. |
| 729 | * |
| 730 | * @param fileName File to save to |
| 731 | */ |
| 732 | void Font::saveAtlasAsBMP(const char *fileName) { |
| 733 | if (!isOk) { |
| 734 | LOG_WARN("Not saving empty font atlas!"); |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | video.restoreSurfacePalette(characterAtlas); |
| 739 | SDL_SaveBMP(characterAtlas, fileName); |
| 740 | } |
| 741 | |
| 742 | #endif |