Debian Codegraph @

src/io/gfx/font.cpp

raw symbols SCIP
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"
24 #include "font.h"
25 #include "video.h"
26 #include "io/log.h"
27
28 #include <stb_rect_pack.h>
29 #include <string.h>
30
31 namespace {
32 constexpr unsigned int INVALID_FONT_CHAR = -1;
33
34 constexpr int normalPadding = 2;
35 constexpr int sceneStringPadding = 1;
36 }
37
38 void Font::commonSetup() {
39 isOk = false;
40 lineHeight = 0;
41 spaceWidth = 0;
42 nCharacters = MAX_FONT_CHARS;
43 memset(atlasRects, 0, sizeof(atlasRects));
44 memset(map, INVALID_FONT_CHAR, sizeof(map));
45 }
46
47 void Font::cleanMapping() {
48 // remove empty
49 for (int i = 0; i < MAX_FONT_CHARS; i++) {
50 if (map[i] >= static_cast<unsigned int>(nCharacters))
51 map[i] = 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 Font::Font (const char* fileName) {
61 commonSetup();
62
63 // Load font from a font file
64 File* file = new File(fileName, PATH_TYPE_GAME);
65 int fileSize = file->getSize();
66
67 // Checking font file
68 char *identifier1 = file->loadString(18);
69 char identifier2 = file->loadChar();
70 if (strncmp(identifier1, "Digital Dimensions", 18) != 0 || identifier2 != 0x1A) {
71 LOG_ERROR("Font not valid!");
72 delete[] identifier1;
73 return;
74 }
75 delete[] identifier1;
76
77 spaceWidth = file->loadChar();
78 lineHeight = file->loadChar();
79 LOG_MAX("spaceWidth: %d, lineHeight: %d", spaceWidth, lineHeight);
80
81 // temporary character data
82 SDL_Surface *chars[MAX_FONT_CHARS];
83 stbrp_rect rects[MAX_FONT_CHARS];
84
85 // Load characters
86 for (int i = 0; i < MAX_FONT_CHARS; i++) {
87 if (file->tell() >= fileSize) {
88 nCharacters = i;
89 LOG_TRACE("Loaded %d characters from font.", nCharacters);
90
91 break;
92 }
93
94 int size = file->loadShort();
95 int w = 0;
96 int h = 0;
97
98 if (size > 4) {
99 unsigned char* pixels = file->loadRLE(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] = video.createSurface(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] = { i, w, h, 0, 0, 0 };
116
117 // save size
118 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 characterAtlas = video.createSurface(nullptr, aW, aH);
127 video.enableColorKey(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, nCharacters);
134 if(res) {
135 for (int i = 0; i < nCharacters; i++) {
136 // save position
137 atlasRects[i].x = rects[i].x;
138 atlasRects[i].y = rects[i].y;
139
140 // copy char to atlas
141 if (rects[i].w > 0 && rects[i].h > 0)
142 SDL_BlitSurface(chars[i], nullptr, characterAtlas, &atlasRects[i]);
143 }
144 } else
145 LOG_WARN("Could not pack font atlas!");
146
147 // Delete single char surfaces
148 for (int i = 0; i < nCharacters; i++) {
149 if(rects[i].w > 0 && rects[i].h > 0)
150 video.destroySurface(chars[i]);
151 }
152
153 // Create ASCII->font map
154 map[33] = 107; // !
155 map[34] = 116; // "
156 map[36] = 63; // $
157 map[39] = 115; // '
158 map[40] = 111; // (
159 map[41] = 112; // )
160 map[43] = 105; // +
161 map[44] = 101; // ,
162 map[45] = 104; // -
163 map[46] = 102; // .
164 map[47] = 108; // /
165 for (int i = 48; i < 58; i++)
166 map[i] = i + 5; // Numbers
167 map[58] = 114; // :
168 map[59] = 113; // ;
169 map[61] = 106; // =
170 map[63] = 103; // ?
171 for (int i = 65; i < 91; i++)
172 map[i] = i - 38; // Upper-case letters
173 for (int i = 97; i < 123; i++)
174 map[i] = i - 96; // Lower-case letters
175
176 cleanMapping();
177
178 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 Font::Font (unsigned char* pixels, bool big) {
189 commonSetup();
190
191 int charWidth = 8;
192 spaceWidth = 5;
193 if (big) {
194 nCharacters = 40;
195 lineHeight = 8;
196 } else {
197 nCharacters = 13;
198 lineHeight = 7;
199 }
200
201 // temporary character data
202 SDL_Surface *chars[nCharacters];
203 stbrp_rect rects[MAX_FONT_CHARS];
204
205 unsigned char* chrPixels = new unsigned char[charWidth * lineHeight];
206
207 for (int i = 0; i < nCharacters; i++) {
208 // copy to atlas
209 for (int y = 0; y < lineHeight; y++) {
210 memcpy(chrPixels + (y * charWidth),
211 pixels + (i * charWidth) + (y * SW), charWidth);
212 }
213 chars[i] = video.createSurface(chrPixels, charWidth, lineHeight);
214
215 // setup sizes for packing
216 rects[i] = { i, charWidth, lineHeight, 0, 0, 0 };
217
218 // save size
219 atlasRects[i] = { -1, -1, charWidth, 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 * lineHeight;
230 } else {
231 // 32x28
232 aW = 4 * charWidth;
233 aH = 4 * lineHeight;
234 }
235
236 characterAtlas = video.createSurface(nullptr, aW, aH);
237 if (big) video.enableColorKey(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, nCharacters);
244 if(res) {
245 for (int i = 0; i < nCharacters; i++) {
246 // save position
247 atlasRects[i].x = rects[i].x;
248 atlasRects[i].y = rects[i].y;
249
250 // copy char to atlas
251 SDL_BlitSurface(chars[i], nullptr, characterAtlas, &atlasRects[i]);
252 }
253 } else
254 LOG_WARN("Could not pack font atlas!");
255
256 // Delete single char surfaces
257 for (int i = 0; i < nCharacters; i++)
258 video.destroySurface(chars[i]);
259
260 // Create ASCII->font map
261 if (big) {
262 // Goes " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-:."
263 map[45] = 37; // -
264 map[46] = 39; // .
265 map[58] = 38; // :
266 for (int i = 47; i < 58; i++)
267 map[i] = i - 47; // Numbers
268 for (int i = 64; i < 91; i++) {
269 map[i] = i - 54; // Upper-case letters
270 map[i+32] = 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 map[i] = i - 47; // Numbers and :;
277 }
278
279 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 Font::Font (bool bonus) {
289 commonSetup();
290
291 // Load font from FONTS.000 or BONUS.000
292 File* file = new File(bonus? "BONUS.000": "FONTS.000", PATH_TYPE_GAME);
293
294 int fileSize = file->getSize();
295 nCharacters = file->loadShort(256);
296
297 if (bonus) {
298 int nSprites = file->loadShort();
299 nCharacters -= nSprites;
300
301 // Skip sprites
302 for (int i = 0; i < nSprites; i++) {
303 file->seek(4, false);
304
305 int width = file->loadShort();
306 if (width == 0xFFFF) width = 0;
307
308 file->seek((width << 2) + file->loadShort(), false);
309 }
310 }
311
312 // temporary character data
313 SDL_Surface *chars[MAX_FONT_CHARS];
314 stbrp_rect rects[MAX_FONT_CHARS];
315
316 // Load characters
317 for (int i = 0; i < nCharacters; i++) {
318 if (file->tell() >= fileSize) {
319 nCharacters = i;
320 LOG_TRACE("Loaded %d characters from font.", nCharacters);
321
322 break;
323 }
324
325 int width = file->loadShort(SW);
326 int height = file->loadShort(SH);
327
328 // adjust
329 if (bonus) width = (width + 3) & ~3;
330 else width <<= 2;
331
332 file->seek(4, false);
333
334 unsigned char *pixels = file->loadPixels(width * height);
335
336 chars[i] = video.createSurface(pixels, width, height);
337
338 delete[] pixels;
339
340 // setup sizes for packing
341 rects[i] = { i, width, height, 0, 0, 0 };
342
343 // save size
344 atlasRects[i] = { -1, -1, width, height };
345 }
346
347 delete file;
348
349 spaceWidth = 5;
350 // use "A" as reference
351 lineHeight = atlasRects[0].h;
352 LOG_MAX("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 characterAtlas = video.createSurface(nullptr, aW, aH);
358 video.enableColorKey(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, nCharacters);
365 if(res) {
366 for (int i = 0; i < nCharacters; i++) {
367 // save position
368 atlasRects[i].x = rects[i].x;
369 atlasRects[i].y = rects[i].y;
370
371 // copy char to atlas
372 SDL_BlitSurface(chars[i], nullptr, characterAtlas, &atlasRects[i]);
373 }
374 } else
375 LOG_WARN("Could not pack font atlas!");
376
377 // Delete single char surfaces
378 for (int i = 0; i < nCharacters; i++)
379 video.destroySurface(chars[i]);
380
381 // Create ASCII->font map
382 if (bonus) {
383 map[42] = 37; // *
384 map[46] = 39; // .
385 map[47] = 38; // /
386 map[58] = 36; // :
387 } else {
388 map[37] = 36; // %
389 }
390 for (int i = 48; i < 58; i++)
391 map[i] = i - 22; // Numbers
392 for (int i = 65; i < 91; i++) {
393 map[i] = i - 65; // Upper-case letters
394 map[i+32] = map[i]; // Lower-case letters (copy)
395 }
396
397 cleanMapping();
398
399 isOk = res;
400 }
401
402
403 /**
404 * Delete the font.
405 */
406 Font::~Font () {
407 video.destroySurface(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 Point Font::showString (const char* string, int x, int y,
423 alignX xAlign, alignY yAlign) {
424
425 if (!isOk) return Point(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 alignX::Left:
432 xOffset = x;
433 break;
434
435 case alignX::Center:
436 xOffset = x - (getStringWidth(string) >> 1);
437 break;
438
439 case alignX::Right:
440 xOffset = x - getStringWidth(string);
441 break;
442 }
443 xOffsetBase = xOffset;
444
445 switch(yAlign) {
446 default:
447 case alignY::Top:
448 yOffset = y;
449 break;
450
451 case alignY::Center:
452 yOffset = y - (getStringHeight(string) >> 1);
453 break;
454
455 case alignY::Bottom:
456 yOffset = y - getStringHeight(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 += lineHeight;
466 } else {
467 unsigned int c = map[int(string[i])];
468
469 // skip spaces and invalid
470 if(c == INVALID_FONT_CHAR) {
471 xOffset += 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_BlitSurface(characterAtlas, &atlasRects[c], canvas, &dst);
484
485 xOffset += atlasRects[c].w + normalPadding;
486 }
487 }
488
489 return Point(xOffset, yOffset);
490 }
491
492 Point Font::showStringCentered (const char *s) {
493 return showString(s, (canvasW >> 1), (canvasH >> 1),
494 alignX::Center, 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 Font::showSceneString (const unsigned char* string, int x, int y) {
508 if (!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] >= nCharacters) {
521 offset += spaceWidth;
522 continue;
523 }
524
525 int c = string[i];
526
527 // Draw the character to the screen
528 SDL_BlitSurface(characterAtlas, &atlasRects[c], canvas, &dst);
529
530 offset += atlasRects[c].w + 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 Font::showNumber (int n, int x, int y) {
547 if (!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 = map[int('0')];
555
556 // Determine 0's position on the screen
557 dst.y = y;
558 dst.x = x - atlasRects[c].w;
559
560 // Draw 0 to the screen
561 SDL_BlitSurface(characterAtlas, &atlasRects[c], 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 = map[int('0' + (count % 10))];
576
577 offset -= 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_BlitSurface(characterAtlas, &atlasRects[c], canvas, &dst);
585
586 count /= 10;
587 }
588
589 // If needed, draw the negative sign
590 if (n < 0) {
591 unsigned int c = map[int('-')];
592
593 // Determine the negative sign's position on the screen
594 dst.y = y;
595 dst.x = offset - atlasRects[c].w;
596
597 // Draw the negative sign to the screen
598 SDL_BlitSurface(characterAtlas, &atlasRects[c], 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 Font::mapPalette (int start, int length, int newStart, int newLength) {
612 if (!isOk) return;
613
614 SDL_Color palette[MAX_PALETTE_COLORS];
615
616 for (int i = 0; i < length; i++) {
617 palette[i].r = palette[i].g = palette[i].b = (i * newLength / length) + newStart;
618 #if OJ_SDL3 || OJ_SDL3
619 palette[i].a = 0xFF;
620 #endif
621 }
622
623 video.setSurfacePalette(characterAtlas, palette, start, length);
624 }
625
626
627 /**
628 * Restore a palette to its original state.
629 */
630 void Font::restorePalette () {
631 if (!isOk) return;
632
633 video.restoreSurfacePalette(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 Font::getStringWidth (const char *string) {
645 if (!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 = map[int(string[i])];
658
659 // skip spaces
660 if(c == INVALID_FONT_CHAR) {
661 tmpWidth += spaceWidth;
662 continue;
663 }
664
665 tmpWidth += atlasRects[c].w + 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 Font::getStringHeight (const char *string) {
684 if (!isOk) return 0;
685
686 int stringHeight = 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 += 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 Font::getSceneStringWidth (const unsigned char *string) {
706 if (!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] >= nCharacters) {
715 stringWidth += spaceWidth;
716 continue;
717 }
718
719 stringWidth += atlasRects[string[i]].w + 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