-
Notifications
You must be signed in to change notification settings - Fork 0
/
OGLFT.cpp
3865 lines (2851 loc) · 105 KB
/
OGLFT.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
/*
* OGLFT: A library for drawing text with OpenGL using the FreeType library
* Copyright (C) 2002 lignum Computing, Inc. <oglft@lignumcomputing.com>
* $Id: OGLFT.cpp,v 1.10 2002/07/12 13:36:20 allen Exp $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <iostream>
#include <iomanip>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef OGLFT_NO_QT
#include <qregexp.h>
#endif
#include <OGLFT.h>
namespace OGLFT {
// This is the static instance of the FreeType library wrapper ...
Library Library::library;
// ... and this is the FreeType library handle itself.
FT_Library Library::library_;
// The static instance above causes this constructor to be called
// when the object module is loaded.
Library::Library ( void )
{
FT_Error error = FT_Init_FreeType( &library_ );
if ( error != 0 ) {
std::cerr << "Could not initialize the FreeType library. Exiting." << std::endl;
exit( 1 );
}
}
Library::~Library ( void )
{
FT_Error error = FT_Done_FreeType( library_ );
if ( error != 0 ) {
std::cerr << "Could not terminate the FreeType library." << std::endl;
}
}
// Return the only instance in the process
FT_Library& Library::instance ( void )
{
return library_;
}
// Load a new face
Face::Face ( const char* filename, double point_size, FT_UInt resolution )
: point_size_( point_size ), resolution_( resolution )
{
valid_ = true; // Assume the best :-)
FT_Face ft_face;
FT_Error error = FT_New_Face( Library::instance(), filename, 0, &ft_face );
if ( error != 0 ) {
valid_ = false;
return;
}
// As of FreeType 2.1: only a UNICODE charmap is automatically activated.
// If no charmap is activated automatically, just use the first one.
if ( ft_face->charmap == 0 && ft_face->num_charmaps > 0 )
FT_Select_Charmap( ft_face, ft_face->charmaps[0]->encoding );
faces_.push_back( FaceData( ft_face ) );
init();
}
// Go with a face that the user has already opened.
Face::Face (FT_Face face, double point_size, FT_UInt resolution )
: point_size_( point_size ), resolution_( resolution )
{
valid_ = true;
// As of FreeType 2.1: only a UNICODE charmap is automatically activated.
// If no charmap is activated automatically, just use the first one.
if ( face->charmap == 0 && face->num_charmaps > 0 )
FT_Select_Charmap( face, face->charmaps[0]->encoding );
faces_.push_back( FaceData( face, false ) );
init();
}
// Standard initialization behavior once the font file is opened.
void Face::init ( void )
{
// By default, each glyph is compiled into a display list the first
// time it is encountered
compile_mode_ = COMPILE;
// By default, all drawing is wrapped with push/pop matrix so that the
// MODELVIEW matrix is not modified. If advance_ is set, then subsequent
// drawings follow from the advance of the last glyph rendered.
advance_ = false;
// Initialize the default colors
foreground_color_[R] = 0.;
foreground_color_[G] = 0.;
foreground_color_[B] = 0.;
foreground_color_[A] = 1.;
background_color_[R] = 1.;
background_color_[G] = 1.;
background_color_[B] = 1.;
background_color_[A] = 0.;
// The default positioning of the text is at the origin of the first glyph
horizontal_justification_ = ORIGIN;
vertical_justification_ = BASELINE;
// By default, strings are rendered in their nominal direction
string_rotation_ = 0;
// setCharacterRotationReference calls the virtual function clearCaches()
// so it is up to a subclass to set the real default
rotation_reference_glyph_ = 0;
rotation_reference_face_ = 0;
rotation_offset_y_ = 0.;
}
Face::~Face ( void )
{
for ( unsigned int i = 0; i < faces_.size(); i++ )
if ( faces_[i].free_on_exit_ )
FT_Done_Face( faces_[i].face_ );
}
// Add another Face to select characters from
bool Face::addAuxiliaryFace ( const char* filename )
{
FT_Face ft_face;
FT_Error error = FT_New_Face( Library::instance(), filename, 0, &ft_face );
if ( error != 0 )
return false;
faces_.push_back( FaceData( ft_face ) );
setCharSize();
return true;
}
// Add another Face to select characters from
bool Face::addAuxiliaryFace ( FT_Face face )
{
faces_.push_back( FaceData( face, false ) );
setCharSize();
return true;
}
// Note: Changing the point size also clears the display list cache
void Face::setPointSize ( float point_size )
{
if ( point_size != point_size_ ) {
point_size_ = point_size;
clearCaches();
setCharSize();
}
}
// Note: Changing the resolution also clears the display list cache
void Face::setResolution ( FT_UInt resolution )
{
if ( resolution != resolution_ ) {
resolution_ = resolution;
clearCaches();
setCharSize();
}
}
// Note: Changing the background color also clears the display list cache.
void Face::setBackgroundColor ( GLfloat red, GLfloat green, GLfloat blue,
GLfloat alpha )
{
if ( background_color_[R] != red ||
background_color_[G] != green ||
background_color_[B] != blue ||
background_color_[A] != alpha ) {
background_color_[R] = red;
background_color_[G] = green;
background_color_[B] = blue;
background_color_[A] = alpha;
clearCaches();
}
}
// Note: Changing the foreground color also clears the display list cache.
void Face::setForegroundColor ( GLfloat red, GLfloat green, GLfloat blue,
GLfloat alpha )
{
if ( foreground_color_[R] != red ||
foreground_color_[G] != green ||
foreground_color_[B] != blue ||
foreground_color_[A] != alpha ) {
foreground_color_[R] = red;
foreground_color_[G] = green;
foreground_color_[B] = blue;
foreground_color_[A] = alpha;
clearCaches();
}
}
// Note: Changing the foreground color also clears the display list cache.
void Face::setForegroundColor ( const GLfloat foreground_color[4] )
{
if ( foreground_color_[R] != foreground_color[R] ||
foreground_color_[G] != foreground_color[G] ||
foreground_color_[B] != foreground_color[B] ||
foreground_color_[A] != foreground_color[A] ) {
foreground_color_[R] = foreground_color[R];
foreground_color_[G] = foreground_color[G];
foreground_color_[B] = foreground_color[B];
foreground_color_[A] = foreground_color[A];
clearCaches();
}
}
// Note: Changing the background color also clears the display list cache.
void Face::setBackgroundColor ( const GLfloat background_color[4] )
{
if ( background_color_[R] != background_color[R] ||
background_color_[G] != background_color[G] ||
background_color_[B] != background_color[B] ||
background_color_[A] != background_color[A] ) {
background_color_[R] = background_color[R];
background_color_[G] = background_color[G];
background_color_[B] = background_color[B];
background_color_[A] = background_color[A];
clearCaches();
}
}
#ifndef OGLFT_NO_QT
// Note: Changing the foreground color also clears the display list cache.
void Face::setForegroundColor ( const QRgb foreground_rgba )
{
GLfloat foreground_color[4];
foreground_color[R] = qRed( foreground_rgba ) / 255.;
foreground_color[G] = qGreen( foreground_rgba ) / 255.;
foreground_color[B] = qBlue( foreground_rgba ) / 255.;
foreground_color[A] = qAlpha( foreground_rgba ) / 255.;
if ( foreground_color_[R] != foreground_color[R] ||
foreground_color_[G] != foreground_color[G] ||
foreground_color_[B] != foreground_color[B] ||
foreground_color_[A] != foreground_color[A] ) {
foreground_color_[R] = foreground_color[R];
foreground_color_[G] = foreground_color[G];
foreground_color_[B] = foreground_color[B];
foreground_color_[A] = foreground_color[A];
clearCaches();
}
}
// Note: Changing the background color also clears the display list cache.
void Face::setBackgroundColor ( const QRgb background_rgba )
{
GLfloat background_color[4];
background_color[R] = qRed( background_rgba ) / 255.;
background_color[G] = qGreen( background_rgba ) / 255.;
background_color[B] = qBlue( background_rgba ) / 255.;
background_color[A] = qAlpha( background_rgba ) / 255.;
if ( background_color_[R] != background_color[R] ||
background_color_[G] != background_color[G] ||
background_color_[B] != background_color[B] ||
background_color_[A] != background_color[A] ) {
background_color_[R] = background_color[R];
background_color_[G] = background_color[G];
background_color_[B] = background_color[B];
background_color_[A] = background_color[A];
clearCaches();
}
}
#endif /* OGLFT_NO_QT */
// Note: Changing the string rotation angle clears the display list cache
void Face::setStringRotation ( GLfloat string_rotation )
{
if ( string_rotation != string_rotation_ ) {
string_rotation_ = string_rotation;
clearCaches();
// Note that this affects ALL glyphs accessed through
// the Face, both the vector and the raster glyphs. Very nice!
if ( string_rotation_ != 0. ) {
float angle;
if ( string_rotation_ < 0. ) {
angle = 360. - fmod( fabs( string_rotation_ ), 360.f );
}
else {
angle = fmod( string_rotation_, 360.f );
}
FT_Matrix rotation_matrix;
FT_Vector sinus;
FT_Vector_Unit( &sinus, (FT_Angle)(angle * 0x10000L) );
rotation_matrix.xx = sinus.x;
rotation_matrix.xy = -sinus.y;
rotation_matrix.yx = sinus.y;
rotation_matrix.yy = sinus.x;
for ( unsigned int i = 0; i < faces_.size(); i++ )
FT_Set_Transform( faces_[i].face_, &rotation_matrix, 0 );
}
else
for ( unsigned int i = 0; i < faces_.size(); i++ )
FT_Set_Transform( faces_[i].face_, 0, 0 );
}
}
// Note: Changing the rotation reference character clears the display list cache.
void Face::setCharacterRotationReference ( unsigned char c )
{
unsigned int f;
FT_UInt glyph_index = 0;
for ( f = 0; f < faces_.size(); f++ ) {
glyph_index = FT_Get_Char_Index( faces_[f].face_, c );
if ( glyph_index != 0 ) break;
}
if ( f < faces_.size() && glyph_index != rotation_reference_glyph_ ) {
FT_Error error = FT_Load_Glyph( faces_[f].face_, glyph_index,
FT_LOAD_DEFAULT );
if ( error != 0 ) return;
rotation_reference_glyph_ = glyph_index;
rotation_reference_face_ = faces_[f].face_;
setRotationOffset();
clearCaches();
}
}
BBox Face::measure ( const char* s )
{
BBox bbox;
char c;
if ( ( c = *s++ ) != 0 ) {
bbox = measure( c );
for ( c = *s; c != 0; c = *++s ) {
BBox char_bbox = measure( c );
bbox += char_bbox;
}
}
return bbox;
}
BBox Face::measureRaw ( const char* s )
{
BBox bbox;
for ( char c = *s; c != 0; c = *++s ) {
BBox char_bbox;
unsigned int f;
FT_UInt glyph_index = 0;
for ( f = 0; f < faces_.size(); f++ ) {
glyph_index = FT_Get_Char_Index( faces_[f].face_, c );
if ( glyph_index != 0 ) break;
}
if ( glyph_index == 0 ) continue;
FT_Error error = FT_Load_Glyph( faces_[f].face_, glyph_index,
FT_LOAD_DEFAULT );
if ( error != 0 ) continue;
FT_Glyph glyph;
error = FT_Get_Glyph( faces_[f].face_->glyph, &glyph );
if ( error != 0 ) continue;
FT_BBox ft_bbox;
FT_Glyph_Get_CBox( glyph, ft_glyph_bbox_unscaled, &ft_bbox );
FT_Done_Glyph( glyph );
char_bbox = ft_bbox;
char_bbox.advance_ = faces_[f].face_->glyph->advance;
bbox += char_bbox;
}
return bbox;
}
#ifndef OGLFT_NO_QT
BBox Face::measure ( const QString& s )
{
BBox bbox;
if ( s.length() > 0 ) {
bbox = measure( s.at( 0 ) );
for ( int i = 1; i < s.length(); i++ ) {
BBox char_bbox = measure( s.at( i ) );
bbox += char_bbox;
}
}
return bbox;
}
BBox Face::measure ( const QString& format, double number )
{
return measure( format_number( format, number ) );
}
BBox Face::measureRaw ( const QString& s )
{
BBox bbox;
for ( int i = 0; i < s.length(); i++ ) {
BBox char_bbox;
unsigned int f;
FT_UInt glyph_index = 0;
for ( f = 0; f < faces_.size(); f++ ) {
glyph_index = FT_Get_Char_Index( faces_[f].face_, s.at( i ).unicode() );
if ( glyph_index != 0 ) break;
}
if ( glyph_index == 0 ) {
continue;
}
FT_Error error = FT_Load_Glyph( faces_[f].face_, glyph_index,
FT_LOAD_DEFAULT );
if ( error != 0 ) continue;
FT_Glyph glyph;
error = FT_Get_Glyph( faces_[f].face_->glyph, &glyph );
if ( error != 0 ) continue;
FT_BBox ft_bbox;
FT_Glyph_Get_CBox( glyph, ft_glyph_bbox_unscaled, &ft_bbox );
FT_Done_Glyph( glyph );
char_bbox = ft_bbox;
char_bbox.advance_ = faces_[f].face_->glyph->advance;
bbox += char_bbox;
}
return bbox;
}
#endif /* OGLFT_NO_QT */
// Measure the bounding box as if the (latin1) string were not rotated
BBox Face::measure_nominal ( const char* s )
{
if ( string_rotation_ == 0. )
return measure( s );
for ( unsigned int f = 0; f < faces_.size(); f++ )
FT_Set_Transform( faces_[f].face_, 0, 0 );
BBox bbox = measure( s );
float angle;
if ( string_rotation_ < 0. ) {
angle = 360. - fmod( fabs( string_rotation_ ), 360.f );
}
else {
angle = fmod( string_rotation_, 360.f );
}
FT_Matrix rotation_matrix;
FT_Vector sinus;
FT_Vector_Unit( &sinus, (FT_Angle)(angle * 0x10000L) );
rotation_matrix.xx = sinus.x;
rotation_matrix.xy = -sinus.y;
rotation_matrix.yx = sinus.y;
rotation_matrix.yy = sinus.x;
for ( unsigned int f = 0; f < faces_.size(); f++ )
FT_Set_Transform( faces_[f].face_, &rotation_matrix, 0 );
return bbox;
}
#ifndef OGLFT_NO_QT
// Measure the bounding box as if the (UNICODE) string were not rotated
BBox Face::measure_nominal ( const QString& s )
{
if ( string_rotation_ == 0. )
return measure( s );
for ( unsigned int f = 0; f < faces_.size(); f++ )
FT_Set_Transform( faces_[f].face_, 0, 0 );
BBox bbox = measure( s );
float angle;
if ( string_rotation_ < 0. ) {
angle = 360. - fmod( fabs( string_rotation_ ), 360.f );
}
else {
angle = fmod( string_rotation_, 360.f );
}
FT_Matrix rotation_matrix;
FT_Vector sinus;
FT_Vector_Unit( &sinus, (FT_Angle)(angle * 0x10000L) );
rotation_matrix.xx = sinus.x;
rotation_matrix.xy = -sinus.y;
rotation_matrix.yx = sinus.y;
rotation_matrix.yy = sinus.x;
for ( unsigned int f = 0; f < faces_.size(); f++ )
FT_Set_Transform( faces_[f].face_, &rotation_matrix, 0 );
return bbox;
}
// Format the number per the given format. Mostly pointless
// for the standard formats, e.g. %12e. You can use the regular
// Qt functions to format such a string and avoid the parsing
// which is done here.
QString Face::format_number ( const QString& format, double number )
{
// This regexp says:
// 1. optionally match any thing up to a format,
// 2. the optional format (%...), and
// 3. optionally anything after it.
// Note that since everything is optional, the match always succeeds.
QRegExp format_regexp("((?:[^%]|%%)*)(%[0-9]*\\.?[0-9]*[efgp])?((?:[^%]|%%)*)");
/*int pos = */ format_regexp.indexIn( format );
QStringList list = format_regexp.capturedTexts();
QStringList::Iterator it = list.begin();
it = list.erase( it ); // Remove the "matched" string, leaving the pieces
if ( it == list.end() ) return QString::null; // Probably an error
// Extract each piece from the list
QString prefix, value_format, postfix;
char type = '\0';
if ( !(*it).isEmpty() )
prefix = *it;
++it;
if ( it != list.end() ) {
if ( !(*it).isEmpty() ) {
// Reparse this to extract the details of the format
QRegExp specifier_regexp( "([0-9]*)\\.?([0-9]*)([efgp])" );
(void)specifier_regexp.indexIn( *it );
QStringList specifier_list = specifier_regexp.capturedTexts();
QStringList::Iterator sit = specifier_list.begin();
sit = specifier_list.erase( sit );
int width = (*sit).toInt();
++sit;
int precision = (*sit).toInt();
++sit;
type = (*sit).at(0).toLatin1();
// The regular formats just use Qt's number formatting capability
if ( type == 'e' || type == 'f' || type == 'g' )
value_format = QString( "%1" ).arg( number, width, type, precision );
// For the fraction, though, we have to convert it the special
// UNICODE encoding
else if ( type == 'p' ) {
// Fixed for now...
if ( fabs( number ) < 1./256. )
value_format = "0";
else {
// Extract the integral part
int a = (int)number;
if ( a != 0 )
value_format = QString::number( a );
// Extract the fractional part: NOTE: THIS IS LIMITED TO
// REPRESENTING ALL FRACTIONS AS n/256
int b = (int)rint( 256. * fabs( number - a ) );
// If b is exactly 256, then the original number was
// essentially an integer (to within 1/256-th)
if ( b == 256 )
value_format = QString::number( rint( number ) );
else if ( b != 0 ) {
int c = 256;
// Remove common factors of two from the numerator and denominator
for ( ; ( b & 0x1 ) == 0; b >>= 1, c >>= 1 );
// Format the numerator and shift to 0xE000 sequence
QString numerator = QString::number( b );
for ( int i = 0; i < numerator.length(); i++ ) {
numerator[i] = QChar( numerator.at(i).unicode() -
QChar('0').unicode() +
0xE000 );
}
value_format += numerator;
value_format += QChar( 0xE00a ); // The '/'
// Format the denominator and shift to 0xE010 sequence
QString denominator = QString::number( c );
for ( int i = 0; i < denominator.length(); i++ ) {
denominator[i] = QChar( denominator.at(i).unicode() -
QChar('0').unicode() +
0xE010 );
}
value_format += denominator;
}
}
}
}
++it;
if ( it != list.end() && !(*it).isEmpty() )
postfix = *it;
}
return prefix + value_format + postfix;
}
#endif /* OGLFT_NO_QT */
// Compile a (latin1) string into a display list
GLuint Face::compile ( const char* s )
{
// First, make sure all the characters in the string are themselves
// in display lists
const char* s_tmp = s;
for ( char c = *s_tmp; c != 0; c = *++s_tmp ) {
compile( c );
}
GLuint dlist = glGenLists( 1 );
glNewList( dlist, GL_COMPILE );
glColor4f( foreground_color_[R], foreground_color_[G], foreground_color_[B],
foreground_color_[A] );
if ( !advance_ )
glPushMatrix();
draw( s );
if ( !advance_ )
glPopMatrix();
glEndList();
return dlist;
}
#ifndef OGLFT_NO_QT
// Compile a (UNICODE) string into a display list
GLuint Face::compile ( const QString& s )
{
// First, make sure all the characters in the string are themselves
// in display lists
for ( int i = 0; i < s.length(); i++ ) {
compile( s.at( i ) );
}
GLuint dlist = glGenLists( 1 );
glNewList( dlist, GL_COMPILE );
glColor4f( foreground_color_[R], foreground_color_[G], foreground_color_[B],
foreground_color_[A] );
if ( !advance_ )
glPushMatrix();
draw( s );
if ( !advance_ )
glPopMatrix();
glEndList();
return dlist;
}
#endif /* OGLFT_NO_QT */
// Compile a (latin1) character glyph into a display list and cache
// it for later
GLuint Face::compile ( unsigned char c )
{
// See if we've done it already
GDLCI fgi = glyph_dlists_.find( c );
if ( fgi != glyph_dlists_.end() )
return fgi->second;
unsigned int f;
FT_UInt glyph_index = 0;
for ( f = 0; f < faces_.size(); f++ ) {
glyph_index = FT_Get_Char_Index( faces_[f].face_, c );
if ( glyph_index != 0 ) break;
}
if ( glyph_index == 0 )
return 0;
GLuint dlist = compileGlyph( faces_[f].face_, glyph_index );
glyph_dlists_[ c ] = dlist;
return dlist;
}
#ifndef OGLFT_NO_QT
// Compile a (UNICODE) character glyph into a display list and cache
// it for later
GLuint Face::compile ( const QChar c )
{
// See if we've done it already
GDLCI fgi = glyph_dlists_.find( c.unicode() );
if ( fgi != glyph_dlists_.end() )
return fgi->second;
unsigned int f;
FT_UInt glyph_index = 0;
for ( f = 0; f < faces_.size(); f++ ) {
glyph_index = FT_Get_Char_Index( faces_[f].face_, c.unicode() );
if ( glyph_index != 0 ) break;
}
if ( glyph_index == 0 )
return 0;
GLuint dlist = compileGlyph( faces_[f].face_, glyph_index );
glyph_dlists_[ c.unicode() ] = dlist;
return dlist;
}
#endif /* OGLFT_NO_QT */
// Assume the MODELVIEW matrix is already set and draw the (latin1)
// string. Note: this routine now ignores almost all settings:
// including the position (both modelview and raster), color,
// justification and advance settings. Consider this to be the raw
// drawing routine for which you are responsible for most of the
// setup.
void Face::draw ( const char* s )
{
DLCI character_display_list = character_display_lists_.begin();
for ( char c = *s; c != 0; c = *++s ) {
if ( character_display_list != character_display_lists_.end() ) {
glCallList( *character_display_list );
character_display_list++;
}
draw( c );
}
}
#ifndef OGLFT_NO_QT
// Assume the MODELVIEW matrix is already set and draw the (UNICODE)
// string. Note: this routine now ignores almost all settings:
// including the position (both modelview and raster), color,
// justification and advance settings. Consider this to be the raw
// drawing routine for which you are responsible for most of the
// setup.
void Face::draw ( const QString& s )
{
DLCI character_display_list = character_display_lists_.begin();
for ( int i = 0; i < s.length(); i++ ) {
if ( character_display_list != character_display_lists_.end() ) {
glCallList( *character_display_list );
character_display_list++;
}
draw( s.at( i ) );
}
}
#endif /* OGLFT_NO_QT */
// Assume the MODELVIEW matrix is already setup and draw the
// (latin1) character.
void Face::draw ( unsigned char c )
{
// See if we've done it already
GDLCI fgi = glyph_dlists_.find( c );
if ( fgi != glyph_dlists_.end( ) ) {
glCallList( fgi->second );
return;
}
unsigned int f;
FT_UInt glyph_index = 0;
for ( f = 0; f < faces_.size(); f++ ) {
glyph_index = FT_Get_Char_Index( faces_[f].face_, c );
if ( glyph_index != 0 ) break;
}
if ( glyph_index == 0 )
return;
// Otherwise, either compile it (and call it) or ...
else if ( compile_mode_ == COMPILE ) {
GLuint dlist = compile( c );
glCallList( dlist );
}
// ... render it immediately
else {
renderGlyph( faces_[f].face_, glyph_index );
}
}
#ifndef OGLFT_NO_QT
// Assume the MODELVIEW matrix is already setup and draw the
// (UNICODE) character.
void Face::draw ( const QChar c )
{
// See if we've done it already
GDLCI fgi = glyph_dlists_.find( c.unicode() );
if ( fgi != glyph_dlists_.end( ) ) {
glCallList( fgi->second );
return;
}
unsigned int f;
FT_UInt glyph_index = 0;
for ( f = 0; f < faces_.size(); f++ ) {
glyph_index = FT_Get_Char_Index( faces_[f].face_, c.unicode() );
if ( glyph_index != 0 ) {
break;
}
}
if ( glyph_index == 0 )
return;
// Otherwise, either compile it (and call it) or ...
if ( compile_mode_ == COMPILE ) {
GLuint dlist = compile( c );
glCallList( dlist );
}
// ... render it immediately
else {
renderGlyph( faces_[f].face_, glyph_index );
}
}
#endif /* OGLFT_NO_QT */
// Draw the (latin1) character at the given position. The MODELVIEW
// matrix is modified by the glyph advance.
void Face::draw ( GLfloat x, GLfloat y, unsigned char c )
{
glTranslatef( x, y, 0. );
glColor4f( foreground_color_[R], foreground_color_[G], foreground_color_[B],
foreground_color_[A] );
glRasterPos2i( 0, 0 );
draw( c );
}
// Draw the (latin1) character at the given position. The MODELVIEW
// matrix is modified by the glyph advance.
void Face::draw ( GLfloat x, GLfloat y, GLfloat z, unsigned char c )
{
glTranslatef( x, y, z );
glColor4f( foreground_color_[R], foreground_color_[G], foreground_color_[B],
foreground_color_[A] );
glRasterPos2i( 0, 0 );
draw( c );
}
#ifndef OGLFT_NO_QT
// Draw the (UNICODE) character at the given position. The MODELVIEW
// matrix is modified by the glyph advance.