VTK  9.0.2
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 
37 class vtkImageData;
38 class vtkPath;
39 class vtkTextProperty;
40 class 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 
48 class FTFont;
49 
50 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
51 class vtkTextPropertyLookup;
52 
53 //----------------------------------------------------------------------------
54 // Singleton cleanup
55 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
56 {
57 public:
60 
61 private:
63  vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
64 };
65 
66 //----------------------------------------------------------------------------
67 // Singleton font cache
68 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
69 {
70 public:
71  struct FaceMetrics
72  {
74  int Ascender;
75  int Descender;
77  std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
79  bool Scalable;
80  bool Bold;
81  bool Italic;
82  };
83  struct GlyphOutline
84  {
87  };
88 
89  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
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 
138  std::array<int, 2> GetUnscaledKerning(vtkTextProperty* tprop, vtkUnicodeStringValueType leftChar,
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  bool GetBoundingBox(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, int bbox[4]);
167 
169 
174  vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
175  bool GetMetrics(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
176  vtkTextRenderer::Metrics& metrics);
178 
180 
190  bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
191  int textDims[2] = nullptr);
192  bool RenderString(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi,
193  vtkImageData* data, int textDims[2] = nullptr);
195 
197 
203  bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
204  bool StringToPath(vtkTextProperty* tprop, const vtkUnicodeString& str, int dpi, vtkPath* path);
206 
208 
214  const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
215  int GetConstrainedFontSize(const vtkUnicodeString& str, vtkTextProperty* tprop, int dpi,
216  int targetWidth, int targetHeight);
218 
223  static vtkTypeUInt16 HashString(const char* str);
224 
229  static vtkTypeUInt32 HashBuffer(const void* str, size_t n, vtkTypeUInt32 hash = 0);
230 
232 
242  void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
243  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
245 
247 
253  vtkSetMacro(ScaleToPowerTwo, bool);
254  vtkGetMacro(ScaleToPowerTwo, bool);
255  vtkBooleanMacro(ScaleToPowerTwo, bool);
257 
259 
264  vtkSetMacro(ForceCompiledFonts, bool);
265  vtkGetMacro(ForceCompiledFonts, bool);
266  vtkBooleanMacro(ForceCompiledFonts, bool);
268 
275  static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
276 
277 protected:
281  virtual FT_Error CreateFTCManager();
282 
284 
287  class MetaData;
288  class ImageMetaData;
289  bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
290  bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
292 
297  void PrepareImageData(vtkImageData* data, int bbox[4]);
298 
302  void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
303 
311  bool GetSize(vtkTextProperty* tprop, FT_Size* size);
312 
319  bool GetFace(vtkTextProperty* tprop, FT_Face* face);
320 
326  bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
327 
329 
341  enum
342  {
343  GLYPH_REQUEST_DEFAULT = 0,
344  GLYPH_REQUEST_BITMAP = 1,
345  GLYPH_REQUEST_OUTLINE = 2
346  };
347  bool GetGlyph(
348  vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
349  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
350  bool GetSize(FTC_Scaler scaler, FT_Size* size);
351  bool GetFace(size_t tprop_cache_id, FT_Face* face);
352  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
353  bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
354  int request = GLYPH_REQUEST_DEFAULT);
355  bool GetGlyph(
356  FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
358 
363 
365  ~vtkFreeTypeTools() override;
366 
370  bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
371 
373 
376  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
377  FT_BitmapGlyph& bitmap_glyph);
378  FT_Bitmap* GetBitmap(
379  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
381 
383 
386  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
387  FT_OutlineGlyph& outline_glyph);
388  FT_Outline* GetOutline(
389  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
391 
396 
400  vtkTextPropertyLookup* TextPropertyLookup;
401 
405  FT_Library* Library;
406 
408 
411  FTC_Manager* CacheManager;
412  FTC_ImageCache* ImageCache;
413  FTC_CMapCache* CMapCache;
415 
417 
420  FTC_Manager* GetCacheManager();
421  FTC_ImageCache* GetImageCache();
422  FTC_CMapCache* GetCMapCache();
424 
425  unsigned int MaximumNumberOfFaces;
426  unsigned int MaximumNumberOfSizes;
427  unsigned long MaximumNumberOfBytes;
428 
431 
434 
435 private:
436  vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
437  void operator=(const vtkFreeTypeTools&) = delete;
438 
442  template <typename StringType>
443  bool RenderStringInternal(
444  vtkTextProperty* tprop, const StringType& str, int dpi, vtkImageData* data, int textDims[2]);
445 
449  template <typename StringType>
450  bool StringToPathInternal(vtkTextProperty* tprop, const StringType& str, int dpi, vtkPath* path);
451 
453 
457  bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
458  bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData& metaData);
459  template <typename T>
460  bool CalculateBoundingBox(const T& str, MetaData& metaData, const T& defaultHeightString);
462 
468  template <typename StringType, typename DataType>
469  bool PopulateData(const StringType& str, DataType data, MetaData& metaData);
470 
474  template <typename IteratorType, typename DataType>
475  bool RenderLine(
476  IteratorType begin, IteratorType end, int lineIndex, DataType data, MetaData& metaData);
477 
479 
482  template <typename CharType>
483  bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
484  vtkImageData* image, MetaData& metaData);
485  template <typename CharType>
486  bool RenderCharacter(CharType character, int& x, int& y, FT_UInt& previousGlyphIndex,
487  vtkPath* path, MetaData& metaData);
489 
490  void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
491 
497  template <typename T>
498  int FitStringToBBox(const T& str, MetaData& metaData, int targetWidth, int targetHeight);
499 
501 
508  template <typename T>
509  void GetLineMetrics(T begin, T end, MetaData& metaData, int& width, int bbox[4]);
511 };
512 
513 // This is here to implement the Schwarz counter idiom.
515 
516 #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.
bool PrepareMetaData(vtkTextProperty *tprop, int dpi, MetaData &metaData)
bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString &str, int dpi, vtkPath *path)
bool GetFace(size_t tprop_cache_id, FT_Face *face)
FTC_ImageCache * ImageCache
FTC_CMapCache * GetCMapCache()
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.
GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty *tprop, vtkUnicodeStringValueType charId)
Return a generic outline of a glyph with some additional metadata.
FT_Outline * GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex, FT_OutlineGlyph &outline_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.
bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString &str, int dpi, vtkTextRenderer::Metrics &metrics)
void ReleaseCacheManager()
vtkTextPropertyLookup * TextPropertyLookup
Lookup table that maps free type font cache face ids to vtkTextProperties.
FT_Bitmap * GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex, FT_BitmapGlyph &bitmap_glyph)
bool GetSize(size_t tprop_cache_id, int font_size, FT_Size *size)
bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph *glyph, int request=GLYPH_REQUEST_DEFAULT)
static void SetInstance(vtkFreeTypeTools *instance)
Supply a user defined instance.
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)
bool GetSize(vtkTextProperty *tprop, FT_Size *size)
Given a text property, get the corresponding FreeType size object (a structure storing both a face an...
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?
FTC_Manager * GetCacheManager()
Get the FreeType cache manager, image cache and charmap cache.
bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt *gindex)
bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString &str, int dpi, vtkImageData *data, int textDims[2]=nullptr)
bool GetGlyph(vtkTextProperty *tprop, FT_UInt32 c, FT_Glyph *glyph, int request=GLYPH_REQUEST_DEFAULT)
static vtkTypeUInt32 HashBuffer(const void *str, size_t n, vtkTypeUInt32 hash=0)
Hash a string of a given length.
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.
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)
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...
FTC_ImageCache * GetImageCache()
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)
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
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.
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.
int GetConstrainedFontSize(const vtkUnicodeString &str, vtkTextProperty *tprop, int dpi, int targetWidth, int targetHeight)
FT_Library * Library
FreeType library instance.
virtual FT_Error CreateFTCManager()
Create the FreeType Cache manager instance and set this->CacheManager.
bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString &str, int dpi, int bbox[4])
FT_Library * GetLibrary()
Get the FreeType library singleton.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
unsigned long MaximumNumberOfBytes
static vtkTypeUInt16 HashString(const char *str)
Turn a string into a hash.
void InitializeCacheManager()
topologically and geometrically regular array of data
Definition: vtkImageData.h:42
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:35
represent text properties.
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
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:161
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:157
vtkTypeUInt32 vtkUnicodeStringValueType