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 "vtk_freetype.h" //since ft2build.h could be in the path
38 #include FT_FREETYPE_H
39 #include FT_GLYPH_H
40 
41 #if (FREETYPE_MAJOR >2 ||(FREETYPE_MAJOR == 2 && ( FREETYPE_MINOR > 1 || (FREETYPE_MINOR == 1 && FREETYPE_PATCH >= 9))))
42 # include FT_CACHE_H
43 // This flag will be used to check if Caching support is to be compiled.
44 # define VTK_FREETYPE_CACHING_SUPPORTED
45 #endif
46 
47 class FTFont;
48 
49 //----------------------------------------------------------------------------
50 // Singleton cleanup
51 
53 {
54 public:
57 };
58 
59 //----------------------------------------------------------------------------
60 // Singleton font cache
61 
63 {
64 public:
66  void PrintSelf(ostream& os, vtkIndent indent);
67 
74  static vtkFreeTypeUtilities *New();
75 
77  static vtkFreeTypeUtilities* GetInstance();
78 
81  static void SetInstance(vtkFreeTypeUtilities *instance);
82 
84  FT_Library* GetLibrary();
85 
87 
90  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
91  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
92  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
93  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
94  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
95  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
97 
103  int GetSize(vtkTextProperty *tprop, FT_Size *size);
104 
110  int GetFace(vtkTextProperty *tprop, FT_Face *face);
111 
115  int GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
116 
117  //BTX
119 
130  enum
131  {
132  GLYPH_REQUEST_DEFAULT = 0,
133  GLYPH_REQUEST_BITMAP = 1,
134  GLYPH_REQUEST_OUTLINE = 2
135  };
136  //ETX
137  int GetGlyph(vtkTextProperty *tprop,
138  FT_UInt32 c,
139  FT_Glyph *glyph,
140  int request = GLYPH_REQUEST_DEFAULT);
142 
144 
153  int GetBoundingBox(vtkTextProperty *tprop, const char *str, int bbox[4]);
154  int IsBoundingBoxValid(int bbox[4]);
156 
158 
160  int RenderString(vtkTextProperty *tprop,
161  const char *str,
162  vtkImageData *data);
164 
166 
167  int RenderString(vtkTextProperty *tprop,
168  const char *str,
169  int x, int y,
170  vtkImageData *data);
172 
174 
182  void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id);
183  void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop);
185 
187 
189  int GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size);
190  int GetFace(unsigned long tprop_cache_id, FT_Face *face);
191  int GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c,
192  FT_UInt *gindex);
193  int GetGlyph(unsigned long tprop_cache_id,
194  int font_size,
195  FT_UInt gindex,
196  FT_Glyph *glyph,
197  int request = GLYPH_REQUEST_DEFAULT);
199 
201 
206  void GetWidthHeightDescender(const char *str,
207  vtkTextProperty *tprop,
208  int *width,
209  int *height,
210  float *descender);
212 
214 
216  void PrepareImageData(vtkImageData *data,
217  vtkTextProperty *tprop,
218  const char *str,
219  int *x, int *y);
221 
223 
225  int GetConstrainedFontSize(const char *str, vtkTextProperty *tprop,
226  double orientation, int targetWidth,
227  int targetHeight);
229 
231 
232  void JustifyLine(const char *str, vtkTextProperty *tprop,
233  int totalWidth, int *x, int *y);
235 
236  // Old Code
237  // Cache entry
238 
239  struct Entry
240  {
242  FTFont *Font;
245  };
246 
248  double override_color[3] = NULL);
249 
250 protected:
251  //Internal helper method called by RenderString
252  int PopulateImageData(vtkTextProperty *tprop, const char *str,
253  int x, int y, vtkImageData *data, int use_shadow_color);
255  virtual ~vtkFreeTypeUtilities();
256 
257 private:
258  vtkFreeTypeUtilities(const vtkFreeTypeUtilities&); // Not implemented.
259  void operator=(const vtkFreeTypeUtilities&); // Not implemented.
260 
261  // The singleton instance and the singleton cleanup instance
262 
263  static vtkFreeTypeUtilities* Instance;
264  static vtkFreeTypeUtilitiesCleanup Cleanup;
265 
266  // The cache manager, image cache and charmap cache
267 
268 #ifdef VTK_FREETYPE_CACHING_SUPPORTED
269  FTC_Manager *CacheManager;
270  FTC_ImageCache *ImageCache;
271  FTC_CMapCache *CMapCache;
272 
274 
275  FTC_Manager* GetCacheManager();
276  FTC_ImageCache* GetImageCache();
277  FTC_CMapCache* GetCMapCache();
278 #endif
279 
280 
281  unsigned int MaximumNumberOfFaces;
282  unsigned int MaximumNumberOfSizes;
283  unsigned long MaximumNumberOfBytes;
284 
285  void InitializeCacheManager();
286  void ReleaseCacheManager();
287 
288  // Old Code
289 
290  void PrintEntry(int i, char *msg = 0);
291  void ReleaseEntry(int i);
292 
293  void InitializeCache();
294  void ReleaseCache();
295 
296  Entry *Entries[VTK_FTFC_CACHE_CAPACITY];
297  int NumberOfEntries;
298 };
299 
300 #endif