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 =========================================================================*/
28 #ifndef __vtkFreeTypeTools_h
29 #define __vtkFreeTypeTools_h
30 
31 #include "vtkRenderingFreeTypeModule.h" // For export macro
32 #include "vtkObject.h"
33 
34 class vtkImageData;
35 class vtkPath;
36 class vtkTextProperty;
37 class vtkStdString;
38 class vtkUnicodeString;
39 
40 // FreeType
41 #include "vtk_freetype.h" //since ft2build.h could be in the path
42 #include FT_FREETYPE_H
43 #include FT_GLYPH_H
44 #include FT_CACHE_H
45 
46 class FTFont;
47 
48 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
49 class vtkTextPropertyLookup;
50 
51 //----------------------------------------------------------------------------
52 // Singleton cleanup
54 {
55 public:
58 };
59 
60 //----------------------------------------------------------------------------
61 // Singleton font cache
63 {
64 public:
66  void PrintSelf(ostream& os, vtkIndent indent);
67 
69  static vtkFreeTypeTools* GetInstance();
70 
73  static void SetInstance(vtkFreeTypeTools *instance);
74 
76  FT_Library* GetLibrary();
77 
79 
82  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
83  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
84  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
85  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
86  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
87  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
89 
91 
100  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
101  int bbox[4]);
102  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
103  int bbox[4]);
104  bool IsBoundingBoxValid(int bbox[4]);
106 
108 
114  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str,
115  vtkImageData *data, int textDims[2] = NULL);
116  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
117  vtkImageData *data, int textDims[2] = NULL);
119 
121 
123  bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str,
124  vtkPath *path);
125  bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
126  vtkPath *path);
128 
130 
133  int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
134  int targetWidth, int targetHeight);
135  int GetConstrainedFontSize(const vtkUnicodeString &str,
136  vtkTextProperty *tprop,
137  int targetWidth, int targetHeight);
139 
142  static vtkTypeUInt16 HashString(const char *str);
143 
145 
153  void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id);
154  void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop);
156 
158 
162  vtkSetMacro(ScaleToPowerTwo, bool);
163  vtkGetMacro(ScaleToPowerTwo, bool);
164  vtkBooleanMacro(ScaleToPowerTwo, bool);
166 
168 
171  vtkSetMacro(ForceCompiledFonts, bool);
172  vtkGetMacro(ForceCompiledFonts, bool);
173  vtkBooleanMacro(ForceCompiledFonts, bool);
175 
180  static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face);
181 
182 protected:
184  virtual FT_Error CreateFTCManager();
185 
187 
189  class MetaData;
190  class ImageMetaData;
191  bool PrepareMetaData(vtkTextProperty *tprop, MetaData &metaData);
192  bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image,
193  ImageMetaData &metaData);
195 
197 
198  template <typename StringType>
199  bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
200  vtkImageData *data, int textDims[2]);
202 
204 
205  template <typename StringType>
206  bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
207  vtkPath *path);
209 
211 
214  template <typename T>
215  bool CalculateBoundingBox(const T& str, MetaData &metaData);
217 
220  void PrepareImageData(vtkImageData *data, int bbox[4]);
221 
223 
226  template <typename StringType, typename DataType>
227  bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
229 
231 
233  template <typename IteratorType, typename DataType>
234  bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
235  DataType data, MetaData &metaData);
237 
239 
241  template <typename CharType>
242  bool RenderCharacter(CharType character, int &x, int &y,
243  FT_UInt &previousGlyphIndex, vtkImageData *image,
244  MetaData &metaData);
245  template <typename CharType>
246  bool RenderCharacter(CharType character, int &x, int &y,
247  FT_UInt &previousGlyphIndex, vtkPath *path,
248  MetaData &metaData);
250 
252  void JustifyPath(vtkPath *path, MetaData &metaData);
253 
255 
258  template <typename T>
259  int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
260  int targetHeight);
262 
267  bool GetSize(vtkTextProperty *tprop, FT_Size *size);
268 
273  bool GetFace(vtkTextProperty *tprop, FT_Face *face);
274 
277  bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
278 
280 
290  enum
291  {
292  GLYPH_REQUEST_DEFAULT = 0,
293  GLYPH_REQUEST_BITMAP = 1,
294  GLYPH_REQUEST_OUTLINE = 2
295  };
296  bool GetGlyph(vtkTextProperty *tprop,
297  FT_UInt32 c,
298  FT_Glyph *glyph,
299  int request = GLYPH_REQUEST_DEFAULT);
300  bool GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size);
301  bool GetFace(unsigned long tprop_cache_id, FT_Face *face);
302  bool GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c,
303  FT_UInt *gindex);
304  bool GetGlyph(unsigned long tprop_cache_id,
305  int font_size,
306  FT_UInt gindex,
307  FT_Glyph *glyph,
308  int request = GLYPH_REQUEST_DEFAULT);
310 
313 
315  virtual ~vtkFreeTypeTools();
316 
318 
319  bool GetFace(vtkTextProperty *prop, unsigned long &prop_cache_id,
320  FT_Face &face, bool &face_has_kerning);
322 
324 
325  FT_Bitmap* GetBitmap(FT_UInt32 c, unsigned long prop_cache_id,
326  int prop_font_size, FT_UInt &gindex,
327  FT_BitmapGlyph &bitmap_glyph);
329 
331 
332  FT_Outline* GetOutline(FT_UInt32 c, unsigned long prop_cache_id,
333  int prop_font_size, FT_UInt &gindex,
334  FT_OutlineGlyph &outline_glyph);
336 
338 
343  template<typename T>
344  void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
345  int bbox[4]);
347 
349 
353 
356  vtkTextPropertyLookup *TextPropertyLookup;
357 
359 
360  FTC_Manager *CacheManager;
361  FTC_ImageCache *ImageCache;
362  FTC_CMapCache *CMapCache;
364 
366 
367  FTC_Manager* GetCacheManager();
368  FTC_ImageCache* GetImageCache();
369  FTC_CMapCache* GetCMapCache();
371 
372  unsigned int MaximumNumberOfFaces;
373  unsigned int MaximumNumberOfSizes;
374  unsigned long MaximumNumberOfBytes;
375 
377 
378  void InitializeCacheManager();
379  void ReleaseCacheManager();
380 
381 private:
382  vtkFreeTypeTools(const vtkFreeTypeTools&); // Not implemented.
383  void operator=(const vtkFreeTypeTools&); // Not implemented.
384 };
385 
386 #endif
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: vtkgl.h:11339
GLsizeiptr size
Definition: vtkgl.h:11843
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:46
abstract base class for most VTK objects
Definition: vtkObject.h:61
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: vtkgl.h:11341
unsigned long MaximumNumberOfBytes
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:34
vtkTextPropertyLookup * TextPropertyLookup
#define vtkGetMacro(name, type)
Definition: vtkSetGet.h:83
FTC_CMapCache * CMapCache
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:137
FTC_Manager * CacheManager
#define vtkSetClampMacro(name, type, min, max)
Definition: vtkSetGet.h:133
static vtkFreeTypeTools * Instance
#define VTKRENDERINGFREETYPE_EXPORT
GLint GLint GLint GLint GLint GLint y
Definition: vtkgl.h:11318
#define vtkTypeMacro(thisClass, superclass)
Definition: vtkSetGet.h:619
unsigned int MaximumNumberOfSizes
GLint GLint GLint GLint GLint x
Definition: vtkgl.h:11318
virtual void PrintSelf(ostream &os, vtkIndent indent)
a simple class to control print indentation
Definition: vtkIndent.h:38
const GLubyte * c
Definition: vtkgl.h:15720
topologically and geometrically regular array of data
Definition: vtkImageData.h:44
GLint GLint GLsizei width
Definition: vtkgl.h:11316
static vtkFreeTypeToolsCleanup Cleanup
FTC_ImageCache * ImageCache
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:133
GLsizei const GLchar ** path
Definition: vtkgl.h:13835
represent text properties.
GLuint GLuint end
Definition: vtkgl.h:11315
unsigned int MaximumNumberOfFaces
#define vtkBooleanMacro(name, type)
Definition: vtkSetGet.h:234
FreeType library support.
GLenum GLuint GLint GLenum face
Definition: vtkgl.h:13568
String class that stores Unicode text.
#define vtkSetMacro(name, type)
Definition: vtkSetGet.h:69