VTK  9.3.1
vtkFreeTypeTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
17 #ifndef vtkFreeTypeTools_h
18 #define vtkFreeTypeTools_h
19 
20 #include "vtkObject.h"
21 #include "vtkRenderingFreeTypeModule.h" // For export macro
22 #include "vtkSmartPointer.h" // For smart pointer
23 #include "vtkTextRenderer.h" // For Metrics struct
24 
25 #include <array> // for std::array
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 class vtkImageData;
29 class vtkPath;
30 class vtkTextProperty;
31 class vtkStdString;
32 
33 // FreeType
34 VTK_ABI_NAMESPACE_END
35 #include "vtk_freetype.h" //since ft2build.h could be in the path
36 #include FT_FREETYPE_H
37 #include FT_GLYPH_H
38 #include FT_CACHE_H
39 
40 VTK_ABI_NAMESPACE_BEGIN
41 class FTFont;
42 
43 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
44 class vtkTextPropertyLookup;
45 
46 //----------------------------------------------------------------------------
47 // Singleton cleanup
48 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
49 {
50 public:
53 
54 private:
56  vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
57 };
58 
59 //----------------------------------------------------------------------------
60 // Singleton font cache
61 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
62 {
63 public:
64  struct FaceMetrics
65  {
67  int Ascender;
68  int Descender;
70  std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
72  bool Scalable;
73  bool Bold;
74  bool Italic;
75  };
76  struct GlyphOutline
77  {
80  };
81 
82  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
83  void PrintSelf(ostream& os, vtkIndent indent) override;
84 
88  static vtkFreeTypeTools* GetInstance();
89 
94  static void SetInstance(vtkFreeTypeTools* instance);
95 
97 
101  vtkSetMacro(DebugTextures, bool);
102  vtkGetMacro(DebugTextures, bool);
103  vtkBooleanMacro(DebugTextures, bool);
105 
109  FT_Library* GetLibrary();
110 
116  FaceMetrics GetFaceMetrics(vtkTextProperty* tprop);
117 
123  GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty* tprop, vtkTypeUInt32 charId);
124 
131  std::array<int, 2> GetUnscaledKerning(
132  vtkTextProperty* tprop, vtkTypeUInt32 leftChar, vtkTypeUInt32 rightChar);
133 
135 
140  vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
141  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
142  vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
143  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
144  vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
145  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
147 
149 
157  bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
159 
161 
165  bool GetMetrics(
166  vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
168 
170 
180  bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
181  int textDims[2] = nullptr);
183 
185 
191  bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
193 
195 
200  int GetConstrainedFontSize(
201  const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
203 
208  static vtkTypeUInt16 HashString(const char* str);
209 
214  static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
215 
217 
227  void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
228  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
230 
232 
238  vtkSetMacro(ScaleToPowerTwo, bool);
239  vtkGetMacro(ScaleToPowerTwo, bool);
240  vtkBooleanMacro(ScaleToPowerTwo, bool);
242 
244 
249  vtkSetMacro(ForceCompiledFonts, bool);
250  vtkGetMacro(ForceCompiledFonts, bool);
251  vtkBooleanMacro(ForceCompiledFonts, bool);
253 
260  static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
261 
262 protected:
266  virtual FT_Error CreateFTCManager();
267 
269 
272  class MetaData;
273  class ImageMetaData;
274  bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
275  bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
277 
282  void PrepareImageData(vtkImageData* data, int bbox[4]);
283 
287  void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
288 
296  bool GetSize(vtkTextProperty* tprop, FT_Size* size);
297 
304  bool GetFace(vtkTextProperty* tprop, FT_Face* face);
305 
311  bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
312 
314 
326  enum
327  {
328  GLYPH_REQUEST_DEFAULT = 0,
329  GLYPH_REQUEST_BITMAP = 1,
330  GLYPH_REQUEST_OUTLINE = 2
331  };
332  bool GetGlyph(
333  vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
334  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
335  bool GetSize(FTC_Scaler scaler, FT_Size* size);
336  bool GetFace(size_t tprop_cache_id, FT_Face* face);
337  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
338  bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
339  int request = GLYPH_REQUEST_DEFAULT);
340  bool GetGlyph(
341  FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
343 
348 
350  ~vtkFreeTypeTools() override;
351 
355  bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
356 
358 
361  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
362  FT_BitmapGlyph& bitmap_glyph);
363  FT_Bitmap* GetBitmap(
364  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
366 
368 
371  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
372  FT_OutlineGlyph& outline_glyph);
373  FT_Outline* GetOutline(
374  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
376 
381 
385  vtkTextPropertyLookup* TextPropertyLookup;
386 
390  FT_Library* Library;
391 
393 
396  FTC_Manager* CacheManager;
397  FTC_ImageCache* ImageCache;
398  FTC_CMapCache* CMapCache;
400 
402 
405  FTC_Manager* GetCacheManager();
406  FTC_ImageCache* GetImageCache();
407  FTC_CMapCache* GetCMapCache();
409 
410  unsigned int MaximumNumberOfFaces;
411  unsigned int MaximumNumberOfSizes;
412  unsigned long MaximumNumberOfBytes;
413 
416 
417  void InitializeCacheManager();
418  void ReleaseCacheManager();
419 
420 private:
421  vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
422  void operator=(const vtkFreeTypeTools&) = delete;
423 
427  bool RenderStringInternal(
428  vtkTextProperty* tprop, const std::string& str, int dpi, vtkImageData* data, int textDims[2]);
429 
433  bool StringToPathInternal(vtkTextProperty* tprop, const std::string& str, int dpi, vtkPath* path);
434 
436 
440  bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
441  bool CalculateBoundingBox(
442  const std::string& str, MetaData& metaData, const std::string& defaultHeightString);
444 
450  template <typename DataType>
451  bool PopulateData(const std::string& str, DataType data, MetaData& metaData);
452 
456  template <typename DataType>
457  bool RenderLine(std::string::const_iterator begin, std::string::const_iterator end, int lineIndex,
458  DataType data, MetaData& metaData);
459 
461 
464  bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
465  vtkImageData* image, MetaData& metaData);
466  bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
467  vtkPath* path, MetaData& metaData);
469 
470  void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
471 
477  int FitStringToBBox(
478  const std::string& str, MetaData& metaData, int targetWidth, int targetHeight);
479 
481 
488  void GetLineMetrics(std::string::const_iterator begin, std::string::const_iterator end,
489  MetaData& metaData, int& width, int bbox[4]);
491 };
492 
493 // This is here to implement the Schwarz counter idiom.
495 
496 VTK_ABI_NAMESPACE_END
497 #endif
bool ScaleToPowerTwo
Should the image be scaled to the next highest power of 2?
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:28
abstract base class for most VTK objects
Definition: vtkObject.h:51
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
unsigned long MaximumNumberOfBytes
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:24
vtkTextPropertyLookup * TextPropertyLookup
Lookup table that maps free type font cache face ids to vtkTextProperties.
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
FTC_CMapCache * CMapCache
The cache manager, image cache and charmap cache.
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:150
FTC_Manager * CacheManager
The cache manager, image cache and charmap cache.
static vtkFreeTypeTools * Instance
The singleton instance.
std::array< int, 4 > BoundingBox
vtkSmartPointer< vtkPath > Path
unsigned int MaximumNumberOfSizes
a simple class to control print indentation
Definition: vtkIndent.h:28
topologically and geometrically regular array of data
Definition: vtkImageData.h:42
FTC_ImageCache * ImageCache
The cache manager, image cache and charmap cache.
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:146
represent text properties.
unsigned int MaximumNumberOfFaces
FreeType library support.
FT_Library * Library
FreeType library instance.