VTK
dox/Rendering/vtkFreeTypeUtilities.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkFreeTypeUtilities.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 __vtkFreeTypeUtilities_h
00026 #define __vtkFreeTypeUtilities_h
00027 
00028 #define VTK_FTFC_CACHE_CAPACITY 150
00029 
00030 #include "vtkObject.h"
00031 
00032 class vtkImageData;
00033 class vtkTextProperty;
00034 
00035 // FreeType
00036 
00037 #include <FTGL.h>
00038 
00039 #include "vtk_freetype.h"  //since ft2build.h could be in the path
00040 #include FT_FREETYPE_H
00041 #include FT_GLYPH_H
00042 
00043 #if (FREETYPE_MAJOR >2 ||(FREETYPE_MAJOR == 2 && ( FREETYPE_MINOR > 1 || (FREETYPE_MINOR == 1 && FREETYPE_PATCH >= 9))))
00044 # include FT_CACHE_H
00045 // This flag will be used to check if Caching support is to be compiled.
00046 # define VTK_FREETYPE_CACHING_SUPPORTED
00047 #endif
00048 
00049 #ifdef FTGL_USE_NAMESPACE
00050 namespace ftgl
00051 {
00052 #endif
00053 class FTFont;
00054 #ifdef FTGL_USE_NAMESPACE
00055 }
00056 #endif
00057 
00058 //----------------------------------------------------------------------------
00059 // Singleton cleanup
00060 
00061 class VTK_RENDERING_EXPORT vtkFreeTypeUtilitiesCleanup
00062 {
00063 public:
00064   vtkFreeTypeUtilitiesCleanup();
00065   ~vtkFreeTypeUtilitiesCleanup();
00066 };
00067 
00068 //----------------------------------------------------------------------------
00069 // Singleton font cache
00070 
00071 class VTK_RENDERING_EXPORT vtkFreeTypeUtilities : public vtkObject
00072 {
00073 public:
00074   vtkTypeMacro(vtkFreeTypeUtilities, vtkObject);
00075   void PrintSelf(ostream& os, vtkIndent indent);
00076 
00083   static vtkFreeTypeUtilities *New();
00084 
00086   static vtkFreeTypeUtilities* GetInstance();
00087 
00090   static void SetInstance(vtkFreeTypeUtilities *instance);
00091 
00093   FT_Library* GetLibrary();
00094 
00096 
00099   vtkSetClampMacro(MaximumNumberOfFaces,unsigned int,1,VTK_UNSIGNED_INT_MAX);
00100   vtkGetMacro(MaximumNumberOfFaces, unsigned int);
00101   vtkSetClampMacro(MaximumNumberOfSizes,unsigned int,1,VTK_UNSIGNED_INT_MAX);
00102   vtkGetMacro(MaximumNumberOfSizes, unsigned int);
00103   vtkSetClampMacro(MaximumNumberOfBytes,unsigned long,1,VTK_UNSIGNED_LONG_MAX);
00104   vtkGetMacro(MaximumNumberOfBytes, unsigned long);
00106 
00112   int GetSize(vtkTextProperty *tprop, FT_Size *size);
00113 
00119   int GetFace(vtkTextProperty *tprop, FT_Face *face);
00120 
00124   int GetGlyphIndex(vtkTextProperty *tprop, FT_UInt32 c, FT_UInt *gindex);
00125 
00126   //BTX
00128 
00139   enum
00140   {
00141     GLYPH_REQUEST_DEFAULT = 0,
00142     GLYPH_REQUEST_BITMAP  = 1,
00143     GLYPH_REQUEST_OUTLINE = 2
00144   };
00145   //ETX
00146   int GetGlyph(vtkTextProperty *tprop,
00147                FT_UInt32 c,
00148                FT_Glyph *glyph,
00149                int request = GLYPH_REQUEST_DEFAULT);
00151 
00153 
00162   int GetBoundingBox(vtkTextProperty *tprop, const char *str, int bbox[4]);
00163   int IsBoundingBoxValid(int bbox[4]);
00165 
00167 
00169   int RenderString(vtkTextProperty *tprop,
00170                    const char *str,
00171                    vtkImageData *data);
00173 
00175 
00176   int RenderString(vtkTextProperty *tprop,
00177                    const char *str,
00178                    int x, int y,
00179                    vtkImageData *data);
00181 
00183 
00191   void MapTextPropertyToId(vtkTextProperty *tprop, unsigned long *tprop_cache_id);
00192   void MapIdToTextProperty(unsigned long tprop_cache_id, vtkTextProperty *tprop);
00194 
00196 
00198   int GetSize(unsigned long tprop_cache_id, int font_size, FT_Size *size);
00199   int GetFace(unsigned long tprop_cache_id, FT_Face *face);
00200   int GetGlyphIndex(unsigned long tprop_cache_id, FT_UInt32 c,
00201                     FT_UInt *gindex);
00202   int GetGlyph(unsigned long tprop_cache_id,
00203                int font_size,
00204                FT_UInt gindex,
00205                FT_Glyph *glyph,
00206                int request = GLYPH_REQUEST_DEFAULT);
00208 
00210 
00215   void GetWidthHeightDescender(const char *str,
00216                                vtkTextProperty *tprop,
00217                                int *width,
00218                                int *height,
00219                                float *descender);
00221 
00223 
00225   void PrepareImageData(vtkImageData *data,
00226                         vtkTextProperty *tprop,
00227                         const char *str,
00228                         int *x, int *y);
00230 
00232 
00234   int GetConstrainedFontSize(const char *str, vtkTextProperty *tprop,
00235                              double orientation, int targetWidth,
00236                              int targetHeight);
00238 
00240 
00241   void JustifyLine(const char *str, vtkTextProperty *tprop,
00242                    int totalWidth, int *x, int *y);
00244 
00245   // Old Code
00246   // Cache entry
00247 
00248   struct Entry
00249   {
00250     vtkTextProperty *TextProperty;
00251 #ifdef FTGL_USE_NAMESPACE
00252     ftgl::FTFont *Font;
00253 #else
00254     FTFont *Font;
00255 #endif
00256     float LargestAscender;
00257     float LargestDescender;
00258   };
00259 
00260   vtkFreeTypeUtilities::Entry* GetFont(vtkTextProperty *tprop,
00261                                        double override_color[3] = NULL);
00262 
00263 protected:
00264   //Internal helper method called by RenderString
00265   int PopulateImageData(vtkTextProperty *tprop, const char *str,
00266                         int x, int y, vtkImageData *data, int use_shadow_color);
00267   vtkFreeTypeUtilities();
00268   virtual ~vtkFreeTypeUtilities();
00269 
00270 private:
00271   vtkFreeTypeUtilities(const vtkFreeTypeUtilities&);  // Not implemented.
00272   void operator=(const vtkFreeTypeUtilities&);  // Not implemented.
00273 
00274   // The singleton instance and the singleton cleanup instance
00275 
00276   static vtkFreeTypeUtilities* Instance;
00277   static vtkFreeTypeUtilitiesCleanup Cleanup;
00278 
00279   // The cache manager, image cache and charmap cache
00280 
00281 #ifdef VTK_FREETYPE_CACHING_SUPPORTED
00282   FTC_Manager *CacheManager;
00283   FTC_ImageCache *ImageCache;
00284   FTC_CMapCache  *CMapCache;
00285 
00287 
00288   FTC_Manager* GetCacheManager();
00289   FTC_ImageCache* GetImageCache();
00290   FTC_CMapCache* GetCMapCache();
00291 #endif
00292 
00293 
00294   unsigned int MaximumNumberOfFaces;
00295   unsigned int MaximumNumberOfSizes;
00296   unsigned long MaximumNumberOfBytes;
00297 
00298   void InitializeCacheManager();
00299   void ReleaseCacheManager();
00300 
00301   // Old Code
00302 
00303   void PrintEntry(int i, char *msg = 0);
00304   void ReleaseEntry(int i);
00305 
00306   void InitializeCache();
00307   void ReleaseCache();
00308 
00309   Entry *Entries[VTK_FTFC_CACHE_CAPACITY];
00310   int NumberOfEntries;
00311 };
00312 
00313 #endif