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 
76  static vtkFreeTypeTools* GetInstance();
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  };
312  bool GetGlyph(vtkTextProperty *tprop,
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 
408  void InitializeCacheManager();
409  void ReleaseCacheManager();
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
bool ScaleToPowerTwo
Should the image be scaled to the next highest power of 2?
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:47
abstract base class for most VTK objects
Definition: vtkObject.h:59
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
unsigned long MaximumNumberOfBytes
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:35
vtkTextPropertyLookup * TextPropertyLookup
Lookup table that maps free type font cache face ids to vtkTextProperties.
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
FTC_CMapCache * CMapCache
The cache manager, image cache and charmap cache.
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:159
FTC_Manager * CacheManager
The cache manager, image cache and charmap cache.
static vtkFreeTypeTools * Instance
The singleton instance.
unsigned int MaximumNumberOfSizes
a simple class to control print indentation
Definition: vtkIndent.h:39
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
FTC_ImageCache * ImageCache
The cache manager, image cache and charmap cache.
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:155
represent text properties.
vtkSetMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
Interface for generating images and path data from string data, using multiple backends.
unsigned int MaximumNumberOfFaces
vtkBooleanMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
FreeType library support.
String class that stores Unicode text.
FT_Library * Library
FreeType library instance.