ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
font.h File Reference
#include <draw.h>
#include <tamtypes.h>
+ Include dependency graph for font.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  fontx_t
 
struct  inidata_t
 
struct  fsfont_t
 

Macros

#define SINGLE_BYTE   0
 
#define DOUBLE_BYTE   1
 
#define LEFT_ALIGN   0
 
#define CENTER_ALIGN   1
 
#define RIGHT_ALIGN   2
 

Functions

int fontx_load (const char *path, fontx_t *fontx, int type, int wmargin, int hmargin, int bold)
 
void fontx_unload (fontx_t *fontx)
 
qword_tfontx_print_ascii (qword_t *q, int context, const unsigned char *str, int alignment, vertex_t *v0, color_t *c0, fontx_t *fontx)
 
qword_tfontx_print_sjis (qword_t *q, int context, const unsigned char *str, int alignment, vertex_t *v0, color_t *c0, fontx_t *ascii, fontx_t *kanji)
 
fsfont_tfontstudio_init (int height)
 
void fontstudio_free (fsfont_t *font)
 
int fontstudio_parse_ini (fsfont_t *font, char *ini, float tex_width, float tex_height)
 
char * fontstudio_load_ini (const char *path)
 
void fontstudio_unload_ini (fsfont_t *font)
 
qword_tfontstudio_print_string (qword_t *q, int context, const unsigned char *string, int alignment, vertex_t *v0, color_t *c0, fsfont_t *font)
 

Detailed Description

Font library.

Definition in file font.h.

Macro Definition Documentation

◆ CENTER_ALIGN

#define CENTER_ALIGN   1

Definition at line 61 of file font.h.

◆ DOUBLE_BYTE

#define DOUBLE_BYTE   1

Definition at line 14 of file font.h.

◆ LEFT_ALIGN

#define LEFT_ALIGN   0

Alignments

Definition at line 60 of file font.h.

◆ RIGHT_ALIGN

#define RIGHT_ALIGN   2

Definition at line 62 of file font.h.

◆ SINGLE_BYTE

#define SINGLE_BYTE   0

FontX2 types

Definition at line 13 of file font.h.

Function Documentation

◆ fontstudio_free()

void fontstudio_free ( fsfont_t font)

Definition at line 36 of file fsfont.c.

37 {
38 
39  if (font->charmap != NULL)
40  {
41  free(font->charmap);
42  }
43 
44  if (font->chardata != NULL)
45  {
46  free(font->chardata);
47  }
48 
49  free(font);
50 
51 }
inidata_t * chardata
Definition: font.h:56
unsigned short * charmap
Definition: font.h:53
#define NULL
Definition: tamtypes.h:91

References fsfont_t::chardata, fsfont_t::charmap, and NULL.

◆ fontstudio_init()

fsfont_t* fontstudio_init ( int  height)

Loads the FontStudio ini file that contains the font texture's characteristics

Definition at line 22 of file fsfont.c.

23 {
24 
25  fsfont_t *font;
26 
27  font = (fsfont_t*)malloc(sizeof(fsfont_t));
28 
29  font->height = char_height;
30  font->scale = 1.0f;
31 
32  return font;
33 
34 }
Definition: font.h:50
float scale
Definition: font.h:51
char height
Definition: font.h:52

References fsfont_t::height, and fsfont_t::scale.

◆ fontstudio_load_ini()

char* fontstudio_load_ini ( const char *  path)

Loads an ini file into memory and returns pointer to it

Definition at line 53 of file fsfont.c.

54 {
55 
56  FILE *file;
57 
58  char *ini;
59  int size;
60 
61  file = fopen(path, "r");
62 
63  if (file == NULL)
64  {
65 
66  printf("Error opening %s.\n", path);
67  return NULL;
68 
69  }
70 
71  fseek(file, 0, SEEK_END);
72  size = ftell(file);
73  fseek(file, 0, SEEK_SET);
74 
75  ini = (char *)malloc(size);
76 
77  if (ini == NULL)
78  {
79  printf("Error allocated %d bytes of memory.\n", size);
80  fclose(file);
81  return NULL;
82  }
83 
84  fread(ini, size, 1, file);
85  fclose(file);
86 
87  return ini;
88 
89 }

References NULL.

Referenced by main().

◆ fontstudio_parse_ini()

int fontstudio_parse_ini ( fsfont_t font,
char *  ini,
float  tex_width,
float  tex_height 
)

