-
Notifications
You must be signed in to change notification settings - Fork 606
/
tr_font.cpp
1903 lines (1624 loc) · 56.6 KB
/
tr_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
/*
===========================================================================
Copyright (C) 2000 - 2013, Raven Software, Inc.
Copyright (C) 2001 - 2013, Activision, Inc.
Copyright (C) 2013 - 2015, OpenJK contributors
This file is part of the OpenJK source code.
OpenJK is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
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/>.
===========================================================================
*/
#include "qcommon/sstring.h" // stl string class won't compile in here (MS shite), so use Gil's.
#include "tr_local.h"
#include "tr_font.h"
#include "qcommon/stringed_ingame.h"
cvar_t *r_fontSharpness;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This file is shared in the single and multiplayer codebases, so be CAREFUL WHAT YOU ADD/CHANGE!!!!!
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef enum
{
eWestern, // ( I only care about asian languages in here at the moment )
eRussian, // .. but now I need to care about this, since it uses a different TP
ePolish, // ditto
eKorean,
eTaiwanese, // 15x15 glyphs tucked against BR of 16x16 space
eJapanese, // 15x15 glyphs tucked against TL of 16x16 space
eChinese, // 15x15 glyphs tucked against TL of 16x16 space
eThai, // 16x16 cells with glyphs against left edge, special file (tha_widths.dat) for variable widths
} Language_e;
// this is to cut down on all the stupid string compares I've been doing, and convert asian stuff to switch-case
//
Language_e GetLanguageEnum()
{
static int iSE_Language_ModificationCount = -1234; // any old silly value that won't match the cvar mod count
static Language_e eLanguage = eWestern;
// only re-strcmp() when language string has changed from what we knew it as...
//
if (iSE_Language_ModificationCount != se_language->modificationCount )
{
iSE_Language_ModificationCount = se_language->modificationCount;
if ( Language_IsRussian() ) eLanguage = eRussian;
else if ( Language_IsPolish() ) eLanguage = ePolish;
else if ( Language_IsKorean() ) eLanguage = eKorean;
else if ( Language_IsTaiwanese() ) eLanguage = eTaiwanese;
else if ( Language_IsJapanese() ) eLanguage = eJapanese;
else if ( Language_IsChinese() ) eLanguage = eChinese;
else if ( Language_IsThai() ) eLanguage = eThai;
else eLanguage = eWestern;
}
return eLanguage;
}
struct SBCSOverrideLanguages_t
{
const char *m_psName;
Language_e m_eLanguage;
};
// so I can do some stuff with for-next loops when I add polish etc...
//
SBCSOverrideLanguages_t g_SBCSOverrideLanguages[]=
{
{"russian", eRussian},
{"polish", ePolish},
{NULL, eWestern}
};
//================================================
//
#define sFILENAME_THAI_WIDTHS "fonts/tha_widths.dat"
#define sFILENAME_THAI_CODES "fonts/tha_codes.dat"
struct ThaiCodes_t
{
std::map <int, int> m_mapValidCodes;
std::vector<int> m_viGlyphWidths;
sstring_t m_strInitFailureReason; // so we don't have to keep retrying to work this out
void Clear( void )
{
m_mapValidCodes.clear();
m_viGlyphWidths.clear();
m_strInitFailureReason = ""; // if blank, never failed, else says don't bother re-trying
}
ThaiCodes_t()
{
Clear();
}
// convert a supplied 1,2 or 3-byte multiplied-up integer into a valid 0..n index, else -1...
//
int GetValidIndex( int iCode )
{
std::map <int,int>::iterator it = m_mapValidCodes.find( iCode );
if (it != m_mapValidCodes.end())
{
return (*it).second;
}
return -1;
}
int GetWidth( int iGlyphIndex )
{
if (iGlyphIndex < (int)m_viGlyphWidths.size())
{
return m_viGlyphWidths[ iGlyphIndex ];
}
assert(0);
return 0;
}
// return is error message to display, or NULL for success
const char *Init(void)
{
if (m_mapValidCodes.empty() && m_viGlyphWidths.empty())
{
if (m_strInitFailureReason.empty()) // never tried and failed already?
{
int *piData = NULL; // note <int>, not <byte>, for []-access
//
// read the valid-codes table in...
//
int iBytesRead = ri.FS_ReadFile( sFILENAME_THAI_CODES, (void **) &piData );
if (iBytesRead > 0 && !(iBytesRead&3)) // valid length and multiple of 4 bytes long
{
int iTableEntries = iBytesRead / sizeof(int);
for (int i=0; i < iTableEntries; i++)
{
m_mapValidCodes[ piData[i] ] = i; // convert MBCS code to sequential index...
}
ri.FS_FreeFile( piData ); // dispose of original
// now read in the widths... (I'll keep these in a simple STL vector, so they'all disappear when the <map> entries do...
//
iBytesRead = ri.FS_ReadFile( sFILENAME_THAI_WIDTHS, (void **) &piData );
if (iBytesRead > 0 && !(iBytesRead&3) && iBytesRead>>2/*sizeof(int)*/ == iTableEntries)
{
for (int i=0; i<iTableEntries; i++)
{
m_viGlyphWidths.push_back( piData[i] );
}
ri.FS_FreeFile( piData ); // dispose of original
}
else
{
m_strInitFailureReason = va("Error with file \"%s\", size = %d!\n", sFILENAME_THAI_WIDTHS, iBytesRead);
}
}
else
{
m_strInitFailureReason = va("Error with file \"%s\", size = %d!\n", sFILENAME_THAI_CODES, iBytesRead);
}
}
}
return m_strInitFailureReason.c_str();
}
};
#define GLYPH_MAX_KOREAN_SHADERS 3
#define GLYPH_MAX_TAIWANESE_SHADERS 4
#define GLYPH_MAX_JAPANESE_SHADERS 3
#define GLYPH_MAX_CHINESE_SHADERS 3
#define GLYPH_MAX_THAI_SHADERS 3
#define GLYPH_MAX_ASIAN_SHADERS 4 // this MUST equal the larger of the above defines
#define MAX_FONT_VARIANTS 8
class CFontInfo
{
private:
// From the fontdat file
glyphInfo_t mGlyphs[GLYPH_COUNT];
// int mAsianHack; // unused junk from John's fontdat file format.
// end of fontdat data
int mShader; // handle to the shader with the glyph
int m_hAsianShaders[GLYPH_MAX_ASIAN_SHADERS]; // shaders for Korean glyphs where applicable
glyphInfo_t m_AsianGlyph; // special glyph containing asian->western scaling info for all glyphs
int m_iAsianGlyphsAcross; // needed to dynamically calculate S,T coords
int m_iAsianPagesLoaded;
bool m_bAsianLastPageHalfHeight;
int m_iLanguageModificationCount; // doesn't matter what this is, so long as it's comparable as being changed
ThaiCodes_t *m_pThaiData;
public:
char m_sFontName[MAX_QPATH]; // eg "fonts/lcd" // needed for korean font-hint if we need >1 hangul set
int mPointSize;
int mHeight;
int mAscender;
int mDescender;
bool mbRoundCalcs; // trying to make this !@#$%^ thing work with scaling
int m_iThisFont; // handle to itself
int m_iAltSBCSFont; // -1 == NULL // alternative single-byte font for languages like russian/polish etc that need to override high characters ?
int m_iOriginalFontWhenSBCSOverriden;
float m_fAltSBCSFontScaleFactor; // -1, else amount to adjust returned values by to make them fit the master western font they're substituting for
bool m_bIsFakeAlienLanguage; // ... if true, don't process as MBCS or override as SBCS etc
CFontInfo *m_variants[MAX_FONT_VARIANTS];
int m_numVariants;
int m_handle;
qboolean m_isVariant;
CFontInfo(const char *fontName);
// CFontInfo(int fill) { memset(this, fill, sizeof(*this)); } // wtf?
~CFontInfo(void) {}
const int GetPointSize(void) const { return(mPointSize); }
const int GetHeight(void) const { return(mHeight); }
const int GetAscender(void) const { return(mAscender); }
const int GetDescender(void) const { return(mDescender); }
const glyphInfo_t *GetLetter(const unsigned int uiLetter, int *piShader = NULL);
const int GetCollapsedAsianCode(ulong uiLetter) const;
const int GetLetterWidth(const unsigned int uiLetter);
const int GetLetterHorizAdvance(const unsigned int uiLetter);
const int GetShader(void) const { return(mShader); }
void FlagNoAsianGlyphs(void) { m_hAsianShaders[0] = 0; m_iLanguageModificationCount = -1; } // used during constructor
bool AsianGlyphsAvailable(void) const { return !!(m_hAsianShaders[0]); }
void UpdateAsianIfNeeded( bool bForceReEval = false);
int GetHandle();
void AddVariant(CFontInfo *variant);
int GetNumVariants();
CFontInfo *GetVariant(int index);
};
//================================================
// round float to one decimal place...
//
float RoundTenth( float fValue )
{
return ( floorf( (fValue*10.0f) + 0.5f) ) / 10.0f;
}
int g_iCurrentFontIndex; // entry 0 is reserved index for missing/invalid, else ++ with each new font registered
std::vector<CFontInfo *> g_vFontArray;
typedef std::map<sstring_t, int> FontIndexMap_t;
FontIndexMap_t g_mapFontIndexes;
int g_iNonScaledCharRange; // this is used with auto-scaling of asian fonts, anything below this number is preserved in scale, anything above is scaled down by 0.75f
//paletteRGBA_c lastcolour;
// =============================== some korean stuff =======================================
#define KSC5601_HANGUL_HIBYTE_START 0xB0 // range is...
#define KSC5601_HANGUL_HIBYTE_STOP 0xC8 // ... inclusive
#define KSC5601_HANGUL_LOBYTE_LOBOUND 0xA0 // range is...
#define KSC5601_HANGUL_LOBYTE_HIBOUND 0xFF // ...bounding (ie only valid in between these points, but NULLs in charsets for these codes)
#define KSC5601_HANGUL_CODES_PER_ROW 96 // 2 more than the number of glyphs
extern qboolean Language_IsKorean( void );
static inline bool Korean_ValidKSC5601Hangul( byte _iHi, byte _iLo )
{
return (_iHi >=KSC5601_HANGUL_HIBYTE_START &&
_iHi <=KSC5601_HANGUL_HIBYTE_STOP &&
_iLo > KSC5601_HANGUL_LOBYTE_LOBOUND &&
_iLo < KSC5601_HANGUL_LOBYTE_HIBOUND
);
}
static inline bool Korean_ValidKSC5601Hangul( unsigned int uiCode )
{
return Korean_ValidKSC5601Hangul( uiCode >> 8, uiCode & 0xFF );
}
// takes a KSC5601 double-byte hangul code and collapses down to a 0..n glyph index...
// Assumes rows are 96 wide (glyph slots), not 94 wide (actual glyphs), so I can ignore boundary markers
//
// (invalid hangul codes will return 0)
//
static int Korean_CollapseKSC5601HangulCode(unsigned int uiCode)
{
if (Korean_ValidKSC5601Hangul( uiCode ))
{
uiCode -= (KSC5601_HANGUL_HIBYTE_START * 256) + KSC5601_HANGUL_LOBYTE_LOBOUND; // sneaky maths on both bytes, reduce to 0x0000 onwards
uiCode = ((uiCode >> 8) * KSC5601_HANGUL_CODES_PER_ROW) + (uiCode & 0xFF);
return uiCode;
}
return 0;
}
static int Korean_InitFields(int &iGlyphTPs, const char *&psLang)
{
psLang = "kor";
iGlyphTPs = GLYPH_MAX_KOREAN_SHADERS;
g_iNonScaledCharRange = 255;
return 32; // m_iAsianGlyphsAcross
}
// ======================== some taiwanese stuff ==============================
// (all ranges inclusive for Big5)...
//
#define BIG5_HIBYTE_START0 0xA1 // (misc chars + level 1 hanzi)
#define BIG5_HIBYTE_STOP0 0xC6 //
#define BIG5_HIBYTE_START1 0xC9 // (level 2 hanzi)
#define BIG5_HIBYTE_STOP1 0xF9 //
#define BIG5_LOBYTE_LOBOUND0 0x40 //
#define BIG5_LOBYTE_HIBOUND0 0x7E //
#define BIG5_LOBYTE_LOBOUND1 0xA1 //
#define BIG5_LOBYTE_HIBOUND1 0xFE //
#define BIG5_CODES_PER_ROW 160 // 3 more than the number of glyphs
extern qboolean Language_IsTaiwanese( void );
static bool Taiwanese_ValidBig5Code( unsigned int uiCode )
{
const byte _iHi = (uiCode >> 8)&0xFF;
if ( (_iHi >= BIG5_HIBYTE_START0 && _iHi <= BIG5_HIBYTE_STOP0)
|| (_iHi >= BIG5_HIBYTE_START1 && _iHi <= BIG5_HIBYTE_STOP1)
)
{
const byte _iLo = uiCode & 0xFF;
if ( (_iLo >= BIG5_LOBYTE_LOBOUND0 && _iLo <= BIG5_LOBYTE_HIBOUND0) ||
(_iLo >= BIG5_LOBYTE_LOBOUND1 && _iLo <= BIG5_LOBYTE_HIBOUND1)
)
{
return true;
}
}
return false;
}
// only call this when Taiwanese_ValidBig5Code() has already returned true...
//
static bool Taiwanese_IsTrailingPunctuation( unsigned int uiCode )
{
// so far I'm just counting the first 21 chars, those seem to be all the basic punctuation...
//
if ( uiCode >= ((BIG5_HIBYTE_START0<<8)|BIG5_LOBYTE_LOBOUND0) &&
uiCode < (((BIG5_HIBYTE_START0<<8)|BIG5_LOBYTE_LOBOUND0)+20)
)
{
return true;
}
return false;
}
// takes a BIG5 double-byte code (including level 2 hanzi) and collapses down to a 0..n glyph index...
// Assumes rows are 160 wide (glyph slots), not 157 wide (actual glyphs), so I can ignore boundary markers
//
// (invalid big5 codes will return 0)
//
static int Taiwanese_CollapseBig5Code( unsigned int uiCode )
{
if (Taiwanese_ValidBig5Code( uiCode ))
{
uiCode -= (BIG5_HIBYTE_START0 * 256) + BIG5_LOBYTE_LOBOUND0; // sneaky maths on both bytes, reduce to 0x0000 onwards
if ( (uiCode & 0xFF) >= (BIG5_LOBYTE_LOBOUND1-1)-BIG5_LOBYTE_LOBOUND0)
{
uiCode -= ((BIG5_LOBYTE_LOBOUND1-1) - (BIG5_LOBYTE_HIBOUND0+1)) -1;
}
uiCode = ((uiCode >> 8) * BIG5_CODES_PER_ROW) + (uiCode & 0xFF);
return uiCode;
}
return 0;
}
static int Taiwanese_InitFields(int &iGlyphTPs, const char *&psLang)
{
psLang = "tai";
iGlyphTPs = GLYPH_MAX_TAIWANESE_SHADERS;
g_iNonScaledCharRange = 255;
return 64; // m_iAsianGlyphsAcross
}
// ======================== some Japanese stuff ==============================
// ( all ranges inclusive for Shift-JIS )
//
#define SHIFTJIS_HIBYTE_START0 0x81
#define SHIFTJIS_HIBYTE_STOP0 0x9F
#define SHIFTJIS_HIBYTE_START1 0xE0
#define SHIFTJIS_HIBYTE_STOP1 0xEF
//
#define SHIFTJIS_LOBYTE_START0 0x40
#define SHIFTJIS_LOBYTE_STOP0 0x7E
#define SHIFTJIS_LOBYTE_START1 0x80
#define SHIFTJIS_LOBYTE_STOP1 0xFC
#define SHIFTJIS_CODES_PER_ROW (((SHIFTJIS_LOBYTE_STOP0-SHIFTJIS_LOBYTE_START0)+1)+((SHIFTJIS_LOBYTE_STOP1-SHIFTJIS_LOBYTE_START1)+1))
extern qboolean Language_IsJapanese( void );
static bool Japanese_ValidShiftJISCode( byte _iHi, byte _iLo )
{
if ( (_iHi >= SHIFTJIS_HIBYTE_START0 && _iHi <= SHIFTJIS_HIBYTE_STOP0)
|| (_iHi >= SHIFTJIS_HIBYTE_START1 && _iHi <= SHIFTJIS_HIBYTE_STOP1)
)
{
if ( (_iLo >= SHIFTJIS_LOBYTE_START0 && _iLo <= SHIFTJIS_LOBYTE_STOP0) ||
(_iLo >= SHIFTJIS_LOBYTE_START1 && _iLo <= SHIFTJIS_LOBYTE_STOP1)
)
{
return true;
}
}
return false;
}
static inline bool Japanese_ValidShiftJISCode( unsigned int uiCode )
{
return Japanese_ValidShiftJISCode( uiCode >> 8, uiCode & 0xFF );
}
// only call this when Japanese_ValidShiftJISCode() has already returned true...
//
static bool Japanese_IsTrailingPunctuation( unsigned int uiCode )
{
// so far I'm just counting the first 18 chars, those seem to be all the basic punctuation...
//
if ( uiCode >= ((SHIFTJIS_HIBYTE_START0<<8)|SHIFTJIS_LOBYTE_START0) &&
uiCode < (((SHIFTJIS_HIBYTE_START0<<8)|SHIFTJIS_LOBYTE_START0)+18)
)
{
return true;
}
return false;
}
// takes a ShiftJIS double-byte code and collapse down to a 0..n glyph index...
//
// (invalid codes will return 0)
//
static int Japanese_CollapseShiftJISCode( unsigned int uiCode )
{
if (Japanese_ValidShiftJISCode( uiCode ))
{
uiCode -= ((SHIFTJIS_HIBYTE_START0<<8)|SHIFTJIS_LOBYTE_START0); // sneaky maths on both bytes, reduce to 0x0000 onwards
if ( (uiCode & 0xFF) >= (SHIFTJIS_LOBYTE_START1)-SHIFTJIS_LOBYTE_START0)
{
uiCode -= ((SHIFTJIS_LOBYTE_START1)-SHIFTJIS_LOBYTE_STOP0)-1;
}
if ( ((uiCode>>8)&0xFF) >= (SHIFTJIS_HIBYTE_START1)-SHIFTJIS_HIBYTE_START0)
{
uiCode -= (((SHIFTJIS_HIBYTE_START1)-SHIFTJIS_HIBYTE_STOP0)-1) << 8;
}
uiCode = ((uiCode >> 8) * SHIFTJIS_CODES_PER_ROW) + (uiCode & 0xFF);
return uiCode;
}
return 0;
}
static int Japanese_InitFields(int &iGlyphTPs, const char *&psLang)
{
psLang = "jap";
iGlyphTPs = GLYPH_MAX_JAPANESE_SHADERS;
g_iNonScaledCharRange = 255;
return 64; // m_iAsianGlyphsAcross
}
// ======================== some Chinese stuff ==============================
#define GB_HIBYTE_START 0xA1 // range is...
#define GB_HIBYTE_STOP 0xF7 // ... inclusive
#define GB_LOBYTE_LOBOUND 0xA0 // range is...
#define GB_LOBYTE_HIBOUND 0xFF // ...bounding (ie only valid in between these points, but NULLs in charsets for these codes)
#define GB_CODES_PER_ROW 95 // 1 more than the number of glyphs
extern qboolean Language_IsChinese( void );
static inline bool Chinese_ValidGBCode( byte _iHi, byte _iLo )
{
return (_iHi >=GB_HIBYTE_START &&
_iHi <=GB_HIBYTE_STOP &&
_iLo > GB_LOBYTE_LOBOUND &&
_iLo < GB_LOBYTE_HIBOUND
);
}
static inline bool Chinese_ValidGBCode( unsigned int uiCode)
{
return Chinese_ValidGBCode( uiCode >> 8, uiCode & 0xFF );
}
// only call this when Chinese_ValidGBCode() has already returned true...
//
static bool Chinese_IsTrailingPunctuation( unsigned int uiCode )
{
// so far I'm just counting the first 13 chars, those seem to be all the basic punctuation...
//
if ( uiCode > ((GB_HIBYTE_START<<8)|GB_LOBYTE_LOBOUND) &&
uiCode < (((GB_HIBYTE_START<<8)|GB_LOBYTE_LOBOUND)+14)
)
{
return true;
}
return false;
}
// takes a GB double-byte code and collapses down to a 0..n glyph index...
// Assumes rows are 96 wide (glyph slots), not 94 wide (actual glyphs), so I can ignore boundary markers
//
// (invalid GB codes will return 0)
//
static int Chinese_CollapseGBCode( unsigned int uiCode )
{
if (Chinese_ValidGBCode( uiCode ))
{
uiCode -= (GB_HIBYTE_START * 256) + GB_LOBYTE_LOBOUND; // sneaky maths on both bytes, reduce to 0x0000 onwards
uiCode = ((uiCode >> 8) * GB_CODES_PER_ROW) + (uiCode & 0xFF);
return uiCode;
}
return 0;
}
static int Chinese_InitFields(int &iGlyphTPs, const char *&psLang)
{
psLang = "chi";
iGlyphTPs = GLYPH_MAX_CHINESE_SHADERS;
g_iNonScaledCharRange = 255;
return 64; // m_iAsianGlyphsAcross
}
// ======================== some Thai stuff ==============================
//TIS 620-2533
#define TIS_GLYPHS_START 160
#define TIS_SARA_AM 0xD3 // special case letter, both a new letter and a trailing accent for the prev one
ThaiCodes_t g_ThaiCodes; // the one and only instance of this object
extern qboolean Language_IsThai( void );
/*
static int Thai_IsAccentChar( unsigned int uiCode )
{
switch (uiCode)
{
case 209:
case 212: case 213: case 214: case 215: case 216: case 217: case 218:
case 231: case 232: case 233: case 234: case 235: case 236: case 237: case 238:
return true;
}
return false;
}
*/
// returns a valid Thai code (or 0), based on taking 1,2 or 3 bytes from the supplied byte stream
// Fills in <iThaiBytes> with 1,2 or 3
static int Thai_ValidTISCode( const byte *psString, int &iThaiBytes )
{
// try a 1-byte code first...
//
if (psString[0] >= 160) // so western letters drop through and use normal font
{
// this code is heavily little-endian, so someone else will need to port for Mac etc... (not my problem ;-)
//
union CodeToTry_t
{
char sChars[4];
unsigned int uiCode;
};
CodeToTry_t CodeToTry;
CodeToTry.uiCode = 0; // important that we clear all 4 bytes in sChars here
// thai codes can be up to 3 bytes long, so see how high we can get...
//
int i;
for (i=0; i<3; i++)
{
CodeToTry.sChars[i] = psString[i];
int iIndex = g_ThaiCodes.GetValidIndex( CodeToTry.uiCode );
if (iIndex == -1)
{
// failed, so return previous-longest code...
//
CodeToTry.sChars[i] = 0;
break;
}
}
iThaiBytes = i;
assert(i); // if 'i' was 0, then this may be an error, trying to get a thai accent as standalone char?
return CodeToTry.uiCode;
}
return 0;
}
// special case, thai can only break on certain letters, and since the rules are complicated then
// we tell the translators to put an underscore ('_') between each word even though in Thai they're
// all jammed together at final output onscreen...
//
static inline bool Thai_IsTrailingPunctuation( unsigned int uiCode )
{
return uiCode == '_';
}
// takes a TIS 1,2 or 3 byte code and collapse down to a 0..n glyph index...
//
// (invalid codes will return 0)
//
static int Thai_CollapseTISCode( unsigned int uiCode )
{
if (uiCode >= TIS_GLYPHS_START) // so western letters drop through as invalid
{
int iCollapsedIndex = g_ThaiCodes.GetValidIndex( uiCode );
if (iCollapsedIndex != -1)
{
return iCollapsedIndex;
}
}
return 0;
}
static int Thai_InitFields(int &iGlyphTPs, const char *&psLang)
{
psLang = "tha";
iGlyphTPs = GLYPH_MAX_THAI_SHADERS;
g_iNonScaledCharRange = INT_MAX; // in other words, don't scale any thai chars down
return 32; // m_iAsianGlyphsAcross
}
// ============================================================================
// takes char *, returns integer char at that point, and advances char * on by enough bytes to move
// past the letter (either western 1 byte or Asian multi-byte)...
//
// looks messy, but the actual execution route is quite short, so it's fast...
//
// Note that I have to have this 3-param form instead of advancing a passed-in "const char **psText" because of VM-crap where you can only change ptr-contents, not ptrs themselves. Bleurgh. Ditto the qtrue:qfalse crap instead of just returning stuff straight through.
//
unsigned int AnyLanguage_ReadCharFromString( const char *psText, int *piAdvanceCount, qboolean *pbIsTrailingPunctuation /* = NULL */)
{
const byte *psString = (const byte *) psText; // avoid sign-promote bug
unsigned int uiLetter;
switch ( GetLanguageEnum() )
{
case eKorean:
{
if ( Korean_ValidKSC5601Hangul( psString[0], psString[1] ))
{
uiLetter = (psString[0] * 256) + psString[1];
*piAdvanceCount = 2;
// not going to bother testing for korean punctuation here, since korean already
// uses spaces, and I don't have the punctuation glyphs defined, only the basic 2350 hanguls
//
if ( pbIsTrailingPunctuation)
{
*pbIsTrailingPunctuation = qfalse;
}
return uiLetter;
}
}
break;
case eTaiwanese:
{
if ( Taiwanese_ValidBig5Code( (psString[0] * 256) + psString[1] ))
{
uiLetter = (psString[0] * 256) + psString[1];
*piAdvanceCount = 2;
// need to ask if this is a trailing (ie like a comma or full-stop) punctuation?...
//
if ( pbIsTrailingPunctuation)
{
*pbIsTrailingPunctuation = Taiwanese_IsTrailingPunctuation( uiLetter ) ? qtrue : qfalse;
}
return uiLetter;
}
}
break;
case eJapanese:
{
if ( Japanese_ValidShiftJISCode( psString[0], psString[1] ))
{
uiLetter = (psString[0] * 256) + psString[1];
*piAdvanceCount = 2;
// need to ask if this is a trailing (ie like a comma or full-stop) punctuation?...
//
if ( pbIsTrailingPunctuation)
{
*pbIsTrailingPunctuation = Japanese_IsTrailingPunctuation( uiLetter ) ? qtrue : qfalse;
}
return uiLetter;
}
}
break;
case eChinese:
{
if ( Chinese_ValidGBCode( (psString[0] * 256) + psString[1] ))
{
uiLetter = (psString[0] * 256) + psString[1];
*piAdvanceCount = 2;
// need to ask if this is a trailing (ie like a comma or full-stop) punctuation?...
//
if ( pbIsTrailingPunctuation)
{
*pbIsTrailingPunctuation = Chinese_IsTrailingPunctuation( uiLetter ) ? qtrue : qfalse;
}
return uiLetter;
}
}
break;
case eThai:
{
int iThaiBytes;
uiLetter = Thai_ValidTISCode( psString, iThaiBytes );
if ( uiLetter )
{
*piAdvanceCount = iThaiBytes;
if ( pbIsTrailingPunctuation )
{
*pbIsTrailingPunctuation = Thai_IsTrailingPunctuation( uiLetter ) ? qtrue : qfalse;
}
return uiLetter;
}
}
break;
default:
break;
}
// ... must not have been an MBCS code...
//
uiLetter = psString[0];
*piAdvanceCount = 1;
if (pbIsTrailingPunctuation)
{
*pbIsTrailingPunctuation = (uiLetter == '!' ||
uiLetter == '?' ||
uiLetter == ',' ||
uiLetter == '.' ||
uiLetter == ';' ||
uiLetter == ':'
) ? qtrue : qfalse;
}
return uiLetter;
}
// needed for subtitle printing since original code no longer worked once camera bar height was changed to 480/10
// rather than refdef height / 10. I now need to bodge the coords to come out right.
//
qboolean Language_IsAsian(void)
{
switch ( GetLanguageEnum() )
{
case eKorean:
case eTaiwanese:
case eJapanese:
case eChinese:
case eThai: // this is asian, but the query is normally used for scaling
return qtrue;
default:
break;
}
return qfalse;
}
qboolean Language_UsesSpaces(void)
{
// ( korean uses spaces )
switch ( GetLanguageEnum() )
{
case eTaiwanese:
case eJapanese:
case eChinese:
case eThai:
return qfalse;
default:
break;
}
return qtrue;
}
// ======================================================================
// name is (eg) "ergo" or "lcd", no extension.
//
// If path present, it's a special language hack for SBCS override languages, eg: "lcd/russian", which means
// just treat the file as "russian", but with the "lcd" part ensuring we don't find a different registered russian font
//
static const char *FontDatPath( const char *_fontName ) {
static char fontName[MAX_QPATH];
sprintf( fontName,"fonts/%s.fontdat",COM_SkipPath(const_cast<char*>(_fontName)) ); // COM_SkipPath should take a const char *, but it's just possible people use it as a char * I guess, so I have to hack around like this <groan>
return fontName;
}
CFontInfo::CFontInfo(const char *_fontName)
{
int len, i;
void *buff;
dfontdat_t *fontdat;
// remove any special hack name insertions...
//
const char *fontName = FontDatPath( _fontName );
// clear some general things...
//
m_pThaiData = NULL;
m_iAltSBCSFont = -1;
m_iThisFont = -1;
m_iOriginalFontWhenSBCSOverriden = -1;
m_fAltSBCSFontScaleFactor = -1;
m_bIsFakeAlienLanguage = !strcmp(_fontName,"aurabesh"); // dont try and make SBCS or asian overrides for this
m_isVariant = qfalse;
len = ri.FS_ReadFile(fontName, NULL);
if (len == sizeof(dfontdat_t))
{
ri.FS_ReadFile(fontName, &buff);
fontdat = (dfontdat_t *)buff;
for(i = 0; i < GLYPH_COUNT; i++)
{
#ifdef Q3_BIG_ENDIAN
mGlyphs[i].width = LittleShort(fontdat->mGlyphs[i].width);
mGlyphs[i].height = LittleShort(fontdat->mGlyphs[i].height);
mGlyphs[i].horizAdvance = LittleShort(fontdat->mGlyphs[i].horizAdvance);
mGlyphs[i].horizOffset = LittleShort(fontdat->mGlyphs[i].horizOffset);
mGlyphs[i].baseline = LittleLong(fontdat->mGlyphs[i].baseline);
mGlyphs[i].s = LittleFloat(fontdat->mGlyphs[i].s);
mGlyphs[i].t = LittleFloat(fontdat->mGlyphs[i].t);
mGlyphs[i].s2 = LittleFloat(fontdat->mGlyphs[i].s2);
mGlyphs[i].t2 = LittleFloat(fontdat->mGlyphs[i].t2);
#else
mGlyphs[i] = fontdat->mGlyphs[i];
#endif
}
mPointSize = LittleShort(fontdat->mPointSize);
mHeight = LittleShort(fontdat->mHeight);
mAscender = LittleShort(fontdat->mAscender);
mDescender = LittleShort(fontdat->mDescender);
// mAsianHack = LittleShort(fontdat->mKoreanHack); // ignore this crap, it's some junk in the fontdat file that no-one uses
mbRoundCalcs = false /*!!strstr(fontName,"ergo")*/;
// cope with bad fontdat headers...
//
if (mHeight == 0)
{
mHeight = mPointSize;
mAscender = mPointSize - Round( ((float)mPointSize/10.0f)+2 ); // have to completely guess at the baseline... sigh.
mDescender = mHeight - mAscender;
}
ri.FS_FreeFile(buff);
}
else
{
mHeight = 0;
mShader = 0;
}
Q_strncpyz(m_sFontName, fontName, sizeof(m_sFontName));
COM_StripExtension( m_sFontName, m_sFontName, sizeof( m_sFontName ) ); // so we get better error printing if failed to load shader (ie lose ".fontdat")
mShader = RE_RegisterShaderNoMip(m_sFontName);
FlagNoAsianGlyphs();
UpdateAsianIfNeeded(true);
// finished...
g_vFontArray.resize(g_iCurrentFontIndex + 1);
m_handle = g_iCurrentFontIndex;
g_vFontArray[g_iCurrentFontIndex++] = this;
if ( ri.Cvar_VariableIntegerValue( "com_buildScript" ) == 2)
{
Com_Printf( "com_buildScript(2): Registering foreign fonts...\n" );
static qboolean bDone = qfalse; // Do this once only (for speed)...
if (!bDone)
{
bDone = qtrue;
char sTemp[MAX_QPATH];
int iGlyphTPs = 0;
const char *psLang = NULL;
// SBCS override languages...
//
fileHandle_t f;
for (int i=0; g_SBCSOverrideLanguages[i].m_psName ;i++)
{
char sTemp[MAX_QPATH];
sprintf(sTemp,"fonts/%s.tga", g_SBCSOverrideLanguages[i].m_psName );
ri.FS_FOpenFileRead( sTemp, &f, qfalse );
if (f) ri.FS_FCloseFile( f );
sprintf(sTemp,"fonts/%s.fontdat", g_SBCSOverrideLanguages[i].m_psName );
ri.FS_FOpenFileRead( sTemp, &f, qfalse );
if (f) ri.FS_FCloseFile( f );
}
// asian MBCS override languages...
//
for (int iLang=0; iLang<5; iLang++)
{
switch (iLang)
{
case 0: m_iAsianGlyphsAcross = Korean_InitFields (iGlyphTPs, psLang); break;
case 1: m_iAsianGlyphsAcross = Taiwanese_InitFields (iGlyphTPs, psLang); break;
case 2: m_iAsianGlyphsAcross = Japanese_InitFields (iGlyphTPs, psLang); break;
case 3: m_iAsianGlyphsAcross = Chinese_InitFields (iGlyphTPs, psLang); break;
case 4: m_iAsianGlyphsAcross = Thai_InitFields (iGlyphTPs, psLang);
{
// additional files needed for Thai language...
//
ri.FS_FOpenFileRead( sFILENAME_THAI_WIDTHS , &f, qfalse );
if (f) {
ri.FS_FCloseFile( f );
}
ri.FS_FOpenFileRead( sFILENAME_THAI_CODES, &f, qfalse );
if (f) {
ri.FS_FCloseFile( f );
}
}
break;
}