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