| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/2dlib/font.cpp $ |
| 21 | * $Revision: 15 $ |
| 22 | * $Date: 4/17/99 6:15p $ |
| 23 | * $Author: Samir $ |
| 24 | * |
| 25 | * will load and manage a font's capabilities. |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/2dlib/font.cpp $ |
| 28 | * |
| 29 | * 15 4/17/99 6:15p Samir |
| 30 | * replaced gr.h with grdefs.h and fixed resulting compile bugs. |
| 31 | * |
| 32 | * 14 10/13/98 10:31a Sean |
| 33 | * Fixed a mem_free (free()) conflict |
| 34 | * |
| 35 | * 13 4/23/98 6:38p Jason |
| 36 | * made bitmaps use 1555 format |
| 37 | * |
| 38 | * 12 3/31/98 3:48p Jason |
| 39 | * added memory lib |
| 40 | * |
| 41 | * 11 12/19/97 12:31p Samir |
| 42 | * load font registration |
| 43 | * |
| 44 | * 10 12/12/97 6:40p Samir |
| 45 | * Some viewport font functions. |
| 46 | * |
| 47 | * 9 11/16/97 6:53p Samir |
| 48 | * Reinstated instance count for fonts. |
| 49 | * |
| 50 | * 8 11/16/97 4:41p Matt |
| 51 | * Commented-out an assert. |
| 52 | * |
| 53 | * 7 11/14/97 12:06p Samir |
| 54 | * When you use a font for the first time, load it. Never free font until |
| 55 | * game closes. |
| 56 | * |
| 57 | * 6 10/15/97 5:20p Jason |
| 58 | * did a HUGE overhaul of the bitmap system |
| 59 | * |
| 60 | * 5 9/26/97 2:53p Samir |
| 61 | * Delete old m_CharSurf if creating a new one. |
| 62 | * |
| 63 | * 4 9/19/97 5:37p Samir |
| 64 | * Software font coloring works. |
| 65 | * |
| 66 | * 3 8/07/97 3:15p Samir |
| 67 | * May have fixed horizontal left clipping problem. |
| 68 | * |
| 69 | * 22 6/25/97 10:10a Samir |
| 70 | * Removed unused code causing warning. |
| 71 | * |
| 72 | * 21 6/24/97 7:42p Matt |
| 73 | * Included bitmap.h instead of font.h |
| 74 | * |
| 75 | * 20 6/16/97 4:45p Samir |
| 76 | * Fixed transparency probs |
| 77 | * |
| 78 | * 19 6/16/97 3:01p Samir |
| 79 | * Changed font system to work better with renderer. Fonts are rendered |
| 80 | * on bitmaps of 128x128 at load time. |
| 81 | * |
| 82 | * 18 6/12/97 6:27p Samir |
| 83 | * DDGR v2.0 (GR v1.1) Changes to ddgr system reflected in 2d system |
| 84 | * |
| 85 | * 17 5/14/97 8:07p Samir |
| 86 | * some pathname problem which screwed up Mac. |
| 87 | * |
| 88 | * 16 3/07/97 4:01p Samir |
| 89 | * Took out malloc.h for ANSI compliance. |
| 90 | * |
| 91 | * 15 3/03/97 6:20p Matt |
| 92 | * Changed cfile functions to use D3 naming convention |
| 93 | * |
| 94 | * 14 2/28/97 3:17 PM Jeremy |
| 95 | * changed a call of strcmpi to stricmp |
| 96 | * |
| 97 | * 13 2/28/97 2:57p Samir |
| 98 | * Took out __cdecl from atexit |
| 99 | * |
| 100 | * 12 2/10/97 12:14p Samir |
| 101 | * Allow multiple font loading per viewport. |
| 102 | * |
| 103 | * 11 2/06/97 6:33p Samir |
| 104 | * Added clipping character functions for font. |
| 105 | * |
| 106 | * 10 2/06/97 12:43p Samir |
| 107 | * Mono fonts work. |
| 108 | * |
| 109 | * 9 2/05/97 12:19p Samir |
| 110 | * Stopped redundant increases in font reference count for same font |
| 111 | * object. |
| 112 | * |
| 113 | * 8 2/05/97 10:35a Samir |
| 114 | * Fixed text drawing at wrong location problem |
| 115 | * |
| 116 | * $NoKeywords: $ |
| 117 | */ |
| 118 | |
| 119 | #include <cstring> |
| 120 | #include <cstdlib> |
| 121 | |
| 122 | #include "bitmap.h" |
| 123 | #include "cfile.h" |
| 124 | #include "gr.h" |
| 125 | #include "mem.h" |
| 126 | #include "mono.h" |
| 127 | #include "pserror.h" |
| 128 | #include "renderer.h" |
| 129 | |
| 130 | #define FT_COLOR 1 |
| 131 | #define FT_PROPORTIONAL 2 |
| 132 | #define FT_KERNED 4 |
| 133 | #define FT_GRADIENT 8 |
| 134 | |
| 135 | #define GRFONT_SURFACE_WIDTH 128 |
| 136 | #define GRFONT_SURFACE_HEIGHT 128 |
| 137 | |
| 138 | // ---------------------------------------------------------------------------- |
| 139 | // Macros for file io. |
| 140 | // ---------------------------------------------------------------------------- |
| 141 | |
| 142 | typedef CFILE *FONTFILE; |
| 143 | |
| 144 | static inline int READ_FONT_INT(FONTFILE ffile); |
| 145 | static inline int16_t READ_FONT_SHORT(FONTFILE ffile); |
| 146 | static inline uint8_t READ_FONT_BYTE(FONTFILE ffile); |
| 147 | static inline int READ_FONT_DATA(FONTFILE ffile, void *buf, int size, int nelem); |
| 148 | static inline FONTFILE OPEN_FONT(char *filename, bool &success); |
| 149 | static inline void CLOSE_FONT(FONTFILE ffile); |
| 150 | |
| 151 | #define BITS_TO_BYTES(_c) (((_c) + 7) >> 3) |
| 152 | #define BITS_TO_SHORTS(_c) (((_c) + 15) >> 4) |
| 153 | |
| 154 | inline int READ_FONT_INT(FONTFILE ffile) { return cf_ReadInt(ffile); } |
| 155 | |
| 156 | inline int16_t READ_FONT_SHORT(FONTFILE ffile) { return cf_ReadShort(ffile); } |
| 157 | |
| 158 | inline uint8_t READ_FONT_BYTE(FONTFILE ffile) { return (uint8_t)cf_ReadByte(ffile); } |
| 159 | |
| 160 | inline int READ_FONT_DATA(FONTFILE ffile, void *buf, int size, int nelem) { |
| 161 | int i; |
| 162 | |
| 163 | i = cf_ReadBytes((uint8_t *)buf, size * nelem, ffile); |
| 164 | |
| 165 | ASSERT(i == (size * nelem)); |
| 166 | |
| 167 | return i; |
| 168 | } |
| 169 | |
| 170 | inline FONTFILE OPEN_FONT(char *filename, bool &success) { |
| 171 | FONTFILE fp; |
| 172 | int file_id; |
| 173 | |
| 174 | success = false; |
| 175 | fp = (FONTFILE)cfopen(filename, "rb"); |
| 176 | if (!fp) |
| 177 | return NULL; |
| 178 | |
| 179 | file_id = READ_FONT_INT(fp); |
| 180 | if (file_id != 0xfeedbaba) |
| 181 | return (FONTFILE)(-1); |
| 182 | |
| 183 | success = true; |
| 184 | return fp; |
| 185 | } |
| 186 | |
| 187 | inline void CLOSE_FONT(FONTFILE ffile) { cfclose(ffile); } |
| 188 | |
| 189 | // ---------------------------------------------------------------------------- |
| 190 | // static variables and functions |
| 191 | // ---------------------------------------------------------------------------- |
| 192 | |
| 193 | gr_font_record grFont::m_FontList[MAX_FONTS]; |
| 194 | grMemorySurface *grFont::m_CharSurf = NULL; |
| 195 | |
| 196 | void grFont::init_system() { |
| 197 | for (int i = 0; i < MAX_FONTS; i++) { |
| 198 | grFont::m_FontList[i].references = 0; |
| 199 | grFont::m_FontList[i].filename[0] = 0; |
| 200 | } |
| 201 | |
| 202 | if (grFont::m_CharSurf) { |
| 203 | delete grFont::m_CharSurf; |
| 204 | grFont::m_CharSurf = NULL; |
| 205 | } |
| 206 | |
| 207 | if (!grFont::m_CharSurf) { |
| 208 | grFont::m_CharSurf = new grMemorySurface; |
| 209 | grFont::m_CharSurf->init(0, 0, BPP_16, NULL, 0, 0, "char_surf"); |
| 210 | } |
| 211 | |
| 212 | atexit(grFont::close_system); |
| 213 | } |
| 214 | |
| 215 | void grFont::close_system() { |
| 216 | if (grFont::m_CharSurf) { |
| 217 | delete grFont::m_CharSurf; |
| 218 | grFont::m_CharSurf = NULL; |
| 219 | } |
| 220 | |
| 221 | // we should free any fonts currently allocated here. |
| 222 | mprintf(0, "Freeing cached fonts..."); |
| 223 | for (int i = 0; i < MAX_FONTS; i++) { |
| 224 | gr_font_record *ft; |
| 225 | if (grFont::m_FontList[i].references) { |
| 226 | ft = &grFont::m_FontList[i]; |
| 227 | grFont::free_font(ft); |
| 228 | grFont::m_FontList[i].references = 0; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | int grFont::register_font(char *fontname, char *filename) { |
| 234 | /* look for a slot with no fontname and place fontname in it */ |
| 235 | ASSERT(fontname != NULL); |
| 236 | |
| 237 | int i; |
| 238 | |
| 239 | for (i = 0; i < MAX_FONTS; i++) { |
| 240 | if (grFont::m_FontList[i].filename[0] == 0) { |
| 241 | strcpy(grFont::m_FontList[i].filename, filename); |
| 242 | strcpy(grFont::m_FontList[i].name, fontname); |
| 243 | grFont::load(m_FontList[i].filename, i); |
| 244 | grFont::m_FontList[i].references = 1; |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (i == MAX_FONTS) |
| 250 | i = -1; |
| 251 | |
| 252 | return i; |
| 253 | } |
| 254 | |
| 255 | // ---------------------------------------------------------------------------- |
| 256 | // constructor and destructor of font object |
| 257 | // ---------------------------------------------------------------------------- |
| 258 | |
| 259 | grFont::grFont() { m_FontHandle = -1; } |
| 260 | |
| 261 | grFont::~grFont() { |
| 262 | if (m_FontHandle > -1) |
| 263 | grFont::free(); |
| 264 | } |
| 265 | |
| 266 | // ---------------------------------------------------------------------------- |
| 267 | // high level font initialization and cleanup routines |
| 268 | // ---------------------------------------------------------------------------- |
| 269 | |
| 270 | void grFont::init(const char *fontname) { |
| 271 | int i, slot = -1; |
| 272 | |
| 273 | // Look for this font in the list, check if it has any references, and load it if |
| 274 | // there are none. |
| 275 | if (fontname == NULL) { |
| 276 | slot = 0; |
| 277 | } else { |
| 278 | for (i = 0; i < MAX_FONTS; i++) { |
| 279 | if (stricmp(grFont::m_FontList[i].name, fontname) == 0) { |
| 280 | slot = i; |
| 281 | |
| 282 | if (grFont::m_FontList[i].references == 0) { |
| 283 | break; |
| 284 | } else { |
| 285 | m_FontHandle = slot; |
| 286 | return; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | ASSERT(slot != -1); |
| 292 | |
| 293 | // clear up surface and bitmap lists |
| 294 | grFont::m_FontList[slot].ch_w = NULL; |
| 295 | grFont::m_FontList[slot].ch_h = NULL; |
| 296 | grFont::m_FontList[slot].ch_u = NULL; |
| 297 | grFont::m_FontList[slot].ch_v = NULL; |
| 298 | |
| 299 | for (i = 0; i < MAX_FONT_BITMAPS; i++) { |
| 300 | grFont::m_FontList[slot].bmps[i] = -1; |
| 301 | grFont::m_FontList[slot].surfs[i] = NULL; |
| 302 | } |
| 303 | |
| 304 | // We must load this font from the 'hogfile' |
| 305 | grFont::load(m_FontList[slot].filename, slot); |
| 306 | |
| 307 | grFont::m_FontList[slot].references++; |
| 308 | } |
| 309 | |
| 310 | m_FontHandle = slot; |
| 311 | } |
| 312 | |
| 313 | void grFont::free() { |
| 314 | if (m_FontHandle == -1) |
| 315 | return; |
| 316 | |
| 317 | return; |
| 318 | |
| 319 | //!! Removed the following assert because there are bugs in the reference count system. Matt, 11-15-97. |
| 320 | //!! ASSERT(grFont::m_FontList[m_FontHandle].references > 0); |
| 321 | //@@ |
| 322 | // free font if there was only one reference left. |
| 323 | //@@ if (grFont::m_FontList[m_FontHandle].references == 1) { |
| 324 | //@@ gr_font_record *ft; |
| 325 | //@@ |
| 326 | //@@ // free up all memory allocated to this font. |
| 327 | //@@ ft = &grFont::m_FontList[m_FontHandle]; |
| 328 | //@@ |
| 329 | //@@ grFont::free_font(ft); |
| 330 | //@@ } |
| 331 | //@@ |
| 332 | //@@ grFont::m_FontList[m_FontHandle].references--; |
| 333 | m_FontHandle = -1; |
| 334 | } |
| 335 | |
| 336 | void grFont::free_font(gr_font_record *ft) { |
| 337 | int i; |
| 338 | |
| 339 | // delete font surface info. |
| 340 | if (ft->ch_wf) |
| 341 | delete[] ft->ch_wf; |
| 342 | if (ft->ch_hf) |
| 343 | delete[] ft->ch_hf; |
| 344 | if (ft->ch_uf) |
| 345 | delete[] ft->ch_uf; |
| 346 | if (ft->ch_vf) |
| 347 | delete[] ft->ch_vf; |
| 348 | if (ft->ch_w) |
| 349 | delete[] ft->ch_w; |
| 350 | if (ft->ch_h) |
| 351 | delete[] ft->ch_h; |
| 352 | if (ft->ch_u) |
| 353 | delete[] ft->ch_u; |
| 354 | if (ft->ch_v) |
| 355 | delete[] ft->ch_v; |
| 356 | if (ft->ch_surf) |
| 357 | delete[] ft->ch_surf; |
| 358 | |
| 359 | for (i = 0; i < MAX_FONT_BITMAPS; i++) { |
| 360 | if (ft->bmps[i] != -1) |
| 361 | bm_FreeBitmap(ft->bmps[i]); |
| 362 | if (ft->surfs[i]) |
| 363 | delete ft->surfs[i]; |
| 364 | } |
| 365 | |
| 366 | // delete font file info. |
| 367 | if (ft->font.flags & FT_PROPORTIONAL) { |
| 368 | delete[] ft->font.char_widths; |
| 369 | } |
| 370 | |
| 371 | ::mem_free(ft->font.raw_data); |
| 372 | ::mem_free(ft->font.char_data); |
| 373 | } |
| 374 | |
| 375 | void grFont::load(char *filename, int slot) { |
| 376 | FONTFILE ff; |
| 377 | gr_font_file_record fnt, *ft; |
| 378 | char fontname[32]; |
| 379 | int num_char; |
| 380 | int i; |
| 381 | |
| 382 | ft = &fnt; |
| 383 | |
| 384 | bool success; |
| 385 | ff = OPEN_FONT(filename, success); |
| 386 | if (!ff) { |
| 387 | Error("Unable to open font in file %s.\n", filename); |
| 388 | } else if (!success) { |
| 389 | Error("Illegal font file: %s.\n", filename); |
| 390 | } |
| 391 | |
| 392 | mprintf(0, "%s font.\n", grFont::m_FontList[slot].name); |
| 393 | |
| 394 | ft->width = READ_FONT_SHORT(ff); |
| 395 | ft->height = READ_FONT_SHORT(ff); |
| 396 | ft->flags = READ_FONT_SHORT(ff); |
| 397 | ft->baseline = READ_FONT_SHORT(ff); |
| 398 | ft->min_ascii = READ_FONT_BYTE(ff); |
| 399 | ft->max_ascii = READ_FONT_BYTE(ff); |
| 400 | READ_FONT_DATA(ff, fontname, 32, 1); |
| 401 | |
| 402 | mprintf(0, " <ht %d>::<min %d>::<max %d>::<base %d>", ft->height, ft->min_ascii, ft->max_ascii, ft->baseline); |
| 403 | |
| 404 | num_char = ft->max_ascii - ft->min_ascii + 1; |
| 405 | |
| 406 | // Read in all widths |
| 407 | if (ft->flags & FT_PROPORTIONAL) { |
| 408 | ft->char_widths = new int16_t[num_char]; |
| 409 | for (i = 0; i < num_char; i++) |
| 410 | ft->char_widths[i] = READ_FONT_SHORT(ff); |
| 411 | mprintf(0, "::proportional"); |
| 412 | } else { |
| 413 | ft->char_widths = NULL; |
| 414 | } |
| 415 | |
| 416 | // Read in pixel data. |
| 417 | // for color fonts, read in byte count and then the data, |
| 418 | // generate character data pointer table |
| 419 | // for mono fonts, read in byte count, then the data, convert to bits and store |
| 420 | // generate character data pointer table |
| 421 | int bytesize = READ_FONT_INT(ff); |
| 422 | |
| 423 | ft->raw_data = (uint8_t *)mem_malloc(bytesize); |
| 424 | ft->char_data = (uint8_t **)mem_malloc(num_char * sizeof(uint8_t *)); |
| 425 | |
| 426 | READ_FONT_DATA(ff, ft->raw_data, bytesize, 1); |
| 427 | |
| 428 | if (ft->flags & FT_COLOR) { |
| 429 | int off = 0; |
| 430 | mprintf(0, "::color"); |
| 431 | for (i = 0; i < num_char; i++) { |
| 432 | ft->char_data[i] = ft->raw_data + off; |
| 433 | if (ft->flags & FT_PROPORTIONAL) |
| 434 | off += (ft->char_widths[i] * ft->height * BITS_TO_BYTES(BPP_16)); |
| 435 | else |
| 436 | off += (ft->width * ft->height * BITS_TO_BYTES(BPP_16)); |
| 437 | } |
| 438 | } else { // Monochrome |
| 439 | uint8_t *ptr = ft->raw_data; |
| 440 | mprintf(0, "::mono"); |
| 441 | for (i = 0; i < num_char; i++) { |
| 442 | ft->char_data[i] = ptr; |
| 443 | if (ft->flags & FT_PROPORTIONAL) |
| 444 | ptr += BITS_TO_BYTES(ft->char_widths[i]) * ft->height; |
| 445 | else |
| 446 | ptr += BITS_TO_BYTES(ft->width) * ft->height; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // Read in kerning data |
| 451 | ft->kern_data = NULL; |
| 452 | |
| 453 | // Then read in |
| 454 | CLOSE_FONT(ff); |
| 455 | |
| 456 | mprintf(0, "\n"); |
| 457 | |
| 458 | grFont::m_FontList[slot].font = fnt; |
| 459 | |
| 460 | // draw font to bitmaps, load into surfaces too. |
| 461 | grFont::translate_to_surfaces(slot); |
| 462 | } |
| 463 | |
| 464 | // translates a font to surfaces |
| 465 | void grFont::translate_to_surfaces(int slot) { |
| 466 | gr_font_file_record *fntfile; |
| 467 | gr_font_record *fnt; |
| 468 | |
| 469 | fnt = &grFont::m_FontList[slot]; |
| 470 | fntfile = &grFont::m_FontList[slot].font; |
| 471 | |
| 472 | // start creating font surfaces, map these surfaces onto bitmaps created via bitmap library |
| 473 | // this is needed for the renderer library. |
| 474 | // create a 128x128 surface first. |
| 475 | // draw each character into surface until we need to create another |
| 476 | // surface. |
| 477 | uint8_t u = 0, v = 0, w; |
| 478 | int ch, num_ch; |
| 479 | uint8_t surf_index = 0; |
| 480 | |
| 481 | num_ch = fntfile->max_ascii - fntfile->min_ascii + 1; |
| 482 | |
| 483 | // initialize memory |
| 484 | fnt->ch_w = new uint8_t[num_ch]; |
| 485 | fnt->ch_h = new uint8_t[num_ch]; |
| 486 | fnt->ch_u = new uint8_t[num_ch]; |
| 487 | fnt->ch_v = new uint8_t[num_ch]; |
| 488 | fnt->ch_surf = new uint8_t[num_ch]; |
| 489 | fnt->ch_uf = new float[num_ch]; |
| 490 | fnt->ch_vf = new float[num_ch]; |
| 491 | fnt->ch_wf = new float[num_ch]; |
| 492 | fnt->ch_hf = new float[num_ch]; |
| 493 | |
| 494 | fnt->bmps[surf_index] = bm_AllocBitmap(GRFONT_SURFACE_WIDTH, GRFONT_SURFACE_HEIGHT, 0); |
| 495 | if (fnt->bmps[surf_index] == -1) |
| 496 | Error("grFont::translate_to_surfaces <Bitmap allocation error>"); |
| 497 | fnt->surfs[surf_index] = new grMemorySurface; |
| 498 | fnt->surfs[surf_index]->init(GRFONT_SURFACE_WIDTH, GRFONT_SURFACE_HEIGHT, BPP_16, |
| 499 | (char *)bm_data(fnt->bmps[surf_index], 0), GRFONT_SURFACE_WIDTH * BITS_TO_BYTES(BPP_16), |
| 500 | SURFFLAG_COLORKEY); |
| 501 | fnt->surfs[surf_index]->clear(TRANSPARENT_COLOR32); |
| 502 | surf_index++; |
| 503 | |
| 504 | for (ch = 0; ch < num_ch; ch++) { |
| 505 | if (fntfile->flags & FT_PROPORTIONAL) |
| 506 | w = (int)fntfile->char_widths[ch]; |
| 507 | else |
| 508 | w = (int)fntfile->width; |
| 509 | |
| 510 | // blt each character |
| 511 | if (fntfile->flags & FT_COLOR) { |
| 512 | m_CharSurf->init(w, fntfile->height, BPP_16, (char *)fntfile->char_data[ch], w * BITS_TO_BYTES(BPP_16), |
| 513 | SURFFLAG_COLORKEY); |
| 514 | |
| 515 | fnt->surfs[surf_index - 1]->blt(u, v, m_CharSurf); |
| 516 | } else { // font monochrome, convert bits to pixels |
| 517 | grFont::translate_mono_char(fnt->surfs[surf_index - 1], u, v, ch, fntfile, w); |
| 518 | } |
| 519 | |
| 520 | fnt->ch_h[ch] = fntfile->height; |
| 521 | fnt->ch_w[ch] = w; |
| 522 | fnt->ch_u[ch] = u; |
| 523 | fnt->ch_v[ch] = v; |
| 524 | fnt->ch_surf[ch] = surf_index - 1; |
| 525 | fnt->ch_hf[ch] = ((float)fntfile->height) / ((float)GRFONT_SURFACE_HEIGHT); |
| 526 | fnt->ch_wf[ch] = ((float)w) / ((float)GRFONT_SURFACE_WIDTH); |
| 527 | fnt->ch_uf[ch] = ((float)u) / ((float)GRFONT_SURFACE_WIDTH); |
| 528 | fnt->ch_vf[ch] = ((float)v) / ((float)GRFONT_SURFACE_HEIGHT); |
| 529 | |
| 530 | // check to adjust uv's if we are outside surface. |
| 531 | u += w; |
| 532 | if ((u + w) > GRFONT_SURFACE_WIDTH) { |
| 533 | u = 0; |
| 534 | v += fntfile->height; |
| 535 | if ((v + fntfile->height) > GRFONT_SURFACE_HEIGHT) { |
| 536 | if (surf_index == MAX_FONT_BITMAPS) |
| 537 | Int3(); |
| 538 | fnt->bmps[surf_index] = bm_AllocBitmap(GRFONT_SURFACE_WIDTH, GRFONT_SURFACE_HEIGHT, 0); |
| 539 | if (fnt->bmps[surf_index] == -1) |
| 540 | Error("grFont::translate_to_surfaces <Bitmap allocation error>"); |
| 541 | fnt->surfs[surf_index] = new grMemorySurface; |
| 542 | fnt->surfs[surf_index]->init(GRFONT_SURFACE_WIDTH, GRFONT_SURFACE_HEIGHT, BPP_16, |
| 543 | (char *)bm_data(fnt->bmps[surf_index], 0), |
| 544 | GRFONT_SURFACE_WIDTH * BITS_TO_BYTES(BPP_16), SURFFLAG_COLORKEY); |
| 545 | fnt->surfs[surf_index]->clear(TRANSPARENT_COLOR32); |
| 546 | surf_index++; |
| 547 | v = 0; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // translate mono font data to the surface |
| 554 | void grFont::translate_mono_char(grSurface *sf, int x, int y, int index, gr_font_file_record *ft, int width) { |
| 555 | int row, col; // byte width of char |
| 556 | int rowsize; |
| 557 | uint8_t bit_mask = 0, byte = 0; |
| 558 | uint8_t *fp; |
| 559 | |
| 560 | fp = ft->char_data[index]; |
| 561 | |
| 562 | switch (sf->bpp()) { |
| 563 | case BPP_16: { |
| 564 | /* draw one-bit one color. */ |
| 565 | uint16_t *dest_ptr; |
| 566 | uint16_t col_w = GR_COLOR_TO_16(GR_RGB(255, 255, 255)); |
| 567 | int rowsize_w; |
| 568 | |
| 569 | dest_ptr = (uint16_t *)sf->lock(&rowsize); |
| 570 | rowsize_w = sf->rowsize() / 2; |
| 571 | dest_ptr += (y * rowsize_w) + x; |
| 572 | |
| 573 | for (row = 0; row < ft->height; row++) { |
| 574 | bit_mask = 0; |
| 575 | for (col = 0; col < width; col++) { |
| 576 | if (bit_mask == 0) { |
| 577 | byte = *fp++; |
| 578 | bit_mask = 0x80; |
| 579 | } |
| 580 | |
| 581 | if (byte & bit_mask) |
| 582 | dest_ptr[col] = col_w; |
| 583 | bit_mask >>= 1; |
| 584 | } |
| 585 | dest_ptr += rowsize_w; |
| 586 | } |
| 587 | |
| 588 | sf->unlock(); |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | case BPP_32: |
| 593 | default: |
| 594 | Int3(); // NOT SUPPORTED YET |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | // ---------------------------------------------------------------------------- |
| 599 | // draw_char function |
| 600 | // give a viewport x,y and char |
| 601 | // ---------------------------------------------------------------------------- |
| 602 | |
| 603 | int grFont::draw_char(grSurface *sf, int x, int y, int ch, tCharProperties *chprop) { |
| 604 | gr_font_record *ft; |
| 605 | int next_x = 0, width, index; |
| 606 | |
| 607 | ASSERT(m_FontHandle > -1); |
| 608 | if ((ch < min_ascii()) || (ch > max_ascii())) |
| 609 | return (next_x + 1); |
| 610 | |
| 611 | // save current x and get this font |
| 612 | ft = &grFont::m_FontList[m_FontHandle]; |
| 613 | next_x = x; |
| 614 | index = ch - ft->font.min_ascii; |
| 615 | |
| 616 | // draw either a color bitmap or mono font. |
| 617 | if (ft->font.flags & FT_PROPORTIONAL) { |
| 618 | width = (int)ft->font.char_widths[index]; |
| 619 | } else { |
| 620 | width = (int)ft->font.width; |
| 621 | } |
| 622 | |
| 623 | // perform blt. |
| 624 | if (sf->get_flags() & SURFFLAG_RENDERER) { |
| 625 | rend_SetCharacterParameters(chprop->col[0], chprop->col[1], chprop->col[2], chprop->col[3]); |
| 626 | rend_DrawFontCharacter(ft->bmps[ft->ch_surf[index]], x, y, x + ft->ch_w[index] - 1, y + ft->ch_h[index] - 1, |
| 627 | ft->ch_uf[index], ft->ch_vf[index], ft->ch_wf[index], ft->ch_hf[index]); |
| 628 | } else if (ft->font.flags & FT_COLOR) { |
| 629 | sf->blt(x, y, ft->surfs[ft->ch_surf[index]], ft->ch_u[index], ft->ch_v[index], ft->ch_w[index], ft->ch_h[index]); |
| 630 | } else { |
| 631 | charblt16(sf, chprop->col[0], x, y, ft->surfs[ft->ch_surf[index]], ft->ch_u[index], ft->ch_v[index], |
| 632 | ft->ch_w[index], ft->ch_h[index]); |
| 633 | } |
| 634 | |
| 635 | // adjust next x value with kerning and return it. |
| 636 | next_x += width; |
| 637 | |
| 638 | return next_x; |
| 639 | } |
| 640 | |
| 641 | int grFont::draw_char(grSurface *sf, int x, int y, int ch, int sx, int sy, int sw, int sh, tCharProperties *chprop) { |
| 642 | gr_font_record *ft; |
| 643 | int next_x = 0, width, index; |
| 644 | |
| 645 | ASSERT(m_FontHandle > -1); |
| 646 | if ((ch < min_ascii()) || (ch > max_ascii())) |
| 647 | return (next_x + 1); |
| 648 | |
| 649 | // save current x and get this font |
| 650 | ft = &grFont::m_FontList[m_FontHandle]; |
| 651 | next_x = x; |
| 652 | index = ch - ft->font.min_ascii; |
| 653 | |
| 654 | // draw either a color bitmap or mono font. |
| 655 | if (ft->font.flags & FT_PROPORTIONAL) { |
| 656 | width = (int)ft->font.char_widths[index]; |
| 657 | } else { |
| 658 | width = (int)ft->font.width; |
| 659 | } |
| 660 | |
| 661 | ASSERT(sx + sw <= width); |
| 662 | ASSERT(sy + sh <= ft->font.height); |
| 663 | |
| 664 | // peform blt instead. |
| 665 | // perform blt. |
| 666 | if (sf->get_flags() & SURFFLAG_RENDERER) { |
| 667 | rend_SetCharacterParameters(chprop->col[0], chprop->col[1], chprop->col[2], chprop->col[3]); |
| 668 | |
| 669 | rend_DrawFontCharacter(ft->bmps[ft->ch_surf[index]], x, y, x + sw - 1, y + sh - 1, |
| 670 | ft->ch_uf[index] + (((float)sx) / ((float)GRFONT_SURFACE_WIDTH)), |
| 671 | ft->ch_vf[index] + (((float)sy) / ((float)GRFONT_SURFACE_HEIGHT)), |
| 672 | ((float)sw) / ((float)GRFONT_SURFACE_WIDTH), ((float)sh) / ((float)GRFONT_SURFACE_HEIGHT)); |
| 673 | } else if (ft->font.flags & FT_COLOR) { |
| 674 | sf->blt(x, y, ft->surfs[ft->ch_surf[index]], ft->ch_u[index] + sx, ft->ch_v[index] + sy, sw, sh); |
| 675 | } else { |
| 676 | charblt16(sf, chprop->col[0], x, y, ft->surfs[ft->ch_surf[index]], ft->ch_u[index] + sx, ft->ch_v[index] + sy, sw, |
| 677 | sh); |
| 678 | } |
| 679 | |
| 680 | // adjust next x value with kerning and return it. |
| 681 | next_x += sw; |
| 682 | |
| 683 | return next_x; |
| 684 | } |
| 685 | |
| 686 | // ---------------------------------------------------------------------------- |
| 687 | // accessor functions |
| 688 | // ---------------------------------------------------------------------------- |
| 689 | |
| 690 | int grFont::get_char_info(int ch, int *width) { |
| 691 | gr_font_file_record *ft; |
| 692 | |
| 693 | ASSERT(m_FontHandle > -1); |
| 694 | |
| 695 | ft = &grFont::m_FontList[m_FontHandle].font; |
| 696 | |
| 697 | if (ch < ft->min_ascii || ch > ft->max_ascii) |
| 698 | *width = 0; |
| 699 | else if (ft->flags & FT_PROPORTIONAL) |
| 700 | *width = ft->char_widths[ch - ft->min_ascii]; |
| 701 | else |
| 702 | *width = ft->width; |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | uint8_t *grFont::get_kern_info(uint8_t c1, uint8_t c2) { |
| 708 | // gr_font_file_record *ft; |
| 709 | // uint8_t *p; |
| 710 | |
| 711 | // p = ft->kern_data; |
| 712 | // ft = &grFont::m_FontList[m_FontHandle].font; |
| 713 | // c2 = c2 - ft->min_ascii; |
| 714 | // c1 = c1 - ft->min_ascii; |
| 715 | |
| 716 | // while (*p!=255) |
| 717 | // { |
| 718 | // if (p[0]==c1 && p[1]==c2) return p; |
| 719 | // else p+=3; |
| 720 | // } |
| 721 | |
| 722 | return NULL; |
| 723 | } |
| 724 | |
| 725 | void grFont::charblt16(grSurface *dsf, ddgr_color col, int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh) { |
| 726 | uint16_t *dbits; |
| 727 | uint16_t *sbits; |
| 728 | int srowsize_w, drowsize_w, row, coln; |
| 729 | uint16_t scol = GR_COLOR_TO_16(col); |
| 730 | |
| 731 | dbits = (uint16_t *)dsf->lock(&drowsize_w); |
| 732 | if (dbits) { |
| 733 | sbits = (uint16_t *)ssf->lock(&srowsize_w); |
| 734 | if (sbits) { |
| 735 | srowsize_w >>= 1; // rowsize in shorts |
| 736 | drowsize_w >>= 1; |
| 737 | dbits = dbits + (dy * drowsize_w) + dx; |
| 738 | sbits = sbits + (sy * srowsize_w) + sx; |
| 739 | |
| 740 | for (row = 0; row < sh; row++) { |
| 741 | for (coln = 0; coln < sw; coln++) { |
| 742 | if (sbits[coln] & OPAQUE_FLAG16) |
| 743 | dbits[coln] = scol; |
| 744 | } |
| 745 | sbits += srowsize_w; |
| 746 | dbits += drowsize_w; |
| 747 | } |
| 748 | |
| 749 | ssf->unlock(); |
| 750 | } |
| 751 | dsf->unlock(); |
| 752 | } |
| 753 | } |