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 "vtkTextRenderer.h" // For Metrics struct
32 
33 class vtkImageData;
34 class vtkPath;
35 class vtkTextProperty;
36 class vtkStdString;
37 class vtkUnicodeString;
38 
39 // FreeType
40 #include "vtk_freetype.h" //since ft2build.h could be in the path
41 #include FT_FREETYPE_H
42 #include FT_GLYPH_H
43 #include FT_CACHE_H
44 
45 class FTFont;
46 
47 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
48 class vtkTextPropertyLookup;
49 
50 //----------------------------------------------------------------------------
51 // Singleton cleanup
52 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
53 {
54 public:
57 private:
60 };
61 
62 //----------------------------------------------------------------------------
63 // Singleton font cache
64 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
65 {
66 public:
67  vtkTypeMacro(vtkFreeTypeTools, vtkObject);
68  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
69 
73  static vtkFreeTypeTools* GetInstance();
74 
79  static void SetInstance(vtkFreeTypeTools *instance);
80 
82 
86  vtkSetMacro(DebugTextures, bool)
87  vtkGetMacro(DebugTextures, bool)
88  vtkBooleanMacro(DebugTextures, bool)
90 
94  FT_Library* GetLibrary();
95 
97 
102  vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
103  vtkGetMacro(MaximumNumberOfFaces, unsigned int);
104  vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
105  vtkGetMacro(MaximumNumberOfSizes, unsigned int);
106  vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
107  vtkGetMacro(MaximumNumberOfBytes, unsigned long);
109 
111 
119  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
120  int dpi, int bbox[4]);
121  bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
122  int dpi, int bbox[4]);
124 
126 
130  bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
131  vtkTextRenderer::Metrics &metrics);
132  bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str, int dpi,
133  vtkTextRenderer::Metrics &metrics);
135 
137 
147  bool RenderString(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
148  vtkImageData *data, int textDims[2] = NULL);
149  bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
150  int dpi, vtkImageData *data, int textDims[2] = NULL);
152 
154 
160  bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str, int dpi,
161  vtkPath *path);
162  bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
163  int dpi, vtkPath *path);
165 
167 
172  int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
173  int dpi, int targetWidth, int targetHeight);
174  int GetConstrainedFontSize(const vtkUnicodeString &str,
175  vtkTextProperty *tprop, int dpi,
176  int targetWidth, int targetHeight);
178 
183  static vtkTypeUInt16 HashString(const char *str);
184 
189  static vtkTypeUInt32 HashBuffer(const void* str, size_t n, vtkTypeUInt32 hash = 0);
190 
192 
202  void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id);
203  void MapIdToTextProperty(size_t tprop_cache_id, vtkTextProperty *tprop);
205 
207 
213  vtkSetMacro(ScaleToPowerTwo, bool);
214  vtkGetMacro(ScaleToPowerTwo, bool);
215  vtkBooleanMacro(ScaleToPowerTwo, bool);
217 
219 
224  vtkSetMacro(ForceCompiledFonts, bool);
225  vtkGetMacro(ForceCompiledFonts, bool);
226  vtkBooleanMacro(ForceCompiledFonts, bool);
228 
235  static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face);
236 
237 protected:
241  virtual FT_Error CreateFTCManager();
242 
244 
247  class MetaData;
248  class ImageMetaData;
249  bool PrepareMetaData(vtkTextProperty *tprop, int dpi, MetaData &metaData);
250  bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image,
251  ImageMetaData &metaData);
253 
258  void PrepareImageData(vtkImageData *data, int bbox[4]);
259 
263  void RenderBackground(vtkTextProperty *tprop, vtkImageData *image,
264  ImageMetaData &metaData);
265 
273  bool GetSize(vtkTextProperty *tprop, FT_Size *size);
274 
281  bool GetFace(vtkTextProperty *tprop, FT_Face *face);
282 
288  bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
289 
291 
303  enum
304  {
305  GLYPH_REQUEST_DEFAULT = 0,
306  GLYPH_REQUEST_BITMAP = 1,
307  GLYPH_REQUEST_OUTLINE = 2
308  };
309  bool GetGlyph(vtkTextProperty *tprop,
310  FT_UInt32 c,
311  FT_Glyph *glyph,
312  int request = GLYPH_REQUEST_DEFAULT);
313  bool GetSize(size_t tprop_cache_id, int font_size, FT_Size *size);
314  bool GetSize(FTC_Scaler scaler, FT_Size *size);
315  bool GetFace(size_t tprop_cache_id, FT_Face *face);
316  bool GetGlyphIndex(size_t tprop_cache_id, FT_UInt32 c,
317  FT_UInt *gindex);
318  bool GetGlyph(size_t tprop_cache_id,
319  int font_size,
320  FT_UInt gindex,
321  FT_Glyph *glyph,
322  int request = GLYPH_REQUEST_DEFAULT);
323  bool GetGlyph(FTC_Scaler scaler,
324  FT_UInt gindex,
325  FT_Glyph *glyph,
326  int request = GLYPH_REQUEST_DEFAULT);
328 
333 
335  ~vtkFreeTypeTools() VTK_OVERRIDE;
336 
340  bool GetFace(vtkTextProperty *prop, size_t &prop_cache_id,
341  FT_Face &face, bool &face_has_kerning);
342 
344 
347  FT_Bitmap* GetBitmap(FT_UInt32 c, size_t prop_cache_id,
348  int prop_font_size, FT_UInt &gindex,
349  FT_BitmapGlyph &bitmap_glyph);
350  FT_Bitmap* GetBitmap(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
351  FT_BitmapGlyph &bitmap_glyph);
353 
355 
358  FT_Outline* GetOutline(FT_UInt32 c, size_t prop_cache_id,
359  int prop_font_size, FT_UInt &gindex,
360  FT_OutlineGlyph &outline_glyph);
361  FT_Outline* GetOutline(FT_UInt32 c, FTC_Scaler scaler, FT_UInt &gindex,
362  FT_OutlineGlyph &outline_glyph);
364 
368  static vtkFreeTypeTools* Instance;
369 
373  vtkTextPropertyLookup *TextPropertyLookup;
374 
378  FT_Library *Library;
379 
381 
384  FTC_Manager *CacheManager;
385  FTC_ImageCache *ImageCache;
386  FTC_CMapCache *CMapCache;
388 
390 
393  FTC_Manager* GetCacheManager();
394  FTC_ImageCache* GetImageCache();
395  FTC_CMapCache* GetCMapCache();
397 
398  unsigned int MaximumNumberOfFaces;
399  unsigned int MaximumNumberOfSizes;
400  unsigned long MaximumNumberOfBytes;
401 
402  bool ForceCompiledFonts;
403  bool DebugTextures;
404 
405  void InitializeCacheManager();
406  void ReleaseCacheManager();
407 
408 private:
409  vtkFreeTypeTools(const vtkFreeTypeTools&) VTK_DELETE_FUNCTION;
410  void operator=(const vtkFreeTypeTools&) VTK_DELETE_FUNCTION;
411 
415  template <typename StringType>
416  bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
417  int dpi, vtkImageData *data, int textDims[2]);
418 
422  template <typename StringType>
423  bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
424  int dpi, vtkPath *path);
425 
427 
431  bool CalculateBoundingBox(const vtkStdString& str, MetaData &metaData);
432  bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData &metaData);
433  template <typename T>
434  bool CalculateBoundingBox(const T& str, MetaData &metaData, const T& defaultHeightString);
436 
442  template <typename StringType, typename DataType>
443  bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
444 
448  template <typename IteratorType, typename DataType>
449  bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
450  DataType data, MetaData &metaData);
451 
453 
456  template <typename CharType>
457  bool RenderCharacter(CharType character, int &x, int &y,
458  FT_UInt &previousGlyphIndex, vtkImageData *image,
459  MetaData &metaData);
460  template <typename CharType>
461  bool RenderCharacter(CharType character, int &x, int &y,
462  FT_UInt &previousGlyphIndex, vtkPath *path,
463  MetaData &metaData);
465 
471  template <typename T>
472  int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
473  int targetHeight);
474 
476 
483  template<typename T>
484  void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
485  int bbox[4]);
486 };
488 
489 // This is here to implement the Schwarz counter idiom.
491 
492 #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:41
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.
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:32
static vtkFreeTypeToolsCleanup vtkFreeTypeToolsCleanupInstance
#define VTK_UNSIGNED_LONG_MAX
Definition: vtkType.h:163
a simple class to control print indentation
Definition: vtkIndent.h:33
topologically and geometrically regular array of data
Definition: vtkImageData.h:39
#define VTK_UNSIGNED_INT_MAX
Definition: vtkType.h:159
represent text properties.
vtkSetMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
vtkBooleanMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
FreeType library support.
String class that stores Unicode text.