forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhpdf_annotation.c
More file actions
415 lines (318 loc) · 11.3 KB
/
hpdf_annotation.c
File metadata and controls
415 lines (318 loc) · 11.3 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* << Haru Free PDF Library 2.0.0 >> -- hpdf_annotation.c
*
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
* It is provided "as is" without express or implied warranty.
*
*/
#include "hpdf_conf.h"
#include "hpdf_utils.h"
#include "hpdf_annotation.h"
#include "hpdf.h"
const static char *HPDF_ANNOT_TYPE_NAMES[] = {
"Text",
"Link",
"Sound",
"FreeText",
"Stamp",
"Square",
"Circle",
"StrikeOut",
"Highlight",
"Underline",
"Ink",
"FileAttachment",
"Popup"
};
const static char *HPDF_ANNOT_ICON_NAMES_NAMES[] = {
"Comment",
"Key",
"Note",
"Help",
"NewParagraph",
"Paragraph",
"Insert"
};
static HPDF_BOOL
CheckSubType (HPDF_Annotation annot,
HPDF_AnnotType type);
/*----------------------------------------------------------------------------*/
/*------ HPDF_Annotation -----------------------------------------------------*/
HPDF_Annotation
HPDF_Annotation_New (HPDF_MMgr mmgr,
HPDF_Xref xref,
HPDF_AnnotType type,
HPDF_Rect rect)
{
HPDF_Annotation annot;
HPDF_Array array;
HPDF_STATUS ret = HPDF_OK;
HPDF_REAL tmp;
HPDF_PTRACE((" HPDF_Annotation_New\n"));
annot = HPDF_Dict_New (mmgr);
if (!annot)
return NULL;
if (HPDF_Xref_Add (xref, annot) != HPDF_OK)
return NULL;
array = HPDF_Array_New (mmgr);
if (!array)
return NULL;
if (HPDF_Dict_Add (annot, "Rect", array) != HPDF_OK)
return NULL;
if (rect.top < rect.bottom) {
tmp = rect.top;
rect.top = rect.bottom;
rect.bottom = tmp;
}
ret += HPDF_Array_AddReal (array, rect.left);
ret += HPDF_Array_AddReal (array, rect.bottom);
ret += HPDF_Array_AddReal (array, rect.right);
ret += HPDF_Array_AddReal (array, rect.top);
ret += HPDF_Dict_AddName (annot, "Type", "Annot");
ret += HPDF_Dict_AddName (annot, "Subtype",
HPDF_ANNOT_TYPE_NAMES[(HPDF_INT)type]);
if (ret != HPDF_OK)
return NULL;
annot->header.obj_class |= HPDF_OSUBCLASS_ANNOTATION;
return annot;
}
HPDF_STATUS
HPDF_Annotation_SetBorderStyle (HPDF_Annotation annot,
HPDF_BSSubtype subtype,
HPDF_REAL width,
HPDF_UINT16 dash_on,
HPDF_UINT16 dash_off,
HPDF_UINT16 dash_phase)
{
HPDF_Dict bs;
HPDF_Array dash;
HPDF_STATUS ret;
HPDF_PTRACE((" HPDF_Annotation_SetBoderStyle\n"));
bs = HPDF_Dict_New (annot->mmgr);
if (!bs)
return HPDF_Error_GetCode (annot->error);
if ((ret = HPDF_Dict_Add (annot, "BS", bs)) != HPDF_OK)
return ret;
if (subtype == HPDF_BS_DASHED) {
dash = HPDF_Array_New (annot->mmgr);
if (!dash)
return HPDF_Error_GetCode (annot->error);
if ((ret = HPDF_Dict_Add (bs, "D", dash)) != HPDF_OK)
return ret;
ret += HPDF_Dict_AddName (bs, "Type", "Border");
ret += HPDF_Array_AddReal (dash, dash_on);
ret += HPDF_Array_AddReal (dash, dash_off);
if (dash_phase != 0)
ret += HPDF_Array_AddReal (dash, dash_off);
}
switch (subtype) {
case HPDF_BS_SOLID:
ret += HPDF_Dict_AddName (bs, "S", "S");
break;
case HPDF_BS_DASHED:
ret += HPDF_Dict_AddName (bs, "S", "D");
break;
case HPDF_BS_BEVELED:
ret += HPDF_Dict_AddName (bs, "S", "B");
break;
case HPDF_BS_INSET:
ret += HPDF_Dict_AddName (bs, "S", "I");
break;
case HPDF_BS_UNDERLINED:
ret += HPDF_Dict_AddName (bs, "S", "U");
break;
default:
return HPDF_SetError (annot->error, HPDF_ANNOT_INVALID_BORDER_STYLE, 0);
}
if (width != HPDF_BS_DEF_WIDTH)
ret += HPDF_Dict_AddReal (bs, "W", width);
if (ret != HPDF_OK)
return HPDF_Error_GetCode (annot->error);
return HPDF_OK;
}
HPDF_Annotation
HPDF_LinkAnnot_New (HPDF_MMgr mmgr,
HPDF_Xref xref,
HPDF_Rect rect,
HPDF_Destination dst)
{
HPDF_Annotation annot;
HPDF_PTRACE((" HPDF_LinkAnnot_New\n"));
annot = HPDF_Annotation_New (mmgr, xref, HPDF_ANNOT_LINK, rect);
if (!annot)
return NULL;
if (HPDF_Dict_Add (annot, "Dest", dst) != HPDF_OK)
return NULL;
return annot;
}
HPDF_Annotation
HPDF_URILinkAnnot_New (HPDF_MMgr mmgr,
HPDF_Xref xref,
HPDF_Rect rect,
const char *uri)
{
HPDF_Annotation annot;
HPDF_Dict action;
HPDF_STATUS ret;
HPDF_PTRACE((" HPDF_URILinkAnnot_New\n"));
annot = HPDF_Annotation_New (mmgr, xref, HPDF_ANNOT_LINK, rect);
if (!annot)
return NULL;
/* create action dictionary */
action = HPDF_Dict_New (mmgr);
if (!action)
return NULL;
ret = HPDF_Dict_Add (annot, "A", action);
if (ret != HPDF_OK)
return NULL;
ret += HPDF_Dict_AddName (action, "Type", "Action");
ret += HPDF_Dict_AddName (action, "S", "URI");
ret += HPDF_Dict_Add (action, "URI", HPDF_String_New (mmgr, uri, NULL));
if (ret != HPDF_OK)
return NULL;
return annot;
}
HPDF_EXPORT(HPDF_STATUS)
HPDF_LinkAnnot_SetBorderStyle (HPDF_Annotation annot,
HPDF_REAL width,
HPDF_UINT16 dash_on,
HPDF_UINT16 dash_off)
{
HPDF_Array array;
HPDF_STATUS ret;
HPDF_PTRACE((" HPDF_LinkAnnot_SetBorderStyle\n"));
if (!CheckSubType (annot, HPDF_ANNOT_LINK))
return HPDF_INVALID_ANNOTATION;
if (width < 0)
return HPDF_RaiseError (annot->error, HPDF_INVALID_PARAMETER, 0);
array = HPDF_Array_New (annot->mmgr);
if (!array)
return HPDF_CheckError (annot->error);
if ((ret = HPDF_Dict_Add (annot, "Border", array)) != HPDF_OK)
return HPDF_CheckError (annot->error);
ret += HPDF_Array_AddNumber (array, 0);
ret += HPDF_Array_AddNumber (array, 0);
ret += HPDF_Array_AddReal (array, width);
if (ret != HPDF_OK)
return HPDF_CheckError (annot->error);
if (dash_on && dash_off) {
HPDF_Array dash = HPDF_Array_New (annot->mmgr);
if (!dash)
return HPDF_CheckError (annot->error);
if ((ret = HPDF_Array_Add (array, dash)) != HPDF_OK)
return HPDF_CheckError (annot->error);
ret += HPDF_Array_AddNumber (dash, dash_on);
ret += HPDF_Array_AddNumber (dash, dash_off);
}
if (ret != HPDF_OK)
return HPDF_CheckError (annot->error);
return ret;
}
HPDF_EXPORT(HPDF_STATUS)
HPDF_LinkAnnot_SetHighlightMode (HPDF_Annotation annot,
HPDF_AnnotHighlightMode mode)
{
HPDF_STATUS ret;
HPDF_PTRACE((" HPDF_LinkAnnot_SetHighlightMode\n"));
if (!CheckSubType (annot, HPDF_ANNOT_LINK))
return HPDF_INVALID_ANNOTATION;
switch (mode) {
case HPDF_ANNOT_NO_HIGHTLIGHT:
ret = HPDF_Dict_AddName (annot, "H", "N");
break;
case HPDF_ANNOT_INVERT_BORDER:
ret = HPDF_Dict_AddName (annot, "H", "O");
break;
case HPDF_ANNOT_DOWN_APPEARANCE:
ret = HPDF_Dict_AddName (annot, "H", "P");
break;
default: /* HPDF_ANNOT_INVERT_BOX */
/* default value */
HPDF_Dict_RemoveElement (annot, "H");
ret = HPDF_OK;
}
if (ret != HPDF_OK)
return HPDF_CheckError (annot->error);
return ret;
}
HPDF_Annotation
HPDF_TextAnnot_New (HPDF_MMgr mmgr,
HPDF_Xref xref,
HPDF_Rect rect,
const char *text,
HPDF_Encoder encoder)
{
HPDF_Annotation annot;
HPDF_String s;
HPDF_PTRACE((" HPDF_TextAnnot_New\n"));
annot = HPDF_Annotation_New (mmgr, xref, HPDF_ANNOT_TEXT_NOTES, rect);
if (!annot)
return NULL;
s = HPDF_String_New (mmgr, text, encoder);
if (!s)
return NULL;
if (HPDF_Dict_Add (annot, "Contents", s) != HPDF_OK)
return NULL;
return annot;
}
HPDF_EXPORT(HPDF_STATUS)
HPDF_TextAnnot_SetIcon (HPDF_Annotation annot,
HPDF_AnnotIcon icon)
{
HPDF_PTRACE((" HPDF_TextAnnot_SetIcon\n"));
if (!CheckSubType (annot, HPDF_ANNOT_TEXT_NOTES))
return HPDF_INVALID_ANNOTATION;
if (icon < 0 || icon >= HPDF_ANNOT_ICON_EOF)
return HPDF_RaiseError (annot->error, HPDF_ANNOT_INVALID_ICON,
(HPDF_STATUS)icon);
if (HPDF_Dict_AddName (annot, "Name",
HPDF_ANNOT_ICON_NAMES_NAMES[(HPDF_INT)icon]) != HPDF_OK)
return HPDF_CheckError (annot->error);
return HPDF_OK;
}
HPDF_EXPORT(HPDF_STATUS)
HPDF_TextAnnot_SetOpened (HPDF_Annotation annot,
HPDF_BOOL opened)
{
HPDF_Boolean b;
HPDF_PTRACE((" HPDF_TextAnnot_SetOpend\n"));
if (!CheckSubType (annot, HPDF_ANNOT_TEXT_NOTES))
return HPDF_INVALID_ANNOTATION;
b = HPDF_Boolean_New (annot->mmgr, opened);
if (!b)
return HPDF_CheckError (annot->error);
return HPDF_Dict_Add (annot, "Open", b);
}
HPDF_BOOL
HPDF_Annotation_Validate (HPDF_Annotation annot)
{
HPDF_PTRACE((" HPDF_Annotation_Validate\n"));
if (!annot)
return HPDF_FALSE;
if (annot->header.obj_class !=
(HPDF_OSUBCLASS_ANNOTATION | HPDF_OCLASS_DICT))
return HPDF_FALSE;
return HPDF_TRUE;
}
static HPDF_BOOL
CheckSubType (HPDF_Annotation annot,
HPDF_AnnotType type)
{
HPDF_Name subtype;
HPDF_PTRACE((" HPDF_Annotation_CheckSubType\n"));
if (!HPDF_Annotation_Validate (annot))
return HPDF_FALSE;
subtype = HPDF_Dict_GetItem (annot, "Subtype", HPDF_OCLASS_NAME);
if (!subtype || HPDF_StrCmp (subtype->value,
HPDF_ANNOT_TYPE_NAMES[(HPDF_INT)type]) != 0) {
HPDF_RaiseError (annot->error, HPDF_INVALID_ANNOTATION, 0);
return HPDF_FALSE;
}
return HPDF_TRUE;
}