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=========================================================================*/
26#ifndef vtkFreeTypeTools_h
27#define vtkFreeTypeTools_h
28
29#include "vtkObject.h"
30#include "vtkRenderingFreeTypeModule.h" // For export macro
31#include "vtkSmartPointer.h" // For smart pointer
32#include "vtkTextRenderer.h" // For Metrics struct
33#include "vtkUnicodeString.h" // For vtkUnicodeStringValueType
34
35#include <array> // for std::array
36
37class vtkImageData;
38class vtkPath;
39class vtkTextProperty;
40class vtkStdString;
41
42// FreeType
43#include "vtk_freetype.h" //since ft2build.h could be in the path
44#include FT_FREETYPE_H
45#include FT_GLYPH_H
46#include FT_CACHE_H
47
48class FTFont;
49
50// PIMPL class for FTC_FaceID->vtkTextProperty lookup
51class vtkTextPropertyLookup;
52
53//----------------------------------------------------------------------------
54// Singleton cleanup
55class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
56{
57public:
60
61private:
63 vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
64};
65
66//----------------------------------------------------------------------------
67// Singleton font cache
68class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
69{
70public:
72 {
77 std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
80 bool Bold;
81 bool Italic;
82 };
84 {
87 };
88
90 void PrintSelf(ostream& os, vtkIndent indent) override;
91
96
101 static void SetInstance(vtkFreeTypeTools* instance);
102
104
108 vtkSetMacro(DebugTextures, bool);
109 vtkGetMacro(DebugTextures, bool);
110 vtkBooleanMacro(DebugTextures, bool);
112
116 FT_Library* GetLibrary();
117
124
131
139 vtkUnicodeStringValueType rightChar);
140
142
147 vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
148 vtkGetMacro(MaximumNumberOfFaces, unsigned int);
149 vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
150 vtkGetMacro(MaximumNumberOfSizes, unsigned int);
151 vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
152 vtkGetMacro(MaximumNumberOfBytes, unsigned long);
154
156
164 bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
165 VTK_DEPRECATED_IN_9_1_0("Use bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& "
166 "str, int dpi, int bbox[4])")
167 bool GetBoundingBox(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, int bbox[4]);
169
171
175 bool GetMetrics(
176 vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
177 VTK_DEPRECATED_IN_9_1_0("Use bool GetMetrics(vtkTextProperty* tprop, const vtkStdString& str, "
178 "int dpi, vtkTextRenderer::Metrics& metrics)")
179 bool GetMetrics(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
180 vtkTextRenderer::Metrics& metrics);
182
184
194 bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
195 int textDims[2] = nullptr);
196 VTK_DEPRECATED_IN_9_1_0("Use bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, "
197 "int dpi, vtkImageData* data, int textDims[2])")
198 bool RenderString(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
199 vtkImageData* data, int textDims[2] = nullptr);
201
203
209 bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
210 VTK_DEPRECATED_IN_9_1_0("Use bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, "
211 "int dpi, vtkPath* path)")
212 bool StringToPath(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, vtkPath* path);
214
216
221 int GetConstrainedFontSize(
222 const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
223 VTK_DEPRECATED_IN_9_1_0("Use int GetConstrainedFontSize(const vtkStdString& str, "
224 "vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight)")
225 int GetConstrainedFontSize(const vtkUnicodeString& str, vtkTextProperty* tprop, int dpi,
226 int targetWidth, int targetHeight);
228
233 static vtkTypeUInt16 HashString(const char* str);
234
239 static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
240
242
252 void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
253 void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
255
257
263 vtkSetMacro(ScaleToPowerTwo, bool);
264 vtkGetMacro(ScaleToPowerTwo, bool);
265 vtkBooleanMacro(ScaleToPowerTwo, bool);
267
269
274 vtkSetMacro(ForceCompiledFonts, bool);
275 vtkGetMacro(ForceCompiledFonts, bool);
276 vtkBooleanMacro(ForceCompiledFonts, bool);
278
285 static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
286
287protected:
291 virtual FT_Error CreateFTCManager();
292
294
297 class MetaData;
298 class ImageMetaData;
299 bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
300 bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
302
307 void PrepareImageData(vtkImageData* data, int bbox[4]);
308
312 void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
313
321 bool GetSize(vtkTextProperty* tprop, FT_Size* size);
322
329 bool GetFace(vtkTextProperty* tprop, FT_Face* face);
330
336 bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
337
339
351 enum
352 {
353 GLYPH_REQUEST_DEFAULT = 0,
354 GLYPH_REQUEST_BITMAP = 1,
355 GLYPH_REQUEST_OUTLINE = 2
356 };
358 vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
359 bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
360 bool GetSize(FTC_Scaler scaler, FT_Size* size);
361 bool GetFace(size_t tprop_cache_id, FT_Face* face);
362 bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
363 bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
364 int request = GLYPH_REQUEST_DEFAULT);
366 FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
368
373
376
380 bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
381
383
386 FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
387 FT_BitmapGlyph& bitmap_glyph);
388 FT_Bitmap* GetBitmap(
389 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
391
393
396 FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
397 FT_OutlineGlyph& outline_glyph);
398 FT_Outline* GetOutline(
399 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
401
406
410 vtkTextPropertyLookup* TextPropertyLookup;
411
415 FT_Library* Library;
416
418
421 FTC_Manager* CacheManager;
422 FTC_ImageCache* ImageCache;
423 FTC_CMapCache* CMapCache;
425
427
430 FTC_Manager* GetCacheManager();
431 FTC_ImageCache* GetImageCache();
432 FTC_CMapCache* GetCMapCache();
434
437 unsigned long MaximumNumberOfBytes;
438
441
444
445private:
446 vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
447 void operator=(const vtkFreeTypeTools&) = delete;
448
452 template <typename StringType>
453 bool RenderStringInternal(
454 vtkTextProperty* tprop, const StringType& str, int dpi, vtkImageData* data, int textDims[2]);
455
459 template <typename StringType>
460 bool StringToPathInternal(vtkTextProperty* tprop, const StringType& str, int dpi, vtkPath* path);
461
463
467 bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
469 "Use bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData)")
470 bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData& metaData);
471 template <typename T>
472 bool CalculateBoundingBox(const T& str, MetaData& metaData, const T& defaultHeightString);
474
480 template <typename StringType, typename DataType>
481 bool PopulateData(const StringType& str, DataType data, MetaData& metaData);
482
486 template <typename IteratorType, typename DataType>
487 bool RenderLine(
488 IteratorType begin, IteratorType end, int lineIndex, DataType data, MetaData& metaData);
489
491
494 template <typename CharType>
495 bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
496 vtkImageData* image, MetaData& metaData);
497 template <typename CharType>
498 bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
499 vtkPath* path, MetaData& metaData);
501
502 void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
503
509 template <typename T>
510 int FitStringToBBox(const T& str, MetaData& metaData, int targetWidth, int targetHeight);
511
513
520 template <typename T>
521 void GetLineMetrics(T begin, T end, MetaData& metaData, int& width, int bbox[4]);
523};
524
525// This is here to implement the Schwarz counter idiom.
527
528#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:48
a simple class to control print indentation
Definition: vtkIndent.h:34
abstract base class for most VTK objects
Definition: vtkObject.h:63
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:33
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:36
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