Parses a font's ini for the font. The content of ini is no longer required afterwards and can be discarded.

Definition at line 91 of file fsfont.c.

92 {
93 
94  int i;
95 
96  char *temp0;
97  char *temp1;
98 
99  temp0 = ini;
100 
101  temp1 = strtok(temp0,"=");
102  if (temp1 == NULL)
103  {
104  printf("Error parsing number of chars.\n");
105  return -1;
106  }
107  temp0 += strlen(temp1)+1;
108 
109  font->totalchars = (int)strtol(temp0,NULL,10);
110 
111  temp1 = strtok(temp0,"=");
112  if (temp1 == NULL)
113  {
114  printf("Error parsing space width.\n");
115  return -1;
116  }
117  temp0 += strlen(temp1)+1;
118 
119  font->spacewidth = (int)strtol(temp0,NULL,10);
120 
121  font->charmap = (unsigned short*)malloc(sizeof(short)*font->totalchars);
122 
123  if (font->charmap == NULL)
124  {
125 
126  //131 kilobytes of memory
127  printf("Error allocated %d bytes of memory.\n", sizeof(short)*font->totalchars);
128  return -1;
129 
130  }
131 
132  // Clear memory
133  memset(font->charmap,0,sizeof(short)*font->totalchars);
134 
135  font->chardata = (inidata_t*)malloc(sizeof(inidata_t)*font->totalchars);
136 
137  if (font->chardata == NULL)
138  {
139 
140  printf("Error allocating %d bytes of memory.\n", sizeof(inidata_t)*font->totalchars);
141  free(font->charmap);
142  return -1;
143 
144  }
145 
146  // Clear memory
147  memset(font->chardata,0,sizeof(inidata_t)*font->totalchars);
148 
149  for (i = 0; i < font->totalchars; i++)
150  {
151 
152  temp1 = strtok(temp0,"=");
153  if (temp1 == NULL)
154  {
155 
156  printf("Error parsing Char for char %d.\n", i);
157  free(font->charmap);
158  free(font->chardata);
159  return -1;
160 
161  }
162  temp0 += strlen(temp1)+1;
163  font->charmap[i] = (int)strtol(temp0,NULL,10);
164 
165  temp1 = strtok(temp0,"=");
166  if (temp1 == NULL)
167  {
168 
169  printf("Error parsing A for char %d.\n", i);
170  free(font->charmap);
171  free(font->chardata);
172  return -1;
173 
174  }
175  temp0 += strlen(temp1)+1;
176  font->chardata[i].A = (int)strtol(temp0,NULL,10);
177 
178  temp1 = strtok(temp0,"=");
179  if (temp1 == NULL)
180  {
181 
182  printf("Error parsing B for char %d.\n", i);
183  free(font->charmap);
184  free(font->chardata);
185  return -1;
186 
187  }
188  temp0 += strlen(temp1)+1;
189  font->chardata[i].B = (int)strtol(temp0,NULL,10);
190 
191  temp1 = strtok(temp0,"=");
192  if (temp1 == NULL)
193  {
194 
195  printf("Error parsing C for char %d.\n", i);
196  free(font->charmap);
197  free(font->chardata);
198  return -1;
199 
200  }
201  temp0 += strlen(temp1)+1;
202  font->chardata[i].C = (int)strtol(temp0,NULL,10);
203 
204  temp1 = strtok(temp0,"=");
205  if (temp1 == NULL)
206  {
207 
208  printf("Error parsing ox for char %d.\n", i);
209  free(font->charmap);
210  free(font->chardata);
211  return -1;
212 
213  }
214  temp0 += strlen(temp1)+1;
215  font->chardata[i].ox = (int)strtol(temp0,NULL,10);
216 
217  temp1 = strtok(temp0,"=");
218  if (temp1 == NULL)
219  {
220 
221  printf("Error parsing oy for char %d.\n", i);
222  free(font->charmap);
223  free(font->chardata);
224  return -1;
225 
226  }
227  temp0 += strlen(temp1)+1;
228  font->chardata[i].oy = (int)strtol(temp0,NULL,10);
229 
230  temp1 = strtok(temp0,"=");
231  if (temp1 == NULL)
232  {
233 
234  printf("Error parsing Wid for char %d.\n", i);
235  free(font->charmap);
236  free(font->chardata);
237  return -1;
238 
239  }
240  temp0 += strlen(temp1)+1;
241  font->chardata[i].width = (int)strtol(temp0,NULL,10);
242 
243  temp1 = strtok(temp0,"=");
244  if (temp1 == NULL)
245  {
246 
247  printf("Error parsing Hgt for char %d.\n", i);
248  free(font->charmap);
249  free(font->chardata);
250  return -1;
251 
252  }
253  temp0 += strlen(temp1)+1;
254  font->chardata[i].height = (int)strtol(temp0,NULL,10);
255 
256  temp1 = strtok(temp0,"=");
257  if (temp1 == NULL)
258  {
259 
260  printf("Error parsing X1 for char %d.\n", i);
261  free(font->charmap);
262  free(font->chardata);
263  return -1;
264 
265  }
266  temp0 += strlen(temp1)+1;
267  font->chardata[i].u1 = ftoi4(((float)(strtod(temp0,NULL) * tex_width)));
268 
269  temp1 = strtok(temp0,"=");
270  if (temp1 == NULL)
271  {
272 
273  printf("Error parsing Y1 for char %d.\n", i);
274  free(font->charmap);
275  free(font->chardata);
276  return -1;
277 
278  }
279  temp0 += strlen(temp1)+1;
280  font->chardata[i].v1 = ftoi4(((float)(strtod(temp0,NULL) * tex_height)));
281 
282  temp1 = strtok(temp0,"=");
283  if (temp1 == NULL)
284  {
285 
286  printf("Error parsing X2 for char %d.\n", i);
287  free(font->charmap);
288  free(font->chardata);
289  return -1;
290 
291  }
292  temp0 += strlen(temp1)+1;
293  font->chardata[i].u2 = ftoi4(((float)(strtod(temp0,NULL) * tex_width)));
294 
295  temp1 = strtok(temp0,"=");
296  if (temp1 == NULL)
297  {
298 
299  printf("Error parsing Y2 for char %d.\n", i);
300  free(font->charmap);
301  free(font->chardata);
302  return -1;
303 
304  }
305  temp0 += strlen(temp1)+1;
306  font->chardata[i].v2 = ftoi4(((float)(strtod(temp0,NULL) * tex_height)));
307  }
308 
309  return 0;
310 
311 }
#define ftoi4(F)
Definition: draw_types.h:15
int totalchars
Definition: font.h:54
int spacewidth
Definition: font.h:55
Definition: font.h:36
unsigned short v2
Definition: font.h:47
char width
Definition: font.h:42
char C
Definition: font.h:39
unsigned short u1
Definition: font.h:44
char A
Definition: font.h:37
char height
Definition: font.h:43
unsigned short u2
Definition: font.h:46
char oy
Definition: font.h:41
unsigned short v1
Definition: font.h:45
char B
Definition: font.h:38
char ox
Definition: font.h:40

