1/*
2 * << Haru Free PDF Library >> -- hpdf_font.c
3 *
4 * URL: http://libharu.org
5 *
6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
8 *
9 * Permission to use, copy, modify, distribute and sell this software
10 * and its documentation for any purpose is hereby granted without fee,
11 * provided that the above copyright notice appear in all copies and
12 * that both that copyright notice and this permission notice appear
13 * in supporting documentation.
14 * It is provided "as is" without express or implied warranty.
15 *
16 */
17
18#include "hpdf_conf.h"
19#include "hpdf_utils.h"
20#include "hpdf.h"
21
22
23HPDF_EXPORT(HPDF_TextWidth)
24HPDF_Font_TextWidth (HPDF_Font font,
25 const HPDF_BYTE *text,
26 HPDF_UINT len)
27{
28 HPDF_TextWidth tw = {0, 0, 0, 0};
29 HPDF_FontAttr attr;
30
31 HPDF_PTRACE ((" HPDF_Font_TextWidth\n"));
32
33 if (!HPDF_Font_Validate(font))
34 return tw;
35
36 if (len > HPDF_LIMIT_MAX_STRING_LEN) {
37 HPDF_RaiseError (font->error, HPDF_STRING_OUT_OF_RANGE, 0);
38 return tw;
39 }
40
41 attr = (HPDF_FontAttr)font->attr;
42
43 if (!attr->text_width_fn) {
44 HPDF_SetError (font->error, HPDF_INVALID_OBJECT, 0);
45 return tw;
46 }
47
48 tw = attr->text_width_fn (font, text, len);
49
50 return tw;
51}
52
53
54HPDF_EXPORT(HPDF_UINT)
55HPDF_Font_MeasureText (HPDF_Font font,
56 const HPDF_BYTE *text,
57 HPDF_UINT len,
58 HPDF_REAL width,
59 HPDF_REAL font_size,
60 HPDF_REAL char_space,
61 HPDF_REAL word_space,
62 HPDF_BOOL wordwrap,
63 HPDF_REAL *real_width)
64{
65 HPDF_FontAttr attr;
66
67 HPDF_PTRACE ((" HPDF_Font_MeasureText\n"));
68
69 if (!HPDF_Font_Validate(font))
70 return 0;
71
72 if (len > HPDF_LIMIT_MAX_STRING_LEN) {
73 HPDF_RaiseError (font->error, HPDF_STRING_OUT_OF_RANGE, 0);
74 return 0;
75 }
76
77 attr = (HPDF_FontAttr)font->attr;
78
79 if (!attr->measure_text_fn) {
80 HPDF_RaiseError (font->error, HPDF_INVALID_OBJECT, 0);
81 return 0;
82 }
83
84 return attr->measure_text_fn (font, text, len, width, font_size,
85 char_space, word_space, wordwrap, real_width);
86}
87
88
89HPDF_EXPORT(const char*)
90HPDF_Font_GetFontName (HPDF_Font font)
91{
92 HPDF_FontAttr attr;
93
94 HPDF_PTRACE((" HPDF_Font_GetFontName\n"));
95
96 if (!HPDF_Font_Validate(font))
97 return NULL;
98
99 attr = (HPDF_FontAttr)font->attr;
100
101 return attr->fontdef->base_font;
102}
103
104
105HPDF_EXPORT(const char*)
106HPDF_Font_GetEncodingName (HPDF_Font font)
107{
108 HPDF_FontAttr attr;
109
110 HPDF_PTRACE((" HPDF_Font_GetEncodingName\n"));
111
112 if (!HPDF_Font_Validate(font))
113 return NULL;
114
115 attr = (HPDF_FontAttr)font->attr;
116
117 return attr->encoder->name;
118}
119
120
121HPDF_EXPORT(HPDF_INT)
122HPDF_Font_GetUnicodeWidth (HPDF_Font font,
123 HPDF_UNICODE code)
124{
125 HPDF_FontAttr attr;
126 HPDF_FontDef fontdef;
127
128 HPDF_PTRACE((" HPDF_Font_GetUnicodeWidth\n"));
129
130 if (!HPDF_Font_Validate(font))
131 return 0;
132
133 attr = (HPDF_FontAttr)font->attr;
134 fontdef = attr->fontdef;
135
136 if (fontdef->type == HPDF_FONTDEF_TYPE_TYPE1) {
137 return HPDF_Type1FontDef_GetWidth (fontdef, code);
138 } else if (fontdef->type == HPDF_FONTDEF_TYPE_TRUETYPE) {
139 return HPDF_TTFontDef_GetCharWidth (fontdef, code);
140 } else if (fontdef->type == HPDF_FONTDEF_TYPE_CID) {
141 HPDF_CMapEncoderAttr encoder_attr =
142 (HPDF_CMapEncoderAttr)attr->encoder->attr;
143 HPDF_UINT l, h;
144
145 for (l = 0; l <= 255; l++) {
146 for (h = 0; h < 255; h++) {
147 if (code == encoder_attr->unicode_map[l][h]) {
148 HPDF_UINT16 cid = encoder_attr->cid_map[l][h];
149
150 return HPDF_CIDFontDef_GetCIDWidth (fontdef, cid);
151 }
152 }
153 }
154 }
155
156 HPDF_PTRACE((" HPDF_Font_GetUnicodeWidth not found (0x%04X)\n", code));
157
158 return 0;
159}
160
161
162HPDF_EXPORT(HPDF_Box)
163HPDF_Font_GetBBox (HPDF_Font font)
164{
165 HPDF_Box bbox = {0, 0, 0, 0};
166
167 HPDF_PTRACE((" HPDF_Font_GetBBox\n"));
168 if (HPDF_Font_Validate(font))
169 return ((HPDF_FontAttr)font->attr)->fontdef->font_bbox;
170
171 return bbox;
172}
173
174HPDF_EXPORT(HPDF_INT)
175HPDF_Font_GetAscent (HPDF_Font font)
176{
177 HPDF_PTRACE((" HPDF_Font_GetAscent\n"));
178
179 if (HPDF_Font_Validate(font))
180 return ((HPDF_FontAttr)font->attr)->fontdef->ascent;
181
182 return 0;
183}
184
185HPDF_EXPORT(HPDF_INT)
186HPDF_Font_GetDescent (HPDF_Font font)
187{
188 HPDF_PTRACE((" HPDF_Font_GetDescent\n"));
189
190 if (HPDF_Font_Validate(font))
191 return ((HPDF_FontAttr)font->attr)->fontdef->descent;
192
193 return 0;
194}
195
196HPDF_EXPORT(HPDF_UINT)
197HPDF_Font_GetXHeight (HPDF_Font font)
198{
199 HPDF_PTRACE((" HPDF_Font_GetXHeight\n"));
200
201 if (HPDF_Font_Validate(font))
202 return ((HPDF_FontAttr)font->attr)->fontdef->x_height;
203
204 return 0;
205}
206
207HPDF_EXPORT(HPDF_UINT)
208HPDF_Font_GetCapHeight (HPDF_Font font)
209{
210 HPDF_PTRACE((" HPDF_Font_GetCapHeight\n"));
211
212 if (HPDF_Font_Validate(font))
213 return ((HPDF_FontAttr)font->attr)->fontdef->cap_height;
214
215 return 0;
216}
217
218
219HPDF_BOOL
220HPDF_Font_Validate (HPDF_Font font)
221{
222 HPDF_PTRACE((" HPDF_Font_Validate\n"));
223
224 if (!font || !font->attr || font->header.obj_class !=
225 (HPDF_OSUBCLASS_FONT | HPDF_OCLASS_DICT))
226 return HPDF_FALSE;
227
228 return HPDF_TRUE;
229}
230
231
232