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 "vtkSmartPointer.h" // For smart pointer
35 #include "vtkTextRenderer.h" // For Metrics struct
36 #include "vtkUnicodeString.h" // For vtkUnicodeStringValueType
37 
38 #include <array> // for std::array
39 
40 class vtkImageData;
41 class vtkPath;
42 class vtkTextProperty;
43 class vtkStdString;
44 
45 // FreeType
46 #include "vtk_freetype.h" //since ft2build.h could be in the path
47 #include FT_FREETYPE_H
48 #include FT_GLYPH_H
49 #include FT_CACHE_H
50 
51 class FTFont;
52 
53 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
54 class vtkTextPropertyLookup;
55 
56 //----------------------------------------------------------------------------
57 // Singleton cleanup
58 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
59 {
60 public:
63 private:
65  vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
66 };
67 
68 //----------------------------------------------------------------------------
69 // Singleton font cache
70 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
71 {
72 public:
73  struct FaceMetrics
74  {
76  int Ascender;
77  int Descender;
79  std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
81  bool Scalable;
82  bool Bold;
83  bool Italic;
84  };
85  struct GlyphOutline
86  {
89  };
90 
91  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
92  void PrintSelf(ostream& os, vtkIndent indent) override;
93 
97  static vtkFreeTypeTools* GetInstance();
98 
103  static void SetInstance(vtkFreeTypeTools *instance);
104 
106 
110  vtkSetMacro(DebugTextures, bool)
111  vtkGetMacro(DebugTextures, bool)
112  vtkBooleanMacro(DebugTextures, bool)
114 
118  FT_Library* GetLibrary();
119 
125  FaceMetrics GetFaceMetrics(vtkTextProperty *tprop);
126 
132  GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty *tprop,
134 
141  std::array<int, 2> GetUnscaledKerning(vtkTextProperty *tprop,
142  vtkUnicodeStringValueType leftChar,
143  vtkUnicodeStringValueType rightChar);
144 
146 
151  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
152  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
153  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
154  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
155  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
156  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
158 
160 
168  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
169  int dpi, int bbox[4]);
170  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
171  int dpi, int bbox[4]);
173 
175 
179  bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
180  vtkTextRenderer::Metrics &metrics);
181  bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str, int dpi,
182  vtkTextRenderer::Metrics &metrics);
184 
186 
196  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
197  vtkImageData *data, int textDims[2] = nullptr);
198  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
199  int dpi, vtkImageData *data, int textDims[2] = nullptr);
201 
203 
209  bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
210  vtkPath *path);
211  bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
212  int dpi, vtkPath *path);
214 
216 
221  int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
222  int dpi, int targetWidth, int targetHeight);
223  int GetConstrainedFontSize(const vtkUnicodeString &str,
224  vtkTextProperty *tprop, int dpi,
225  int targetWidth, int targetHeight);
227 
232  static vtkTypeUInt16 HashString(const char *str);
233 
238  static vtkTypeUInt32 HashBuffer(const void* str, size_t n, vtkTypeUInt32 hash = 0);
239 
241 
251  void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id);
252  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty *tprop);
254 
256 
262  vtkSetMacro(ScaleToPowerTwo, bool);
263  vtkGetMacro(ScaleToPowerTwo, bool);
264  vtkBooleanMacro(ScaleToPowerTwo, bool);
266 
268 
273  vtkSetMacro(ForceCompiledFonts, bool);
274  vtkGetMacro(ForceCompiledFonts, bool);
275  vtkBooleanMacro(ForceCompiledFonts, bool);
277 
284  static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face);
285 
286 protected:
290  virtual FT_Error CreateFTCManager();
291 
293 
296  class MetaData;
297  class ImageMetaData;
298  bool PrepareMetaData(vtkTextProperty *tprop, int dpi, MetaData &metaData);
299  bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image,
300  ImageMetaData &metaData);
302 
307  void PrepareImageData(vtkImageData *data, int bbox[4]);
308 
312  void RenderBackground(vtkTextProperty *tprop, vtkImageData *image,
313  ImageMetaData &metaData);
314 
322  bool GetSize(vtkTextProperty *tprop, FT_Size *size);
323 
330  bool GetFace(vtkTextProperty *tprop, FT_Face *face);
331 
337  bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
338 
340 
352  enum
353  {
354  GLYPH_REQUEST_DEFAULT = 0,
355  GLYPH_REQUEST_BITMAP = 1,
356  GLYPH_REQUEST_OUTLINE = 2
357  };
358  bool GetGlyph(vtkTextProperty *tprop,
359  FT_UInt32 c,
360  FT_Glyph *glyph,
361  int request = GLYPH_REQUEST_DEFAULT);
362  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size *size);
363  bool GetSize(FTC_Scaler scaler, FT_Size *size);
364  bool GetFace(size_t tprop_cache_id, FT_Face *face);
365  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c,
366  FT_UInt *gindex);
367  bool GetGlyph(size_t tprop_cache_id,
368  int font_size,
369  FT_UInt gindex,
370  FT_Glyph *glyph,
371  int request = GLYPH_REQUEST_DEFAULT);
372  bool GetGlyph(FTC_Scaler scaler,
373  FT_UInt gindex,
374  FT_Glyph *glyph,
375  int request = GLYPH_REQUEST_DEFAULT);
377 
382 
384  ~vtkFreeTypeTools() override;
385 
389  bool GetFace(vtkTextProperty *prop, size_t &prop_cache_id,
390  FT_Face &face, bool &face_has_kerning);
391 
393 
396  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id,
397  int prop_font_size, FT_UInt &gindex,
398  FT_BitmapGlyph &bitmap_glyph);
399  FT_Bitmap* GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
400  FT_BitmapGlyph &bitmap_glyph);
402 
404 
407  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id,
408  int prop_font_size, FT_UInt &gindex,
409  FT_OutlineGlyph &outline_glyph);
410  FT_Outline* GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
411  FT_OutlineGlyph &outline_glyph);
413 
418 
422  vtkTextPropertyLookup *TextPropertyLookup;
423 
427  FT_Library *Library;
428 
430 
433  FTC_Manager *CacheManager;
434  FTC_ImageCache *ImageCache;
435  FTC_CMapCache *CMapCache;
437 
439 
442  FTC_Manager* GetCacheManager();
443  FTC_ImageCache* GetImageCache();
444  FTC_CMapCache* GetCMapCache();
446 
447  unsigned int MaximumNumberOfFaces;
448  unsigned int MaximumNumberOfSizes;
449  unsigned long MaximumNumberOfBytes;
450 
453 
454  void InitializeCacheManager();
455  void ReleaseCacheManager();
456 
457 private:
458  vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
459  void operator=(const vtkFreeTypeTools&) = delete;
460 
464  template <typename StringType>
465  bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
466  int dpi, vtkImageData *data, int textDims[2]);
467 
471  template <typename StringType>
472  bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
473  int dpi, vtkPath *path);
474 
476 
480  bool CalculateBoundingBox(const vtkStdString& str, MetaData &metaData);
481  bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData &metaData);
482  template <typename T>
483  bool CalculateBoundingBox(const T& str, MetaData &metaData, const T& defaultHeightString);
485 
491  template <typename StringType, typename DataType>
492  bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
493 
497  template <typename IteratorType, typename DataType>
498  bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
499  DataType data, MetaData &metaData);
500 
502 
505  template <typename CharType>
506  bool RenderCharacter(CharType character, int &x, int &y,
507  FT_UInt &previousGlyphIndex, vtkImageData *image,
508  MetaData &metaData);
509  template <typename CharType>
510  bool RenderCharacter(CharType character, int &x, int &y,
511  FT_UInt &previousGlyphIndex, vtkPath *path,
512  MetaData &metaData);
514 
515  void OutlineToPath(int x, int y, FT_Outline *outline, vtkPath *path);
516 
522  template <typename T>
523  int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
524  int targetHeight);
525 
527 
534  template<typename T>
535  void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
536  int bbox[4]);
537 };
539 
540 // This is here to implement the Schwarz counter idiom.
542 
543 #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:40
abstract base class for most VTK objects
Definition: vtkObject.h:59
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:165
FTC_Manager * CacheManager
The cache manager, image cache and charmap cache.
static vtkFreeTypeTools * Instance
The singleton instance.
std::array< int, 4 > BoundingBox
vtkSmartPointer< vtkPath > Path
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:161
represent text properties.
Interface for generating images and path data from string data, using multiple backends.
unsigned int MaximumNumberOfFaces
FreeType library support.
vtkTypeUInt32 vtkUnicodeStringValueType
String class that stores Unicode text.
FT_Library * Library
FreeType library instance.