VTK
dox/Rendering/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 =========================================================================*/
00025 #ifndef __vtkFreeTypeTools_h
00026 #define __vtkFreeTypeTools_h
00027 
00028 #include "vtkObject.h"
00029 
00030 class vtkImageData;
00031 class vtkTextProperty;
00032 class vtkStdString;
00033 class vtkUnicodeString;
00034 
00035 // FreeType
00036 #include "vtk_freetype.h"  //since ft2build.h could be in the path
00037 #include FT_FREETYPE_H
00038 #include FT_GLYPH_H
00039 #include FT_CACHE_H
00040 
00041 class FTFont;
00042 
00043 //----------------------------------------------------------------------------
00044 // Singleton cleanup
00045 class VTK_RENDERING_EXPORT vtkFreeTypeToolsCleanup
00046 {
00047 public:
00048   vtkFreeTypeToolsCleanup();
00049   ~vtkFreeTypeToolsCleanup();
00050 };
00051 
00052 //----------------------------------------------------------------------------
00053 // Singleton font cache
00054 class VTK_RENDERING_EXPORT vtkFreeTypeTools : public vtkObject
00055 {
00056 public:
00057   vtkTypeMacro(vtkFreeTypeTools, vtkObject);
00058   void PrintSelf(ostream& os, vtkIndent indent);
00059 
00061   static vtkFreeTypeTools* GetInstance();
00062 
00065   static void SetInstance(vtkFreeTypeTools *instance);
00066 
00068   FT_Library* GetLibrary();
00069 
00071 
00074   vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
00075   vtkGetMacro(MaximumNumberOfFaces, unsigned int);
00076   vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
00077   vtkGetMacro(MaximumNumberOfSizes, unsigned int);
00078   vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
00079   vtkGetMacro(MaximumNumberOfBytes, unsigned long);
00081 
00083 
00092   bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
00093                       int bbox[4]);
00094   bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
00095                       int bbox[4]);
00096   bool IsBoundingBoxValid(int bbox[4]);
00098 
00100 
00102   bool RenderString(vtkTextProperty *tprop, const vtkStdString& str,
00103                     vtkImageData *data);
00104   bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString& str,
00105                     vtkImageData *data);
00107 
00109 
00117   void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id);
00118   void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop);
00120 
00122 
00126   vtkSetMacro(ScaleToPowerTwo, bool);
00127   vtkGetMacro(ScaleToPowerTwo, bool);
00128   vtkBooleanMacro(ScaleToPowerTwo, bool);
00130 
00131 protected:
00133 
00135   template <typename T>
00136   bool CalculateBoundingBox(vtkTextProperty *tprop, const T& str, int bbox[4]);
00138 
00140 
00142   template <typename T>
00143   void PrepareImageData(vtkImageData *data,
00144                         vtkTextProperty *tprop,
00145                         const T& str,
00146                         int *x, int *y);
00148 
00150 
00151   template <typename T>
00152   bool PopulateImageData(vtkTextProperty *tprop, const T& str,
00153                          int x, int y, vtkImageData *data);
00155 
00160   bool GetSize(vtkTextProperty *tprop, FT_Size *size);
00161 
00166   bool GetFace(vtkTextProperty *tprop, FT_Face *face);
00167 
00170   bool GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
00171 
00173 
00183   enum
00184   {
00185     GLYPH_REQUEST_DEFAULT = 0,
00186     GLYPH_REQUEST_BITMAP  = 1,
00187     GLYPH_REQUEST_OUTLINE = 2
00188   };
00189   bool GetGlyph(vtkTextProperty *tprop,
00190                 FT_UInt32 c,
00191                 FT_Glyph *glyph,
00192                 int request = GLYPH_REQUEST_DEFAULT);
00193   bool GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size);
00194   bool GetFace(unsigned long tprop_cache_id, FT_Face *face);
00195   bool GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c,
00196                      FT_UInt *gindex);
00197   bool GetGlyph(unsigned long tprop_cache_id,
00198                 int font_size,
00199                 FT_UInt gindex,
00200                 FT_Glyph *glyph,
00201                 int request = GLYPH_REQUEST_DEFAULT);
00203 
00205   bool ScaleToPowerTwo;
00206 
00207   vtkFreeTypeTools();
00208   virtual ~vtkFreeTypeTools();
00209 
00210 private:
00211   vtkFreeTypeTools(const vtkFreeTypeTools&);  // Not implemented.
00212   void operator=(const vtkFreeTypeTools&);  // Not implemented.
00213 
00215 
00216   bool GetFace(vtkTextProperty *prop, unsigned long &prop_cache_id,
00217                FT_Face &face, bool &face_has_kerning);
00219 
00221 
00222   FT_Bitmap* GetBitmap(FT_UInt32 c, unsigned long prop_cache_id,
00223                        int prop_font_size, FT_UInt &gindex,
00224                        FT_BitmapGlyph &bitmap_glyph);
00226 
00227   // The singleton instance and the singleton cleanup instance
00228   static vtkFreeTypeTools* Instance;
00229   static vtkFreeTypeToolsCleanup Cleanup;
00230 
00231   // The cache manager, image cache and charmap cache
00232   FTC_Manager *CacheManager;
00233   FTC_ImageCache *ImageCache;
00234   FTC_CMapCache  *CMapCache;
00235 
00237 
00238   FTC_Manager* GetCacheManager();
00239   FTC_ImageCache* GetImageCache();
00240   FTC_CMapCache* GetCMapCache();
00242 
00243   unsigned int MaximumNumberOfFaces;
00244   unsigned int MaximumNumberOfSizes;
00245   unsigned long MaximumNumberOfBytes;
00246 
00247   void InitializeCacheManager();
00248   void ReleaseCacheManager();
00249 };
00250 
00251 #endif