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=========================================================================*/
65#ifndef vtkFreeTypeTools_h
66#define vtkFreeTypeTools_h
67
68#include "vtkObject.h"
69#include "vtkRenderingFreeTypeModule.h" // For export macro
70#include "vtkSmartPointer.h" // For smart pointer
71#include "vtkTextRenderer.h" // For Metrics struct
72#include "vtkUnicodeString.h" // For vtkUnicodeStringValueType
73
74#include <array> // for std::array
75
76class vtkImageData;
77class vtkPath;
78class vtkTextProperty;
79class vtkStdString;
80
81// FreeType
82#include "vtk_freetype.h" //since ft2build.h could be in the path
83#include FT_FREETYPE_H
84#include FT_GLYPH_H
85#include FT_CACHE_H
86
87class FTFont;
88
89// PIMPL class for FTC_FaceID->vtkTextProperty lookup
90class vtkTextPropertyLookup;
91
92//----------------------------------------------------------------------------
93// Singleton cleanup
94class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
95{
96public:
99
100private:
102 vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
103};
104
105//----------------------------------------------------------------------------
106// Singleton font cache
107class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
108{
109public:
111 {
116 std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
119 bool Bold;
120 bool Italic;
121 };
123 {
126 };
127
129 void PrintSelf(ostream& os, vtkIndent indent) override;
130
135
140 static void SetInstance(vtkFreeTypeTools* instance);
141
143
147 vtkSetMacro(DebugTextures, bool);
148 vtkGetMacro(DebugTextures, bool);
149 vtkBooleanMacro(DebugTextures, bool);
151
155 FT_Library* GetLibrary();
156
163
170
178 vtkUnicodeStringValueType rightChar);
179
181
186 vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
187 vtkGetMacro(MaximumNumberOfFaces, unsigned int);
188 vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
189 vtkGetMacro(MaximumNumberOfSizes, unsigned int);
190 vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
191 vtkGetMacro(MaximumNumberOfBytes, unsigned long);
193
195
203 bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
204 VTK_DEPRECATED_IN_9_1_0("Use bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& "
205 "str, int dpi, int bbox[4])")
206 bool GetBoundingBox(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, int bbox[4]);
208
210
214 bool GetMetrics(
215 vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
216 VTK_DEPRECATED_IN_9_1_0("Use bool GetMetrics(vtkTextProperty* tprop, const vtkStdString& str, "
217 "int dpi, vtkTextRenderer::Metrics& metrics)")
218 bool GetMetrics(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
219 vtkTextRenderer::Metrics& metrics);
221
223
233 bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
234 int textDims[2] = nullptr);
235 VTK_DEPRECATED_IN_9_1_0("Use bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, "
236 "int dpi, vtkImageData* data, int textDims[2])")
237 bool RenderString(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
238 vtkImageData* data, int textDims[2] = nullptr);
240
242
248 bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
249 VTK_DEPRECATED_IN_9_1_0("Use bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, "
250 "int dpi, vtkPath* path)")
251 bool StringToPath(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, vtkPath* path);
253
255
260 int GetConstrainedFontSize(
261 const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
262 VTK_DEPRECATED_IN_9_1_0("Use int GetConstrainedFontSize(const vtkStdString& str, "
263 "vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight)")
264 int GetConstrainedFontSize(const vtkUnicodeString& str, vtkTextProperty* tprop, int dpi,
265 int targetWidth, int targetHeight);
267
272 static vtkTypeUInt16 HashString(const char* str);
273
278 static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
279
281
291 void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
292 void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
294
296
302 vtkSetMacro(ScaleToPowerTwo, bool);
303 vtkGetMacro(ScaleToPowerTwo, bool);
304 vtkBooleanMacro(ScaleToPowerTwo, bool);
306
308
313 vtkSetMacro(ForceCompiledFonts, bool);
314 vtkGetMacro(ForceCompiledFonts, bool);
315 vtkBooleanMacro(ForceCompiledFonts, bool);
317
324 static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
325
326protected:
330 virtual FT_Error CreateFTCManager();
331
333
336 class MetaData;
337 class ImageMetaData;
338 bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
339 bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
341
346 void PrepareImageData(vtkImageData* data, int bbox[4]);
347
351 void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
352
360 bool GetSize(vtkTextProperty* tprop, FT_Size* size);
361
368 bool GetFace(vtkTextProperty* tprop, FT_Face* face);
369
375 bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
376
378
390 enum
391 {
392 GLYPH_REQUEST_DEFAULT = 0,
393 GLYPH_REQUEST_BITMAP = 1,
394 GLYPH_REQUEST_OUTLINE = 2
395 };
397 vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
398 bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
399 bool GetSize(FTC_Scaler scaler, FT_Size* size);
400 bool GetFace(size_t tprop_cache_id, FT_Face* face);
401 bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
402 bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
403 int request = GLYPH_REQUEST_DEFAULT);
405 FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
407
412
415
419 bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
420
422
425 FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
426 FT_BitmapGlyph& bitmap_glyph);
427 FT_Bitmap* GetBitmap(
428 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
430
432
435 FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
436 FT_OutlineGlyph& outline_glyph);
437 FT_Outline* GetOutline(
438 FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
440
445
449 vtkTextPropertyLookup* TextPropertyLookup;
450
454 FT_Library* Library;
455
457
460 FTC_Manager* CacheManager;
461 FTC_ImageCache* ImageCache;
462 FTC_CMapCache* CMapCache;
464
466
469 FTC_Manager* GetCacheManager();
470 FTC_ImageCache* GetImageCache();
471 FTC_CMapCache* GetCMapCache();
473
476 unsigned long MaximumNumberOfBytes;
477
480
483
484private:
485 vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
486 void operator=(const vtkFreeTypeTools&) = delete;
487
491 template <typename StringType>
492 bool RenderStringInternal(
493 vtkTextProperty* tprop, const StringType& str, int dpi, vtkImageData* data, int textDims[2]);
494
498 template <typename StringType>
499 bool StringToPathInternal(vtkTextProperty* tprop, const StringType& str, int dpi, vtkPath* path);
500
502
506 bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
508 "Use bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData)")
509 bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData& metaData);
510 template <typename T>
511 bool CalculateBoundingBox(const T& str, MetaData& metaData, const T& defaultHeightString);
513
519 template <typename StringType, typename DataType>
520 bool PopulateData(const StringType& str, DataType data, MetaData& metaData);
521
525 template <typename IteratorType, typename DataType>
526 bool RenderLine(
527 IteratorType begin, IteratorType end, int lineIndex, DataType data, MetaData& metaData);
528
530
533 template <typename CharType>
534 bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
535 vtkImageData* image, MetaData& metaData);
536 template <typename CharType>
537 bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
538 vtkPath* path, MetaData& metaData);
540
541 void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
542
548 template <typename T>
549 int FitStringToBBox(const T& str, MetaData& metaData, int targetWidth, int targetHeight);
550
552
559 template <typename T>
560 void GetLineMetrics(T begin, T end, MetaData& metaData, int& width, int bbox[4]);
562};
563
564// This is here to implement the Schwarz counter idiom.
566
567#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:157
a simple class to control print indentation
Definition: vtkIndent.h:113
abstract base class for most VTK objects
Definition: vtkObject.h:73
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:33
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:105
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