dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #include "core/fpdfdoc/cpvt_generateap.h" |
| 8 | |
thestig | 717d133 | 2016-09-20 05:58:19 -0700 | [diff] [blame] | 9 | #include <algorithm> |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <utility> |
thestig | 717d133 | 2016-09-20 05:58:19 -0700 | [diff] [blame] | 12 | |
dsinclair | bc5e6d2 | 2016-10-04 11:08:49 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/font/cpdf_font.h" |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 14 | #include "core/fpdfapi/parser/cpdf_boolean.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 15 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 16 | #include "core/fpdfapi/parser/cpdf_document.h" |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 17 | #include "core/fpdfapi/parser/cpdf_name.h" |
| 18 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 19 | #include "core/fpdfapi/parser/cpdf_reference.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 20 | #include "core/fpdfapi/parser/cpdf_simple_parser.h" |
| 21 | #include "core/fpdfapi/parser/cpdf_stream.h" |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 22 | #include "core/fpdfapi/parser/cpdf_string.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 23 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 24 | #include "core/fpdfdoc/cpdf_annot.h" |
| 25 | #include "core/fpdfdoc/cpdf_formfield.h" |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 26 | #include "core/fpdfdoc/cpvt_color.h" |
| 27 | #include "core/fpdfdoc/cpvt_fontmap.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 28 | #include "core/fpdfdoc/cpvt_word.h" |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 29 | #include "third_party/base/ptr_util.h" |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 33 | bool GenerateWidgetAP(CPDF_Document* pDoc, |
| 34 | CPDF_Dictionary* pAnnotDict, |
| 35 | const int32_t& nWidgetType) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 36 | CPDF_Dictionary* pFormDict = nullptr; |
| 37 | if (CPDF_Dictionary* pRootDict = pDoc->GetRoot()) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 38 | pFormDict = pRootDict->GetDictFor("AcroForm"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 39 | if (!pFormDict) |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 40 | return false; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 41 | |
| 42 | CFX_ByteString DA; |
| 43 | if (CPDF_Object* pDAObj = FPDF_GetFieldAttr(pAnnotDict, "DA")) |
| 44 | DA = pDAObj->GetString(); |
| 45 | if (DA.IsEmpty()) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 46 | DA = pFormDict->GetStringFor("DA"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 47 | if (DA.IsEmpty()) |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 48 | return false; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 49 | |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 50 | CPDF_SimpleParser syntax(DA.AsStringC()); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 51 | syntax.FindTagParamFromStart("Tf", 2); |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 52 | CFX_ByteString sFontName(syntax.GetWord()); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 53 | sFontName = PDF_NameDecode(sFontName); |
| 54 | if (sFontName.IsEmpty()) |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 55 | return false; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 56 | |
| 57 | FX_FLOAT fFontSize = FX_atof(syntax.GetWord()); |
| 58 | CPVT_Color crText = CPVT_Color::ParseColor(DA); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 59 | CPDF_Dictionary* pDRDict = pFormDict->GetDictFor("DR"); |
npm | 860a2d0 | 2016-09-12 15:02:28 -0700 | [diff] [blame] | 60 | if (!pDRDict) |
| 61 | return false; |
| 62 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 63 | CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 64 | if (!pDRFontDict) |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 65 | return false; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 66 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 67 | CPDF_Dictionary* pFontDict = pDRFontDict->GetDictFor(sFontName.Mid(1)); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 68 | if (!pFontDict) { |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 69 | pFontDict = pDoc->NewIndirect<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 70 | pFontDict->SetNewFor<CPDF_Name>("Type", "Font"); |
| 71 | pFontDict->SetNewFor<CPDF_Name>("Subtype", "Type1"); |
| 72 | pFontDict->SetNewFor<CPDF_Name>("BaseFont", "Helvetica"); |
| 73 | pFontDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding"); |
| 74 | pDRFontDict->SetNewFor<CPDF_Reference>(sFontName.Mid(1), pDoc, |
| 75 | pFontDict->GetObjNum()); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 76 | } |
| 77 | CPDF_Font* pDefFont = pDoc->LoadFont(pFontDict); |
| 78 | if (!pDefFont) |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 79 | return false; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 80 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 81 | CFX_FloatRect rcAnnot = pAnnotDict->GetRectFor("Rect"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 82 | int32_t nRotate = 0; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 83 | if (CPDF_Dictionary* pMKDict = pAnnotDict->GetDictFor("MK")) |
| 84 | nRotate = pMKDict->GetIntegerFor("R"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 85 | |
| 86 | CFX_FloatRect rcBBox; |
| 87 | CFX_Matrix matrix; |
| 88 | switch (nRotate % 360) { |
| 89 | case 0: |
| 90 | rcBBox = CFX_FloatRect(0, 0, rcAnnot.right - rcAnnot.left, |
| 91 | rcAnnot.top - rcAnnot.bottom); |
| 92 | break; |
| 93 | case 90: |
| 94 | matrix = CFX_Matrix(0, 1, -1, 0, rcAnnot.right - rcAnnot.left, 0); |
| 95 | rcBBox = CFX_FloatRect(0, 0, rcAnnot.top - rcAnnot.bottom, |
| 96 | rcAnnot.right - rcAnnot.left); |
| 97 | break; |
| 98 | case 180: |
| 99 | matrix = CFX_Matrix(-1, 0, 0, -1, rcAnnot.right - rcAnnot.left, |
| 100 | rcAnnot.top - rcAnnot.bottom); |
| 101 | rcBBox = CFX_FloatRect(0, 0, rcAnnot.right - rcAnnot.left, |
| 102 | rcAnnot.top - rcAnnot.bottom); |
| 103 | break; |
| 104 | case 270: |
| 105 | matrix = CFX_Matrix(0, -1, 1, 0, 0, rcAnnot.top - rcAnnot.bottom); |
| 106 | rcBBox = CFX_FloatRect(0, 0, rcAnnot.top - rcAnnot.bottom, |
| 107 | rcAnnot.right - rcAnnot.left); |
| 108 | break; |
| 109 | } |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 110 | |
| 111 | BorderStyle nBorderStyle = BorderStyle::SOLID; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 112 | FX_FLOAT fBorderWidth = 1; |
| 113 | CPVT_Dash dsBorder(3, 0, 0); |
| 114 | CPVT_Color crLeftTop, crRightBottom; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 115 | if (CPDF_Dictionary* pBSDict = pAnnotDict->GetDictFor("BS")) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 116 | if (pBSDict->KeyExist("W")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 117 | fBorderWidth = pBSDict->GetNumberFor("W"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 118 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 119 | if (CPDF_Array* pArray = pBSDict->GetArrayFor("D")) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 120 | dsBorder = CPVT_Dash(pArray->GetIntegerAt(0), pArray->GetIntegerAt(1), |
| 121 | pArray->GetIntegerAt(2)); |
| 122 | } |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 123 | switch (pBSDict->GetStringFor("S").GetAt(0)) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 124 | case 'S': |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 125 | nBorderStyle = BorderStyle::SOLID; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 126 | break; |
| 127 | case 'D': |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 128 | nBorderStyle = BorderStyle::DASH; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 129 | break; |
| 130 | case 'B': |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 131 | nBorderStyle = BorderStyle::BEVELED; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 132 | fBorderWidth *= 2; |
| 133 | crLeftTop = CPVT_Color(CPVT_Color::kGray, 1); |
| 134 | crRightBottom = CPVT_Color(CPVT_Color::kGray, 0.5); |
| 135 | break; |
| 136 | case 'I': |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 137 | nBorderStyle = BorderStyle::INSET; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 138 | fBorderWidth *= 2; |
| 139 | crLeftTop = CPVT_Color(CPVT_Color::kGray, 0.5); |
| 140 | crRightBottom = CPVT_Color(CPVT_Color::kGray, 0.75); |
| 141 | break; |
| 142 | case 'U': |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 143 | nBorderStyle = BorderStyle::UNDERLINE; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | } |
| 147 | CPVT_Color crBorder, crBG; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 148 | if (CPDF_Dictionary* pMKDict = pAnnotDict->GetDictFor("MK")) { |
| 149 | if (CPDF_Array* pArray = pMKDict->GetArrayFor("BC")) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 150 | crBorder = CPVT_Color::ParseColor(*pArray); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 151 | if (CPDF_Array* pArray = pMKDict->GetArrayFor("BG")) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 152 | crBG = CPVT_Color::ParseColor(*pArray); |
| 153 | } |
| 154 | CFX_ByteTextBuf sAppStream; |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 155 | CFX_ByteString sBG = |
| 156 | CPVT_GenerateAP::GenerateColorAP(crBG, PaintOperation::FILL); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 157 | if (sBG.GetLength() > 0) { |
| 158 | sAppStream << "q\n" << sBG << rcBBox.left << " " << rcBBox.bottom << " " |
| 159 | << rcBBox.Width() << " " << rcBBox.Height() << " re f\n" |
| 160 | << "Q\n"; |
| 161 | } |
| 162 | CFX_ByteString sBorderStream = CPVT_GenerateAP::GenerateBorderAP( |
| 163 | rcBBox, fBorderWidth, crBorder, crLeftTop, crRightBottom, nBorderStyle, |
| 164 | dsBorder); |
| 165 | if (sBorderStream.GetLength() > 0) |
| 166 | sAppStream << "q\n" << sBorderStream << "Q\n"; |
| 167 | |
| 168 | CFX_FloatRect rcBody = |
| 169 | CFX_FloatRect(rcBBox.left + fBorderWidth, rcBBox.bottom + fBorderWidth, |
| 170 | rcBBox.right - fBorderWidth, rcBBox.top - fBorderWidth); |
| 171 | rcBody.Normalize(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 172 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 173 | CPDF_Dictionary* pAPDict = pAnnotDict->GetDictFor("AP"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 174 | if (!pAPDict) |
| 175 | pAPDict = pAnnotDict->SetNewFor<CPDF_Dictionary>("AP"); |
| 176 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 177 | CPDF_Stream* pNormalStream = pAPDict->GetStreamFor("N"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 178 | if (!pNormalStream) { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 179 | pNormalStream = pDoc->NewIndirect<CPDF_Stream>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 180 | pAPDict->SetNewFor<CPDF_Reference>("N", pDoc, pNormalStream->GetObjNum()); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 181 | } |
| 182 | CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); |
| 183 | if (pStreamDict) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 184 | pStreamDict->SetMatrixFor("Matrix", matrix); |
| 185 | pStreamDict->SetRectFor("BBox", rcBBox); |
| 186 | CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 187 | if (pStreamResList) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 188 | CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 189 | if (!pStreamResFontList) |
| 190 | pStreamResFontList = pStreamResList->SetNewFor<CPDF_Dictionary>("Font"); |
| 191 | if (!pStreamResFontList->KeyExist(sFontName)) { |
| 192 | pStreamResFontList->SetNewFor<CPDF_Reference>(sFontName, pDoc, |
| 193 | pFontDict->GetObjNum()); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 194 | } |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 195 | } else { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 196 | pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone()); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 197 | pStreamResList = pStreamDict->GetDictFor("Resources"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | switch (nWidgetType) { |
| 201 | case 0: { |
| 202 | CFX_WideString swValue = |
| 203 | FPDF_GetFieldAttr(pAnnotDict, "V") |
| 204 | ? FPDF_GetFieldAttr(pAnnotDict, "V")->GetUnicodeText() |
| 205 | : CFX_WideString(); |
| 206 | int32_t nAlign = FPDF_GetFieldAttr(pAnnotDict, "Q") |
| 207 | ? FPDF_GetFieldAttr(pAnnotDict, "Q")->GetInteger() |
| 208 | : 0; |
| 209 | uint32_t dwFlags = FPDF_GetFieldAttr(pAnnotDict, "Ff") |
| 210 | ? FPDF_GetFieldAttr(pAnnotDict, "Ff")->GetInteger() |
| 211 | : 0; |
| 212 | uint32_t dwMaxLen = |
| 213 | FPDF_GetFieldAttr(pAnnotDict, "MaxLen") |
| 214 | ? FPDF_GetFieldAttr(pAnnotDict, "MaxLen")->GetInteger() |
| 215 | : 0; |
| 216 | CPVT_FontMap map( |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 217 | pDoc, pStreamDict ? pStreamDict->GetDictFor("Resources") : nullptr, |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 218 | pDefFont, sFontName.Right(sFontName.GetLength() - 1)); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 219 | CPDF_VariableText::Provider prd(&map); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 220 | CPDF_VariableText vt; |
| 221 | vt.SetProvider(&prd); |
| 222 | vt.SetPlateRect(rcBody); |
| 223 | vt.SetAlignment(nAlign); |
| 224 | if (IsFloatZero(fFontSize)) |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 225 | vt.SetAutoFontSize(true); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 226 | else |
| 227 | vt.SetFontSize(fFontSize); |
| 228 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 229 | bool bMultiLine = (dwFlags >> 12) & 1; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 230 | if (bMultiLine) { |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 231 | vt.SetMultiLine(true); |
| 232 | vt.SetAutoReturn(true); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 233 | } |
| 234 | uint16_t subWord = 0; |
| 235 | if ((dwFlags >> 13) & 1) { |
| 236 | subWord = '*'; |
| 237 | vt.SetPasswordChar(subWord); |
| 238 | } |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 239 | bool bCharArray = (dwFlags >> 24) & 1; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 240 | if (bCharArray) |
| 241 | vt.SetCharArray(dwMaxLen); |
| 242 | else |
| 243 | vt.SetLimitChar(dwMaxLen); |
| 244 | |
| 245 | vt.Initialize(); |
tsepez | 067990c | 2016-09-13 06:46:40 -0700 | [diff] [blame] | 246 | vt.SetText(swValue); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 247 | vt.RearrangeAll(); |
| 248 | CFX_FloatRect rcContent = vt.GetContentRect(); |
tsepez | 63f545c | 2016-09-13 16:08:49 -0700 | [diff] [blame] | 249 | CFX_FloatPoint ptOffset; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 250 | if (!bMultiLine) { |
| 251 | ptOffset = |
| 252 | CFX_FloatPoint(0.0f, (rcContent.Height() - rcBody.Height()) / 2.0f); |
| 253 | } |
| 254 | CFX_ByteString sBody = CPVT_GenerateAP::GenerateEditAP( |
| 255 | &map, vt.GetIterator(), ptOffset, !bCharArray, subWord); |
| 256 | if (sBody.GetLength() > 0) { |
| 257 | sAppStream << "/Tx BMC\n" |
| 258 | << "q\n"; |
| 259 | if (rcContent.Width() > rcBody.Width() || |
| 260 | rcContent.Height() > rcBody.Height()) { |
| 261 | sAppStream << rcBody.left << " " << rcBody.bottom << " " |
| 262 | << rcBody.Width() << " " << rcBody.Height() |
| 263 | << " re\nW\nn\n"; |
| 264 | } |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 265 | sAppStream << "BT\n" |
| 266 | << CPVT_GenerateAP::GenerateColorAP(crText, |
| 267 | PaintOperation::FILL) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 268 | << sBody << "ET\n" |
| 269 | << "Q\nEMC\n"; |
| 270 | } |
| 271 | } break; |
| 272 | case 1: { |
| 273 | CFX_WideString swValue = |
| 274 | FPDF_GetFieldAttr(pAnnotDict, "V") |
| 275 | ? FPDF_GetFieldAttr(pAnnotDict, "V")->GetUnicodeText() |
| 276 | : CFX_WideString(); |
| 277 | CPVT_FontMap map( |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 278 | pDoc, pStreamDict ? pStreamDict->GetDictFor("Resources") : nullptr, |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 279 | pDefFont, sFontName.Right(sFontName.GetLength() - 1)); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 280 | CPDF_VariableText::Provider prd(&map); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 281 | CPDF_VariableText vt; |
| 282 | vt.SetProvider(&prd); |
| 283 | CFX_FloatRect rcButton = rcBody; |
| 284 | rcButton.left = rcButton.right - 13; |
| 285 | rcButton.Normalize(); |
| 286 | CFX_FloatRect rcEdit = rcBody; |
| 287 | rcEdit.right = rcButton.left; |
| 288 | rcEdit.Normalize(); |
| 289 | vt.SetPlateRect(rcEdit); |
| 290 | if (IsFloatZero(fFontSize)) |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 291 | vt.SetAutoFontSize(true); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 292 | else |
| 293 | vt.SetFontSize(fFontSize); |
| 294 | |
| 295 | vt.Initialize(); |
tsepez | 067990c | 2016-09-13 06:46:40 -0700 | [diff] [blame] | 296 | vt.SetText(swValue); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 297 | vt.RearrangeAll(); |
| 298 | CFX_FloatRect rcContent = vt.GetContentRect(); |
| 299 | CFX_FloatPoint ptOffset = |
| 300 | CFX_FloatPoint(0.0f, (rcContent.Height() - rcEdit.Height()) / 2.0f); |
| 301 | CFX_ByteString sEdit = CPVT_GenerateAP::GenerateEditAP( |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 302 | &map, vt.GetIterator(), ptOffset, true, 0); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 303 | if (sEdit.GetLength() > 0) { |
| 304 | sAppStream << "/Tx BMC\n" |
| 305 | << "q\n"; |
| 306 | sAppStream << rcEdit.left << " " << rcEdit.bottom << " " |
| 307 | << rcEdit.Width() << " " << rcEdit.Height() << " re\nW\nn\n"; |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 308 | sAppStream << "BT\n" |
| 309 | << CPVT_GenerateAP::GenerateColorAP(crText, |
| 310 | PaintOperation::FILL) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 311 | << sEdit << "ET\n" |
| 312 | << "Q\nEMC\n"; |
| 313 | } |
| 314 | CFX_ByteString sButton = CPVT_GenerateAP::GenerateColorAP( |
| 315 | CPVT_Color(CPVT_Color::kRGB, 220.0f / 255.0f, 220.0f / 255.0f, |
| 316 | 220.0f / 255.0f), |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 317 | PaintOperation::FILL); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 318 | if (sButton.GetLength() > 0 && !rcButton.IsEmpty()) { |
| 319 | sAppStream << "q\n" << sButton; |
| 320 | sAppStream << rcButton.left << " " << rcButton.bottom << " " |
| 321 | << rcButton.Width() << " " << rcButton.Height() << " re f\n"; |
| 322 | sAppStream << "Q\n"; |
| 323 | CFX_ByteString sButtonBorder = CPVT_GenerateAP::GenerateBorderAP( |
| 324 | rcButton, 2, CPVT_Color(CPVT_Color::kGray, 0), |
| 325 | CPVT_Color(CPVT_Color::kGray, 1), |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 326 | CPVT_Color(CPVT_Color::kGray, 0.5), BorderStyle::BEVELED, |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 327 | CPVT_Dash(3, 0, 0)); |
| 328 | if (sButtonBorder.GetLength() > 0) |
| 329 | sAppStream << "q\n" << sButtonBorder << "Q\n"; |
| 330 | |
| 331 | CFX_FloatPoint ptCenter = |
| 332 | CFX_FloatPoint((rcButton.left + rcButton.right) / 2, |
| 333 | (rcButton.top + rcButton.bottom) / 2); |
| 334 | if (IsFloatBigger(rcButton.Width(), 6) && |
| 335 | IsFloatBigger(rcButton.Height(), 6)) { |
| 336 | sAppStream << "q\n" |
| 337 | << " 0 g\n"; |
| 338 | sAppStream << ptCenter.x - 3 << " " << ptCenter.y + 1.5f << " m\n"; |
| 339 | sAppStream << ptCenter.x + 3 << " " << ptCenter.y + 1.5f << " l\n"; |
| 340 | sAppStream << ptCenter.x << " " << ptCenter.y - 1.5f << " l\n"; |
| 341 | sAppStream << ptCenter.x - 3 << " " << ptCenter.y + 1.5f << " l f\n"; |
| 342 | sAppStream << sButton << "Q\n"; |
| 343 | } |
| 344 | } |
| 345 | } break; |
| 346 | case 2: { |
| 347 | CPVT_FontMap map( |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 348 | pDoc, pStreamDict ? pStreamDict->GetDictFor("Resources") : nullptr, |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 349 | pDefFont, sFontName.Right(sFontName.GetLength() - 1)); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 350 | CPDF_VariableText::Provider prd(&map); |
thestig | b8bf55f | 2016-05-21 21:08:05 -0700 | [diff] [blame] | 351 | CPDF_Array* pOpts = ToArray(FPDF_GetFieldAttr(pAnnotDict, "Opt")); |
| 352 | CPDF_Array* pSels = ToArray(FPDF_GetFieldAttr(pAnnotDict, "I")); |
| 353 | CPDF_Object* pTi = FPDF_GetFieldAttr(pAnnotDict, "TI"); |
| 354 | int32_t nTop = pTi ? pTi->GetInteger() : 0; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 355 | CFX_ByteTextBuf sBody; |
| 356 | if (pOpts) { |
| 357 | FX_FLOAT fy = rcBody.top; |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 358 | for (size_t i = nTop, sz = pOpts->GetCount(); i < sz; i++) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 359 | if (IsFloatSmaller(fy, rcBody.bottom)) |
| 360 | break; |
| 361 | |
| 362 | if (CPDF_Object* pOpt = pOpts->GetDirectObjectAt(i)) { |
| 363 | CFX_WideString swItem; |
| 364 | if (pOpt->IsString()) |
| 365 | swItem = pOpt->GetUnicodeText(); |
| 366 | else if (CPDF_Array* pArray = pOpt->AsArray()) |
| 367 | swItem = pArray->GetDirectObjectAt(1)->GetUnicodeText(); |
| 368 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 369 | bool bSelected = false; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 370 | if (pSels) { |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 371 | for (size_t s = 0, ssz = pSels->GetCount(); s < ssz; s++) { |
ochang | 6b19ec6 | 2016-04-12 11:53:23 -0700 | [diff] [blame] | 372 | int value = pSels->GetIntegerAt(s); |
| 373 | if (value >= 0 && i == static_cast<size_t>(value)) { |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 374 | bSelected = true; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | CPDF_VariableText vt; |
| 380 | vt.SetProvider(&prd); |
| 381 | vt.SetPlateRect( |
| 382 | CFX_FloatRect(rcBody.left, 0.0f, rcBody.right, 0.0f)); |
| 383 | vt.SetFontSize(IsFloatZero(fFontSize) ? 12.0f : fFontSize); |
| 384 | |
| 385 | vt.Initialize(); |
tsepez | 067990c | 2016-09-13 06:46:40 -0700 | [diff] [blame] | 386 | vt.SetText(swItem); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 387 | vt.RearrangeAll(); |
| 388 | FX_FLOAT fItemHeight = vt.GetContentRect().Height(); |
| 389 | if (bSelected) { |
| 390 | CFX_FloatRect rcItem = CFX_FloatRect( |
| 391 | rcBody.left, fy - fItemHeight, rcBody.right, fy); |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 392 | sBody << "q\n" |
| 393 | << CPVT_GenerateAP::GenerateColorAP( |
| 394 | CPVT_Color(CPVT_Color::kRGB, 0, 51.0f / 255.0f, |
| 395 | 113.0f / 255.0f), |
| 396 | PaintOperation::FILL) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 397 | << rcItem.left << " " << rcItem.bottom << " " |
| 398 | << rcItem.Width() << " " << rcItem.Height() << " re f\n" |
| 399 | << "Q\n"; |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 400 | sBody << "BT\n" |
| 401 | << CPVT_GenerateAP::GenerateColorAP( |
| 402 | CPVT_Color(CPVT_Color::kGray, 1), |
| 403 | PaintOperation::FILL) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 404 | << CPVT_GenerateAP::GenerateEditAP(&map, vt.GetIterator(), |
| 405 | CFX_FloatPoint(0.0f, fy), |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 406 | true, 0) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 407 | << "ET\n"; |
| 408 | } else { |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 409 | sBody << "BT\n" |
| 410 | << CPVT_GenerateAP::GenerateColorAP(crText, |
| 411 | PaintOperation::FILL) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 412 | << CPVT_GenerateAP::GenerateEditAP(&map, vt.GetIterator(), |
| 413 | CFX_FloatPoint(0.0f, fy), |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 414 | true, 0) |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 415 | << "ET\n"; |
| 416 | } |
| 417 | fy -= fItemHeight; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | if (sBody.GetSize() > 0) { |
thestig | b8bf55f | 2016-05-21 21:08:05 -0700 | [diff] [blame] | 422 | sAppStream << "/Tx BMC\nq\n" |
| 423 | << rcBody.left << " " << rcBody.bottom << " " |
| 424 | << rcBody.Width() << " " << rcBody.Height() << " re\nW\nn\n" |
| 425 | << sBody.AsStringC() << "Q\nEMC\n"; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 426 | } |
| 427 | } break; |
| 428 | } |
| 429 | if (pNormalStream) { |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 430 | pNormalStream->SetData(sAppStream.GetBuffer(), sAppStream.GetSize()); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 431 | pStreamDict = pNormalStream->GetDict(); |
| 432 | if (pStreamDict) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 433 | pStreamDict->SetMatrixFor("Matrix", matrix); |
| 434 | pStreamDict->SetRectFor("BBox", rcBBox); |
| 435 | CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 436 | if (pStreamResList) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 437 | CPDF_Dictionary* pStreamResFontList = |
| 438 | pStreamResList->GetDictFor("Font"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 439 | if (!pStreamResFontList) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 440 | pStreamResFontList = |
| 441 | pStreamResList->SetNewFor<CPDF_Dictionary>("Font"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 442 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 443 | if (!pStreamResFontList->KeyExist(sFontName)) { |
| 444 | pStreamResFontList->SetNewFor<CPDF_Reference>(sFontName, pDoc, |
| 445 | pFontDict->GetObjNum()); |
| 446 | } |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 447 | } else { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 448 | pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone()); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 449 | pStreamResList = pStreamDict->GetDictFor("Resources"); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 453 | return true; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 454 | } |
| 455 | |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 456 | CFX_ByteString GetColorStringWithDefault(CPDF_Array* pColor, |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 457 | const CPVT_Color& crDefaultColor, |
| 458 | PaintOperation nOperation) { |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 459 | if (pColor) { |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 460 | CPVT_Color color = CPVT_Color::ParseColor(*pColor); |
| 461 | return CPVT_GenerateAP::GenerateColorAP(color, nOperation); |
| 462 | } |
| 463 | |
| 464 | return CPVT_GenerateAP::GenerateColorAP(crDefaultColor, nOperation); |
| 465 | } |
| 466 | |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 467 | FX_FLOAT GetBorderWidth(const CPDF_Dictionary& pAnnotDict) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 468 | if (CPDF_Dictionary* pBorderStyleDict = pAnnotDict.GetDictFor("BS")) { |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 469 | if (pBorderStyleDict->KeyExist("W")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 470 | return pBorderStyleDict->GetNumberFor("W"); |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 471 | } |
| 472 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 473 | if (CPDF_Array* pBorderArray = pAnnotDict.GetArrayFor("Border")) { |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 474 | if (pBorderArray->GetCount() > 2) |
| 475 | return pBorderArray->GetNumberAt(2); |
| 476 | } |
| 477 | |
| 478 | return 1; |
| 479 | } |
| 480 | |
| 481 | CPDF_Array* GetDashArray(const CPDF_Dictionary& pAnnotDict) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 482 | if (CPDF_Dictionary* pBorderStyleDict = pAnnotDict.GetDictFor("BS")) { |
| 483 | if (pBorderStyleDict->GetStringFor("S") == "D") |
| 484 | return pBorderStyleDict->GetArrayFor("D"); |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 485 | } |
| 486 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 487 | if (CPDF_Array* pBorderArray = pAnnotDict.GetArrayFor("Border")) { |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 488 | if (pBorderArray->GetCount() == 4) |
| 489 | return pBorderArray->GetArrayAt(3); |
| 490 | } |
| 491 | |
| 492 | return nullptr; |
| 493 | } |
| 494 | |
| 495 | CFX_ByteString GetDashPatternString(const CPDF_Dictionary& pAnnotDict) { |
| 496 | CPDF_Array* pDashArray = GetDashArray(pAnnotDict); |
| 497 | if (!pDashArray || pDashArray->IsEmpty()) |
| 498 | return CFX_ByteString(); |
| 499 | |
| 500 | // Support maximum of ten elements in the dash array. |
| 501 | size_t pDashArrayCount = std::min<size_t>(pDashArray->GetCount(), 10); |
| 502 | CFX_ByteTextBuf sDashStream; |
| 503 | |
| 504 | sDashStream << "["; |
| 505 | for (size_t i = 0; i < pDashArrayCount; ++i) |
| 506 | sDashStream << pDashArray->GetNumberAt(i) << " "; |
| 507 | sDashStream << "] 0 d\n"; |
| 508 | |
| 509 | return sDashStream.MakeString(); |
| 510 | } |
| 511 | |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 512 | CFX_ByteString GetPopupContentsString(CPDF_Document* pDoc, |
| 513 | const CPDF_Dictionary& pAnnotDict, |
| 514 | CPDF_Font* pDefFont, |
| 515 | const CFX_ByteString& sFontName) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 516 | CFX_WideString swValue(pAnnotDict.GetUnicodeTextFor("T")); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 517 | swValue += L'\n'; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 518 | swValue += pAnnotDict.GetUnicodeTextFor("Contents"); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 519 | CPVT_FontMap map(pDoc, nullptr, pDefFont, sFontName); |
| 520 | |
| 521 | CPDF_VariableText::Provider prd(&map); |
| 522 | CPDF_VariableText vt; |
| 523 | vt.SetProvider(&prd); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 524 | vt.SetPlateRect(pAnnotDict.GetRectFor("Rect")); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 525 | vt.SetFontSize(12); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 526 | vt.SetAutoReturn(true); |
| 527 | vt.SetMultiLine(true); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 528 | |
| 529 | vt.Initialize(); |
tsepez | 067990c | 2016-09-13 06:46:40 -0700 | [diff] [blame] | 530 | vt.SetText(swValue); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 531 | vt.RearrangeAll(); |
| 532 | CFX_FloatPoint ptOffset(3.0f, -3.0f); |
| 533 | CFX_ByteString sContent = CPVT_GenerateAP::GenerateEditAP( |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 534 | &map, vt.GetIterator(), ptOffset, false, 0); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 535 | |
| 536 | if (sContent.IsEmpty()) |
| 537 | return CFX_ByteString(); |
| 538 | |
| 539 | CFX_ByteTextBuf sAppStream; |
| 540 | sAppStream << "BT\n" |
| 541 | << CPVT_GenerateAP::GenerateColorAP( |
| 542 | CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), PaintOperation::FILL) |
| 543 | << sContent << "ET\n" |
| 544 | << "Q\n"; |
| 545 | return sAppStream.MakeString(); |
| 546 | } |
| 547 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 548 | std::unique_ptr<CPDF_Dictionary> GenerateExtGStateDict( |
| 549 | const CPDF_Dictionary& pAnnotDict, |
| 550 | const CFX_ByteString& sExtGSDictName, |
| 551 | const CFX_ByteString& sBlendMode) { |
| 552 | auto pGSDict = |
| 553 | pdfium::MakeUnique<CPDF_Dictionary>(pAnnotDict.GetByteStringPool()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 554 | pGSDict->SetNewFor<CPDF_String>("Type", "ExtGState", false); |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 555 | |
| 556 | FX_FLOAT fOpacity = |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 557 | pAnnotDict.KeyExist("CA") ? pAnnotDict.GetNumberFor("CA") : 1; |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 558 | pGSDict->SetNewFor<CPDF_Number>("CA", fOpacity); |
| 559 | pGSDict->SetNewFor<CPDF_Number>("ca", fOpacity); |
| 560 | pGSDict->SetNewFor<CPDF_Boolean>("AIS", false); |
| 561 | pGSDict->SetNewFor<CPDF_String>("BM", sBlendMode, false); |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 562 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 563 | auto pExtGStateDict = |
| 564 | pdfium::MakeUnique<CPDF_Dictionary>(pAnnotDict.GetByteStringPool()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 565 | pExtGStateDict->SetFor(sExtGSDictName, std::move(pGSDict)); |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 566 | return pExtGStateDict; |
| 567 | } |
| 568 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 569 | std::unique_ptr<CPDF_Dictionary> GenerateResourceFontDict( |
| 570 | CPDF_Document* pDoc, |
| 571 | const CFX_ByteString& sFontDictName) { |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 572 | CPDF_Dictionary* pFontDict = pDoc->NewIndirect<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 573 | pFontDict->SetNewFor<CPDF_Name>("Type", "Font"); |
| 574 | pFontDict->SetNewFor<CPDF_Name>("Subtype", "Type1"); |
| 575 | pFontDict->SetNewFor<CPDF_Name>("BaseFont", "Helvetica"); |
| 576 | pFontDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding"); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 577 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 578 | auto pResourceFontDict = |
| 579 | pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 580 | pResourceFontDict->SetNewFor<CPDF_Reference>(sFontDictName, pDoc, |
| 581 | pFontDict->GetObjNum()); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 582 | return pResourceFontDict; |
| 583 | } |
| 584 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 585 | std::unique_ptr<CPDF_Dictionary> GenerateResourceDict( |
| 586 | CPDF_Document* pDoc, |
| 587 | std::unique_ptr<CPDF_Dictionary> pExtGStateDict, |
| 588 | std::unique_ptr<CPDF_Dictionary> pResourceFontDict) { |
| 589 | auto pResourceDict = |
| 590 | pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 591 | if (pExtGStateDict) |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 592 | pResourceDict->SetFor("ExtGState", std::move(pExtGStateDict)); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 593 | if (pResourceFontDict) |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 594 | pResourceDict->SetFor("Font", std::move(pResourceFontDict)); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 595 | return pResourceDict; |
| 596 | } |
| 597 | |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 598 | void GenerateAndSetAPDict(CPDF_Document* pDoc, |
| 599 | CPDF_Dictionary* pAnnotDict, |
| 600 | const CFX_ByteTextBuf& sAppStream, |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 601 | std::unique_ptr<CPDF_Dictionary> pResourceDict, |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 602 | bool bIsTextMarkupAnnotation) { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 603 | CPDF_Stream* pNormalStream = pDoc->NewIndirect<CPDF_Stream>(); |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 604 | pNormalStream->SetData(sAppStream.GetBuffer(), sAppStream.GetSize()); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 605 | |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 606 | CPDF_Dictionary* pAPDict = pAnnotDict->SetNewFor<CPDF_Dictionary>("AP"); |
| 607 | pAPDict->SetNewFor<CPDF_Reference>("N", pDoc, pNormalStream->GetObjNum()); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 608 | |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 609 | CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 610 | pStreamDict->SetNewFor<CPDF_Number>("FormType", 1); |
| 611 | pStreamDict->SetNewFor<CPDF_String>("Subtype", "Form", false); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 612 | pStreamDict->SetMatrixFor("Matrix", CFX_Matrix()); |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 613 | |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 614 | CFX_FloatRect rect = bIsTextMarkupAnnotation |
| 615 | ? CPDF_Annot::RectFromQuadPoints(pAnnotDict) |
| 616 | : pAnnotDict->GetRectFor("Rect"); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 617 | pStreamDict->SetRectFor("BBox", rect); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 618 | pStreamDict->SetFor("Resources", std::move(pResourceDict)); |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 619 | } |
| 620 | |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 621 | CFX_ByteString GetPaintOperatorString(bool bIsStrokeRect, bool bIsFillRect) { |
| 622 | if (bIsStrokeRect) |
| 623 | return bIsFillRect ? "b" : "s"; |
| 624 | return bIsFillRect ? "f" : "n"; |
| 625 | } |
| 626 | |
jaepark | c38de11 | 2016-08-22 17:54:56 -0700 | [diff] [blame] | 627 | CFX_ByteString GenerateTextSymbolAP(const CFX_FloatRect& rect) { |
| 628 | CFX_ByteTextBuf sAppStream; |
| 629 | sAppStream << CPVT_GenerateAP::GenerateColorAP( |
| 630 | CPVT_Color(CPVT_Color::kRGB, 1, 1, 0), PaintOperation::FILL); |
| 631 | sAppStream << CPVT_GenerateAP::GenerateColorAP( |
| 632 | CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), PaintOperation::STROKE); |
| 633 | |
| 634 | const FX_FLOAT fBorderWidth = 1; |
| 635 | sAppStream << fBorderWidth << " w\n"; |
| 636 | |
| 637 | const FX_FLOAT fHalfWidth = fBorderWidth / 2; |
| 638 | const FX_FLOAT fTipDelta = 4; |
| 639 | |
| 640 | CFX_FloatRect outerRect1 = rect; |
| 641 | outerRect1.Deflate(fHalfWidth, fHalfWidth); |
| 642 | outerRect1.bottom += fTipDelta; |
| 643 | |
| 644 | CFX_FloatRect outerRect2 = outerRect1; |
| 645 | outerRect2.left += fTipDelta; |
| 646 | outerRect2.right = outerRect2.left + fTipDelta; |
| 647 | outerRect2.top = outerRect2.bottom - fTipDelta; |
| 648 | FX_FLOAT outerRect2Middle = (outerRect2.left + outerRect2.right) / 2; |
| 649 | |
| 650 | // Draw outer boxes. |
| 651 | sAppStream << outerRect1.left << " " << outerRect1.bottom << " m\n" |
| 652 | << outerRect1.left << " " << outerRect1.top << " l\n" |
| 653 | << outerRect1.right << " " << outerRect1.top << " l\n" |
| 654 | << outerRect1.right << " " << outerRect1.bottom << " l\n" |
| 655 | << outerRect2.right << " " << outerRect2.bottom << " l\n" |
| 656 | << outerRect2Middle << " " << outerRect2.top << " l\n" |
| 657 | << outerRect2.left << " " << outerRect2.bottom << " l\n" |
| 658 | << outerRect1.left << " " << outerRect1.bottom << " l\n"; |
| 659 | |
| 660 | // Draw inner lines. |
| 661 | CFX_FloatRect lineRect = outerRect1; |
| 662 | const FX_FLOAT fXDelta = 2; |
| 663 | const FX_FLOAT fYDelta = (lineRect.top - lineRect.bottom) / 4; |
| 664 | |
| 665 | lineRect.left += fXDelta; |
| 666 | lineRect.right -= fXDelta; |
| 667 | for (int i = 0; i < 3; ++i) { |
| 668 | lineRect.top -= fYDelta; |
| 669 | sAppStream << lineRect.left << " " << lineRect.top << " m\n" |
| 670 | << lineRect.right << " " << lineRect.top << " l\n"; |
| 671 | } |
| 672 | sAppStream << "B*\n"; |
| 673 | |
| 674 | return sAppStream.MakeString(); |
| 675 | } |
| 676 | |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 677 | } // namespace |
| 678 | |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 679 | bool FPDF_GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 680 | if (!pAnnotDict || pAnnotDict->GetStringFor("Subtype") != "Widget") |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 681 | return false; |
| 682 | |
thestig | 717d133 | 2016-09-20 05:58:19 -0700 | [diff] [blame] | 683 | CPDF_Object* pFieldTypeObj = FPDF_GetFieldAttr(pAnnotDict, "FT"); |
| 684 | if (!pFieldTypeObj) |
| 685 | return false; |
| 686 | |
| 687 | CFX_ByteString field_type = pFieldTypeObj->GetString(); |
| 688 | if (field_type == "Tx") |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 689 | return CPVT_GenerateAP::GenerateTextFieldAP(pDoc, pAnnotDict); |
thestig | 717d133 | 2016-09-20 05:58:19 -0700 | [diff] [blame] | 690 | |
| 691 | CPDF_Object* pFieldFlagsObj = FPDF_GetFieldAttr(pAnnotDict, "Ff"); |
| 692 | uint32_t flags = pFieldFlagsObj ? pFieldFlagsObj->GetInteger() : 0; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 693 | if (field_type == "Ch") { |
| 694 | return (flags & (1 << 17)) |
| 695 | ? CPVT_GenerateAP::GenerateComboBoxAP(pDoc, pAnnotDict) |
| 696 | : CPVT_GenerateAP::GenerateListBoxAP(pDoc, pAnnotDict); |
| 697 | } |
thestig | 717d133 | 2016-09-20 05:58:19 -0700 | [diff] [blame] | 698 | |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 699 | if (field_type == "Btn") { |
| 700 | if (!(flags & (1 << 16))) { |
| 701 | if (!pAnnotDict->KeyExist("AS")) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 702 | if (CPDF_Dictionary* pParentDict = pAnnotDict->GetDictFor("Parent")) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 703 | if (pParentDict->KeyExist("AS")) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 704 | pAnnotDict->SetNewFor<CPDF_String>( |
| 705 | "AS", pParentDict->GetStringFor("AS"), false); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | } |
thestig | 717d133 | 2016-09-20 05:58:19 -0700 | [diff] [blame] | 711 | |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 712 | return false; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Static. |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 716 | bool CPVT_GenerateAP::GenerateComboBoxAP(CPDF_Document* pDoc, |
| 717 | CPDF_Dictionary* pAnnotDict) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 718 | return GenerateWidgetAP(pDoc, pAnnotDict, 1); |
| 719 | } |
| 720 | |
| 721 | // Static. |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 722 | bool CPVT_GenerateAP::GenerateListBoxAP(CPDF_Document* pDoc, |
| 723 | CPDF_Dictionary* pAnnotDict) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 724 | return GenerateWidgetAP(pDoc, pAnnotDict, 2); |
| 725 | } |
| 726 | |
| 727 | // Static. |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 728 | bool CPVT_GenerateAP::GenerateTextFieldAP(CPDF_Document* pDoc, |
| 729 | CPDF_Dictionary* pAnnotDict) { |
| 730 | return GenerateWidgetAP(pDoc, pAnnotDict, 0); |
| 731 | } |
| 732 | |
jaepark | e7107f4 | 2016-08-09 11:40:58 -0700 | [diff] [blame] | 733 | bool CPVT_GenerateAP::GenerateCircleAP(CPDF_Document* pDoc, |
| 734 | CPDF_Dictionary* pAnnotDict) { |
jaepark | e7107f4 | 2016-08-09 11:40:58 -0700 | [diff] [blame] | 735 | CFX_ByteTextBuf sAppStream; |
| 736 | CFX_ByteString sExtGSDictName = "GS"; |
| 737 | sAppStream << "/" << sExtGSDictName << " gs "; |
| 738 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 739 | CPDF_Array* pInteriorColor = pAnnotDict->GetArrayFor("IC"); |
jaepark | e7107f4 | 2016-08-09 11:40:58 -0700 | [diff] [blame] | 740 | sAppStream << GetColorStringWithDefault(pInteriorColor, |
| 741 | CPVT_Color(CPVT_Color::kTransparent), |
| 742 | PaintOperation::FILL); |
| 743 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 744 | sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayFor("C"), |
jaepark | e7107f4 | 2016-08-09 11:40:58 -0700 | [diff] [blame] | 745 | CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
| 746 | PaintOperation::STROKE); |
| 747 | |
| 748 | FX_FLOAT fBorderWidth = GetBorderWidth(*pAnnotDict); |
| 749 | bool bIsStrokeRect = fBorderWidth > 0; |
| 750 | |
| 751 | if (bIsStrokeRect) { |
| 752 | sAppStream << fBorderWidth << " w "; |
| 753 | sAppStream << GetDashPatternString(*pAnnotDict); |
| 754 | } |
| 755 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 756 | CFX_FloatRect rect = pAnnotDict->GetRectFor("Rect"); |
jaepark | e7107f4 | 2016-08-09 11:40:58 -0700 | [diff] [blame] | 757 | rect.Normalize(); |
| 758 | |
| 759 | if (bIsStrokeRect) { |
| 760 | // Deflating rect because stroking a path entails painting all points whose |
| 761 | // perpendicular distance from the path in user space is less than or equal |
| 762 | // to half the line width. |
| 763 | rect.Deflate(fBorderWidth / 2, fBorderWidth / 2); |
| 764 | } |
| 765 | |
| 766 | const FX_FLOAT fMiddleX = (rect.left + rect.right) / 2; |
| 767 | const FX_FLOAT fMiddleY = (rect.top + rect.bottom) / 2; |
| 768 | |
| 769 | // |fL| is precalculated approximate value of 4 * tan((3.14 / 2) / 4) / 3, |
| 770 | // where |fL| * radius is a good approximation of control points for |
| 771 | // arc with 90 degrees. |
| 772 | const FX_FLOAT fL = 0.5523f; |
| 773 | const FX_FLOAT fDeltaX = fL * rect.Width() / 2.0; |
| 774 | const FX_FLOAT fDeltaY = fL * rect.Height() / 2.0; |
| 775 | |
| 776 | // Starting point |
| 777 | sAppStream << fMiddleX << " " << rect.top << " m\n"; |
| 778 | // First Bezier Curve |
| 779 | sAppStream << fMiddleX + fDeltaX << " " << rect.top << " " << rect.right |
| 780 | << " " << fMiddleY + fDeltaY << " " << rect.right << " " |
| 781 | << fMiddleY << " c\n"; |
| 782 | // Second Bezier Curve |
| 783 | sAppStream << rect.right << " " << fMiddleY - fDeltaY << " " |
| 784 | << fMiddleX + fDeltaX << " " << rect.bottom << " " << fMiddleX |
| 785 | << " " << rect.bottom << " c\n"; |
| 786 | // Third Bezier Curve |
| 787 | sAppStream << fMiddleX - fDeltaX << " " << rect.bottom << " " << rect.left |
| 788 | << " " << fMiddleY - fDeltaY << " " << rect.left << " " << fMiddleY |
| 789 | << " c\n"; |
| 790 | // Fourth Bezier Curve |
| 791 | sAppStream << rect.left << " " << fMiddleY + fDeltaY << " " |
| 792 | << fMiddleX - fDeltaX << " " << rect.top << " " << fMiddleX << " " |
| 793 | << rect.top << " c\n"; |
| 794 | |
| 795 | bool bIsFillRect = pInteriorColor && !pInteriorColor->IsEmpty(); |
| 796 | sAppStream << GetPaintOperatorString(bIsStrokeRect, bIsFillRect) << "\n"; |
| 797 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 798 | auto pExtGStateDict = |
jaepark | e7107f4 | 2016-08-09 11:40:58 -0700 | [diff] [blame] | 799 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 800 | auto pResourceDict = |
| 801 | GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); |
| 802 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 803 | false /*IsTextMarkupAnnotation*/); |
jaepark | e7107f4 | 2016-08-09 11:40:58 -0700 | [diff] [blame] | 804 | return true; |
| 805 | } |
| 806 | |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 807 | bool CPVT_GenerateAP::GenerateHighlightAP(CPDF_Document* pDoc, |
| 808 | CPDF_Dictionary* pAnnotDict) { |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 809 | CFX_ByteTextBuf sAppStream; |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 810 | CFX_ByteString sExtGSDictName = "GS"; |
| 811 | sAppStream << "/" << sExtGSDictName << " gs "; |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 812 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 813 | sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayFor("C"), |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 814 | CPVT_Color(CPVT_Color::kRGB, 1, 1, 0), |
| 815 | PaintOperation::FILL); |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 816 | |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 817 | CFX_FloatRect rect = CPDF_Annot::RectFromQuadPoints(pAnnotDict); |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 818 | rect.Normalize(); |
| 819 | |
| 820 | sAppStream << rect.left << " " << rect.top << " m " << rect.right << " " |
| 821 | << rect.top << " l " << rect.right << " " << rect.bottom << " l " |
| 822 | << rect.left << " " << rect.bottom << " l " |
| 823 | << "h f\n"; |
| 824 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 825 | auto pExtGStateDict = |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 826 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Multiply"); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 827 | auto pResourceDict = |
| 828 | GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); |
| 829 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 830 | true /*IsTextMarkupAnnotation*/); |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 831 | |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 832 | return true; |
| 833 | } |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 834 | |
jaepark | 39ba18a | 2016-08-09 15:19:35 -0700 | [diff] [blame] | 835 | bool CPVT_GenerateAP::GenerateInkAP(CPDF_Document* pDoc, |
| 836 | CPDF_Dictionary* pAnnotDict) { |
jaepark | 39ba18a | 2016-08-09 15:19:35 -0700 | [diff] [blame] | 837 | FX_FLOAT fBorderWidth = GetBorderWidth(*pAnnotDict); |
| 838 | bool bIsStroke = fBorderWidth > 0; |
| 839 | |
| 840 | if (!bIsStroke) |
| 841 | return false; |
| 842 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 843 | CPDF_Array* pInkList = pAnnotDict->GetArrayFor("InkList"); |
jaepark | 39ba18a | 2016-08-09 15:19:35 -0700 | [diff] [blame] | 844 | if (!pInkList || pInkList->IsEmpty()) |
| 845 | return false; |
| 846 | |
| 847 | CFX_ByteTextBuf sAppStream; |
| 848 | CFX_ByteString sExtGSDictName = "GS"; |
| 849 | sAppStream << "/" << sExtGSDictName << " gs "; |
| 850 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 851 | sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayFor("C"), |
jaepark | 39ba18a | 2016-08-09 15:19:35 -0700 | [diff] [blame] | 852 | CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
| 853 | PaintOperation::STROKE); |
| 854 | |
| 855 | sAppStream << fBorderWidth << " w "; |
| 856 | sAppStream << GetDashPatternString(*pAnnotDict); |
| 857 | |
| 858 | // Set inflated rect as a new rect because paths near the border with large |
| 859 | // width should not be clipped to the original rect. |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 860 | CFX_FloatRect rect = pAnnotDict->GetRectFor("Rect"); |
jaepark | 39ba18a | 2016-08-09 15:19:35 -0700 | [diff] [blame] | 861 | rect.Inflate(fBorderWidth / 2, fBorderWidth / 2); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 862 | pAnnotDict->SetRectFor("Rect", rect); |
jaepark | 39ba18a | 2016-08-09 15:19:35 -0700 | [diff] [blame] | 863 | |
| 864 | for (size_t i = 0; i < pInkList->GetCount(); i++) { |
| 865 | CPDF_Array* pInkCoordList = pInkList->GetArrayAt(i); |
| 866 | if (!pInkCoordList || pInkCoordList->GetCount() < 2) |
| 867 | continue; |
| 868 | |
| 869 | sAppStream << pInkCoordList->GetNumberAt(0) << " " |
| 870 | << pInkCoordList->GetNumberAt(1) << " m "; |
| 871 | |
| 872 | for (size_t j = 0; j < pInkCoordList->GetCount() - 1; j += 2) { |
| 873 | sAppStream << pInkCoordList->GetNumberAt(j) << " " |
| 874 | << pInkCoordList->GetNumberAt(j + 1) << " l "; |
| 875 | } |
| 876 | |
| 877 | sAppStream << "S\n"; |
| 878 | } |
| 879 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 880 | auto pExtGStateDict = |
jaepark | 39ba18a | 2016-08-09 15:19:35 -0700 | [diff] [blame] | 881 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 882 | auto pResourceDict = |
| 883 | GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); |
| 884 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 885 | false /*IsTextMarkupAnnotation*/); |
jaepark | 39ba18a | 2016-08-09 15:19:35 -0700 | [diff] [blame] | 886 | return true; |
| 887 | } |
| 888 | |
jaepark | c38de11 | 2016-08-22 17:54:56 -0700 | [diff] [blame] | 889 | bool CPVT_GenerateAP::GenerateTextAP(CPDF_Document* pDoc, |
| 890 | CPDF_Dictionary* pAnnotDict) { |
jaepark | c38de11 | 2016-08-22 17:54:56 -0700 | [diff] [blame] | 891 | CFX_ByteTextBuf sAppStream; |
| 892 | CFX_ByteString sExtGSDictName = "GS"; |
| 893 | sAppStream << "/" << sExtGSDictName << " gs "; |
| 894 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 895 | CFX_FloatRect rect = pAnnotDict->GetRectFor("Rect"); |
jaepark | c38de11 | 2016-08-22 17:54:56 -0700 | [diff] [blame] | 896 | const FX_FLOAT fNoteLength = 20; |
| 897 | CFX_FloatRect noteRect(rect.left, rect.bottom, rect.left + fNoteLength, |
| 898 | rect.bottom + fNoteLength); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 899 | pAnnotDict->SetRectFor("Rect", noteRect); |
jaepark | c38de11 | 2016-08-22 17:54:56 -0700 | [diff] [blame] | 900 | |
| 901 | sAppStream << GenerateTextSymbolAP(noteRect); |
| 902 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 903 | auto pExtGStateDict = |
jaepark | c38de11 | 2016-08-22 17:54:56 -0700 | [diff] [blame] | 904 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 905 | auto pResourceDict = |
| 906 | GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); |
| 907 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 908 | false /*IsTextMarkupAnnotation*/); |
jaepark | c38de11 | 2016-08-22 17:54:56 -0700 | [diff] [blame] | 909 | return true; |
| 910 | } |
| 911 | |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 912 | bool CPVT_GenerateAP::GenerateUnderlineAP(CPDF_Document* pDoc, |
| 913 | CPDF_Dictionary* pAnnotDict) { |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 914 | CFX_ByteTextBuf sAppStream; |
| 915 | CFX_ByteString sExtGSDictName = "GS"; |
| 916 | sAppStream << "/" << sExtGSDictName << " gs "; |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 917 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 918 | sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayFor("C"), |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 919 | CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
| 920 | PaintOperation::STROKE); |
jaepark | 2d7ab18 | 2016-08-01 18:07:21 -0700 | [diff] [blame] | 921 | |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 922 | CFX_FloatRect rect = CPDF_Annot::RectFromQuadPoints(pAnnotDict); |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 923 | rect.Normalize(); |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 924 | |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 925 | FX_FLOAT fLineWidth = 1.0; |
| 926 | sAppStream << fLineWidth << " w " << rect.left << " " |
| 927 | << rect.bottom + fLineWidth << " m " << rect.right << " " |
| 928 | << rect.bottom + fLineWidth << " l S\n"; |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 929 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 930 | auto pExtGStateDict = |
jaepark | 33e9b26 | 2016-08-02 16:22:52 -0700 | [diff] [blame] | 931 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 932 | auto pResourceDict = |
| 933 | GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); |
| 934 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 935 | true /*IsTextMarkupAnnotation*/); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 936 | return true; |
| 937 | } |
| 938 | |
| 939 | bool CPVT_GenerateAP::GeneratePopupAP(CPDF_Document* pDoc, |
| 940 | CPDF_Dictionary* pAnnotDict) { |
| 941 | CFX_ByteTextBuf sAppStream; |
| 942 | CFX_ByteString sExtGSDictName = "GS"; |
| 943 | sAppStream << "/" << sExtGSDictName << " gs\n"; |
| 944 | |
| 945 | sAppStream << GenerateColorAP(CPVT_Color(CPVT_Color::kRGB, 1, 1, 0), |
| 946 | PaintOperation::FILL); |
| 947 | sAppStream << GenerateColorAP(CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
| 948 | PaintOperation::STROKE); |
| 949 | |
| 950 | const FX_FLOAT fBorderWidth = 1; |
| 951 | sAppStream << fBorderWidth << " w\n"; |
| 952 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 953 | CFX_FloatRect rect = pAnnotDict->GetRectFor("Rect"); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 954 | rect.Normalize(); |
| 955 | rect.Deflate(fBorderWidth / 2, fBorderWidth / 2); |
| 956 | |
| 957 | sAppStream << rect.left << " " << rect.bottom << " " << rect.Width() << " " |
| 958 | << rect.Height() << " re b\n"; |
| 959 | |
| 960 | CFX_ByteString sFontName = "FONT"; |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 961 | auto pResourceFontDict = GenerateResourceFontDict(pDoc, sFontName); |
| 962 | CPDF_Font* pDefFont = pDoc->LoadFont(pResourceFontDict.get()); |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 963 | if (!pDefFont) |
| 964 | return false; |
| 965 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 966 | auto pExtGStateDict = |
| 967 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
| 968 | auto pResourceDict = GenerateResourceDict(pDoc, std::move(pResourceFontDict), |
| 969 | std::move(pExtGStateDict)); |
| 970 | |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 971 | sAppStream << GetPopupContentsString(pDoc, *pAnnotDict, pDefFont, sFontName); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 972 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 973 | false /*IsTextMarkupAnnotation*/); |
jaepark | ab47ace | 2016-07-29 19:53:40 -0700 | [diff] [blame] | 974 | return true; |
| 975 | } |
| 976 | |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 977 | bool CPVT_GenerateAP::GenerateSquareAP(CPDF_Document* pDoc, |
| 978 | CPDF_Dictionary* pAnnotDict) { |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 979 | CFX_ByteTextBuf sAppStream; |
| 980 | CFX_ByteString sExtGSDictName = "GS"; |
| 981 | sAppStream << "/" << sExtGSDictName << " gs "; |
| 982 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 983 | CPDF_Array* pInteriorColor = pAnnotDict->GetArrayFor("IC"); |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 984 | sAppStream << GetColorStringWithDefault(pInteriorColor, |
| 985 | CPVT_Color(CPVT_Color::kTransparent), |
| 986 | PaintOperation::FILL); |
| 987 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 988 | sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayFor("C"), |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 989 | CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
| 990 | PaintOperation::STROKE); |
| 991 | |
| 992 | FX_FLOAT fBorderWidth = GetBorderWidth(*pAnnotDict); |
| 993 | bool bIsStrokeRect = fBorderWidth > 0; |
| 994 | |
| 995 | if (bIsStrokeRect) { |
| 996 | sAppStream << fBorderWidth << " w "; |
| 997 | sAppStream << GetDashPatternString(*pAnnotDict); |
| 998 | } |
| 999 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1000 | CFX_FloatRect rect = pAnnotDict->GetRectFor("Rect"); |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 1001 | rect.Normalize(); |
| 1002 | |
| 1003 | if (bIsStrokeRect) { |
| 1004 | // Deflating rect because stroking a path entails painting all points whose |
| 1005 | // perpendicular distance from the path in user space is less than or equal |
| 1006 | // to half the line width. |
| 1007 | rect.Deflate(fBorderWidth / 2, fBorderWidth / 2); |
| 1008 | } |
| 1009 | |
| 1010 | bool bIsFillRect = pInteriorColor && (pInteriorColor->GetCount() > 0); |
| 1011 | |
| 1012 | sAppStream << rect.left << " " << rect.bottom << " " << rect.Width() << " " |
| 1013 | << rect.Height() << " re " |
| 1014 | << GetPaintOperatorString(bIsStrokeRect, bIsFillRect) << "\n"; |
| 1015 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 1016 | auto pExtGStateDict = |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 1017 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 1018 | auto pResourceDict = |
| 1019 | GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); |
| 1020 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 1021 | false /*IsTextMarkupAnnotation*/); |
jaepark | af7ab33 | 2016-08-09 10:23:14 -0700 | [diff] [blame] | 1022 | return true; |
| 1023 | } |
| 1024 | |
jaepark | 96a0786 | 2016-08-03 14:17:02 -0700 | [diff] [blame] | 1025 | bool CPVT_GenerateAP::GenerateSquigglyAP(CPDF_Document* pDoc, |
| 1026 | CPDF_Dictionary* pAnnotDict) { |
jaepark | 96a0786 | 2016-08-03 14:17:02 -0700 | [diff] [blame] | 1027 | CFX_ByteTextBuf sAppStream; |
| 1028 | CFX_ByteString sExtGSDictName = "GS"; |
| 1029 | sAppStream << "/" << sExtGSDictName << " gs "; |
| 1030 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1031 | sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayFor("C"), |
jaepark | 96a0786 | 2016-08-03 14:17:02 -0700 | [diff] [blame] | 1032 | CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
| 1033 | PaintOperation::STROKE); |
| 1034 | |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 1035 | CFX_FloatRect rect = CPDF_Annot::RectFromQuadPoints(pAnnotDict); |
jaepark | 96a0786 | 2016-08-03 14:17:02 -0700 | [diff] [blame] | 1036 | rect.Normalize(); |
| 1037 | |
| 1038 | FX_FLOAT fLineWidth = 1.0; |
| 1039 | sAppStream << fLineWidth << " w "; |
| 1040 | |
| 1041 | const FX_FLOAT fDelta = 2.0; |
| 1042 | const FX_FLOAT fTop = rect.bottom + fDelta; |
| 1043 | const FX_FLOAT fBottom = rect.bottom; |
| 1044 | |
| 1045 | sAppStream << rect.left << " " << fTop << " m "; |
| 1046 | |
| 1047 | FX_FLOAT fX = rect.left + fDelta; |
| 1048 | bool isUpwards = false; |
| 1049 | |
| 1050 | while (fX < rect.right) { |
| 1051 | sAppStream << fX << " " << (isUpwards ? fTop : fBottom) << " l "; |
| 1052 | |
| 1053 | fX += fDelta; |
| 1054 | isUpwards = !isUpwards; |
| 1055 | } |
| 1056 | |
| 1057 | FX_FLOAT fRemainder = rect.right - (fX - fDelta); |
| 1058 | if (isUpwards) |
| 1059 | sAppStream << rect.right << " " << fBottom + fRemainder << " l "; |
| 1060 | else |
| 1061 | sAppStream << rect.right << " " << fTop - fRemainder << " l "; |
| 1062 | |
| 1063 | sAppStream << "S\n"; |
| 1064 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 1065 | auto pExtGStateDict = |
jaepark | 96a0786 | 2016-08-03 14:17:02 -0700 | [diff] [blame] | 1066 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 1067 | auto pResourceDict = |
| 1068 | GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); |
| 1069 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 1070 | true /*IsTextMarkupAnnotation*/); |
jaepark | 96a0786 | 2016-08-03 14:17:02 -0700 | [diff] [blame] | 1071 | return true; |
| 1072 | } |
| 1073 | |
jaepark | 0d8c2d1 | 2016-08-02 17:32:23 -0700 | [diff] [blame] | 1074 | bool CPVT_GenerateAP::GenerateStrikeOutAP(CPDF_Document* pDoc, |
| 1075 | CPDF_Dictionary* pAnnotDict) { |
jaepark | 0d8c2d1 | 2016-08-02 17:32:23 -0700 | [diff] [blame] | 1076 | CFX_ByteTextBuf sAppStream; |
| 1077 | CFX_ByteString sExtGSDictName = "GS"; |
| 1078 | sAppStream << "/" << sExtGSDictName << " gs "; |
| 1079 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1080 | sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayFor("C"), |
jaepark | 0d8c2d1 | 2016-08-02 17:32:23 -0700 | [diff] [blame] | 1081 | CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
| 1082 | PaintOperation::STROKE); |
| 1083 | |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 1084 | CFX_FloatRect rect = CPDF_Annot::RectFromQuadPoints(pAnnotDict); |
jaepark | 0d8c2d1 | 2016-08-02 17:32:23 -0700 | [diff] [blame] | 1085 | rect.Normalize(); |
| 1086 | |
| 1087 | FX_FLOAT fLineWidth = 1.0; |
| 1088 | FX_FLOAT fY = (rect.top + rect.bottom) / 2; |
| 1089 | sAppStream << fLineWidth << " w " << rect.left << " " << fY << " m " |
| 1090 | << rect.right << " " << fY << " l S\n"; |
| 1091 | |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 1092 | auto pExtGStateDict = |
jaepark | 0d8c2d1 | 2016-08-02 17:32:23 -0700 | [diff] [blame] | 1093 | GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
thestig | 69bbfa8 | 2016-11-10 10:39:01 -0800 | [diff] [blame] | 1094 | auto pResourceDict = |
| 1095 | GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); |
| 1096 | GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), |
tonikitoo | 0a17faf | 2016-09-15 13:50:50 -0700 | [diff] [blame] | 1097 | true /*IsTextMarkupAnnotation*/); |
jaepark | 0d8c2d1 | 2016-08-02 17:32:23 -0700 | [diff] [blame] | 1098 | return true; |
| 1099 | } |
| 1100 | |
jaepark | 9c987e9 | 2016-07-29 18:17:44 -0700 | [diff] [blame] | 1101 | // Static. |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1102 | CFX_ByteString CPVT_GenerateAP::GenerateEditAP( |
| 1103 | IPVT_FontMap* pFontMap, |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 1104 | CPDF_VariableText::Iterator* pIterator, |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1105 | const CFX_FloatPoint& ptOffset, |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 1106 | bool bContinuous, |
thestig | 2c3a16a | 2016-05-10 13:24:16 -0700 | [diff] [blame] | 1107 | uint16_t SubWord) { |
thestig | 594b20b | 2016-05-12 21:56:43 -0700 | [diff] [blame] | 1108 | CFX_ByteTextBuf sEditStream; |
| 1109 | CFX_ByteTextBuf sLineStream; |
| 1110 | CFX_ByteTextBuf sWords; |
tsepez | 63f545c | 2016-09-13 16:08:49 -0700 | [diff] [blame] | 1111 | CFX_FloatPoint ptOld; |
| 1112 | CFX_FloatPoint ptNew; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1113 | int32_t nCurFontIndex = -1; |
tsepez | 63f545c | 2016-09-13 16:08:49 -0700 | [diff] [blame] | 1114 | CPVT_WordPlace oldplace; |
thestig | 594b20b | 2016-05-12 21:56:43 -0700 | [diff] [blame] | 1115 | |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1116 | pIterator->SetAt(0); |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1117 | while (pIterator->NextWord()) { |
| 1118 | CPVT_WordPlace place = pIterator->GetAt(); |
| 1119 | if (bContinuous) { |
| 1120 | if (place.LineCmp(oldplace) != 0) { |
| 1121 | if (sWords.GetSize() > 0) { |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 1122 | sLineStream << GetWordRenderString(sWords.MakeString()); |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1123 | sEditStream << sLineStream; |
| 1124 | sLineStream.Clear(); |
| 1125 | sWords.Clear(); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1126 | } |
| 1127 | CPVT_Word word; |
| 1128 | if (pIterator->GetWord(word)) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1129 | ptNew = CFX_FloatPoint(word.ptWord.x + ptOffset.x, |
| 1130 | word.ptWord.y + ptOffset.y); |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1131 | } else { |
| 1132 | CPVT_Line line; |
| 1133 | pIterator->GetLine(line); |
| 1134 | ptNew = CFX_FloatPoint(line.ptLine.x + ptOffset.x, |
| 1135 | line.ptLine.y + ptOffset.y); |
| 1136 | } |
tsepez | 63f545c | 2016-09-13 16:08:49 -0700 | [diff] [blame] | 1137 | if (ptNew != ptOld) { |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1138 | sLineStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y |
| 1139 | << " Td\n"; |
| 1140 | ptOld = ptNew; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1143 | CPVT_Word word; |
| 1144 | if (pIterator->GetWord(word)) { |
| 1145 | if (word.nFontIndex != nCurFontIndex) { |
| 1146 | if (sWords.GetSize() > 0) { |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 1147 | sLineStream << GetWordRenderString(sWords.MakeString()); |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1148 | sWords.Clear(); |
| 1149 | } |
| 1150 | sLineStream << GetFontSetString(pFontMap, word.nFontIndex, |
| 1151 | word.fFontSize); |
| 1152 | nCurFontIndex = word.nFontIndex; |
| 1153 | } |
| 1154 | sWords << GetPDFWordString(pFontMap, nCurFontIndex, word.Word, SubWord); |
| 1155 | } |
| 1156 | oldplace = place; |
| 1157 | } else { |
| 1158 | CPVT_Word word; |
| 1159 | if (pIterator->GetWord(word)) { |
| 1160 | ptNew = CFX_FloatPoint(word.ptWord.x + ptOffset.x, |
| 1161 | word.ptWord.y + ptOffset.y); |
tsepez | 63f545c | 2016-09-13 16:08:49 -0700 | [diff] [blame] | 1162 | if (ptNew != ptOld) { |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1163 | sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y |
| 1164 | << " Td\n"; |
| 1165 | ptOld = ptNew; |
| 1166 | } |
| 1167 | if (word.nFontIndex != nCurFontIndex) { |
| 1168 | sEditStream << GetFontSetString(pFontMap, word.nFontIndex, |
| 1169 | word.fFontSize); |
| 1170 | nCurFontIndex = word.nFontIndex; |
| 1171 | } |
| 1172 | sEditStream << GetWordRenderString( |
| 1173 | GetPDFWordString(pFontMap, nCurFontIndex, word.Word, SubWord)); |
| 1174 | } |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1175 | } |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1176 | } |
| 1177 | if (sWords.GetSize() > 0) { |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 1178 | sLineStream << GetWordRenderString(sWords.MakeString()); |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 1179 | sEditStream << sLineStream; |
| 1180 | sWords.Clear(); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1181 | } |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 1182 | return sEditStream.MakeString(); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | // Static. |
| 1186 | CFX_ByteString CPVT_GenerateAP::GenerateBorderAP( |
| 1187 | const CFX_FloatRect& rect, |
| 1188 | FX_FLOAT fWidth, |
| 1189 | const CPVT_Color& color, |
| 1190 | const CPVT_Color& crLeftTop, |
| 1191 | const CPVT_Color& crRightBottom, |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 1192 | BorderStyle nStyle, |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1193 | const CPVT_Dash& dash) { |
| 1194 | CFX_ByteTextBuf sAppStream; |
| 1195 | CFX_ByteString sColor; |
| 1196 | FX_FLOAT fLeft = rect.left; |
| 1197 | FX_FLOAT fRight = rect.right; |
| 1198 | FX_FLOAT fTop = rect.top; |
| 1199 | FX_FLOAT fBottom = rect.bottom; |
| 1200 | if (fWidth > 0.0f) { |
| 1201 | FX_FLOAT fHalfWidth = fWidth / 2.0f; |
| 1202 | switch (nStyle) { |
| 1203 | default: |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 1204 | case BorderStyle::SOLID: |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1205 | sColor = GenerateColorAP(color, PaintOperation::FILL); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1206 | if (sColor.GetLength() > 0) { |
| 1207 | sAppStream << sColor; |
| 1208 | sAppStream << fLeft << " " << fBottom << " " << fRight - fLeft << " " |
| 1209 | << fTop - fBottom << " re\n"; |
| 1210 | sAppStream << fLeft + fWidth << " " << fBottom + fWidth << " " |
| 1211 | << fRight - fLeft - fWidth * 2 << " " |
| 1212 | << fTop - fBottom - fWidth * 2 << " re\n"; |
| 1213 | sAppStream << "f*\n"; |
| 1214 | } |
| 1215 | break; |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 1216 | case BorderStyle::DASH: |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1217 | sColor = GenerateColorAP(color, PaintOperation::STROKE); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1218 | if (sColor.GetLength() > 0) { |
| 1219 | sAppStream << sColor; |
| 1220 | sAppStream << fWidth << " w" |
| 1221 | << " [" << dash.nDash << " " << dash.nGap << "] " |
| 1222 | << dash.nPhase << " d\n"; |
| 1223 | sAppStream << fLeft + fWidth / 2 << " " << fBottom + fWidth / 2 |
| 1224 | << " m\n"; |
| 1225 | sAppStream << fLeft + fWidth / 2 << " " << fTop - fWidth / 2 |
| 1226 | << " l\n"; |
| 1227 | sAppStream << fRight - fWidth / 2 << " " << fTop - fWidth / 2 |
| 1228 | << " l\n"; |
| 1229 | sAppStream << fRight - fWidth / 2 << " " << fBottom + fWidth / 2 |
| 1230 | << " l\n"; |
| 1231 | sAppStream << fLeft + fWidth / 2 << " " << fBottom + fWidth / 2 |
| 1232 | << " l S\n"; |
| 1233 | } |
| 1234 | break; |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 1235 | case BorderStyle::BEVELED: |
| 1236 | case BorderStyle::INSET: |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1237 | sColor = GenerateColorAP(crLeftTop, PaintOperation::FILL); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1238 | if (sColor.GetLength() > 0) { |
| 1239 | sAppStream << sColor; |
| 1240 | sAppStream << fLeft + fHalfWidth << " " << fBottom + fHalfWidth |
| 1241 | << " m\n"; |
| 1242 | sAppStream << fLeft + fHalfWidth << " " << fTop - fHalfWidth |
| 1243 | << " l\n"; |
| 1244 | sAppStream << fRight - fHalfWidth << " " << fTop - fHalfWidth |
| 1245 | << " l\n"; |
| 1246 | sAppStream << fRight - fHalfWidth * 2 << " " << fTop - fHalfWidth * 2 |
| 1247 | << " l\n"; |
| 1248 | sAppStream << fLeft + fHalfWidth * 2 << " " << fTop - fHalfWidth * 2 |
| 1249 | << " l\n"; |
| 1250 | sAppStream << fLeft + fHalfWidth * 2 << " " |
| 1251 | << fBottom + fHalfWidth * 2 << " l f\n"; |
| 1252 | } |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1253 | sColor = GenerateColorAP(crRightBottom, PaintOperation::FILL); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1254 | if (sColor.GetLength() > 0) { |
| 1255 | sAppStream << sColor; |
| 1256 | sAppStream << fRight - fHalfWidth << " " << fTop - fHalfWidth |
| 1257 | << " m\n"; |
| 1258 | sAppStream << fRight - fHalfWidth << " " << fBottom + fHalfWidth |
| 1259 | << " l\n"; |
| 1260 | sAppStream << fLeft + fHalfWidth << " " << fBottom + fHalfWidth |
| 1261 | << " l\n"; |
| 1262 | sAppStream << fLeft + fHalfWidth * 2 << " " |
| 1263 | << fBottom + fHalfWidth * 2 << " l\n"; |
| 1264 | sAppStream << fRight - fHalfWidth * 2 << " " |
| 1265 | << fBottom + fHalfWidth * 2 << " l\n"; |
| 1266 | sAppStream << fRight - fHalfWidth * 2 << " " << fTop - fHalfWidth * 2 |
| 1267 | << " l f\n"; |
| 1268 | } |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1269 | sColor = GenerateColorAP(color, PaintOperation::FILL); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1270 | if (sColor.GetLength() > 0) { |
| 1271 | sAppStream << sColor; |
| 1272 | sAppStream << fLeft << " " << fBottom << " " << fRight - fLeft << " " |
| 1273 | << fTop - fBottom << " re\n"; |
| 1274 | sAppStream << fLeft + fHalfWidth << " " << fBottom + fHalfWidth << " " |
| 1275 | << fRight - fLeft - fHalfWidth * 2 << " " |
| 1276 | << fTop - fBottom - fHalfWidth * 2 << " re f*\n"; |
| 1277 | } |
| 1278 | break; |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 1279 | case BorderStyle::UNDERLINE: |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1280 | sColor = GenerateColorAP(color, PaintOperation::STROKE); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1281 | if (sColor.GetLength() > 0) { |
| 1282 | sAppStream << sColor; |
| 1283 | sAppStream << fWidth << " w\n"; |
| 1284 | sAppStream << fLeft << " " << fBottom + fWidth / 2 << " m\n"; |
| 1285 | sAppStream << fRight << " " << fBottom + fWidth / 2 << " l S\n"; |
| 1286 | } |
| 1287 | break; |
| 1288 | } |
| 1289 | } |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 1290 | return sAppStream.MakeString(); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | // Static. |
| 1294 | CFX_ByteString CPVT_GenerateAP::GenerateColorAP(const CPVT_Color& color, |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1295 | PaintOperation nOperation) { |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1296 | CFX_ByteTextBuf sColorStream; |
| 1297 | switch (color.nColorType) { |
| 1298 | case CPVT_Color::kRGB: |
| 1299 | sColorStream << color.fColor1 << " " << color.fColor2 << " " |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1300 | << color.fColor3 << " " |
| 1301 | << (nOperation == PaintOperation::STROKE ? "RG" : "rg") |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1302 | << "\n"; |
| 1303 | break; |
| 1304 | case CPVT_Color::kGray: |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1305 | sColorStream << color.fColor1 << " " |
| 1306 | << (nOperation == PaintOperation::STROKE ? "G" : "g") |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1307 | << "\n"; |
| 1308 | break; |
| 1309 | case CPVT_Color::kCMYK: |
| 1310 | sColorStream << color.fColor1 << " " << color.fColor2 << " " |
| 1311 | << color.fColor3 << " " << color.fColor4 << " " |
jaepark | b2e6337 | 2016-08-02 12:25:15 -0700 | [diff] [blame] | 1312 | << (nOperation == PaintOperation::STROKE ? "K" : "k") |
| 1313 | << "\n"; |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1314 | break; |
| 1315 | case CPVT_Color::kTransparent: |
| 1316 | break; |
| 1317 | } |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 1318 | return sColorStream.MakeString(); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | // Static. |
| 1322 | CFX_ByteString CPVT_GenerateAP::GetPDFWordString(IPVT_FontMap* pFontMap, |
| 1323 | int32_t nFontIndex, |
| 1324 | uint16_t Word, |
| 1325 | uint16_t SubWord) { |
| 1326 | CFX_ByteString sWord; |
| 1327 | if (SubWord > 0) { |
| 1328 | sWord.Format("%c", SubWord); |
| 1329 | return sWord; |
| 1330 | } |
| 1331 | |
| 1332 | if (!pFontMap) |
| 1333 | return sWord; |
| 1334 | |
| 1335 | if (CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex)) { |
| 1336 | if (pPDFFont->GetBaseFont().Compare("Symbol") == 0 || |
| 1337 | pPDFFont->GetBaseFont().Compare("ZapfDingbats") == 0) { |
| 1338 | sWord.Format("%c", Word); |
| 1339 | } else { |
| 1340 | uint32_t dwCharCode = pPDFFont->CharCodeFromUnicode(Word); |
| 1341 | if (dwCharCode != CPDF_Font::kInvalidCharCode) |
| 1342 | pPDFFont->AppendChar(sWord, dwCharCode); |
| 1343 | } |
| 1344 | } |
| 1345 | return sWord; |
| 1346 | } |
| 1347 | |
| 1348 | // Static. |
| 1349 | CFX_ByteString CPVT_GenerateAP::GetWordRenderString( |
| 1350 | const CFX_ByteString& strWords) { |
| 1351 | if (strWords.GetLength() > 0) |
| 1352 | return PDF_EncodeString(strWords) + " Tj\n"; |
| 1353 | return ""; |
| 1354 | } |
| 1355 | |
| 1356 | // Static. |
| 1357 | CFX_ByteString CPVT_GenerateAP::GetFontSetString(IPVT_FontMap* pFontMap, |
| 1358 | int32_t nFontIndex, |
| 1359 | FX_FLOAT fFontSize) { |
| 1360 | CFX_ByteTextBuf sRet; |
| 1361 | if (pFontMap) { |
| 1362 | CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex); |
| 1363 | if (sFontAlias.GetLength() > 0 && fFontSize > 0) |
| 1364 | sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n"; |
| 1365 | } |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 1366 | return sRet.MakeString(); |
dsinclair | 777b333 | 2016-03-31 20:03:08 -0700 | [diff] [blame] | 1367 | } |