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 =========================================================================*/
29 #ifndef vtkFreeTypeTools_h
30 #define vtkFreeTypeTools_h
31 
32 #include "vtkRenderingFreeTypeModule.h" // For export macro
33 #include "vtkObject.h"
34 #include "vtkTextRenderer.h" // For Metrics struct
35 
36 class vtkImageData;
37 class vtkPath;
38 class vtkTextProperty;
39 class vtkStdString;
40 class vtkUnicodeString;
41 
42 // FreeType
43 #include "vtk_freetype.h" //since ft2build.h could be in the path
44 #include FT_FREETYPE_H
45 #include FT_GLYPH_H
46 #include FT_CACHE_H
47 
48 class FTFont;
49 
50 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
51 class vtkTextPropertyLookup;
52 
53 //----------------------------------------------------------------------------
54 // Singleton cleanup
55 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
56 {
57 public:
60 private:
63 };
64 
65 //----------------------------------------------------------------------------
66 // Singleton font cache
67 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
68 {
69 public:
70  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
71  void PrintSelf(ostream& os, vtkIndent indent);
72 
77 
82  static void SetInstance(vtkFreeTypeTools *instance);
83 
85 
89  vtkSetMacro(DebugTextures, bool)
90  vtkGetMacro(DebugTextures, bool)
91  vtkBooleanMacro(DebugTextures, bool)
93 
97  FT_Library* GetLibrary();
98 
100 
105  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
106  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
107  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
108  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
109  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
110  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
112 
114 
122  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
123  int dpi, int bbox[4]);
124  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
125  int dpi, int bbox[4]);
127 
129 
133  bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
134  vtkTextRenderer::Metrics &metrics);
135  bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str, int dpi,
136  vtkTextRenderer::Metrics &metrics);
138 
140 
150  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
151  vtkImageData *data, int textDims[2] = NULL);
152  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
153  int dpi, vtkImageData *data, int textDims[2] = NULL);
155 
157 
163  bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
164  vtkPath *path);
165  bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
166  int dpi, vtkPath *path);
168 
170 
175  int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
176  int dpi, int targetWidth, int targetHeight);
177  int GetConstrainedFontSize(const vtkUnicodeString &str,
178  vtkTextProperty *tprop, int dpi,
179  int targetWidth, int targetHeight);
181 
186  static vtkTypeUInt16 HashString(const char *str);
187 
192  static vtkTypeUInt32 HashBuffer(const void* str, size_t n, vtkTypeUInt32 hash = 0);
193 
195 
205  void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id);
206  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty *tprop);
208 
210 
216  vtkSetMacro(ScaleToPowerTwo, bool);
217  vtkGetMacro(ScaleToPowerTwo, bool);
218  vtkBooleanMacro(ScaleToPowerTwo, bool);
220 
222 
227  vtkSetMacro(ForceCompiledFonts, bool);
228  vtkGetMacro(ForceCompiledFonts, bool);
229  vtkBooleanMacro(ForceCompiledFonts, bool);
231 
238  static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face);
239 
240 protected:
244  virtual FT_Error CreateFTCManager();
245 
247 
250  class MetaData;
251  class ImageMetaData;
252  bool PrepareMetaData(vtkTextProperty *tprop, int dpi, MetaData &metaData);
253  bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image,
254  ImageMetaData &metaData);
256 
261  void PrepareImageData(vtkImageData *data, int bbox[4]);
262 
266  void RenderBackground(vtkTextProperty *tprop, vtkImageData *image,
267  ImageMetaData &metaData);
268 
276  bool GetSize(vtkTextProperty *tprop, FT_Size *size);
277 
284  bool GetFace(vtkTextProperty *tprop, FT_Face *face);
285 
291  bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
292 
294 
306  enum
307  {
308  GLYPH_REQUEST_DEFAULT = 0,
309  GLYPH_REQUEST_BITMAP = 1,
310  GLYPH_REQUEST_OUTLINE = 2
311  };
313  FT_UInt32 c,
314  FT_Glyph *glyph,
315  int request = GLYPH_REQUEST_DEFAULT);
316  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size *size);
317  bool GetSize(FTC_Scaler scaler, FT_Size *size);
318  bool GetFace(size_t tprop_cache_id, FT_Face *face);
319  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c,
320  FT_UInt *gindex);
321  bool GetGlyph(size_t tprop_cache_id,
322  int font_size,
323  FT_UInt gindex,
324  FT_Glyph *glyph,
325  int request = GLYPH_REQUEST_DEFAULT);
326  bool GetGlyph(FTC_Scaler scaler,
327  FT_UInt gindex,
328  FT_Glyph *glyph,
329  int request = GLYPH_REQUEST_DEFAULT);
331 
336 
338  virtual ~vtkFreeTypeTools();
339 
343  bool GetFace(vtkTextProperty *prop, size_t &prop_cache_id,
344  FT_Face &face, bool &face_has_kerning);
345 
347 
350  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id,
351  int prop_font_size, FT_UInt &gindex,
352  FT_BitmapGlyph &bitmap_glyph);
353  FT_Bitmap* GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
354  FT_BitmapGlyph &bitmap_glyph);
356 
358 
361  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id,
362  int prop_font_size, FT_UInt &gindex,
363  FT_OutlineGlyph &outline_glyph);
364  FT_Outline* GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
365  FT_OutlineGlyph &outline_glyph);
367 
372 
376  vtkTextPropertyLookup *TextPropertyLookup;
377 
381  FT_Library *Library;
382 
384 
387  FTC_Manager *CacheManager;
388  FTC_ImageCache *ImageCache;
389  FTC_CMapCache *CMapCache;
391 
393 
396  FTC_Manager* GetCacheManager();
397  FTC_ImageCache* GetImageCache();
398  FTC_CMapCache* GetCMapCache();
400 
401  unsigned int MaximumNumberOfFaces;
402  unsigned int MaximumNumberOfSizes;
403  unsigned long MaximumNumberOfBytes;
404 
407 
410 
411 private:
412  vtkFreeTypeTools(const vtkFreeTypeTools&) VTK_DELETE_FUNCTION;
413  void operator=(const vtkFreeTypeTools&) VTK_DELETE_FUNCTION;
414 
418  template <typename StringType>
419  bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
420  int dpi, vtkImageData *data, int textDims[2]);
421 
425  template <typename StringType>
426  bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
427  int dpi, vtkPath *path);
428 
430 
434  bool CalculateBoundingBox(const vtkStdString& str, MetaData &metaData);
435  bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData &metaData);
436  template <typename T>
437  bool CalculateBoundingBox(const T& str, MetaData &metaData, const T& defaultHeightString);
439 
445  template <typename StringType, typename DataType>
446  bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
447 
451  template <typename IteratorType, typename DataType>
452  bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
453  DataType data, MetaData &metaData);
454 
456 
459  template <typename CharType>
460  bool RenderCharacter(CharType character, int &x, int &y,
461  FT_UInt &previousGlyphIndex, vtkImageData *image,
462  MetaData &metaData);
463  template <typename CharType>
464  bool RenderCharacter(CharType character, int &x, int &y,
465  FT_UInt &previousGlyphIndex, vtkPath *path,
466  MetaData &metaData);
468 
474  template <typename T>
475  int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
476  int targetHeight);
477 
479 
486  template<typename T>
487  void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
488  int bbox[4]);
489 };
491 
492 // This is here to implement the Schwarz counter idiom.
494 
495 #endif
vtkFreeTypeTools::MaximumNumberOfBytes
unsigned long MaximumNumberOfBytes
Definition: vtkFreeTypeTools.h:403
vtkFreeTypeTools::GetCMapCache
FTC_CMapCache * GetCMapCache()
vtkFreeTypeTools::GetBitmap
FT_Bitmap * GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex, FT_BitmapGlyph &bitmap_glyph)
vtkFreeTypeTools::GetFace
bool GetFace(size_t tprop_cache_id, FT_Face *face)
vtkFreeTypeTools::ForceCompiledFonts
bool ForceCompiledFonts
Definition: vtkFreeTypeTools.h:405
vtkFreeTypeTools::DebugTextures
bool DebugTextures
Definition: vtkFreeTypeTools.h:406
vtkFreeTypeTools::MaximumNumberOfSizes
unsigned int MaximumNumberOfSizes
Definition: vtkFreeTypeTools.h:402
vtkX3D::data
@ data
Definition: vtkX3D.h:315
vtkX3D::image
@ image
Definition: vtkX3D.h:374
VTK_UNSIGNED_LONG_MAX
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:159
vtkPath
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:36
vtkFreeTypeTools::InitializeCacheManager
void InitializeCacheManager()
vtkFreeTypeTools::GetBitmap
FT_Bitmap * GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt &gindex, FT_BitmapGlyph &bitmap_glyph)
Now attempt to get the bitmap for the specified character.
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:60
vtkFreeTypeTools::Instance
static vtkFreeTypeTools * Instance
The singleton instance.
Definition: vtkFreeTypeTools.h:371
vtkFreeTypeTools::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
vtkTextRenderer.h
vtkFreeTypeTools::GetGlyphIndex
bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt *gindex)
vtkFreeTypeTools::ScaleToPowerTwo
bool ScaleToPowerTwo
Should the image be scaled to the next highest power of 2?
Definition: vtkFreeTypeTools.h:335
vtkFreeTypeTools::GetSize
bool GetSize(size_t tprop_cache_id, int font_size, FT_Size *size)
vtkFreeTypeTools::GetCacheManager
FTC_Manager * GetCacheManager()
Get the FreeType cache manager, image cache and charmap cache.
vtkFreeTypeTools::ImageCache
FTC_ImageCache * ImageCache
Definition: vtkFreeTypeTools.h:388
vtkFreeTypeTools::~vtkFreeTypeTools
virtual ~vtkFreeTypeTools()
vtkFreeTypeTools::GetOutline
FT_Outline * GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt &gindex, FT_OutlineGlyph &outline_glyph)
Attempt to get the outline for the specified character.
vtkFreeTypeTools::GetGlyph
bool GetGlyph(vtkTextProperty *tprop, FT_UInt32 c, FT_Glyph *glyph, int request=GLYPH_REQUEST_DEFAULT)
vtkFreeTypeTools::CacheManager
FTC_Manager * CacheManager
The cache manager, image cache and charmap cache.
Definition: vtkFreeTypeTools.h:387
vtkFreeTypeTools::TextPropertyLookup
vtkTextPropertyLookup * TextPropertyLookup
Lookup table that maps free type font cache face ids to vtkTextProperties.
Definition: vtkFreeTypeTools.h:376
vtkImageData
topologically and geometrically regular array of data
Definition: vtkImageData.h:46
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:40
vtkUnicodeString
String class that stores Unicode text.
Definition: vtkUnicodeString.h:73
vtkFreeTypeToolsCleanup
Definition: vtkFreeTypeTools.h:56
vtkX3D::size
@ size
Definition: vtkX3D.h:253
vtkFreeTypeTools::SetInstance
static void SetInstance(vtkFreeTypeTools *instance)
Supply a user defined instance.
vtkFreeTypeToolsCleanupInstance
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
Definition: vtkFreeTypeTools.h:493
vtkFreeTypeTools::GetGlyph
bool GetGlyph(FTC_Scaler scaler, FT_UInt gindex, FT_Glyph *glyph, int request=GLYPH_REQUEST_DEFAULT)
vtkObject.h
vtkFreeTypeTools::GetSize
bool GetSize(FTC_Scaler scaler, FT_Size *size)
vtkTextProperty
represent text properties.
Definition: vtkTextProperty.h:40
vtkBooleanMacro
vtkBooleanMacro(IgnoreDriverBugs, bool)
Updates the extensions string.
vtkSetMacro
vtkSetMacro(IgnoreDriverBugs, bool)
Updates the extensions string.
vtkFreeTypeTools::GetGlyph
bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph *glyph, int request=GLYPH_REQUEST_DEFAULT)
vtkFreeTypeTools::vtkFreeTypeTools
vtkFreeTypeTools()
vtkFreeTypeTools::MaximumNumberOfFaces
unsigned int MaximumNumberOfFaces
Definition: vtkFreeTypeTools.h:401
VTK_UNSIGNED_INT_MAX
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:155
vtkTextRenderer
Interface for generating images and path data from string data, using multiple backends.
Definition: vtkTextRenderer.h:76
vtkFreeTypeTools::GetOutline
FT_Outline * GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex, FT_OutlineGlyph &outline_glyph)
vtkFreeTypeToolsCleanup::~vtkFreeTypeToolsCleanup
~vtkFreeTypeToolsCleanup()
vtkFreeTypeTools::Library
FT_Library * Library
FreeType library instance.
Definition: vtkFreeTypeTools.h:381
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:49
vtkFreeTypeTools::GetFace
bool GetFace(vtkTextProperty *prop, size_t &prop_cache_id, FT_Face &face, bool &face_has_kerning)
Attempt to get the typeface of the specified font.
vtkFreeTypeTools::GetInstance
static vtkFreeTypeTools * GetInstance()
Return the singleton instance with no reference counting.
vtkFreeTypeToolsCleanup::vtkFreeTypeToolsCleanup
vtkFreeTypeToolsCleanup()
vtkFreeTypeTools::ReleaseCacheManager
void ReleaseCacheManager()
vtkFreeTypeTools::CMapCache
FTC_CMapCache * CMapCache
Definition: vtkFreeTypeTools.h:389
vtkFreeTypeTools::GetImageCache
FTC_ImageCache * GetImageCache()
vtkFreeTypeTools
FreeType library support.
Definition: vtkFreeTypeTools.h:68