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 #include "vtkTextRenderer.h" // For Metrics struct
34 
35 class vtkImageData;
36 class vtkPath;
37 class vtkTextProperty;
38 class vtkStdString;
39 class vtkUnicodeString;
40 
41 // FreeType
42 #include "vtk_freetype.h" //since ft2build.h could be in the path
43 #include FT_FREETYPE_H
44 #include FT_GLYPH_H
45 #include FT_CACHE_H
46 
47 class FTFont;
48 
49 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
50 class vtkTextPropertyLookup;
51 
52 //----------------------------------------------------------------------------
53 // Singleton cleanup
55 {
56 public:
59 };
60 
61 //----------------------------------------------------------------------------
62 // Singleton font cache
64 {
65 public:
67  void PrintSelf(ostream& os, vtkIndent indent);
68 
70  static vtkFreeTypeTools* GetInstance();
71 
74  static void SetInstance(vtkFreeTypeTools *instance);
75 
77 
79  vtkSetMacro(DebugTextures, bool)
80  vtkGetMacro(DebugTextures, bool)
81  vtkBooleanMacro(DebugTextures, bool)
83 
85  FT_Library* GetLibrary();
86 
88 
91  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
92  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
93  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
94  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
95  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
96  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
98 
100 
105  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
106  int bbox[4]);
107  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
108  int bbox[4]);
110 
112 
114  bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str,
115  vtkTextRenderer::Metrics &metrics);
116  bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str,
117  vtkTextRenderer::Metrics &metrics);
119 
121 
129  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str,
130  vtkImageData *data, int textDims[2] = NULL);
131  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
132  vtkImageData *data, int textDims[2] = NULL);
134 
136 
140  bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str,
141  vtkPath *path);
142  bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
143  vtkPath *path);
145 
147 
150  int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
151  int targetWidth, int targetHeight);
152  int GetConstrainedFontSize(const vtkUnicodeString &str,
153  vtkTextProperty *tprop,
154  int targetWidth, int targetHeight);
156 
159  static vtkTypeUInt16 HashString(const char *str);
160 
162 
170  void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id);
171  void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop);
173 
175 
179  vtkSetMacro(ScaleToPowerTwo, bool);
180  vtkGetMacro(ScaleToPowerTwo, bool);
181  vtkBooleanMacro(ScaleToPowerTwo, bool);
183 
185 
188  vtkSetMacro(ForceCompiledFonts, bool);
189  vtkGetMacro(ForceCompiledFonts, bool);
190  vtkBooleanMacro(ForceCompiledFonts, bool);
192 
197  static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face);
198 
199 protected:
201  virtual FT_Error CreateFTCManager();
202 
204 
206  class MetaData;
207  class ImageMetaData;
208  bool PrepareMetaData(vtkTextProperty *tprop, MetaData &metaData);
209  bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image,
210  ImageMetaData &metaData);
212 
215  void PrepareImageData(vtkImageData *data, int bbox[4]);
216 
218 
219  void RenderBackground(vtkTextProperty *tprop, vtkImageData *image,
220  ImageMetaData &metaData);
222 
227  bool GetSize(vtkTextProperty *tprop, FT_Size *size);
228 
233  bool GetFace(vtkTextProperty *tprop, FT_Face *face);
234 
237  bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
238 
240 
250  enum
251  {
252  GLYPH_REQUEST_DEFAULT = 0,
253  GLYPH_REQUEST_BITMAP = 1,
254  GLYPH_REQUEST_OUTLINE = 2
255  };
256  bool GetGlyph(vtkTextProperty *tprop,
257  FT_UInt32 c,
258  FT_Glyph *glyph,
259  int request = GLYPH_REQUEST_DEFAULT);
260  bool GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size);
261  bool GetFace(unsigned long tprop_cache_id, FT_Face *face);
262  bool GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c,
263  FT_UInt *gindex);
264  bool GetGlyph(unsigned long tprop_cache_id,
265  int font_size,
266  FT_UInt gindex,
267  FT_Glyph *glyph,
268  int request = GLYPH_REQUEST_DEFAULT);
270 
273 
275  virtual ~vtkFreeTypeTools();
276 
278 
279  bool GetFace(vtkTextProperty *prop, unsigned long &prop_cache_id,
280  FT_Face &face, bool &face_has_kerning);
282 
284 
285  FT_Bitmap* GetBitmap(FT_UInt32 c, unsigned long prop_cache_id,
286  int prop_font_size, FT_UInt &gindex,
287  FT_BitmapGlyph &bitmap_glyph);
289 
291 
292  FT_Outline* GetOutline(FT_UInt32 c, unsigned long prop_cache_id,
293  int prop_font_size, FT_UInt &gindex,
294  FT_OutlineGlyph &outline_glyph);
296 
298 
302 
305  vtkTextPropertyLookup *TextPropertyLookup;
306 
308 
309  FTC_Manager *CacheManager;
310  FTC_ImageCache *ImageCache;
311  FTC_CMapCache *CMapCache;
313 
315 
316  FTC_Manager* GetCacheManager();
317  FTC_ImageCache* GetImageCache();
318  FTC_CMapCache* GetCMapCache();
320 
321  unsigned int MaximumNumberOfFaces;
322  unsigned int MaximumNumberOfSizes;
323  unsigned long MaximumNumberOfBytes;
324 
327 
328  void InitializeCacheManager();
329  void ReleaseCacheManager();
330 
331 private:
332  vtkFreeTypeTools(const vtkFreeTypeTools&); // Not implemented.
333  void operator=(const vtkFreeTypeTools&); // Not implemented.
334 
336 
337  template <typename StringType>
338  bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
339  vtkImageData *data, int textDims[2]);
341 
343 
344  template <typename StringType>
345  bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
346  vtkPath *path);
348 
350 
352  template <typename T>
353  bool CalculateBoundingBox(const T& str, MetaData &metaData);
355 
357 
360  template <typename StringType, typename DataType>
361  bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
363 
365 
367  template <typename IteratorType, typename DataType>
368  bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
369  DataType data, MetaData &metaData);
371 
373 
375  template <typename CharType>
376  bool RenderCharacter(CharType character, int &x, int &y,
377  FT_UInt &previousGlyphIndex, vtkImageData *image,
378  MetaData &metaData);
379  template <typename CharType>
380  bool RenderCharacter(CharType character, int &x, int &y,
381  FT_UInt &previousGlyphIndex, vtkPath *path,
382  MetaData &metaData);
384 
386 
389  template <typename T>
390  int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
391  int targetHeight);
393 
395 
400  template<typename T>
401  void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
402  int bbox[4]);
403 };
405 
406 #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:138
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:632
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:134
GLsizei const GLchar ** path
Definition: vtkgl.h:13835
represent text properties.
GLuint GLuint end
Definition: vtkgl.h:11315
Interface for generating images and path data from string data, using multiple backends.
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