VTK
dox/Rendering/FreeType/vtkFreeTypeTools.h
Go to the documentation of this file.
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 
00034 class vtkImageData;
00035 class vtkPath;
00036 class vtkTextProperty;
00037 class vtkStdString;
00038 class vtkUnicodeString;
00039 
00040 // FreeType
00041 #include "vtk_freetype.h"  //since ft2build.h could be in the path
00042 #include FT_FREETYPE_H
00043 #include FT_GLYPH_H
00044 #include FT_CACHE_H
00045 
00046 class FTFont;
00047 
00048 // PIMPL class for FTC_FaceID->vtkTextProperty lookup
00049 class vtkTextPropertyLookup;
00050 
00051 //----------------------------------------------------------------------------
00052 // Singleton cleanup
00053 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeToolsCleanup
00054 {
00055 public:
00056   vtkFreeTypeToolsCleanup();
00057   ~vtkFreeTypeToolsCleanup();
00058 };
00059 
00060 //----------------------------------------------------------------------------
00061 // Singleton font cache
00062 class VTKRENDERINGFREETYPE_EXPORT vtkFreeTypeTools : public vtkObject
00063 {
00064 public:
00065   vtkTypeMacro(vtkFreeTypeTools, vtkObject);
00066   void PrintSelf(ostream& os, vtkIndent indent);
00067 
00069   static vtkFreeTypeTools* GetInstance();
00070 
00073   static void SetInstance(vtkFreeTypeTools *instance);
00074 
00076   FT_Library* GetLibrary();
00077 
00079 
00082   vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
00083   vtkGetMacro(MaximumNumberOfFaces, unsigned int);
00084   vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
00085   vtkGetMacro(MaximumNumberOfSizes, unsigned int);
00086   vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
00087   vtkGetMacro(MaximumNumberOfBytes, unsigned long);
00089 
00091 
00100   bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
00101                       int bbox[4]);
00102   bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
00103                       int bbox[4]);
00104   bool IsBoundingBoxValid(int bbox[4]);
00106 
00108 
00114   bool RenderString(vtkTextProperty *tprop, const vtkStdString& str,
00115                     vtkImageData *data, int textDims[2] = NULL);
00116   bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
00117                     vtkImageData *data, int textDims[2] = NULL);
00119 
00121 
00123   bool StringToPath(vtkTextProperty *tprop, const vtkStdString& str,
00124                     vtkPath *path);
00125   bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString& str,
00126                     vtkPath *path);
00128 
00130 
00133   int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop,
00134                              int targetWidth, int targetHeight);
00135   int GetConstrainedFontSize(const vtkUnicodeString &str,
00136                              vtkTextProperty *tprop,
00137                              int targetWidth, int targetHeight);
00139 
00142   static vtkTypeUInt16 HashString(const char *str);
00143 
00145 
00153   void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id);
00154   void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop);
00156 
00158 
00162   vtkSetMacro(ScaleToPowerTwo, bool);
00163   vtkGetMacro(ScaleToPowerTwo, bool);
00164   vtkBooleanMacro(ScaleToPowerTwo, bool);
00166 
00168 
00171   vtkSetMacro(ForceCompiledFonts, bool);
00172   vtkGetMacro(ForceCompiledFonts, bool);
00173   vtkBooleanMacro(ForceCompiledFonts, bool);
00175 
00180   static bool LookupFace(vtkTextProperty *tprop, FT_Library lib, FT_Face *face);
00181 
00182 protected:
00184   virtual FT_Error CreateFTCManager();
00185 
00187 
00189   class MetaData;
00190   class ImageMetaData;
00191   bool PrepareMetaData(vtkTextProperty *tprop, MetaData &metaData);
00192   bool PrepareImageMetaData(vtkTextProperty *tprop, vtkImageData *image,
00193                             ImageMetaData &metaData);
00195 
00197 
00198   template <typename StringType>
00199   bool RenderStringInternal(vtkTextProperty *tprop, const StringType &str,
00200                             vtkImageData *data, int textDims[2]);
00202 
00204 
00205   template <typename StringType>
00206   bool StringToPathInternal(vtkTextProperty *tprop, const StringType &str,
00207                             vtkPath *path);
00209 
00211 
00214   template <typename T>
00215   bool CalculateBoundingBox(const T& str, MetaData &metaData);
00217 
00220   void PrepareImageData(vtkImageData *data, int bbox[4]);
00221 
00223 
00226   template <typename StringType, typename DataType>
00227   bool PopulateData(const StringType& str, DataType data, MetaData &metaData);
00229 
00231 
00233   template <typename IteratorType, typename DataType>
00234   bool RenderLine(IteratorType begin, IteratorType end, int lineIndex,
00235                   DataType data, MetaData &metaData);
00237 
00239 
00241   template <typename CharType>
00242   bool RenderCharacter(CharType character, int &x, int &y,
00243                        FT_UInt &previousGlyphIndex, vtkImageData *image,
00244                        MetaData &metaData);
00245   template <typename CharType>
00246   bool RenderCharacter(CharType character, int &x, int &y,
00247                        FT_UInt &previousGlyphIndex, vtkPath *path,
00248                        MetaData &metaData);
00250 
00252   void JustifyPath(vtkPath *path, MetaData &metaData);
00253 
00255 
00258   template <typename T>
00259   int FitStringToBBox(const T &str, MetaData &metaData, int targetWidth,
00260                       int targetHeight);
00262 
00267   bool GetSize(vtkTextProperty *tprop, FT_Size *size);
00268 
00273   bool GetFace(vtkTextProperty *tprop, FT_Face *face);
00274 
00277   bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
00278 
00280 
00290   enum
00291   {
00292     GLYPH_REQUEST_DEFAULT = 0,
00293     GLYPH_REQUEST_BITMAP  = 1,
00294     GLYPH_REQUEST_OUTLINE = 2
00295   };
00296   bool GetGlyph(vtkTextProperty *tprop,
00297                 FT_UInt32 c,
00298                 FT_Glyph *glyph,
00299                 int request = GLYPH_REQUEST_DEFAULT);
00300   bool GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size);
00301   bool GetFace(unsigned long tprop_cache_id, FT_Face *face);
00302   bool GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c,
00303                      FT_UInt *gindex);
00304   bool GetGlyph(unsigned long tprop_cache_id,
00305                 int font_size,
00306                 FT_UInt gindex,
00307                 FT_Glyph *glyph,
00308                 int request = GLYPH_REQUEST_DEFAULT);
00310 
00312   bool ScaleToPowerTwo;
00313 
00314   vtkFreeTypeTools();
00315   virtual ~vtkFreeTypeTools();
00316 
00318 
00319   bool GetFace(vtkTextProperty *prop, unsigned long &prop_cache_id,
00320                FT_Face &face, bool &face_has_kerning);
00322 
00324 
00325   FT_Bitmap* GetBitmap(FT_UInt32 c, unsigned long prop_cache_id,
00326                        int prop_font_size, FT_UInt &gindex,
00327                        FT_BitmapGlyph &bitmap_glyph);
00329 
00331 
00332   FT_Outline* GetOutline(FT_UInt32 c, unsigned long prop_cache_id,
00333                          int prop_font_size, FT_UInt &gindex,
00334                          FT_OutlineGlyph &outline_glyph);
00336 
00338 
00343   template<typename T>
00344   void GetLineMetrics(T begin, T end, MetaData &metaData, int &width,
00345                       int bbox[4]);
00347 
00349 
00350   static vtkFreeTypeTools* Instance;
00351   static vtkFreeTypeToolsCleanup Cleanup;
00353 
00356   vtkTextPropertyLookup *TextPropertyLookup;
00357 
00359 
00360   FTC_Manager *CacheManager;
00361   FTC_ImageCache *ImageCache;
00362   FTC_CMapCache  *CMapCache;
00364 
00366 
00367   FTC_Manager* GetCacheManager();
00368   FTC_ImageCache* GetImageCache();
00369   FTC_CMapCache* GetCMapCache();
00371 
00372   unsigned int MaximumNumberOfFaces;
00373   unsigned int MaximumNumberOfSizes;
00374   unsigned long MaximumNumberOfBytes;
00375 
00376   bool ForceCompiledFonts;
00377 
00378   void InitializeCacheManager();
00379   void ReleaseCacheManager();
00380 
00381 private:
00382   vtkFreeTypeTools(const vtkFreeTypeTools&);  // Not implemented.
00383   void operator=(const vtkFreeTypeTools&);  // Not implemented.
00384 };
00385 
00386 #endif