summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmtruetypecache.h
blob: c128f7f23a9b737885d9e9184ff0c67cffc77d77 (plain)
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
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2002 by Andreas Zehender
    email                : zehender@kde.org
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/

#ifndef PMTRUETYPECACHE_H
#define PMTRUETYPECACHE_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "pmsplinesegment.h"

#include <tqcache.h>
#include <kstaticdeleter.h>

#ifdef HAVE_FREETYPE
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_OUTLINE_H
#endif

class PMTrueTypeFont;

/**
 * Cache for truetype fonts
 */
class PMTrueTypeCache
{
public:
   /**
    * Returns a pointer to the font with file name "file" or 0, if the
    * font does not exist or is no truetype font
    *
    * Returns 0 if freetype is not available.
    */
   static PMTrueTypeFont* font( const TQString& file );
   
#ifdef HAVE_FREETYPE
   
   /**
    * Destructor
    */
   ~PMTrueTypeCache( );
   
private:
   static PMTrueTypeCache* s_pInstance;
   static KStaticDeleter<PMTrueTypeCache> s_staticDeleter;

   /**
    * Lookup function
    */
   PMTrueTypeFont* lookUp( const TQString& file );
   
   TQCache<PMTrueTypeFont> m_cache;
   FT_Library m_library;

#endif //HAVE_FREETYPE
   
private:
   /**
    * Standard constructor
    */
   PMTrueTypeCache( );
};

/**
 * Class that represents a truetype character outline
 */
class PMTrueTypeOutline
{
public:
#ifdef HAVE_FREETYPE
   /**
    * Constructor that generates the outline for the glyph.
    * The outline is scaled to match the font height.
    */
   PMTrueTypeOutline( FT_OutlineGlyph glyph, FT_Face face );
#else
   /**
    * Standard constructor.
    *
    * Don't use this constructor. It is only added as dummy if freetype
    * is not installed.
    */   
   PMTrueTypeOutline( )
   {
      m_contours = 0;
      m_segments = 0;
      m_advance = 0;
   }
#endif
   /**
    * Returns the outline
    */
   const PMSegmentListList& outline( ) const { return m_outline; }
   /**
    * Returns the number of contours
    */
   int contours( ) const { return m_contours; }
   /**
    * Returns the sum of the number of segments for all contours
    */
   int segments( ) const { return m_segments; }
   /**
    * Returns the offset for the next character
    */
   double advance( ) const { return m_advance; }
       
   PMSegmentListList m_outline;
   int m_contours;
   int m_segments;
   double m_advance;
};

/**
 * Class that represents a truetype font.
 *
 * This class caches the glyph outlines.
 */
class PMTrueTypeFont
{
public:
   /**
    * Returns the outline for the character
    *
    * Returns 0 if there is no glyph for the character or the font is
    * not a valid, scalable true type font.
    */
   PMTrueTypeOutline* outline( TQChar c );
   /**
    * Returns true if the font is a valid, scalable true type font
    */
   bool isValid( );
   /**
    * Returns the kerning offset for the two characters
    */
   double kerning( TQChar c1, TQChar c2 );
   
#ifdef HAVE_FREETYPE
   
public:
   /**
    * Creates a true type font
    */
   PMTrueTypeFont( FT_Library lib, FT_Face face );
   /**
    * Deletes the true type font
    */
   ~PMTrueTypeFont( );

private:
   FT_UInt findGlyphIndex( TQChar c );
   
   FT_Library m_library;
   FT_Face m_face;
   
   bool m_valid;
   bool m_validChecked;
   bool m_useKerning;

   TQCache<PMTrueTypeOutline> m_cache;
   
#else //!HAVE_FREETYPE
   
public:
   /**
    * Standard constructor.
    *
    * Don't use this constructor. It is only added as dummy if freetype
    * is not installed.
    */
   PMTrueTypeFont( );
   
#endif //HAVE_FREETYPE
   
};

#endif