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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "public/fpdf_annot.h"
#include <utility>
#include "core/fpdfapi/page/cpdf_form.h"
#include "core/fpdfapi/page/cpdf_page.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/cpdf_name.h"
#include "core/fpdfapi/parser/cpdf_number.h"
#include "core/fpdfapi/parser/cpdf_string.h"
#include "core/fpdfdoc/cpdf_annot.h"
#include "core/fpdfdoc/cpvt_color.h"
#include "core/fpdfdoc/cpvt_generateap.h"
#include "fpdfsdk/fsdk_define.h"
namespace {
// These checks ensure the consistency of annotation subtype values across core/
// and public.
static_assert(static_cast<int>(CPDF_Annot::Subtype::UNKNOWN) ==
FPDF_ANNOT_UNKNOWN,
"CPDF_Annot::UNKNOWN value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::TEXT) == FPDF_ANNOT_TEXT,
"CPDF_Annot::TEXT value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::LINK) == FPDF_ANNOT_LINK,
"CPDF_Annot::LINK value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::FREETEXT) ==
FPDF_ANNOT_FREETEXT,
"CPDF_Annot::FREETEXT value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::LINE) == FPDF_ANNOT_LINE,
"CPDF_Annot::LINE value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::SQUARE) ==
FPDF_ANNOT_SQUARE,
"CPDF_Annot::SQUARE value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::CIRCLE) ==
FPDF_ANNOT_CIRCLE,
"CPDF_Annot::CIRCLE value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::POLYGON) ==
FPDF_ANNOT_POLYGON,
"CPDF_Annot::POLYGON value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::POLYLINE) ==
FPDF_ANNOT_POLYLINE,
"CPDF_Annot::POLYLINE value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::HIGHLIGHT) ==
FPDF_ANNOT_HIGHLIGHT,
"CPDF_Annot::HIGHLIGHT value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::UNDERLINE) ==
FPDF_ANNOT_UNDERLINE,
"CPDF_Annot::UNDERLINE value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::SQUIGGLY) ==
FPDF_ANNOT_SQUIGGLY,
"CPDF_Annot::SQUIGGLY value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::STRIKEOUT) ==
FPDF_ANNOT_STRIKEOUT,
"CPDF_Annot::STRIKEOUT value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::STAMP) == FPDF_ANNOT_STAMP,
"CPDF_Annot::STAMP value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::CARET) == FPDF_ANNOT_CARET,
"CPDF_Annot::CARET value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::INK) == FPDF_ANNOT_INK,
"CPDF_Annot::INK value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::POPUP) == FPDF_ANNOT_POPUP,
"CPDF_Annot::POPUP value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::FILEATTACHMENT) ==
FPDF_ANNOT_FILEATTACHMENT,
"CPDF_Annot::FILEATTACHMENT value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::SOUND) == FPDF_ANNOT_SOUND,
"CPDF_Annot::SOUND value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::MOVIE) == FPDF_ANNOT_MOVIE,
"CPDF_Annot::MOVIE value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::WIDGET) ==
FPDF_ANNOT_WIDGET,
"CPDF_Annot::WIDGET value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::SCREEN) ==
FPDF_ANNOT_SCREEN,
"CPDF_Annot::SCREEN value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::PRINTERMARK) ==
FPDF_ANNOT_PRINTERMARK,
"CPDF_Annot::PRINTERMARK value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::TRAPNET) ==
FPDF_ANNOT_TRAPNET,
"CPDF_Annot::TRAPNET value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::WATERMARK) ==
FPDF_ANNOT_WATERMARK,
"CPDF_Annot::WATERMARK value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::THREED) ==
FPDF_ANNOT_THREED,
"CPDF_Annot::THREED value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::RICHMEDIA) ==
FPDF_ANNOT_RICHMEDIA,
"CPDF_Annot::RICHMEDIA value mismatch");
static_assert(static_cast<int>(CPDF_Annot::Subtype::XFAWIDGET) ==
FPDF_ANNOT_XFAWIDGET,
"CPDF_Annot::XFAWIDGET value mismatch");
class CPDF_AnnotContext {
public:
CPDF_AnnotContext(CPDF_Dictionary* pAnnotDict,
CPDF_Page* pPage,
CPDF_Stream* pStream)
: m_pAnnotDict(pAnnotDict), m_pPage(pPage) {
SetForm(pStream);
}
~CPDF_AnnotContext() {}
bool HasForm() const { return !!m_pAnnotForm; }
void SetForm(CPDF_Stream* pStream) {
if (!pStream)
return;
m_pAnnotForm = pdfium::MakeUnique<CPDF_Form>(
m_pPage->m_pDocument.Get(), m_pPage->m_pResources.Get(), pStream);
m_pAnnotForm->ParseContent(nullptr, nullptr, nullptr);
}
CPDF_Form* GetForm() const { return m_pAnnotForm.get(); }
CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); }
private:
std::unique_ptr<CPDF_Form> m_pAnnotForm;
CFX_UnownedPtr<CPDF_Dictionary> m_pAnnotDict;
CFX_UnownedPtr<CPDF_Page> m_pPage;
};
FPDF_ANNOTATION FPDFAnnotationFromCPDFAnnotContext(CPDF_AnnotContext* pAnnot) {
return static_cast<FPDF_ANNOTATION>(pAnnot);
}
CPDF_AnnotContext* CPDFAnnotContextFromFPDFAnnotation(FPDF_ANNOTATION annot) {
return static_cast<CPDF_AnnotContext*>(annot);
}
} // namespace
DLLEXPORT FPDF_BOOL STDCALL
FPDFAnnot_IsSupportedSubtype(FPDF_ANNOTATION_SUBTYPE subtype) {
return subtype == FPDF_ANNOT_CIRCLE || subtype == FPDF_ANNOT_HIGHLIGHT ||
subtype == FPDF_ANNOT_INK || subtype == FPDF_ANNOT_POPUP ||
subtype == FPDF_ANNOT_SQUARE || subtype == FPDF_ANNOT_SQUIGGLY ||
subtype == FPDF_ANNOT_STRIKEOUT || subtype == FPDF_ANNOT_TEXT ||
subtype == FPDF_ANNOT_UNDERLINE;
}
DLLEXPORT FPDF_BOOL STDCALL
FPDFPage_CreateAnnot(FPDF_PAGE page,
FPDF_ANNOTATION_SUBTYPE subtype,
FPDF_ANNOTATION* annot) {
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage || !FPDFAnnot_IsSupportedSubtype(subtype))
return false;
auto pDict = pdfium::MakeUnique<CPDF_Dictionary>(
pPage->m_pDocument->GetByteStringPool());
pDict->SetNewFor<CPDF_Name>("Type", "Annot");
pDict->SetNewFor<CPDF_Name>("Subtype",
CPDF_Annot::AnnotSubtypeToString(
static_cast<CPDF_Annot::Subtype>(subtype)));
if (annot) {
auto pNewAnnot =
pdfium::MakeUnique<CPDF_AnnotContext>(pDict.get(), pPage, nullptr);
*annot = FPDFAnnotationFromCPDFAnnotContext(pNewAnnot.release());
}
CPDF_Array* pAnnotList = pPage->m_pFormDict->GetArrayFor("Annots");
if (!pAnnotList)
pAnnotList = pPage->m_pFormDict->SetNewFor<CPDF_Array>("Annots");
pAnnotList->Add(std::move(pDict));
return true;
}
DLLEXPORT int STDCALL FPDFPage_GetAnnotCount(FPDF_PAGE page) {
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage || !pPage->m_pFormDict)
return 0;
CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
return pAnnots ? pAnnots->GetCount() : 0;
}
DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetAnnot(FPDF_PAGE page,
int index,
FPDF_ANNOTATION* annot) {
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage || !pPage->m_pFormDict || index < 0 || !annot)
return false;
CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
if (!pAnnots || static_cast<size_t>(index) >= pAnnots->GetCount())
return false;
CPDF_Dictionary* pDict = ToDictionary(pAnnots->GetDirectObjectAt(index));
auto pNewAnnot = pdfium::MakeUnique<CPDF_AnnotContext>(pDict, pPage, nullptr);
*annot = FPDFAnnotationFromCPDFAnnotContext(pNewAnnot.release());
return true;
}
DLLEXPORT void STDCALL FPDFPage_CloseAnnot(FPDF_ANNOTATION annot) {
delete CPDFAnnotContextFromFPDFAnnotation(annot);
}
DLLEXPORT FPDF_ANNOTATION_SUBTYPE STDCALL
FPDFAnnot_GetSubtype(FPDF_ANNOTATION annot) {
CPDF_Dictionary* pAnnotDict =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
if (!pAnnotDict)
return FPDF_ANNOT_UNKNOWN;
return static_cast<FPDF_ANNOTATION_SUBTYPE>(
CPDF_Annot::StringToAnnotSubtype(pAnnotDict->GetStringFor("Subtype")));
}
DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetColor(FPDF_ANNOTATION annot,
FPDFANNOT_COLORTYPE type,
unsigned int R,
unsigned int G,
unsigned int B,
unsigned int A) {
CPDF_Dictionary* pAnnotDict =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
if (!pAnnotDict || R > 255 || G > 255 || B > 255 || A > 255)
return false;
// Set the opacity of the annotation.
pAnnotDict->SetNewFor<CPDF_Number>("CA", A / 255.f);
// Set the color of the annotation.
CFX_ByteString key = type == FPDFANNOT_COLORTYPE_InteriorColor ? "IC" : "C";
CPDF_Array* pColor = pAnnotDict->GetArrayFor(key);
if (pColor)
pColor->Clear();
else
pColor = pAnnotDict->SetNewFor<CPDF_Array>(key);
pColor->AddNew<CPDF_Number>(R / 255.f);
pColor->AddNew<CPDF_Number>(G / 255.f);
pColor->AddNew<CPDF_Number>(B / 255.f);
return true;
}
DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_GetColor(FPDF_ANNOTATION annot,
FPDFANNOT_COLORTYPE type,
unsigned int* R,
unsigned int* G,
unsigned int* B,
unsigned int* A) {
CPDF_Dictionary* pAnnotDict =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
if (!pAnnotDict || !R || !G || !B || !A)
return false;
CPDF_Array* pColor = pAnnotDict->GetArrayFor(
type == FPDFANNOT_COLORTYPE_InteriorColor ? "IC" : "C");
*A =
(pAnnotDict->KeyExist("CA") ? pAnnotDict->GetNumberFor("CA") : 1) * 255.f;
if (!pColor) {
// Use default color. The default colors must be consistent with the ones
// used to generate AP. See calls to GetColorStringWithDefault() in
// CPVT_GenerateAP::Generate*AP().
if (pAnnotDict->GetStringFor("Subtype") == "Highlight") {
*R = 255;
*G = 255;
*B = 0;
} else {
*R = 0;
*G = 0;
*B = 0;
}
return true;
}
CPVT_Color color = CPVT_Color::ParseColor(*pColor);
switch (color.nColorType) {
case CPVT_Color::kRGB:
*R = color.fColor1 * 255.f;
*G = color.fColor2 * 255.f;
*B = color.fColor3 * 255.f;
break;
case CPVT_Color::kGray:
*R = 255.f * color.fColor1;
*G = 255.f * color.fColor1;
*B = 255.f * color.fColor1;
break;
case CPVT_Color::kCMYK:
*R = 255.f * (1 - color.fColor1) * (1 - color.fColor4);
*G = 255.f * (1 - color.fColor2) * (1 - color.fColor4);
*B = 255.f * (1 - color.fColor3) * (1 - color.fColor4);
break;
case CPVT_Color::kTransparent:
*R = 0;
*G = 0;
*B = 0;
break;
}
return true;
}
DLLEXPORT FPDF_BOOL STDCALL
FPDFAnnot_HasAttachmentPoints(FPDF_ANNOTATION annot) {
if (!annot)
return false;
FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot);
return subtype == FPDF_ANNOT_LINK || subtype == FPDF_ANNOT_HIGHLIGHT ||
subtype == FPDF_ANNOT_UNDERLINE || subtype == FPDF_ANNOT_SQUIGGLY ||
subtype == FPDF_ANNOT_STRIKEOUT;
}
DLLEXPORT FPDF_BOOL STDCALL
FPDFAnnot_SetAttachmentPoints(FPDF_ANNOTATION annot,
FS_QUADPOINTSF quadPoints) {
if (!annot || !FPDFAnnot_HasAttachmentPoints(annot))
return false;
CPDF_Dictionary* pAnnotDict =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
CPDF_Array* pQuadPoints = pAnnotDict->GetArrayFor("QuadPoints");
if (pQuadPoints)
pQuadPoints->Clear();
else
pQuadPoints = pAnnotDict->SetNewFor<CPDF_Array>("QuadPoints");
pQuadPoints->AddNew<CPDF_Number>(quadPoints.x1);
pQuadPoints->AddNew<CPDF_Number>(quadPoints.y1);
pQuadPoints->AddNew<CPDF_Number>(quadPoints.x2);
pQuadPoints->AddNew<CPDF_Number>(quadPoints.y2);
pQuadPoints->AddNew<CPDF_Number>(quadPoints.x3);
pQuadPoints->AddNew<CPDF_Number>(quadPoints.y3);
pQuadPoints->AddNew<CPDF_Number>(quadPoints.x4);
pQuadPoints->AddNew<CPDF_Number>(quadPoints.y4);
return true;
}
DLLEXPORT FPDF_BOOL STDCALL
FPDFAnnot_GetAttachmentPoints(FPDF_ANNOTATION annot,
FS_QUADPOINTSF* quadPoints) {
if (!annot || !quadPoints || !FPDFAnnot_HasAttachmentPoints(annot))
return false;
CPDF_Array* pArray =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict()->GetArrayFor(
"QuadPoints");
if (!pArray)
return false;
quadPoints->x1 = pArray->GetNumberAt(0);
quadPoints->y1 = pArray->GetNumberAt(1);
quadPoints->x2 = pArray->GetNumberAt(2);
quadPoints->y2 = pArray->GetNumberAt(3);
quadPoints->x3 = pArray->GetNumberAt(4);
quadPoints->y3 = pArray->GetNumberAt(5);
quadPoints->x4 = pArray->GetNumberAt(6);
quadPoints->y4 = pArray->GetNumberAt(7);
return true;
}
DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetRect(FPDF_ANNOTATION annot,
FS_RECTF rect) {
CPDF_Dictionary* pAnnotDict =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
if (!pAnnotDict)
return false;
CPDF_Array* pRect = pAnnotDict->GetArrayFor("Rect");
if (pRect)
pRect->Clear();
else
pRect = pAnnotDict->SetNewFor<CPDF_Array>("Rect");
pRect->AddNew<CPDF_Number>(rect.left);
pRect->AddNew<CPDF_Number>(rect.bottom);
pRect->AddNew<CPDF_Number>(rect.right);
pRect->AddNew<CPDF_Number>(rect.top);
return true;
}
DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_GetRect(FPDF_ANNOTATION annot,
FS_RECTF* rect) {
CPDF_Dictionary* pAnnotDict =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
if (!rect || !pAnnotDict)
return false;
CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect");
if (rt.IsEmpty())
return false;
rect->left = rt.left;
rect->bottom = rt.bottom;
rect->right = rt.right;
rect->top = rt.top;
return true;
}
DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetText(FPDF_ANNOTATION annot,
FPDFANNOT_TEXTTYPE type,
FPDF_WIDESTRING text) {
CPDF_Dictionary* pAnnotDict =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
if (!pAnnotDict)
return false;
CFX_ByteString key = type == FPDFANNOT_TEXTTYPE_Author ? "T" : "Contents";
FX_STRSIZE len = CFX_WideString::WStringLength(text);
CFX_WideString encodedText = CFX_WideString::FromUTF16LE(text, len);
pAnnotDict->SetNewFor<CPDF_String>(key, encodedText.UTF8Encode(), false);
return true;
}
DLLEXPORT unsigned long STDCALL FPDFAnnot_GetText(FPDF_ANNOTATION annot,
FPDFANNOT_TEXTTYPE type,
void* buffer,
unsigned long buflen) {
CPDF_Dictionary* pAnnotDict =
CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
if (!pAnnotDict)
return 0;
CFX_ByteString key = type == FPDFANNOT_TEXTTYPE_Author ? "T" : "Contents";
CFX_ByteString contents = pAnnotDict->GetUnicodeTextFor(key).UTF16LE_Encode();
unsigned long len = contents.GetLength();
if (buffer && buflen >= len)
memcpy(buffer, contents.c_str(), len);
return len;
}
|