blob: fee99c844baec2e81e6f31a777be6321168f5ed1 [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jane Liu4fd9a472017-06-01 18:56:09 -04005#include "public/fpdf_annot.h"
6
Jane Liud60e9ad2017-06-26 11:28:36 -04007#include <memory>
Jane Liu20eafda2017-06-07 10:33:24 -04008#include <utility>
9
Jane Liubaa7ff42017-06-29 19:18:23 -040010#include "core/fpdfapi/edit/cpdf_pagecontentgenerator.h"
Jane Liue10509a2017-06-20 16:47:41 -040011#include "core/fpdfapi/page/cpdf_form.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "core/fpdfapi/page/cpdf_page.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "core/fpdfapi/page/cpdf_pageobject.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "core/fpdfapi/parser/cpdf_array.h"
15#include "core/fpdfapi/parser/cpdf_dictionary.h"
Jane Liu20eafda2017-06-07 10:33:24 -040016#include "core/fpdfapi/parser/cpdf_document.h"
17#include "core/fpdfapi/parser/cpdf_name.h"
18#include "core/fpdfapi/parser/cpdf_number.h"
19#include "core/fpdfapi/parser/cpdf_string.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040020#include "core/fpdfdoc/cpdf_annot.h"
Diana Gage7e0c05d2017-07-19 17:33:33 -070021#include "core/fpdfdoc/cpdf_formfield.h"
22#include "core/fpdfdoc/cpdf_interform.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040023#include "core/fpdfdoc/cpvt_color.h"
24#include "core/fpdfdoc/cpvt_generateap.h"
25#include "fpdfsdk/fsdk_define.h"
26
Jane Liue10509a2017-06-20 16:47:41 -040027namespace {
28
Jane Liu4fd9a472017-06-01 18:56:09 -040029// These checks ensure the consistency of annotation subtype values across core/
30// and public.
31static_assert(static_cast<int>(CPDF_Annot::Subtype::UNKNOWN) ==
32 FPDF_ANNOT_UNKNOWN,
33 "CPDF_Annot::UNKNOWN value mismatch");
34static_assert(static_cast<int>(CPDF_Annot::Subtype::TEXT) == FPDF_ANNOT_TEXT,
35 "CPDF_Annot::TEXT value mismatch");
36static_assert(static_cast<int>(CPDF_Annot::Subtype::LINK) == FPDF_ANNOT_LINK,
37 "CPDF_Annot::LINK value mismatch");
38static_assert(static_cast<int>(CPDF_Annot::Subtype::FREETEXT) ==
39 FPDF_ANNOT_FREETEXT,
40 "CPDF_Annot::FREETEXT value mismatch");
41static_assert(static_cast<int>(CPDF_Annot::Subtype::LINE) == FPDF_ANNOT_LINE,
42 "CPDF_Annot::LINE value mismatch");
43static_assert(static_cast<int>(CPDF_Annot::Subtype::SQUARE) ==
44 FPDF_ANNOT_SQUARE,
45 "CPDF_Annot::SQUARE value mismatch");
46static_assert(static_cast<int>(CPDF_Annot::Subtype::CIRCLE) ==
47 FPDF_ANNOT_CIRCLE,
48 "CPDF_Annot::CIRCLE value mismatch");
49static_assert(static_cast<int>(CPDF_Annot::Subtype::POLYGON) ==
50 FPDF_ANNOT_POLYGON,
51 "CPDF_Annot::POLYGON value mismatch");
52static_assert(static_cast<int>(CPDF_Annot::Subtype::POLYLINE) ==
53 FPDF_ANNOT_POLYLINE,
54 "CPDF_Annot::POLYLINE value mismatch");
55static_assert(static_cast<int>(CPDF_Annot::Subtype::HIGHLIGHT) ==
56 FPDF_ANNOT_HIGHLIGHT,
57 "CPDF_Annot::HIGHLIGHT value mismatch");
58static_assert(static_cast<int>(CPDF_Annot::Subtype::UNDERLINE) ==
59 FPDF_ANNOT_UNDERLINE,
60 "CPDF_Annot::UNDERLINE value mismatch");
61static_assert(static_cast<int>(CPDF_Annot::Subtype::SQUIGGLY) ==
62 FPDF_ANNOT_SQUIGGLY,
63 "CPDF_Annot::SQUIGGLY value mismatch");
64static_assert(static_cast<int>(CPDF_Annot::Subtype::STRIKEOUT) ==
65 FPDF_ANNOT_STRIKEOUT,
66 "CPDF_Annot::STRIKEOUT value mismatch");
67static_assert(static_cast<int>(CPDF_Annot::Subtype::STAMP) == FPDF_ANNOT_STAMP,
68 "CPDF_Annot::STAMP value mismatch");
69static_assert(static_cast<int>(CPDF_Annot::Subtype::CARET) == FPDF_ANNOT_CARET,
70 "CPDF_Annot::CARET value mismatch");
71static_assert(static_cast<int>(CPDF_Annot::Subtype::INK) == FPDF_ANNOT_INK,
72 "CPDF_Annot::INK value mismatch");
73static_assert(static_cast<int>(CPDF_Annot::Subtype::POPUP) == FPDF_ANNOT_POPUP,
74 "CPDF_Annot::POPUP value mismatch");
75static_assert(static_cast<int>(CPDF_Annot::Subtype::FILEATTACHMENT) ==
76 FPDF_ANNOT_FILEATTACHMENT,
77 "CPDF_Annot::FILEATTACHMENT value mismatch");
78static_assert(static_cast<int>(CPDF_Annot::Subtype::SOUND) == FPDF_ANNOT_SOUND,
79 "CPDF_Annot::SOUND value mismatch");
80static_assert(static_cast<int>(CPDF_Annot::Subtype::MOVIE) == FPDF_ANNOT_MOVIE,
81 "CPDF_Annot::MOVIE value mismatch");
82static_assert(static_cast<int>(CPDF_Annot::Subtype::WIDGET) ==
83 FPDF_ANNOT_WIDGET,
84 "CPDF_Annot::WIDGET value mismatch");
85static_assert(static_cast<int>(CPDF_Annot::Subtype::SCREEN) ==
86 FPDF_ANNOT_SCREEN,
87 "CPDF_Annot::SCREEN value mismatch");
88static_assert(static_cast<int>(CPDF_Annot::Subtype::PRINTERMARK) ==
89 FPDF_ANNOT_PRINTERMARK,
90 "CPDF_Annot::PRINTERMARK value mismatch");
91static_assert(static_cast<int>(CPDF_Annot::Subtype::TRAPNET) ==
92 FPDF_ANNOT_TRAPNET,
93 "CPDF_Annot::TRAPNET value mismatch");
94static_assert(static_cast<int>(CPDF_Annot::Subtype::WATERMARK) ==
95 FPDF_ANNOT_WATERMARK,
96 "CPDF_Annot::WATERMARK value mismatch");
97static_assert(static_cast<int>(CPDF_Annot::Subtype::THREED) ==
98 FPDF_ANNOT_THREED,
99 "CPDF_Annot::THREED value mismatch");
100static_assert(static_cast<int>(CPDF_Annot::Subtype::RICHMEDIA) ==
101 FPDF_ANNOT_RICHMEDIA,
102 "CPDF_Annot::RICHMEDIA value mismatch");
103static_assert(static_cast<int>(CPDF_Annot::Subtype::XFAWIDGET) ==
104 FPDF_ANNOT_XFAWIDGET,
105 "CPDF_Annot::XFAWIDGET value mismatch");
106
Jane Liu2e1a32b2017-07-06 12:01:25 -0400107// These checks ensure the consistency of dictionary value types across core/
108// and public/.
109static_assert(static_cast<int>(CPDF_Object::Type::BOOLEAN) ==
110 FPDF_OBJECT_BOOLEAN,
111 "CPDF_Object::BOOLEAN value mismatch");
112static_assert(static_cast<int>(CPDF_Object::Type::NUMBER) == FPDF_OBJECT_NUMBER,
113 "CPDF_Object::NUMBER value mismatch");
114static_assert(static_cast<int>(CPDF_Object::Type::STRING) == FPDF_OBJECT_STRING,
115 "CPDF_Object::STRING value mismatch");
116static_assert(static_cast<int>(CPDF_Object::Type::NAME) == FPDF_OBJECT_NAME,
117 "CPDF_Object::NAME value mismatch");
118static_assert(static_cast<int>(CPDF_Object::Type::ARRAY) == FPDF_OBJECT_ARRAY,
119 "CPDF_Object::ARRAY value mismatch");
120static_assert(static_cast<int>(CPDF_Object::Type::DICTIONARY) ==
121 FPDF_OBJECT_DICTIONARY,
122 "CPDF_Object::DICTIONARY value mismatch");
123static_assert(static_cast<int>(CPDF_Object::Type::STREAM) == FPDF_OBJECT_STREAM,
124 "CPDF_Object::STREAM value mismatch");
125static_assert(static_cast<int>(CPDF_Object::Type::NULLOBJ) ==
126 FPDF_OBJECT_NULLOBJ,
127 "CPDF_Object::NULLOBJ value mismatch");
128static_assert(static_cast<int>(CPDF_Object::Type::REFERENCE) ==
129 FPDF_OBJECT_REFERENCE,
130 "CPDF_Object::REFERENCE value mismatch");
131
Jane Liue10509a2017-06-20 16:47:41 -0400132class CPDF_AnnotContext {
133 public:
134 CPDF_AnnotContext(CPDF_Dictionary* pAnnotDict,
135 CPDF_Page* pPage,
136 CPDF_Stream* pStream)
137 : m_pAnnotDict(pAnnotDict), m_pPage(pPage) {
138 SetForm(pStream);
139 }
140 ~CPDF_AnnotContext() {}
141
142 bool HasForm() const { return !!m_pAnnotForm; }
143
144 void SetForm(CPDF_Stream* pStream) {
145 if (!pStream)
146 return;
147
Jane Liubaa7ff42017-06-29 19:18:23 -0400148 // Reset the annotation matrix to be the identity matrix, since the
149 // appearance stream already takes matrix into account.
150 pStream->GetDict()->SetMatrixFor("Matrix", CFX_Matrix());
151
Jane Liue10509a2017-06-20 16:47:41 -0400152 m_pAnnotForm = pdfium::MakeUnique<CPDF_Form>(
153 m_pPage->m_pDocument.Get(), m_pPage->m_pResources.Get(), pStream);
154 m_pAnnotForm->ParseContent(nullptr, nullptr, nullptr);
155 }
156
157 CPDF_Form* GetForm() const { return m_pAnnotForm.get(); }
158 CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); }
Jane Liubaa7ff42017-06-29 19:18:23 -0400159 CPDF_Page* GetPage() const { return m_pPage.Get(); }
Jane Liue10509a2017-06-20 16:47:41 -0400160
161 private:
162 std::unique_ptr<CPDF_Form> m_pAnnotForm;
163 CFX_UnownedPtr<CPDF_Dictionary> m_pAnnotDict;
164 CFX_UnownedPtr<CPDF_Page> m_pPage;
165};
166
Jane Liue10509a2017-06-20 16:47:41 -0400167CPDF_AnnotContext* CPDFAnnotContextFromFPDFAnnotation(FPDF_ANNOTATION annot) {
168 return static_cast<CPDF_AnnotContext*>(annot);
169}
170
Jane Liu06462752017-06-27 16:41:14 -0400171bool HasAPStream(const CPDF_Dictionary* pAnnotDict) {
172 return !!FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
173}
174
Jane Liu7a9a38b2017-07-11 13:47:37 -0400175void UpdateContentStream(CPDF_Form* pForm, CPDF_Stream* pStream) {
176 ASSERT(pForm);
177 ASSERT(pStream);
178
179 CPDF_PageContentGenerator generator(pForm);
180 std::ostringstream buf;
181 generator.ProcessPageObjects(&buf);
Artem Strygin90555e02017-07-28 19:41:59 +0300182 pStream->SetDataAndRemoveFilter(&buf);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400183}
Jane Liu2e1a32b2017-07-06 12:01:25 -0400184
Jane Liue10509a2017-06-20 16:47:41 -0400185} // namespace
186
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400187FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu20eafda2017-06-07 10:33:24 -0400188FPDFAnnot_IsSupportedSubtype(FPDF_ANNOTATION_SUBTYPE subtype) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400189 // The supported subtypes must also be communicated in the user doc.
Jane Liu20eafda2017-06-07 10:33:24 -0400190 return subtype == FPDF_ANNOT_CIRCLE || subtype == FPDF_ANNOT_HIGHLIGHT ||
191 subtype == FPDF_ANNOT_INK || subtype == FPDF_ANNOT_POPUP ||
192 subtype == FPDF_ANNOT_SQUARE || subtype == FPDF_ANNOT_SQUIGGLY ||
Jane Liubaa7ff42017-06-29 19:18:23 -0400193 subtype == FPDF_ANNOT_STAMP || subtype == FPDF_ANNOT_STRIKEOUT ||
194 subtype == FPDF_ANNOT_TEXT || subtype == FPDF_ANNOT_UNDERLINE;
Jane Liu20eafda2017-06-07 10:33:24 -0400195}
196
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400197FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV
Jane Liud60e9ad2017-06-26 11:28:36 -0400198FPDFPage_CreateAnnot(FPDF_PAGE page, FPDF_ANNOTATION_SUBTYPE subtype) {
Jane Liu20eafda2017-06-07 10:33:24 -0400199 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
200 if (!pPage || !FPDFAnnot_IsSupportedSubtype(subtype))
Jane Liud60e9ad2017-06-26 11:28:36 -0400201 return nullptr;
Jane Liu20eafda2017-06-07 10:33:24 -0400202
203 auto pDict = pdfium::MakeUnique<CPDF_Dictionary>(
204 pPage->m_pDocument->GetByteStringPool());
205 pDict->SetNewFor<CPDF_Name>("Type", "Annot");
206 pDict->SetNewFor<CPDF_Name>("Subtype",
207 CPDF_Annot::AnnotSubtypeToString(
208 static_cast<CPDF_Annot::Subtype>(subtype)));
Jane Liud60e9ad2017-06-26 11:28:36 -0400209 auto pNewAnnot =
210 pdfium::MakeUnique<CPDF_AnnotContext>(pDict.get(), pPage, nullptr);
Jane Liu20eafda2017-06-07 10:33:24 -0400211
212 CPDF_Array* pAnnotList = pPage->m_pFormDict->GetArrayFor("Annots");
213 if (!pAnnotList)
214 pAnnotList = pPage->m_pFormDict->SetNewFor<CPDF_Array>("Annots");
215
216 pAnnotList->Add(std::move(pDict));
Jane Liud60e9ad2017-06-26 11:28:36 -0400217 return pNewAnnot.release();
Jane Liu20eafda2017-06-07 10:33:24 -0400218}
219
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400220FPDF_EXPORT int FPDF_CALLCONV FPDFPage_GetAnnotCount(FPDF_PAGE page) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400221 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
222 if (!pPage || !pPage->m_pFormDict)
223 return 0;
Jane Liu20eafda2017-06-07 10:33:24 -0400224
Jane Liu4fd9a472017-06-01 18:56:09 -0400225 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
226 return pAnnots ? pAnnots->GetCount() : 0;
227}
228
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400229FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV FPDFPage_GetAnnot(FPDF_PAGE page,
230 int index) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400231 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Jane Liud60e9ad2017-06-26 11:28:36 -0400232 if (!pPage || !pPage->m_pFormDict || index < 0)
233 return nullptr;
Jane Liu20eafda2017-06-07 10:33:24 -0400234
Jane Liu4fd9a472017-06-01 18:56:09 -0400235 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
236 if (!pAnnots || static_cast<size_t>(index) >= pAnnots->GetCount())
Jane Liud60e9ad2017-06-26 11:28:36 -0400237 return nullptr;
Jane Liu4fd9a472017-06-01 18:56:09 -0400238
239 CPDF_Dictionary* pDict = ToDictionary(pAnnots->GetDirectObjectAt(index));
Jane Liue10509a2017-06-20 16:47:41 -0400240 auto pNewAnnot = pdfium::MakeUnique<CPDF_AnnotContext>(pDict, pPage, nullptr);
Jane Liud60e9ad2017-06-26 11:28:36 -0400241 return pNewAnnot.release();
Jane Liue10509a2017-06-20 16:47:41 -0400242}
243
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400244FPDF_EXPORT void FPDF_CALLCONV FPDFPage_CloseAnnot(FPDF_ANNOTATION annot) {
Jane Liue10509a2017-06-20 16:47:41 -0400245 delete CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400246}
247
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400248FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_RemoveAnnot(FPDF_PAGE page,
249 int index) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400250 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
251 if (!pPage || !pPage->m_pFormDict || index < 0)
252 return false;
253
254 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
255 if (!pAnnots || static_cast<size_t>(index) >= pAnnots->GetCount())
256 return false;
257
258 pAnnots->RemoveAt(index);
259 return true;
260}
261
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400262FPDF_EXPORT FPDF_ANNOTATION_SUBTYPE FPDF_CALLCONV
Jane Liu4fd9a472017-06-01 18:56:09 -0400263FPDFAnnot_GetSubtype(FPDF_ANNOTATION annot) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400264 if (!annot)
265 return FPDF_ANNOT_UNKNOWN;
266
Jane Liue10509a2017-06-20 16:47:41 -0400267 CPDF_Dictionary* pAnnotDict =
268 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liu4fd9a472017-06-01 18:56:09 -0400269 if (!pAnnotDict)
270 return FPDF_ANNOT_UNKNOWN;
Jane Liu20eafda2017-06-07 10:33:24 -0400271
Jane Liu4fd9a472017-06-01 18:56:09 -0400272 return static_cast<FPDF_ANNOTATION_SUBTYPE>(
273 CPDF_Annot::StringToAnnotSubtype(pAnnotDict->GetStringFor("Subtype")));
274}
275
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400276FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu7a9a38b2017-07-11 13:47:37 -0400277FPDFAnnot_IsObjectSupportedSubtype(FPDF_ANNOTATION_SUBTYPE subtype) {
278 // The supported subtypes must also be communicated in the user doc.
279 return subtype == FPDF_ANNOT_INK || subtype == FPDF_ANNOT_STAMP;
280}
281
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400282FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
283FPDFAnnot_UpdateObject(FPDF_ANNOTATION annot, FPDF_PAGEOBJECT obj) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400284 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu36567742017-07-06 11:13:35 -0400285 CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(obj);
286 if (!pAnnot || !pAnnot->GetAnnotDict() || !pAnnot->HasForm() || !pObj)
Jane Liubaa7ff42017-06-29 19:18:23 -0400287 return false;
288
289 // Check that the annotation type is supported by this method.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400290 if (!FPDFAnnot_IsObjectSupportedSubtype(FPDFAnnot_GetSubtype(annot)))
Jane Liubaa7ff42017-06-29 19:18:23 -0400291 return false;
292
293 // Check that the annotation already has an appearance stream, since an
Jane Liu36567742017-07-06 11:13:35 -0400294 // existing object is to be updated.
Jane Liubaa7ff42017-06-29 19:18:23 -0400295 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(pAnnot->GetAnnotDict(),
296 CPDF_Annot::AppearanceMode::Normal);
297 if (!pStream)
298 return false;
299
Jane Liu36567742017-07-06 11:13:35 -0400300 // Check that the object is already in this annotation's object list.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400301 CPDF_Form* pForm = pAnnot->GetForm();
302 CPDF_PageObjectList* pObjList = pForm->GetPageObjectList();
Jane Liu36567742017-07-06 11:13:35 -0400303 auto it =
304 std::find_if(pObjList->begin(), pObjList->end(),
305 [pObj](const std::unique_ptr<CPDF_PageObject>& candidate) {
306 return candidate.get() == pObj;
307 });
Jane Liubaa7ff42017-06-29 19:18:23 -0400308 if (it == pObjList->end())
309 return false;
310
311 // Update the content stream data in the annotation's AP stream.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400312 UpdateContentStream(pForm, pStream);
Jane Liubaa7ff42017-06-29 19:18:23 -0400313 return true;
314}
315
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400316FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
317FPDFAnnot_AppendObject(FPDF_ANNOTATION annot, FPDF_PAGEOBJECT obj) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400318 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu36567742017-07-06 11:13:35 -0400319 CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(obj);
320 if (!pAnnot || !pObj)
Jane Liubaa7ff42017-06-29 19:18:23 -0400321 return false;
322
323 CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
324 CPDF_Page* pPage = pAnnot->GetPage();
325 if (!pAnnotDict || !pPage)
326 return false;
327
328 // Check that the annotation type is supported by this method.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400329 if (!FPDFAnnot_IsObjectSupportedSubtype(FPDFAnnot_GetSubtype(annot)))
Jane Liubaa7ff42017-06-29 19:18:23 -0400330 return false;
331
332 // If the annotation does not have an AP stream yet, generate and set it.
333 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(pAnnot->GetAnnotDict(),
334 CPDF_Annot::AppearanceMode::Normal);
335 if (!pStream) {
336 auto pExtGStateDict =
337 CPVT_GenerateAP::GenerateExtGStateDict(*pAnnotDict, "GS", "Normal");
338 auto pResourceDict = CPVT_GenerateAP::GenerateResourceDict(
339 pPage->m_pDocument.Get(), std::move(pExtGStateDict), nullptr);
340 std::ostringstream sStream;
341 CPVT_GenerateAP::GenerateAndSetAPDict(pPage->m_pDocument.Get(), pAnnotDict,
342 &sStream, std::move(pResourceDict),
343 false);
344 pStream =
345 FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
346 if (!pStream)
347 return false;
348 }
349
350 // Get the annotation's corresponding form object for parsing its AP stream.
351 if (!pAnnot->HasForm())
352 pAnnot->SetForm(pStream);
353
Jane Liu36567742017-07-06 11:13:35 -0400354 // Check that the object did not come from the same annotation. If this check
355 // succeeds, then it is assumed that the object came from
356 // FPDFPageObj_CreateNew{Path|Rect}() or FPDFPageObj_New{Text|Image}Obj().
357 // Note that an object that came from a different annotation must not be
358 // passed here, since an object cannot belong to more than one annotation.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400359 CPDF_Form* pForm = pAnnot->GetForm();
Jane Liubaa7ff42017-06-29 19:18:23 -0400360 CPDF_PageObjectList* pObjList = pForm->GetPageObjectList();
Jane Liu36567742017-07-06 11:13:35 -0400361 auto it =
362 std::find_if(pObjList->begin(), pObjList->end(),
363 [pObj](const std::unique_ptr<CPDF_PageObject>& candidate) {
364 return candidate.get() == pObj;
365 });
Jane Liubaa7ff42017-06-29 19:18:23 -0400366 if (it != pObjList->end())
367 return false;
368
Jane Liu36567742017-07-06 11:13:35 -0400369 // Append the object to the object list.
370 std::unique_ptr<CPDF_PageObject> pPageObjHolder(pObj);
Jane Liubaa7ff42017-06-29 19:18:23 -0400371 pObjList->push_back(std::move(pPageObjHolder));
372
373 // Set the content stream data in the annotation's AP stream.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400374 UpdateContentStream(pForm, pStream);
Jane Liubaa7ff42017-06-29 19:18:23 -0400375 return true;
376}
377
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400378FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetObjectCount(FPDF_ANNOTATION annot) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400379 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu36567742017-07-06 11:13:35 -0400380 if (!pAnnot || !pAnnot->GetAnnotDict())
Jane Liubaa7ff42017-06-29 19:18:23 -0400381 return 0;
382
383 if (!pAnnot->HasForm()) {
384 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(
385 pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::Normal);
386 if (!pStream)
387 return 0;
388
389 pAnnot->SetForm(pStream);
390 }
Jane Liu57f228d2017-07-13 18:10:30 -0400391 return pdfium::CollectionSize<int>(*pAnnot->GetForm()->GetPageObjectList());
Jane Liubaa7ff42017-06-29 19:18:23 -0400392}
393
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400394FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV
395FPDFAnnot_GetObject(FPDF_ANNOTATION annot, int index) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400396 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu36567742017-07-06 11:13:35 -0400397 if (!pAnnot || !pAnnot->GetAnnotDict() || index < 0)
Jane Liubaa7ff42017-06-29 19:18:23 -0400398 return nullptr;
399
400 if (!pAnnot->HasForm()) {
401 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(
402 pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::Normal);
403 if (!pStream)
404 return nullptr;
405
406 pAnnot->SetForm(pStream);
407 }
408
Jane Liu36567742017-07-06 11:13:35 -0400409 return pAnnot->GetForm()->GetPageObjectList()->GetPageObjectByIndex(index);
Jane Liubaa7ff42017-06-29 19:18:23 -0400410}
411
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400412FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
413FPDFAnnot_RemoveObject(FPDF_ANNOTATION annot, int index) {
Jane Liu7a9a38b2017-07-11 13:47:37 -0400414 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
415 if (!pAnnot || !pAnnot->GetAnnotDict() || !pAnnot->HasForm() || index < 0)
416 return false;
417
418 // Check that the annotation type is supported by this method.
419 if (!FPDFAnnot_IsObjectSupportedSubtype(FPDFAnnot_GetSubtype(annot)))
420 return false;
421
422 // Check that the annotation already has an appearance stream, since an
423 // existing object is to be deleted.
424 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(pAnnot->GetAnnotDict(),
425 CPDF_Annot::AppearanceMode::Normal);
426 if (!pStream)
427 return false;
428
429 CPDF_PageObjectList* pObjList = pAnnot->GetForm()->GetPageObjectList();
430 if (static_cast<size_t>(index) >= pObjList->size())
431 return false;
432
433 pObjList->erase(pObjList->begin() + index);
434 UpdateContentStream(pAnnot->GetForm(), pStream);
435 return true;
436}
437
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400438FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetColor(FPDF_ANNOTATION annot,
439 FPDFANNOT_COLORTYPE type,
440 unsigned int R,
441 unsigned int G,
442 unsigned int B,
443 unsigned int A) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400444 if (!annot || R > 255 || G > 255 || B > 255 || A > 255)
445 return false;
446
Jane Liue10509a2017-06-20 16:47:41 -0400447 CPDF_Dictionary* pAnnotDict =
448 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liud60e9ad2017-06-26 11:28:36 -0400449 if (!pAnnotDict)
Jane Liu20eafda2017-06-07 10:33:24 -0400450 return false;
451
Jane Liu06462752017-06-27 16:41:14 -0400452 // For annotations with their appearance streams already defined, the path
453 // stream's own color definitions take priority over the annotation color
454 // definitions set by this method, hence this method will simply fail.
455 if (HasAPStream(pAnnotDict))
456 return false;
457
Jane Liu20eafda2017-06-07 10:33:24 -0400458 // Set the opacity of the annotation.
459 pAnnotDict->SetNewFor<CPDF_Number>("CA", A / 255.f);
460
461 // Set the color of the annotation.
462 CFX_ByteString key = type == FPDFANNOT_COLORTYPE_InteriorColor ? "IC" : "C";
463 CPDF_Array* pColor = pAnnotDict->GetArrayFor(key);
464 if (pColor)
Lei Zhang59c1ac02017-06-09 11:04:41 -0700465 pColor->Clear();
Jane Liu20eafda2017-06-07 10:33:24 -0400466 else
467 pColor = pAnnotDict->SetNewFor<CPDF_Array>(key);
468
469 pColor->AddNew<CPDF_Number>(R / 255.f);
470 pColor->AddNew<CPDF_Number>(G / 255.f);
471 pColor->AddNew<CPDF_Number>(B / 255.f);
472
473 return true;
474}
475
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400476FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetColor(FPDF_ANNOTATION annot,
477 FPDFANNOT_COLORTYPE type,
478 unsigned int* R,
479 unsigned int* G,
480 unsigned int* B,
481 unsigned int* A) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400482 if (!annot || !R || !G || !B || !A)
483 return false;
484
Jane Liue10509a2017-06-20 16:47:41 -0400485 CPDF_Dictionary* pAnnotDict =
486 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liud60e9ad2017-06-26 11:28:36 -0400487 if (!pAnnotDict)
Jane Liu4fd9a472017-06-01 18:56:09 -0400488 return false;
489
Jane Liu06462752017-06-27 16:41:14 -0400490 // For annotations with their appearance streams already defined, the path
491 // stream's own color definitions take priority over the annotation color
492 // definitions retrieved by this method, hence this method will simply fail.
493 if (HasAPStream(pAnnotDict))
494 return false;
495
Jane Liu4fd9a472017-06-01 18:56:09 -0400496 CPDF_Array* pColor = pAnnotDict->GetArrayFor(
497 type == FPDFANNOT_COLORTYPE_InteriorColor ? "IC" : "C");
498 *A =
499 (pAnnotDict->KeyExist("CA") ? pAnnotDict->GetNumberFor("CA") : 1) * 255.f;
500 if (!pColor) {
501 // Use default color. The default colors must be consistent with the ones
502 // used to generate AP. See calls to GetColorStringWithDefault() in
503 // CPVT_GenerateAP::Generate*AP().
504 if (pAnnotDict->GetStringFor("Subtype") == "Highlight") {
505 *R = 255;
506 *G = 255;
507 *B = 0;
508 } else {
509 *R = 0;
510 *G = 0;
511 *B = 0;
512 }
513 return true;
514 }
515 CPVT_Color color = CPVT_Color::ParseColor(*pColor);
516 switch (color.nColorType) {
517 case CPVT_Color::kRGB:
518 *R = color.fColor1 * 255.f;
519 *G = color.fColor2 * 255.f;
520 *B = color.fColor3 * 255.f;
521 break;
522 case CPVT_Color::kGray:
523 *R = 255.f * color.fColor1;
524 *G = 255.f * color.fColor1;
525 *B = 255.f * color.fColor1;
526 break;
527 case CPVT_Color::kCMYK:
528 *R = 255.f * (1 - color.fColor1) * (1 - color.fColor4);
529 *G = 255.f * (1 - color.fColor2) * (1 - color.fColor4);
530 *B = 255.f * (1 - color.fColor3) * (1 - color.fColor4);
531 break;
532 case CPVT_Color::kTransparent:
533 *R = 0;
534 *G = 0;
535 *B = 0;
536 break;
537 }
538 return true;
539}
540
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400541FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu4fd9a472017-06-01 18:56:09 -0400542FPDFAnnot_HasAttachmentPoints(FPDF_ANNOTATION annot) {
543 if (!annot)
544 return false;
Jane Liu20eafda2017-06-07 10:33:24 -0400545
Jane Liu4fd9a472017-06-01 18:56:09 -0400546 FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot);
547 return subtype == FPDF_ANNOT_LINK || subtype == FPDF_ANNOT_HIGHLIGHT ||
548 subtype == FPDF_ANNOT_UNDERLINE || subtype == FPDF_ANNOT_SQUIGGLY ||
549 subtype == FPDF_ANNOT_STRIKEOUT;
550}
551
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400552FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu20eafda2017-06-07 10:33:24 -0400553FPDFAnnot_SetAttachmentPoints(FPDF_ANNOTATION annot,
Jane Liu06462752017-06-27 16:41:14 -0400554 const FS_QUADPOINTSF* quadPoints) {
555 if (!annot || !quadPoints || !FPDFAnnot_HasAttachmentPoints(annot))
Jane Liu20eafda2017-06-07 10:33:24 -0400556 return false;
557
Jane Liue10509a2017-06-20 16:47:41 -0400558 CPDF_Dictionary* pAnnotDict =
559 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liud60e9ad2017-06-26 11:28:36 -0400560 if (!pAnnotDict)
561 return false;
562
Jane Liu06462752017-06-27 16:41:14 -0400563 // Update the "QuadPoints" entry in the annotation dictionary.
Jane Liu20eafda2017-06-07 10:33:24 -0400564 CPDF_Array* pQuadPoints = pAnnotDict->GetArrayFor("QuadPoints");
565 if (pQuadPoints)
Lei Zhang59c1ac02017-06-09 11:04:41 -0700566 pQuadPoints->Clear();
Jane Liu20eafda2017-06-07 10:33:24 -0400567 else
568 pQuadPoints = pAnnotDict->SetNewFor<CPDF_Array>("QuadPoints");
569
Jane Liu06462752017-06-27 16:41:14 -0400570 pQuadPoints->AddNew<CPDF_Number>(quadPoints->x1);
571 pQuadPoints->AddNew<CPDF_Number>(quadPoints->y1);
572 pQuadPoints->AddNew<CPDF_Number>(quadPoints->x2);
573 pQuadPoints->AddNew<CPDF_Number>(quadPoints->y2);
574 pQuadPoints->AddNew<CPDF_Number>(quadPoints->x3);
575 pQuadPoints->AddNew<CPDF_Number>(quadPoints->y3);
576 pQuadPoints->AddNew<CPDF_Number>(quadPoints->x4);
577 pQuadPoints->AddNew<CPDF_Number>(quadPoints->y4);
578
579 // If the annotation's appearance stream is defined, and the new quadpoints
580 // defines a bigger bounding box than the appearance stream currently
581 // specifies, then update the "BBox" entry in the AP dictionary too, since it
582 // comes from annotation dictionary's "QuadPoints" entry.
583 CPDF_Stream* pStream =
584 FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
585 if (pStream) {
586 CFX_FloatRect newRect = CPDF_Annot::RectFromQuadPoints(pAnnotDict);
587 if (newRect.Contains(pStream->GetDict()->GetRectFor("BBox")))
588 pStream->GetDict()->SetRectFor("BBox", newRect);
589 }
Jane Liu20eafda2017-06-07 10:33:24 -0400590 return true;
591}
592
Jane Liu0c6b07d2017-08-15 10:50:22 -0400593FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
594FPDFAnnot_GetAttachmentPoints(FPDF_ANNOTATION annot,
595 FS_QUADPOINTSF* quadPoints) {
596 if (!annot || !FPDFAnnot_HasAttachmentPoints(annot) || !quadPoints)
597 return false;
Jane Liu20eafda2017-06-07 10:33:24 -0400598
Jane Liud60e9ad2017-06-26 11:28:36 -0400599 CPDF_Dictionary* pAnnotDict =
600 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
601 if (!pAnnotDict)
Jane Liu0c6b07d2017-08-15 10:50:22 -0400602 return false;
Jane Liud60e9ad2017-06-26 11:28:36 -0400603
Jane Liu06462752017-06-27 16:41:14 -0400604 // If the annotation's appearance stream is defined, then retrieve the
605 // quadpoints defined by the "BBox" entry in the AP dictionary, since its
606 // "BBox" entry comes from annotation dictionary's "QuadPoints" entry, but
607 // takes priority over "QuadPoints" when rendering. Otherwise, retrieve
608 // the "Quadpoints" entry from the annotation dictionary.
609 CPDF_Array* pArray;
Jane Liu06462752017-06-27 16:41:14 -0400610 CPDF_Stream* pStream =
611 FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
612 if (pStream) {
613 pArray = pStream->GetDict()->GetArrayFor("BBox");
614 if (!pArray)
Jane Liu0c6b07d2017-08-15 10:50:22 -0400615 return false;
Jane Liu06462752017-06-27 16:41:14 -0400616
617 // Convert the BBox array into quadpoint coordinates. BBox array follows the
618 // order of a rectangle array: (left, bottom, right, up); and quadpoints
619 // follows the following order: (top-left vertex, top-right vertex, bottom-
620 // left vertex, bottom-right vertex).
Jane Liu0c6b07d2017-08-15 10:50:22 -0400621 quadPoints->x1 = pArray->GetNumberAt(0);
622 quadPoints->y1 = pArray->GetNumberAt(3);
623 quadPoints->x2 = pArray->GetNumberAt(2);
624 quadPoints->y2 = pArray->GetNumberAt(3);
625 quadPoints->x3 = pArray->GetNumberAt(0);
626 quadPoints->y3 = pArray->GetNumberAt(1);
627 quadPoints->x4 = pArray->GetNumberAt(2);
628 quadPoints->y4 = pArray->GetNumberAt(1);
Jane Liu06462752017-06-27 16:41:14 -0400629 } else {
630 pArray = pAnnotDict->GetArrayFor("QuadPoints");
631 if (!pArray)
Jane Liu0c6b07d2017-08-15 10:50:22 -0400632 return false;
Jane Liu06462752017-06-27 16:41:14 -0400633
Jane Liu0c6b07d2017-08-15 10:50:22 -0400634 quadPoints->x1 = pArray->GetNumberAt(0);
635 quadPoints->y1 = pArray->GetNumberAt(1);
636 quadPoints->x2 = pArray->GetNumberAt(2);
637 quadPoints->y2 = pArray->GetNumberAt(3);
638 quadPoints->x3 = pArray->GetNumberAt(4);
639 quadPoints->y3 = pArray->GetNumberAt(5);
640 quadPoints->x4 = pArray->GetNumberAt(6);
641 quadPoints->y4 = pArray->GetNumberAt(7);
Jane Liu06462752017-06-27 16:41:14 -0400642 }
Jane Liu0c6b07d2017-08-15 10:50:22 -0400643 return true;
Jane Liu4fd9a472017-06-01 18:56:09 -0400644}
645
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400646FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetRect(FPDF_ANNOTATION annot,
647 const FS_RECTF* rect) {
Jane Liu06462752017-06-27 16:41:14 -0400648 if (!annot || !rect)
Jane Liud60e9ad2017-06-26 11:28:36 -0400649 return false;
650
Jane Liue10509a2017-06-20 16:47:41 -0400651 CPDF_Dictionary* pAnnotDict =
652 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liu20eafda2017-06-07 10:33:24 -0400653 if (!pAnnotDict)
654 return false;
655
Jane Liu06462752017-06-27 16:41:14 -0400656 CFX_FloatRect newRect(rect->left, rect->bottom, rect->right, rect->top);
Jane Liu20eafda2017-06-07 10:33:24 -0400657
Jane Liu06462752017-06-27 16:41:14 -0400658 // Update the "Rect" entry in the annotation dictionary.
659 pAnnotDict->SetRectFor("Rect", newRect);
660
661 // If the annotation's appearance stream is defined, the annotation is of a
662 // type that does not have quadpoints, and the new rectangle is bigger than
663 // the current bounding box, then update the "BBox" entry in the AP
664 // dictionary too, since its "BBox" entry comes from annotation dictionary's
665 // "Rect" entry.
666 if (FPDFAnnot_HasAttachmentPoints(annot))
667 return true;
668
669 CPDF_Stream* pStream =
670 FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
671 if (pStream && newRect.Contains(pStream->GetDict()->GetRectFor("BBox")))
672 pStream->GetDict()->SetRectFor("BBox", newRect);
Jane Liu20eafda2017-06-07 10:33:24 -0400673 return true;
674}
675
Jane Liu0c6b07d2017-08-15 10:50:22 -0400676FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetRect(FPDF_ANNOTATION annot,
677 FS_RECTF* rect) {
678 if (!annot || !rect)
679 return false;
Jane Liud60e9ad2017-06-26 11:28:36 -0400680
Jane Liue10509a2017-06-20 16:47:41 -0400681 CPDF_Dictionary* pAnnotDict =
682 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liud60e9ad2017-06-26 11:28:36 -0400683 if (!pAnnotDict)
Jane Liu0c6b07d2017-08-15 10:50:22 -0400684 return false;
Jane Liu20eafda2017-06-07 10:33:24 -0400685
Jane Liu06462752017-06-27 16:41:14 -0400686 // If the annotation's appearance stream is defined and the annotation is of
687 // a type that does not have quadpoints, then retrieve the rectangle defined
688 // by the "BBox" entry in the AP dictionary, since its "BBox" entry comes
689 // from annotation dictionary's "Rect" entry, but takes priority over "Rect"
690 // when rendering. Otherwise, retrieve the "Rect" entry from the annotation
691 // dictionary.
692 CFX_FloatRect rt;
693 CPDF_Stream* pStream =
694 FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
695 if (!pStream || FPDFAnnot_HasAttachmentPoints(annot))
696 rt = pAnnotDict->GetRectFor("Rect");
697 else
698 rt = pStream->GetDict()->GetRectFor("BBox");
Jane Liu20eafda2017-06-07 10:33:24 -0400699
Jane Liu0c6b07d2017-08-15 10:50:22 -0400700 rect->left = rt.left;
701 rect->bottom = rt.bottom;
702 rect->right = rt.right;
703 rect->top = rt.top;
704 return true;
Jane Liu4fd9a472017-06-01 18:56:09 -0400705}
706
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400707FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_HasKey(FPDF_ANNOTATION annot,
708 FPDF_WIDESTRING key) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400709 if (!annot)
710 return false;
711
Jane Liue10509a2017-06-20 16:47:41 -0400712 CPDF_Dictionary* pAnnotDict =
713 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liu20eafda2017-06-07 10:33:24 -0400714 if (!pAnnotDict)
715 return false;
716
Jane Liu2e1a32b2017-07-06 12:01:25 -0400717 return pAnnotDict->KeyExist(CFXByteStringFromFPDFWideString(key));
718}
719
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400720FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV
721FPDFAnnot_GetValueType(FPDF_ANNOTATION annot, FPDF_WIDESTRING key) {
Jane Liu2e1a32b2017-07-06 12:01:25 -0400722 if (!FPDFAnnot_HasKey(annot, key))
723 return FPDF_OBJECT_UNKNOWN;
724
725 CPDF_Object* pObj =
726 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict()->GetObjectFor(
727 CFXByteStringFromFPDFWideString(key));
728 if (!pObj)
729 return FPDF_OBJECT_UNKNOWN;
730
731 return pObj->GetType();
732}
733
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400734FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
735FPDFAnnot_SetStringValue(FPDF_ANNOTATION annot,
736 FPDF_WIDESTRING key,
737 FPDF_WIDESTRING value) {
Jane Liu2e1a32b2017-07-06 12:01:25 -0400738 if (!annot)
739 return false;
740
741 CPDF_Dictionary* pAnnotDict =
742 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
743 if (!pAnnotDict)
744 return false;
745
746 pAnnotDict->SetNewFor<CPDF_String>(CFXByteStringFromFPDFWideString(key),
747 CFXByteStringFromFPDFWideString(value),
748 false);
Jane Liu20eafda2017-06-07 10:33:24 -0400749 return true;
750}
751
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400752FPDF_EXPORT unsigned long FPDF_CALLCONV
753FPDFAnnot_GetStringValue(FPDF_ANNOTATION annot,
754 FPDF_WIDESTRING key,
755 void* buffer,
756 unsigned long buflen) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400757 if (!annot)
758 return 0;
759
Jane Liue10509a2017-06-20 16:47:41 -0400760 CPDF_Dictionary* pAnnotDict =
761 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liu4fd9a472017-06-01 18:56:09 -0400762 if (!pAnnotDict)
763 return 0;
Jane Liu20eafda2017-06-07 10:33:24 -0400764
Jane Liu18ae06d2017-07-18 10:15:16 -0400765 return Utf16EncodeMaybeCopyAndReturnLength(
766 pAnnotDict->GetUnicodeTextFor(CFXByteStringFromFPDFWideString(key)),
767 buffer, buflen);
Jane Liu4fd9a472017-06-01 18:56:09 -0400768}
Jane Liub137e752017-07-05 15:04:33 -0400769
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400770FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetFlags(FPDF_ANNOTATION annot) {
Jane Liub137e752017-07-05 15:04:33 -0400771 if (!annot)
772 return FPDF_ANNOT_FLAG_NONE;
773
774 CPDF_Dictionary* pAnnotDict =
775 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
776 if (!pAnnotDict)
777 return FPDF_ANNOT_FLAG_NONE;
778
779 return pAnnotDict->GetIntegerFor("F");
780}
781
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400782FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetFlags(FPDF_ANNOTATION annot,
783 int flags) {
Jane Liub137e752017-07-05 15:04:33 -0400784 if (!annot)
785 return false;
786
787 CPDF_Dictionary* pAnnotDict =
788 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
789 if (!pAnnotDict)
790 return false;
791
792 pAnnotDict->SetNewFor<CPDF_Number>("F", flags);
793 return true;
794}
Diana Gage7e0c05d2017-07-19 17:33:33 -0700795
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400796FPDF_EXPORT int FPDF_CALLCONV
797FPDFAnnot_GetFormFieldFlags(FPDF_PAGE page, FPDF_ANNOTATION annot) {
Diana Gage7e0c05d2017-07-19 17:33:33 -0700798 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
799 if (!pPage || !annot)
800 return FPDF_FORMFLAG_NONE;
801
802 CPDF_Dictionary* pAnnotDict =
803 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
804 if (!pAnnotDict)
805 return FPDF_FORMFLAG_NONE;
806
807 CPDF_InterForm interform(pPage->m_pDocument.Get());
808 CPDF_FormField* pFormField = interform.GetFieldByDict(pAnnotDict);
809 return pFormField ? pFormField->GetFieldFlags() : FPDF_FORMFLAG_NONE;
810}
Diana Gage40870db2017-07-19 18:16:03 -0700811
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400812FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV
Diana Gage40870db2017-07-19 18:16:03 -0700813FPDFAnnot_GetFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
814 FPDF_PAGE page,
815 double page_x,
816 double page_y) {
817 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
818 if (!hHandle || !pPage)
819 return nullptr;
820
821 CPDF_InterForm interform(pPage->m_pDocument.Get());
822 int annot_index = -1;
823 CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
824 pPage, CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
825 &annot_index);
826 if (!pFormCtrl || annot_index == -1)
827 return nullptr;
828 return FPDFPage_GetAnnot(page, annot_index);
829}