summaryrefslogtreecommitdiff
path: root/core/src/fpdfdoc/doc_formcontrol.cpp
blob: db7cf37fdb5e659de981fd1a55777d5246ca9978 (plain)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2014 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 "../../include/fpdfdoc/fpdf_doc.h"
CPDF_FormControl::CPDF_FormControl(CPDF_FormField* pField,
                                   CPDF_Dictionary* pWidgetDict) {
  m_pField = pField;
  m_pWidgetDict = pWidgetDict;
  m_pForm = m_pField->m_pForm;
}
CFX_FloatRect CPDF_FormControl::GetRect() {
  return m_pWidgetDict->GetRect("Rect");
}
CFX_ByteString CPDF_FormControl::GetOnStateName() {
  ASSERT(GetType() == CPDF_FormField::CheckBox ||
         GetType() == CPDF_FormField::RadioButton);
  CFX_ByteString csOn;
  CPDF_Dictionary* pAP = m_pWidgetDict->GetDict("AP");
  if (pAP == NULL) {
    return csOn;
  }
  CPDF_Dictionary* pN = pAP->GetDict("N");
  if (pN == NULL) {
    return csOn;
  }
  FX_POSITION pos = pN->GetStartPos();
  while (pos) {
    pN->GetNextElement(pos, csOn);
    if (csOn != "Off") {
      return csOn;
    }
  }
  return CFX_ByteString();
}
void CPDF_FormControl::SetOnStateName(const CFX_ByteString& csOn) {
  ASSERT(GetType() == CPDF_FormField::CheckBox ||
         GetType() == CPDF_FormField::RadioButton);
  CFX_ByteString csValue = csOn;
  if (csValue.IsEmpty()) {
    csValue = "Yes";
  }
  if (csValue == "Off") {
    csValue = "Yes";
  }
  CFX_ByteString csAS = m_pWidgetDict->GetString("AS", "Off");
  if (csAS != "Off") {
    m_pWidgetDict->SetAtName("AS", csValue);
  }
  CPDF_Dictionary* pAP = m_pWidgetDict->GetDict("AP");
  if (pAP == NULL) {
    return;
  }
  FX_POSITION pos1 = pAP->GetStartPos();
  while (pos1) {
    CFX_ByteString csKey1;
    CPDF_Object* pObj1 = pAP->GetNextElement(pos1, csKey1);
    if (pObj1 == NULL) {
      continue;
    }
    CPDF_Object* pObjDirect1 = pObj1->GetDirect();
    if (pObjDirect1->GetType() != PDFOBJ_DICTIONARY) {
      continue;
    }
    CPDF_Dictionary* pSubDict = (CPDF_Dictionary*)pObjDirect1;
    FX_POSITION pos2 = pSubDict->GetStartPos();
    while (pos2) {
      CFX_ByteString csKey2;
      CPDF_Object* pObj2 = pSubDict->GetNextElement(pos2, csKey2);
      if (pObj2 == NULL) {
        continue;
      }
      if (csKey2 != "Off") {
        pSubDict->ReplaceKey(csKey2, csValue);
        break;
      }
    }
  }
}
CFX_ByteString CPDF_FormControl::GetCheckedAPState() {
  ASSERT(GetType() == CPDF_FormField::CheckBox ||
         GetType() == CPDF_FormField::RadioButton);
  CFX_ByteString csOn = GetOnStateName();
  if (GetType() == CPDF_FormField::RadioButton ||
      GetType() == CPDF_FormField::CheckBox) {
    CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pField->m_pDict, "Opt");
    if (pOpt != NULL && pOpt->GetType() == PDFOBJ_ARRAY) {
      int iIndex = m_pField->GetControlIndex(this);
      csOn.Format("%d", iIndex);
    }
  }
  if (csOn.IsEmpty()) {
    csOn = "Yes";
  }
  return csOn;
}
CFX_WideString CPDF_FormControl::GetExportValue() {
  ASSERT(GetType() == CPDF_FormField::CheckBox ||
         GetType() == CPDF_FormField::RadioButton);
  CFX_ByteString csOn = GetOnStateName();
  if (GetType() == CPDF_FormField::RadioButton ||
      GetType() == CPDF_FormField::CheckBox) {
    CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pField->m_pDict, "Opt");
    if (pOpt != NULL && pOpt->GetType() == PDFOBJ_ARRAY) {
      int iIndex = m_pField->GetControlIndex(this);
      csOn = ((CPDF_Array*)pOpt)->GetString(iIndex);
    }
  }
  if (csOn.IsEmpty()) {
    csOn = "Yes";
  }
  CFX_WideString csWOn = PDF_DecodeText(csOn);
  return csWOn;
}
FX_BOOL CPDF_FormControl::IsChecked() {
  ASSERT(GetType() == CPDF_FormField::CheckBox ||
         GetType() == CPDF_FormField::RadioButton);
  CFX_ByteString csOn = GetOnStateName();
  CFX_ByteString csAS = m_pWidgetDict->GetString("AS");
  return csAS == csOn;
}
FX_BOOL CPDF_FormControl::IsDefaultChecked() {
  ASSERT(GetType() == CPDF_FormField::CheckBox ||
         GetType() == CPDF_FormField::RadioButton);
  CPDF_Object* pDV = FPDF_GetFieldAttr(m_pField->m_pDict, "DV");
  if (pDV == NULL) {
    return FALSE;
  }
  CFX_ByteString csDV = pDV->GetString();
  CFX_ByteString csOn = GetOnStateName();
  return (csDV == csOn);
}
void CPDF_FormControl::CheckControl(FX_BOOL bChecked) {
  ASSERT(GetType() == CPDF_FormField::CheckBox ||
         GetType() == CPDF_FormField::RadioButton);
  CFX_ByteString csOn = GetOnStateName();
  CFX_ByteString csOldAS = m_pWidgetDict->GetString("AS", "Off");
  CFX_ByteString csAS = "Off";
  if (bChecked) {
    csAS = csOn;
  }
  if (csOldAS == csAS) {
    return;
  }
  m_pWidgetDict->SetAtName("AS", csAS);
  m_pForm->m_bUpdated = TRUE;
}
CPDF_Stream* FPDFDOC_GetAnnotAP(CPDF_Dictionary* pAnnotDict,
                                CPDF_Annot::AppearanceMode mode);
