VTK
vtkFreeTypeUtilities.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkFreeTypeUtilities.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
25 #ifndef __vtkFreeTypeUtilities_h
26 #define __vtkFreeTypeUtilities_h
27 
28 #define VTK_FTFC_CACHE_CAPACITY 150
29 
30 #include "vtkObject.h"
31 
32 class vtkImageData;
33 class vtkTextProperty;
34 
35 // FreeType
36 
37 #include <FTGL.h>
38 
39 #include "vtk_freetype.h" //since ft2build.h could be in the path
40 #include FT_FREETYPE_H
41 #include FT_GLYPH_H
42 
43 #if (FREETYPE_MAJOR >2 ||(FREETYPE_MAJOR == 2 && ( FREETYPE_MINOR > 1 || (FREETYPE_MINOR == 1 && FREETYPE_PATCH >= 9))))
44 # include FT_CACHE_H
45 // This flag will be used to check if Caching support is to be compiled.
46 # define VTK_FREETYPE_CACHING_SUPPORTED
47 #endif
48 
49 #ifdef FTGL_USE_NAMESPACE
50 namespace ftgl
51 {
52 #endif
53 class FTFont;
54 #ifdef FTGL_USE_NAMESPACE
55 }
56 #endif
57 
58 //----------------------------------------------------------------------------
59 // Singleton cleanup
60 
62 {
63 public:
66 };
67 
68 //----------------------------------------------------------------------------
69 // Singleton font cache
70 
72 {
73 public:
75  void PrintSelf(ostream& os, vtkIndent indent);
76 
83  static vtkFreeTypeUtilities *New();
84 
86  static vtkFreeTypeUtilities* GetInstance();
87 
90  static void SetInstance(vtkFreeTypeUtilities *instance);
91 
93  FT_Library* GetLibrary();
94 
96 
99  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
100  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
101  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
102  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
103  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
104  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
106 
112  int GetSize(vtkTextProperty *tprop, FT_Size *size);
113 
119  int GetFace(vtkTextProperty *tprop, FT_Face *face);
120 
124  int GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
125 
126  //BTX
128 
139  enum
140  {
141  GLYPH_REQUEST_DEFAULT = 0,
142  GLYPH_REQUEST_BITMAP = 1,
143  GLYPH_REQUEST_OUTLINE = 2
144  };
145  //ETX
146  int GetGlyph(vtkTextProperty *tprop,
147  FT_UInt32 c,
148  FT_Glyph *glyph,
149  int request = GLYPH_REQUEST_DEFAULT);
151 
153 
162  int GetBoundingBox(vtkTextProperty *tprop, const char *str, int bbox[4]);
163  int IsBoundingBoxValid(int bbox[4]);
165 
167 
169  int RenderString(vtkTextProperty *tprop,
170  const char *str,
171  vtkImageData *data);
173 
175 
176  int RenderString(vtkTextProperty *tprop,
177  const char *str,
178  int x, int y,
179  vtkImageData *data);
181 
183 
191  void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id);
192  void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop);
194 
196 
198  int GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size);
199  int GetFace(unsigned long tprop_cache_id, FT_Face *face);
200  int GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c,
201  FT_UInt *gindex);
202  int GetGlyph(unsigned long tprop_cache_id,
203  int font_size,
204  FT_UInt gindex,
205  FT_Glyph *glyph,
206  int request = GLYPH_REQUEST_DEFAULT);
208 
210 
215  void GetWidthHeightDescender(const char *str,
216  vtkTextProperty *tprop,
217  int *width,
218  int *height,
219  float *descender);
221 
223 
225  void PrepareImageData(vtkImageData *data,
226  vtkTextProperty *tprop,
227  const char *str,
228  int *x, int *y);
230 
232 
234  int GetConstrainedFontSize(const char *str, vtkTextProperty *tprop,
235  double orientation, int targetWidth,
236  int targetHeight);
238 
240 
241  void JustifyLine(const char *str, vtkTextProperty *tprop,
242  int totalWidth, int *x, int *y);
244 
245  // Old Code
246  // Cache entry
247 
248  struct Entry
249  {
251 #ifdef FTGL_USE_NAMESPACE
252  ftgl::FTFont *Font;
253 #else
254  FTFont *Font;
255 #endif
258  };
259 
261  double override_color[3] = NULL);
262 
263 protected:
264  //Internal helper method called by RenderString
265  int PopulateImageData(vtkTextProperty *tprop, const char *str,
266  int x, int y, vtkImageData *data, int use_shadow_color);
268  virtual ~vtkFreeTypeUtilities();
269 
270 private:
271  vtkFreeTypeUtilities(const vtkFreeTypeUtilities&); // Not implemented.
272  void operator=(const vtkFreeTypeUtilities&); // Not implemented.
273 
274  // The singleton instance and the singleton cleanup instance
275 
276  static vtkFreeTypeUtilities* Instance;
277  static vtkFreeTypeUtilitiesCleanup Cleanup;
278 
279  // The cache manager, image cache and charmap cache
280 
281 #ifdef VTK_FREETYPE_CACHING_SUPPORTED
282  FTC_Manager *CacheManager;
283  FTC_ImageCache *ImageCache;
284  FTC_CMapCache *CMapCache;
285 
287 
288  FTC_Manager* GetCacheManager();
289  FTC_ImageCache* GetImageCache();
290  FTC_CMapCache* GetCMapCache();
291 #endif
292 
293 
294  unsigned int MaximumNumberOfFaces;
295  unsigned int MaximumNumberOfSizes;
296  unsigned long MaximumNumberOfBytes;
297 
298  void InitializeCacheManager();
299  void ReleaseCacheManager();
300 
301  // Old Code
302 
303  void PrintEntry(int i, char *msg = 0);
304  void ReleaseEntry(int i);
305 
306  void InitializeCache();
307  void ReleaseCache();
308 
309  Entry *Entries[VTK_FTFC_CACHE_CAPACITY];
310  int NumberOfEntries;
311 };
312 
313 #endif