VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkFreeTypeTools.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00028 #ifndef vtkFreeTypeTools_h 00029 #define vtkFreeTypeTools_h 00030 00031 #include "vtkRenderingFreeTypeModule.h" // For export macro 00032 #include "vtkObject.h" 00033 #include "vtkTextRenderer.h" // For Metrics struct 00034 00035 class vtkImageData; 00036 class vtkPath; 00037 class vtkTextProperty; 00038 class vtkStdString; 00039 class vtkUnicodeString; 00040 00041 // FreeType 00042 #include "vtk_freetype.h" //since ft2build.h could be in the path 00043 #include FT_FREETYPE_H 00044 #include FT_GLYPH_H 00045 #include FT_CACHE_H 00046 00047 class FTFont; 00048 00049 // PIMPL class for FTC_FaceID->vtkTextProperty lookup 00050 class vtkTextPropertyLookup; 00051 00052 //---------------------------------------------------------------------------- 00053 // Singleton cleanup 00054 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup 00055 { 00056 public: 00057 vtkFreeTypeToolsCleanup(); 00058 ~vtkFreeTypeToolsCleanup(); 00059 }; 00060 00061 //---------------------------------------------------------------------------- 00062 // Singleton font cache 00063 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject 00064 { 00065 public: 00066 vtkTypeMacro(vtkFreeTypeTools, vtkObject); 00067 void PrintSelf(ostream& os, vtkIndent indent); 00068 00070 static vtkFreeTypeTools* GetInstance(); 00071 00074 static void SetInstance(vtkFreeTypeTools *instance); 00075 00077 00079 vtkSetMacro(DebugTextures, bool) 00080 vtkGetMacro(DebugTextures, bool) 00081 vtkBooleanMacro(DebugTextures, bool) 00083 00085 FT_Library* GetLibrary(); 00086 00088 00091 vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX); 00092 vtkGetMacro(MaximumNumberOfFaces, unsigned int); 00093 vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX); 00094 vtkGetMacro(MaximumNumberOfSizes, unsigned int); 00095 vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX); 00096 vtkGetMacro(MaximumNumberOfBytes, unsigned long); 00098 00100 00105 bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str, 00106 int bbox[4]); 00107 bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str, 00108 int bbox[4]); 00110 00112 00114 bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str, 00115 vtkTextRenderer::Metrics &metrics); 00116 bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str, 00117 vtkTextRenderer::Metrics &metrics); 00119 00121 00129 bool RenderString(vtkTextProperty *tprop, const vtkStdString& str, 00130 vtkImageData *data, int textDims[2] = NULL); 00131 bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str, 00132 vtkImageData *data, int textDims[2] = NULL); 00134 00136 00140 bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str, 00141 vtkPath *path); 00142 bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str, 00143 vtkPath *path); 00145 00147 00150 int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop, 00151 int targetWidth, int targetHeight); 00152 int GetConstrainedFontSize(const vtkUnicodeString &str, 00153 vtkTextProperty *tprop, 00154 int targetWidth, int targetHeight); 00156 00159 static vtkTypeUInt16 HashString(const char *str); 00160 00162 00170 void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id); 00171 void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop); 00173 00175 00179 vtkSetMacro(ScaleToPowerTwo, bool); 00180 vtkGetMacro(ScaleToPowerTwo, bool); 00181 vtkBooleanMacro(ScaleToPowerTwo, bool); 00183 00185 00188 vtkSetMacro(ForceCompiledFonts, bool); 00189 vtkGetMacro(ForceCompiledFonts, bool); 00190 vtkBooleanMacro(ForceCompiledFonts, bool); 00192 00197 static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face); 00198 00199 protected: 00201 virtual FT_Error CreateFTCManager(); 00202 00204 00206 class MetaData; 00207 class ImageMetaData; 00208 bool PrepareMetaData(vtkTextProperty *tprop, MetaData &metaData); 00209 bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image, 00210 ImageMetaData &metaData); 00212 00215 void PrepareImageData(vtkImageData *data, int bbox[4]); 00216 00218 00219 void RenderBackground(vtkTextProperty *tprop, vtkImageData *image, 00220 ImageMetaData &metaData); 00222 00227 bool GetSize(vtkTextProperty *tprop, FT_Size *size); 00228 00233 bool GetFace(vtkTextProperty *tprop, FT_Face *face); 00234 00237 bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex); 00238 00240 00250 enum 00251 { 00252 GLYPH_REQUEST_DEFAULT = 0, 00253 GLYPH_REQUEST_BITMAP = 1, 00254 GLYPH_REQUEST_OUTLINE = 2 00255 }; 00256 bool GetGlyph(vtkTextProperty *tprop, 00257 FT_UInt32 c, 00258 FT_Glyph *glyph, 00259 int request = GLYPH_REQUEST_DEFAULT); 00260 bool GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size); 00261 bool GetFace(unsigned long tprop_cache_id, FT_Face *face); 00262 bool GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c, 00263 FT_UInt *gindex); 00264 bool GetGlyph(unsigned long tprop_cache_id, 00265 int font_size, 00266 FT_UInt gindex, 00267 FT_Glyph *glyph, 00268 int request = GLYPH_REQUEST_DEFAULT); 00270 00272 bool ScaleToPowerTwo; 00273 00274 vtkFreeTypeTools(); 00275 virtual ~vtkFreeTypeTools(); 00276 00278 00279 bool GetFace(vtkTextProperty *prop, unsigned long &prop_cache_id, 00280 FT_Face &face, bool &face_has_kerning); 00282 00284 00285 FT_Bitmap* GetBitmap(FT_UInt32 c, unsigned long prop_cache_id, 00286 int prop_font_size, FT_UInt &gindex, 00287 FT_BitmapGlyph &bitmap_glyph); 00289 00291 00292 FT_Outline* GetOutline(FT_UInt32 c, unsigned long prop_cache_id, 00293 int prop_font_size, FT_UInt &gindex, 00294 FT_OutlineGlyph &outline_glyph); 00296 00298 00299 static vtkFreeTypeTools* Instance; 00300 static vtkFreeTypeToolsCleanup Cleanup; 00302 00305 vtkTextPropertyLookup *TextPropertyLookup; 00306 00308 00309 FTC_Manager *CacheManager; 00310 FTC_ImageCache *ImageCache; 00311 FTC_CMapCache *CMapCache; 00313 00315 00316 FTC_Manager* GetCacheManager(); 00317 FTC_ImageCache* GetImageCache(); 00318 FTC_CMapCache* GetCMapCache(); 00320 00321 unsigned int MaximumNumberOfFaces; 00322 unsigned int MaximumNumberOfSizes; 00323 unsigned long MaximumNumberOfBytes; 00324 00325 bool ForceCompiledFonts; 00326 bool DebugTextures; 00327 00328 void InitializeCacheManager(); 00329 void ReleaseCacheManager(); 00330 00331 private: 00332 vtkFreeTypeTools(const vtkFreeTypeTools&); // Not implemented. 00333 void operator=(const vtkFreeTypeTools&); // Not implemented. 00334 00336 00337 template <typename StringType> 00338 bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str, 00339 vtkImageData *data, int textDims[2]); 00341 00343 00344 template <typename StringType> 00345 bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str, 00346 vtkPath *path); 00348 00350 00352 template <typename T> 00353 bool CalculateBoundingBox(const T& str, MetaData &metaData); 00355 00357 00360 template <typename StringType, typename DataType> 00361 bool PopulateData(const StringType& str, DataType data, MetaData &metaData); 00363 00365 00367 template <typename IteratorType, typename DataType> 00368 bool RenderLine(IteratorType begin, IteratorType end, int lineIndex, 00369 DataType data, MetaData &metaData); 00371 00373 00375 template <typename CharType> 00376 bool RenderCharacter(CharType character, int &x, int &y, 00377 FT_UInt &previousGlyphIndex, vtkImageData *image, 00378 MetaData &metaData); 00379 template <typename CharType> 00380 bool RenderCharacter(CharType character, int &x, int &y, 00381 FT_UInt &previousGlyphIndex, vtkPath *path, 00382 MetaData &metaData); 00384 00386 00389 template <typename T> 00390 int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth, 00391 int targetHeight); 00393 00395 00400 template<typename T> 00401 void GetLineMetrics(T begin, T end, MetaData &metaData, int &width, 00402 int bbox[4]); 00403 }; 00405 00406 #endif