VTK  9.3.20240419
vtkFreeTypeTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
56 #ifndef vtkFreeTypeTools_h
57 #define vtkFreeTypeTools_h
58 
59 #include "vtkObject.h"
60 #include "vtkRenderingFreeTypeModule.h" // For export macro
61 #include "vtkSmartPointer.h" // For smart pointer
62 #include "vtkTextRenderer.h" // For Metrics struct
63 
64 #include <array> // for std::array
65 
66 VTK_ABI_NAMESPACE_BEGIN
67 class vtkImageData;
68 class vtkPath;
69 class vtkTextProperty;
70 class vtkStdString;
71 
72 // FreeType
73 VTK_ABI_NAMESPACE_END
74 #include "vtk_freetype.h" //since ft2build.h could be in the path
75 #include FT_FREETYPE_H
76 #include FT_GLYPH_H
77 #include FT_CACHE_H
78 
79 VTK_ABI_NAMESPACE_BEGIN
80 class FTFont;
81 
82 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
83 class vtkTextPropertyLookup;
84 
85 //----------------------------------------------------------------------------
86 // Singleton cleanup
87 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
88 {
89 public:
92 
93 private:
95  vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
96 };
97 
98 //----------------------------------------------------------------------------
99 // Singleton font cache
100 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
101 {
102 public:
103  struct FaceMetrics
104  {
106  int Ascender;
109  std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
111  bool Scalable;
112  bool Bold;
113  bool Italic;
114  };
116  {
119  };
120 
121  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
122  void PrintSelf(ostream& os, vtkIndent indent) override;
123 
128 
133  static void SetInstance(vtkFreeTypeTools* instance);
134 
136 
140  vtkSetMacro(DebugTextures, bool);
141  vtkGetMacro(DebugTextures, bool);
142  vtkBooleanMacro(DebugTextures, bool);
144 
148  FT_Library* GetLibrary();
149 
156 
162  GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty* tprop, vtkTypeUInt32 charId);
163 
170  std::array<int, 2> GetUnscaledKerning(
171  vtkTextProperty* tprop, vtkTypeUInt32 leftChar, vtkTypeUInt32 rightChar);
172 
174 
179  vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
180  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
181  vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
182  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
183  vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
184  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
186 
188 
196  bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
198 
200 
205  vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
207 
209 
219  bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
220  int textDims[2] = nullptr);
222 
224 
230  bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
232 
234 
240  const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
242 
247  static vtkTypeUInt16 HashString(const char* str);
248 
253  static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
254 
256 
266  void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
267  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
269 
271 
277  vtkSetMacro(ScaleToPowerTwo, bool);
278  vtkGetMacro(ScaleToPowerTwo, bool);
279  vtkBooleanMacro(ScaleToPowerTwo, bool);
281 
283 
288  vtkSetMacro(ForceCompiledFonts, bool);
289  vtkGetMacro(ForceCompiledFonts, bool);
290  vtkBooleanMacro(ForceCompiledFonts, bool);
292 
299  static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
300 
301 protected:
305  virtual FT_Error CreateFTCManager();
306 
308 
311  class MetaData;
312  class ImageMetaData;
313  bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
314  bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
316 
321  void PrepareImageData(vtkImageData* data, int bbox[4]);
322 
326  void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
327 
335  bool GetSize(vtkTextProperty* tprop, FT_Size* size);
336 
343  bool GetFace(vtkTextProperty* tprop, FT_Face* face);
344 
350  bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
351 
353 
365  enum
366  {
367  GLYPH_REQUEST_DEFAULT = 0,
368  GLYPH_REQUEST_BITMAP = 1,
369  GLYPH_REQUEST_OUTLINE = 2
370  };
371  bool GetGlyph(
372  vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
373  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
374  bool GetSize(FTC_Scaler scaler, FT_Size* size);
375  bool GetFace(size_t tprop_cache_id, FT_Face* face);
376  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
377  bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
378  int request = GLYPH_REQUEST_DEFAULT);
379  bool GetGlyph(
380  FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
382 
387 
389  ~vtkFreeTypeTools() override;
390 
394  bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
395 
397 
400  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
401  FT_BitmapGlyph& bitmap_glyph);
402  FT_Bitmap* GetBitmap(
403  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
405 
407 
410  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
411  FT_OutlineGlyph& outline_glyph);
412  FT_Outline* GetOutline(
413  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
415 
420 
424  vtkTextPropertyLookup* TextPropertyLookup;
425 
429  FT_Library* Library;
430 
432 
435  FTC_Manager* CacheManager;
436  FTC_ImageCache* ImageCache;
437  FTC_CMapCache* CMapCache;
439 
441 
444  FTC_Manager* GetCacheManager();
445  FTC_ImageCache* GetImageCache();
446  FTC_CMapCache* GetCMapCache();
448 
449  unsigned int MaximumNumberOfFaces;
450  unsigned int MaximumNumberOfSizes;
451  unsigned long MaximumNumberOfBytes;
452 
455 
458 
459 private:
460  vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
461  void operator=(const vtkFreeTypeTools&) = delete;
462 
466  bool RenderStringInternal(
467  vtkTextProperty* tprop, const std::string& str, int dpi, vtkImageData* data, int textDims[2]);
468 
472  bool StringToPathInternal(vtkTextProperty* tprop, const std::string& str, int dpi, vtkPath* path);
473 
475 
479  bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
480  bool CalculateBoundingBox(
481  const std::string& str, MetaData& metaData, const std::string& defaultHeightString);
483 
489  template <typename DataType>
490  bool PopulateData(const std::string& str, DataType data, MetaData& metaData);
491 
495  template <typename DataType>
496  bool RenderLine(std::string::const_iterator begin, std::string::const_iterator end, int lineIndex,
497  DataType data, MetaData& metaData);
498 
500 
503  bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
504  vtkImageData* image, MetaData& metaData);
505  bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
506  vtkPath* path, MetaData& metaData);
508 
509  void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
510 
516  int FitStringToBBox(
517  const std::string& str, MetaData& metaData, int targetWidth, int targetHeight);
518 
520 
527  void GetLineMetrics(std::string::const_iterator begin, std::string::const_iterator end,
528  MetaData& metaData, int& width, int bbox[4]);
530 };
531 
532 // This is here to implement the Schwarz counter idiom.
534 
535 VTK_ABI_NAMESPACE_END
536 #endif
FreeType library support.
bool GetFace(vtkTextProperty *tprop, FT_Face *face)
Given a text property, get the corresponding FreeType face.
void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id)
Given a text property 'tprop', get its unique ID in our cache framework.
static vtkFreeTypeTools * GetInstance()
Return the singleton instance with no reference counting.
FaceMetrics GetFaceMetrics(vtkTextProperty *tprop)
Return some metrics about a font face.
bool PrepareMetaData(vtkTextProperty *tprop, int dpi, MetaData &metaData)
bool GetFace(size_t tprop_cache_id, FT_Face *face)
Given a text property and a character, get the corresponding FreeType glyph.
FTC_ImageCache * ImageCache
The cache manager, image cache and charmap cache.
FTC_CMapCache * GetCMapCache()
Get the FreeType cache manager, image cache and charmap cache.
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.
FT_Outline * GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex, FT_OutlineGlyph &outline_glyph)
Attempt to get the outline for the specified character.
bool GetMetrics(vtkTextProperty *tprop, const vtkStdString &str, int dpi, vtkTextRenderer::Metrics &metrics)
Given a text property and a string, get the metrics of the rendered string.
std::array< int, 2 > GetUnscaledKerning(vtkTextProperty *tprop, vtkTypeUInt32 leftChar, vtkTypeUInt32 rightChar)
Return a 2D vector detailing the unscaled kerning offset for a pair of characters.
void ReleaseCacheManager()
vtkTextPropertyLookup * TextPropertyLookup
Lookup table that maps free type font cache face ids to vtkTextProperties.
FT_Bitmap * GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex, FT_BitmapGlyph &bitmap_glyph)
Now attempt to get the bitmap for the specified character.
bool GetSize(size_t tprop_cache_id, int font_size, FT_Size *size)
Given a text property and a character, get the corresponding FreeType glyph.
bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph *glyph, int request=GLYPH_REQUEST_DEFAULT)
Given a text property and a character, get the corresponding FreeType glyph.
static void SetInstance(vtkFreeTypeTools *instance)
Supply a user defined instance.
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.
static vtkFreeTypeTools * Instance
The singleton instance.
bool GetGlyph(FTC_Scaler scaler, FT_UInt gindex, FT_Glyph *glyph, int request=GLYPH_REQUEST_DEFAULT)
Given a text property and a character, get the corresponding FreeType glyph.
bool GetSize(vtkTextProperty *tprop, FT_Size *size)
Given a text property, get the corresponding FreeType size object (a structure storing both a face an...
void PrepareImageData(vtkImageData *data, int bbox[4])
This function initializes the extent of the ImageData to eventually receive the text stored in str.
bool ScaleToPowerTwo
Should the image be scaled to the next highest power of 2?
FTC_Manager * GetCacheManager()
Get the FreeType cache manager, image cache and charmap cache.
bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt *gindex)
Given a text property and a character, get the corresponding FreeType glyph.
bool GetGlyph(vtkTextProperty *tprop, FT_UInt32 c, FT_Glyph *glyph, int request=GLYPH_REQUEST_DEFAULT)
Given a text property and a character, get the corresponding FreeType glyph.
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.
unsigned int MaximumNumberOfFaces
~vtkFreeTypeTools() override
void RenderBackground(vtkTextProperty *tprop, vtkImageData *image, ImageMetaData &metaData)
Draw the background quad on the image.
bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image, ImageMetaData &metaData)
bool GetSize(FTC_Scaler scaler, FT_Size *size)
Given a text property and a character, get the corresponding FreeType glyph.
bool RenderString(vtkTextProperty *tprop, const vtkStdString &str, int dpi, vtkImageData *data, int textDims[2]=nullptr)
Given a text property and a string, this function initializes the vtkImageData *data and renders it i...
FTC_ImageCache * GetImageCache()
Get the FreeType cache manager, image cache and charmap cache.
bool StringToPath(vtkTextProperty *tprop, const vtkStdString &str, int dpi, vtkPath *path)
Given a text property and a string, this function populates the vtkPath path with the outline of the ...
FTC_Manager * CacheManager
The cache manager, image cache and charmap cache.
unsigned int MaximumNumberOfSizes
static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face)
Lookup and set the FreeType font face face best matching the text property tprop using the compiled A...
void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty *tprop)
Given a text property 'tprop', get its unique ID in our cache framework.
bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex)
Given a text property and a character, get the corresponding FreeType glyph index.
FTC_CMapCache * CMapCache
The cache manager, image cache and charmap cache.
bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString &str, int dpi, int bbox[4])
Given a text property and a string, get the bounding box {xmin, xmax, ymin, ymax} of the rendered str...
int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop, int dpi, int targetWidth, int targetHeight)
This function returns the font size (in points) required to fit the string in the target rectangle.
FT_Library * Library
FreeType library instance.
virtual FT_Error CreateFTCManager()
Create the FreeType Cache manager instance and set this->CacheManager.
GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty *tprop, vtkTypeUInt32 charId)
Return a generic outline of a glyph with some additional metadata.
FT_Library * GetLibrary()
Get the FreeType library singleton.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
unsigned long MaximumNumberOfBytes
static vtkTypeUInt32 HashBuffer(const void *buffer, size_t n, vtkTypeUInt32 hash=0)
Hash a buffer of a given length.
static vtkTypeUInt16 HashString(const char *str)
Turn a string into a hash.
void InitializeCacheManager()
topologically and geometrically regular array of data
Definition: vtkImageData.h:156
a simple class to control print indentation
Definition: vtkIndent.h:108
abstract base class for most VTK objects
Definition: vtkObject.h:162
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:44
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:78
represent text properties.
@ image
Definition: vtkX3D.h:374
@ size
Definition: vtkX3D.h:253
@ data
Definition: vtkX3D.h:315
@ string
Definition: vtkX3D.h:490
std::array< int, 4 > BoundingBox
vtkSmartPointer< vtkPath > Path
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:150
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:146