Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / xpdf / FontCache_2.h
blob7829c6d31be144a882a2370f694c69091a35c6f5
1 //========================================================================
2 //
3 // FontCache.h
4 //
5 // Copyright 1999-2001 Emmanuel Lesueur
6 //
7 //========================================================================
9 #ifndef FONTCACHE_H
10 #define FONTCACHE_H
12 #ifdef __GNUC__
13 #pragma interface
14 #endif
16 #include "Object.h"
17 #include "GfxFont.h"
18 #include "FontEncoding.h"
20 class OutputFont;
22 enum EncodingID {
23 encUnknown, encStandard, encMacRoman, encMacExpert, encWinAnsi
26 //------------------------------------------------------------------------
27 // GfxFontCache
28 //------------------------------------------------------------------------
30 class GfxFontCache {
31 enum { cacheSize = 32 };
32 public:
34 // Constructor.
35 GfxFontCache();
37 // Destructor.
38 ~GfxFontCache() { clear(); }
40 // Clear the cache.
41 void clear();
43 // Get a font. This creates a new font if necessary.
44 GfxFont *getGfxFont(XRef *xref, Ref id1);
46 // Decrement the use counter for that font.
47 void release(GfxFont *gfxFont);
49 private:
51 struct Entry {
52 Ref id;
53 GfxFont *gfxFont;
54 int count;
57 Entry *entries; // cache entries in reverse-LRU order
58 int nValid; // number of valid entries in entries[]
59 int nTotal; // size of then entries array
63 //------------------------------------------------------------------------
64 // BaseFont
65 //------------------------------------------------------------------------
67 class BaseFont {
68 enum { cacheSize = 8 };
69 public:
71 // Constructor.
72 BaseFont(const char *filename);
74 // Destructor.
75 virtual ~BaseFont();
77 // Was font created successfully?
78 virtual GBool isOk() = 0;
80 // Returns the font name, as specified internally by the font file.
81 // Returns NULL if no name is available.
82 char *getName() { return name.getCString(); }
84 // Returns the custom font encoding, or NULL if the encoding is
85 // not available. If <taken> is set, the caller of this function
86 // will be responsible for freeing the encoding object.
87 FontEncoding *getEncoding(GBool taken);
89 // Build the map from character code to internal font index.
90 virtual void buildCharMap(FontEncoding *, short *map) = 0;
92 // Get an output font with the given transformation matrix.
93 OutputFont *get(double m11, double m12, double m21, double m22);
95 protected:
97 // Create a font with the given transformation matrix.
98 virtual OutputFont *create(double m11, double m12, double m21, double m22) = 0;
100 virtual void loadEncoding() = 0;
102 void clearCache();
103 private:
104 GBool hex; // subsetted font with hex char codes
105 // (this flag is used for text output)
106 OutputFont *
107 fonts[cacheSize]; // font cache
108 int nFonts; // number of fonts in cache.
109 protected:
110 FontEncoding *encoding;
111 GBool freeEnc;
112 GString name;
116 //------------------------------------------------------------------------
117 // T3BaseFont
118 //------------------------------------------------------------------------
120 class T3BaseFont : public BaseFont {
121 public:
123 // Constructor.
124 T3BaseFont(XRef *xrefA, Dict *fontDict);
126 // Destructor.
127 virtual ~T3BaseFont();
129 // Was font created successfully?
130 virtual GBool isOk() { return ok; }
132 // Build the map from character code to internal font index.
133 virtual void buildCharMap(FontEncoding *, short *map);
135 // Get a character glyph.
136 GBool getGlyph(int c, Object *result) {
137 if (c >= 0 && c < numGlyphs) {
138 glyphs[c].fetch(xref, result);
139 return gTrue;
140 } else {
141 result->initNull();
142 return gFalse;
146 protected:
148 // Create a font with the given transformation matrix.
149 virtual OutputFont *create(double m11, double m12, double m21, double m22) {
150 return NULL;
153 virtual void loadEncoding() {}
155 private:
156 GBool ok;
157 Dict *dict;
158 int numGlyphs;
159 Object *glyphs;
160 XRef *xref;
164 //------------------------------------------------------------------------
165 // OutputFont
166 //------------------------------------------------------------------------
168 class OutputFont {
169 public:
171 // Constructor.
172 OutputFont(BaseFont *base,
173 double m11, double m12, double m21, double m22);
175 // Does this font match the ID, size, and angle?
176 GBool matches(double m11, double m12, double m21, double m22)
177 { return fabs(tm11-m11) + fabs(tm12-m12) + fabs(tm21-m21) +
178 fabs(tm22-m22) < 0.1; }
180 // Was font created successfully?
181 virtual GBool isOk() = 0;
183 // Destructor.
184 virtual ~OutputFont();
186 // Get character metrics
187 virtual GBool getMetrics(int c,
188 double *xMin, double *yMin,
189 double *xMax, double *yMax,
190 double *advanceX, double *avanceY) =0;
192 protected:
193 BaseFont *base; // base font
194 double tm11, tm12, // original transform matrix
195 tm21, tm22;
198 //------------------------------------------------------------------------
199 // BaseFontCache
200 //------------------------------------------------------------------------
202 class BaseFontCache {
203 enum { cacheSize = 16 };
204 public:
206 // Constructor.
207 BaseFontCache();
209 // Destructor.
210 ~BaseFontCache();
212 // Get a font. This loads a new font if necessary.
213 BaseFont *getFont(XRef *xref, const char *name, GfxFont* gfxFont, EncodingID);
215 // Decrement the use count.
216 void release(BaseFont *font);
218 private:
219 struct Entry {
220 GString* name;
221 Ref id;
222 BaseFont *baseFont;
223 int count;
226 Entry *entries; // cache entries in reverse-LRU order
227 int nValid; // number of valid entries in entries[]
228 int nTotal; // size of then entries array
231 extern BaseFontCache *baseFontCache;
232 extern GfxFontCache *gfxFontCache;
234 #endif