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:
66  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
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 dpi, int bbox[4]);
107  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
108  int dpi, int bbox[4]);
110 
112 
114  bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
115  vtkTextRenderer::Metrics &metrics);
116  bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str, int dpi,
117  vtkTextRenderer::Metrics &metrics);
119 
121 
129  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
130  vtkImageData *data, int textDims[2] = NULL);
131  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
132  int dpi, vtkImageData *data, int textDims[2] = NULL);
134 
136 
140  bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
141  vtkPath *path);
142  bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
143  int dpi, vtkPath *path);
145 
147 
150  int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
151  int dpi, int targetWidth, int targetHeight);
152  int GetConstrainedFontSize(const vtkUnicodeString &str,
153  vtkTextProperty *tprop, int dpi,
154  int targetWidth, int targetHeight);
156 
159  static vtkTypeUInt16 HashString(const char *str);
160 
162 
170  void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id);
171  void MapIdToTextProperty(size_t 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, int dpi, 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(size_t tprop_cache_id, int font_size, FT_Size *size);
261  bool GetSize(FTC_Scaler scaler, FT_Size *size);
262  bool GetFace(size_t tprop_cache_id, FT_Face *face);
263  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c,
264  FT_UInt *gindex);
265  bool GetGlyph(size_t tprop_cache_id,
266  int font_size,
267  FT_UInt gindex,
268  FT_Glyph *glyph,
269  int request = GLYPH_REQUEST_DEFAULT);
270  bool GetGlyph(FTC_Scaler scaler,
271  FT_UInt gindex,
272  FT_Glyph *glyph,
273  int request = GLYPH_REQUEST_DEFAULT);
275 
278 
280  virtual ~vtkFreeTypeTools();
281 
283 
284  bool GetFace(vtkTextProperty *prop, size_t &prop_cache_id,
285  FT_Face &face, bool &face_has_kerning);
287 
289 
290  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id,
291  int prop_font_size, FT_UInt &gindex,
292  FT_BitmapGlyph &bitmap_glyph);
293  FT_Bitmap* GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
294  FT_BitmapGlyph &bitmap_glyph);
296 
298 
299  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id,
300  int prop_font_size, FT_UInt &gindex,
301  FT_OutlineGlyph &outline_glyph);
302  FT_Outline* GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
303  FT_OutlineGlyph &outline_glyph);
305 
307 
311 
314  vtkTextPropertyLookup *TextPropertyLookup;
315 
317 
318  FTC_Manager *CacheManager;
319  FTC_ImageCache *ImageCache;
320  FTC_CMapCache *CMapCache;
322 
324 
325  FTC_Manager* GetCacheManager();
326  FTC_ImageCache* GetImageCache();
327  FTC_CMapCache* GetCMapCache();
329 
330  unsigned int MaximumNumberOfFaces;
331  unsigned int MaximumNumberOfSizes;
332  unsigned long MaximumNumberOfBytes;
333 
336 
337  void InitializeCacheManager();
338  void ReleaseCacheManager();
339 
340 private:
341  vtkFreeTypeTools(const vtkFreeTypeTools&); // Not implemented.
342  void operator=(const vtkFreeTypeTools&); // Not implemented.
343 
345 
346  template <typename StringType>
347  bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
348  int dpi, vtkImageData *data, int textDims[2]);
350 
352 
353  template <typename StringType>
354  bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
355  int dpi, vtkPath *path);
357 
359 
361  template <typename T>
362  bool CalculateBoundingBox(const T& str, MetaData &metaData);
364 
366 
369  template <typename StringType, typename DataType>
370  bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
372 
374 
376  template <typename IteratorType, typename DataType>
377  bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
378  DataType data, MetaData &metaData);
380 
382 
384  template <typename CharType>
385  bool RenderCharacter(CharType character, int &x, int &y,
386  FT_UInt &previousGlyphIndex, vtkImageData *image,
387  MetaData &metaData);
388  template <typename CharType>
389  bool RenderCharacter(CharType character, int &x, int &y,
390  FT_UInt &previousGlyphIndex, vtkPath *path,
391  MetaData &metaData);
393 
395 
398  template <typename T>
399  int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
400  int targetHeight);
402 
404 
409  template<typename T>
410  void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
411  int bbox[4]);
412 };
414 
415 #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
FTC_CMapCache * CMapCache
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:138
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
static vtkFreeTypeToolsCleanup Cleanup
FTC_ImageCache * ImageCache
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:134
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.