-
Notifications
You must be signed in to change notification settings - Fork 0
/
OGLFT.h
2112 lines (1932 loc) · 79.7 KB
/
OGLFT.h
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
// -*- c++ -*-
/*
* OGLFT: A library for drawing text with OpenGL using the FreeType library
* Copyright (C) 2002 lignum Computing, Inc. <oglft@lignumcomputing.com>
* $Id: OGLFT.h,v 1.13 2002/07/12 11:56:42 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
*
*/
#ifndef OGLFT_H
#define OGLFT_H
#include <cmath>
#include <map>
#include <list>
#include <vector>
#ifdef HAVE_MPATROL
#include <mpdebug.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#ifndef OGLFT_NO_SOLID
#include <GL/gle.h>
#endif
#ifndef OGLFT_NO_QT
#include <qstring.h>
#include <qcolor.h>
#endif
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_OUTLINE_H
#include FT_TRIGONOMETRY_H
//! All of OGLFT C++ objects are in this namespace.
namespace OGLFT {
//! Thanks to DesCartes, I'd consider these manifest constants.
enum Coordinates {
X, //!< The X component of space
Y, //!< The Y component of space
Z, //!< The Z component of space
W //!< The projection component of space
};
//! Who to credit? Newton? I'd consider these manifest constants.
enum ColorSpace {
R, //!< The Red component of a color
G, //!< The Green component of a color
B, //!< The Blue component of a color
A, //!< The Alpha (or transparency) of a color
};
//! Callback from GLU tessellation routines.
typedef void (*GLUTessCallback)();
//! The FreeType library instance.
/*!
* The FreeType library has a single, global instance of a library
* handle. This reference is used to load font faces. This detail
* is generally hidden from the user of OGLFT, however, it
* can be useful to get the FT_Library instance if you want to open
* a font file yourself, either from disk or embedded in the program.
*/
class Library {
public:
/*!
* The FreeType library's library handle is only available through this
* accessor method.
* \return the global OGLFT FreeType library handle.
*/
static FT_Library& instance ( void );
protected:
/*!
* The constructor for this class is automatically called when
* this library is loaded. Access the instance through the instance()
* method.
*/
Library ( void );
/*!
* This destructor is automatically called when the program exits.
*/
~Library( void );
private:
static Library library;
static FT_Library library_;
};
//! Advance describes the "advance" of a glyph, namely the distance in
//! model space at which the NEXT glyph should be drawn. This class exists
//! to assist the computation of string metrics.
struct Advance {
float dx_; //!< Advance increment in the X direction.
float dy_; //!< Advance increment in the Y direction.
//! Default constructor. An otherwise uninitialized Advance contains zeros.
Advance ( float dx = 0, float dy = 0 ) : dx_( dx ), dy_( dy )
{}
//! Initialize an advance from a FreeType advance member.
Advance ( FT_Vector v )
{
dx_ = v.x / 64.;
dy_ = v.y / 64.;
}
//! Increment Advance with a FreeType advance member.
//! \return a reference to oneself.
Advance& operator+= ( const FT_Vector v )
{
dx_ += v.x / 64.;
dy_ += v.y / 64.;
return *this;
}
};
//! Describe the metrics of a glyph or string relative to the origin
//! of the first character
struct BBox {
float x_min_; //!< The left-most position at which "ink" appears.
float y_min_; //!< the bottom-most position at which "ink" appears.
float x_max_; //!< The right-most position at which "ink" appears.
float y_max_; //!< The top-most position at which "ink" appears.
Advance advance_; //!< The (total) advancement
//! Default constructor is all zeros.
BBox () : x_min_( 0 ), y_min_( 0 ), x_max_( 0 ), y_max_( 0 )
{}
/*!
*(Partially) initialize a BBox from a FreeType bounding box member.
*(The advancement is initialized to zero by its default constructor).
* \param ft_bbox a FreeType bounding box as retrieved from
* \c FT_Glyph_Get_CBox.
*/
BBox ( FT_BBox ft_bbox )
{
x_min_ = ft_bbox.xMin / 64.;
y_min_ = ft_bbox.yMin / 64.;
x_max_ = ft_bbox.xMax / 64.;
y_max_ = ft_bbox.yMax / 64.;
}
//! Scale the bounding box by a constant.
//! \param k a constant to scale the bounding box by.
//! \return a reference to oneself.
BBox& operator*= ( double k )
{
x_min_ *= k;
y_min_ *= k;
x_max_ *= k;
y_max_ *= k;
advance_.dx_ *= k;
advance_.dy_ *= k;
return *this;
}
/*!
* Merge a bounding box into the current one (not really addition).
* Each time a BBox is "added", the current BBox is expanded to include
* the metrics of the new BBox. May only work for horizontal fonts, though.
* \param b the bounding box to merge.
* \return a reference to oneself.
*/
BBox& operator+= ( const BBox& b )
{
float new_value;
new_value = b.x_min_ + advance_.dx_;
if ( new_value < x_min_ ) x_min_ = new_value;
new_value = b.y_min_ + advance_.dy_;
if ( new_value < y_min_ ) y_min_ = new_value;
new_value = b.x_max_ + advance_.dx_;
if ( new_value > x_max_ ) x_max_ = new_value;
new_value = b.y_max_ + advance_.dy_;
if ( new_value > y_max_ ) y_max_ = new_value;
advance_.dx_ += b.advance_.dx_;
advance_.dy_ += b.advance_.dy_;
return *this;
}
};
//! During tesselation of a polygonal Face (outline, filled or solid),
//! an object which implements this interface can be used to compute a
//! different color for each vertex.
class ColorTess {
public:
//! Compute a color for this position. Note that the position is
//! in the glyph's local coordinate system.
//! \param p vertex position in glyph's local coordinate system. Argument is
//! a GLdouble[3].
//! \return GLfloat[4] (RGBA) color specification.
virtual GLfloat* color ( GLdouble* p ) = 0;
};
//! During tesselation of a polygonal Face (outline, filled or solid),
//! an object which implements this interface can be used to compute a
//! different texture coordinate for each vertex.
class TextureTess {
public:
//! Compute a texture coordinate for this position. Note that the
//! position is in the glyph's local coordinate system.
//! \param p vertex position in glyph's local coordinate system. Argument is
//! a GLdouble[3].
//! \return GLfloat[2] (s,t) texture coordinates.
virtual GLfloat* texCoord ( GLdouble* p ) = 0;
};
//! The argument to setCharacterDisplayLists is an STL vector of
//! OpenGL display list names (GLuints).
typedef std::vector<GLuint> DisplayLists;
//! A convenience definition of an iterator for display list vectors.
typedef DisplayLists::const_iterator DLCI;
//! A convenience definition of an iterator for display list vectors.
typedef DisplayLists::iterator DLI;
//! A face (aka font) used to render text with OpenGL.
/*!
* This is an abstract class, but it does define most the functions that
* you are likely to call to manipulate the rendering of the text.
*/
class Face {
public:
//! Thanks to the standard formerly known as PHIGS. Horizontal text
//! justification constants.
enum HorizontalJustification {
LEFT, //!< Left justified justification of text
ORIGIN, //!< Natural origin alignment of text (default)
CENTER, //!< Center justified alignment of text
RIGHT //!< Right justified alignment of text
};
//! Thanks to the standard formerly known as PHIGS. Vertical text
//! justification constants.
enum VerticalJustification {
BOTTOM, //!< Descender alignment of text
BASELINE, //!< Baseline alignment of text (default)
MIDDLE, //!< Centered alignment of text
TOP //!< Ascender justification of text
};
//! Control how OpenGL display lists are created for individual glyphs.
//! The default mode is to create display lists for each glyph as it
//! is requested. Therefore, the Face drawing routines cannot themselves
//! be called from within an open display list. In IMMEDIATE mode,
//! cached glyphs will be drawn if available, otherwise the FreeType
//! data for a glyph is re-rendered each time.
enum GlyphCompileMode {
COMPILE, //!< Compile new glyphs when seen for the first time.
IMMEDIATE //!< Do not \em create display lists for glyphs.
};
private:
//! We allow a Face to be constructed either from a file name
//! or passed in as an already opened FreeType FT_Face. In the case
//! of the later (already opened), we don't close the FT_Face on
//! destruction. This way you can share FT_Faces between related
//! OGLFT faces. Also, we're experimenting with being able to use
//! multiple FT_Faces in a single OGLFT Face, so this is represented
//! as a data structure.
struct FaceData {
FT_Face face_;
bool free_on_exit_;
FaceData ( FT_Face face, bool free_on_exit = true )
: face_( face ), free_on_exit_( free_on_exit )
{}
};
protected:
//! The FreeType face - experimentally, this is now an array of
//! faces so that we can handle a wider range of UNICODE points
//! in case a face doesn't cover the points of interest.
std::vector< FaceData > faces_;
//! Did a font load OK?
bool valid_;
//! Glyph display list creation mode.
enum GlyphCompileMode compile_mode_;
//! Nominal point size.
double point_size_;
//! Display resolution in pixels per inch.
FT_UInt resolution_;
//! Does rendering text affect the MODELVIEW matrix?
bool advance_;
//! Foreground color (I really wanted to avoid this, but not really
//! possible without state queries, which you can't put into
//! display lists. Anyway, you'll be able to get even more fancy
//! by passing in a function to map the color with, so why balk at
//! this?)
GLfloat foreground_color_[4];
//! Background color (what modes would use this?)
GLfloat background_color_[4];
//! PHIGS-like horizontal positioning of text.
enum HorizontalJustification horizontal_justification_;
//! PHIGS-like vertical positioning of text.
enum VerticalJustification vertical_justification_;
//! Rotate an entire string in the Z plane
GLfloat string_rotation_;
//! Let the user decide which character to use as the rotation reference.
//! Use "o" by default, I suppose.
FT_UInt rotation_reference_glyph_;
//! The rotation reference character could be in any face.
FT_Face rotation_reference_face_;
//! These are the translation offsets provided by the rotation reference
//! character; for whom, we've discovered, only the Y position is relevant.
GLfloat rotation_offset_y_;
//! Type of the cache of defined glyph to display list mapping.
typedef std::map< FT_UInt, GLuint > GlyphDLists;
//! A convenience definition of the iterator over the glyph to display
//! list map.
typedef GlyphDLists::const_iterator GDLCI;
//! A convenience definition of the iterator over the glyph to display
//! list map.
typedef GlyphDLists::iterator GDLI;
//! Cache of defined glyph display lists
GlyphDLists glyph_dlists_;
//! The user can supply an array of display list which are invoked
//! before each glyph is rendered.
DisplayLists character_display_lists_;
public:
/*!
* Construct a Face by loading a font from the given file.
* \param file the filename which contains the font face.
* \param point_size the initial point size of the font to generate. A point
* is essentially 1/72th of an inch. Defaults to 12.
* \param resolution the pixel density of the display in dots per inch (DPI).
* Defaults to 100 DPI.
*/
Face ( const char* filename, double point_size = 12, FT_UInt resolution = 100 );
/*!
* Alternatively, the user may have already opened a face and just
* wants to draw with it. This is useful for Multiple Master fonts or
* combining multiple files to increase UNICODE point coverage.
* \param face open Freetype FT_Face.
* \param point_size the initial point size of the font to generate. A point
* is essentially 1/72th of an inch. Defaults to 12.
* \param resolution the pixel density of the display in dots per inch (DPI).
* Defaults to 100 DPI.
*/
Face ( FT_Face face, double point_size = 12, FT_UInt resolution = 100 );
/*!
* Deleting a Face frees its FreeType face (and anything else it's
* styles have allocated).
*/
virtual ~Face ( void );
/*!
* Let the user test to see if the font was loaded OK.
* \return true if the FT_Face was successfully created.
*/
bool isValid ( void ) const { return valid_; }
/*!
* Add another FT_Face to the OGLFT Face. Generally used to add more
* coverage of UNICODE points (at least that's the plan). This
* routine takes a filename and takes ownership of the FT_Face.
* \param filename name of file containing font face data.
* \return true if face was successfully added.
*/
bool addAuxiliaryFace ( const char* filename );
/*!
* Add another FT_Face to the OGLFT Face. Generally used to add more
* coverage of UNICODE points (at least that's the plan). This
* routine takes an already open FT_Face. The user is responsible
* for clean up.
* \param face open FreeType FT_Face
* \return true if face was successfully added.
*/
bool addAuxiliaryFace ( FT_Face face );
/*!
* By default, each time a new character is seen, its glyph is rendered
* into a display list. This means that a display list cannot already
* be open (since OpenGL doesn't allow nested display list creation).
* Rendering can be set into immediate mode in which case glyphs are
* rendered from display lists if available, but are otherwise generated
* anew each time.
* \param compile_mode the new compile mode.
*/
void setCompileMode ( enum GlyphCompileMode compile_mode )
{
compile_mode_ = compile_mode;
}
/*!
* \return the current glyph compile mode.
*/
enum GlyphCompileMode compileMode ( void ) const { return compile_mode_; }
/*!
* For the rasterized styles (Monochrome, Grayscale, Translucent, Texture),
* glyphs are rendered at the pixel size given by:
*
* point_size [pts] * / 72 [pts/in] * resolution [dots/in] = [dots].
*
* For the polygon styles (Outline, Filled, Solid), the "nominal" size of
* the glyphs is:
*
* point_size[pts] / 72 [pts/in] * resolution [dots/in]
* / units_per_EM [font unit/EM] = [dots * EM].
*
* If the MODELVIEW and PROJECTION matrices are such that one screen pixel
* corresponds to one modeling unit, then polygonal Faces will
* be the same size as raster Faces.
*
* Note that changing the point size after Face creation will invalidate
* the cache of OpenGL display lists and any other information which
* the individual styles have cached.
* \param point_size the new point size in points (1/72-th inch).
*/
void setPointSize ( float point_size );
/*!
* \return the current point size.
*/
float pointSize ( void ) { return point_size_; }
/*!
* For the rasterized styles (Monochrome, Grayscale,
* Translucent, Texture), the exact rendered size of the glyphs depends on
* the resolution of the display (as opposed to the polygon styles
* whose size is controlled by the viewing matrices). The Texture
* style is slightly different because the glyphs are texture-mapped
* onto an arbitrary rectangle; here, the resolution only controls
* how accurately the glyph is rendered.
* \param resolution the resolution in DPI (dots per inch).
*/
void setResolution ( FT_UInt resolution );
/*!
* \return the current raster resolution.
*/
FT_UInt resolution ( void ) { return resolution_; }
/*!
* If advance is true, then the changes made to the MODELVIEW matrix
* to render a string are allowed to remain. Otherwise, the library
* pushes the current MODELVIEW matrix onto the matrix stack, renders
* the string and then pops it off again. Rendering a character always
* modifies the MODELVIEW matrix.
* \param advance whether or not the MODELVIEW matrix should be left
* translated by the advancement of a rendered string.
*/
void setAdvance ( bool advance ) { advance_ = advance; }
/*!
* \return the advance value.
*/
bool advance ( void ) const { return advance_; }
/*!
* This is the nominal color of the glyphs. A lot of other things
* can alter what you actually see! Note that changing the foreground
* color invalidates the glyph cache.
* \param red the red component of the foreground color.
* \param green the green component of the foreground color.
* \param blue the blue component of the foreground color.
* \param alpha the alpha component of the foreground color.
*/
void setForegroundColor ( GLfloat red = 0.0,
GLfloat green = 0.0,
GLfloat blue = 0.0,
GLfloat alpha = 1.0 );
/*!
* This is the nominal color of the glyphs. A lot of other things
* can alter what you actually see! Note that changing the foreground
* color invalidates the glyph cache.
* \param foreground_color an array of 4 values corresponding to the
* red, green, blue and alpha components of the foreground color.
*/
void setForegroundColor ( const GLfloat foreground_color[4] );
#ifndef OGLFT_NO_QT
/*!
* This is the nominal color of the glyphs. A lot of other things
* can alter what you actually see! Note that changing the foreground
* color invalidates the glyph cache.
* \param foreground_color the foreground color as an unsigned int.
*/
void setForegroundColor ( const QRgb foreground_color );
#endif /* OGLFT_NO_QT */
/*!
* \return the red component of the foreground color
*/
GLfloat foregroundRed ( void ) const { return foreground_color_[R]; }
/*!
* \return the green component of the foreground color
*/
GLfloat foregroundGreen ( void ) const { return foreground_color_[G]; }
/*!
* \return the blue component of the foreground color
*/
GLfloat foregroundBlue ( void ) const { return foreground_color_[B]; }
/*!
* \return the alpha component of the foreground color
*/
GLfloat foregroundAlpha ( void ) const { return foreground_color_[A]; }
/*!
* This is the nominal background color of the glyphs. A lot of other things
* can alter what you actually see! Note that changing the background
* color invalidates the glyph cache.
* \param red the red component of the background color.
* \param green the green component of the background color.
* \param blue the blue component of the background color.
* \param alpha the alpha component of the background color.
*/
void setBackgroundColor ( GLfloat red = 1.0,
GLfloat green = 1.0,
GLfloat blue = 1.0,
GLfloat alpha = 0.0 );
/*!
* This is the nominal background color of the glyphs. A lot of other things
* can alter what you actually see! Note that changing the background
* color invalidates the glyph cache.
* \param background_color an array of 4 values corresponding to the
* red, green, blue and alpha components of the background color.
*/
void setBackgroundColor ( const GLfloat background_color[4] );
#ifndef OGLFT_NO_QT
/*!
* This is the nominal background color of the glyphs. A lot of other things
* can alter what you actually see! Note that changing the background
* color invalidates the glyph cache.
* \param background_color the background color as an unsigned int.
*/
void setBackgroundColor ( const QRgb background_color );
#endif /* OGLFT_NO_QT */
/*!
* \return the red component of the background color
*/
GLfloat backgroundRed ( void ) const { return background_color_[R]; }
/*!
* \return the green component of the background color
*/
GLfloat backgroundGreen ( void ) const { return background_color_[G]; }
/*!
* \return the blue component of the background color
*/
GLfloat backgroundBlue ( void ) const { return background_color_[B]; }
/*!
* \return the alpha component of the background color
*/
GLfloat backgroundAlpha ( void ) const { return background_color_[A]; }
/*!
* Set the individual character rotation in the Z direction.
* \param character_rotation_z angle in degrees of z rotation.
*/
virtual void setCharacterRotationZ ( GLfloat character_rotation_z ) = 0;
/*!
* \return the character rotation in the Z direction.
*/
virtual GLfloat characterRotationZ ( void ) const = 0;
/*!
* The z rotation angle needs a center. Nominate a character whose
* center is to be the center of rotation. By default, use "o".
* \param c rotation reference character.
*/
void setCharacterRotationReference ( unsigned char c );
/*!
* Rotate an entire string through the given angle (in the Z plane only).
* (Somewhat pointless for the vector styles since you can do mostly
* the same thing with the MODELVIEW transform, however, for what its
* worth, this routine uses the FreeType rotation function to compute
* the "proper" metrics for glyph advance.)
* \param string_rotation angle in degrees of z rotation.
*/
void setStringRotation ( GLfloat string_rotation );
/*!
* \return the (Z plane) string rotation angle.
*/
GLfloat stringRotation ( void ) const { return string_rotation_; }
/*!
* Set the horizontal justification.
* \param horizontal_justification the new horizontal justification.
*/
void setHorizontalJustification ( enum HorizontalJustification
horizontal_justification )
{
horizontal_justification_ = horizontal_justification;
}
/*!
* \return the horizontal justification.
*/
enum HorizontalJustification horizontalJustification ( void ) const
{ return horizontal_justification_; }
/*!
* Set the vertical justification.
* \param vertical_justification the new vertical justification
*/
void setVerticalJustification ( enum VerticalJustification
vertical_justification )
{
vertical_justification_ = vertical_justification;
}
/*!
* \return the vertical justification.
*/
enum VerticalJustification verticaljustification ( void )
const { return vertical_justification_; }
/*!
* Specify an OpenGL display list to be invoked before
* each character in a string. Face makes a copy of the argument. Pass
* an empty DisplayLists to disable this feature.
* \param character_display_lists STL vector<GLuint> containing a display
* list to invoke before each glyph in a string is drawn.
*/
void setCharacterDisplayLists ( const DisplayLists& character_display_lists )
{
character_display_lists_ = character_display_lists;
}
/*!
* \return a reference to the array of character display lists. This is
* the live list as stored in the Face.
*/
DisplayLists& characterDisplayLists ( void )
{ return character_display_lists_; }
/*!
* \return the height (i.e., line spacing) at the current character size.
*/
virtual double height ( void ) const = 0;
/*!
* Compute the bounding box info for a character.
* \param c the (latin1) character to measure.
* \return the bounding box of c.
*/
virtual BBox measure ( unsigned char c ) = 0;
#ifndef OGLFT_NO_QT
/*!
* Compute the bounding box info for a character.
* \param c the (UNICODE) character to measure.
* \return the bounding box of c.
*/
virtual BBox measure ( QChar c ) = 0;
#endif /* OGLFT_NO_QT */
/*!
* Compute the bounding box info for a string.
* \param s the (latin1) string to measure.
* \return the bounding box of s.
*/
virtual BBox measure ( const char* s );
/*!
* Compute the bounding box info for a string without conversion
* to modeling coordinates.
* \param s the (latin1) string to measure.
* \return the bounding box of s.
*/
virtual BBox measureRaw ( const char* s );
#ifndef OGLFT_NO_QT
/*!
* Compute the bounding box info for a string.
* \param s the (UNICODE) string to measure.
* \return the bounding box of s.
*/
virtual BBox measure ( const QString& s );
/*!
* Compute the bounding box info for a real number formatted as specified.
* \param format (see draw for valid formats)
* \param value real number.
* \return the bounding box of the formatted number.
*/
virtual BBox measure ( const QString& format, double number );
/*!
* Compute the bounding box info for a string without conversion
* to modeling coordinates.
* \param s the (UNICODE) string to measure.
* \return the bounding box of s.
*/
virtual BBox measureRaw ( const QString& s );
#endif /* OGLFT_NO_QT */
/*!
* Compile a string into an OpenGL display list for later
* rendering. Essentially, the string is rendered at the origin
* of the current MODELVIEW. Note: no other display lists should
* be open when this routine is called. Also, the Face does not
* keep track of these lists, so you must delete them in order
* to recover the memory.
* \param s the (latin1) string to compile.
* \return the display list name for the string.
*/
GLuint compile ( const char* s );
#ifndef OGLFT_NO_QT
/*!
* Compile a string into an OpenGL display list for later
* rendering. Essentially, the string is rendered at the origin
* of the current MODELVIEW. Note: no other display lists should
* be open when this routine is called. Also, the Face does not
* keep track of these lists, so you must delete them in order
* to recover the memory.
* \param s the (UNICODE) string to compile.
* \return the display list name for the string.
*/
GLuint compile ( const QString& s );
#endif /* OGLFT_NO_QT */
/*!
* Compile a single character (glyph) into an OpenGL display list
* for later rendering. The Face \em does keep track of these
* display lists, so do not delete them.
* \param c the (latin1) character to compile.
* \return the display list name for the character.
*/
GLuint compile ( unsigned char c );
#ifndef OGLFT_NO_QT
/*!
* Compile a single character (glyph) into an OpenGL display list
* for later rendering. The Face \em does keep track of these
* display lists, so do not delete them.
* \param c the (UNICODE) character to compile.
* \return the display list name for the character.
*/
GLuint compile ( const QChar c );
#endif /* OGLFT_NO_QT */
/*!
* Draw a (latin1) string using the current MODELVIEW matrix. If
* advance is true, then the final glyph advance changes to the
* MODELVIEW matrix are left in place.
* \param s the (latin1) string to draw.
*/
void draw ( const char* s );
#ifndef OGLFT_NO_QT
/*!
* Draw a (UNICODE) string using the current MODELVIEW
* matrix. If advance is true, then the final glyph advance
* changes to the MODELVIEW matrix are left in place.
* \param s the (UNICODE) string to draw.
*/
void draw ( const QString& s );
#endif /* OGLFT_NO_QT */
/*!
* Draw the character using the current MODELVIEW matrix. Note that
* the MODELVIEW matrix is modified by the glyph advance. Draw a
* string if you don't want the MODELVIEW matrix changed.
* \param c the (latin1) character to draw.
*/
void draw ( unsigned char c );
#ifndef OGLFT_NO_QT
/*!
* Draw the character using the current MODELVIEW matrix. Note that
* the MODELVIEW matrix is modified by the glyph advance. Draw a
* string if you don't want the MODELVIEW matrix changed.
* \param c the (UNICODE) character to draw.
*/
void draw ( const QChar c );
#endif /* OGLFT_NO_QT */
/*!
* Draw the (latin1) character at the given 2D point. Note that
* the MODELVIEW matrix is modified by the glyph advance. Draw
* a string if you don't want the MODELVIEW matrix changed.
* \param x the X position.
* \param y the Y position.
* \param c the (latin1) character to draw.
*/
void draw ( GLfloat x, GLfloat y, unsigned char c );
/*!
* Draw the (latin1) character at the given 3D point. Note that
* the MODELVIEW matrix is modified by the glyph advance. Draw
* a string if you don't want the MODELVIEW matrix changed.
* \param x the X position.
* \param y the Y position.
* \param z the Z position.
* \param c the (latin1) character to draw.
*/
void draw ( GLfloat x, GLfloat y, GLfloat z, unsigned char c );
#ifndef OGLFT_NO_QT
/*!
* Draw the (UNICODE) character at the given 2D point. Note that
* the MODELVIEW matrix is modified by the glyph advance. Draw
* a string if you don't want the MODELVIEW matrix changed.
* \param x the X position.
* \param y the Y position.
* \param c the (UNICODE) character to draw.
*/
void draw ( GLfloat x, GLfloat y, QChar c );
/*!
* Draw the (UNICODE) character at the given 3D point. Note that
* the MODELVIEW matrix is modified by the glyph advance. Draw
* a string if you don't want the MODELVIEW matrix changed.
* \param x the X position.
* \param y the Y position.
* \param z the Z position.
* \param c the (UNICODE) character to draw.
*/
void draw ( GLfloat x, GLfloat y, GLfloat z, QChar c );
#endif /* OGLFT_NO_QT */
/*!
* Draw a string at the given 2D point.
* \param x the X position.
* \param y the Y position.
* \param s the (latin1) string to draw.
*/
void draw ( GLfloat x, GLfloat y, const char* s );
/*!
* Draw a string at the given 3D point.
* \param x the X position.
* \param y the Y position.
* \param z the Z position.
* \param s the (latin1) string to draw.
*/
void draw ( GLfloat x, GLfloat y, GLfloat z, const char* s );
#ifndef OGLFT_NO_QT
/*!
* Draw a string at the given 2D point.
* \param x the X position.
* \param y the Y position.
* \param s the (UNICODE) string to draw.
*/
void draw ( GLfloat x, GLfloat y, const QString& s );
/*!
* Draw a string at the given 3D point.
* \param x the X position.
* \param y the Y position.
* \param z the Z position.
* \param s the (UNICODE) string to draw.
*/
void draw ( GLfloat x, GLfloat y, GLfloat z, const QString& s );
/*!
* Draw a real number per the given format at the given 2D point.
* \param x the X position.
* \param y the Y position.
* \param format Like a typical printf format. Regular text is printed
* while a '%' introduces the real number's format. Includes the
* following format flags:
* \li %%x.yf - floating point in field width x and precision y
* \li %%x.ye - scientific notation in field width x and precision y
* \li %%x.yg - pick best floating or scientific in field width x and
* precision y
* \li %%p - draw as a proper fraction, e.g. 1 1/2. Note: this currently
* requires a special font which encodes glyphs to be drawn for the
* numerator and demoninator in the UNICODE Private Area (0xE000).
*
* \param number the numeric value.
*/
void draw ( GLfloat x, GLfloat y, const QString& format, double number );
/*!
* Draw a real number per the given format at the given 3D point.
* \param x the X position.
* \param y the Y position.
* \param z the Z position.
* \param format Like a typical printf format. Regular text is printed
* while a '%' introduces the real number's format. Includes the
* following format flags:
* \li %%x.yf - floating point in field width x and precision y
* \li %%x.ye - scientific notation in field width x and precision y
* \li %%x.yg - pick best floating or scientific in field width x and
* precision y
* \li %%p - draw as a proper fraction, e.g. 1 1/2. Note: this currently
* requires a special font which encodes glyphs to be drawn for the
* numerator and demoninator in the UNICODE Private Area (0xE000).
*
* \param number the numeric value.
*/
void draw ( GLfloat x, GLfloat y, GLfloat z, const QString& format,
double number );
#endif /* OGLFT_NO_QT */
/*!
* \return the nominal ascender from the face. This is in "notional"
* units.
*/
int ascender ( void ) { return faces_.front().face_->ascender; }
/*!
* \return the nominal descender from the face. This is in "notional"
* units.
*/
int descender ( void ) { return faces_.front().face_->descender; }
protected:
// The various styles override these routines
//! Some styles, in particular the Texture, need specialized steps
//! to compile a glyph into an OpenGL display list.
//! \param face the FT_Face containing the glyph.
//! \param glyph_index the index of the glyph in face.
//! \return the display list of the compiled glyph.
virtual GLuint compileGlyph ( FT_Face face, FT_UInt glyph_index ) = 0;
//! Each style implements its own glyph rendering routine.
//! \param face the FT_Face containing the glyph.
//! \param glyph_index the index of the glyph in face.
virtual void renderGlyph ( FT_Face face, FT_UInt glyph_index ) = 0;
//! There is a slight different between the way in which the polygonal
//! and raster styles select the character size for FreeType to generate.
virtual void setCharSize ( void ) = 0;
//! The different styles have different caching needs (well, really only
//! the texture style currently has more than the display list cache).
virtual void clearCaches ( void ) = 0;
//! The polygonal and raster styles compute different values for the
//! Z rotation offset. (It's in integer pixels for the raster styles and
//! in floating point pixels for the polygonal styles.)
virtual void setRotationOffset ( void ) = 0;
private:
void init ( void );
BBox measure_nominal ( const char* s );
#ifndef OGLFT_NO_QT
BBox measure_nominal ( const QString& s );
QString format_number ( const QString& format, double number );
#endif /* OGLFT_NO_QT */
};
//! This is the base class of the polygonal styles: outline, filled and solid.
/*!
* In the polygonal styles, the detailed geometric outlines of the glyphs
* are extracted from the font file and rendered as polygons.
*/
class Polygonal : public Face {
protected:
//! Angle of rotation of characters relative to text orientation.
struct {
bool active_;
GLfloat x_, y_, z_;
} character_rotation_;
//! The tessellation of curves is pretty crude; regardless of length,
//! use the same number of increments (and as near as I can tell, this
//! is more than sufficient unless the glyph takes up the whole screen).
unsigned int tessellation_steps_;
//! When curves are tessellated, we use the forward difference algorithm
//! from Foley and van Dam for parametric curves (pg. 511 of 2nd Ed. in C).
//! So, the step size, delta, is in the parametric variable which is always
//! on the interval [0,1]. Therefore, delta = 1/tessellation_steps
double delta_, delta2_, delta3_;
//! For vector rendition modes, FreeType is allowed to generate the
//! lines and arcs at the original face definition resolution. To
//! get to the proper glyph size, the vertices are scaled before
//! they're passed to the GLU tessellation routines.
double vector_scale_;
//! Callbacks for FreeType glyph decomposition into outlines
FT_Outline_Funcs interface_;
//! Default number of steps to break TrueType and Type1 arcs into.
//! (Note: this looks good to me, anyway)
static const unsigned int DEFAULT_TESSELLATION_STEPS = 4;