References inidata_t::A, inidata_t::B, inidata_t::C, fsfont_t::chardata, fsfont_t::charmap, ftoi4, inidata_t::height, NULL, inidata_t::ox, inidata_t::oy, fsfont_t::spacewidth, fsfont_t::totalchars, inidata_t::u1, inidata_t::u2, inidata_t::v1, inidata_t::v2, and inidata_t::width.

Referenced by main().

◆ fontstudio_print_string()

qword_t* fontstudio_print_string ( qword_t q,
int  context,
const unsigned char *  string,
int  alignment,
vertex_t v0,
color_t c0,
fsfont_t font 
)

Prints a unicode formatted string (UTF+8)

Definition at line 456 of file fsfont.c.

457 {
458 
459  int i = 0,j;
460 
461  unsigned short curchar = 0;
462 
463  int length;
464 
465  vertex_t v_pos = *v0;
466 
467  unsigned short utf8[2048];
468 
469  memset(utf8,0,sizeof(short)*2048);
470 
471  // Decodes the encoded string into unicode numbers U+xxxx
472  // length is the number of characters in the string
473  length = decode_unicode(str, utf8);
474 
475  //for (i = 0; i < length; i++)
476  //{
477  // printf("utf8[%d] = %d\n", i, utf8[i]);
478  //}
479 
480  // Converts the unicode numbers into the index numbers
481  // used by the FontStudio ini
482  convert_to_index(utf8,length,font);
483 
484  //for (i = 0; i < length; i++)
485  //{
486  // printf("utf8[%d] = %d\n", i, utf8[i]);
487  //}
488 
489  float line_num[100];
490  int line = 0;
491 
492  float x_orig[100];
493  x_orig[0] = 0;
494 
495  // line_num is used to keep track of number of characters per line
496  line_num[0] = 0;
497 
498  switch (alignment)
499  {
500  default:
501  case LEFT_ALIGN:
502  {
503  for (i = 0; i < length; i++)
504  {
505 
506  while (utf8[i] == TAB || utf8[i] == NEWLINE)
507  {
508  if (utf8[i] == NEWLINE)
509  {
510  x_orig[line] = v_pos.x;
511  line++;
512  line_num[line] = 0;
513  }
514  i++;
515  }
516 
517  if (i == length-1)
518  {
519  x_orig[line] = v_pos.x;
520  line++;
521  }
522 
523  }
524  break;
525 
526  }
527  case RIGHT_ALIGN:
528  {
529  for (i = 0; i < length; i++)
530  {
531 
532  while (utf8[i] == TAB || utf8[i] == NEWLINE || utf8[i] == SPACE)
533  {
534  if (utf8[i] == NEWLINE)
535  {
536  x_orig[line] = v_pos.x - line_num[line]*font->scale;
537  line++;
538  line_num[line] = 0;
539  }
540  if (utf8[i] == TAB)
541  {
542  line_num[line] += font->spacewidth * 4;
543  }
544  if (utf8[i] == SPACE)
545  {
546  line_num[line] += font->spacewidth;
547  }
548  i++;
549  }
550 
551  curchar = utf8[i];
552  line_num[line] += font->chardata[curchar].A + font->chardata[curchar].B + font->chardata[curchar].C;
553 
554  if (i == length-1)
555  {
556  x_orig[line] = v_pos.x - line_num[line]*font->scale;
557  line++;
558  }
559 
560  }
561  break;
562 
563  }
564  case CENTER_ALIGN:
565  {
566  for (i = 0; i < length; i++)
567  {
568 
569  while (utf8[i] == TAB || utf8[i] == NEWLINE || utf8[i] == SPACE)
570  {
571  if (utf8[i] == NEWLINE)
572  {
573  x_orig[line] = v_pos.x - (line_num[line]*font->scale)/2.0f;
574  line++;
575  line_num[line] = 0;
576  }
577  if (utf8[i] == TAB)
578  {
579  line_num[line] += font->spacewidth * 4;
580  }
581  if (utf8[i] == SPACE)
582  {
583  line_num[line] += font->spacewidth;
584  }
585  i++;
586  }
587 
588  curchar = utf8[i];
589  line_num[line] += font->chardata[curchar].A + font->chardata[curchar].B + font->chardata[curchar].C;
590 
591  if (i == length-1)
592  {
593  x_orig[line] = v_pos.x - (line_num[line]*font->scale)/2.0f;
594  line++;
595  }
596 
597  }
598  break;
599 
600  }
601 
602  };
603 
604  line = 0;
605  v_pos.x = x_orig[0];
606 
607  q = draw_prim_start(q,context,&charprim,c0);
608 
609  for (j = 0; j < length; j++)
610  {
611 
612  while(utf8[j] == NEWLINE || utf8[j] == TAB || utf8[j] == SPACE)
613  {
614  if (utf8[j] == NEWLINE)
615  {
616  line++;
617  v_pos.y += font->height*font->scale;
618  v_pos.x = x_orig[line];
619  }
620  if (utf8[j] == TAB)
621  {
622  v_pos.x += font->spacewidth*font->scale * 4.0f;
623  }
624  if (utf8[j] == SPACE)
625  {
626  v_pos.x += font->spacewidth*font->scale;
627  }
628  j++;
629  }
630 
631  v_pos.x += (font->chardata[utf8[j]].A*font->scale);
632 
633  q = draw_fontstudio_char(q,utf8[j],&v_pos,font);
634 
635  v_pos.x += (font->chardata[utf8[j]].B*font->scale) + (font->chardata[utf8[j]].C*font->scale);
636 
637 
638  }
639 
641 
642  return q;
643 }
#define v0
Definition: as_reg_compat.h:35
qword_t * draw_prim_start(qword_t *q, int context, prim_t *prim, color_t *color)
Definition: draw3d.c:11
#define DRAW_UV_REGLIST
Definition: draw3d.h:24
qword_t * draw_prim_end(qword_t *q, int nreg, u64 reglist)
Definition: draw3d.c:33
u8 context
Definition: main.c:71
#define RIGHT_ALIGN
Definition: font.h:62
#define LEFT_ALIGN
Definition: font.h:60
#define CENTER_ALIGN
Definition: font.h:61
int decode_unicode(const unsigned char *in, unsigned short *out)
Definition: fsfont.c:329
void convert_to_index(unsigned short *in, int num, fsfont_t *font)
Definition: fsfont.c:400
#define SPACE
Definition: fsfont.c:20
#define NEWLINE
Definition: fsfont.c:19
qword_t * draw_fontstudio_char(qword_t *q, unsigned int c, vertex_t *v0, fsfont_t *font)
Definition: fsfont.c:436
static prim_t charprim
Definition: fsfont.c:11
#define TAB
Definition: fsfont.c:18
float y
Definition: draw_types.h:53
float x
Definition: draw_types.h:52

