VTK  9.3.0
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
17 #ifndef vtkFreeTypeTools_h
18 #define vtkFreeTypeTools_h
19 
20 #include "vtkObject.h"
21 #include "vtkRenderingFreeTypeModule.h" // For export macro
22 #include "vtkSmartPointer.h" // For smart pointer
23 #include "vtkTextRenderer.h" // For Metrics struct
24 
25 #include <array> // for std::array
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 class vtkImageData;
29 class vtkPath;
30 class vtkTextProperty;
31 class vtkStdString;
32 
33 // FreeType
34 VTK_ABI_NAMESPACE_END
35 #include "vtk_freetype.h" //since ft2build.h could be in the path
36 #include FT_FREETYPE_H
37 #include FT_GLYPH_H
38 #include FT_CACHE_H
39 
40 VTK_ABI_NAMESPACE_BEGIN
41 class FTFont;
42 
43 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
44 class vtkTextPropertyLookup;
45 
46 //----------------------------------------------------------------------------
47 // Singleton cleanup
48 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
49 {
50 public:
53 
54 private:
56  vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
57 };
58 
59 //----------------------------------------------------------------------------
60 // Singleton font cache
61 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
62 {
63 public:
64  struct FaceMetrics
65  {
67  int Ascender;
68  int Descender;
70  std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
72  bool Scalable;
73  bool Bold;
74  bool Italic;
75  };
76  struct GlyphOutline
77  {
80  };
81 
82  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
83  void PrintSelf(ostream& os, vtkIndent indent) override;
84 
89 
94  static void SetInstance(vtkFreeTypeTools* instance);
95 
97 
101  vtkSetMacro(DebugTextures, bool);
102  vtkGetMacro(DebugTextures, bool);
103  vtkBooleanMacro(DebugTextures, bool);
105 
109  FT_Library* GetLibrary();
110 
117 
123  GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty* tprop, vtkTypeUInt32 charId);
124 
131  std::array<int, 2> GetUnscaledKerning(
132  vtkTextProperty* tprop, vtkTypeUInt32 leftChar, vtkTypeUInt32 rightChar);
133 
135 
140  vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
141  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
142  vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
143  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
144  vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
145  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
147 
149 
157  bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
159 
161 
166  vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
168 
170 
180  bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
181  int textDims[2] = nullptr);
183 
185 
191  bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
193 
195 
201  const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
203 
208  static vtkTypeUInt16 HashString(const char* str);
209 
214  static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
215 
217 
227  void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
228  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
230 
232 
238  vtkSetMacro(ScaleToPowerTwo, bool);
239  vtkGetMacro(ScaleToPowerTwo, bool);
240  vtkBooleanMacro(ScaleToPowerTwo, bool);
242 
244 
249  vtkSetMacro(ForceCompiledFonts, bool);
250  vtkGetMacro(ForceCompiledFonts, bool);
251  vtkBooleanMacro(ForceCompiledFonts, bool);
253 
260  static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
261 
262 protected:
266  virtual FT_Error CreateFTCManager();
267 
269 
272  class MetaData;
273  class ImageMetaData;
274  bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
275  bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
277 
282  void PrepareImageData(vtkImageData* data, int bbox[4]);
283 
287  void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
288 
296  bool GetSize(vtkTextProperty* tprop, FT_Size* size);
297 
304  bool GetFace(vtkTextProperty* tprop, FT_Face* face);
305 
311  bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
312 
314 
326  enum
327  {
328  GLYPH_REQUEST_DEFAULT = 0,
329  GLYPH_REQUEST_BITMAP = 1,
330  GLYPH_REQUEST_OUTLINE = 2
331  };
332  bool GetGlyph(
333  vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
334  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
335  bool GetSize(FTC_Scaler scaler, FT_Size* size);
336  bool GetFace(size_t tprop_cache_id, FT_Face* face);
337  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
338  bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
339  int request = GLYPH_REQUEST_DEFAULT);
340  bool GetGlyph(
341  FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
343 
348 
350  ~vtkFreeTypeTools() override;
351 
355  bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
356 
358 
361  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
362  FT_BitmapGlyph& bitmap_glyph);
363  FT_Bitmap* GetBitmap(
364  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
366 
368 
371  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
372  FT_OutlineGlyph& outline_glyph);
373  FT_Outline* GetOutline(
374  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
376 
381 
385  vtkTextPropertyLookup* TextPropertyLookup;
386 
390  FT_Library* Library;
391 
393 
396  FTC_Manager* CacheManager;
397  FTC_ImageCache* ImageCache;
398  FTC_CMapCache* CMapCache;
400 
402 
405  FTC_Manager* GetCacheManager();
406  FTC_ImageCache* GetImageCache();
407  FTC_CMapCache* GetCMapCache();
409 
410  unsigned int MaximumNumberOfFaces;
411  unsigned int MaximumNumberOfSizes;
412  unsigned long MaximumNumberOfBytes;
413 
416 
419 
420 private:
421  vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
422  void operator=(const vtkFreeTypeTools&) = delete;
423 
427  bool RenderStringInternal(
428  vtkTextProperty* tprop, const std::string& str, int dpi, vtkImageData* data, int textDims[2]);
429 
433  bool StringToPathInternal(vtkTextProperty* tprop, const std::string& str, int dpi, vtkPath* path);
434 
436 
440  bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
441  bool CalculateBoundingBox(
442  const std::string& str, MetaData& metaData, const std::string& defaultHeightString);
444 
450  template <typename DataType>
451  bool PopulateData(const std::string& str, DataType data, MetaData& metaData);
452 
456  template <typename DataType>
457  bool RenderLine(std::string::const_iterator begin, std::string::const_iterator end, int lineIndex,
458  DataType data, MetaData& metaData);
459 
461 
464  bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
465  vtkImageData* image, MetaData& metaData);
466  bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
467  vtkPath* path, MetaData& metaData);
469 
470  void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
471 
477  int FitStringToBBox(
478  const std::string& str, MetaData& metaData, int targetWidth, int targetHeight);
479 
481 
488  void GetLineMetrics(std::string::const_iterator begin, std::string::const_iterator end,
489  MetaData& metaData, int& width, int bbox[4]);
491 };
492 
493 // This is here to implement the Schwarz counter idiom.
495 
496 VTK_ABI_NAMESPACE_END
497 #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:43
a simple class to control print indentation
Definition: vtkIndent.h:29
abstract base class for most VTK objects
Definition: vtkObject.h:52
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:25
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:29
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