void CPDF_FormControl::DrawControl(CFX_RenderDevice* pDevice,
                                   CFX_AffineMatrix* pMatrix,
                                   CPDF_Page* pPage,
                                   CPDF_Annot::AppearanceMode mode,
                                   const CPDF_RenderOptions* pOptions) {
  if (m_pWidgetDict->GetInteger("F") & ANNOTFLAG_HIDDEN) {
    return;
  }
  CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(m_pWidgetDict, mode);
  if (pStream == NULL) {
    return;
  }
  CFX_FloatRect form_bbox = pStream->GetDict()->GetRect("BBox");
  CFX_AffineMatrix form_matrix = pStream->GetDict()->GetMatrix("Matrix");
  form_matrix.TransformRect(form_bbox);
  CFX_FloatRect arect = m_pWidgetDict->GetRect("Rect");
  CFX_AffineMatrix matrix;
  matrix.MatchRect(arect, form_bbox);
  matrix.Concat(*pMatrix);
  CPDF_Form form(m_pField->m_pForm->m_pDocument,
                 m_pField->m_pForm->m_pFormDict->GetDict("DR"), pStream);
  form.ParseContent(NULL, NULL, NULL, NULL);
  CPDF_RenderContext context;
  context.Create(pPage);
  context.DrawObjectList(pDevice, &form, &matrix, pOptions);
}
const FX_CHAR* g_sHighlightingMode[] = {"N", "I", "O", "P", "T", ""};
CPDF_FormControl::HighlightingMode CPDF_FormControl::GetHighlightingMode() {
  if (m_pWidgetDict == NULL) {
    return Invert;
  }
  CFX_ByteString csH = m_pWidgetDict->GetString("H", "I");
  int i = 0;
  while (g_sHighlightingMode[i][0] != '\0') {
    if (csH.Equal(g_sHighlightingMode[i])) {
      return (HighlightingMode)i;
    }
    i++;
  }
  return Invert;
}
CPDF_ApSettings CPDF_FormControl::GetMK(FX_BOOL bCreate) {
  if (!m_pWidgetDict) {
    return NULL;
  }
  CPDF_ApSettings mk = m_pWidgetDict->GetDict(FX_BSTRC("MK"));
  if (!mk && bCreate) {
    mk = CPDF_Dictionary::Create();
    if (mk == NULL) {
      return NULL;
    }
    m_pWidgetDict->SetAt(FX_BSTRC("MK"), mk);
  }
  return mk;
}
FX_BOOL CPDF_FormControl::HasMKEntry(CFX_ByteString csEntry) {
  CPDF_ApSettings mk = GetMK(FALSE);
  return mk.HasMKEntry(csEntry);
}
int CPDF_FormControl::GetRotation() {
  CPDF_ApSettings mk = GetMK(FALSE);
  return mk.GetRotation();
}
FX_ARGB CPDF_FormControl::GetColor(int& iColorType, CFX_ByteString csEntry) {
  CPDF_ApSettings mk = GetMK(FALSE);
  return mk.GetColor(iColorType, csEntry);
}
FX_FLOAT CPDF_FormControl::GetOriginalColor(int index, CFX_ByteString csEntry) {
  CPDF_ApSettings mk = GetMK(FALSE);
  return mk.GetOriginalColor(index, csEntry);
}
void CPDF_FormControl::GetOriginalColor(int& iColorType,
                                        FX_FLOAT fc[4],
                                        CFX_ByteString csEntry) {
  CPDF_ApSettings mk = GetMK(FALSE);
  mk.GetOriginalColor(iColorType, fc, csEntry);
}
CFX_WideString CPDF_FormControl::GetCaption(CFX_ByteString csEntry) {
  CPDF_ApSettings mk = GetMK(FALSE);
  return mk.GetCaption(csEntry);
}
CPDF_Stream* CPDF_FormControl::GetIcon(CFX_ByteString csEntry) {
  CPDF_ApSettings mk = GetMK(FALSE);
  return mk.GetIcon(csEntry);
}
CPDF_IconFit CPDF_FormControl::GetIconFit() {
  CPDF_ApSettings mk = GetMK(FALSE);
  return mk.GetIconFit();
}
int CPDF_FormControl::GetTextPosition() {
  CPDF_ApSettings mk = GetMK(FALSE);
  return mk.GetTextPosition();
}
CPDF_Action CPDF_FormControl::GetAction() {
  if (!m_pWidgetDict) {
    return CPDF_Action();
  }
  if (m_pWidgetDict->KeyExist("A")) {
    return CPDF_Action(m_pWidgetDict->GetDict("A"));
  }
  CPDF_Object* pObj = FPDF_GetFieldAttr(m_pField->m_pDict, "A");
  if (!pObj) {
    return CPDF_Action();
  }
  return CPDF_Action(pObj->GetDict());
}
CPDF_AAction CPDF_FormControl::GetAdditionalAction() {
  if (!m_pWidgetDict) {
    return nullptr;
  }
  if (m_pWidgetDict->KeyExist("AA")) {
    return m_pWidgetDict->GetDict("AA");
  }
  return m_pField->GetAdditionalAction();
}
CPDF_DefaultAppearance CPDF_FormControl::GetDefaultAppearance() {
  if (!m_pWidgetDict) {
    return CFX_ByteString();
  }
  if (m_pWidgetDict->KeyExist("DA")) {
    return m_pWidgetDict->GetString("DA");
  }
  CPDF_Object* pObj = FPDF_GetFieldAttr(m_pField->m_pDict, "DA");
  if (!pObj) {
    return m_pField->m_pForm->GetDefaultAppearance();
  }
  return pObj->GetString();
}

