/
rend_font.cpp
1572 lines (1366 loc) · 42.3 KB
/
rend_font.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file rend_font.cpp
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
/**
* Font Renderer.
*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define DENG_NO_API_MACROS_FONT_RENDER
#include "de_base.h"
#include "de_console.h"
#include "de_graphics.h"
#include "de_system.h"
#include "de_ui.h"
#include "render/rend_font.h"
#include "render/rend_list.h"
#include "resource/font.h"
#include "resource/fonts.h"
#include "resource/bitmapfont.h"
#include "m_misc.h"
/**
* @ingroup drawTextFlags
* @{
*/
#define DTF_INTERNAL_MASK 0xff00
#define DTF_NO_CHARACTER 0x8000 /// Only draw text decorations.
/**@}*/
typedef struct fr_state_attributes_s {
int tracking;
float leading;
float rgba[4];
int shadowOffsetX, shadowOffsetY;
float shadowStrength;
float glitterStrength;
boolean caseScale;
} fr_state_attributes_t;
// Used with FR_LoadDefaultAttribs
static fr_state_attributes_t defaultAttribs = {
FR_DEF_ATTRIB_TRACKING,
FR_DEF_ATTRIB_LEADING,
{ FR_DEF_ATTRIB_COLOR_RED, FR_DEF_ATTRIB_COLOR_GREEN, FR_DEF_ATTRIB_COLOR_BLUE, FR_DEF_ATTRIB_ALPHA },
FR_DEF_ATTRIB_SHADOW_XOFFSET,
FR_DEF_ATTRIB_SHADOW_YOFFSET,
FR_DEF_ATTRIB_SHADOW_STRENGTH,
FR_DEF_ATTRIB_GLITTER_STRENGTH,
FR_DEF_ATTRIB_CASE_SCALE
};
typedef struct {
fontid_t fontNum;
int attribStackDepth;
fr_state_attributes_t attribStack[FR_MAX_ATTRIB_STACK_DEPTH];
} fr_state_t;
static fr_state_t fr;
fr_state_t* frState = &fr;
typedef struct {
fontid_t fontNum;
float scaleX, scaleY;
float offX, offY;
float angle;
float rgba[4];
float glitterStrength, shadowStrength;
int shadowOffsetX, shadowOffsetY;
int tracking;
float leading;
int lastLineHeight;
boolean typeIn;
boolean caseScale;
struct {
float scale, offset;
} caseMod[2]; // 1=upper, 0=lower
} drawtextstate_t;
static __inline fr_state_attributes_t* currentAttribs(void);
static int topToAscent(font_t* font);
static int lineHeight(font_t* font, unsigned char ch);
static void drawChar(unsigned char ch, int posX, int posY, font_t* font, int alignFlags, short textFlags);
static void drawFlash(const Point2Raw* origin, const Size2Raw* size, int bright);
static int inited = false;
static char smallTextBuffer[FR_SMALL_TEXT_BUFFER_SIZE+1];
static char* largeTextBuffer = NULL;
static size_t largeTextBufferSize = 0;
static int typeInTime;
static void errorIfNotInited(const char* callerName)
{
if(inited) return;
Con_Error("%s: font renderer module is not presently initialized.", callerName);
// Unreachable. Prevents static analysers from getting rather confused, poor things.
exit(1);
}
static int topToAscent(font_t* font)
{
int lineHeight = Fonts_Leading(font);
if(lineHeight == 0)
return 0;
return lineHeight - Fonts_Ascent(font);
}
static int lineHeight(font_t* font, unsigned char ch)
{
int ascent = Fonts_Ascent(font);
if(ascent != 0)
return ascent;
return Fonts_CharHeight(font, ch);
}
static __inline fr_state_attributes_t* currentAttribs(void)
{
return fr.attribStack + fr.attribStackDepth;
}
void FR_Shutdown(void)
{
if(!inited) return;
inited = false;
}
boolean FR_Available(void)
{
return inited;
}
void FR_Ticker(timespan_t /*ticLength*/)
{
if(!inited)
return;
// Restricted to fixed 35 Hz ticks.
/// @todo We should not be synced to the games' fixed "sharp" timing.
/// This font renderer is used by the engine's UI also.
if(!DD_IsSharpTick())
return; // It's too soon.
++typeInTime;
}
void FR_ResetTypeinTimer(void)
{
errorIfNotInited("FR_ResetTypeinTimer");
typeInTime = 0;
}
void FR_SetFont(fontid_t num)
{
errorIfNotInited("FR_SetFont");
if(!Fonts_ToFont(num))
{
//LegacyCore_PrintfLogFragmentAtLevel(DE2_LOG_WARNING, "Requested invalid font %i.\n", num);
return; // No such font.
}
fr.fontNum = num;
}
void FR_SetNoFont(void)
{
errorIfNotInited("FR_SetNoFont");
fr.fontNum = 0;
}
fontid_t FR_Font(void)
{
errorIfNotInited("FR_Font");
return fr.fontNum;
}
void FR_LoadDefaultAttrib(void)
{
errorIfNotInited("FR_LoadDefaultAttrib");
memcpy(currentAttribs(), &defaultAttribs, sizeof(defaultAttribs));
}
void FR_PushAttrib(void)
{
errorIfNotInited("FR_PushAttrib");
if(fr.attribStackDepth+1 == FR_MAX_ATTRIB_STACK_DEPTH)
{
Con_Error("FR_PushAttrib: STACK_OVERFLOW.");
exit(1); // Unreachable.
}
memcpy(fr.attribStack + fr.attribStackDepth + 1, fr.attribStack + fr.attribStackDepth, sizeof(fr.attribStack[0]));
++fr.attribStackDepth;
}
void FR_PopAttrib(void)
{
errorIfNotInited("FR_PopAttrib");
if(fr.attribStackDepth == 0)
{
Con_Error("FR_PopAttrib: STACK_UNDERFLOW.");
exit(1); // Unreachable.
}
--fr.attribStackDepth;
}
float FR_Leading(void)
{
errorIfNotInited("FR_Leading");
return currentAttribs()->leading;
}
void FR_SetLeading(float value)
{
errorIfNotInited("FR_SetLeading");
currentAttribs()->leading = value;
}
int FR_Tracking(void)
{
errorIfNotInited("FR_Tracking");
return currentAttribs()->tracking;
}
void FR_SetTracking(int value)
{
errorIfNotInited("FR_SetTracking");
currentAttribs()->tracking = value;
}
void FR_ColorAndAlpha(float rgba[4])
{
errorIfNotInited("FR_ColorAndAlpha");
memcpy(rgba, currentAttribs()->rgba, sizeof(rgba[0]) * 4);
}
void FR_SetColor(float red, float green, float blue)
{
fr_state_attributes_t* sat = currentAttribs();
errorIfNotInited("FR_SetColor");
sat->rgba[CR] = red;
sat->rgba[CG] = green;
sat->rgba[CB] = blue;
}
void FR_SetColorv(const float rgba[3])
{
fr_state_attributes_t* sat = currentAttribs();
errorIfNotInited("FR_SetColorv");
sat->rgba[CR] = rgba[CR];
sat->rgba[CG] = rgba[CG];
sat->rgba[CB] = rgba[CB];
}
void FR_SetColorAndAlpha(float red, float green, float blue, float alpha)
{
fr_state_attributes_t* sat = currentAttribs();
errorIfNotInited("FR_SetColorAndAlpha");
sat->rgba[CR] = red;
sat->rgba[CG] = green;
sat->rgba[CB] = blue;
sat->rgba[CA] = alpha;
}
void FR_SetColorAndAlphav(const float rgba[4])
{
fr_state_attributes_t* sat = currentAttribs();
errorIfNotInited("FR_SetColorAndAlphav");
sat->rgba[CR] = rgba[CR];
sat->rgba[CG] = rgba[CG];
sat->rgba[CB] = rgba[CB];
sat->rgba[CA] = rgba[CA];
}
float FR_ColorRed(void)
{
errorIfNotInited("FR_ColorRed");
return currentAttribs()->rgba[CR];
}
void FR_SetColorRed(float value)
{
errorIfNotInited("FR_SetColorRed");
currentAttribs()->rgba[CR] = value;
}
float FR_ColorGreen(void)
{
errorIfNotInited("FR_ColorGreen");
return currentAttribs()->rgba[CG];
}
void FR_SetColorGreen(float value)
{
errorIfNotInited("FR_SetColorGreen");
currentAttribs()->rgba[CG] = value;
}
float FR_ColorBlue(void)
{
errorIfNotInited("FR_ColorBlue");
return currentAttribs()->rgba[CB];
}
void FR_SetColorBlue(float value)
{
errorIfNotInited("FR_SetColorBlue");
currentAttribs()->rgba[CB] = value;
}
float FR_Alpha(void)
{
errorIfNotInited("FR_Alpha");
return currentAttribs()->rgba[CA];
}
void FR_SetAlpha(float value)
{
errorIfNotInited("FR_SetAlpha");
currentAttribs()->rgba[CA] = value;
}
void FR_ShadowOffset(int* offsetX, int* offsetY)
{
fr_state_attributes_t* sat = currentAttribs();
errorIfNotInited("FR_ShadowOffset");
if(NULL != offsetX) *offsetX = sat->shadowOffsetX;
if(NULL != offsetY) *offsetY = sat->shadowOffsetY;
}
void FR_SetShadowOffset(int offsetX, int offsetY)
{
fr_state_attributes_t* sat = currentAttribs();
errorIfNotInited("FR_SetShadowOffset");
sat->shadowOffsetX = offsetX;
sat->shadowOffsetY = offsetY;
}
float FR_ShadowStrength(void)
{
errorIfNotInited("FR_ShadowStrength");
return currentAttribs()->shadowStrength;
}
void FR_SetShadowStrength(float value)
{
errorIfNotInited("FR_SetShadowStrength");
currentAttribs()->shadowStrength = MINMAX_OF(0, value, 1);
}
float FR_GlitterStrength(void)
{
errorIfNotInited("FR_GlitterStrength");
return currentAttribs()->glitterStrength;
}
void FR_SetGlitterStrength(float value)
{
errorIfNotInited("FR_SetGlitterStrength");
currentAttribs()->glitterStrength = MINMAX_OF(0, value, 1);
}
boolean FR_CaseScale(void)
{
errorIfNotInited("FR_CaseScale");
return currentAttribs()->caseScale;
}
void FR_SetCaseScale(boolean value)
{
errorIfNotInited("FR_SetCaseScale");
currentAttribs()->caseScale = value;
}
void FR_CharSize(Size2Raw* size, unsigned char ch)
{
errorIfNotInited("FR_CharSize");
Fonts_CharSize(Fonts_ToFont(fr.fontNum), size, ch);
}
int FR_CharWidth(unsigned char ch)
{
errorIfNotInited("FR_CharWidth");
if(fr.fontNum != 0)
return Fonts_CharWidth(Fonts_ToFont(fr.fontNum), ch);
return 0;
}
int FR_CharHeight(unsigned char ch)
{
errorIfNotInited("FR_CharHeight");
if(fr.fontNum != 0)
return Fonts_CharHeight(Fonts_ToFont(fr.fontNum), ch);
return 0;
}
int FR_SingleLineHeight(const char* text)
{
errorIfNotInited("FR_SingleLineHeight");
if(fr.fontNum == 0 || !text)
return 0;
{ int ascent = Fonts_Ascent(Fonts_ToFont(fr.fontNum));
if(ascent != 0)
return ascent;
}
return Fonts_CharHeight(Fonts_ToFont(fr.fontNum), (unsigned char)text[0]);
}
int FR_GlyphTopToAscent(const char* text)
{
int lineHeight;
errorIfNotInited("FR_GlyphTopToAscent");
if(fr.fontNum == 0 || !text)
return 0;
lineHeight = Fonts_Leading(Fonts_ToFont(fr.fontNum));
if(lineHeight == 0)
return 0;
return lineHeight - Fonts_Ascent(Fonts_ToFont(fr.fontNum));
}
static int textFragmentWidth(const char* fragment)
{
assert(NULL != fragment);
{
size_t i, len;
int width = 0;
const char* ch;
unsigned char c;
if(fr.fontNum == 0)
{
Con_Error("textFragmentHeight: Cannot determine height without a current font.");
exit(1);
}
// Just add them together.
len = strlen(fragment);
i = 0;
ch = fragment;
while(i++ < len && (c = *ch++) != 0 && c != '\n')
width += FR_CharWidth(c);
return (int) (width + currentAttribs()->tracking * (len-1));
}
}
static int textFragmentHeight(const char* fragment)
{
assert(NULL != fragment);
{
const char* ch;
unsigned char c;
int height = 0;
size_t len;
uint i;
if(fr.fontNum == 0)
{
Con_Error("textFragmentHeight: Cannot determine height without a current font.");
exit(1);
}
// Find the greatest height.
i = 0;
height = 0;
len = strlen(fragment);
ch = fragment;
while(i++ < len && (c = *ch++) != 0 && c != '\n')
{
height = MAX_OF(height, FR_CharHeight(c));
}
return topToAscent(Fonts_ToFont(fr.fontNum)) + height;
}
}
/*
static void textFragmentSize(int* width, int* height, const char* fragment)
{
if(width) *width = textFragmentWidth(fragment);
if(height) *height = textFragmentHeight(fragment);
}
*/
static void textFragmentDrawer(const char* fragment, int x, int y, int alignFlags,
short textFlags, int initialCount)
{
assert(fragment && fragment[0]);
{
font_t* font = Fonts_ToFont(fr.fontNum);
fr_state_attributes_t* sat = currentAttribs();
boolean noTypein = (textFlags & DTF_NO_TYPEIN) != 0;
boolean noGlitter = (sat->glitterStrength <= 0 || (textFlags & DTF_NO_GLITTER) != 0);
boolean noShadow = (sat->shadowStrength <= 0 || (textFlags & DTF_NO_SHADOW) != 0 ||
(Font_Flags(font) & FF_SHADOWED) != 0);
boolean noCharacter = (textFlags & DTF_NO_CHARACTER) != 0;
float glitter = (noGlitter? 0 : sat->glitterStrength), glitterMul;
float shadow = (noShadow ? 0 : sat->shadowStrength), shadowMul;
float flashColor[3] = { 0, 0, 0 };
int w, h, cx, cy, count, yoff;
unsigned char c;
const char* ch;
if(alignFlags & ALIGN_RIGHT)
x -= textFragmentWidth(fragment);
else if(!(alignFlags & ALIGN_LEFT))
x -= textFragmentWidth(fragment)/2;
if(alignFlags & ALIGN_BOTTOM)
y -= textFragmentHeight(fragment);
else if(!(alignFlags & ALIGN_TOP))
y -= textFragmentHeight(fragment)/2;
if(!(noTypein && noGlitter))
{
flashColor[CR] = (1 + 2 * sat->rgba[CR]) / 3;
flashColor[CG] = (1 + 2 * sat->rgba[CG]) / 3;
flashColor[CB] = (1 + 2 * sat->rgba[CB]) / 3;
}
if(renderWireframe > 1)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_TEXTURE_2D);
}
if(Font_Type(font) == FT_BITMAP && 0 != BitmapFont_GLTextureName(font))
{
GL_BindTextureUnmanaged(BitmapFont_GLTextureName(font), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
filterUI? GL_LINEAR : GL_NEAREST);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glScalef(1.f / BitmapFont_TextureWidth(font),
1.f / BitmapFont_TextureHeight(font), 1.f);
}
{ int pass;
for(pass = (noShadow? 1 : 0); pass < (noCharacter && noGlitter? 1 : 2); ++pass)
{
count = initialCount;
ch = fragment;
cx = x + (pass == 0? sat->shadowOffsetX : 0);
cy = y + (pass == 0? sat->shadowOffsetY : 0);
for(;;)
{
c = *ch++;
yoff = 0;
glitter = (noGlitter? 0 : sat->glitterStrength);
glitterMul = 0;
shadow = (noShadow? 0 : sat->shadowStrength);
shadowMul = (noShadow? 0 : sat->rgba[CA]);
// Do the type-in effect?
if(!noTypein && (pass || (!noShadow && !pass)))
{
int maxCount = (typeInTime > 0? typeInTime * 2 : 0);
if(pass)
{
if(!noGlitter)
{
if(count == maxCount)
{
glitterMul = 1;
flashColor[CR] = sat->rgba[CR];
flashColor[CG] = sat->rgba[CG];
flashColor[CB] = sat->rgba[CB];
}
else if(count + 1 == maxCount)
{
glitterMul = 0.88f;
flashColor[CR] = (1 + sat->rgba[CR]) / 2;
flashColor[CG] = (1 + sat->rgba[CG]) / 2;
flashColor[CB] = (1 + sat->rgba[CB]) / 2;
}
else if(count + 2 == maxCount)
{
glitterMul = 0.75f;
flashColor[CR] = sat->rgba[CR];
flashColor[CG] = sat->rgba[CG];
flashColor[CB] = sat->rgba[CB];
}
else if(count + 3 == maxCount)
{
glitterMul = 0.5f;
flashColor[CR] = sat->rgba[CR];
flashColor[CG] = sat->rgba[CG];
flashColor[CB] = sat->rgba[CB];
}
else if(count > maxCount)
{
break;
}
}
else if(count > maxCount)
{
break;
}
}
else
{
if(count == maxCount)
{
shadowMul = 0;
}
else if(count + 1 == maxCount)
{
shadowMul *= .25f;
}
else if(count + 2 == maxCount)
{
shadowMul *= .5f;
}
else if(count + 3 == maxCount)
{
shadowMul *= .75f;
}
else if(count > maxCount)
{
break;
}
}
}
count++;
if(!c || c == '\n')
break;
w = FR_CharWidth(c);
h = FR_CharHeight(c);
if(' ' != c)
{
// A non-white-space character we have a glyph for.
if(pass)
{
if(!noCharacter)
{
// The character itself.
glColor4fv(sat->rgba);
drawChar(c, cx, cy + yoff, font, ALIGN_TOPLEFT, DTF_NO_EFFECTS);
}
if(!noGlitter && glitter > 0)
{
// Do something flashy.
Point2Raw origin;
Size2Raw size;
origin.x = cx;
origin.y = cy + yoff;
size.width = w;
size.height = h;
glColor4f(flashColor[CR], flashColor[CG], flashColor[CB], glitter * glitterMul);
drawFlash(&origin, &size, true);
}
}
else if(!noShadow)
{
Point2Raw origin;
Size2Raw size;
origin.x = cx;
origin.y = cy + yoff;
size.width = w;
size.height = h;
glColor4f(1, 1, 1, shadow * shadowMul);
drawFlash(&origin, &size, false);
}
}
cx += w + sat->tracking;
}
}}
// Restore previous GL-state.
if(renderWireframe > 1)
{
/// @todo do not assume previous state.
glEnable(GL_TEXTURE_2D);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if(Font_Type(font) == FT_BITMAP && 0 != BitmapFont_GLTextureName(font))
{
glMatrixMode(GL_TEXTURE);
glPopMatrix();
}
}
}
static void drawChar(unsigned char ch, int posX, int posY, font_t* font,
int alignFlags, short /*textFlags*/)
{
float x = (float) posX, y = (float) posY;
Point2Raw coords[4];
RectRaw geometry;
if(alignFlags & ALIGN_RIGHT)
x -= Fonts_CharWidth(font, ch);
else if(!(alignFlags & ALIGN_LEFT))
x -= Fonts_CharWidth(font, ch) / 2;
if(alignFlags & ALIGN_BOTTOM)
y -= topToAscent(font) + lineHeight(font, ch);
else if(!(alignFlags & ALIGN_TOP))
y -= (topToAscent(font) + lineHeight(font, ch))/2;
glMatrixMode(GL_MODELVIEW);
glTranslatef(x, y, 0);
switch(Font_Type(font))
{
case FT_BITMAP:
/// @todo Filtering should be determined at a higher level.
/// @todo We should not need to re-bind this texture here.
GL_BindTextureUnmanaged(BitmapFont_GLTextureName(font), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
filterUI? GL_LINEAR : GL_NEAREST);
std::memcpy(&geometry, BitmapFont_CharGeometry(font, ch), sizeof(geometry));
BitmapFont_CharCoords(font, ch, coords);
break;
case FT_BITMAPCOMPOSITE: {
uint8_t const border = BitmapCompositeFont_CharBorder(font, ch);
GL_BindTexture(BitmapCompositeFont_CharTexture(font, ch));
std::memcpy(&geometry, BitmapCompositeFont_CharGeometry(font, ch), sizeof(geometry));
if(border)
{
geometry.origin.x -= border;
geometry.origin.y -= border;
geometry.size.width += border*2;
geometry.size.height += border*2;
}
BitmapCompositeFont_CharCoords(font, ch, coords);
break; }
default:
Con_Error("FR_DrawChar: Invalid font type %i.", (int) Font_Type(font));
exit(1); // Unreachable.
}
if(font->_marginWidth)
{
geometry.origin.x -= font->_marginWidth;
geometry.size.width += font->_marginWidth*2;
}
if(font->_marginHeight)
{
geometry.origin.y -= font->_marginHeight;
geometry.size.height += font->_marginHeight*2;
}
GL_DrawRectWithCoords(&geometry, coords);
if(Font_Type(font) == FT_BITMAPCOMPOSITE)
{
GL_SetNoTexture();
}
glMatrixMode(GL_MODELVIEW);
glTranslatef(-x, -y, 0);
}
static void drawFlash(const Point2Raw* origin, const Size2Raw* size, int bright)
{
float fsize = 4.f + bright;
float fw = fsize * size->width / 2.0f;
float fh = fsize * size->height / 2.0f;
int x, y, w, h;
// Don't draw anything for very small letters.
if(size->height <= 4) return;
x = origin->x + (int) (size->width / 2.0f - fw / 2);
y = origin->y + (int) (size->height / 2.0f - fh / 2);
w = (int) fw;
h = (int) fh;
GL_BindTextureUnmanaged(GL_PrepareLSTexture(LST_DYNAMIC),
GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
if(bright)
GL_BlendMode(BM_ADD);
else
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + w, y);
glTexCoord2f(1, 1);
glVertex2f(x + w, y + h);
glTexCoord2f(0, 1);
glVertex2f(x, y + h);
glEnd();
GL_BlendMode(BM_NORMAL);
}
/**
* Expected:
* <pre>
* "<whitespace> = <whitespace> <float>"
* </pre>
*/
static float parseFloat(char** str)
{
float value;
char* end;
*str = M_SkipWhite(*str);
if(**str != '=') return 0; // Now I'm confused!
*str = M_SkipWhite(*str + 1);
value = (float) strtod(*str, &end);
*str = end;
return value;
}
/**
* Expected:
* <pre>
* "<whitespace> = <whitespace> [|"]<string>[|"]"
* </pre>
*/
static boolean parseString(char** str, char* buf, size_t bufLen)
{
size_t len;
char* end;
if(!buf || bufLen == 0) return false;
*str = M_SkipWhite(*str);
if(**str != '=') return false; // Now I'm confused!
// Skip over any leading whitespace.
*str = M_SkipWhite(*str + 1);
// Skip over any opening '"' character.
if(**str == '"') (*str)++;
// Find the end of the string.
end = *str;
while(*end && *end != '}' && *end != ',' && *end !='"') { end++; }
len = end - *str;
if(len != 0)
{
dd_snprintf(buf, MIN_OF(len+1, bufLen), "%s", *str);
*str = end;
}
// Skip over any closing '"' character.
if(**str == '"')
(*str)++;
return true;
}
static void parseParamaterBlock(char** strPtr, drawtextstate_t* state, int* numBreaks)
{
(*strPtr)++;
while(*(*strPtr) && *(*strPtr) != '}')
{
(*strPtr) = M_SkipWhite((*strPtr));
// What do we have here?
if(!strnicmp((*strPtr), "flash", 5))
{
(*strPtr) += 5;
state->typeIn = true;
}
else if(!strnicmp((*strPtr), "noflash", 7))
{
(*strPtr) += 7;
state->typeIn = false;
}
else if(!strnicmp((*strPtr), "case", 4))
{
(*strPtr) += 4;
state->caseScale = true;
}
else if(!strnicmp((*strPtr), "nocase", 6))
{
(*strPtr) += 6;
state->caseScale = false;
}
else if(!strnicmp((*strPtr), "ups", 3))
{
(*strPtr) += 3;
state->caseMod[1].scale = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "upo", 3))
{
(*strPtr) += 3;
state->caseMod[1].offset = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "los", 3))
{
(*strPtr) += 3;
state->caseMod[0].scale = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "loo", 3))
{
(*strPtr) += 3;
state->caseMod[0].offset = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "break", 5))
{
(*strPtr) += 5;
++(*numBreaks);
}
else if(!strnicmp((*strPtr), "r", 1))
{
(*strPtr)++;
state->rgba[CR] = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "g", 1))
{
(*strPtr)++;
state->rgba[CG] = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "b", 1))
{
(*strPtr)++;
state->rgba[CB] = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "a", 1))
{
(*strPtr)++;
state->rgba[CA] = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "x", 1))
{
(*strPtr)++;
state->offX = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "y", 1))
{
(*strPtr)++;
state->offY = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "scalex", 6))
{
(*strPtr) += 6;
state->scaleX = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "scaley", 6))
{
(*strPtr) += 6;
state->scaleY = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "scale", 5))
{
(*strPtr) += 5;
state->scaleX = state->scaleY = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "angle", 5))
{
(*strPtr) += 5;
state->angle = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "glitter", 7))
{
(*strPtr) += 7;
state->glitterStrength = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "shadow", 6))
{
(*strPtr) += 6;
state->shadowStrength = parseFloat(&(*strPtr));
}
else if(!strnicmp((*strPtr), "tracking", 8))