hpdf 2.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History +57 -0
- data/License +25 -0
- data/README +165 -0
- data/examples/arc_demo.rb +82 -0
- data/examples/demo.rb +92 -0
- data/examples/encryption.rb +41 -0
- data/examples/ext_gstater_demo.rb +171 -0
- data/examples/font_demo.rb +67 -0
- data/examples/jpfont_demo.rb +122 -0
- data/examples/line_demo.rb +301 -0
- data/examples/slide_show_demo.rb +139 -0
- data/examples/text_demo2.rb +189 -0
- data/examples/ttfont_demo.rb +106 -0
- data/ext/extconf.rb +6 -0
- data/ext/hpdf.c +3779 -0
- data/ext/hpdf_annotation.c +415 -0
- data/ext/hpdf_array.c +344 -0
- data/ext/hpdf_binary.c +117 -0
- data/ext/hpdf_boolean.c +47 -0
- data/ext/hpdf_catalog.c +354 -0
- data/ext/hpdf_destination.c +339 -0
- data/ext/hpdf_dict.c +488 -0
- data/ext/hpdf_doc.c +2020 -0
- data/ext/hpdf_doc_png.c +137 -0
- data/ext/hpdf_encoder.c +2991 -0
- data/ext/hpdf_encoder_cns.c +36359 -0
- data/ext/hpdf_encoder_cnt.c +15307 -0
- data/ext/hpdf_encoder_jp.c +16125 -0
- data/ext/hpdf_encoder_kr.c +27978 -0
- data/ext/hpdf_encrypt.c +632 -0
- data/ext/hpdf_encryptdict.c +240 -0
- data/ext/hpdf_error.c +114 -0
- data/ext/hpdf_ext_gstate.c +150 -0
- data/ext/hpdf_font.c +229 -0
- data/ext/hpdf_font_cid.c +1030 -0
- data/ext/hpdf_font_tt.c +406 -0
- data/ext/hpdf_font_type1.c +391 -0
- data/ext/hpdf_fontdef.c +56 -0
- data/ext/hpdf_fontdef_base14.c +4506 -0
- data/ext/hpdf_fontdef_cid.c +194 -0
- data/ext/hpdf_fontdef_cns.c +471 -0
- data/ext/hpdf_fontdef_cnt.c +250 -0
- data/ext/hpdf_fontdef_jp.c +1904 -0
- data/ext/hpdf_fontdef_kr.c +1572 -0
- data/ext/hpdf_fontdef_tt.c +2230 -0
- data/ext/hpdf_fontdef_type1.c +524 -0
- data/ext/hpdf_gstate.c +119 -0
- data/ext/hpdf_image.c +568 -0
- data/ext/hpdf_image_png.c +424 -0
- data/ext/hpdf_info.c +164 -0
- data/ext/hpdf_list.c +333 -0
- data/ext/hpdf_mmgr.c +243 -0
- data/ext/hpdf_name.c +71 -0
- data/ext/hpdf_null.c +31 -0
- data/ext/hpdf_number.c +49 -0
- data/ext/hpdf_objects.c +172 -0
- data/ext/hpdf_outline.c +329 -0
- data/ext/hpdf_page_label.c +74 -0
- data/ext/hpdf_page_operator.c +2764 -0
- data/ext/hpdf_pages.c +1508 -0
- data/ext/hpdf_real.c +61 -0
- data/ext/hpdf_streams.c +1435 -0
- data/ext/hpdf_string.c +189 -0
- data/ext/hpdf_utils.c +438 -0
- data/ext/hpdf_xref.c +348 -0
- data/ext/include/hpdf.h +1227 -0
- data/ext/include/hpdf_annotation.h +74 -0
- data/ext/include/hpdf_catalog.h +81 -0
- data/ext/include/hpdf_conf.h +76 -0
- data/ext/include/hpdf_consts.h +549 -0
- data/ext/include/hpdf_destination.h +41 -0
- data/ext/include/hpdf_doc.h +159 -0
- data/ext/include/hpdf_encoder.h +311 -0
- data/ext/include/hpdf_encrypt.h +156 -0
- data/ext/include/hpdf_encryptdict.h +66 -0
- data/ext/include/hpdf_error.h +201 -0
- data/ext/include/hpdf_ext_gstate.h +38 -0
- data/ext/include/hpdf_font.h +112 -0
- data/ext/include/hpdf_fontdef.h +403 -0
- data/ext/include/hpdf_gstate.h +80 -0
- data/ext/include/hpdf_image.h +72 -0
- data/ext/include/hpdf_info.h +48 -0
- data/ext/include/hpdf_list.h +85 -0
- data/ext/include/hpdf_mmgr.h +82 -0
- data/ext/include/hpdf_objects.h +587 -0
- data/ext/include/hpdf_outline.h +74 -0
- data/ext/include/hpdf_page_label.h +35 -0
- data/ext/include/hpdf_pages.h +128 -0
- data/ext/include/hpdf_streams.h +276 -0
- data/ext/include/hpdf_types.h +488 -0
- data/ext/include/hpdf_utils.h +161 -0
- data/tests/arc_demo.rb +82 -0
- data/tests/demo.rb +91 -0
- data/tests/encryption.rb +41 -0
- data/tests/ext_gstater_demo.rb +171 -0
- data/tests/font_demo.rb +67 -0
- data/tests/line_demo.rb +301 -0
- data/tests/slide_show_demo.rb +139 -0
- data/tests/test_all_examples.rb +31 -0
- data/tests/text_demo2.rb +189 -0
- metadata +147 -0
data/ext/hpdf_font.c
ADDED
@@ -0,0 +1,229 @@
|
|
1
|
+
/*
|
2
|
+
* << Haru Free PDF Library 2.0.3 >> -- hpdf_font.c
|
3
|
+
*
|
4
|
+
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
|
5
|
+
*
|
6
|
+
* Permission to use, copy, modify, distribute and sell this software
|
7
|
+
* and its documentation for any purpose is hereby granted without fee,
|
8
|
+
* provided that the above copyright notice appear in all copies and
|
9
|
+
* that both that copyright notice and this permission notice appear
|
10
|
+
* in supporting documentation.
|
11
|
+
* It is provided "as is" without express or implied warranty.
|
12
|
+
*
|
13
|
+
* 2006.07.25 modified.
|
14
|
+
*/
|
15
|
+
|
16
|
+
#include "hpdf_conf.h"
|
17
|
+
#include "hpdf_utils.h"
|
18
|
+
#include "hpdf.h"
|
19
|
+
|
20
|
+
|
21
|
+
HPDF_EXPORT(HPDF_TextWidth)
|
22
|
+
HPDF_Font_TextWidth (HPDF_Font font,
|
23
|
+
const HPDF_BYTE *text,
|
24
|
+
HPDF_UINT len)
|
25
|
+
{
|
26
|
+
HPDF_TextWidth tw = {0, 0, 0, 0};
|
27
|
+
HPDF_FontAttr attr;
|
28
|
+
|
29
|
+
HPDF_PTRACE ((" HPDF_Font_TextWidth\n"));
|
30
|
+
|
31
|
+
if (!HPDF_Font_Validate(font))
|
32
|
+
return tw;
|
33
|
+
|
34
|
+
if (len > HPDF_LIMIT_MAX_STRING_LEN) {
|
35
|
+
HPDF_RaiseError (font->error, HPDF_STRING_OUT_OF_RANGE, 0);
|
36
|
+
return tw;
|
37
|
+
}
|
38
|
+
|
39
|
+
attr = (HPDF_FontAttr)font->attr;
|
40
|
+
|
41
|
+
if (!attr->text_width_fn) {
|
42
|
+
HPDF_SetError (font->error, HPDF_INVALID_OBJECT, 0);
|
43
|
+
return tw;
|
44
|
+
}
|
45
|
+
|
46
|
+
tw = attr->text_width_fn (font, text, len);
|
47
|
+
|
48
|
+
return tw;
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
HPDF_EXPORT(HPDF_UINT)
|
53
|
+
HPDF_Font_MeasureText (HPDF_Font font,
|
54
|
+
const HPDF_BYTE *text,
|
55
|
+
HPDF_UINT len,
|
56
|
+
HPDF_REAL width,
|
57
|
+
HPDF_REAL font_size,
|
58
|
+
HPDF_REAL char_space,
|
59
|
+
HPDF_REAL word_space,
|
60
|
+
HPDF_BOOL wordwrap,
|
61
|
+
HPDF_REAL *real_width)
|
62
|
+
{
|
63
|
+
HPDF_FontAttr attr;
|
64
|
+
|
65
|
+
HPDF_PTRACE ((" HPDF_Font_MeasureText\n"));
|
66
|
+
|
67
|
+
if (!HPDF_Font_Validate(font))
|
68
|
+
return 0;
|
69
|
+
|
70
|
+
if (len > HPDF_LIMIT_MAX_STRING_LEN) {
|
71
|
+
HPDF_RaiseError (font->error, HPDF_STRING_OUT_OF_RANGE, 0);
|
72
|
+
return 0;
|
73
|
+
}
|
74
|
+
|
75
|
+
attr = (HPDF_FontAttr)font->attr;
|
76
|
+
|
77
|
+
if (!attr->measure_text_fn) {
|
78
|
+
HPDF_RaiseError (font->error, HPDF_INVALID_OBJECT, 0);
|
79
|
+
return 0;
|
80
|
+
}
|
81
|
+
|
82
|
+
return attr->measure_text_fn (font, text, len, width, font_size,
|
83
|
+
char_space, word_space, wordwrap, real_width);
|
84
|
+
}
|
85
|
+
|
86
|
+
|
87
|
+
HPDF_EXPORT(const char*)
|
88
|
+
HPDF_Font_GetFontName (HPDF_Font font)
|
89
|
+
{
|
90
|
+
HPDF_FontAttr attr;
|
91
|
+
|
92
|
+
HPDF_PTRACE((" HPDF_Font_GetFontName\n"));
|
93
|
+
|
94
|
+
if (!HPDF_Font_Validate(font))
|
95
|
+
return NULL;
|
96
|
+
|
97
|
+
attr = (HPDF_FontAttr)font->attr;
|
98
|
+
|
99
|
+
return attr->fontdef->base_font;
|
100
|
+
}
|
101
|
+
|
102
|
+
|
103
|
+
HPDF_EXPORT(const char*)
|
104
|
+
HPDF_Font_GetEncodingName (HPDF_Font font)
|
105
|
+
{
|
106
|
+
HPDF_FontAttr attr;
|
107
|
+
|
108
|
+
HPDF_PTRACE((" HPDF_Font_GetEncodingName\n"));
|
109
|
+
|
110
|
+
if (!HPDF_Font_Validate(font))
|
111
|
+
return NULL;
|
112
|
+
|
113
|
+
attr = (HPDF_FontAttr)font->attr;
|
114
|
+
|
115
|
+
return attr->encoder->name;
|
116
|
+
}
|
117
|
+
|
118
|
+
|
119
|
+
HPDF_EXPORT(HPDF_INT)
|
120
|
+
HPDF_Font_GetUnicodeWidth (HPDF_Font font,
|
121
|
+
HPDF_UNICODE code)
|
122
|
+
{
|
123
|
+
HPDF_FontAttr attr;
|
124
|
+
HPDF_FontDef fontdef;
|
125
|
+
|
126
|
+
HPDF_PTRACE((" HPDF_Font_GetUnicodeWidth\n"));
|
127
|
+
|
128
|
+
if (!HPDF_Font_Validate(font))
|
129
|
+
return 0;
|
130
|
+
|
131
|
+
attr = (HPDF_FontAttr)font->attr;
|
132
|
+
fontdef = attr->fontdef;
|
133
|
+
|
134
|
+
if (fontdef->type == HPDF_FONTDEF_TYPE_TYPE1) {
|
135
|
+
return HPDF_Type1FontDef_GetWidth (fontdef, code);
|
136
|
+
} else if (fontdef->type == HPDF_FONTDEF_TYPE_TRUETYPE) {
|
137
|
+
return HPDF_TTFontDef_GetCharWidth (fontdef, code);
|
138
|
+
} else if (fontdef->type == HPDF_FONTDEF_TYPE_CID) {
|
139
|
+
HPDF_CMapEncoderAttr encoder_attr =
|
140
|
+
(HPDF_CMapEncoderAttr)attr->encoder->attr;
|
141
|
+
HPDF_UINT l, h;
|
142
|
+
|
143
|
+
for (l = 0; l <= 255; l++) {
|
144
|
+
for (h = 0; h < 255; h++) {
|
145
|
+
if (code == encoder_attr->unicode_map[l][h]) {
|
146
|
+
HPDF_UINT16 cid = encoder_attr->cid_map[l][h];
|
147
|
+
|
148
|
+
return HPDF_CIDFontDef_GetCIDWidth (fontdef, cid);
|
149
|
+
}
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
HPDF_PTRACE((" HPDF_Font_GetUnicodeWidth not found (0x%04X)\n", code));
|
155
|
+
|
156
|
+
return 0;
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
HPDF_EXPORT(HPDF_Box)
|
161
|
+
HPDF_Font_GetBBox (HPDF_Font font)
|
162
|
+
{
|
163
|
+
HPDF_Box bbox = {0, 0, 0, 0};
|
164
|
+
|
165
|
+
HPDF_PTRACE((" HPDF_Font_GetBBox\n"));
|
166
|
+
if (HPDF_Font_Validate(font))
|
167
|
+
return ((HPDF_FontAttr)font->attr)->fontdef->font_bbox;
|
168
|
+
|
169
|
+
return bbox;
|
170
|
+
}
|
171
|
+
|
172
|
+
HPDF_EXPORT(HPDF_INT)
|
173
|
+
HPDF_Font_GetAscent (HPDF_Font font)
|
174
|
+
{
|
175
|
+
HPDF_PTRACE((" HPDF_Font_GetAscent\n"));
|
176
|
+
|
177
|
+
if (HPDF_Font_Validate(font))
|
178
|
+
return ((HPDF_FontAttr)font->attr)->fontdef->ascent;
|
179
|
+
|
180
|
+
return 0;
|
181
|
+
}
|
182
|
+
|
183
|
+
HPDF_EXPORT(HPDF_INT)
|
184
|
+
HPDF_Font_GetDescent (HPDF_Font font)
|
185
|
+
{
|
186
|
+
HPDF_PTRACE((" HPDF_Font_GetDescent\n"));
|
187
|
+
|
188
|
+
if (HPDF_Font_Validate(font))
|
189
|
+
return ((HPDF_FontAttr)font->attr)->fontdef->descent;
|
190
|
+
|
191
|
+
return 0;
|
192
|
+
}
|
193
|
+
|
194
|
+
HPDF_EXPORT(HPDF_UINT)
|
195
|
+
HPDF_Font_GetXHeight (HPDF_Font font)
|
196
|
+
{
|
197
|
+
HPDF_PTRACE((" HPDF_Font_GetXHeight\n"));
|
198
|
+
|
199
|
+
if (HPDF_Font_Validate(font))
|
200
|
+
return ((HPDF_FontAttr)font->attr)->fontdef->x_height;
|
201
|
+
|
202
|
+
return 0;
|
203
|
+
}
|
204
|
+
|
205
|
+
HPDF_EXPORT(HPDF_UINT)
|
206
|
+
HPDF_Font_GetCapHeight (HPDF_Font font)
|
207
|
+
{
|
208
|
+
HPDF_PTRACE((" HPDF_Font_GetCapHeight\n"));
|
209
|
+
|
210
|
+
if (HPDF_Font_Validate(font))
|
211
|
+
return ((HPDF_FontAttr)font->attr)->fontdef->cap_height;
|
212
|
+
|
213
|
+
return 0;
|
214
|
+
}
|
215
|
+
|
216
|
+
|
217
|
+
HPDF_BOOL
|
218
|
+
HPDF_Font_Validate (HPDF_Font font)
|
219
|
+
{
|
220
|
+
HPDF_PTRACE((" HPDF_Font_Validate\n"));
|
221
|
+
|
222
|
+
if (!font || !font->attr || font->header.obj_class !=
|
223
|
+
(HPDF_OSUBCLASS_FONT | HPDF_OCLASS_DICT))
|
224
|
+
return HPDF_FALSE;
|
225
|
+
|
226
|
+
return HPDF_TRUE;
|
227
|
+
}
|
228
|
+
|
229
|
+
|
data/ext/hpdf_font_cid.c
ADDED
@@ -0,0 +1,1030 @@
|
|
1
|
+
/*
|
2
|
+
* << Haru Free PDF Library 2.0.3 >> -- hpdf_font_cid.c
|
3
|
+
*
|
4
|
+
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
|
5
|
+
*
|
6
|
+
* Permission to use, copy, modify, distribute and sell this software
|
7
|
+
* and its documentation for any purpose is hereby granted without fee,
|
8
|
+
* provided that the above copyright notice appear in all copies and
|
9
|
+
* that both that copyright notice and this permission notice appear
|
10
|
+
* in supporting documentation.
|
11
|
+
* It is provided "as is" without express or implied warranty.
|
12
|
+
*
|
13
|
+
* 2006.08.04 modified MeasureText().
|
14
|
+
*/
|
15
|
+
|
16
|
+
#include "hpdf_conf.h"
|
17
|
+
#include "hpdf_utils.h"
|
18
|
+
#include "hpdf_font.h"
|
19
|
+
|
20
|
+
static HPDF_Font
|
21
|
+
CIDFontType0_New (HPDF_Font parent,
|
22
|
+
HPDF_Xref xref);
|
23
|
+
|
24
|
+
|
25
|
+
static HPDF_Font
|
26
|
+
CIDFontType2_New (HPDF_Font parent,
|
27
|
+
HPDF_Xref xref);
|
28
|
+
|
29
|
+
|
30
|
+
static HPDF_TextWidth
|
31
|
+
TextWidth (HPDF_Font font,
|
32
|
+
const HPDF_BYTE *text,
|
33
|
+
HPDF_UINT len);
|
34
|
+
|
35
|
+
|
36
|
+
static HPDF_UINT
|
37
|
+
MeasureText (HPDF_Font font,
|
38
|
+
const HPDF_BYTE *text,
|
39
|
+
HPDF_UINT len,
|
40
|
+
HPDF_REAL width,
|
41
|
+
HPDF_REAL font_size,
|
42
|
+
HPDF_REAL char_space,
|
43
|
+
HPDF_REAL word_space,
|
44
|
+
HPDF_BOOL wordwrap,
|
45
|
+
HPDF_REAL *real_width);
|
46
|
+
|
47
|
+
|
48
|
+
static char*
|
49
|
+
UINT16ToHex (char *s,
|
50
|
+
HPDF_UINT16 val,
|
51
|
+
char *eptr);
|
52
|
+
|
53
|
+
|
54
|
+
static HPDF_Dict
|
55
|
+
CreateCMap (HPDF_Encoder encoder,
|
56
|
+
HPDF_Xref xref);
|
57
|
+
|
58
|
+
|
59
|
+
static void
|
60
|
+
OnFree_Func (HPDF_Dict obj);
|
61
|
+
|
62
|
+
|
63
|
+
static HPDF_STATUS
|
64
|
+
CIDFontType2_BeforeWrite_Func (HPDF_Dict obj);
|
65
|
+
|
66
|
+
|
67
|
+
/*--------------------------------------------------------------------------*/
|
68
|
+
|
69
|
+
HPDF_Font
|
70
|
+
HPDF_Type0Font_New (HPDF_MMgr mmgr,
|
71
|
+
HPDF_FontDef fontdef,
|
72
|
+
HPDF_Encoder encoder,
|
73
|
+
HPDF_Xref xref)
|
74
|
+
{
|
75
|
+
HPDF_Dict font;
|
76
|
+
HPDF_FontAttr attr;
|
77
|
+
HPDF_CMapEncoderAttr encoder_attr;
|
78
|
+
HPDF_STATUS ret = 0;
|
79
|
+
HPDF_Array descendant_fonts;
|
80
|
+
|
81
|
+
HPDF_PTRACE ((" HPDF_Type0Font_New\n"));
|
82
|
+
|
83
|
+
font = HPDF_Dict_New (mmgr);
|
84
|
+
if (!font)
|
85
|
+
return NULL;
|
86
|
+
|
87
|
+
font->header.obj_class |= HPDF_OSUBCLASS_FONT;
|
88
|
+
|
89
|
+
/* check whether the fontdef object and the encoder object is valid. */
|
90
|
+
if (encoder->type != HPDF_ENCODER_TYPE_DOUBLE_BYTE) {
|
91
|
+
HPDF_SetError(font->error, HPDF_INVALID_ENCODER_TYPE, 0);
|
92
|
+
return NULL;
|
93
|
+
}
|
94
|
+
|
95
|
+
if (fontdef->type != HPDF_FONTDEF_TYPE_CID &&
|
96
|
+
fontdef->type != HPDF_FONTDEF_TYPE_TRUETYPE) {
|
97
|
+
HPDF_SetError(font->error, HPDF_INVALID_FONTDEF_TYPE, 0);
|
98
|
+
return NULL;
|
99
|
+
}
|
100
|
+
|
101
|
+
attr = HPDF_GetMem (mmgr, sizeof(HPDF_FontAttr_Rec));
|
102
|
+
if (!attr) {
|
103
|
+
HPDF_Dict_Free (font);
|
104
|
+
return NULL;
|
105
|
+
}
|
106
|
+
|
107
|
+
font->header.obj_class |= HPDF_OSUBCLASS_FONT;
|
108
|
+
font->write_fn = NULL;
|
109
|
+
font->free_fn = OnFree_Func;
|
110
|
+
font->attr = attr;
|
111
|
+
|
112
|
+
encoder_attr = (HPDF_CMapEncoderAttr)encoder->attr;
|
113
|
+
|
114
|
+
HPDF_MemSet (attr, 0, sizeof(HPDF_FontAttr_Rec));
|
115
|
+
|
116
|
+
attr->writing_mode = encoder_attr->writing_mode;
|
117
|
+
attr->text_width_fn = TextWidth;
|
118
|
+
attr->measure_text_fn = MeasureText;
|
119
|
+
attr->fontdef = fontdef;
|
120
|
+
attr->encoder = encoder;
|
121
|
+
attr->xref = xref;
|
122
|
+
|
123
|
+
if (HPDF_Xref_Add (xref, font) != HPDF_OK)
|
124
|
+
return NULL;
|
125
|
+
|
126
|
+
ret += HPDF_Dict_AddName (font, "Type", "Font");
|
127
|
+
ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font);
|
128
|
+
ret += HPDF_Dict_AddName (font, "Subtype", "Type0");
|
129
|
+
|
130
|
+
if (fontdef->type == HPDF_FONTDEF_TYPE_CID) {
|
131
|
+
ret += HPDF_Dict_AddName (font, "Encoding", encoder->name);
|
132
|
+
} else {
|
133
|
+
attr->cmap_stream = CreateCMap (encoder, xref);
|
134
|
+
|
135
|
+
if (attr->cmap_stream) {
|
136
|
+
ret += HPDF_Dict_Add (font, "Encoding", attr->cmap_stream);
|
137
|
+
} else
|
138
|
+
return NULL;
|
139
|
+
}
|
140
|
+
|
141
|
+
if (ret != HPDF_OK)
|
142
|
+
return NULL;
|
143
|
+
|
144
|
+
descendant_fonts = HPDF_Array_New (mmgr);
|
145
|
+
if (!descendant_fonts)
|
146
|
+
return NULL;
|
147
|
+
|
148
|
+
if (HPDF_Dict_Add (font, "DescendantFonts", descendant_fonts) != HPDF_OK)
|
149
|
+
return NULL;
|
150
|
+
|
151
|
+
if (fontdef->type == HPDF_FONTDEF_TYPE_CID) {
|
152
|
+
attr->descendant_font = CIDFontType0_New (font, xref);
|
153
|
+
attr->type = HPDF_FONT_TYPE0_CID;
|
154
|
+
} else {
|
155
|
+
attr->descendant_font = CIDFontType2_New (font, xref);
|
156
|
+
attr->type = HPDF_FONT_TYPE0_TT;
|
157
|
+
}
|
158
|
+
|
159
|
+
if (!attr->descendant_font)
|
160
|
+
return NULL;
|
161
|
+
else
|
162
|
+
if (HPDF_Array_Add (descendant_fonts, attr->descendant_font) !=
|
163
|
+
HPDF_OK)
|
164
|
+
return NULL;
|
165
|
+
|
166
|
+
return font;
|
167
|
+
}
|
168
|
+
|
169
|
+
static void
|
170
|
+
OnFree_Func (HPDF_Dict obj)
|
171
|
+
{
|
172
|
+
HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr;
|
173
|
+
|
174
|
+
HPDF_PTRACE ((" HPDF_Type0Font_OnFree\n"));
|
175
|
+
|
176
|
+
if (attr)
|
177
|
+
HPDF_FreeMem (obj->mmgr, attr);
|
178
|
+
}
|
179
|
+
|
180
|
+
static HPDF_Font
|
181
|
+
CIDFontType0_New (HPDF_Font parent, HPDF_Xref xref)
|
182
|
+
{
|
183
|
+
HPDF_STATUS ret = HPDF_OK;
|
184
|
+
HPDF_FontAttr attr = (HPDF_FontAttr)parent->attr;
|
185
|
+
HPDF_FontDef fontdef = attr->fontdef;
|
186
|
+
HPDF_CIDFontDefAttr fontdef_attr = (HPDF_CIDFontDefAttr)fontdef->attr;
|
187
|
+
HPDF_Encoder encoder = attr->encoder;
|
188
|
+
HPDF_CMapEncoderAttr encoder_attr =
|
189
|
+
(HPDF_CMapEncoderAttr)encoder->attr;
|
190
|
+
|
191
|
+
HPDF_UINT16 save_cid = 0;
|
192
|
+
HPDF_Font font;
|
193
|
+
HPDF_Array array;
|
194
|
+
HPDF_Array sub_array = NULL;
|
195
|
+
HPDF_UINT i;
|
196
|
+
|
197
|
+
HPDF_Dict descriptor;
|
198
|
+
HPDF_Dict cid_system_info;
|
199
|
+
|
200
|
+
HPDF_PTRACE ((" HPDF_CIDFontType0_New\n"));
|
201
|
+
|
202
|
+
font = HPDF_Dict_New (parent->mmgr);
|
203
|
+
if (!font)
|
204
|
+
return NULL;
|
205
|
+
|
206
|
+
if (HPDF_Xref_Add (xref, font) != HPDF_OK)
|
207
|
+
return NULL;
|
208
|
+
|
209
|
+
ret += HPDF_Dict_AddName (font, "Type", "Font");
|
210
|
+
ret += HPDF_Dict_AddName (font, "Subtype", "CIDFontType0");
|
211
|
+
ret += HPDF_Dict_AddNumber (font, "DW", fontdef_attr->DW);
|
212
|
+
ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font);
|
213
|
+
if (ret != HPDF_OK)
|
214
|
+
return NULL;
|
215
|
+
|
216
|
+
/* add 'DW2' element */
|
217
|
+
array = HPDF_Array_New (parent->mmgr);
|
218
|
+
if (!array)
|
219
|
+
return NULL;
|
220
|
+
|
221
|
+
if (HPDF_Dict_Add (font, "DW2", array) != HPDF_OK)
|
222
|
+
return NULL;
|
223
|
+
|
224
|
+
ret += HPDF_Array_AddNumber (array, fontdef_attr->DW2[0]);
|
225
|
+
ret += HPDF_Array_AddNumber (array, fontdef_attr->DW2[1]);
|
226
|
+
|
227
|
+
if (ret != HPDF_OK)
|
228
|
+
return NULL;
|
229
|
+
|
230
|
+
/* add 'W' element */
|
231
|
+
array = HPDF_Array_New (parent->mmgr);
|
232
|
+
if (!array)
|
233
|
+
return NULL;
|
234
|
+
|
235
|
+
if (HPDF_Dict_Add (font, "W", array) != HPDF_OK)
|
236
|
+
return NULL;
|
237
|
+
|
238
|
+
/* Create W array. */
|
239
|
+
for (i = 0; i< fontdef_attr->widths->count; i++) {
|
240
|
+
HPDF_CID_Width *w =
|
241
|
+
(HPDF_CID_Width *)HPDF_List_ItemAt (fontdef_attr->widths, i);
|
242
|
+
|
243
|
+
if (w->cid != save_cid + 1 || !sub_array) {
|
244
|
+
sub_array = HPDF_Array_New (parent->mmgr);
|
245
|
+
if (!sub_array)
|
246
|
+
return NULL;
|
247
|
+
|
248
|
+
ret += HPDF_Array_AddNumber (array, w->cid);
|
249
|
+
ret += HPDF_Array_Add (array, sub_array);
|
250
|
+
}
|
251
|
+
|
252
|
+
ret += HPDF_Array_AddNumber (sub_array, w->width);
|
253
|
+
save_cid = w->cid;
|
254
|
+
|
255
|
+
if (ret != HPDF_OK)
|
256
|
+
return NULL;
|
257
|
+
}
|
258
|
+
|
259
|
+
/* create descriptor */
|
260
|
+
descriptor = HPDF_Dict_New (parent->mmgr);
|
261
|
+
if (!descriptor)
|
262
|
+
return NULL;
|
263
|
+
|
264
|
+
if (HPDF_Xref_Add (xref, descriptor) != HPDF_OK)
|
265
|
+
return NULL;
|
266
|
+
|
267
|
+
if (HPDF_Dict_Add (font, "FontDescriptor", descriptor) != HPDF_OK)
|
268
|
+
return NULL;
|
269
|
+
|
270
|
+
ret += HPDF_Dict_AddName (descriptor, "Type", "FontDescriptor");
|
271
|
+
ret += HPDF_Dict_AddName (descriptor, "FontName", fontdef->base_font);
|
272
|
+
ret += HPDF_Dict_AddNumber (descriptor, "Ascent", fontdef->ascent);
|
273
|
+
ret += HPDF_Dict_AddNumber (descriptor, "Descent", fontdef->descent);
|
274
|
+
ret += HPDF_Dict_AddNumber (descriptor, "CapHeight",
|
275
|
+
fontdef->cap_height);
|
276
|
+
ret += HPDF_Dict_AddNumber (descriptor, "MissingWidth",
|
277
|
+
fontdef->missing_width);
|
278
|
+
ret += HPDF_Dict_AddNumber (descriptor, "Flags", fontdef->flags);
|
279
|
+
|
280
|
+
if (ret != HPDF_OK)
|
281
|
+
return NULL;
|
282
|
+
|
283
|
+
array = HPDF_Box_Array_New (parent->mmgr, fontdef->font_bbox);
|
284
|
+
if (!array)
|
285
|
+
return NULL;
|
286
|
+
|
287
|
+
ret += HPDF_Dict_Add (descriptor, "FontBBox", array);
|
288
|
+
ret += HPDF_Dict_AddNumber (descriptor, "ItalicAngle",
|
289
|
+
fontdef->italic_angle);
|
290
|
+
ret += HPDF_Dict_AddNumber (descriptor, "StemV", fontdef->stemv);
|
291
|
+
|
292
|
+
if (ret != HPDF_OK)
|
293
|
+
return NULL;
|
294
|
+
|
295
|
+
/* create CIDSystemInfo dictionary */
|
296
|
+
cid_system_info = HPDF_Dict_New (parent->mmgr);
|
297
|
+
if (!cid_system_info)
|
298
|
+
return NULL;
|
299
|
+
|
300
|
+
if (HPDF_Dict_Add (font, "CIDSystemInfo", cid_system_info) != HPDF_OK)
|
301
|
+
return NULL;
|
302
|
+
|
303
|
+
ret += HPDF_Dict_Add (cid_system_info, "Registry",
|
304
|
+
HPDF_String_New (parent->mmgr, encoder_attr->registry, NULL));
|
305
|
+
ret += HPDF_Dict_Add (cid_system_info, "Ordering",
|
306
|
+
HPDF_String_New (parent->mmgr, encoder_attr->ordering, NULL));
|
307
|
+
ret += HPDF_Dict_AddNumber (cid_system_info, "Supplement",
|
308
|
+
encoder_attr->suppliment);
|
309
|
+
|
310
|
+
if (ret != HPDF_OK)
|
311
|
+
return NULL;
|
312
|
+
|
313
|
+
return font;
|
314
|
+
}
|
315
|
+
|
316
|
+
static HPDF_Font
|
317
|
+
CIDFontType2_New (HPDF_Font parent, HPDF_Xref xref)
|
318
|
+
{
|
319
|
+
HPDF_STATUS ret = HPDF_OK;
|
320
|
+
HPDF_FontAttr attr = (HPDF_FontAttr)parent->attr;
|
321
|
+
HPDF_FontDef fontdef = attr->fontdef;
|
322
|
+
HPDF_TTFontDefAttr fontdef_attr = (HPDF_TTFontDefAttr)fontdef->attr;
|
323
|
+
HPDF_Encoder encoder = attr->encoder;
|
324
|
+
HPDF_CMapEncoderAttr encoder_attr =
|
325
|
+
(HPDF_CMapEncoderAttr)encoder->attr;
|
326
|
+
|
327
|
+
HPDF_Font font;
|
328
|
+
HPDF_Array array;
|
329
|
+
HPDF_UINT i;
|
330
|
+
HPDF_UNICODE tmp_map[65536];
|
331
|
+
HPDF_Dict cid_system_info;
|
332
|
+
|
333
|
+
HPDF_UINT16 max = 0;
|
334
|
+
|
335
|
+
HPDF_PTRACE ((" HPDF_CIDFontType2_New\n"));
|
336
|
+
|
337
|
+
font = HPDF_Dict_New (parent->mmgr);
|
338
|
+
if (!font)
|
339
|
+
return NULL;
|
340
|
+
|
341
|
+
if (HPDF_Xref_Add (xref, font) != HPDF_OK)
|
342
|
+
return NULL;
|
343
|
+
|
344
|
+
parent->before_write_fn = CIDFontType2_BeforeWrite_Func;
|
345
|
+
|
346
|
+
ret += HPDF_Dict_AddName (font, "Type", "Font");
|
347
|
+
ret += HPDF_Dict_AddName (font, "Subtype", "CIDFontType2");
|
348
|
+
ret += HPDF_Dict_AddNumber (font, "DW", fontdef->missing_width);
|
349
|
+
if (ret != HPDF_OK)
|
350
|
+
return NULL;
|
351
|
+
|
352
|
+
/* add 'DW2' element */
|
353
|
+
array = HPDF_Array_New (font->mmgr);
|
354
|
+
if (!array)
|
355
|
+
return NULL;
|
356
|
+
|
357
|
+
if (HPDF_Dict_Add (font, "DW2", array) != HPDF_OK)
|
358
|
+
return NULL;
|
359
|
+
|
360
|
+
ret += HPDF_Array_AddNumber (array, fontdef->font_bbox.bottom);
|
361
|
+
ret += HPDF_Array_AddNumber (array, fontdef->font_bbox.bottom -
|
362
|
+
fontdef->font_bbox.top);
|
363
|
+
|
364
|
+
HPDF_MemSet (tmp_map, 0, sizeof(HPDF_UNICODE) * 65536);
|
365
|
+
|
366
|
+
if (ret != HPDF_OK)
|
367
|
+
return NULL;
|
368
|
+
|
369
|
+
for (i = 0; i < 256; i++) {
|
370
|
+
HPDF_UINT j;
|
371
|
+
|
372
|
+
for (j = 0; j < 256; j++) {
|
373
|
+
HPDF_UINT16 cid = encoder_attr->cid_map[i][j];
|
374
|
+
if (cid != 0) {
|
375
|
+
HPDF_UNICODE unicode = encoder_attr->unicode_map[i][j];
|
376
|
+
HPDF_UINT16 gid = HPDF_TTFontDef_GetGlyphid (fontdef, unicode);
|
377
|
+
tmp_map[cid] = gid;
|
378
|
+
if (max < cid)
|
379
|
+
max = cid;
|
380
|
+
}
|
381
|
+
}
|
382
|
+
}
|
383
|
+
|
384
|
+
if (max > 0) {
|
385
|
+
HPDF_INT16 dw = fontdef->missing_width;
|
386
|
+
HPDF_UNICODE *ptmp_map = tmp_map;
|
387
|
+
HPDF_Array tmp_array = NULL;
|
388
|
+
|
389
|
+
/* add 'W' element */
|
390
|
+
array = HPDF_Array_New (font->mmgr);
|
391
|
+
if (!array)
|
392
|
+
return NULL;
|
393
|
+
|
394
|
+
if (HPDF_Dict_Add (font, "W", array) != HPDF_OK)
|
395
|
+
return NULL;
|
396
|
+
|
397
|
+
for (i = 0; i < max; i++, ptmp_map++) {
|
398
|
+
HPDF_INT w = HPDF_TTFontDef_GetGidWidth (fontdef, *ptmp_map);
|
399
|
+
|
400
|
+
if (w != dw) {
|
401
|
+
if (!tmp_array) {
|
402
|
+
if ((ret = HPDF_Array_AddNumber (array, i)) != HPDF_OK)
|
403
|
+
return NULL;
|
404
|
+
|
405
|
+
tmp_array = HPDF_Array_New (font->mmgr);
|
406
|
+
if (!tmp_array)
|
407
|
+
return NULL;
|
408
|
+
|
409
|
+
if ((ret = HPDF_Array_Add (array, tmp_array)) != HPDF_OK)
|
410
|
+
return NULL;
|
411
|
+
}
|
412
|
+
|
413
|
+
if ((ret = HPDF_Array_AddNumber (tmp_array, w)) != HPDF_OK)
|
414
|
+
return NULL;
|
415
|
+
} else
|
416
|
+
tmp_array = NULL;
|
417
|
+
}
|
418
|
+
|
419
|
+
/* create "CIDToGIDMap" data */
|
420
|
+
if (fontdef_attr->embedding) {
|
421
|
+
attr->map_stream = HPDF_DictStream_New (font->mmgr, xref);
|
422
|
+
if (!attr->map_stream)
|
423
|
+
return NULL;
|
424
|
+
|
425
|
+
if ((ret = HPDF_Dict_Add (font, "CIDToGIDMap", attr->map_stream))
|
426
|
+
!= HPDF_OK)
|
427
|
+
return NULL;
|
428
|
+
|
429
|
+
for (i = 0; i < max; i++) {
|
430
|
+
HPDF_BYTE u[2];
|
431
|
+
HPDF_UINT16 gid = tmp_map[i];
|
432
|
+
|
433
|
+
u[0] = gid >> 8;
|
434
|
+
u[1] = gid;
|
435
|
+
|
436
|
+
HPDF_MemCpy ((HPDF_BYTE *)(tmp_map + i), u, 2);
|
437
|
+
}
|
438
|
+
|
439
|
+
if ((ret = HPDF_Stream_Write (attr->map_stream->stream,
|
440
|
+
(HPDF_BYTE *)tmp_map, max * 2)) != HPDF_OK)
|
441
|
+
return NULL;
|
442
|
+
}
|
443
|
+
} else {
|
444
|
+
HPDF_SetError (font->error, HPDF_INVALID_FONTDEF_DATA, 0);
|
445
|
+
return 0;
|
446
|
+
}
|
447
|
+
|
448
|
+
/* create CIDSystemInfo dictionary */
|
449
|
+
cid_system_info = HPDF_Dict_New (parent->mmgr);
|
450
|
+
if (!cid_system_info)
|
451
|
+
return NULL;
|
452
|
+
|
453
|
+
if (HPDF_Dict_Add (font, "CIDSystemInfo", cid_system_info) != HPDF_OK)
|
454
|
+
return NULL;
|
455
|
+
|
456
|
+
ret += HPDF_Dict_Add (cid_system_info, "Registry",
|
457
|
+
HPDF_String_New (parent->mmgr, encoder_attr->registry, NULL));
|
458
|
+
ret += HPDF_Dict_Add (cid_system_info, "Ordering",
|
459
|
+
HPDF_String_New (parent->mmgr, encoder_attr->ordering, NULL));
|
460
|
+
ret += HPDF_Dict_AddNumber (cid_system_info, "Supplement",
|
461
|
+
encoder_attr->suppliment);
|
462
|
+
|
463
|
+
if (ret != HPDF_OK)
|
464
|
+
return NULL;
|
465
|
+
|
466
|
+
return font;
|
467
|
+
}
|
468
|
+
|
469
|
+
|
470
|
+
static HPDF_STATUS
|
471
|
+
CIDFontType2_BeforeWrite_Func (HPDF_Dict obj)
|
472
|
+
{
|
473
|
+
HPDF_FontAttr font_attr = (HPDF_FontAttr)obj->attr;
|
474
|
+
HPDF_FontDef def = font_attr->fontdef;
|
475
|
+
HPDF_TTFontDefAttr def_attr = (HPDF_TTFontDefAttr)def->attr;
|
476
|
+
HPDF_STATUS ret = 0;
|
477
|
+
|
478
|
+
HPDF_PTRACE ((" CIDFontType2_BeforeWrite_Func\n"));
|
479
|
+
|
480
|
+
if (font_attr->map_stream)
|
481
|
+
font_attr->map_stream->filter = obj->filter;
|
482
|
+
|
483
|
+
if (font_attr->cmap_stream)
|
484
|
+
font_attr->cmap_stream->filter = obj->filter;
|
485
|
+
|
486
|
+
if (!font_attr->fontdef->descriptor) {
|
487
|
+
HPDF_Dict descriptor = HPDF_Dict_New (obj->mmgr);
|
488
|
+
HPDF_Array array;
|
489
|
+
|
490
|
+
if (!descriptor)
|
491
|
+
return HPDF_Error_GetCode (obj->error);
|
492
|
+
|
493
|
+
if (def_attr->embedding) {
|
494
|
+
HPDF_Dict font_data = HPDF_DictStream_New (obj->mmgr,
|
495
|
+
font_attr->xref);
|
496
|
+
|
497
|
+
if (!font_data)
|
498
|
+
return HPDF_Error_GetCode (obj->error);
|
499
|
+
|
500
|
+
if (HPDF_TTFontDef_SaveFontData (font_attr->fontdef,
|
501
|
+
font_data->stream) != HPDF_OK)
|
502
|
+
return HPDF_Error_GetCode (obj->error);
|
503
|
+
|
504
|
+
ret += HPDF_Dict_Add (descriptor, "FontFile2", font_data);
|
505
|
+
ret += HPDF_Dict_AddNumber (font_data, "Length1",
|
506
|
+
def_attr->length1);
|
507
|
+
ret += HPDF_Dict_AddNumber (font_data, "Length2", 0);
|
508
|
+
ret += HPDF_Dict_AddNumber (font_data, "Length3", 0);
|
509
|
+
|
510
|
+
font_data->filter = obj->filter;
|
511
|
+
|
512
|
+
if (ret != HPDF_OK)
|
513
|
+
return HPDF_Error_GetCode (obj->error);
|
514
|
+
}
|
515
|
+
|
516
|
+
ret += HPDF_Xref_Add (font_attr->xref, descriptor);
|
517
|
+
ret += HPDF_Dict_AddName (descriptor, "Type", "FontDescriptor");
|
518
|
+
ret += HPDF_Dict_AddNumber (descriptor, "Ascent", def->ascent);
|
519
|
+
ret += HPDF_Dict_AddNumber (descriptor, "Descent", def->descent);
|
520
|
+
ret += HPDF_Dict_AddNumber (descriptor, "Flags", def->flags);
|
521
|
+
|
522
|
+
array = HPDF_Box_Array_New (obj->mmgr, def->font_bbox);
|
523
|
+
ret += HPDF_Dict_Add (descriptor, "FontBBox", array);
|
524
|
+
|
525
|
+
ret += HPDF_Dict_AddName (descriptor, "FontName", def_attr->base_font);
|
526
|
+
ret += HPDF_Dict_AddNumber (descriptor, "ItalicAngle",
|
527
|
+
def->italic_angle);
|
528
|
+
ret += HPDF_Dict_AddNumber (descriptor, "StemV", def->stemv);
|
529
|
+
ret += HPDF_Dict_AddNumber (descriptor, "XHeight", def->x_height);
|
530
|
+
|
531
|
+
if (ret != HPDF_OK)
|
532
|
+
return HPDF_Error_GetCode (obj->error);
|
533
|
+
|
534
|
+
font_attr->fontdef->descriptor = descriptor;
|
535
|
+
}
|
536
|
+
|
537
|
+
if ((ret = HPDF_Dict_AddName (obj, "BaseFont",
|
538
|
+
def_attr->base_font)) != HPDF_OK)
|
539
|
+
return ret;
|
540
|
+
|
541
|
+
if ((ret = HPDF_Dict_AddName (font_attr->descendant_font, "BaseFont",
|
542
|
+
def_attr->base_font)) != HPDF_OK)
|
543
|
+
return ret;
|
544
|
+
|
545
|
+
return HPDF_Dict_Add (font_attr->descendant_font, "FontDescriptor",
|
546
|
+
font_attr->fontdef->descriptor);
|
547
|
+
}
|
548
|
+
|
549
|
+
|
550
|
+
static HPDF_TextWidth
|
551
|
+
TextWidth (HPDF_Font font,
|
552
|
+
const HPDF_BYTE *text,
|
553
|
+
HPDF_UINT len)
|
554
|
+
{
|
555
|
+
HPDF_TextWidth tw = {0, 0, 0, 0};
|
556
|
+
HPDF_FontAttr attr = (HPDF_FontAttr)font->attr;
|
557
|
+
HPDF_ParseText_Rec parse_state;
|
558
|
+
HPDF_Encoder encoder = attr->encoder;
|
559
|
+
HPDF_UINT i = 0;
|
560
|
+
HPDF_INT dw2;
|
561
|
+
HPDF_BYTE b = 0;
|
562
|
+
|
563
|
+
HPDF_PTRACE ((" HPDF_Type0Font_TextWidth\n"));
|
564
|
+
|
565
|
+
if (attr->fontdef->type == HPDF_FONTDEF_TYPE_CID) {
|
566
|
+
HPDF_CIDFontDefAttr cid_fontdef_attr =
|
567
|
+
(HPDF_CIDFontDefAttr)attr->fontdef->attr;
|
568
|
+
dw2 = cid_fontdef_attr->DW2[1];
|
569
|
+
} else {
|
570
|
+
dw2 = attr->fontdef->font_bbox.bottom -
|
571
|
+
attr->fontdef->font_bbox.top;
|
572
|
+
}
|
573
|
+
|
574
|
+
HPDF_Encoder_SetParseText (encoder, &parse_state, text, len);
|
575
|
+
|
576
|
+
while (i < len) {
|
577
|
+
HPDF_ByteType btype = HPDF_CMapEncoder_ByteType (encoder, &parse_state);
|
578
|
+
HPDF_UINT16 cid;
|
579
|
+
HPDF_UNICODE unicode;
|
580
|
+
HPDF_UINT16 code;
|
581
|
+
HPDF_UINT w = 0;
|
582
|
+
|
583
|
+
b = *text++;
|
584
|
+
code = b;
|
585
|
+
|
586
|
+
if (btype == HPDF_BYTE_TYPE_LEAD) {
|
587
|
+
code <<= 8;
|
588
|
+
code += *text;
|
589
|
+
}
|
590
|
+
|
591
|
+
if (btype != HPDF_BYTE_TYPE_TRIAL) {
|
592
|
+
if (attr->writing_mode == HPDF_WMODE_HORIZONTAL) {
|
593
|
+
if (attr->fontdef->type == HPDF_FONTDEF_TYPE_CID) {
|
594
|
+
/* cid-based font */
|
595
|
+
cid = HPDF_CMapEncoder_ToCID (encoder, code);
|
596
|
+
w = HPDF_CIDFontDef_GetCIDWidth (attr->fontdef, cid);
|
597
|
+
} else {
|
598
|
+
/* unicode-based font */
|
599
|
+
unicode = HPDF_CMapEncoder_ToUnicode (encoder, code);
|
600
|
+
w = HPDF_TTFontDef_GetCharWidth (attr->fontdef, unicode);
|
601
|
+
}
|
602
|
+
} else {
|
603
|
+
w = -dw2;
|
604
|
+
}
|
605
|
+
|
606
|
+
tw.numchars++;
|
607
|
+
}
|
608
|
+
|
609
|
+
if (HPDF_IS_WHITE_SPACE(code)) {
|
610
|
+
tw.numwords++;
|
611
|
+
tw.numspace++;
|
612
|
+
}
|
613
|
+
|
614
|
+
tw.width += w;
|
615
|
+
i++;
|
616
|
+
}
|
617
|
+
|
618
|
+
/* 2006.08.19 add. */
|
619
|
+
if (HPDF_IS_WHITE_SPACE(b))
|
620
|
+
; /* do nothing. */
|
621
|
+
else
|
622
|
+
tw.numwords++;
|
623
|
+
|
624
|
+
return tw;
|
625
|
+
}
|
626
|
+
|
627
|
+
|
628
|
+
static HPDF_UINT
|
629
|
+
MeasureText (HPDF_Font font,
|
630
|
+
const HPDF_BYTE *text,
|
631
|
+
HPDF_UINT len,
|
632
|
+
HPDF_REAL width,
|
633
|
+
HPDF_REAL font_size,
|
634
|
+
HPDF_REAL char_space,
|
635
|
+
HPDF_REAL word_space,
|
636
|
+
HPDF_BOOL wordwrap,
|
637
|
+
HPDF_REAL *real_width)
|
638
|
+
{
|
639
|
+
HPDF_REAL w = 0;
|
640
|
+
HPDF_UINT tmp_len = 0;
|
641
|
+
HPDF_UINT i;
|
642
|
+
HPDF_FontAttr attr = (HPDF_FontAttr)font->attr;
|
643
|
+
HPDF_ByteType last_btype = HPDF_BYTE_TYPE_TRIAL;
|
644
|
+
HPDF_Encoder encoder = attr->encoder;
|
645
|
+
HPDF_ParseText_Rec parse_state;
|
646
|
+
HPDF_INT dw2;
|
647
|
+
|
648
|
+
HPDF_PTRACE ((" HPDF_Type0Font_MeasureText\n"));
|
649
|
+
|
650
|
+
if (attr->fontdef->type == HPDF_FONTDEF_TYPE_CID) {
|
651
|
+
HPDF_CIDFontDefAttr cid_fontdef_attr =
|
652
|
+
(HPDF_CIDFontDefAttr)attr->fontdef->attr;
|
653
|
+
dw2 = cid_fontdef_attr->DW2[1];
|
654
|
+
} else {
|
655
|
+
dw2 = attr->fontdef->font_bbox.bottom -
|
656
|
+
attr->fontdef->font_bbox.top;
|
657
|
+
}
|
658
|
+
|
659
|
+
HPDF_Encoder_SetParseText (encoder, &parse_state, text, len);
|
660
|
+
|
661
|
+
for (i = 0; i < len; i++) {
|
662
|
+
HPDF_BYTE b = *text++;
|
663
|
+
HPDF_BYTE b2 = *text; /* next byte */
|
664
|
+
HPDF_ByteType btype = HPDF_Encoder_ByteType (encoder, &parse_state);
|
665
|
+
HPDF_UNICODE unicode;
|
666
|
+
HPDF_UINT16 code = b;
|
667
|
+
HPDF_UINT16 tmp_w = 0;
|
668
|
+
|
669
|
+
if (btype == HPDF_BYTE_TYPE_LEAD) {
|
670
|
+
code <<= 8;
|
671
|
+
code += b2;
|
672
|
+
}
|
673
|
+
|
674
|
+
if (!wordwrap) {
|
675
|
+
if (HPDF_IS_WHITE_SPACE(b)) {
|
676
|
+
tmp_len = i + 1;
|
677
|
+
if (real_width)
|
678
|
+
*real_width = w;
|
679
|
+
} else if (btype == HPDF_BYTE_TYPE_SINGLE ||
|
680
|
+
btype == HPDF_BYTE_TYPE_LEAD) {
|
681
|
+
tmp_len = i;
|
682
|
+
if (real_width)
|
683
|
+
*real_width = w;
|
684
|
+
}
|
685
|
+
} else {
|
686
|
+
if (HPDF_IS_WHITE_SPACE(b)) {
|
687
|
+
tmp_len = i + 1;
|
688
|
+
if (real_width)
|
689
|
+
*real_width = w;
|
690
|
+
} else
|
691
|
+
if (last_btype == HPDF_BYTE_TYPE_TRIAL ||
|
692
|
+
(btype == HPDF_BYTE_TYPE_LEAD &&
|
693
|
+
last_btype == HPDF_BYTE_TYPE_SINGLE)) {
|
694
|
+
if (!HPDF_Encoder_CheckJWWLineHead(encoder, code)) {
|
695
|
+
tmp_len = i;
|
696
|
+
if (real_width)
|
697
|
+
*real_width = w;
|
698
|
+
}
|
699
|
+
}
|
700
|
+
}
|
701
|
+
|
702
|
+
if (HPDF_IS_WHITE_SPACE(b)) {
|
703
|
+
w += word_space;
|
704
|
+
}
|
705
|
+
|
706
|
+
if (btype != HPDF_BYTE_TYPE_TRIAL) {
|
707
|
+
if (attr->writing_mode == HPDF_WMODE_HORIZONTAL) {
|
708
|
+
if (attr->fontdef->type == HPDF_FONTDEF_TYPE_CID) {
|
709
|
+
/* cid-based font */
|
710
|
+
HPDF_UINT16 cid = HPDF_CMapEncoder_ToCID (encoder, code);
|
711
|
+
tmp_w = HPDF_CIDFontDef_GetCIDWidth (attr->fontdef, cid);
|
712
|
+
} else {
|
713
|
+
/* unicode-based font */
|
714
|
+
unicode = HPDF_CMapEncoder_ToUnicode (encoder, code);
|
715
|
+
tmp_w = HPDF_TTFontDef_GetCharWidth (attr->fontdef,
|
716
|
+
unicode);
|
717
|
+
}
|
718
|
+
} else {
|
719
|
+
tmp_w = -dw2;
|
720
|
+
}
|
721
|
+
|
722
|
+
if (i > 0)
|
723
|
+
w += char_space;
|
724
|
+
}
|
725
|
+
|
726
|
+
w += (HPDF_DOUBLE)tmp_w * font_size / 1000;
|
727
|
+
|
728
|
+
/* 2006.08.04 break when it encountered line feed */
|
729
|
+
if (w > width || b == 0x0A)
|
730
|
+
return tmp_len;
|
731
|
+
|
732
|
+
if (HPDF_IS_WHITE_SPACE(b))
|
733
|
+
last_btype = HPDF_BYTE_TYPE_TRIAL;
|
734
|
+
else
|
735
|
+
last_btype = btype;
|
736
|
+
}
|
737
|
+
|
738
|
+
/* all of text can be put in the specified width */
|
739
|
+
if (real_width)
|
740
|
+
*real_width = w;
|
741
|
+
|
742
|
+
return len;
|
743
|
+
}
|
744
|
+
|
745
|
+
|
746
|
+
static char*
|
747
|
+
UINT16ToHex (char *s,
|
748
|
+
HPDF_UINT16 val,
|
749
|
+
char *eptr)
|
750
|
+
{
|
751
|
+
HPDF_BYTE b[2];
|
752
|
+
HPDF_UINT16 val2;
|
753
|
+
char c;
|
754
|
+
|
755
|
+
if (eptr - s < 7)
|
756
|
+
return s;
|
757
|
+
|
758
|
+
/* align byte-order */
|
759
|
+
HPDF_MemCpy (b, (HPDF_BYTE *)&val, 2);
|
760
|
+
val2 = (HPDF_UINT16)((HPDF_UINT16)b[0] << 8 | (HPDF_UINT16)b[1]);
|
761
|
+
|
762
|
+
HPDF_MemCpy (b, (HPDF_BYTE *)&val2, 2);
|
763
|
+
|
764
|
+
*s++ = '<';
|
765
|
+
|
766
|
+
if (b[0] != 0) {
|
767
|
+
c = b[0] >> 4;
|
768
|
+
if (c <= 9)
|
769
|
+
c += 0x30;
|
770
|
+
else
|
771
|
+
c += 0x41 - 10;
|
772
|
+
*s++ = c;
|
773
|
+
|
774
|
+
c = b[0] & 0x0f;
|
775
|
+
if (c <= 9)
|
776
|
+
c += 0x30;
|
777
|
+
else
|
778
|
+
c += 0x41 - 10;
|
779
|
+
*s++ = c;
|
780
|
+
}
|
781
|
+
|
782
|
+
c = b[1] >> 4;
|
783
|
+
if (c <= 9)
|
784
|
+
c += 0x30;
|
785
|
+
else
|
786
|
+
c += 0x41 - 10;
|
787
|
+
*s++ = c;
|
788
|
+
|
789
|
+
c = b[1] & 0x0f;
|
790
|
+
if (c <= 9)
|
791
|
+
c += 0x30;
|
792
|
+
else
|
793
|
+
c += 0x41 - 10;
|
794
|
+
*s++ = c;
|
795
|
+
|
796
|
+
*s++ = '>';
|
797
|
+
*s = 0;
|
798
|
+
|
799
|
+
return s;
|
800
|
+
}
|
801
|
+
|
802
|
+
|
803
|
+
static HPDF_Dict
|
804
|
+
CreateCMap (HPDF_Encoder encoder,
|
805
|
+
HPDF_Xref xref)
|
806
|
+
{
|
807
|
+
HPDF_STATUS ret = HPDF_OK;
|
808
|
+
HPDF_Dict cmap = HPDF_DictStream_New (encoder->mmgr, xref);
|
809
|
+
HPDF_CMapEncoderAttr attr = (HPDF_CMapEncoderAttr)encoder->attr;
|
810
|
+
char buf[HPDF_TMP_BUF_SIZ];
|
811
|
+
char *pbuf;
|
812
|
+
char *eptr = buf + HPDF_TMP_BUF_SIZ - 1;
|
813
|
+
HPDF_UINT i;
|
814
|
+
HPDF_UINT phase, odd;
|
815
|
+
HPDF_Dict sysinfo;
|
816
|
+
|
817
|
+
if (!cmap)
|
818
|
+
return NULL;
|
819
|
+
|
820
|
+
ret += HPDF_Dict_AddName (cmap, "Type", "CMap");
|
821
|
+
ret += HPDF_Dict_AddName (cmap, "CMapName", encoder->name);
|
822
|
+
|
823
|
+
sysinfo = HPDF_Dict_New (encoder->mmgr);
|
824
|
+
if (!sysinfo)
|
825
|
+
return NULL;
|
826
|
+
|
827
|
+
if (HPDF_Dict_Add (cmap, "CIDSystemInfo", sysinfo) != HPDF_OK)
|
828
|
+
return NULL;
|
829
|
+
|
830
|
+
ret += HPDF_Dict_Add (sysinfo, "Registry", HPDF_String_New (encoder->mmgr,
|
831
|
+
attr->registry, NULL));
|
832
|
+
ret += HPDF_Dict_Add (sysinfo, "Ordering", HPDF_String_New (encoder->mmgr,
|
833
|
+
attr->ordering, NULL));
|
834
|
+
ret += HPDF_Dict_AddNumber (sysinfo, "Supplement", attr->suppliment);
|
835
|
+
ret += HPDF_Dict_AddNumber (cmap, "WMode",
|
836
|
+
(HPDF_UINT32)attr->writing_mode);
|
837
|
+
|
838
|
+
/* create cmap data from encoding data */
|
839
|
+
ret += HPDF_Stream_WriteStr (cmap->stream,
|
840
|
+
"%!PS-Adobe-3.0 Resource-CMap\r\n");
|
841
|
+
ret += HPDF_Stream_WriteStr (cmap->stream,
|
842
|
+
"%%DocumentNeededResources: ProcSet (CIDInit)\r\n");
|
843
|
+
ret += HPDF_Stream_WriteStr (cmap->stream,
|
844
|
+
"%%IncludeResource: ProcSet (CIDInit)\r\n");
|
845
|
+
|
846
|
+
pbuf = HPDF_StrCpy (buf, "%%BeginResource: CMap (", eptr);
|
847
|
+
pbuf = HPDF_StrCpy (pbuf, encoder->name, eptr);
|
848
|
+
HPDF_StrCpy (pbuf, ")\r\n", eptr);
|
849
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
850
|
+
|
851
|
+
pbuf = HPDF_StrCpy (buf, "%%Title: (", eptr);
|
852
|
+
pbuf = HPDF_StrCpy (pbuf, encoder->name, eptr);
|
853
|
+
*pbuf++ = ' ';
|
854
|
+
pbuf = HPDF_StrCpy (pbuf, attr->registry, eptr);
|
855
|
+
*pbuf++ = ' ';
|
856
|
+
pbuf = HPDF_StrCpy (pbuf, attr->ordering, eptr);
|
857
|
+
*pbuf++ = ' ';
|
858
|
+
pbuf = HPDF_IToA (pbuf, attr->suppliment, eptr);
|
859
|
+
HPDF_StrCpy (pbuf, ")\r\n", eptr);
|
860
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
861
|
+
|
862
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, "%%Version: 1.0\r\n");
|
863
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, "%%EndComments\r\n");
|
864
|
+
|
865
|
+
ret += HPDF_Stream_WriteStr (cmap->stream,
|
866
|
+
"/CIDInit /ProcSet findresource begin\r\n\r\n");
|
867
|
+
|
868
|
+
/* Adobe CMap and CIDFont Files Specification recommends to allocate
|
869
|
+
* five more elements to this dictionary than existing elements.
|
870
|
+
*/
|
871
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, "12 dict begin\r\n\r\n");
|
872
|
+
|
873
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, "begincmap\r\n\r\n");
|
874
|
+
ret += HPDF_Stream_WriteStr (cmap->stream,
|
875
|
+
"/CIDSystemInfo 3 dict dup begin\r\n");
|
876
|
+
|
877
|
+
pbuf = HPDF_StrCpy (buf, " /Registry (", eptr);
|
878
|
+
pbuf = HPDF_StrCpy (pbuf, attr->registry, eptr);
|
879
|
+
HPDF_StrCpy (pbuf, ") def\r\n", eptr);
|
880
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
881
|
+
|
882
|
+
pbuf = HPDF_StrCpy (buf, " /Ordering (", eptr);
|
883
|
+
pbuf = HPDF_StrCpy (pbuf, attr->ordering, eptr);
|
884
|
+
HPDF_StrCpy (pbuf, ") def\r\n", eptr);
|
885
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
886
|
+
|
887
|
+
pbuf = HPDF_StrCpy (buf, " /Supplement ", eptr);
|
888
|
+
pbuf = HPDF_IToA (pbuf, attr->suppliment, eptr);
|
889
|
+
pbuf = HPDF_StrCpy (pbuf, " def\r\n", eptr);
|
890
|
+
HPDF_StrCpy (pbuf, "end def\r\n\r\n", eptr);
|
891
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
892
|
+
|
893
|
+
pbuf = HPDF_StrCpy (buf, "/CMapName /", eptr);
|
894
|
+
pbuf = HPDF_StrCpy (pbuf, encoder->name, eptr);
|
895
|
+
HPDF_StrCpy (pbuf, " def\r\n", eptr);
|
896
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
897
|
+
|
898
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, "/CMapVersion 1.0 def\r\n");
|
899
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, "/CMapType 1 def\r\n\r\n");
|
900
|
+
|
901
|
+
if (attr->uid_offset >= 0) {
|
902
|
+
pbuf = HPDF_StrCpy (buf, "/UIDOffset ", eptr);
|
903
|
+
pbuf = HPDF_IToA (pbuf, attr->uid_offset, eptr);
|
904
|
+
HPDF_StrCpy (pbuf, " def\r\n\r\n", eptr);
|
905
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
906
|
+
}
|
907
|
+
|
908
|
+
pbuf = HPDF_StrCpy (buf, "/XUID [", eptr);
|
909
|
+
pbuf = HPDF_IToA (pbuf, attr->xuid[0], eptr);
|
910
|
+
*pbuf++ = ' ';
|
911
|
+
pbuf = HPDF_IToA (pbuf, attr->xuid[1], eptr);
|
912
|
+
*pbuf++ = ' ';
|
913
|
+
pbuf = HPDF_IToA (pbuf, attr->xuid[2], eptr);
|
914
|
+
HPDF_StrCpy (pbuf, "] def\r\n\r\n", eptr);
|
915
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
916
|
+
|
917
|
+
pbuf = HPDF_StrCpy (buf, "/WMode ", eptr);
|
918
|
+
pbuf = HPDF_IToA (pbuf, (HPDF_UINT32)attr->writing_mode, eptr);
|
919
|
+
HPDF_StrCpy (pbuf, " def\r\n\r\n", eptr);
|
920
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
921
|
+
|
922
|
+
/* add code-space-range */
|
923
|
+
pbuf = HPDF_IToA (buf, attr->code_space_range->count, eptr);
|
924
|
+
HPDF_StrCpy (pbuf, " begincodespacerange\r\n", eptr);
|
925
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
926
|
+
|
927
|
+
for (i = 0; i < attr->code_space_range->count; i++) {
|
928
|
+
HPDF_CidRange_Rec *range = HPDF_List_ItemAt (attr->code_space_range,
|
929
|
+
i);
|
930
|
+
|
931
|
+
pbuf = UINT16ToHex (buf, range->from, eptr);
|
932
|
+
*pbuf++ = ' ';
|
933
|
+
pbuf = UINT16ToHex (pbuf, range->to, eptr);
|
934
|
+
HPDF_StrCpy (pbuf, "\r\n", eptr);
|
935
|
+
|
936
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
937
|
+
|
938
|
+
if (ret != HPDF_OK)
|
939
|
+
return NULL;
|
940
|
+
}
|
941
|
+
|
942
|
+
HPDF_StrCpy (buf, "endcodespacerange\r\n\r\n", eptr);
|
943
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
944
|
+
if (ret != HPDF_OK)
|
945
|
+
return NULL;
|
946
|
+
|
947
|
+
/* add not-def-range */
|
948
|
+
pbuf = HPDF_IToA (buf, attr->notdef_range->count, eptr);
|
949
|
+
HPDF_StrCpy (pbuf, " beginnotdefrange\r\n", eptr);
|
950
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
951
|
+
|
952
|
+
for (i = 0; i < attr->notdef_range->count; i++) {
|
953
|
+
HPDF_CidRange_Rec *range = HPDF_List_ItemAt (attr->notdef_range, i);
|
954
|
+
|
955
|
+
pbuf = UINT16ToHex (buf, range->from, eptr);
|
956
|
+
*pbuf++ = ' ';
|
957
|
+
pbuf = UINT16ToHex (pbuf, range->to, eptr);
|
958
|
+
*pbuf++ = ' ';
|
959
|
+
pbuf = HPDF_IToA (pbuf, range->cid, eptr);
|
960
|
+
HPDF_StrCpy (pbuf, "\r\n", eptr);
|
961
|
+
|
962
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
963
|
+
|
964
|
+
if (ret != HPDF_OK)
|
965
|
+
return NULL;
|
966
|
+
}
|
967
|
+
|
968
|
+
HPDF_StrCpy (buf, "endnotdefrange\r\n\r\n", eptr);
|
969
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
970
|
+
if (ret != HPDF_OK)
|
971
|
+
return NULL;
|
972
|
+
|
973
|
+
/* add cid-range */
|
974
|
+
phase = attr->cmap_range->count / 100;
|
975
|
+
odd = attr->cmap_range->count % 100;
|
976
|
+
if (phase > 0)
|
977
|
+
pbuf = HPDF_IToA (buf, 100, eptr);
|
978
|
+
else
|
979
|
+
pbuf = HPDF_IToA (buf, odd, eptr);
|
980
|
+
HPDF_StrCpy (pbuf, " begincidrange\r\n", eptr);
|
981
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
982
|
+
|
983
|
+
for (i = 0; i < attr->cmap_range->count; i++) {
|
984
|
+
HPDF_CidRange_Rec *range = HPDF_List_ItemAt (attr->cmap_range, i);
|
985
|
+
|
986
|
+
pbuf = UINT16ToHex (buf, range->from, eptr);
|
987
|
+
*pbuf++ = ' ';
|
988
|
+
pbuf = UINT16ToHex (pbuf, range->to, eptr);
|
989
|
+
*pbuf++ = ' ';
|
990
|
+
pbuf = HPDF_IToA (pbuf, range->cid, eptr);
|
991
|
+
HPDF_StrCpy (pbuf, "\r\n", eptr);
|
992
|
+
|
993
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
994
|
+
|
995
|
+
if ((i + 1) %100 == 0) {
|
996
|
+
phase--;
|
997
|
+
pbuf = HPDF_StrCpy (buf, "endcidrange\r\n\r\n", eptr);
|
998
|
+
|
999
|
+
if (phase > 0)
|
1000
|
+
pbuf = HPDF_IToA (pbuf, 100, eptr);
|
1001
|
+
else
|
1002
|
+
pbuf = HPDF_IToA (pbuf, odd, eptr);
|
1003
|
+
|
1004
|
+
HPDF_StrCpy (pbuf, " begincidrange\r\n", eptr);
|
1005
|
+
|
1006
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
1007
|
+
}
|
1008
|
+
|
1009
|
+
if (ret != HPDF_OK)
|
1010
|
+
return NULL;
|
1011
|
+
}
|
1012
|
+
|
1013
|
+
if (odd > 0)
|
1014
|
+
pbuf = HPDF_StrCpy (buf, "endcidrange\r\n", eptr);
|
1015
|
+
|
1016
|
+
pbuf = HPDF_StrCpy (pbuf, "endcmap\r\n", eptr);
|
1017
|
+
pbuf = HPDF_StrCpy (pbuf, "CMapName currentdict /CMap "
|
1018
|
+
"defineresource pop\r\n", eptr);
|
1019
|
+
pbuf = HPDF_StrCpy (pbuf, "end\r\n", eptr);
|
1020
|
+
pbuf = HPDF_StrCpy (pbuf, "end\r\n\r\n", eptr);
|
1021
|
+
pbuf = HPDF_StrCpy (pbuf, "%%EndResource\r\n", eptr);
|
1022
|
+
HPDF_StrCpy (pbuf, "%%EOF\r\n", eptr);
|
1023
|
+
ret += HPDF_Stream_WriteStr (cmap->stream, buf);
|
1024
|
+
|
1025
|
+
if (ret != HPDF_OK)
|
1026
|
+
return NULL;
|
1027
|
+
|
1028
|
+
return cmap;
|
1029
|
+
}
|
1030
|
+
|