VTK  9.1.0
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=========================================================================*/
35#ifndef vtkFreeTypeTools_h
36#define vtkFreeTypeTools_h
37
38#include "vtkObject.h"
39#include "vtkRenderingFreeTypeModule.h" // For export macro
40#include "vtkSmartPointer.h" // For smart pointer
41#include "vtkTextRenderer.h" // For Metrics struct
42#include "vtkUnicodeString.h" // For vtkUnicodeStringValueType
43
44#include <array> // for std::array
45
46class vtkImageData;
47class vtkPath;
48class vtkTextProperty;
49class vtkStdString;
50
51// FreeType
52#include "vtk_freetype.h" //since ft2build.h could be in the path
53#include FT_FREETYPE_H
54#include FT_GLYPH_H
55#include FT_CACHE_H
56
57class FTFont;
58
59// PIMPL class for FTC_FaceID->vtkTextProperty lookup
60class vtkTextPropertyLookup;
61
62//----------------------------------------------------------------------------
63// Singleton cleanup
64class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
65{
66public:
69
70private:
72 vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
73};
74
75//----------------------------------------------------------------------------
76// Singleton font cache
77class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
78{
79public:
81 {
86 std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
89 bool Bold;
90 bool Italic;
91 };
93 {
96 };
97
99 void PrintSelf(ostream& os, vtkIndent indent) override;
100
105
110 static void SetInstance(vtkFreeTypeTools* instance);
111
113
117 vtkSetMacro(DebugTextures, bool);
118 vtkGetMacro(DebugTextures, bool);
119 vtkBooleanMacro(DebugTextures, bool);
121
125 FT_Library* GetLibrary();
126
133
140
148 vtkUnicodeStringValueType rightChar);
149
151
156 vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
157 vtkGetMacro(MaximumNumberOfFaces, unsigned int);
158 vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
159 vtkGetMacro(MaximumNumberOfSizes, unsigned int);
160 vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
161 vtkGetMacro(MaximumNumberOfBytes, unsigned long);
163
165
173 bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
174 VTK_DEPRECATED_IN_9_1_0("Use bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& "
175 "str, int dpi, int bbox[4])")
176 bool GetBoundingBox(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, int bbox[4]);
178
180
184 bool GetMetrics(
185 vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
186 VTK_DEPRECATED_IN_9_1_0("Use bool GetMetrics(vtkTextProperty* tprop, const vtkStdString& str, "
187 "int dpi, vtkTextRenderer::Metrics& metrics)")
188 bool GetMetrics(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
189 vtkTextRenderer::Metrics& metrics);
191
193
203 bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
204 int textDims[2] = nullptr);
205 VTK_DEPRECATED_IN_9_1_0("Use bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, "
206 "int dpi, vtkImageData* data, int textDims[2])")
207 bool RenderString(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
208 vtkImageData* data, int textDims[2] = nullptr);
210
212
218 bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
219 VTK_DEPRECATED_IN_9_1_0("Use bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, "
220 "int dpi, vtkPath* path)")
221 bool StringToPath(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, vtkPath* path);
223
225
230 int GetConstrainedFontSize(
231 const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
232 VTK_DEPRECATED_IN_9_1_0("Use int GetConstrainedFontSize(const vtkStdString& str, "
233 "vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight)")
234 int GetConstrainedFontSize(const vtkUnicodeString& str, vtkTextProperty* tprop, int dpi,
235 int targetWidth, int targetHeight);
237
242 static vtkTypeUInt16 HashString(const char* str);
243
248 static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
249
251
261 void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
262 void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
264
266
272 vtkSetMacro(ScaleToPowerTwo, bool);
273 vtkGetMacro(ScaleToPowerTwo, bool);
274 vtkBooleanMacro(ScaleToPowerTwo, bool);
276
278
283 vtkSetMacro(ForceCompiledFonts, bool);
284 vtkGetMacro(ForceCompiledFonts, bool);
285 vtkBooleanMacro(ForceCompiledFonts, bool);
287
294 static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
295
296protected:
300 virtual FT_Error CreateFTCManager();
301
303
306 class MetaData;
307 class ImageMetaData;
308 bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
309 bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
311
316 void PrepareImageData(vtkImageData* data, int bbox[4]);
317
321 void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
322
330 bool GetSize(vtkTextProperty* tprop, FT_Size* size);
331
338 bool GetFace(vtkTextProperty* tprop, FT_Face* face);
339
345 bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
346
348
360 enum
361 {
362 GLYPH_REQUEST_DEFAULT = 0,
363 GLYPH_REQUEST_BITMAP = 1,
364 GLYPH_REQUEST_OUTLINE = 2
365 };
367 vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
368 bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
369 bool GetSize(FTC_Scaler scaler, FT_Size* size);
370 bool GetFace(size_t tprop_cache_id, FT_Face* face);
371 bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
372 bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
373 int request = GLYPH_REQUEST_DEFAULT);
375 FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
377
382
385
389 bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
390
392
395 FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
396 FT_BitmapGlyph& bitmap_glyph);
397 FT_Bitmap* GetBitmap(
398 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
400
402
405 FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
406 FT_OutlineGlyph& outline_glyph);
407 FT_Outline* GetOutline(
408 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
410
415
419 vtkTextPropertyLookup* TextPropertyLookup;
420
424 FT_Library* Library;
425
427
430 FTC_Manager* CacheManager;
431 FTC_ImageCache* ImageCache;
432 FTC_CMapCache* CMapCache;
434
436
439 FTC_Manager* GetCacheManager();
440 FTC_ImageCache* GetImageCache();
441 FTC_CMapCache* GetCMapCache();
443
446 unsigned long MaximumNumberOfBytes;
447
450
453
454private:
455 vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
456 void operator=(const vtkFreeTypeTools&) = delete;
457
461 template <typename StringType>
462 bool RenderStringInternal(
463 vtkTextProperty* tprop, const StringType& str, int dpi, vtkImageData* data, int textDims[2]);
464
468 template <typename StringType>
469 bool StringToPathInternal(vtkTextProperty* tprop, const StringType& str, int dpi, vtkPath* path);
470
472
476 bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
478 "Use bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData)")
479 bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData& metaData);
480 template <typename T>
481 bool CalculateBoundingBox(const T& str, MetaData& metaData, const T& defaultHeightString);
483
489 template <typename StringType, typename DataType>
490 bool PopulateData(const StringType& str, DataType data, MetaData& metaData);
491
495 template <typename IteratorType, typename DataType>
496 bool RenderLine(
497 IteratorType begin, IteratorType end, int lineIndex, DataType data, MetaData& metaData);
498
500
503 template <typename CharType>
504 bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
505 vtkImageData* image, MetaData& metaData);
506 template <typename CharType>
507 bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
508 vtkPath* path, MetaData& metaData);
510
511 void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
512
518 template <typename T>
519 int FitStringToBBox(const T& str, MetaData& metaData, int targetWidth, int targetHeight);
520
522
529 template <typename T>
530 void GetLineMetrics(T begin, T end, MetaData& metaData, int& width, int bbox[4]);
532};
533
534// This is here to implement the Schwarz counter idiom.
536
537#endif
FreeType library support.
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 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.
GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty *tprop, vtkUnicodeStringValueType charId)
Return a generic outline of a glyph with some additional metadata.
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.
FT_Library * GetLibrary()
Get the FreeType library singleton.
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.
unsigned int MaximumNumberOfFaces
~vtkFreeTypeTools() override
bool GetSize(FTC_Scaler scaler, FT_Size *size)
Given a text property and a character, get the corresponding FreeType glyph.
FTC_Manager * CacheManager
The cache manager, image cache and charmap cache.
unsigned int MaximumNumberOfSizes
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...
FTC_CMapCache * GetCMapCache()
Get the FreeType cache manager, image cache and charmap cache.
FT_Library * Library
FreeType library instance.
std::array< int, 2 > GetUnscaledKerning(vtkTextProperty *tprop, vtkUnicodeStringValueType leftChar, vtkUnicodeStringValueType rightChar)
Return a 2D vector detailing the unscaled kerning offset for a pair of characters.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
unsigned long MaximumNumberOfBytes
void InitializeCacheManager()
topologically and geometrically regular array of data
Definition: vtkImageData.h:57
a simple class to control print indentation
Definition: vtkIndent.h:43
abstract base class for most VTK objects
Definition: vtkObject.h:63
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:42
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:45
represent text properties.
Interface for generating images and path data from string data, using multiple backends.
String class that stores Unicode text.
@ 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
#define VTK_DEPRECATED_IN_9_1_0(reason)
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:161
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:157
vtkTypeUInt32 vtkUnicodeStringValueType