blob: a671032b1e8d2afd1b77e56661743bb7a20ab8d4 [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_generateap.h"
Dan Sinclair8e7f9322017-10-16 11:35:42 -040024#include "core/fxge/cfx_color.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040025#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);
Lei Zhangaf59cf12017-08-28 18:09:38 -0700154 m_pAnnotForm->ParseContent();
Jane Liue10509a2017-06-20 16:47:41 -0400155 }
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;
Dan Sinclairaee0db02017-09-21 16:53:58 -0400163 UnownedPtr<CPDF_Dictionary> m_pAnnotDict;
164 UnownedPtr<CPDF_Page> m_pPage;
Jane Liue10509a2017-06-20 16:47:41 -0400165};
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
Jane Liud1ed1ce2017-08-24 12:31:10 -0400244FPDF_EXPORT int FPDF_CALLCONV FPDFPage_GetAnnotIndex(FPDF_PAGE page,
245 FPDF_ANNOTATION annot) {
246 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
247 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
248 if (!pPage || !pPage->m_pFormDict || !pAnnot || !pAnnot->GetAnnotDict())
249 return -1;
250
251 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
252 if (!pAnnots)
253 return -1;
254
255 CPDF_Dictionary* pDict = pAnnot->GetAnnotDict();
256 auto it =
257 std::find_if(pAnnots->begin(), pAnnots->end(),
258 [pDict](const std::unique_ptr<CPDF_Object>& candidate) {
259 return candidate->GetDirect() == pDict;
260 });
261
262 if (it == pAnnots->end())
263 return -1;
264
265 return it - pAnnots->begin();
266}
267
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400268FPDF_EXPORT void FPDF_CALLCONV FPDFPage_CloseAnnot(FPDF_ANNOTATION annot) {
Jane Liue10509a2017-06-20 16:47:41 -0400269 delete CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400270}
271
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400272FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_RemoveAnnot(FPDF_PAGE page,
273 int index) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400274 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
275 if (!pPage || !pPage->m_pFormDict || index < 0)
276 return false;
277
278 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
279 if (!pAnnots || static_cast<size_t>(index) >= pAnnots->GetCount())
280 return false;
281
282 pAnnots->RemoveAt(index);
283 return true;
284}
285
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400286FPDF_EXPORT FPDF_ANNOTATION_SUBTYPE FPDF_CALLCONV
Jane Liu4fd9a472017-06-01 18:56:09 -0400287FPDFAnnot_GetSubtype(FPDF_ANNOTATION annot) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400288 if (!annot)
289 return FPDF_ANNOT_UNKNOWN;
290
Jane Liue10509a2017-06-20 16:47:41 -0400291 CPDF_Dictionary* pAnnotDict =
292 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liu4fd9a472017-06-01 18:56:09 -0400293 if (!pAnnotDict)
294 return FPDF_ANNOT_UNKNOWN;
Jane Liu20eafda2017-06-07 10:33:24 -0400295
Jane Liu4fd9a472017-06-01 18:56:09 -0400296 return static_cast<FPDF_ANNOTATION_SUBTYPE>(
297 CPDF_Annot::StringToAnnotSubtype(pAnnotDict->GetStringFor("Subtype")));
298}
299
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400300FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu7a9a38b2017-07-11 13:47:37 -0400301FPDFAnnot_IsObjectSupportedSubtype(FPDF_ANNOTATION_SUBTYPE subtype) {
302 // The supported subtypes must also be communicated in the user doc.
303 return subtype == FPDF_ANNOT_INK || subtype == FPDF_ANNOT_STAMP;
304}
305
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400306FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
307FPDFAnnot_UpdateObject(FPDF_ANNOTATION annot, FPDF_PAGEOBJECT obj) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400308 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu36567742017-07-06 11:13:35 -0400309 CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(obj);
310 if (!pAnnot || !pAnnot->GetAnnotDict() || !pAnnot->HasForm() || !pObj)
Jane Liubaa7ff42017-06-29 19:18:23 -0400311 return false;
312
313 // Check that the annotation type is supported by this method.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400314 if (!FPDFAnnot_IsObjectSupportedSubtype(FPDFAnnot_GetSubtype(annot)))
Jane Liubaa7ff42017-06-29 19:18:23 -0400315 return false;
316
317 // Check that the annotation already has an appearance stream, since an
Jane Liu36567742017-07-06 11:13:35 -0400318 // existing object is to be updated.
Jane Liubaa7ff42017-06-29 19:18:23 -0400319 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(pAnnot->GetAnnotDict(),
320 CPDF_Annot::AppearanceMode::Normal);
321 if (!pStream)
322 return false;
323
Jane Liu36567742017-07-06 11:13:35 -0400324 // Check that the object is already in this annotation's object list.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400325 CPDF_Form* pForm = pAnnot->GetForm();
326 CPDF_PageObjectList* pObjList = pForm->GetPageObjectList();
Jane Liu36567742017-07-06 11:13:35 -0400327 auto it =
328 std::find_if(pObjList->begin(), pObjList->end(),
329 [pObj](const std::unique_ptr<CPDF_PageObject>& candidate) {
330 return candidate.get() == pObj;
331 });
Jane Liubaa7ff42017-06-29 19:18:23 -0400332 if (it == pObjList->end())
333 return false;
334
335 // Update the content stream data in the annotation's AP stream.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400336 UpdateContentStream(pForm, pStream);
Jane Liubaa7ff42017-06-29 19:18:23 -0400337 return true;
338}
339
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400340FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
341FPDFAnnot_AppendObject(FPDF_ANNOTATION annot, FPDF_PAGEOBJECT obj) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400342 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu36567742017-07-06 11:13:35 -0400343 CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(obj);
344 if (!pAnnot || !pObj)
Jane Liubaa7ff42017-06-29 19:18:23 -0400345 return false;
346
347 CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
348 CPDF_Page* pPage = pAnnot->GetPage();
349 if (!pAnnotDict || !pPage)
350 return false;
351
352 // Check that the annotation type is supported by this method.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400353 if (!FPDFAnnot_IsObjectSupportedSubtype(FPDFAnnot_GetSubtype(annot)))
Jane Liubaa7ff42017-06-29 19:18:23 -0400354 return false;
355
356 // If the annotation does not have an AP stream yet, generate and set it.
357 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(pAnnot->GetAnnotDict(),
358 CPDF_Annot::AppearanceMode::Normal);
359 if (!pStream) {
360 auto pExtGStateDict =
361 CPVT_GenerateAP::GenerateExtGStateDict(*pAnnotDict, "GS", "Normal");
362 auto pResourceDict = CPVT_GenerateAP::GenerateResourceDict(
363 pPage->m_pDocument.Get(), std::move(pExtGStateDict), nullptr);
364 std::ostringstream sStream;
365 CPVT_GenerateAP::GenerateAndSetAPDict(pPage->m_pDocument.Get(), pAnnotDict,
366 &sStream, std::move(pResourceDict),
367 false);
368 pStream =
369 FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
370 if (!pStream)
371 return false;
372 }
373
374 // Get the annotation's corresponding form object for parsing its AP stream.
375 if (!pAnnot->HasForm())
376 pAnnot->SetForm(pStream);
377
Jane Liu36567742017-07-06 11:13:35 -0400378 // Check that the object did not come from the same annotation. If this check
379 // succeeds, then it is assumed that the object came from
380 // FPDFPageObj_CreateNew{Path|Rect}() or FPDFPageObj_New{Text|Image}Obj().
381 // Note that an object that came from a different annotation must not be
382 // passed here, since an object cannot belong to more than one annotation.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400383 CPDF_Form* pForm = pAnnot->GetForm();
Jane Liubaa7ff42017-06-29 19:18:23 -0400384 CPDF_PageObjectList* pObjList = pForm->GetPageObjectList();
Jane Liu36567742017-07-06 11:13:35 -0400385 auto it =
386 std::find_if(pObjList->begin(), pObjList->end(),
387 [pObj](const std::unique_ptr<CPDF_PageObject>& candidate) {
388 return candidate.get() == pObj;
389 });
Jane Liubaa7ff42017-06-29 19:18:23 -0400390 if (it != pObjList->end())
391 return false;
392
Jane Liu36567742017-07-06 11:13:35 -0400393 // Append the object to the object list.
394 std::unique_ptr<CPDF_PageObject> pPageObjHolder(pObj);
Jane Liubaa7ff42017-06-29 19:18:23 -0400395 pObjList->push_back(std::move(pPageObjHolder));
396
397 // Set the content stream data in the annotation's AP stream.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400398 UpdateContentStream(pForm, pStream);
Jane Liubaa7ff42017-06-29 19:18:23 -0400399 return true;
400}
401
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400402FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetObjectCount(FPDF_ANNOTATION annot) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400403 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu36567742017-07-06 11:13:35 -0400404 if (!pAnnot || !pAnnot->GetAnnotDict())
Jane Liubaa7ff42017-06-29 19:18:23 -0400405 return 0;
406
407 if (!pAnnot->HasForm()) {
408 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(
409 pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::Normal);
410 if (!pStream)
411 return 0;
412
413 pAnnot->SetForm(pStream);
414 }
Jane Liu57f228d2017-07-13 18:10:30 -0400415 return pdfium::CollectionSize<int>(*pAnnot->GetForm()->GetPageObjectList());
Jane Liubaa7ff42017-06-29 19:18:23 -0400416}
417
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400418FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV
419FPDFAnnot_GetObject(FPDF_ANNOTATION annot, int index) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400420 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
Jane Liu36567742017-07-06 11:13:35 -0400421 if (!pAnnot || !pAnnot->GetAnnotDict() || index < 0)
Jane Liubaa7ff42017-06-29 19:18:23 -0400422 return nullptr;
423
424 if (!pAnnot->HasForm()) {
425 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(
426 pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::Normal);
427 if (!pStream)
428 return nullptr;
429
430 pAnnot->SetForm(pStream);
431 }
432
Jane Liu36567742017-07-06 11:13:35 -0400433 return pAnnot->GetForm()->GetPageObjectList()->GetPageObjectByIndex(index);
Jane Liubaa7ff42017-06-29 19:18:23 -0400434}
435
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400436FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
437FPDFAnnot_RemoveObject(FPDF_ANNOTATION annot, int index) {
Jane Liu7a9a38b2017-07-11 13:47:37 -0400438 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
439 if (!pAnnot || !pAnnot->GetAnnotDict() || !pAnnot->HasForm() || index < 0)
440 return false;
441
442 // Check that the annotation type is supported by this method.
443 if (!FPDFAnnot_IsObjectSupportedSubtype(FPDFAnnot_GetSubtype(annot)))
444 return false;
445
446 // Check that the annotation already has an appearance stream, since an
447 // existing object is to be deleted.
448 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(pAnnot->GetAnnotDict(),
449 CPDF_Annot::AppearanceMode::Normal);
450 if (!pStream)
451 return false;
452
453 CPDF_PageObjectList* pObjList = pAnnot->GetForm()->GetPageObjectList();
454 if (static_cast<size_t>(index) >= pObjList->size())
455 return false;
456
457 pObjList->erase(pObjList->begin() + index);
458 UpdateContentStream(pAnnot->GetForm(), pStream);
459 return true;
460}
461
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400462FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetColor(FPDF_ANNOTATION annot,
463 FPDFANNOT_COLORTYPE type,
464 unsigned int R,
465 unsigned int G,
466 unsigned int B,
467 unsigned int A) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400468 if (!annot || R > 255 || G > 255 || B > 255 || A > 255)
469 return false;
470
Jane Liue10509a2017-06-20 16:47:41 -0400471 CPDF_Dictionary* pAnnotDict =
472 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liud60e9ad2017-06-26 11:28:36 -0400473 if (!pAnnotDict)
Jane Liu20eafda2017-06-07 10:33:24 -0400474 return false;
475
Jane Liu06462752017-06-27 16:41:14 -0400476 // For annotations with their appearance streams already defined, the path
477 // stream's own color definitions take priority over the annotation color
478 // definitions set by this method, hence this method will simply fail.
479 if (HasAPStream(pAnnotDict))
480 return false;
481
Jane Liu20eafda2017-06-07 10:33:24 -0400482 // Set the opacity of the annotation.
483 pAnnotDict->SetNewFor<CPDF_Number>("CA", A / 255.f);
484
485 // Set the color of the annotation.
Ryan Harrison275e2602017-09-18 14:23:18 -0400486 ByteString key = type == FPDFANNOT_COLORTYPE_InteriorColor ? "IC" : "C";
Jane Liu20eafda2017-06-07 10:33:24 -0400487 CPDF_Array* pColor = pAnnotDict->GetArrayFor(key);
488 if (pColor)
Lei Zhang59c1ac02017-06-09 11:04:41 -0700489 pColor->Clear();
Jane Liu20eafda2017-06-07 10:33:24 -0400490 else
491 pColor = pAnnotDict->SetNewFor<CPDF_Array>(key);
492
493 pColor->AddNew<CPDF_Number>(R / 255.f);
494 pColor->AddNew<CPDF_Number>(G / 255.f);
495 pColor->AddNew<CPDF_Number>(B / 255.f);
496
497 return true;
498}
499
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400500FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetColor(FPDF_ANNOTATION annot,
501 FPDFANNOT_COLORTYPE type,
502 unsigned int* R,
503 unsigned int* G,
504 unsigned int* B,
505 unsigned int* A) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400506 if (!annot || !R || !G || !B || !A)
507 return false;
508
Jane Liue10509a2017-06-20 16:47:41 -0400509 CPDF_Dictionary* pAnnotDict =
510 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liud60e9ad2017-06-26 11:28:36 -0400511 if (!pAnnotDict)
Jane Liu4fd9a472017-06-01 18:56:09 -0400512 return false;
513
Jane Liu06462752017-06-27 16:41:14 -0400514 // For annotations with their appearance streams already defined, the path
515 // stream's own color definitions take priority over the annotation color
516 // definitions retrieved by this method, hence this method will simply fail.
517 if (HasAPStream(pAnnotDict))
518 return false;
519
Jane Liu4fd9a472017-06-01 18:56:09 -0400520 CPDF_Array* pColor = pAnnotDict->GetArrayFor(
521 type == FPDFANNOT_COLORTYPE_InteriorColor ? "IC" : "C");
522 *A =
523 (pAnnotDict->KeyExist("CA") ? pAnnotDict->GetNumberFor("CA") : 1) * 255.f;
524 if (!pColor) {
525 // Use default color. The default colors must be consistent with the ones
526 // used to generate AP. See calls to GetColorStringWithDefault() in
527 // CPVT_GenerateAP::Generate*AP().
528 if (pAnnotDict->GetStringFor("Subtype") == "Highlight") {
529 *R = 255;
530 *G = 255;
531 *B = 0;
532 } else {
533 *R = 0;
534 *G = 0;
535 *B = 0;
536 }
537 return true;
538 }
Dan Sinclair8e7f9322017-10-16 11:35:42 -0400539
540 CFX_Color color = CFX_Color::ParseColor(*pColor);
Jane Liu4fd9a472017-06-01 18:56:09 -0400541 switch (color.nColorType) {
Dan Sinclair8e7f9322017-10-16 11:35:42 -0400542 case CFX_Color::kRGB:
Jane Liu4fd9a472017-06-01 18:56:09 -0400543 *R = color.fColor1 * 255.f;
544 *G = color.fColor2 * 255.f;
545 *B = color.fColor3 * 255.f;
546 break;
Dan Sinclair8e7f9322017-10-16 11:35:42 -0400547 case CFX_Color::kGray:
Jane Liu4fd9a472017-06-01 18:56:09 -0400548 *R = 255.f * color.fColor1;
549 *G = 255.f * color.fColor1;
550 *B = 255.f * color.fColor1;
551 break;
Dan Sinclair8e7f9322017-10-16 11:35:42 -0400552 case CFX_Color::kCMYK:
Jane Liu4fd9a472017-06-01 18:56:09 -0400553 *R = 255.f * (1 - color.fColor1) * (1 - color.fColor4);
554 *G = 255.f * (1 - color.fColor2) * (1 - color.fColor4);
555 *B = 255.f * (1 - color.fColor3) * (1 - color.fColor4);
556 break;
Dan Sinclair8e7f9322017-10-16 11:35:42 -0400557 case CFX_Color::kTransparent:
Jane Liu4fd9a472017-06-01 18:56:09 -0400558 *R = 0;
559 *G = 0;
560 *B = 0;
561 break;
562 }
563 return true;
564}
565
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400566FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu4fd9a472017-06-01 18:56:09 -0400567FPDFAnnot_HasAttachmentPoints(FPDF_ANNOTATION annot) {
568 if (!annot)
569 return false;
Jane Liu20eafda2017-06-07 10:33:24 -0400570
Jane Liu4fd9a472017-06-01 18:56:09 -0400571 FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot);
572 return subtype == FPDF_ANNOT_LINK || subtype == FPDF_ANNOT_HIGHLIGHT ||
573 subtype == FPDF_ANNOT_UNDERLINE || subtype == FPDF_ANNOT_SQUIGGLY ||
574 subtype == FPDF_ANNOT_STRIKEOUT;
575}
576
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400577FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu20eafda2017-06-07 10:33:24 -0400578FPDFAnnot_SetAttachmentPoints(FPDF_ANNOTATION annot,
Jane Liu06462752017-06-27 16:41:14 -0400579 const FS_QUADPOINTSF* quadPoints) {
580 if (!annot || !quadPoints || !FPDFAnnot_HasAttachmentPoints(annot))
Jane Liu20eafda2017-06-07 10:33:24 -0400581 return false;
582
Jane Liue10509a2017-06-20 16:47:41 -0400583 CPDF_Dictionary* pAnnotDict =
584 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liud60e9ad2017-06-26 11:28:36 -0400585 if (!pAnnotDict)
586 return false;
587
Jane Liu06462752017-06-27 16:41:14 -0400588 // Update the "QuadPoints" entry in the annotation dictionary.
Jane Liu20eafda2017-06-07 10:33:24 -0400589 CPDF_Array* pQuadPoints = pAnnotDict->GetArrayFor("QuadPoints");
590 if (pQuadPoints)
Lei Zhang59c1ac02017-06-09 11:04:41 -0700591 pQuadPoints->Clear();
Jane Liu20eafda2017-06-07 10:33:24 -0400592 else
593 pQuadPoints = pAnnotDict->SetNewFor<CPDF_Array>("QuadPoints");
594
Jane Liu06462752017-06-27 16:41:14 -0400595 pQuadPoints->AddNew<CPDF_Number>(quadPoints->x1);
596 pQuadPoints->AddNew<CPDF_Number>(quadPoints->y1);
597 pQuadPoints->AddNew<CPDF_Number>(quadPoints->x2);
598 pQuadPoints->AddNew<CPDF_Number>(quadPoints->y2);
599 pQuadPoints->AddNew<CPDF_Number>(quadPoints->x3);
600 pQuadPoints->AddNew<CPDF_Number>(quadPoints->y3);
601 pQuadPoints->AddNew<CPDF_Number>(quadPoints->x4);
602 pQuadPoints->AddNew<CPDF_Number>(quadPoints->y4);
603
604 // If the annotation's appearance stream is defined, and the new quadpoints
605 // defines a bigger bounding box than the appearance stream currently
606 // specifies, then update the "BBox" entry in the AP dictionary too, since it
607 // comes from annotation dictionary's "QuadPoints" entry.
608 CPDF_Stream* pStream =
609 FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
610 if (pStream) {
611 CFX_FloatRect newRect = CPDF_Annot::RectFromQuadPoints(pAnnotDict);
612 if (newRect.Contains(pStream->GetDict()->GetRectFor("BBox")))
613 pStream->GetDict()->SetRectFor("BBox", newRect);
614 }
Jane Liu20eafda2017-06-07 10:33:24 -0400615 return true;
616}
617
Jane Liu0c6b07d2017-08-15 10:50:22 -0400618FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
619FPDFAnnot_GetAttachmentPoints(FPDF_ANNOTATION annot,
620 FS_QUADPOINTSF* quadPoints) {
621 if (!annot || !FPDFAnnot_HasAttachmentPoints(annot) || !quadPoints)
622 return false;
Jane Liu20eafda2017-06-07 10:33:24 -0400623
Jane Liud60e9ad2017-06-26 11:28:36 -0400624 CPDF_Dictionary* pAnnotDict =
625 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
626 if (!pAnnotDict)
Jane Liu0c6b07d2017-08-15 10:50:22 -0400627 return false;
Jane Liud60e9ad2017-06-26 11:28:36 -0400628
Jane Liub370e5a2017-08-16 13:24:58 -0400629 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
630 if (!pArray)
631 return false;
Jane Liu06462752017-06-27 16:41:14 -0400632
Jane Liub370e5a2017-08-16 13:24:58 -0400633 quadPoints->x1 = pArray->GetNumberAt(0);
634 quadPoints->y1 = pArray->GetNumberAt(1);
635 quadPoints->x2 = pArray->GetNumberAt(2);
636 quadPoints->y2 = pArray->GetNumberAt(3);
637 quadPoints->x3 = pArray->GetNumberAt(4);
638 quadPoints->y3 = pArray->GetNumberAt(5);
639 quadPoints->x4 = pArray->GetNumberAt(6);
640 quadPoints->y4 = pArray->GetNumberAt(7);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400641 return true;
Jane Liu4fd9a472017-06-01 18:56:09 -0400642}
643
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400644FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetRect(FPDF_ANNOTATION annot,
645 const FS_RECTF* rect) {
Jane Liu06462752017-06-27 16:41:14 -0400646 if (!annot || !rect)
Jane Liud60e9ad2017-06-26 11:28:36 -0400647 return false;
648
Jane Liue10509a2017-06-20 16:47:41 -0400649 CPDF_Dictionary* pAnnotDict =
650 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liu20eafda2017-06-07 10:33:24 -0400651 if (!pAnnotDict)
652 return false;
653
Jane Liu06462752017-06-27 16:41:14 -0400654 CFX_FloatRect newRect(rect->left, rect->bottom, rect->right, rect->top);
Jane Liu20eafda2017-06-07 10:33:24 -0400655
Jane Liu06462752017-06-27 16:41:14 -0400656 // Update the "Rect" entry in the annotation dictionary.
657 pAnnotDict->SetRectFor("Rect", newRect);
658
659 // If the annotation's appearance stream is defined, the annotation is of a
660 // type that does not have quadpoints, and the new rectangle is bigger than
661 // the current bounding box, then update the "BBox" entry in the AP
662 // dictionary too, since its "BBox" entry comes from annotation dictionary's
663 // "Rect" entry.
664 if (FPDFAnnot_HasAttachmentPoints(annot))
665 return true;
666
667 CPDF_Stream* pStream =
668 FPDFDOC_GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
669 if (pStream && newRect.Contains(pStream->GetDict()->GetRectFor("BBox")))
670 pStream->GetDict()->SetRectFor("BBox", newRect);
Jane Liu20eafda2017-06-07 10:33:24 -0400671 return true;
672}
673
Jane Liu0c6b07d2017-08-15 10:50:22 -0400674FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetRect(FPDF_ANNOTATION annot,
675 FS_RECTF* rect) {
676 if (!annot || !rect)
677 return false;
Jane Liud60e9ad2017-06-26 11:28:36 -0400678
Jane Liue10509a2017-06-20 16:47:41 -0400679 CPDF_Dictionary* pAnnotDict =
680 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liud60e9ad2017-06-26 11:28:36 -0400681 if (!pAnnotDict)
Jane Liu0c6b07d2017-08-15 10:50:22 -0400682 return false;
Jane Liu20eafda2017-06-07 10:33:24 -0400683
Jane Liub370e5a2017-08-16 13:24:58 -0400684 CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect");
Jane Liu0c6b07d2017-08-15 10:50:22 -0400685 rect->left = rt.left;
686 rect->bottom = rt.bottom;
687 rect->right = rt.right;
688 rect->top = rt.top;
689 return true;
Jane Liu4fd9a472017-06-01 18:56:09 -0400690}
691
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400692FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_HasKey(FPDF_ANNOTATION annot,
Lei Zhangdf064df2017-08-31 02:33:27 -0700693 FPDF_BYTESTRING key) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400694 if (!annot)
695 return false;
696
Jane Liue10509a2017-06-20 16:47:41 -0400697 CPDF_Dictionary* pAnnotDict =
698 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liu20eafda2017-06-07 10:33:24 -0400699 if (!pAnnotDict)
700 return false;
701
Lei Zhangdf064df2017-08-31 02:33:27 -0700702 return pAnnotDict->KeyExist(key);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400703}
704
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400705FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV
Lei Zhangdf064df2017-08-31 02:33:27 -0700706FPDFAnnot_GetValueType(FPDF_ANNOTATION annot, FPDF_BYTESTRING key) {
Jane Liu2e1a32b2017-07-06 12:01:25 -0400707 if (!FPDFAnnot_HasKey(annot, key))
708 return FPDF_OBJECT_UNKNOWN;
709
Lei Zhangdf064df2017-08-31 02:33:27 -0700710 auto* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
711 CPDF_Object* pObj = pAnnot->GetAnnotDict()->GetObjectFor(key);
712 return pObj ? pObj->GetType() : FPDF_OBJECT_UNKNOWN;
Jane Liu2e1a32b2017-07-06 12:01:25 -0400713}
714
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400715FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
716FPDFAnnot_SetStringValue(FPDF_ANNOTATION annot,
Lei Zhangdf064df2017-08-31 02:33:27 -0700717 FPDF_BYTESTRING key,
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400718 FPDF_WIDESTRING value) {
Jane Liu2e1a32b2017-07-06 12:01:25 -0400719 if (!annot)
720 return false;
721
722 CPDF_Dictionary* pAnnotDict =
723 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
724 if (!pAnnotDict)
725 return false;
726
Lei Zhangdf064df2017-08-31 02:33:27 -0700727 pAnnotDict->SetNewFor<CPDF_String>(
728 key, CFXByteStringFromFPDFWideString(value), false);
Jane Liu20eafda2017-06-07 10:33:24 -0400729 return true;
730}
731
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400732FPDF_EXPORT unsigned long FPDF_CALLCONV
733FPDFAnnot_GetStringValue(FPDF_ANNOTATION annot,
Lei Zhangdf064df2017-08-31 02:33:27 -0700734 FPDF_BYTESTRING key,
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400735 void* buffer,
736 unsigned long buflen) {
Jane Liud60e9ad2017-06-26 11:28:36 -0400737 if (!annot)
738 return 0;
739
Jane Liue10509a2017-06-20 16:47:41 -0400740 CPDF_Dictionary* pAnnotDict =
741 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Jane Liu4fd9a472017-06-01 18:56:09 -0400742 if (!pAnnotDict)
743 return 0;
Jane Liu20eafda2017-06-07 10:33:24 -0400744
Lei Zhangdf064df2017-08-31 02:33:27 -0700745 return Utf16EncodeMaybeCopyAndReturnLength(pAnnotDict->GetUnicodeTextFor(key),
746 buffer, buflen);
Jane Liu4fd9a472017-06-01 18:56:09 -0400747}
Jane Liub137e752017-07-05 15:04:33 -0400748
Jane Liu300bb272017-08-21 14:37:53 -0400749FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV
Lei Zhangdf064df2017-08-31 02:33:27 -0700750FPDFAnnot_GetLinkedAnnot(FPDF_ANNOTATION annot, FPDF_BYTESTRING key) {
Jane Liu300bb272017-08-21 14:37:53 -0400751 CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
752 if (!pAnnot || !pAnnot->GetAnnotDict())
753 return nullptr;
754
Lei Zhangdf064df2017-08-31 02:33:27 -0700755 CPDF_Dictionary* pLinkedDict = pAnnot->GetAnnotDict()->GetDictFor(key);
Jane Liu300bb272017-08-21 14:37:53 -0400756 if (!pLinkedDict || pLinkedDict->GetStringFor("Type") != "Annot")
757 return nullptr;
758
759 auto pLinkedAnnot = pdfium::MakeUnique<CPDF_AnnotContext>(
760 pLinkedDict, pAnnot->GetPage(), nullptr);
761 return pLinkedAnnot.release();
762}
763
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400764FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetFlags(FPDF_ANNOTATION annot) {
Jane Liub137e752017-07-05 15:04:33 -0400765 if (!annot)
766 return FPDF_ANNOT_FLAG_NONE;
767
768 CPDF_Dictionary* pAnnotDict =
769 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
Lei Zhangdf064df2017-08-31 02:33:27 -0700770 return pAnnotDict ? pAnnotDict->GetIntegerFor("F") : FPDF_ANNOT_FLAG_NONE;
Jane Liub137e752017-07-05 15:04:33 -0400771}
772
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400773FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetFlags(FPDF_ANNOTATION annot,
774 int flags) {
Jane Liub137e752017-07-05 15:04:33 -0400775 if (!annot)
776 return false;
777
778 CPDF_Dictionary* pAnnotDict =
779 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
780 if (!pAnnotDict)
781 return false;
782
783 pAnnotDict->SetNewFor<CPDF_Number>("F", flags);
784 return true;
785}
Diana Gage7e0c05d2017-07-19 17:33:33 -0700786
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400787FPDF_EXPORT int FPDF_CALLCONV
788FPDFAnnot_GetFormFieldFlags(FPDF_PAGE page, FPDF_ANNOTATION annot) {
Diana Gage7e0c05d2017-07-19 17:33:33 -0700789 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
790 if (!pPage || !annot)
791 return FPDF_FORMFLAG_NONE;
792
793 CPDF_Dictionary* pAnnotDict =
794 CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
795 if (!pAnnotDict)
796 return FPDF_FORMFLAG_NONE;
797
798 CPDF_InterForm interform(pPage->m_pDocument.Get());
799 CPDF_FormField* pFormField = interform.GetFieldByDict(pAnnotDict);
800 return pFormField ? pFormField->GetFieldFlags() : FPDF_FORMFLAG_NONE;
801}
Diana Gage40870db2017-07-19 18:16:03 -0700802
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400803FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV
Diana Gage40870db2017-07-19 18:16:03 -0700804FPDFAnnot_GetFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
805 FPDF_PAGE page,
806 double page_x,
807 double page_y) {
808 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
809 if (!hHandle || !pPage)
810 return nullptr;
811
812 CPDF_InterForm interform(pPage->m_pDocument.Get());
813 int annot_index = -1;
814 CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
815 pPage, CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
816 &annot_index);
817 if (!pFormCtrl || annot_index == -1)
818 return nullptr;
819 return FPDFPage_GetAnnot(page, annot_index);
820}