References inidata_t::A, inidata_t::B, inidata_t::C, CENTER_ALIGN, fsfont_t::chardata, charprim, context, convert_to_index(), decode_unicode(), draw_fontstudio_char(), draw_prim_end(), draw_prim_start(), DRAW_UV_REGLIST, fsfont_t::height, LEFT_ALIGN, NEWLINE, RIGHT_ALIGN, fsfont_t::scale, SPACE, fsfont_t::spacewidth, TAB, v0, vertex_t::x, and vertex_t::y.

Referenced by run_demo().

◆ fontstudio_unload_ini()

void fontstudio_unload_ini ( fsfont_t font)

Unloads a parsed font's ini file.

Definition at line 313 of file fsfont.c.

314 {
315  if(font->charmap != NULL)
316  {
317  free(font->charmap);
318  font->charmap = NULL;
319  }
320  if(font->chardata != NULL)
321  {
322  free(font->chardata);
323  font->chardata = NULL;
324  }
325 }

References fsfont_t::chardata, fsfont_t::charmap, and NULL.

Referenced by main().

◆ fontx_load()

int fontx_load ( const char *  path,
fontx_t fontx,
int  type,
int  wmargin,
int  hmargin,
int  bold 
)

Loads a FontX2 type font with set characteristics Use "rom0:KROM" as the path to load the PS2's internal FontX2 font

