VTK  9.0.1
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 =========================================================================*/
26 #ifndef vtkFreeTypeTools_h
27 #define vtkFreeTypeTools_h
28 
29 #include "vtkObject.h"
30 #include "vtkRenderingFreeTypeModule.h" // For export macro
31 #include "vtkSmartPointer.h" // For smart pointer
32 #include "vtkTextRenderer.h" // For Metrics struct
33 #include "vtkUnicodeString.h" // For vtkUnicodeStringValueType
34 
35 #include <array> // for std::array
36 
37 class vtkImageData;
38 class vtkPath;
39 class vtkTextProperty;
40 class vtkStdString;
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 
61 private:
63  vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
64 };
65 
66 //----------------------------------------------------------------------------
67 // Singleton font cache
68 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
69 {
70 public:
71  struct FaceMetrics
72  {
74  int Ascender;
75  int Descender;
77  std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
79  bool Scalable;
80  bool Bold;
81  bool Italic;
82  };
83  struct GlyphOutline
84  {
87  };
88 
89  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
90  void PrintSelf(ostream& os, vtkIndent indent) override;
91 
95  static vtkFreeTypeTools* GetInstance();
96 
101  static void SetInstance(vtkFreeTypeTools* instance);
102 
104 
108  vtkSetMacro(DebugTextures, bool);
109  vtkGetMacro(DebugTextures, bool);
110  vtkBooleanMacro(DebugTextures, bool);
112 
116  FT_Library* GetLibrary();
117 
123  FaceMetrics GetFaceMetrics(vtkTextProperty* tprop);
124 
130  GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty* tprop, vtkUnicodeStringValueType charId);
131 
138  std::array<int, 2> GetUnscaledKerning(vtkTextProperty* tprop, vtkUnicodeStringValueType leftChar,
139  vtkUnicodeStringValueType rightChar);
140 
142 
147  vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
148  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
149  vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
150  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
151  vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
152  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
154 
156 
164  bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
165  bool GetBoundingBox(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, int bbox[4]);
167 
169 
173  bool GetMetrics(
174  vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
175  bool GetMetrics(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
176  vtkTextRenderer::Metrics& metrics);
178 
180 
190  bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
191  int textDims[2] = nullptr);
192  bool RenderString(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
193  vtkImageData* data, int textDims[2] = nullptr);
195 
197 
203  bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
204  bool StringToPath(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, vtkPath* path);
206 
208 
213  int GetConstrainedFontSize(
214  const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
215  int GetConstrainedFontSize(const vtkUnicodeString& str, vtkTextProperty* tprop, int dpi,
216  int targetWidth, int targetHeight);
218 
223  static vtkTypeUInt16 HashString(const char* str);
224 
229  static vtkTypeUInt32 HashBuffer(const void* str, size_t n, vtkTypeUInt32 hash = 0);
230 
232 
242  void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
243  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
245 
247 
253  vtkSetMacro(ScaleToPowerTwo, bool);
254  vtkGetMacro(ScaleToPowerTwo, bool);
255  vtkBooleanMacro(ScaleToPowerTwo, bool);
257 
259 
264  vtkSetMacro(ForceCompiledFonts, bool);
265  vtkGetMacro(ForceCompiledFonts, bool);
266  vtkBooleanMacro(ForceCompiledFonts, bool);
268 
275  static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
276 
277 protected:
281  virtual FT_Error CreateFTCManager();
282 
284 
287  class MetaData;
288  class ImageMetaData;
289  bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
290  bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
292 
297  void PrepareImageData(vtkImageData* data, int bbox[4]);
298 
302  void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
303 
311  bool GetSize(vtkTextProperty* tprop, FT_Size* size);
312 
319  bool GetFace(vtkTextProperty* tprop, FT_Face* face);
320 
326  bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
327 
329 
341  enum
342  {
343  GLYPH_REQUEST_DEFAULT = 0,
344  GLYPH_REQUEST_BITMAP = 1,
345  GLYPH_REQUEST_OUTLINE = 2
346  };
347  bool GetGlyph(
348  vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
349  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
350  bool GetSize(FTC_Scaler scaler, FT_Size* size);
351  bool GetFace(size_t tprop_cache_id, FT_Face* face);
352  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
353  bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
354  int request = GLYPH_REQUEST_DEFAULT);
355  bool GetGlyph(
356  FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
358 
363 
365  ~vtkFreeTypeTools() override;
366 
370  bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
371 
373 
376  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
377  FT_BitmapGlyph& bitmap_glyph);
378  FT_Bitmap* GetBitmap(
379  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
381 
383 
386  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
387  FT_OutlineGlyph& outline_glyph);
388  FT_Outline* GetOutline(
389  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
391 
396 
400  vtkTextPropertyLookup* TextPropertyLookup;
401 
405  FT_Library* Library;
406 
408 
411  FTC_Manager* CacheManager;
412  FTC_ImageCache* ImageCache;
413  FTC_CMapCache* CMapCache;
415 
417 
420  FTC_Manager* GetCacheManager();
421  FTC_ImageCache* GetImageCache();
422  FTC_CMapCache* GetCMapCache();
424 
425  unsigned int MaximumNumberOfFaces;
426  unsigned int MaximumNumberOfSizes;
427  unsigned long MaximumNumberOfBytes;
428 
431 
432  void InitializeCacheManager();
433  void ReleaseCacheManager();
434 
435 private:
436  vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
437  void operator=(const vtkFreeTypeTools&) = delete;
438 
442  template <typename StringType>
443  bool RenderStringInternal(
444  vtkTextProperty* tprop, const StringType& str, int dpi, vtkImageData* data, int textDims[2]);
445 
449  template <typename StringType>
450  bool StringToPathInternal(vtkTextProperty* tprop, const StringType& str, int dpi, vtkPath* path);
451 
453 
457  bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
458  bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData& metaData);
459  template <typename T>
460  bool CalculateBoundingBox(const T& str, MetaData& metaData, const T& defaultHeightString);
462 
468  template <typename StringType, typename DataType>
469  bool PopulateData(const StringType& str, DataType data, MetaData& metaData);
470 
474  template <typename IteratorType, typename DataType>
475  bool RenderLine(
476  IteratorType begin, IteratorType end, int lineIndex, DataType data, MetaData& metaData);
477 
479 
482  template <typename CharType>
483  bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
484  vtkImageData* image, MetaData& metaData);
485  template <typename CharType>
486  bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
487  vtkPath* path, MetaData& metaData);
489 
490  void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
491 
497  template <typename T>
498  int FitStringToBBox(const T& str, MetaData& metaData, int targetWidth, int targetHeight);
499 
501 
508  template <typename T>
509  void GetLineMetrics(T begin, T end, MetaData& metaData, int& width, int bbox[4]);
511 };
512 
513 // This is here to implement the Schwarz counter idiom.
515 
516 #endif
vtkFreeTypeTools::MaximumNumberOfBytes
unsigned long MaximumNumberOfBytes
Definition: vtkFreeTypeTools.h:427
vtkFreeTypeTools::ForceCompiledFonts
bool ForceCompiledFonts
Definition: vtkFreeTypeTools.h:429
vtkFreeTypeTools::DebugTextures
bool DebugTextures
Definition: vtkFreeTypeTools.h:430
vtkFreeTypeTools::MaximumNumberOfSizes
unsigned int MaximumNumberOfSizes
Definition: vtkFreeTypeTools.h:426
vtkFreeTypeTools::FaceMetrics::FamilyName
std::string FamilyName
Definition: vtkFreeTypeTools.h:78
vtkX3D::data
@ data
Definition: vtkX3D.h:321
vtkX3D::image
@ image
Definition: vtkX3D.h:380
vtkUnicodeString.h
vtkFreeTypeTools::FaceMetrics::UnitsPerEM
int UnitsPerEM
Definition: vtkFreeTypeTools.h:73
VTK_UNSIGNED_LONG_MAX
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:161
vtkPath
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:32
vtkFreeTypeTools::GlyphOutline::HorizAdvance
int HorizAdvance
Definition: vtkFreeTypeTools.h:85
vtkSmartPointer< vtkPath >
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkFreeTypeTools::Instance
static vtkFreeTypeTools * Instance
The singleton instance.
Definition: vtkFreeTypeTools.h:395
vtkTextRenderer.h
vtkFreeTypeTools::ScaleToPowerTwo
bool ScaleToPowerTwo
Should the image be scaled to the next highest power of 2?
Definition: vtkFreeTypeTools.h:362
vtkFreeTypeTools::FaceMetrics::Italic
bool Italic
Definition: vtkFreeTypeTools.h:81
vtkFreeTypeTools::ImageCache
FTC_ImageCache * ImageCache
Definition: vtkFreeTypeTools.h:412
vtkFreeTypeTools::CacheManager
FTC_Manager * CacheManager
The cache manager, image cache and charmap cache.
Definition: vtkFreeTypeTools.h:411
vtkFreeTypeTools::FaceMetrics::Bold
bool Bold
Definition: vtkFreeTypeTools.h:80
vtkFreeTypeTools::TextPropertyLookup
vtkTextPropertyLookup * TextPropertyLookup
Lookup table that maps free type font cache face ids to vtkTextProperties.
Definition: vtkFreeTypeTools.h:400
vtkImageData
topologically and geometrically regular array of data
Definition: vtkImageData.h:41
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkFreeTypeTools::GlyphOutline
Definition: vtkFreeTypeTools.h:83
vtkUnicodeString
String class that stores Unicode text.
Definition: vtkUnicodeString.h:69
vtkFreeTypeToolsCleanup
Definition: vtkFreeTypeTools.h:55
vtkUnicodeStringValueType
vtkTypeUInt32 vtkUnicodeStringValueType
Definition: vtkUnicodeString.h:55
vtkSmartPointer.h
vtkFreeTypeTools::FaceMetrics
Definition: vtkFreeTypeTools.h:71
vtkFreeTypeTools::FaceMetrics::Ascender
int Ascender
Definition: vtkFreeTypeTools.h:74
vtkX3D::size
@ size
Definition: vtkX3D.h:259
vtkFreeTypeToolsCleanupInstance
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
Definition: vtkFreeTypeTools.h:514
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkObject.h
vtkFreeTypeTools::FaceMetrics::HorizAdvance
int HorizAdvance
Definition: vtkFreeTypeTools.h:76
vtkTextProperty
represent text properties.
Definition: vtkTextProperty.h:33
vtkX3D::string
@ string
Definition: vtkX3D.h:496
vtkFreeTypeTools::FaceMetrics::BoundingBox
std::array< int, 4 > BoundingBox
Definition: vtkFreeTypeTools.h:77
vtkFreeTypeTools::FaceMetrics::Descender
int Descender
Definition: vtkFreeTypeTools.h:75
vtkFreeTypeTools::MaximumNumberOfFaces
unsigned int MaximumNumberOfFaces
Definition: vtkFreeTypeTools.h:425
VTK_UNSIGNED_INT_MAX
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:157
vtkFreeTypeTools::Library
FT_Library * Library
FreeType library instance.
Definition: vtkFreeTypeTools.h:405
vtkFreeTypeTools::GlyphOutline::Path
vtkSmartPointer< vtkPath > Path
Definition: vtkFreeTypeTools.h:86
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:34
vtkTextRenderer::Metrics
Definition: vtkTextRenderer.h:76
vtkFreeTypeTools::CMapCache
FTC_CMapCache * CMapCache
Definition: vtkFreeTypeTools.h:413
vtkFreeTypeTools::FaceMetrics::Scalable
bool Scalable
Definition: vtkFreeTypeTools.h:79
vtkFreeTypeTools
FreeType library support.
Definition: vtkFreeTypeTools.h:68