CPDF_Font* CPDF_FormControl::GetDefaultControlFont() {
  CPDF_DefaultAppearance cDA = GetDefaultAppearance();
  CFX_ByteString csFontNameTag;
  FX_FLOAT fFontSize;
  cDA.GetFont(csFontNameTag, fFontSize);
  if (csFontNameTag.IsEmpty())
    return nullptr;

  CPDF_Object* pObj = FPDF_GetFieldAttr(m_pWidgetDict, "DR");
  if (pObj && pObj->GetType() == PDFOBJ_DICTIONARY) {
    CPDF_Dictionary* pFonts = ((CPDF_Dictionary*)pObj)->GetDict("Font");
    if (pFonts) {
      CPDF_Dictionary* pElement = pFonts->GetDict(csFontNameTag);
      if (pElement) {
        CPDF_Font* pFont = m_pField->m_pForm->m_pDocument->LoadFont(pElement);
        if (pFont) {
          return pFont;
        }
      }
    }
  }
  if (CPDF_Font* pFormFont = m_pField->m_pForm->GetFormFont(csFontNameTag))
    return pFormFont;

  CPDF_Dictionary* pPageDict = m_pWidgetDict->GetDict("P");
  pObj = FPDF_GetFieldAttr(pPageDict, "Resources");
  if (pObj && pObj->GetType() == PDFOBJ_DICTIONARY) {
    CPDF_Dictionary* pFonts = ((CPDF_Dictionary*)pObj)->GetDict("Font");
    if (pFonts) {
      CPDF_Dictionary* pElement = pFonts->GetDict(csFontNameTag);
      if (pElement) {
        CPDF_Font* pFont = m_pField->m_pForm->m_pDocument->LoadFont(pElement);
        if (pFont) {
          return pFont;
        }
      }
    }
  }
  return nullptr;
}

