VTK  9.2.6
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 =========================================================================*/
29 #ifndef vtkFreeTypeTools_h
30 #define vtkFreeTypeTools_h
31 
32 #include "vtkObject.h"
33 #include "vtkRenderingFreeTypeModule.h" // For export macro
34 #include "vtkSmartPointer.h" // For smart pointer
35 #include "vtkTextRenderer.h" // For Metrics struct
36 
37 #include <array> // for std::array
38 
39 class vtkImageData;
40 class vtkPath;
41 class vtkTextProperty;
42 class vtkStdString;
43 
44 // FreeType
45 #include "vtk_freetype.h" //since ft2build.h could be in the path
46 #include FT_FREETYPE_H
47 #include FT_GLYPH_H
48 #include FT_CACHE_H
49 
50 class FTFont;
51 
52 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
53 class vtkTextPropertyLookup;
54 
55 //----------------------------------------------------------------------------
56 // Singleton cleanup
57 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
58 {
59 public:
62 
63 private:
65  vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
66 };
67 
68 //----------------------------------------------------------------------------
69 // Singleton font cache
70 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
71 {
72 public:
73  struct FaceMetrics
74  {
76  int Ascender;
77  int Descender;
79  std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
81  bool Scalable;
82  bool Bold;
83  bool Italic;
84  };
85  struct GlyphOutline
86  {
89  };
90 
91  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
92  void PrintSelf(ostream& os, vtkIndent indent) override;
93 
97  static vtkFreeTypeTools* GetInstance();
98 
103  static void SetInstance(vtkFreeTypeTools* instance);
104 
106 
110  vtkSetMacro(DebugTextures, bool);
111  vtkGetMacro(DebugTextures, bool);
112  vtkBooleanMacro(DebugTextures, bool);
114 
118  FT_Library* GetLibrary();
119 
125  FaceMetrics GetFaceMetrics(vtkTextProperty* tprop);
126 
132  GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty* tprop, vtkTypeUInt32 charId);
133 
140  std::array<int, 2> GetUnscaledKerning(
141  vtkTextProperty* tprop, vtkTypeUInt32 leftChar, vtkTypeUInt32 rightChar);
142 
144 
149  vtkSetClampMacro(MaximumNumberOfFaces, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
150  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
151  vtkSetClampMacro(MaximumNumberOfSizes, unsigned int, 1, VTK_UNSIGNED_INT_MAX);
152  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
153  vtkSetClampMacro(MaximumNumberOfBytes, unsigned long, 1, VTK_UNSIGNED_LONG_MAX);
154  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
156 
158 
166  bool GetBoundingBox(vtkTextProperty* tprop, const vtkStdString& str, int dpi, int bbox[4]);
168 
170 
174  bool GetMetrics(
175  vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkTextRenderer::Metrics& metrics);
177 
179 
189  bool RenderString(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkImageData* data,
190  int textDims[2] = nullptr);
192 
194 
200  bool StringToPath(vtkTextProperty* tprop, const vtkStdString& str, int dpi, vtkPath* path);
202 
204 
209  int GetConstrainedFontSize(
210  const vtkStdString& str, vtkTextProperty* tprop, int dpi, int targetWidth, int targetHeight);
212 
217  static vtkTypeUInt16 HashString(const char* str);
218 
223  static vtkTypeUInt32 HashBuffer(const void* buffer, size_t n, vtkTypeUInt32 hash = 0);
224 
226 
236  void MapTextPropertyToId(vtkTextProperty* tprop, size_t* tprop_cache_id);
237  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty* tprop);
239 
241 
247  vtkSetMacro(ScaleToPowerTwo, bool);
248  vtkGetMacro(ScaleToPowerTwo, bool);
249  vtkBooleanMacro(ScaleToPowerTwo, bool);
251 
253 
258  vtkSetMacro(ForceCompiledFonts, bool);
259  vtkGetMacro(ForceCompiledFonts, bool);
260  vtkBooleanMacro(ForceCompiledFonts, bool);
262 
269  static bool LookupFace(vtkTextProperty* tprop, FT_Library lib, FT_Face* face);
270 
271 protected:
275  virtual FT_Error CreateFTCManager();
276 
278 
281  class MetaData;
282  class ImageMetaData;
283  bool PrepareMetaData(vtkTextProperty* tprop, int dpi, MetaData& metaData);
284  bool PrepareImageMetaData(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
286 
291  void PrepareImageData(vtkImageData* data, int bbox[4]);
292 
296  void RenderBackground(vtkTextProperty* tprop, vtkImageData* image, ImageMetaData& metaData);
297 
305  bool GetSize(vtkTextProperty* tprop, FT_Size* size);
306 
313  bool GetFace(vtkTextProperty* tprop, FT_Face* face);
314 
320  bool GetGlyphIndex(vtkTextProperty* tprop, FT_UInt32 c, FT_UInt* gindex);
321 
323 
335  enum
336  {
337  GLYPH_REQUEST_DEFAULT = 0,
338  GLYPH_REQUEST_BITMAP = 1,
339  GLYPH_REQUEST_OUTLINE = 2
340  };
341  bool GetGlyph(
342  vtkTextProperty* tprop, FT_UInt32 c, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
343  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size* size);
344  bool GetSize(FTC_Scaler scaler, FT_Size* size);
345  bool GetFace(size_t tprop_cache_id, FT_Face* face);
346  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c, FT_UInt* gindex);
347  bool GetGlyph(size_t tprop_cache_id, int font_size, FT_UInt gindex, FT_Glyph* glyph,
348  int request = GLYPH_REQUEST_DEFAULT);
349  bool GetGlyph(
350  FTC_Scaler scaler, FT_UInt gindex, FT_Glyph* glyph, int request = GLYPH_REQUEST_DEFAULT);
352 
357 
359  ~vtkFreeTypeTools() override;
360 
364  bool GetFace(vtkTextProperty* prop, size_t& prop_cache_id, FT_Face& face, bool& face_has_kerning);
365 
367 
370  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
371  FT_BitmapGlyph& bitmap_glyph);
372  FT_Bitmap* GetBitmap(
373  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_BitmapGlyph& bitmap_glyph);
375 
377 
380  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id, int prop_font_size, FT_UInt& gindex,
381  FT_OutlineGlyph& outline_glyph);
382  FT_Outline* GetOutline(
383  FT_UInt32 c, FTC_Scaler scaler, FT_UInt& gindex, FT_OutlineGlyph& outline_glyph);
385 
390 
394  vtkTextPropertyLookup* TextPropertyLookup;
395 
399  FT_Library* Library;
400 
402 
405  FTC_Manager* CacheManager;
406  FTC_ImageCache* ImageCache;
407  FTC_CMapCache* CMapCache;
409 
411 
414  FTC_Manager* GetCacheManager();
415  FTC_ImageCache* GetImageCache();
416  FTC_CMapCache* GetCMapCache();
418 
419  unsigned int MaximumNumberOfFaces;
420  unsigned int MaximumNumberOfSizes;
421  unsigned long MaximumNumberOfBytes;
422 
425 
426  void InitializeCacheManager();
427  void ReleaseCacheManager();
428 
429 private:
430  vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
431  void operator=(const vtkFreeTypeTools&) = delete;
432 
436  bool RenderStringInternal(
437  vtkTextProperty* tprop, const std::string& str, int dpi, vtkImageData* data, int textDims[2]);
438 
442  bool StringToPathInternal(vtkTextProperty* tprop, const std::string& str, int dpi, vtkPath* path);
443 
445 
449  bool CalculateBoundingBox(const vtkStdString& str, MetaData& metaData);
450  bool CalculateBoundingBox(
451  const std::string& str, MetaData& metaData, const std::string& defaultHeightString);
453 
459  template <typename DataType>
460  bool PopulateData(const std::string& str, DataType data, MetaData& metaData);
461 
465  template <typename DataType>
466  bool RenderLine(std::string::const_iterator begin, std::string::const_iterator end, int lineIndex,
467  DataType data, MetaData& metaData);
468 
470 
473  bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
474  vtkImageData* image, MetaData& metaData);
475  bool RenderCharacter(FT_UInt32 codepoint, int& x, int& y, FT_UInt& previousGlyphIndex,
476  vtkPath* path, MetaData& metaData);
478 
479  void OutlineToPath(int x, int y, FT_Outline* outline, vtkPath* path);
480 
486  int FitStringToBBox(
487  const std::string& str, MetaData& metaData, int targetWidth, int targetHeight);
488 
490 
497  void GetLineMetrics(std::string::const_iterator begin, std::string::const_iterator end,
498  MetaData& metaData, int& width, int bbox[4]);
500 };
501 
502 // This is here to implement the Schwarz counter idiom.
504 
505 #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:38
abstract base class for most VTK objects
Definition: vtkObject.h:62
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:35
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:161
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:39
topologically and geometrically regular array of data
Definition: vtkImageData.h:53
FTC_ImageCache * ImageCache
The cache manager, image cache and charmap cache.
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:157
represent text properties.
unsigned int MaximumNumberOfFaces
FreeType library support.
FT_Library * Library
FreeType library instance.