VTK  9.2.6
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=========================================================================*/
68#ifndef vtkFreeTypeTools_h
69#define vtkFreeTypeTools_h
70
71#include "vtkObject.h"
72#include "vtkRenderingFreeTypeModule.h" // For export macro
73#include "vtkSmartPointer.h" // For smart pointer
74#include "vtkTextRenderer.h" // For Metrics struct
75
76#include <array> // for std::array
77
78class vtkImageData;
79class vtkPath;
80class vtkTextProperty;
81class vtkStdString;
82
83// FreeType
84#include "vtk_freetype.h" //since ft2build.h could be in the path
85#include FT_FREETYPE_H
86#include FT_GLYPH_H
87#include FT_CACHE_H
88
89class FTFont;
90
91// PIMPL class for FTC_FaceID->vtkTextProperty lookup
92class vtkTextPropertyLookup;
93
94//----------------------------------------------------------------------------
95// Singleton cleanup
96class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
97{
98public:
101
102private:
104 vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
105};
106
107//----------------------------------------------------------------------------
108// Singleton font cache
109class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
110{
111public:
113 {
118 std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
121 bool Bold;
122 bool Italic;
123 };
125 {
128 };
129
131 void PrintSelf(ostream& os, vtkIndent indent) override;
132
137
142 static void SetInstance(vtkFreeTypeTools* instance);
143
145
149 vtkSetMacro(DebugTextures, bool);
150 vtkGetMacro(DebugTextures, bool);
151 vtkBooleanMacro(DebugTextures, bool);
153
157 FT_Library* GetLibrary();
158
165
172
179 std::array<int, 2> GetUnscaledKerning(
180 vtkTextProperty* tprop, vtkTypeUInt32 leftChar, vtkTypeUInt32 rightChar);
181
183
188 vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
189 vtkGetMacro(MaximumNumberOfFaces, unsigned int);
190 vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
191 vtkGetMacro(MaximumNumberOfSizes, unsigned int);
192 vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
193 vtkGetMacro(MaximumNumberOfBytes, unsigned long);
195
197
205 bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
207
209
214 vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
216
218
228 bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
229 int textDims[2] = nullptr);
231
233
239 bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
241
243
249 const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
251
256 static vtkTypeUInt16 HashString(const char* str);
257
262 static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
263
265
275 void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
276 void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
278
280
286 vtkSetMacro(ScaleToPowerTwo, bool);
287 vtkGetMacro(ScaleToPowerTwo, bool);
288 vtkBooleanMacro(ScaleToPowerTwo, bool);
290
292
297 vtkSetMacro(ForceCompiledFonts, bool);
298 vtkGetMacro(ForceCompiledFonts, bool);
299 vtkBooleanMacro(ForceCompiledFonts, bool);
301
308 static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
309
310protected:
314 virtual FT_Error CreateFTCManager();
315
317
320 class MetaData;
321 class ImageMetaData;
322 bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
323 bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
325
330 void PrepareImageData(vtkImageData* data, int bbox[4]);
331
335 void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
336
344 bool GetSize(vtkTextProperty* tprop, FT_Size* size);
345
352 bool GetFace(vtkTextProperty* tprop, FT_Face* face);
353
359 bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
360
362
374 enum
375 {
376 GLYPH_REQUEST_DEFAULT = 0,
377 GLYPH_REQUEST_BITMAP = 1,
378 GLYPH_REQUEST_OUTLINE = 2
379 };
381 vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
382 bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
383 bool GetSize(FTC_Scaler scaler, FT_Size* size);
384 bool GetFace(size_t tprop_cache_id, FT_Face* face);
385 bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
386 bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
387 int request = GLYPH_REQUEST_DEFAULT);
389 FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
391
396
399
403 bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
404
406
409 FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
410 FT_BitmapGlyph& bitmap_glyph);
411 FT_Bitmap* GetBitmap(
412 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
414
416
419 FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
420 FT_OutlineGlyph& outline_glyph);
421 FT_Outline* GetOutline(
422 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
424
429
433 vtkTextPropertyLookup* TextPropertyLookup;
434
438 FT_Library* Library;
439
441
444 FTC_Manager* CacheManager;
445 FTC_ImageCache* ImageCache;
446 FTC_CMapCache* CMapCache;
448
450
453 FTC_Manager* GetCacheManager();
454 FTC_ImageCache* GetImageCache();
455 FTC_CMapCache* GetCMapCache();
457
460 unsigned long MaximumNumberOfBytes;
461
464
467
468private:
469 vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
470 void operator=(const vtkFreeTypeTools&) = delete;
471
475 bool RenderStringInternal(
476 vtkTextProperty* tprop, const std::string& str, int dpi, vtkImageData* data, int textDims[2]);
477
481 bool StringToPathInternal(vtkTextProperty* tprop, const std::string& str, int dpi, vtkPath* path);
482
484
488 bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
489 bool CalculateBoundingBox(
490 const std::string& str, MetaData& metaData, const std::string& defaultHeightString);
492
498 template <typename DataType>
499 bool PopulateData(const std::string& str, DataType data, MetaData& metaData);
500
504 template <typename DataType>
505 bool RenderLine(std::string::const_iterator begin, std::string::const_iterator end, int lineIndex,
506 DataType data, MetaData& metaData);
507
509
512 bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
513 vtkImageData* image, MetaData& metaData);
514 bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
515 vtkPath* path, MetaData& metaData);
517
518 void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
519
525 int FitStringToBBox(
526 const std::string& str, MetaData& metaData, int targetWidth, int targetHeight);
527
529
536 void GetLineMetrics(std::string::const_iterator begin, std::string::const_iterator end,
537 MetaData& metaData, int& width, int bbox[4]);
539};
540
541// This is here to implement the Schwarz counter idiom.
543
544#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.
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.
FTC_ImageCache * ImageCache
The cache manager, image cache and charmap cache.
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.
void ReleaseCacheManager()
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.
static void SetInstance(vtkFreeTypeTools *instance)
Supply a user defined instance.
FTC_ImageCache * GetImageCache()
Get the FreeType cache manager, image cache and charmap cache.
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...
FT_Library * GetLibrary()
Get the FreeType library singleton.
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 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 ...
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.
FTC_CMapCache * GetCMapCache()
Get the FreeType cache manager, image cache and charmap cache.
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.
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:163
a simple class to control print indentation
Definition: vtkIndent.h:119
abstract base class for most VTK objects
Definition: vtkObject.h:79
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:36
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:108
represent text properties.
@ image
Definition: vtkX3D.h:380
@ size
Definition: vtkX3D.h:259
@ data
Definition: vtkX3D.h:321
@ string
Definition: vtkX3D.h:496
std::array< int, 4 > BoundingBox
vtkSmartPointer< vtkPath > Path
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:161
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:157