int CPDF_FormControl::GetControlAlignment() {
  if (!m_pWidgetDict) {
    return 0;
  }
  if (m_pWidgetDict->KeyExist("Q")) {
    return m_pWidgetDict->GetInteger("Q", 0);
  }
  CPDF_Object* pObj = FPDF_GetFieldAttr(m_pField->m_pDict, "Q");
  if (pObj == NULL) {
    return m_pField->m_pForm->GetFormAlignment();
  }
  return pObj->GetInteger();
}
FX_BOOL CPDF_ApSettings::HasMKEntry(const CFX_ByteStringC& csEntry) {
  if (m_pDict == NULL) {
    return FALSE;
  }
  return m_pDict->KeyExist(csEntry);
}
int CPDF_ApSettings::GetRotation() {
  if (m_pDict == NULL) {
    return 0;
  }
  return m_pDict->GetInteger(FX_BSTRC("R"));
}
FX_ARGB CPDF_ApSettings::GetColor(int& iColorType,
                                  const CFX_ByteStringC& csEntry) {
  iColorType = COLORTYPE_TRANSPARENT;
  if (m_pDict == NULL) {
    return 0;
  }
  FX_ARGB color = 0;
  CPDF_Array* pEntry = m_pDict->GetArray(csEntry);
  if (pEntry == NULL) {
    return color;
  }
  FX_DWORD dwCount = pEntry->GetCount();
  if (dwCount == 1) {
    iColorType = COLORTYPE_GRAY;
    FX_FLOAT g = pEntry->GetNumber(0) * 255;
    color = ArgbEncode(255, (int)g, (int)g, (int)g);
  } else if (dwCount == 3) {
    iColorType = COLORTYPE_RGB;
    FX_FLOAT r = pEntry->GetNumber(0) * 255;
    FX_FLOAT g = pEntry->GetNumber(1) * 255;
    FX_FLOAT b = pEntry->GetNumber(2) * 255;
    color = ArgbEncode(255, (int)r, (int)g, (int)b);
  } else if (dwCount == 4) {
    iColorType = COLORTYPE_CMYK;
    FX_FLOAT c = pEntry->GetNumber(0);
    FX_FLOAT m = pEntry->GetNumber(1);
    FX_FLOAT y = pEntry->GetNumber(2);
    FX_FLOAT k = pEntry->GetNumber(3);
    FX_FLOAT r = 1.0f - FX_MIN(1.0f, c + k);
    FX_FLOAT g = 1.0f - FX_MIN(1.0f, m + k);
    FX_FLOAT b = 1.0f - FX_MIN(1.0f, y + k);
    color = ArgbEncode(255, (int)(r * 255), (int)(g * 255), (int)(b * 255));
  }
  return color;
}
FX_FLOAT CPDF_ApSettings::GetOriginalColor(int index,
                                           const CFX_ByteStringC& csEntry) {
  if (m_pDict == NULL) {
    return 0;
  }
  CPDF_Array* pEntry = m_pDict->GetArray(csEntry);
  if (pEntry != NULL) {
    return pEntry->GetNumber(index);
  }
  return 0;
}
void CPDF_ApSettings::GetOriginalColor(int& iColorType,
                                       FX_FLOAT fc[4],
                                       const CFX_ByteStringC& csEntry) {
  iColorType = COLORTYPE_TRANSPARENT;
  for (int i = 0; i < 4; i++) {
    fc[i] = 0;
  }
  if (m_pDict == NULL) {
    return;
  }
  CPDF_Array* pEntry = m_pDict->GetArray(csEntry);
  if (pEntry == NULL) {
    return;
  }
  FX_DWORD dwCount = pEntry->GetCount();
  if (dwCount == 1) {
    iColorType = COLORTYPE_GRAY;
    fc[0] = pEntry->GetNumber(0);
  } else if (dwCount == 3) {
    iColorType = COLORTYPE_RGB;
    fc[0] = pEntry->GetNumber(0);
    fc[1] = pEntry->GetNumber(1);
    fc[2] = pEntry->GetNumber(2);
  } else if (dwCount == 4) {
    iColorType = COLORTYPE_CMYK;
    fc[0] = pEntry->GetNumber(0);
    fc[1] = pEntry->GetNumber(1);
    fc[2] = pEntry->GetNumber(2);
    fc[3] = pEntry->GetNumber(3);
  }
}
CFX_WideString CPDF_ApSettings::GetCaption(const CFX_ByteStringC& csEntry) {
  CFX_WideString csCaption;
  if (m_pDict == NULL) {
    return csCaption;
  }
  return m_pDict->GetUnicodeText(csEntry);
}
CPDF_Stream* CPDF_ApSettings::GetIcon(const CFX_ByteStringC& csEntry) {
  if (m_pDict == NULL) {
    return NULL;
  }
  return m_pDict->GetStream(csEntry);
}
CPDF_IconFit CPDF_ApSettings::GetIconFit() {
  if (m_pDict == NULL) {
    return NULL;
  }
  return m_pDict->GetDict(FX_BSTRC("IF"));
}
int CPDF_ApSettings::GetTextPosition() {
  if (m_pDict == NULL) {
    return TEXTPOS_CAPTION;
  }
  return m_pDict->GetInteger(FX_BSTRC("TP"), TEXTPOS_CAPTION);
}