Definition at line 262 of file fontx.c.

263 {
264 
265  FILE *file = NULL;
266 
267  int ret = -1;
268  long size = 0;
269 
270  fontx_hdr *fontx_header = NULL;
271 
272  if (!strcmp("rom0:KROM",path) || !strcmp("rom0:/KROM",path))
273  {
274 
275  if (type == SINGLE_BYTE)
276  {
277 
278  ret = fontx_load_single_krom(fontx);
279 
280  }
281  else
282  {
283 
284  ret = fontx_load_double_krom(fontx);
285 
286  }
287 
288  if (ret < 0)
289  {
290 
291  printf("Error opening %s\n", path);
292  return -1;
293 
294  }
295 
296  }
297 
298  else
299  {
300 
301  file = fopen(path, "r");
302 
303  if (file == NULL)
304  {
305 
306  printf("Error opening %s\n", path);
307  return -1;
308 
309  }
310 
311  // get size of file
312  fseek(file, 0, SEEK_END);
313  size = ftell(file);
314  fseek(file, 0, SEEK_SET);
315 
316  fontx->font = (char *)malloc(size);
317 
318  if (fontx->font == NULL)
319  {
320 
321  printf("Error allocating %ld bytes of memory.\n", size);
322  fclose(file);
323  return -1;
324 
325  }
326 
327  fread(fontx->font, size, 1, file);
328 
329  fclose(file);
330 
331  }
332 
333  fontx_header = (fontx_hdr*)fontx->font;
334 
335  if (strncmp(fontx_header->id, "FONTX2", 6) != 0)
336  {
337 
338  printf("Not FONTX2 type font!\n");
339  free(fontx->font);
340 
341  return -1;
342 
343  }
344 
345  if (fontx_header->type != type)
346  {
347 
348  printf("Type mismatch\n");
349  free(fontx->font);
350 
351  return -1;
352 
353  }
354 
355  // Fill in some information about the font
356  strcpy(fontx->name,fontx_header->name);
357 
358  fontx->rowsize = ((fontx_header->width+7)>>3);
359  fontx->charsize = fontx->rowsize * fontx_header->height;
360  fontx->w_margin = wmargin;
361  fontx->h_margin = hmargin;
362  fontx->bold = bold;
363 
364  // This is the offset that character data starts
365  if (fontx_header->type == SINGLE_BYTE)
366  {
367 
368  fontx->offset = 17;
369 
370  }
371  else
372  {
373 
374  // 17 + 1 + (number of tables * 4) bytes
375  fontx->offset = 18 + (fontx_header->table_num * 4);
376 
377  }
378 
379  return 0;
380 
381 }
#define SINGLE_BYTE
Definition: font.h:13
int fontx_load_single_krom(fontx_t *fontx)
Definition: fontx.c:102
int fontx_load_double_krom(fontx_t *fontx)
Definition: fontx.c:180
unsigned char width
Definition: fontx.c:24
unsigned char height
Definition: fontx.c:26
unsigned char table_num
Definition: fontx.c:31
char id[7]
Definition: fontx.c:20
char name[9]
Definition: fontx.c:22
unsigned char type
Definition: fontx.c:28
int offset
Definition: font.h:30
int charsize
Definition: font.h:28
char * font
Definition: font.h:32
char bold
Definition: font.h:20
char name[9]
Definition: font.h:18
int rowsize
Definition: font.h:26
char w_margin
Definition: font.h:22
char h_margin
Definition: font.h:24

