VTK
vtkFreeTypeTools.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkFreeTypeTools.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 __vtkFreeTypeTools_h
26 #define __vtkFreeTypeTools_h
27 
28 #include "vtkRenderingFreeTypeModule.h" // For export macro
29 #include "vtkObject.h"
30 
31 class vtkImageData;
32 class vtkTextProperty;
33 class vtkStdString;
34 class vtkUnicodeString;
35 
36 // FreeType
37 #include "vtk_freetype.h" //since ft2build.h could be in the path
38 #include FT_FREETYPE_H
39 #include FT_GLYPH_H
40 #include FT_CACHE_H
41 
42 class FTFont;
43 
44 //----------------------------------------------------------------------------
45 // Singleton cleanup
46 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
47 {
48 public:
51 };
52 
53 //----------------------------------------------------------------------------
54 // Singleton font cache
55 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
56 {
57 public:
58  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
59  void PrintSelf(ostream& os, vtkIndent indent);
60 
62  static vtkFreeTypeTools* GetInstance();
63 
66  static void SetInstance(vtkFreeTypeTools *instance);
67 
69  FT_Library* GetLibrary();
70 
72 
75  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
76  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
77  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
78  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
79  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
80  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
82 
84 
93  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
94  int bbox[4]);
95  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
96  int bbox[4]);
97  bool IsBoundingBoxValid(int bbox[4]);
99 
101 
103  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str,
104  vtkImageData *data);
105  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
106  vtkImageData *data);
108 
110 
118  void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id);
119  void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop);
121 
123 
127  vtkSetMacro(ScaleToPowerTwo, bool);
128  vtkGetMacro(ScaleToPowerTwo, bool);
129  vtkBooleanMacro(ScaleToPowerTwo, bool);
131 
132 protected:
134 
136  template <typename T>
137  bool CalculateBoundingBox(vtkTextProperty *tprop, const T& str, int bbox[4]);
139 
141 
143  template <typename T>
144  void PrepareImageData(vtkImageData *data,
145  vtkTextProperty *tprop,
146  const T& str,
147  int *x, int *y);
149 
151 
152  template <typename T>
153  bool PopulateImageData(vtkTextProperty *tprop, const T& str,
154  int x, int y, vtkImageData *data);
156 
161  bool GetSize(vtkTextProperty *tprop, FT_Size *size);
162 
167  bool GetFace(vtkTextProperty *tprop, FT_Face *face);
168 
171  bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
172 
174 
184  enum
185  {
186  GLYPH_REQUEST_DEFAULT = 0,
187  GLYPH_REQUEST_BITMAP = 1,
188  GLYPH_REQUEST_OUTLINE = 2
189  };
190  bool GetGlyph(vtkTextProperty *tprop,
191  FT_UInt32 c,
192  FT_Glyph *glyph,
193  int request = GLYPH_REQUEST_DEFAULT);
194  bool GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size);
195  bool GetFace(unsigned long tprop_cache_id, FT_Face *face);
196  bool GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c,
197  FT_UInt *gindex);
198  bool GetGlyph(unsigned long tprop_cache_id,
199  int font_size,
200  FT_UInt gindex,
201  FT_Glyph *glyph,
202  int request = GLYPH_REQUEST_DEFAULT);
204 
207 
209  virtual ~vtkFreeTypeTools();
210 
211 private:
212  vtkFreeTypeTools(const vtkFreeTypeTools&); // Not implemented.
213  void operator=(const vtkFreeTypeTools&); // Not implemented.
214 
216 
217  bool GetFace(vtkTextProperty *prop, unsigned long &prop_cache_id,
218  FT_Face &face, bool &face_has_kerning);
220 
222 
223  FT_Bitmap* GetBitmap(FT_UInt32 c, unsigned long prop_cache_id,
224  int prop_font_size, FT_UInt &gindex,
225  FT_BitmapGlyph &bitmap_glyph);
227 
228  // The singleton instance and the singleton cleanup instance
229  static vtkFreeTypeTools* Instance;
230  static vtkFreeTypeToolsCleanup Cleanup;
231 
232  // The cache manager, image cache and charmap cache
233  FTC_Manager *CacheManager;
234  FTC_ImageCache *ImageCache;
235  FTC_CMapCache *CMapCache;
236 
238 
239  FTC_Manager* GetCacheManager();
240  FTC_ImageCache* GetImageCache();
241  FTC_CMapCache* GetCMapCache();
243 
244  unsigned int MaximumNumberOfFaces;
245  unsigned int MaximumNumberOfSizes;
246  unsigned long MaximumNumberOfBytes;
247 
248  void InitializeCacheManager();
249  void ReleaseCacheManager();
250 };
251 
252 #endif