| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Graphical routines for drawing fonts. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <bit> |
| 28 |
#include <memory> |
| 29 |
#include <stdexcept> |
| 30 |
#include <stdarg.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#ifndef macintosh |
| 35 |
#include <fcntl.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "ui.h" |
| 39 |
#include "editor/editor.h" |
| 40 |
#include "u_mem.h" |
| 41 |
#include "gr.h" |
| 42 |
#include "dxxerror.h" |
| 43 |
#include "common/2d/bitmap.h" |
| 44 |
#include "physfsx.h" |
| 45 |
#include "gamefont.h" |
| 46 |
#include "byteutil.h" |
| 47 |
#include "console.h" |
| 48 |
#include "config.h" |
| 49 |
#if DXX_USE_OGL |
| 50 |
#include "ogl_init.h" |
| 51 |
#endif |
| 52 |
|
| 53 |
#include "compiler-range_for.h" |
| 54 |
#include "partial_range.h" |
| 55 |
#include "d_range.h" |
| 56 |
#include <array> |
| 57 |
#include <memory> |
| 58 |
|
| 59 |
namespace dcx { |
| 60 |
|
| 61 |
namespace { |
| 62 |
|
| 63 |
struct grs_disk_font |
| 64 |
{ |
| 65 |
const uint16_t ft_w; // Width in pixels |
| 66 |
const uint16_t ft_h; // Height in pixels |
| 67 |
const int16_t ft_flags; // Proportional? |
| 68 |
const int16_t ft_baseline; // |
| 69 |
const uint8_t ft_minchar; // First char defined by this font |
| 70 |
const uint8_t ft_maxchar; // Last char defined by this font |
| 71 |
const uint32_t ft_data; // Ptr to raw data. |
| 72 |
const uint32_t ft_widths; // Array of widths (required for prop font) |
| 73 |
const uint32_t ft_kerndata; // Array of kerning triplet data |
| 74 |
}; |
| 75 |
|
| 76 |
static font_x_scale_float FONTSCALE_X() |
| 77 |
{ |
| 78 |
return font_x_scale_float(FNTScaleX.operator float()); |
| 79 |
} |
| 80 |
|
| 81 |
static auto FONTSCALE_Y(const int &y) |
| 82 |
{ |
| 83 |
return font_y_scaled_float(FNTScaleY * y); |
| 84 |
} |
| 85 |
|
| 86 |
constexpr std::integral_constant<uint8_t, 255> kerndata_terminator{}; |
| 87 |
|
| 88 |
#define BITS_TO_BYTES(x) (((x)+7)>>3) |
| 89 |
|
| 90 |
static int gr_internal_string_clipped(grs_canvas &, const grs_font &cv_font, int x, int y, const char *s); |
| 91 |
static int gr_internal_string_clipped_m(grs_canvas &, const grs_font &cv_font, int x, int y, const char *s); |
| 92 |
|
| 93 |
static const uint8_t *find_kern_entry(const grs_font &font, const uint8_t first, const uint8_t second) |
| 94 |
{ |
| 95 |
auto p{font.ft_kerndata}; |
| 96 |
|
| 97 |
while (*p != kerndata_terminator) |
| 98 |
if (p[0]==first && p[1]==second) |
| 99 |
return p; |
| 100 |
else p+=3; |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
|
| 104 |
//takes the character AFTER being offset into font |
| 105 |
|
| 106 |
class font_character_extent |
| 107 |
{ |
| 108 |
const unsigned r; |
| 109 |
public: |
| 110 |
font_character_extent(const grs_font &cv_font) : |
| 111 |
r(cv_font.ft_maxchar - cv_font.ft_minchar) |
| 112 |
{ |
| 113 |
} |
| 114 |
bool operator()(const unsigned c) const |
| 115 |
{ |
| 116 |
return c <= r; |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
template <typename T> |
| 121 |
struct get_char_width_result |
| 122 |
{ |
| 123 |
T width, spacing; |
| 124 |
}; |
| 125 |
|
| 126 |
/* Floating form never uses width. This specialization allows the |
| 127 |
* compiler to recognize width as dead, shortening |
| 128 |
* get_char_width<float>. |
| 129 |
*/ |
| 130 |
template <> |
| 131 |
struct get_char_width_result<float> |
| 132 |
{ |
| 133 |
float spacing; |
| 134 |
get_char_width_result(float, float s) : |
| 135 |
spacing(s) |
| 136 |
{ |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
//takes the character BEFORE being offset into current font |
| 141 |
template <typename T> |
| 142 |
static get_char_width_result<T> get_char_width(const grs_font &cv_font, const uint8_t c, const uint8_t c2) |
| 143 |
{ |
| 144 |
const unsigned letter = c - cv_font.ft_minchar; |
| 145 |
const auto ft_flags{cv_font.ft_flags}; |
| 146 |
const auto proportional{ft_flags & FT_PROPORTIONAL}; |
| 147 |
|
| 148 |
const auto fontscale_x{FONTSCALE_X()}; |
| 149 |
const font_character_extent INFONT{cv_font}; |
| 150 |
if (!INFONT(letter)) { //not in font, draw as space |
| 151 |
return {0, static_cast<T>(proportional ? fontscale_x(cv_font.ft_w) / 2 : cv_font.ft_w)}; |
| 152 |
} |
| 153 |
const T width = proportional ? fontscale_x(cv_font.ft_widths[letter]).operator float() : cv_font.ft_w; |
| 154 |
if (ft_flags & FT_KERNED) |
| 155 |
{ |
| 156 |
if (!(c2==0 || c2=='\n')) { |
| 157 |
const unsigned letter2 = c2 - cv_font.ft_minchar; |
| 158 |
|
| 159 |
if (INFONT(letter2)) { |
| 160 |
const auto p{find_kern_entry(cv_font, letter, letter2)}; |
| 161 |
if (p) |
| 162 |
return {width, static_cast<T>(fontscale_x(p[2]))}; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
return {width, width}; |
| 167 |
} |
| 168 |
|
| 169 |
static int get_centered_x(const grs_canvas &canvas, const grs_font &cv_font, const char *s) |
| 170 |
{ |
| 171 |
float w{0.f}; |
| 172 |
for (; const char c{*s}; ++s) |
| 173 |
{ |
| 174 |
if (c == '\n') |
| 175 |
break; |
| 176 |
if (c <= 0x06) |
| 177 |
{ |
| 178 |
if (c <= 0x03) |
| 179 |
s++; |
| 180 |
continue;//skip color codes. |
| 181 |
} |
| 182 |
w += get_char_width<float>(cv_font, c, s[1]).spacing; |
| 183 |
} |
| 184 |
|
| 185 |
return (canvas.cv_bitmap.bm_w - w) / 2; |
| 186 |
} |
| 187 |
|
| 188 |
//hack to allow color codes to be embedded in strings -MPM |
| 189 |
//note we subtract one from color, since 255 is "transparent" so it'll never be used, and 0 would otherwise end the string. |
| 190 |
//function must already have orig_color var set (or they could be passed as args...) |
| 191 |
//perhaps some sort of recursive orig_color type thing would be better, but that would be way too much trouble for little gain |
| 192 |
constexpr std::integral_constant<int, 1> gr_message_color_level{}; |
| 193 |
|
| 194 |
struct per_character_row_state |
| 195 |
{ |
| 196 |
uint8_t draw_full_width_as_fg_color{}; |
| 197 |
}; |
| 198 |
|
| 199 |
#define CHECK_EMBEDDED_COLORS() if (const char control_code{*text_ptr}; control_code >= 0x01 && control_code <= 0x02) { \ |
| 200 |
text_ptr++; \ |
| 201 |
if (*text_ptr){ \ |
| 202 |
if (gr_message_color_level >= control_code) \ |
| 203 |
canvas.cv_font_fg_color = *text_ptr; \ |
| 204 |
text_ptr++; \ |
| 205 |
} \ |
| 206 |
} \ |
| 207 |
else if (control_code == 0x03) \ |
| 208 |
{ \ |
| 209 |
state.draw_full_width_as_fg_color = 1; \ |
| 210 |
text_ptr++; \ |
| 211 |
} \ |
| 212 |
else if (control_code >= 0x04 && control_code <= 0x06) { \ |
| 213 |
if (gr_message_color_level >= control_code - 3) \ |
| 214 |
canvas.cv_font_fg_color= orig_color; \ |
| 215 |
text_ptr++; \ |
| 216 |
} |
| 217 |
|
| 218 |
template <bool masked_draws_background> |
| 219 |
static int gr_internal_string0_template(grs_canvas &canvas, const grs_font &cv_font, const int x, int y, const char *const s) |
| 220 |
{ |
| 221 |
const font_character_extent INFONT{cv_font}; |
| 222 |
const auto ft_flags{cv_font.ft_flags}; |
| 223 |
const auto proportional{ft_flags & FT_PROPORTIONAL}; |
| 224 |
const auto cv_font_bg_color{canvas.cv_font_bg_color}; |
| 225 |
int skip_lines{}; |
| 226 |
|
| 227 |
//to allow easy reseting to default string color with colored strings -MPM |
| 228 |
const auto orig_color{canvas.cv_font_fg_color}; |
| 229 |
unsigned VideoOffset1 = y * canvas.cv_bitmap.bm_rowsize + x; |
| 230 |
for (auto next_row{s}; next_row;) |
| 231 |
{ |
| 232 |
const auto text_ptr1{std::exchange(next_row, nullptr)}; |
| 233 |
|
| 234 |
if (x==0x8000) { //centered |
| 235 |
VideoOffset1 = y * canvas.cv_bitmap.bm_rowsize + get_centered_x(canvas, cv_font, text_ptr1); |
| 236 |
} |
| 237 |
|
| 238 |
for (int r{}; r < cv_font.ft_h; ++r) |
| 239 |
{ |
| 240 |
auto text_ptr{text_ptr1}; |
| 241 |
unsigned VideoOffset{VideoOffset1}; |
| 242 |
|
| 243 |
for (; const uint8_t c0 = *text_ptr; ++text_ptr) |
| 244 |
{ |
| 245 |
if (c0 == '\n') |
| 246 |
{ |
| 247 |
next_row = &text_ptr[1]; |
| 248 |
break; |
| 249 |
} |
| 250 |
|
| 251 |
if (c0 == CC_COLOR) |
| 252 |
{ |
| 253 |
canvas.cv_font_fg_color = static_cast<uint8_t>(*++text_ptr); |
| 254 |
continue; |
| 255 |
} |
| 256 |
|
| 257 |
if (c0 == CC_LSPACING) |
| 258 |
{ |
| 259 |
skip_lines = *++text_ptr - '0'; |
| 260 |
continue; |
| 261 |
} |
| 262 |
|
| 263 |
per_character_row_state state{ |
| 264 |
(unlikely(c0 == CC_UNDERLINE) |
| 265 |
? ++text_ptr, r == cv_font.ft_baseline + 2 || r == cv_font.ft_baseline + 3 |
| 266 |
: false), |
| 267 |
}; |
| 268 |
|
| 269 |
const uint8_t c = *text_ptr; |
| 270 |
const auto [width, spacing]{get_char_width<int>(cv_font, c, text_ptr[1])}; |
| 271 |
|
| 272 |
const unsigned letter = c - cv_font.ft_minchar; |
| 273 |
|
| 274 |
if constexpr (masked_draws_background) |
| 275 |
{ |
| 276 |
(void)orig_color; |
| 277 |
if (!INFONT(letter)) { //not in font, draw as space |
| 278 |
VideoOffset += spacing; |
| 279 |
text_ptr++; |
| 280 |
continue; |
| 281 |
} |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
if (!INFONT(letter) || c <= 0x06) //not in font, draw as space |
| 286 |
{ |
| 287 |
CHECK_EMBEDDED_COLORS() else{ |
| 288 |
VideoOffset += spacing; |
| 289 |
text_ptr++; |
| 290 |
} |
| 291 |
continue; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if (width) |
| 296 |
{ |
| 297 |
auto data{&canvas.cv_bitmap.get_bitmap_data()[VideoOffset]}; |
| 298 |
const auto cv_font_fg_color{canvas.cv_font_fg_color}; |
| 299 |
if (state.draw_full_width_as_fg_color) |
| 300 |
{ |
| 301 |
std::fill_n(data, width, cv_font_fg_color); |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
auto fp{(proportional ? cv_font.ft_chars[letter] : &cv_font.ft_data[letter * BITS_TO_BYTES(width) * cv_font.ft_h]) + (BITS_TO_BYTES(width) * r)}; |
| 306 |
|
| 307 |
/* Setting bits=0 is a dead store, but is necessary to |
| 308 |
* prevent -Og -Wuninitialized from issuing a bogus |
| 309 |
* warning. -Og does not see that bits_remaining=0 |
| 310 |
* guarantees that bits will be initialized from *fp before |
| 311 |
* it is read. |
| 312 |
*/ |
| 313 |
uint8_t bits_remaining{}, bits{}; |
| 314 |
for (uint_fast32_t i = width; i--; ++data, --bits_remaining) |
| 315 |
{ |
| 316 |
if (!bits_remaining) |
| 317 |
{ |
| 318 |
bits = *fp++; |
| 319 |
bits_remaining = 8; |
| 320 |
} |
| 321 |
|
| 322 |
const auto bit_enabled{(bits & 0x80)}; |
| 323 |
bits <<= 1; |
| 324 |
if constexpr (!masked_draws_background) |
| 325 |
{ |
| 326 |
if (!bit_enabled) |
| 327 |
continue; |
| 328 |
} |
| 329 |
*data = bit_enabled ? cv_font_fg_color : cv_font_bg_color; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
VideoOffset += spacing; |
| 334 |
} |
| 335 |
VideoOffset1 += canvas.cv_bitmap.bm_rowsize; |
| 336 |
y++; |
| 337 |
} |
| 338 |
y += skip_lines; |
| 339 |
VideoOffset1 += canvas.cv_bitmap.bm_rowsize * skip_lines; |
| 340 |
skip_lines = 0; |
| 341 |
} |
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
static int gr_internal_string0(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s) |
| 346 |
{ |
| 347 |
return gr_internal_string0_template<true>(canvas, cv_font, x, y, s); |
| 348 |
} |
| 349 |
|
| 350 |
static int gr_internal_string0m(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s) |
| 351 |
{ |
| 352 |
return gr_internal_string0_template<false>(canvas, cv_font, x, y, s); |
| 353 |
} |
| 354 |
|
| 355 |
#if !DXX_USE_OGL |
| 356 |
static void gr_internal_color_string(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s) |
| 357 |
{ |
| 358 |
//a bitmap for the character |
| 359 |
grs_bitmap char_bm{}; |
| 360 |
char_bm.set_type(bm_mode::linear); |
| 361 |
char_bm.set_flags(BM_FLAG_TRANSPARENT); |
| 362 |
|
| 363 |
const font_character_extent INFONT{cv_font}; |
| 364 |
char_bm.bm_h = cv_font.ft_h; //set height for chars of this font |
| 365 |
|
| 366 |
auto next_row{s}; |
| 367 |
auto yy{y}; |
| 368 |
|
| 369 |
const auto &&fspacy1{FSPACY(1)}; |
| 370 |
while (next_row != nullptr) |
| 371 |
{ |
| 372 |
auto text_ptr1{next_row}; |
| 373 |
next_row = nullptr; |
| 374 |
auto text_ptr{text_ptr1}; |
| 375 |
|
| 376 |
auto xx{x}; |
| 377 |
|
| 378 |
if (xx==0x8000) //centered |
| 379 |
xx = get_centered_x(canvas, cv_font, text_ptr); |
| 380 |
|
| 381 |
while (*text_ptr) |
| 382 |
{ |
| 383 |
if (*text_ptr == '\n' ) |
| 384 |
{ |
| 385 |
next_row = &text_ptr[1]; |
| 386 |
yy += cv_font.ft_h + fspacy1; |
| 387 |
break; |
| 388 |
} |
| 389 |
|
| 390 |
const int letter{static_cast<uint8_t>(*text_ptr) - cv_font.ft_minchar}; |
| 391 |
const auto [width, spacing]{get_char_width<int>(cv_font, text_ptr[0], text_ptr[1])}; |
| 392 |
|
| 393 |
if (!INFONT(letter)) { //not in font, draw as space |
| 394 |
xx += spacing; |
| 395 |
text_ptr++; |
| 396 |
continue; |
| 397 |
} |
| 398 |
|
| 399 |
const auto fp{(cv_font.ft_flags & FT_PROPORTIONAL) |
| 400 |
? cv_font.ft_chars[letter] |
| 401 |
: &cv_font.ft_data[letter * BITS_TO_BYTES(width) * cv_font.ft_h]}; |
| 402 |
|
| 403 |
/* Cast away const-ness on the font pointer. The function |
| 404 |
* must take the parameter as non-const because some callers |
| 405 |
* provide an uninitialized pointer and fill it later. |
| 406 |
*/ |
| 407 |
gr_init_bitmap(char_bm, bm_mode::linear, 0, 0, width, cv_font.ft_h, width, const_cast<color_palette_index *>(fp)); |
| 408 |
gr_bitmapm(canvas, xx, yy, char_bm); |
| 409 |
|
| 410 |
xx += spacing; |
| 411 |
|
| 412 |
text_ptr++; |
| 413 |
} |
| 414 |
|
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
#else //OGL |
| 419 |
|
| 420 |
static unsigned get_font_total_width(const grs_font &font) |
| 421 |
{ |
| 422 |
if (font.ft_flags & FT_PROPORTIONAL) |
| 423 |
{ |
| 424 |
unsigned w{}; |
| 425 |
range_for (const auto v, unchecked_partial_range(font.ft_widths, static_cast<unsigned>(font.ft_maxchar - font.ft_minchar) + 1)) |
| 426 |
w += v; |
| 427 |
return w; |
| 428 |
}else{ |
| 429 |
return {unsigned{font.ft_w} * (font.ft_maxchar - font.ft_minchar + 1u)}; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
static std::pair<unsigned, unsigned> ogl_font_choose_size(const grs_font *const font, const uint8_t gap) |
| 434 |
{ |
| 435 |
const auto nchars{font->ft_maxchar - font->ft_minchar + 1u}; |
| 436 |
int nc{}, smallest{999999}; |
| 437 |
int smallprop{10000}; |
| 438 |
std::optional<std::pair<unsigned, unsigned>> rwh; |
| 439 |
for (unsigned h{32}; h <= 256; h *= 2) |
| 440 |
{ |
| 441 |
if (font->ft_h>h)continue; |
| 442 |
const auto r{h / (font->ft_h + gap)}; |
| 443 |
auto w{std::bit_ceil((get_font_total_width(*font) + (nchars - r) * gap) / r)}; |
| 444 |
int tries{}; |
| 445 |
do { |
| 446 |
if (tries) |
| 447 |
w = std::bit_ceil(w + 1u); |
| 448 |
if(tries>3){ |
| 449 |
break; |
| 450 |
} |
| 451 |
nc=0; |
| 452 |
int y{}; |
| 453 |
while(y+font->ft_h<=h){ |
| 454 |
int x{}; |
| 455 |
while (x<w){ |
| 456 |
if (nc==nchars) |
| 457 |
break; |
| 458 |
if (font->ft_flags & FT_PROPORTIONAL){ |
| 459 |
if (x+font->ft_widths[nc]+gap>w)break; |
| 460 |
x+=font->ft_widths[nc++]+gap; |
| 461 |
}else{ |
| 462 |
if (x+font->ft_w+gap>w)break; |
| 463 |
x+=font->ft_w+gap; |
| 464 |
nc++; |
| 465 |
} |
| 466 |
} |
| 467 |
if (nc==nchars) |
| 468 |
break; |
| 469 |
y+=font->ft_h+gap; |
| 470 |
} |
| 471 |
|
| 472 |
tries++; |
| 473 |
}while(nc!=nchars); |
| 474 |
if (nc!=nchars) |
| 475 |
continue; |
| 476 |
|
| 477 |
const auto whproduct{w * h}; |
| 478 |
if (whproduct == smallest) //this gives squarer sizes priority (ie, 128x128 would be better than 512*32) |
| 479 |
{ |
| 480 |
if (w>=h){ |
| 481 |
if (w/h<smallprop){ |
| 482 |
smallprop=w/h; |
| 483 |
smallest++;//hack |
| 484 |
} |
| 485 |
}else{ |
| 486 |
if (h/w<smallprop){ |
| 487 |
smallprop=h/w; |
| 488 |
smallest++;//hack |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
if (whproduct < smallest) |
| 493 |
{ |
| 494 |
smallest = whproduct; |
| 495 |
rwh = {w, h}; |
| 496 |
} |
| 497 |
} |
| 498 |
if (!rwh) |
| 499 |
Error("Could not fit font?\n"); |
| 500 |
return *rwh; |
| 501 |
} |
| 502 |
|
| 503 |
static void ogl_init_font(grs_font *const font) |
| 504 |
{ |
| 505 |
int oglflags{OGL_FLAG_ALPHA}; |
| 506 |
const unsigned nchars = font->ft_maxchar - font->ft_minchar + 1; |
| 507 |
constexpr uint8_t gap{1}; // x/y offset between the chars so we can filter |
| 508 |
|
| 509 |
const auto [tw, th]{ogl_font_choose_size(font, gap)}; |
| 510 |
{ |
| 511 |
RAIIdmem<uint8_t[]> data; |
| 512 |
const unsigned length{tw * th}; |
| 513 |
MALLOC(data, uint8_t[], length); |
| 514 |
std::fill_n(data.get(), length, TRANSPARENCY_COLOR); // map the whole data with transparency so we won't have borders if using gap |
| 515 |
gr_init_main_bitmap(font->ft_parent_bitmap, bm_mode::linear, 0, 0, tw, th, tw, std::move(data)); |
| 516 |
} |
| 517 |
gr_set_transparent(font->ft_parent_bitmap, 1); |
| 518 |
|
| 519 |
if (!(font->ft_flags & FT_COLOR)) |
| 520 |
oglflags |= OGL_FLAG_NOCOLOR; |
| 521 |
ogl_init_texture(*(font->ft_parent_bitmap.gltexture = ogl_get_free_texture()), tw, th, oglflags); // have to init the gltexture here so the subbitmaps will find it. |
| 522 |
|
| 523 |
font->ft_bitmaps = std::make_unique<grs_bitmap[]>(nchars); |
| 524 |
const auto h{font->ft_h}; |
| 525 |
|
| 526 |
uint16_t curx{}; |
| 527 |
uint16_t cury{}; |
| 528 |
for (const auto i : xrange(nchars)) |
| 529 |
{ |
| 530 |
const auto w{ |
| 531 |
(font->ft_flags & FT_PROPORTIONAL) |
| 532 |
? font->ft_widths[i] |
| 533 |
: font->ft_w |
| 534 |
}; |
| 535 |
|
| 536 |
if (std::cmp_less(w, 1u)) |
| 537 |
continue; |
| 538 |
if (std::cmp_greater(w, 256u)) |
| 539 |
continue; |
| 540 |
|
| 541 |
if (std::cmp_greater(unsigned{curx} + w + gap, tw)) |
| 542 |
{ |
| 543 |
const unsigned next_y{unsigned{cury} + h + gap}; |
| 544 |
if (!std::in_range<uint16_t>(next_y) || std::cmp_greater(next_y + h, th)) |
| 545 |
{ |
| 546 |
std::array<char, 124> buf; |
| 547 |
const auto written{std::snprintf(std::data(buf), std::size(buf), "failed to fit font: i=%u, nchars=%u, h=%hu, cury=%hu", i, nchars, h, cury)}; |
| 548 |
throw std::runtime_error(std::string{std::data(buf), written > 0 ? std::min<unsigned>(std::size(buf), written) : 0}); |
| 549 |
} |
| 550 |
cury = next_y; |
| 551 |
curx=0; |
| 552 |
} |
| 553 |
|
| 554 |
if (font->ft_flags & FT_COLOR) |
| 555 |
{ |
| 556 |
const auto fp{(font->ft_flags & FT_PROPORTIONAL) |
| 557 |
? font->ft_chars[i] |
| 558 |
: font->ft_data + i * w*h}; |
| 559 |
for (const auto y : xrange(h)) |
| 560 |
{ |
| 561 |
for (const auto x : xrange(w)) |
| 562 |
font->ft_parent_bitmap.get_bitmap_data()[curx+x+(cury+y)*tw] = fp[x+y*w]; |
| 563 |
// Let's call this a HACK: |
| 564 |
// If we filter the fonts, the sliders will be messed up as the border pixels will have an |
| 565 |
// alpha value while filtering. So the slider bitmaps will not look "connected". |
| 566 |
// To prevent this, duplicate the first/last pixel-row with a 1-pixel offset. |
| 567 |
if (gap && i >= 99 && i <= 102) |
| 568 |
{ |
| 569 |
// See which bitmaps need left/right shifts: |
| 570 |
// 99 = SLIDER_LEFT - shift RIGHT |
| 571 |
// 100 = SLIDER_RIGHT - shift LEFT |
| 572 |
// 101 = SLIDER_MIDDLE - shift LEFT+RIGHT |
| 573 |
// 102 = SLIDER_MARKER - shift RIGHT |
| 574 |
|
| 575 |
// shift left border |
| 576 |
std::size_t oi, ii; |
| 577 |
if (i != 99 && i != 102) |
| 578 |
{ |
| 579 |
oi = (curx + (cury + y) * tw) - 1; |
| 580 |
ii = y * w; |
| 581 |
} |
| 582 |
// shift right border |
| 583 |
else if (i != 100) |
| 584 |
{ |
| 585 |
oi = (curx + (w - 1) + (cury + y) * tw) + 1; |
| 586 |
ii = (w - 1) + y * w; |
| 587 |
} |
| 588 |
else |
| 589 |
continue; |
| 590 |
font->ft_parent_bitmap.get_bitmap_data()[oi] = fp[ii]; |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
auto white{gr_find_closest_color(63, 63, 63)}; |
| 597 |
auto fp{(font->ft_flags & FT_PROPORTIONAL) |
| 598 |
? font->ft_chars[i] |
| 599 |
: font->ft_data + i * BITS_TO_BYTES(w)*h}; |
| 600 |
for (const auto y : xrange(h)) |
| 601 |
{ |
| 602 |
uint8_t BitMask{}; |
| 603 |
uint8_t bits{}; |
| 604 |
for (const auto x : xrange(w)) |
| 605 |
{ |
| 606 |
if (BitMask==0) { |
| 607 |
bits = *fp++; |
| 608 |
BitMask = 0x80; |
| 609 |
} |
| 610 |
|
| 611 |
font->ft_parent_bitmap.get_bitmap_data()[curx+x+(cury+y)*tw] = (bits & BitMask) |
| 612 |
? white |
| 613 |
: 255; |
| 614 |
BitMask >>= 1; |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
gr_init_sub_bitmap(font->ft_bitmaps[i], font->ft_parent_bitmap, {curx}, {cury}, {w}, {h}); |
| 619 |
curx+=w+gap; |
| 620 |
} |
| 621 |
ogl_loadbmtexture_f(font->ft_parent_bitmap, CGameCfg.TexFilt, 0, 0); |
| 622 |
} |
| 623 |
|
| 624 |
static void ogl_internal_string(grs_canvas &canvas, const grs_font &cv_font, const int entry_x, int yy, const char *const s) |
| 625 |
{ |
| 626 |
auto orig_color{canvas.cv_font_fg_color}; //to allow easy reseting to default string color with colored strings -MPM |
| 627 |
|
| 628 |
if (grd_curscreen->sc_canvas.cv_bitmap.get_type() != bm_mode::ogl) |
| 629 |
Error("carp.\n"); |
| 630 |
const auto &&fspacy1{FSPACY(1)}; |
| 631 |
const font_character_extent INFONT{cv_font}; |
| 632 |
const auto &&fontscale_x{FONTSCALE_X()}; |
| 633 |
const auto &&FONTSCALE_Y_ft_h{FONTSCALE_Y(cv_font.ft_h)}; |
| 634 |
ogl_colors colors; |
| 635 |
for (auto next_row{s}; next_row;) |
| 636 |
{ |
| 637 |
auto text_ptr{std::exchange(next_row, nullptr)}; |
| 638 |
auto line_x{entry_x == 0x8000 |
| 639 |
? get_centered_x(canvas, cv_font, text_ptr) |
| 640 |
: entry_x}; |
| 641 |
|
| 642 |
for (; const auto c0{*text_ptr};) |
| 643 |
{ |
| 644 |
if (c0 == '\n') |
| 645 |
{ |
| 646 |
next_row = &text_ptr[1]; |
| 647 |
yy += FONTSCALE_Y_ft_h + fspacy1; |
| 648 |
break; |
| 649 |
} |
| 650 |
|
| 651 |
const auto letter{c0 - cv_font.ft_minchar}; |
| 652 |
const auto spacing{get_char_width<int>(cv_font, c0, text_ptr[1]).spacing}; |
| 653 |
|
| 654 |
per_character_row_state state; |
| 655 |
if (!INFONT(letter) || c0 <= 0x06) //not in font, draw as space |
| 656 |
{ |
| 657 |
CHECK_EMBEDDED_COLORS() else{ |
| 658 |
line_x += spacing; |
| 659 |
text_ptr++; |
| 660 |
} |
| 661 |
if (state.draw_full_width_as_fg_color) |
| 662 |
{ |
| 663 |
const auto color{canvas.cv_font_fg_color}; |
| 664 |
gr_rect(canvas, line_x, yy + cv_font.ft_baseline + 2, line_x + cv_font.ft_w, yy + cv_font.ft_baseline + 3, color); |
| 665 |
} |
| 666 |
|
| 667 |
continue; |
| 668 |
} |
| 669 |
const auto ft_w{(cv_font.ft_flags & FT_PROPORTIONAL) |
| 670 |
? cv_font.ft_widths[letter] |
| 671 |
: cv_font.ft_w}; |
| 672 |
|
| 673 |
ogl_ubitmapm_cs(canvas, line_x, yy, fontscale_x(ft_w), FONTSCALE_Y_ft_h, cv_font.ft_bitmaps[letter], (cv_font.ft_flags & FT_COLOR) ? colors.white : (canvas.cv_bitmap.get_type() == bm_mode::ogl) ? colors.init(canvas.cv_font_fg_color) : throw std::runtime_error("non-color string to non-ogl dest")); |
| 674 |
|
| 675 |
line_x += spacing; |
| 676 |
text_ptr++; |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
#define gr_internal_color_string ogl_internal_string |
| 682 |
#endif //OGL |
| 683 |
|
| 684 |
static inline int PHYSFSX_readU8_ptr(PHYSFS_File *const file, uint8_t *const b) |
| 685 |
{ |
| 686 |
return PHYSFSX_readBytes(file, b, sizeof(*b)) == sizeof(*b); |
| 687 |
} |
| 688 |
|
| 689 |
static constexpr PHYSFSX_read_helper<uint8_t, PHYSFSX_readU8_ptr> PHYSFSX_readU8{}; |
| 690 |
|
| 691 |
static grs_disk_font grs_disk_font_read(NamedPHYSFS_File fp) |
| 692 |
{ |
| 693 |
return grs_disk_font{ |
| 694 |
.ft_w = PHYSFSX_readULE16(fp), |
| 695 |
.ft_h = PHYSFSX_readULE16(fp), |
| 696 |
.ft_flags = PHYSFSX_readSLE16(fp), |
| 697 |
.ft_baseline = PHYSFSX_readSLE16(fp), |
| 698 |
.ft_minchar = PHYSFSX_readU8(fp), |
| 699 |
.ft_maxchar = PHYSFSX_readU8(fp), |
| 700 |
.ft_data = (PHYSFSX_skipBytes<2>(fp), PHYSFSX_readULE32(fp) - GRS_FONT_SIZE), |
| 701 |
.ft_widths = (PHYSFSX_skipBytes<4>(fp), PHYSFSX_readULE32(fp) - GRS_FONT_SIZE), |
| 702 |
.ft_kerndata = PHYSFSX_readULE32(fp) - GRS_FONT_SIZE, |
| 703 |
}; |
| 704 |
} |
| 705 |
|
| 706 |
} |
| 707 |
|
| 708 |
void gr_string(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s) |
| 709 |
{ |
| 710 |
const auto &&[w, h]{gr_get_string_size(cv_font, s)}; |
| 711 |
gr_string(canvas, cv_font, x, y, s, w, h); |
| 712 |
} |
| 713 |
|
| 714 |
namespace { |
| 715 |
|
| 716 |
static void gr_ustring_mono(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s) |
| 717 |
{ |
| 718 |
switch (canvas.cv_bitmap.get_type()) |
| 719 |
{ |
| 720 |
case bm_mode::linear: |
| 721 |
if (canvas.cv_font_bg_color == TRANSPARENCY_COLOR) |
| 722 |
gr_internal_string0m(canvas, cv_font, x, y, s); |
| 723 |
else |
| 724 |
gr_internal_string0(canvas, cv_font, x, y, s); |
| 725 |
break; |
| 726 |
case bm_mode::ilbm: |
| 727 |
case bm_mode::rgb15: |
| 728 |
#if DXX_USE_OGL |
| 729 |
case bm_mode::ogl: |
| 730 |
#endif |
| 731 |
break; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
} |
| 736 |
|
| 737 |
void gr_string(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s, const int w, const int h) |
| 738 |
{ |
| 739 |
if (y + h < 0) |
| 740 |
return; |
| 741 |
const auto bm_h{canvas.cv_bitmap.bm_h}; |
| 742 |
if (y > bm_h) |
| 743 |
return; |
| 744 |
const auto bm_w{canvas.cv_bitmap.bm_w}; |
| 745 |
int xw{w}; |
| 746 |
if ( x == 0x8000 ) { |
| 747 |
// for x, since this will be centered, only look at |
| 748 |
// width. |
| 749 |
} else { |
| 750 |
if (x > bm_w) |
| 751 |
return; |
| 752 |
xw += x; |
| 753 |
if (xw < 0) |
| 754 |
return; |
| 755 |
} |
| 756 |
if ( |
| 757 |
#if DXX_USE_OGL |
| 758 |
canvas.cv_bitmap.get_type() == bm_mode::ogl || |
| 759 |
#endif |
| 760 |
cv_font.ft_flags & FT_COLOR) |
| 761 |
{ |
| 762 |
gr_internal_color_string(canvas, cv_font, x, y, s); |
| 763 |
return; |
| 764 |
} |
| 765 |
// Partially clipped... |
| 766 |
if (!(y < 0 || |
| 767 |
x < 0 || |
| 768 |
xw > bm_w || |
| 769 |
y + h > bm_h)) |
| 770 |
{ |
| 771 |
gr_ustring_mono(canvas, cv_font, x, y, s); |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
if (canvas.cv_font_bg_color == TRANSPARENCY_COLOR) |
| 776 |
{ |
| 777 |
gr_internal_string_clipped_m(canvas, cv_font, x, y, s); |
| 778 |
return; |
| 779 |
} |
| 780 |
gr_internal_string_clipped(canvas, cv_font, x, y, s); |
| 781 |
} |
| 782 |
|
| 783 |
void gr_ustring(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s) |
| 784 |
{ |
| 785 |
#if DXX_USE_OGL |
| 786 |
if (canvas.cv_bitmap.get_type() == bm_mode::ogl) |
| 787 |
{ |
| 788 |
ogl_internal_string(canvas, cv_font, x, y, s); |
| 789 |
return; |
| 790 |
} |
| 791 |
#endif |
| 792 |
|
| 793 |
if (cv_font.ft_flags & FT_COLOR) |
| 794 |
gr_internal_color_string(canvas, cv_font, x, y, s); |
| 795 |
else |
| 796 |
gr_ustring_mono(canvas, cv_font, x, y, s); |
| 797 |
} |
| 798 |
|
| 799 |
unsigned gr_get_string_height(const grs_font &cv_font, const unsigned lines) |
| 800 |
{ |
| 801 |
const auto fontscale_y{FONTSCALE_Y(cv_font.ft_h)}; |
| 802 |
return static_cast<unsigned>(fontscale_y + (lines * (fontscale_y + FSPACY(1)))); |
| 803 |
} |
| 804 |
|
| 805 |
gr_string_size gr_get_string_size(const grs_font &cv_font, const char *s) |
| 806 |
{ |
| 807 |
return gr_get_string_size(cv_font, s, UINT_MAX); |
| 808 |
} |
| 809 |
|
| 810 |
gr_string_size gr_get_string_size(const grs_font &cv_font, const char *s, const unsigned max_chars_per_line) |
| 811 |
{ |
| 812 |
float longest_width{0.f}, string_width_f{0.f}; |
| 813 |
unsigned lines{}; |
| 814 |
if (s) |
| 815 |
{ |
| 816 |
unsigned remaining_chars_this_line{max_chars_per_line}; |
| 817 |
while (*s) |
| 818 |
{ |
| 819 |
if (*s == '\n') |
| 820 |
{ |
| 821 |
if (longest_width < string_width_f) |
| 822 |
longest_width = string_width_f; |
| 823 |
string_width_f = 0; |
| 824 |
const auto os{s}; |
| 825 |
while (*++s == '\n') |
| 826 |
{ |
| 827 |
} |
| 828 |
lines += s - os; |
| 829 |
if (!*s) |
| 830 |
break; |
| 831 |
remaining_chars_this_line = max_chars_per_line; |
| 832 |
} |
| 833 |
|
| 834 |
string_width_f += get_char_width<float>(cv_font, s[0], s[1]).spacing; |
| 835 |
s++; |
| 836 |
if (!--remaining_chars_this_line) |
| 837 |
break; |
| 838 |
} |
| 839 |
} |
| 840 |
return {static_cast<unsigned long>(std::max(longest_width, string_width_f)), gr_get_string_height(cv_font, lines)}; |
| 841 |
} |
| 842 |
|
| 843 |
std::pair<const char *, unsigned> gr_get_string_wrap(const grs_font &cv_font, const char *s, unsigned limit) |
| 844 |
{ |
| 845 |
assert(s); |
| 846 |
float string_width_f{0.f}; |
| 847 |
const float limit_f = limit; |
| 848 |
for (; const uint8_t c{*s}; ++s) |
| 849 |
{ |
| 850 |
const float next_string_width{string_width_f + get_char_width<float>(cv_font, c, s[1]).spacing}; |
| 851 |
if (next_string_width >= limit_f) |
| 852 |
break; |
| 853 |
string_width_f = next_string_width; |
| 854 |
} |
| 855 |
return {s, static_cast<unsigned>(string_width_f)}; |
| 856 |
} |
| 857 |
|
| 858 |
template <void (&F)(grs_canvas &, const grs_font &, int, int, const char *)> |
| 859 |
void (gr_printt)(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const format, ...) |
| 860 |
{ |
| 861 |
char buffer[1000]; |
| 862 |
va_list args; |
| 863 |
|
| 864 |
va_start(args, format ); |
| 865 |
vsnprintf(buffer, sizeof(buffer), format, args); |
| 866 |
va_end(args); |
| 867 |
F(canvas, cv_font, x, y, buffer); |
| 868 |
} |
| 869 |
|
| 870 |
template void gr_printt<gr_string>(grs_canvas &, const grs_font &, int, int, const char *, ...); |
| 871 |
template void gr_printt<gr_ustring>(grs_canvas &, const grs_font &, int, int, const char *, ...); |
| 872 |
|
| 873 |
//remap a font, re-reading its data & palette |
| 874 |
static void gr_remap_font(grs_font *font); |
| 875 |
|
| 876 |
//remap (by re-reading) all the color fonts |
| 877 |
void gr_remap_color_fonts() |
| 878 |
{ |
| 879 |
#if DXX_USE_EDITOR |
| 880 |
gr_remap_font(ui_small_font.get()); |
| 881 |
gr_remap_font(editor_font.get()); |
| 882 |
#endif |
| 883 |
for (auto &gf : Gamefonts) |
| 884 |
{ |
| 885 |
gr_remap_font(gf.font.get()); |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
namespace { |
| 890 |
|
| 891 |
/* |
| 892 |
* reads a grs_font structure from a PHYSFS_File |
| 893 |
*/ |
| 894 |
static void grs_font_read(grs_font *gf, NamedPHYSFS_File fp) |
| 895 |
{ |
| 896 |
const auto d{grs_disk_font_read(fp)}; |
| 897 |
gf->ft_w = {d.ft_w}; |
| 898 |
gf->ft_h = {d.ft_h}; |
| 899 |
gf->ft_flags = {d.ft_flags}; |
| 900 |
gf->ft_baseline = {d.ft_baseline}; |
| 901 |
gf->ft_minchar = {d.ft_minchar}; |
| 902 |
gf->ft_maxchar = {d.ft_maxchar}; |
| 903 |
gf->ft_data = reinterpret_cast<uint8_t *>(d.ft_data); |
| 904 |
gf->ft_widths = reinterpret_cast<uint16_t *>(d.ft_widths); |
| 905 |
gf->ft_kerndata = reinterpret_cast<uint8_t *>(d.ft_kerndata); |
| 906 |
} |
| 907 |
|
| 908 |
static std::unique_ptr<grs_font> gr_internal_init_font(const std::span<const char> fontname) |
| 909 |
{ |
| 910 |
color_palette_index *ptr; |
| 911 |
color_palette_index *ft_data; |
| 912 |
struct { |
| 913 |
std::array<char, 4> magic; |
| 914 |
unsigned datasize; //size up to (but not including) palette |
| 915 |
} file_header; |
| 916 |
|
| 917 |
auto &&[fontfile, physfserr]{PHYSFSX_openReadBuffered(fontname.data())}; |
| 918 |
if (!fontfile) { |
| 919 |
[[unlikely]]; |
| 920 |
con_printf(CON_VERBOSE, "Failed to open font file \"%s\": %s", fontname.data(), PHYSFS_getErrorByCode(physfserr)); |
| 921 |
return {}; |
| 922 |
} |
| 923 |
|
| 924 |
static_assert(sizeof(file_header) == 8, "file header size error"); |
| 925 |
if (PHYSFSX_readBytes(fontfile, &file_header, sizeof(file_header)) != sizeof(file_header) || |
| 926 |
memcmp(file_header.magic.data(), "PSFN", 4) || |
| 927 |
(file_header.datasize = INTEL_INT(file_header.datasize)) < GRS_FONT_SIZE) |
| 928 |
{ |
| 929 |
[[unlikely]]; |
| 930 |
con_printf(CON_NORMAL, "Invalid header in font file %s", fontname.data()); |
| 931 |
return {}; |
| 932 |
} |
| 933 |
|
| 934 |
file_header.datasize -= GRS_FONT_SIZE; // subtract the size of the header. |
| 935 |
const auto &datasize{file_header.datasize}; |
| 936 |
|
| 937 |
auto font{std::make_unique<grs_font>()}; |
| 938 |
grs_font_read(font.get(), fontfile); |
| 939 |
|
| 940 |
const unsigned nchars = font->ft_maxchar - font->ft_minchar + 1; |
| 941 |
auto ft_chars_storage{(font->ft_flags & FT_PROPORTIONAL) |
| 942 |
? std::size_t{sizeof(uint8_t *) * nchars} |
| 943 |
: std::size_t{}}; |
| 944 |
|
| 945 |
auto ft_allocdata{std::make_unique<color_palette_index[]>(datasize + ft_chars_storage)}; |
| 946 |
const auto font_data{&ft_allocdata[ft_chars_storage]}; |
| 947 |
if (PHYSFSX_readBytes(fontfile, font_data, datasize) != datasize) |
| 948 |
{ |
| 949 |
[[unlikely]]; |
| 950 |
con_printf(CON_URGENT, "Insufficient data in font file %s", fontname.data()); |
| 951 |
return {}; |
| 952 |
} |
| 953 |
|
| 954 |
if (font->ft_flags & FT_PROPORTIONAL) { |
| 955 |
const auto offset_widths{reinterpret_cast<uintptr_t>(font->ft_widths)}; |
| 956 |
auto w{reinterpret_cast<uint16_t *>(&font_data[offset_widths])}; |
| 957 |
if (offset_widths >= datasize || offset_widths + (nchars * sizeof(*w)) >= datasize) |
| 958 |
{ |
| 959 |
[[unlikely]]; |
| 960 |
con_printf(CON_URGENT, "Missing widths in font file %s", fontname.data()); |
| 961 |
return {}; |
| 962 |
} |
| 963 |
font->ft_widths = w; |
| 964 |
const auto offset_data{reinterpret_cast<uintptr_t>(font->ft_data)}; |
| 965 |
if (offset_data >= datasize) |
| 966 |
{ |
| 967 |
[[unlikely]]; |
| 968 |
con_printf(CON_URGENT, "Missing data in font file %s", fontname.data()); |
| 969 |
return {}; |
| 970 |
} |
| 971 |
font->ft_data = ptr = ft_data = &font_data[offset_data]; |
| 972 |
const auto ft_chars{reinterpret_cast<const uint8_t **>(ft_allocdata.get())}; |
| 973 |
font->ft_chars = reinterpret_cast<const uint8_t *const *>(ft_chars); |
| 974 |
|
| 975 |
const auto is_color{font->ft_flags & FT_COLOR}; |
| 976 |
const unsigned ft_h{font->ft_h}; |
| 977 |
std::generate_n(ft_chars, nchars, [is_color, ft_h, &w, &ptr]{ |
| 978 |
const unsigned s{INTEL_SHORT(*w)}; |
| 979 |
if constexpr (words_bigendian) |
| 980 |
*w = static_cast<uint16_t>(s); |
| 981 |
++w; |
| 982 |
const auto r{ptr}; |
| 983 |
ptr += ft_h * (is_color ? s : BITS_TO_BYTES(s)); |
| 984 |
return r; |
| 985 |
}); |
| 986 |
} else { |
| 987 |
font->ft_data = ft_data = font_data; |
| 988 |
font->ft_widths = nullptr; |
| 989 |
|
| 990 |
ptr = ft_data + (nchars * font->ft_w * font->ft_h); |
| 991 |
} |
| 992 |
|
| 993 |
if (font->ft_flags & FT_KERNED) |
| 994 |
{ |
| 995 |
const auto offset_kerndata{reinterpret_cast<uintptr_t>(font->ft_kerndata)}; |
| 996 |
if (datasize <= offset_kerndata) |
| 997 |
{ |
| 998 |
[[unlikely]]; |
| 999 |
con_printf(CON_URGENT, "Missing kerndata in font file %s", fontname.data()); |
| 1000 |
return {}; |
| 1001 |
} |
| 1002 |
const auto begin_kerndata{reinterpret_cast<const unsigned char *>(&font_data[offset_kerndata])}; |
| 1003 |
const auto end_font_data{&font_data[datasize - ((datasize - offset_kerndata + 2) % 3)]}; |
| 1004 |
for (auto cur_kerndata{begin_kerndata};; cur_kerndata += 3) |
| 1005 |
{ |
| 1006 |
if (cur_kerndata == end_font_data) |
| 1007 |
{ |
| 1008 |
[[unlikely]]; |
| 1009 |
con_printf(CON_URGENT, "Unterminated kerndata in font file %s", fontname.data()); |
| 1010 |
return {}; |
| 1011 |
} |
| 1012 |
if (*cur_kerndata == kerndata_terminator) |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
font->ft_kerndata = begin_kerndata; |
| 1016 |
} |
| 1017 |
else |
| 1018 |
font->ft_kerndata = nullptr; |
| 1019 |
|
| 1020 |
if (font->ft_flags & FT_COLOR) { //remap palette |
| 1021 |
palette_array_t palette; |
| 1022 |
/* `freq` exists so that decode_data can write to it, but it is |
| 1023 |
* otherwise unused. `decode_data` is not guaranteed to write |
| 1024 |
* to every element, but the bitset constructor will initialize |
| 1025 |
* the storage, so reading unwritten fields will always return |
| 1026 |
* false. |
| 1027 |
*/ |
| 1028 |
std::bitset<256> freq; |
| 1029 |
|
| 1030 |
if (constexpr std::size_t buffer_size{sizeof(palette[0]) * palette.size()}; PHYSFSX_readBytes(fontfile, palette, buffer_size) != buffer_size) //read the palette |
| 1031 |
{ |
| 1032 |
[[unlikely]]; |
| 1033 |
return {}; |
| 1034 |
} |
| 1035 |
|
| 1036 |
auto colormap{build_colormap_good(palette)}; |
| 1037 |
|
| 1038 |
colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR; // changed from colormap[255] = 255 to this for macintosh |
| 1039 |
|
| 1040 |
decode_data(std::span{ft_data, ptr}, colormap, freq); |
| 1041 |
} |
| 1042 |
fontfile.reset(); |
| 1043 |
//set curcanv vars |
| 1044 |
|
| 1045 |
auto &ft_filename{font->ft_filename}; |
| 1046 |
font->ft_allocdata = std::move(ft_allocdata); |
| 1047 |
std::memcpy(ft_filename.data(), fontname.data(), std::min(fontname.size(), std::size(ft_filename) - 1)); |
| 1048 |
return font; |
| 1049 |
} |
| 1050 |
|
| 1051 |
} |
| 1052 |
|
| 1053 |
grs_font_ptr gr_init_font(grs_canvas &canvas, const std::span<const char> fontname) |
| 1054 |
{ |
| 1055 |
auto font{gr_internal_init_font(fontname)}; |
| 1056 |
if (!font) |
| 1057 |
return {}; |
| 1058 |
if (!std::in_range<uint8_t>(font->ft_h)) |
| 1059 |
/* Reject fonts that are very tall. This is an arbitrary cap that |
| 1060 |
* should never be exceeded, and it allows later logic to assume that |
| 1061 |
* uint16_t can hold all results arising from `ft_h` plus a small |
| 1062 |
* constant. |
| 1063 |
*/ |
| 1064 |
return {}; |
| 1065 |
|
| 1066 |
canvas.cv_font = font.get(); |
| 1067 |
canvas.cv_font_fg_color = 0; |
| 1068 |
canvas.cv_font_bg_color = 0; |
| 1069 |
#if DXX_USE_OGL |
| 1070 |
ogl_init_font(font.get()); |
| 1071 |
#endif |
| 1072 |
return grs_font_ptr(font.release()); |
| 1073 |
} |
| 1074 |
|
| 1075 |
//remap a font by re-reading its data & palette |
| 1076 |
void gr_remap_font(grs_font *font) |
| 1077 |
{ |
| 1078 |
if (!font) |
| 1079 |
/* To simplify callers, allow the caller to pass `nullptr`, and ignore |
| 1080 |
* an attempt to remap it. |
| 1081 |
*/ |
| 1082 |
return; |
| 1083 |
if (!(font->ft_flags & FT_COLOR)) |
| 1084 |
/* If the font does not use color, then remapping it will not change |
| 1085 |
* its content. Ignore the attempt. |
| 1086 |
*/ |
| 1087 |
return; |
| 1088 |
auto n{gr_internal_init_font(font->ft_filename)}; |
| 1089 |
if (!n) |
| 1090 |
/* If the font fails to load, retain the old font, which may have |
| 1091 |
* incorrect colors in the new palette, but that is still better than |
| 1092 |
* having no font. |
| 1093 |
*/ |
| 1094 |
return; |
| 1095 |
*font = std::move(*n.get()); |
| 1096 |
#if DXX_USE_OGL |
| 1097 |
ogl_init_font(font); |
| 1098 |
#endif |
| 1099 |
} |
| 1100 |
|
| 1101 |
void gr_set_curfont(grs_canvas &canvas, const grs_font &n) |
| 1102 |
{ |
| 1103 |
canvas.cv_font = &n; |
| 1104 |
} |
| 1105 |
|
| 1106 |
namespace { |
| 1107 |
|
| 1108 |
template <bool masked_draws_background> |
| 1109 |
static int gr_internal_string_clipped_template(grs_canvas &canvas, const grs_font &cv_font, int x, int y, const char *const s) |
| 1110 |
{ |
| 1111 |
int letter; |
| 1112 |
int x1{x}; |
| 1113 |
|
| 1114 |
const font_character_extent INFONT{cv_font}; |
| 1115 |
const auto ft_flags{cv_font.ft_flags}; |
| 1116 |
const auto proportional{ft_flags & FT_PROPORTIONAL}; |
| 1117 |
const auto cv_font_bg_color{canvas.cv_font_bg_color}; |
| 1118 |
|
| 1119 |
for (auto next_row{s}; next_row;) |
| 1120 |
{ |
| 1121 |
const auto text_ptr1{next_row}; |
| 1122 |
next_row = nullptr; |
| 1123 |
|
| 1124 |
x = x1; |
| 1125 |
if (x==0x8000) //centered |
| 1126 |
x = get_centered_x(canvas, cv_font, text_ptr1); |
| 1127 |
|
| 1128 |
int last_x{x}; |
| 1129 |
|
| 1130 |
for (const auto r : xrange(cv_font.ft_h)) |
| 1131 |
{ |
| 1132 |
auto text_ptr{text_ptr1}; |
| 1133 |
x = last_x; |
| 1134 |
|
| 1135 |
for (; const uint8_t c0 = *text_ptr; ++text_ptr) |
| 1136 |
{ |
| 1137 |
if (c0 == '\n') |
| 1138 |
{ |
| 1139 |
next_row = &text_ptr[1]; |
| 1140 |
break; |
| 1141 |
} |
| 1142 |
if (c0 == CC_COLOR) |
| 1143 |
{ |
| 1144 |
canvas.cv_font_fg_color = static_cast<uint8_t>(*++text_ptr); |
| 1145 |
continue; |
| 1146 |
} |
| 1147 |
|
| 1148 |
if (c0 == CC_LSPACING) |
| 1149 |
{ |
| 1150 |
Int3(); // Warning: skip lines not supported for clipped strings. |
| 1151 |
text_ptr += 1; |
| 1152 |
continue; |
| 1153 |
} |
| 1154 |
|
| 1155 |
per_character_row_state state{ |
| 1156 |
(unlikely(c0 == CC_UNDERLINE) |
| 1157 |
? ++text_ptr, r == cv_font.ft_baseline + 2 || r == cv_font.ft_baseline + 3 |
| 1158 |
: false), |
| 1159 |
}; |
| 1160 |
const uint8_t c = *text_ptr; |
| 1161 |
const auto [width, spacing]{get_char_width<int>(cv_font, c, text_ptr[1])}; |
| 1162 |
|
| 1163 |
letter = c - cv_font.ft_minchar; |
| 1164 |
|
| 1165 |
if (!INFONT(letter)) { //not in font, draw as space |
| 1166 |
x += spacing; |
| 1167 |
continue; |
| 1168 |
} |
| 1169 |
const auto cv_font_fg_color{canvas.cv_font_fg_color}; |
| 1170 |
auto color{cv_font_fg_color}; |
| 1171 |
if (width) |
| 1172 |
{ |
| 1173 |
if (state.draw_full_width_as_fg_color) { |
| 1174 |
for (uint_fast32_t i = width; i--;) |
| 1175 |
{ |
| 1176 |
gr_pixel(canvas.cv_bitmap, x++, y, color); |
| 1177 |
} |
| 1178 |
} else { |
| 1179 |
auto fp{proportional ? cv_font.ft_chars[letter] : cv_font.ft_data + letter * BITS_TO_BYTES(width) * cv_font.ft_h}; |
| 1180 |
fp += BITS_TO_BYTES(width)*r; |
| 1181 |
|
| 1182 |
/* Setting bits=0 is a dead store, but is necessary to |
| 1183 |
* prevent -Og -Wuninitialized from issuing a bogus |
| 1184 |
* warning. -Og does not see that bits_remaining=0 |
| 1185 |
* guarantees that bits will be initialized from *fp before |
| 1186 |
* it is read. |
| 1187 |
*/ |
| 1188 |
uint8_t bits_remaining{}, bits{}; |
| 1189 |
|
| 1190 |
for (uint_fast32_t i = width; i--; ++x, --bits_remaining) |
| 1191 |
{ |
| 1192 |
if (!bits_remaining) |
| 1193 |
{ |
| 1194 |
bits = *fp++; |
| 1195 |
bits_remaining = 8; |
| 1196 |
} |
| 1197 |
const auto bit_enabled{(bits & 0x80)}; |
| 1198 |
bits <<= 1; |
| 1199 |
if constexpr (masked_draws_background) |
| 1200 |
color = bit_enabled ? cv_font_fg_color : cv_font_bg_color; |
| 1201 |
else |
| 1202 |
{ |
| 1203 |
if (!bit_enabled) |
| 1204 |
continue; |
| 1205 |
} |
| 1206 |
gr_pixel(canvas.cv_bitmap, x, y, color); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
x += spacing-width; //for kerning |
| 1211 |
} |
| 1212 |
y++; |
| 1213 |
} |
| 1214 |
} |
| 1215 |
return 0; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static int gr_internal_string_clipped_m(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s) |
| 1219 |
{ |
| 1220 |
return gr_internal_string_clipped_template<true>(canvas, cv_font, x, y, s); |
| 1221 |
} |
| 1222 |
|
| 1223 |
static int gr_internal_string_clipped(grs_canvas &canvas, const grs_font &cv_font, const int x, const int y, const char *const s) |
| 1224 |
{ |
| 1225 |
return gr_internal_string_clipped_template<false>(canvas, cv_font, x, y, s); |
| 1226 |
} |
| 1227 |
|
| 1228 |
} |
| 1229 |
|
| 1230 |
} |