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 private:
62 };
63 
64 //----------------------------------------------------------------------------
65 // Singleton font cache
67 {
68 public:
69  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
70  void PrintSelf(ostream& os, vtkIndent indent);
71 
73  static vtkFreeTypeTools* GetInstance();
74 
77  static void SetInstance(vtkFreeTypeTools *instance);
78 
80 
82  vtkSetMacro(DebugTextures, bool)
83  vtkGetMacro(DebugTextures, bool)
84  vtkBooleanMacro(DebugTextures, bool)
86 
88  FT_Library* GetLibrary();
89 
91 
94  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
95  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
96  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
97  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
98  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
99  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
101 
103 
108  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
109  int dpi, int bbox[4]);
110  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
111  int dpi, int bbox[4]);
113 
115 
117  bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
118  vtkTextRenderer::Metrics &metrics);
119  bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str, int dpi,
120  vtkTextRenderer::Metrics &metrics);
122 
124 
132  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
133  vtkImageData *data, int textDims[2] = NULL);
134  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
135  int dpi, vtkImageData *data, int textDims[2] = NULL);
137 
139 
143  bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
144  vtkPath *path);
145  bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
146  int dpi, vtkPath *path);
148 
150 
153  int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
154  int dpi, int targetWidth, int targetHeight);
155  int GetConstrainedFontSize(const vtkUnicodeString &str,
156  vtkTextProperty *tprop, int dpi,
157  int targetWidth, int targetHeight);
159 
162  static vtkTypeUInt16 HashString(const char *str);
163 
166  static vtkTypeUInt32 HashBuffer(const void* str, size_t n, vtkTypeUInt32 hash = 0);
167 
169 
177  void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id);
178  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty *tprop);
180 
182 
186  vtkSetMacro(ScaleToPowerTwo, bool);
187  vtkGetMacro(ScaleToPowerTwo, bool);
188  vtkBooleanMacro(ScaleToPowerTwo, bool);
190 
192 
195  vtkSetMacro(ForceCompiledFonts, bool);
196  vtkGetMacro(ForceCompiledFonts, bool);
197  vtkBooleanMacro(ForceCompiledFonts, bool);
199 
204  static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face);
205 
206 protected:
208  virtual FT_Error CreateFTCManager();
209 
211 
213  class MetaData;
214  class ImageMetaData;
215  bool PrepareMetaData(vtkTextProperty *tprop, int dpi, MetaData &metaData);
216  bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image,
217  ImageMetaData &metaData);
219 
222  void PrepareImageData(vtkImageData *data, int bbox[4]);
223 
225 
226  void RenderBackground(vtkTextProperty *tprop, vtkImageData *image,
227  ImageMetaData &metaData);
229 
234  bool GetSize(vtkTextProperty *tprop, FT_Size *size);
235 
240  bool GetFace(vtkTextProperty *tprop, FT_Face *face);
241 
244  bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
245 
247 
257  enum
258  {
259  GLYPH_REQUEST_DEFAULT = 0,
260  GLYPH_REQUEST_BITMAP = 1,
261  GLYPH_REQUEST_OUTLINE = 2
262  };
263  bool GetGlyph(vtkTextProperty *tprop,
264  FT_UInt32 c,
265  FT_Glyph *glyph,
266  int request = GLYPH_REQUEST_DEFAULT);
267  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size *size);
268  bool GetSize(FTC_Scaler scaler, FT_Size *size);
269  bool GetFace(size_t tprop_cache_id, FT_Face *face);
270  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c,
271  FT_UInt *gindex);
272  bool GetGlyph(size_t tprop_cache_id,
273  int font_size,
274  FT_UInt gindex,
275  FT_Glyph *glyph,
276  int request = GLYPH_REQUEST_DEFAULT);
277  bool GetGlyph(FTC_Scaler scaler,
278  FT_UInt gindex,
279  FT_Glyph *glyph,
280  int request = GLYPH_REQUEST_DEFAULT);
282 
285 
287  virtual ~vtkFreeTypeTools();
288 
290 
291  bool GetFace(vtkTextProperty *prop, size_t &prop_cache_id,
292  FT_Face &face, bool &face_has_kerning);
294 
296 
297  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id,
298  int prop_font_size, FT_UInt &gindex,
299  FT_BitmapGlyph &bitmap_glyph);
300  FT_Bitmap* GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
301  FT_BitmapGlyph &bitmap_glyph);
303 
305 
306  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id,
307  int prop_font_size, FT_UInt &gindex,
308  FT_OutlineGlyph &outline_glyph);
309  FT_Outline* GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
310  FT_OutlineGlyph &outline_glyph);
312 
315 
318  vtkTextPropertyLookup *TextPropertyLookup;
319 
321  FT_Library *Library;
322 
324 
325  FTC_Manager *CacheManager;
326  FTC_ImageCache *ImageCache;
327  FTC_CMapCache *CMapCache;
329 
331 
332  FTC_Manager* GetCacheManager();
333  FTC_ImageCache* GetImageCache();
334  FTC_CMapCache* GetCMapCache();
336 
337  unsigned int MaximumNumberOfFaces;
338  unsigned int MaximumNumberOfSizes;
339  unsigned long MaximumNumberOfBytes;
340 
343 
344  void InitializeCacheManager();
345  void ReleaseCacheManager();
346 
347 private:
348  vtkFreeTypeTools(const vtkFreeTypeTools&); // Not implemented.
349  void operator=(const vtkFreeTypeTools&); // Not implemented.
350 
352 
353  template <typename StringType>
354  bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
355  int dpi, vtkImageData *data, int textDims[2]);
357 
359 
360  template <typename StringType>
361  bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
362  int dpi, vtkPath *path);
364 
366 
368  template <typename T>
369  bool CalculateBoundingBox(const T& str, MetaData &metaData);
371 
373 
376  template <typename StringType, typename DataType>
377  bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
379 
381 
383  template <typename IteratorType, typename DataType>
384  bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
385  DataType data, MetaData &metaData);
387 
389 
391  template <typename CharType>
392  bool RenderCharacter(CharType character, int &x, int &y,
393  FT_UInt &previousGlyphIndex, vtkImageData *image,
394  MetaData &metaData);
395  template <typename CharType>
396  bool RenderCharacter(CharType character, int &x, int &y,
397  FT_UInt &previousGlyphIndex, vtkPath *path,
398  MetaData &metaData);
400 
402 
405  template <typename T>
406  int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
407  int targetHeight);
409 
411 
416  template<typename T>
417  void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
418  int bbox[4]);
419 };
421 
422 // This is here to implement the Schwarz counter idiom.
424 
425 #endif
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:46
abstract base class for most VTK objects
Definition: vtkObject.h:61
unsigned long MaximumNumberOfBytes
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:34
vtkTextPropertyLookup * TextPropertyLookup
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
FTC_CMapCache * CMapCache
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:136
FTC_Manager * CacheManager
static vtkFreeTypeTools * Instance
#define VTKRENDERINGFREETYPE_EXPORT
unsigned int MaximumNumberOfSizes
virtual void PrintSelf(ostream &os, vtkIndent indent)
a simple class to control print indentation
Definition: vtkIndent.h:38
topologically and geometrically regular array of data
Definition: vtkImageData.h:44
FTC_ImageCache * ImageCache
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:132
represent text properties.
Interface for generating images and path data from string data, using multiple backends.
unsigned int MaximumNumberOfFaces
FreeType library support.
String class that stores Unicode text.
FT_Library * Library