References fontx_t::bold, fontx_t::charsize, fontx_t::font, fontx_load_double_krom(), fontx_load_single_krom(), fontx_t::h_margin, fontx_hdr::height, fontx_hdr::id, fontx_t::name, fontx_hdr::name, NULL, fontx_t::offset, fontx_t::rowsize, SINGLE_BYTE, fontx_hdr::table_num, fontx_hdr::type, fontx_t::w_margin, and fontx_hdr::width.

Referenced by main().

◆ fontx_print_ascii()

qword_t* fontx_print_ascii ( qword_t q,
int  context,
const unsigned char *  str,
int  alignment,
vertex_t v0,
color_t c0,
fontx_t fontx 
)

Prints an ascii/JISX201 formatted string

Definition at line 559 of file fontx.c.

560 {
561 
562  int i,j;
563 
564  fontx_hdr *fontx_header = (fontx_hdr*)fontx->font;
565 
566  vertex_t v_pos = *v0;
567 
568  int length = strlen((const char *)str);
569  short line_num[100];
570  int line = 0;
571 
572  float x_orig[100];
573  x_orig[0] = 0;
574 
575  float w = fontx_header->width;
576  float h = fontx_header->height;
577 
578  float wm = fontx->w_margin;
579  float hm = fontx->h_margin;
580 
581  // line_num is used to keep track of number of characters per line
582  line_num[0] = 0;
583 
584  switch (alignment)
585  {
586  default:
587  case LEFT_ALIGN:
588  {
589  for (i = 0; i < length; i++)
590  {
591 
592  while (str[i] == '\t' || str[i] == '\n')
593  {
594 
595  if(str[i] == '\n')
596  {
597  x_orig[line] = v_pos.x;
598  line++;
599  line_num[line] = 0;
600  }
601 
602  i++;
603 
604  }
605 
606 
607  if (i == length-1)
608  {
609  x_orig[line] = v_pos.x;
610  line++;
611  }
612 
613  }
614  break;
615 
616  }
617  case RIGHT_ALIGN:
618  {
619  for (i = 0; i < length; i++)
620  {
621 
622  while (str[i] == '\t' || str[i] == '\n')
623  {
624 
625  if (str[i] == '\t')
626  {
627  line_num[line] += 4;
628  }
629 
630  if (str[i] == '\n')
631  {
632  x_orig[line] = v_pos.x - (line_num[line] * (w + wm));
633  line++;
634  line_num[line] = 0;
635  }
636 
637  i++;
638 
639  }
640 
641 
642  line_num[line]++;
643 
644  if (i == length-1)
645  {
646  x_orig[line] = v_pos.x - (line_num[line] * (w + wm));
647  line++;
648  }
649 
650  }
651  break;
652 
653  }
654  case CENTER_ALIGN:
655  {
656  for (i = 0; i < length; i++)
657  {
658 
659  while (str[i] == '\t' || str[i] == '\n')
660  {
661 
662  if (str[i] == '\t')
663  {
664  line_num[line] += 4;
665  }
666 
667  if (str[i] == '\n')
668  {
669  x_orig[line] = v_pos.x - (line_num[line] * (w + wm))/2.0f;
670  line++;
671  line_num[line] = 0;
672  }
673 
674  i++;
675 
676  }
677 
678  line_num[line]++;
679 
680  if (i == length-1)
681  {
682  x_orig[line] = v_pos.x - (line_num[line] * (w + wm))/2.0f;
683  line++;
684  }
685 
686  }
687  break;
688 
689  }
690 
691  };
692 
693  line = 0;
694  v_pos.x = x_orig[0];
695 
696  q = draw_prim_start(q,context,&charprim,c0);
697 
698  for (j = 0; j < length; j++)
699  {
700 
701  while(str[j] == '\n' || str[j] == '\t')
702  {
703  if (str[j] == '\n')
704  {
705  line++;
706  v_pos.y += h + hm;
707  v_pos.x = x_orig[line];
708  }
709  if (str[j] == '\t')
710  {
711  v_pos.x += w * 5.0f;
712  }
713  j++;
714  }
715 
716  if (str[j] < 0x80)
717  {
718  q = draw_fontx_char(q,str[j],&v_pos,fontx);
719  }
720  else if (str[j] >= 0xA1 && str[j] <= 0xDF)
721  {
722  q = draw_fontx_char(q,str[j],&v_pos,fontx);
723  }
724 
725  v_pos.x += w + wm;
726 
727  }
728 
730 
731  return q;
732 
733 }
#define DRAW_XYZ_REGLIST
Definition: draw3d.h:16
static prim_t charprim
Definition: fontx.c:40
qword_t * draw_fontx_char(qword_t *q, unsigned short c, vertex_t *v0, fontx_t *fontx)
Definition: fontx.c:502

