VTK
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 "vtkRenderingFreeTypeModule.h" // For export macro
30 #include "vtkObject.h"
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 private:
62  vtkFreeTypeToolsCleanup& operator=(const vtkFreeTypeToolsCleanup&) = delete;
63 };
64 
65 //----------------------------------------------------------------------------
66 // Singleton font cache
67 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
68 {
69 public:
70  struct FaceMetrics
71  {
73  int Ascender;
74  int Descender;
76  std::array<int, 4> BoundingBox; // xmin, xmax, ymin, ymax
78  bool Scalable;
79  bool Bold;
80  bool Italic;
81  };
82  struct GlyphOutline
83  {
86  };
87 
88  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
89  void PrintSelf(ostream& os, vtkIndent indent) override;
90 
94  static vtkFreeTypeTools* GetInstance();
95 
100  static void SetInstance(vtkFreeTypeTools *instance);
101 
103 
107  vtkSetMacro(DebugTextures, bool)
108  vtkGetMacro(DebugTextures, bool)
109  vtkBooleanMacro(DebugTextures, bool)
111 
115  FT_Library* GetLibrary();
116 
122  FaceMetrics GetFaceMetrics(vtkTextProperty *tprop);
123 
129  GlyphOutline GetUnscaledGlyphOutline(vtkTextProperty *tprop,
131 
138  std::array<int, 2> GetUnscaledKerning(vtkTextProperty *tprop,
139  vtkUnicodeStringValueType leftChar,
140  vtkUnicodeStringValueType rightChar);
141 
143 
148  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
149  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
150  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
151  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
152  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
153  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
155 
157 
165  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
166  int dpi, int bbox[4]);
167  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
168  int dpi, int bbox[4]);
170 
172 
176  bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
177  vtkTextRenderer::Metrics &metrics);
178  bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str, int dpi,
179  vtkTextRenderer::Metrics &metrics);
181 
183 
193  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
194  vtkImageData *data, int textDims[2] = nullptr);
195  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
196  int dpi, vtkImageData *data, int textDims[2] = nullptr);
198 
200 
206  bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
207  vtkPath *path);
208  bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
209  int dpi, vtkPath *path);
211 
213 
218  int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
219  int dpi, int targetWidth, int targetHeight);
220  int GetConstrainedFontSize(const vtkUnicodeString &str,
221  vtkTextProperty *tprop, int dpi,
222  int targetWidth, int targetHeight);
224 
229  static vtkTypeUInt16 HashString(const char *str);
230 
235  static vtkTypeUInt32 HashBuffer(const void* str, size_t n, vtkTypeUInt32 hash = 0);
236 
238 
248  void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id);
249  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty *tprop);
251 
253 
259  vtkSetMacro(ScaleToPowerTwo, bool);
260  vtkGetMacro(ScaleToPowerTwo, bool);
261  vtkBooleanMacro(ScaleToPowerTwo, bool);
263 
265 
270  vtkSetMacro(ForceCompiledFonts, bool);
271  vtkGetMacro(ForceCompiledFonts, bool);
272  vtkBooleanMacro(ForceCompiledFonts, bool);
274 
281  static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face);
282 
283 protected:
287  virtual FT_Error CreateFTCManager();
288 
290 
293  class MetaData;
294  class ImageMetaData;
295  bool PrepareMetaData(vtkTextProperty *tprop, int dpi, MetaData &metaData);
296  bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image,
297  ImageMetaData &metaData);
299 
304  void PrepareImageData(vtkImageData *data, int bbox[4]);
305 
309  void RenderBackground(vtkTextProperty *tprop, vtkImageData *image,
310  ImageMetaData &metaData);
311 
319  bool GetSize(vtkTextProperty *tprop, FT_Size *size);
320 
327  bool GetFace(vtkTextProperty *tprop, FT_Face *face);
328 
334  bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
335 
337 
349  enum
350  {
351  GLYPH_REQUEST_DEFAULT = 0,
352  GLYPH_REQUEST_BITMAP = 1,
353  GLYPH_REQUEST_OUTLINE = 2
354  };
355  bool GetGlyph(vtkTextProperty *tprop,
356  FT_UInt32 c,
357  FT_Glyph *glyph,
358  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,
363  FT_UInt *gindex);
364  bool GetGlyph(size_t tprop_cache_id,
365  int font_size,
366  FT_UInt gindex,
367  FT_Glyph *glyph,
368  int request = GLYPH_REQUEST_DEFAULT);
369  bool GetGlyph(FTC_Scaler scaler,
370  FT_UInt gindex,
371  FT_Glyph *glyph,
372  int request = GLYPH_REQUEST_DEFAULT);
374 
379 
381  ~vtkFreeTypeTools() override;
382 
386  bool GetFace(vtkTextProperty *prop, size_t &prop_cache_id,
387  FT_Face &face, bool &face_has_kerning);
388 
390 
393  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id,
394  int prop_font_size, FT_UInt &gindex,
395  FT_BitmapGlyph &bitmap_glyph);
396  FT_Bitmap* GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
397  FT_BitmapGlyph &bitmap_glyph);
399 
401 
404  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id,
405  int prop_font_size, FT_UInt &gindex,
406  FT_OutlineGlyph &outline_glyph);
407  FT_Outline* GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
408  FT_OutlineGlyph &outline_glyph);
410 
415 
419  vtkTextPropertyLookup *TextPropertyLookup;
420 
424  FT_Library *Library;
425 
427 
430  FTC_Manager *CacheManager;
431  FTC_ImageCache *ImageCache;
432  FTC_CMapCache *CMapCache;
434 
436 
439  FTC_Manager* GetCacheManager();
440  FTC_ImageCache* GetImageCache();
441  FTC_CMapCache* GetCMapCache();
443 
444  unsigned int MaximumNumberOfFaces;
445  unsigned int MaximumNumberOfSizes;
446  unsigned long MaximumNumberOfBytes;
447 
450 
451  void InitializeCacheManager();
452  void ReleaseCacheManager();
453 
454 private:
455  vtkFreeTypeTools(const vtkFreeTypeTools&) = delete;
456  void operator=(const vtkFreeTypeTools&) = delete;
457 
461  template <typename StringType>
462  bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
463  int dpi, vtkImageData *data, int textDims[2]);
464 
468  template <typename StringType>
469  bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
470  int dpi, vtkPath *path);
471 
473 
477  bool CalculateBoundingBox(const vtkStdString& str, MetaData &metaData);
478  bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData &metaData);
479  template <typename T>
480  bool CalculateBoundingBox(const T& str, MetaData &metaData, const T& defaultHeightString);
482 
488  template <typename StringType, typename DataType>
489  bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
490 
494  template <typename IteratorType, typename DataType>
495  bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
496  DataType data, MetaData &metaData);
497 
499 
502  template <typename CharType>
503  bool RenderCharacter(CharType character, int &x, int &y,
504  FT_UInt &previousGlyphIndex, vtkImageData *image,
505  MetaData &metaData);
506  template <typename CharType>
507  bool RenderCharacter(CharType character, int &x, int &y,
508  FT_UInt &previousGlyphIndex, vtkPath *path,
509  MetaData &metaData);
511 
512  void OutlineToPath(int x, int y, FT_Outline *outline, vtkPath *path);
513 
519  template <typename T>
520  int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
521  int targetHeight);
522 
524 
531  template<typename T>
532  void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
533  int bbox[4]);
534 };
536 
537 // This is here to implement the Schwarz counter idiom.
539 
540 #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:34
abstract base class for most VTK objects
Definition: vtkObject.h:53
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:32
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:165
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:33
topologically and geometrically regular array of data
Definition: vtkImageData.h:39
FTC_ImageCache * ImageCache
The cache manager, image cache and charmap cache.
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:161
represent text properties.
unsigned int MaximumNumberOfFaces
FreeType library support.
vtkTypeUInt32 vtkUnicodeStringValueType
String class that stores Unicode text.
FT_Library * Library
FreeType library instance.