VTK  9.7.20260929
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
55
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#include <atomic> // for std::atomic
66#include <mutex> // for std::mutex
67#include <shared_mutex> // for std::shared_mutex
68
69VTK_ABI_NAMESPACE_BEGIN
70class vtkImageData;
71class vtkPath;
72class vtkTextProperty;
73class vtkStdString;
74
75// FreeType
76VTK_ABI_NAMESPACE_END
77#include "vtk_freetype.h" //since ft2build.h could be in the path
78#include FT_FREETYPE_H
79#include FT_GLYPH_H
80#include FT_CACHE_H
81
82VTK_ABI_NAMESPACE_BEGIN
83class FTFont;
84
85// PIMPL class for FTC_FaceID->vtkTextProperty lookup
86class vtkTextPropertyLookup;
87
88//----------------------------------------------------------------------------
89// Singleton cleanup
90class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
91{
92public:
95
96private:
98 vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
99};
100
101//----------------------------------------------------------------------------
102// Singleton font cache
103class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
104{
105public:
107 {
112 std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
113 std::string FamilyName;
115 bool Bold;
116 bool Italic;
117 };
123
125 void PrintSelf(ostream& os, vtkIndent indent) override;
126
131
136 static void SetInstance(vtkFreeTypeTools* instance);
137
139
143 vtkSetMacro(DebugTextures, bool);
144 vtkGetMacro(DebugTextures, bool);
145 vtkBooleanMacro(DebugTextures, bool);
147
151 FT_Library* GetLibrary();
152
159
166
173 std::array<int, 2> GetUnscaledKerning(
174 vtkTextProperty* tprop, vtkTypeUInt32 leftChar, vtkTypeUInt32 rightChar);
175
177
182 vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
183 vtkGetMacro(MaximumNumberOfFaces, unsigned int);
184 vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
185 vtkGetMacro(MaximumNumberOfSizes, unsigned int);
186 vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
187 vtkGetMacro(MaximumNumberOfBytes, unsigned long);
189
191
199 bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
201
203
208 vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
210
212
222 bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
223 int textDims[2] = nullptr);
225
227
233 bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
235
237
243 const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
245
250 static vtkTypeUInt16 HashString(const char* str);
251
256 static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
257
259
269 void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
270 void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
272
274
280 vtkSetMacro(ScaleToPowerTwo, bool);
281 vtkGetMacro(ScaleToPowerTwo, bool);
282 vtkBooleanMacro(ScaleToPowerTwo, bool);
284
286
291 vtkSetMacro(ForceCompiledFonts, bool);
292 vtkGetMacro(ForceCompiledFonts, bool);
293 vtkBooleanMacro(ForceCompiledFonts, bool);
295
302 static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
303
309 struct FTThreadLocalData;
310
311protected:
316 virtual FT_Error CreateFTCManager();
317
319
326
328
331 class MetaData;
332 class ImageMetaData;
333 bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
334 bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
336
341 void PrepareImageData(vtkImageData* data, int bbox[4]);
342
346 void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
347
355 bool GetSize(vtkTextProperty* tprop, FT_Size* size);
356
363 bool GetFace(vtkTextProperty* tprop, FT_Face* face);
364
370 bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
371
373
385 enum
386 {
390 };
392 vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
393 bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
394 bool GetSize(FTC_Scaler scaler, FT_Size* size);
395 bool GetFace(size_t tprop_cache_id, FT_Face* face);
396 bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
397 bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
398 int request = GLYPH_REQUEST_DEFAULT);
400 FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
402
407
410
414 bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
415
417
420 FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
421 FT_BitmapGlyph& bitmap_glyph);
422 FT_Bitmap* GetBitmap(
423 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
425
427
430 FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
431 FT_OutlineGlyph& outline_glyph);
432 FT_Outline* GetOutline(
433 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
435
441 static std::atomic<vtkFreeTypeTools*> Instance;
442 static std::mutex InstanceMutex;
443
448 vtkTextPropertyLookup* TextPropertyLookup;
449 mutable std::shared_mutex TextPropertyLookupMutex;
450
452
456 FTC_Manager* GetCacheManager();
457 FTC_ImageCache* GetImageCache();
458 FTC_CMapCache* GetCMapCache();
460
463 unsigned long MaximumNumberOfBytes;
464
467
470
471private:
472 vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
473 void operator=(const vtkFreeTypeTools&) = delete;
474
481 vtkTypeUInt64 InstanceId;
482
486 bool RenderStringInternal(
487 vtkTextProperty* tprop, const std::string& str, int dpi, vtkImageData* data, int textDims[2]);
488
492 bool StringToPathInternal(vtkTextProperty* tprop, const std::string& str, int dpi, vtkPath* path);
493
495
499 bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
500 bool CalculateBoundingBox(
501 const std::string& str, MetaData& metaData, const std::string& defaultHeightString);
503
509 template <typename DataType>
510 bool PopulateData(const std::string& str, DataType data, MetaData& metaData);
511
515 template <typename DataType>
516 bool RenderLine(std::string::const_iterator begin, std::string::const_iterator end, int lineIndex,
517 DataType data, MetaData& metaData);
518
520
523 bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
524 vtkImageData* image, MetaData& metaData);
525 bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
526 vtkPath* path, MetaData& metaData);
528
529 void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
530
536 int FitStringToBBox(
537 const std::string& str, MetaData& metaData, int targetWidth, int targetHeight);
538
540
547 void GetLineMetrics(std::string::const_iterator begin, std::string::const_iterator end,
548 MetaData& metaData, int& width, int bbox[4]);
550};
551
552// This is here to implement the Schwarz counter idiom.
554
555VTK_ABI_NAMESPACE_END
556#endif
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.
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.
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.
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.
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.
vtkTextPropertyLookup * TextPropertyLookup
Lookup table that maps free type font cache face ids to vtkTextProperties.
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.
FTC_Manager * GetCacheManager()
Get the FreeType cache manager, image cache and charmap cache for the calling thread.
static void SetInstance(vtkFreeTypeTools *instance)
Supply a user defined instance.
FTC_ImageCache * GetImageCache()
Get the FreeType cache manager, image cache and charmap cache for the calling thread.
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.
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...
FT_Library * GetLibrary()
Get the FreeType library for the calling thread.
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?
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.
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.
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.
unsigned int MaximumNumberOfFaces
~vtkFreeTypeTools() override
void ReleaseCacheManager(FTThreadLocalData &tld)
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...
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 ...
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.
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.
void InitializeCacheManager(FTThreadLocalData &tld)
FTC_CMapCache * GetCMapCache()
Get the FreeType cache manager, image cache and charmap cache for the calling thread.
virtual FT_Error CreateFTCManager()
Create the FreeType Cache manager instance and set the thread-local CacheManager.
GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty *tprop, vtkTypeUInt32 charId)
Return a generic outline of a glyph with some additional metadata.
std::shared_mutex TextPropertyLookupMutex
static std::mutex InstanceMutex
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static std::atomic< vtkFreeTypeTools * > Instance
The singleton instance and the mutex that protects its creation.
unsigned long MaximumNumberOfBytes
FTThreadLocalData & GetThreadLocalData()
Per-thread FreeType library, cache manager, image cache and charmap cache.
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.
topologically and geometrically regular array of data
a simple class to control print indentation
Definition vtkIndent.h:108
concrete dataset representing a path defined by Bezier curves.
Definition vtkPath.h:45
Hold a reference to a vtkObjectBase instance.
Wrapper around std::string to keep symbols short.
represent text properties.
Private implementation details shared between vtkFreeTypeTools and subclasses that need access to the...
std::array< int, 4 > BoundingBox
vtkSmartPointer< vtkPath > Path
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
#define VTK_UNSIGNED_LONG_MAX
Definition vtkType.h:198
#define VTK_UNSIGNED_INT_MAX
Definition vtkType.h:194