References CENTER_ALIGN, charprim, context, draw_fontx_char(), draw_prim_end(), draw_prim_start(), DRAW_XYZ_REGLIST, fontx_t::font, fontx_t::h_margin, fontx_hdr::height, LEFT_ALIGN, RIGHT_ALIGN, v0, fontx_t::w_margin, and fontx_hdr::width.

◆ fontx_print_sjis()

qword_t* fontx_print_sjis ( qword_t q,
int  context,
const unsigned char *  str,
int  alignment,
vertex_t v0,
color_t c0,
fontx_t ascii,
fontx_t kanji 
)

Prints a SJIS formatted string

Definition at line 735 of file fontx.c.

736 {
737 
738  int i,j;
739 
740  fontx_hdr *ascii_header = (fontx_hdr*)ascii->font;
741  fontx_hdr *kanji_header = (fontx_hdr*)kanji->font;
742 
743  vertex_t v_pos = *v0;
744 
745  int length = strlen((const char *)str);
746 
747  int line = 0;
748  short halfwidth[100];
749  short fullwidth[100];
750  float x_orig[100];
751  x_orig[0] = 0;
752 
753  unsigned short wide;
754 
755  float hw = ascii_header->width;
756 
757  float fw = kanji_header->width;
758  float h = kanji_header->height;
759 
760  float wm = kanji->w_margin;
761  float hm = kanji->h_margin;
762 
763  // line_num is used to keep track of number of characters per line
764  halfwidth[0] = 0;
765  fullwidth[0] = 0;
766 
767  switch (alignment)
768  {
769  default:
770  case LEFT_ALIGN:
771  {
772  for (i = 0; i < length; i++)
773  {
774 
775  while (str[i] == '\t'|| str[i] == '\n')
776  {
777  if (str[i] == '\t')
778  {
779  halfwidth[line] += 4;
780  }
781  if (str[i] == '\n')
782  {
783  x_orig[line] = v_pos.x;
784  line++;
785  halfwidth[line] = 0;
786  fullwidth[line] = 0;
787  }
788  i++;
789  }
790 
791 
792  if (i == length-1)
793  {
794  x_orig[line] = v_pos.x;
795  line++;
796  }
797 
798  }
799  break;
800 
801  }
802  case RIGHT_ALIGN:
803  {
804  for (i = 0; i < length; i++)
805  {
806 
807  while (str[i] == '\t'|| str[i] == '\n')
808  {
809  if (str[i] == '\t')
810  {
811  halfwidth[line] += 4;
812  }
813  if (str[i] == '\n')
814  {
815  x_orig[line] = v_pos.x - ((halfwidth[line] * (hw+wm)) + (fullwidth[line] * (fw + wm)));;
816  line++;
817  halfwidth[line] = 0;
818  fullwidth[line] = 0;
819  }
820  i++;
821  }
822 
823  if (str[i] < 0x80)
824  {
825  halfwidth[line]++;
826  }
827  else if (str[i] >= 0xA1 && str[i] <= 0xDF)
828  {
829  halfwidth[line]++;
830  }
831  else if (str[i] >= 0x81 && str[i] <= 0x9F)
832  {
833  fullwidth[line]++;
834  }
835  else if (str[i] >= 0xE0 && str[i] <= 0xEF)
836  {
837  fullwidth[line]++;
838  }
839 
840  if (i == length-1)
841  {
842  x_orig[line] = v_pos.x - ((halfwidth[line] * (hw+wm)) + (fullwidth[line] * (fw + wm)));
843  line++;
844  }
845 
846  }
847  break;
848 
849  }
850  case CENTER_ALIGN:
851  {
852  for (i = 0; i < length; i++)
853  {
854 
855  while (str[i] == '\t'|| str[i] == '\n')
856  {
857  if (str[i] == '\t')
858  {
859  halfwidth[line] += 4;
860  }
861  if (str[i] == '\n')
862  {
863  x_orig[line] = v_pos.x - ((halfwidth[line] * (hw+wm)) + (fullwidth[line] * (fw + wm)))/2.0f;
864  line++;
865  halfwidth[line] = 0;
866  fullwidth[line] = 0;
867  }
868  i++;
869  }
870 
871  if (str[i] < 0x80)
872  {
873  halfwidth[line]++;
874  }
875  else if (str[i] >= 0xA1 && str[i] <= 0xDF)
876  {
877  halfwidth[line]++;
878  }
879  else if (str[i] >= 0x81 && str[i] <= 0x9F)
880  {
881  fullwidth[line]++;
882  }
883  else if (str[i] >= 0xE0 && str[i] <= 0xEF)
884  {
885  fullwidth[line]++;
886  }
887 
888  if (i == length-1)
889  {
890  x_orig[line] = v_pos.x - ((halfwidth[line] * (hw+wm)) + (fullwidth[line] * (fw + wm)))/2.0f;
891  line++;
892  }
893 
894  }
895  break;
896 
897  }
898 
899  };
900 
901  line = 0;
902  v_pos.x = x_orig[0];
903 
904  q = draw_prim_start(q,context,&charprim,c0);
905 
906  for (j = 0; j < length; j++)
907  {
908 
909  wide = 0;
910 
911  while(str[j] == '\n' || str[j] == '\t')
912  {
913  if (str[j] == '\n')
914  {
915  line++;
916  v_pos.y += h + hm;
917  v_pos.x = x_orig[line];
918  }
919  if (str[j] == '\t')
920  {
921  v_pos.x += hw * 5.0f;
922  }
923  j++;
924  }
925 
926  if (str[j] < 0x80)
927  {
928  q = draw_fontx_char(q,str[j],&v_pos,ascii);
929  v_pos.x += hw + wm;
930  }
931  else if (str[j] >= 0xA1 && str[j] <= 0xDF)
932  {
933  q = draw_fontx_char(q,str[j],&v_pos,ascii);
934  v_pos.x += hw + wm;
935  }
936  else if (str[j] >= 0x81 && str[j] <= 0x9F)
937  {
938  wide = str[j++]<<8;
939  wide += str[j];
940  q = draw_fontx_char(q,wide,&v_pos,kanji);
941  v_pos.x += fw + wm;
942  }
943  else if (str[j] >= 0xE0 && str[j] <= 0xEF)
944  {
945  wide = str[j++]<<8;
946  wide += str[j];
947  q = draw_fontx_char(q,wide,&v_pos,kanji);
948  v_pos.x += fw + wm;
949  }
950  else
951  {
952  v_pos.x += fw + wm;
953  }
954 
955  }
956 
958 
959  return q;
960 
961 }

References CENTER_ALIGN, charprim, context, draw_fontx_char(), draw_prim_end(), draw_prim_start(), DRAW_XYZ_REGLIST, fontx_t::font, fontx_t::h_margin, LEFT_ALIGN, RIGHT_ALIGN, v0, fontx_t::w_margin, fontx_hdr::width, vertex_t::x, and vertex_t::y.

Referenced by run_demo().

◆ fontx_unload()

void fontx_unload ( fontx_t fontx)

Frees memory if a font is loaded

Definition at line 383 of file fontx.c.

384 {
385 
386  if (fontx->font != NULL)
387  {
388 
389  free(fontx->font);
390 
391  }
392 
393 }

References fontx_t::font, and NULL.

Referenced by main().