Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pdf/xpdf/GfxFont.h
blob: 0e894d9c4541d52e886a039eee175bb6b2618f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//========================================================================
//
// GfxFont.h
//
// Copyright 1996 Derek B. Noonburg
//
//========================================================================

#ifndef GFXFONT_H
#define GFXFONT_H

#ifdef __GNUC__
#pragma interface
#endif

#include "gtypes.h"
#include "GString.h"
#include "Object.h"

class Dict;

//------------------------------------------------------------------------
// GfxFontEncoding
//------------------------------------------------------------------------

#define gfxFontEncHashSize 419

class GfxFontEncoding {
public:

  // Construct an empty encoding.
  GfxFontEncoding();

  // Construct an encoding from an array of char names.
  GfxFontEncoding(char **encoding1, int encSize);

  // Destructor.
  ~GfxFontEncoding();

  // Add a char to the encoding.
  void addChar(int code, char *name);

  // Return the character name associated with <code>.
  char *getCharName(int code) { return encoding[code]; }

  // Return the code associated with <name>.
  int getCharCode(char *name);

private:

  int hash(char *name);
  void addChar1(int code, char *name);

  char **encoding;		// code --> name mapping
  GBool freeEnc;		// should we free the encoding array?
  short				// name --> code hash table
    hashTab[gfxFontEncHashSize];
};

//------------------------------------------------------------------------
// GfxFontCharSet16
//------------------------------------------------------------------------

enum GfxFontCharSet16 {
  font16AdobeJapan12			// Adobe-Japan1-2
};

//------------------------------------------------------------------------
// GfxFontEncoding16
//------------------------------------------------------------------------

struct GfxFontEncoding16 {
  Guchar codeLen[256];		// length of codes, in bytes, indexed by
				//   first byte of code
  Gushort map1[256];		// one-byte code mapping:
				//   map1[code] --> 16-bit char selector
  Gushort *map2;		// two-byte code mapping
				//   map2[2*i]   --> first code in range
				//   map2[2*i+1] --> 16-bit char selector
				//                   for map2[2*i]
  int map2Len;			// length of map2 array (divided by 2)
};

//------------------------------------------------------------------------
// GfxFontWidths16
//------------------------------------------------------------------------

struct GfxFontWidthExcep {
  int first;			// chars <first>..<last> have
  int last;			//   width <width>
  double width;
};

struct GfxFontWidths16 {
  double defWidth;		// default char width
  GfxFontWidthExcep *exceps;	// exceptions
  int numExceps;		// number of valid entries in exceps
};

//------------------------------------------------------------------------
// GfxFont
//------------------------------------------------------------------------

#define fontFixedWidth (1 << 0)
#define fontSerif      (1 << 1)
#define fontSymbolic   (1 << 2)
#define fontItalic     (1 << 6)
#define fontBold       (1 << 18)

enum GfxFontType {
  fontUnknownType,
  fontType1,
  fontType3,
  fontTrueType,
  fontType0
};

class GfxFont {
public:

  // Constructor.
  GfxFont(char *tag1, Ref id1, Dict *fontDict);

  // Destructor.
  ~GfxFont();

  // Get font tag.
  GString *getTag() { return tag; }

  // Get font dictionary ID.
  Ref getID() { return id; }

  // Does this font match the tag?
  GBool matches(char *tag1) { return !tag->cmp(tag1); }

  // Get base font name.
  GString *getName() { return name; }

  // Get font type.
  GfxFontType getType() { return type; }

  // Does this font use 16-bit characters?
  GBool is16Bit() { return is16; }

  // Get embedded font ID, i.e., a ref for the font file stream.
  // Returns false if there is no embedded font.
  GBool getEmbeddedFontID(Ref *embID)
    { *embID = embFontID; return embFontID.num >= 0; }

  // Get the PostScript font name for the embedded font.  Returns
  // NULL if there is no embedded font.
  char *getEmbeddedFontName()
    { return embFontName ? embFontName->getCString() : (char *)NULL; }

  // Get the name of the external font file.  Returns NULL if there
  // is no external font file.
  char *getExtFontFile()
    { return extFontFile ? extFontFile->getCString() : (char *)NULL; }

  // Get font descriptor flags.
  GBool isFixedWidth() { return flags & fontFixedWidth; }
  GBool isSerif() { return flags & fontSerif; }
  GBool isSymbolic() { return flags & fontSymbolic; }
  GBool isItalic() { return flags & fontItalic; }
  GBool isBold() { return flags & fontBold; }

  // Get width of a character or string.
  double getWidth(Guchar c) { return widths[c]; }
  double getWidth(GString *s);
  double getWidth16(int c);
  double getWidth16(GString *s);

  // Return the character name associated with <code>.
  char *getCharName(int code) { return encoding->getCharName(code); }

  // Return the code associated with <name>.
  int getCharCode(char *charName) { return encoding->getCharCode(charName); }

  // Return the 16-bit character set and encoding.
  GfxFontCharSet16 getCharSet16() { return enc16.charSet; }
  GfxFontEncoding16 *getEncoding16() { return enc16.enc; }

  // Return the font matrix.
  double *getFontMatrix() { return fontMat; }

private:

  void makeEncoding(Dict *fontDict, GfxFontEncoding *builtinEncoding);
  GfxFontEncoding *makeEncoding1(Object obj, Dict *fontDesc,
				 GfxFontEncoding *builtinEncoding);
  void getType1Encoding(Stream *str);
  void makeWidths(Dict *fontDict, GfxFontEncoding *builtinEncoding,
		  Gushort *builtinWidths);
  void getType0EncAndWidths(Dict *fontDict);

  GString *tag;			// PDF font tag
  Ref id;			// reference (used as unique ID)
  GString *name;		// font name
  int flags;			// font descriptor flags
  GfxFontType type;		// type of font
  GBool is16;			// set if font uses 16-bit chars
  GString *embFontName;		// name of embedded font
  Ref embFontID;		// ref to embedded font file stream
  GString *extFontFile;		// external font file name
  double fontMat[6];		// font matrix
  union {
    GfxFontEncoding *encoding;	// 8-bit font encoding
    struct {
      GfxFontCharSet16 charSet;	// 16-bit character set
      GfxFontEncoding16 *enc;	// 16-bit encoding (CMap)
    } enc16;
  };
  union {
    double widths[256];		// width of each char for 8-bit font
    GfxFontWidths16 widths16;	// char widths for 16-bit font
  };
};

//------------------------------------------------------------------------
// GfxFontDict
//------------------------------------------------------------------------

class GfxFontDict {
public:

  // Build the font dictionary, given the PDF font dictionary.
  GfxFontDict(Dict *fontDict);

  // Destructor.
  ~GfxFontDict();

  // Get the specified font.
  GfxFont *lookup(char *tag);

  // Iterative access.
  int getNumFonts() { return numFonts; }
  GfxFont *getFont(int i) { return fonts[i]; }

private:

  GfxFont **fonts;		// list of fonts
  int numFonts;			// number of fonts
};

#endif