00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Side Effects Software Inc 00008 * 123 Front Street West, Suite 1401 00009 * Toronto, Ontario 00010 * Canada M5J 2M2 00011 * 416-504-9876 00012 * 00013 * NAME: RE_Font.h (RE Library, C++) 00014 * 00015 * COMMENTS: 00016 */ 00017 00018 #ifndef __RE_Font__ 00019 #define __RE_Font__ 00020 00021 #include "RE_API.h" 00022 #include <UT/UT_String.h> 00023 00024 #define DEFAULT_FONTNAME "Proportional" 00025 #define DEFAULT_FONTSIZE 12.0 00026 00027 class RE_Render; 00028 class UT_WorkBuffer; 00029 00030 #define LOADFONT() { if (!myFontLoaded) loadFont(); } 00031 #define POINTS_PER_INCH 72.0 00032 00033 class RE_API RE_Font 00034 { 00035 /* 00036 * This is the generic base class for font access. Sub-classes of RE_Font 00037 * must implement the pure virtual methods defined below. Font objects 00038 * are instantiated by calling RE_Render::getFont which relies on 00039 * its device dependent sub-class to create a RE_Font sub-class suitable 00040 * for use on that device. 00041 */ 00042 public: 00043 /// Construct an RE_Font. name is the name of the font face you want to 00044 /// use, and size is the size in points you want to use. 00045 RE_Font(const char *name, float size = DEFAULT_FONTSIZE); 00046 virtual ~RE_Font(); 00047 00048 /// Set the current font to name and size. Invalidates the current font. 00049 void setFont(const char *name, float size); 00050 00051 /// Note that returning a UT_String usually isn't safe, but myFontName has 00052 /// been constructed with UT_String::ALWAYS_DEEP, so it won't cause any 00053 /// problems. 00054 UT_String getName() const 00055 { return myFontName; } 00056 00057 /// Get the size, in points, of this font. 00058 float getSize() const 00059 { return myFontSize; } 00060 00061 /// Render string s using the position defined by the renderer r's 00062 /// raster X, Y, and Z, set with the RE_Render::textMove* functions. 00063 /// Implicitly updates the raster position to point at the end of the 00064 /// rendered text. 00065 virtual void render(RE_Render *r, const char *s) = 0; 00066 00067 /// The descender is the distance, in pixels, from the baseline to the 00068 /// bottommost pixel of all glyphs in the font. 00069 int getDescender() 00070 { LOADFONT(); return myDescender; } 00071 00072 /// The height is the designed height, in pixels, of the font. Often 00073 /// thought to be ascender + descender, but this isn't necessarily the 00074 /// case. 00075 int getHeight() 00076 { LOADFONT(); return myFontHeight; } 00077 00078 /// Get the width, in pixels, of a particular character. 00079 /// Most often, this will just be the horizontal advance of the glyph. 00080 /// NOTE: This ignores kerning! If you want to get the width of a whole 00081 /// string, use getStringWidth instead. 00082 int getCharWidth(char c) 00083 { LOADFONT(); return getCharWidth(c, '\0'); } 00084 00085 /// Get the width, in pixels, of a particular character as laid out on the 00086 /// screen, including kerning. 00087 /// This is better thought of as answering the question "how far 00088 /// horizontally will the font renderer travel if it renders c after prev?" 00089 virtual int getCharWidth(char c, char prev) = 0; 00090 00091 /// Get the width of a string, including all kerning adjustments. 00092 virtual int getStringWidth(const char *s) = 0; 00093 00094 /// Get the number of glyphs this font. Note that it's dangerous 00095 /// to assume that the glyphs this font contains correspond to the 00096 /// numbers betwen 0 and getNumGlyphs - 1: they might not even be 00097 /// contiguous! 00098 int getNumGlyphs() 00099 { LOADFONT(); return myNumGlyphs; } 00100 00101 /// This method wraps the string src to fit into a box of width w and 00102 /// height h. It takes into account the character height and width of 00103 /// this font. The string dest contains the wrapped text (\n are 00104 /// inserted where necessary). If h is -1, then no limit is placed on 00105 /// height, otherwise your dest string may contain only the portion of 00106 /// the src string that fits in h. 00107 void wrapString(const char *src, UT_WorkBuffer &dest, 00108 int w, int h = -1); 00109 protected: // functions 00110 00111 /// A helper function to set up myFontName correctly depending on whether 00112 /// name is non-NULL (set it to name) or NULL (set it to the default). 00113 void setFontName(const char *name); 00114 00115 /// Override this to load your font and set myFontLoaded to true. 00116 virtual void loadFont() = 0; 00117 00118 protected: // variables 00119 00120 /// The name of this font face. 00121 UT_String myFontName; 00122 00123 /// The size, in points, of this font. 00124 float myFontSize; 00125 00126 /// Whether this font has been loaded yet. Set this in sub-classes on load. 00127 /// All variables below this one are not valid if myFontLoaded is false. 00128 bool myFontLoaded; 00129 00130 /// The text height of this font, in pixels. Can be thought of as the 00131 /// maximum height of the bounding box of all glyphs in this font. 00132 int myFontHeight; 00133 00134 /// The distance from the baseline to the bottom of the lowest font glyph. 00135 /// Set this in sub-classes on load. 00136 int myDescender; 00137 00138 /// The number of glyphs available in this font. Note that this doesn't 00139 /// mean that you can then render the characters between 0 and 00140 /// myNumGlyphs - 1: number of glyphs is unrelated to the characters 00141 /// they represent! 00142 int myNumGlyphs; 00143 }; 00144 #endif
1.5.9