| OLD | NEW |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "core/fpdfapi/page/cpdf_streamcontentparser.h" | 7 #include "core/fpdfapi/page/cpdf_streamcontentparser.h" |
| 8 | 8 |
| 9 #include <memory> |
| 10 #include <utility> |
| 11 #include <vector> |
| 12 |
| 9 #include "core/fpdfapi/font/cpdf_font.h" | 13 #include "core/fpdfapi/font/cpdf_font.h" |
| 10 #include "core/fpdfapi/font/cpdf_type3font.h" | 14 #include "core/fpdfapi/font/cpdf_type3font.h" |
| 11 #include "core/fpdfapi/page/cpdf_allstates.h" | 15 #include "core/fpdfapi/page/cpdf_allstates.h" |
| 12 #include "core/fpdfapi/page/cpdf_docpagedata.h" | 16 #include "core/fpdfapi/page/cpdf_docpagedata.h" |
| 13 #include "core/fpdfapi/page/cpdf_form.h" | 17 #include "core/fpdfapi/page/cpdf_form.h" |
| 14 #include "core/fpdfapi/page/cpdf_formobject.h" | 18 #include "core/fpdfapi/page/cpdf_formobject.h" |
| 15 #include "core/fpdfapi/page/cpdf_image.h" | 19 #include "core/fpdfapi/page/cpdf_image.h" |
| 16 #include "core/fpdfapi/page/cpdf_imageobject.h" | 20 #include "core/fpdfapi/page/cpdf_imageobject.h" |
| 17 #include "core/fpdfapi/page/cpdf_meshstream.h" | 21 #include "core/fpdfapi/page/cpdf_meshstream.h" |
| 18 #include "core/fpdfapi/page/cpdf_pageobject.h" | 22 #include "core/fpdfapi/page/cpdf_pageobject.h" |
| 19 #include "core/fpdfapi/page/cpdf_pathobject.h" | 23 #include "core/fpdfapi/page/cpdf_pathobject.h" |
| 20 #include "core/fpdfapi/page/cpdf_shadingobject.h" | 24 #include "core/fpdfapi/page/cpdf_shadingobject.h" |
| 21 #include "core/fpdfapi/page/cpdf_shadingpattern.h" | 25 #include "core/fpdfapi/page/cpdf_shadingpattern.h" |
| 22 #include "core/fpdfapi/page/cpdf_textobject.h" | 26 #include "core/fpdfapi/page/cpdf_textobject.h" |
| 23 #include "core/fpdfapi/page/pageint.h" | 27 #include "core/fpdfapi/page/pageint.h" |
| 24 #include "core/fpdfapi/parser/cpdf_array.h" | 28 #include "core/fpdfapi/parser/cpdf_array.h" |
| 29 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 25 #include "core/fpdfapi/parser/cpdf_document.h" | 30 #include "core/fpdfapi/parser/cpdf_document.h" |
| 26 #include "core/fpdfapi/parser/cpdf_name.h" | 31 #include "core/fpdfapi/parser/cpdf_name.h" |
| 27 #include "core/fpdfapi/parser/cpdf_number.h" | 32 #include "core/fpdfapi/parser/cpdf_number.h" |
| 33 #include "core/fpdfapi/parser/cpdf_stream.h" |
| 28 #include "core/fpdfapi/parser/fpdf_parser_decode.h" | 34 #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
| 29 #include "core/fxcrt/fx_safe_types.h" | 35 #include "core/fxcrt/fx_safe_types.h" |
| 36 #include "core/fxge/cfx_graphstatedata.h" |
| 30 #include "third_party/base/ptr_util.h" | 37 #include "third_party/base/ptr_util.h" |
| 31 | 38 |
| 32 namespace { | 39 namespace { |
| 33 | 40 |
| 34 const int kMaxFormLevel = 30; | 41 const int kMaxFormLevel = 30; |
| 35 | 42 |
| 36 const int kSingleCoordinatePair = 1; | 43 const int kSingleCoordinatePair = 1; |
| 37 const int kTensorCoordinatePairs = 16; | 44 const int kTensorCoordinatePairs = 16; |
| 38 const int kCoonsCoordinatePairs = 12; | 45 const int kCoonsCoordinatePairs = 12; |
| 39 const int kSingleColorPerPatch = 1; | 46 const int kSingleColorPerPatch = 1; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 break; | 122 break; |
| 116 | 123 |
| 117 stream.BitStream()->SkipBits(nBits.ValueOrDie()); | 124 stream.BitStream()->SkipBits(nBits.ValueOrDie()); |
| 118 if (bGouraud) | 125 if (bGouraud) |
| 119 stream.BitStream()->ByteAlign(); | 126 stream.BitStream()->ByteAlign(); |
| 120 } | 127 } |
| 121 rect.Transform(&matrix); | 128 rect.Transform(&matrix); |
| 122 return rect; | 129 return rect; |
| 123 } | 130 } |
| 124 | 131 |
| 132 struct AbbrPair { |
| 133 const FX_CHAR* abbr; |
| 134 const FX_CHAR* full_name; |
| 135 }; |
| 136 |
| 137 const AbbrPair InlineKeyAbbr[] = { |
| 138 {"BPC", "BitsPerComponent"}, {"CS", "ColorSpace"}, {"D", "Decode"}, |
| 139 {"DP", "DecodeParms"}, {"F", "Filter"}, {"H", "Height"}, |
| 140 {"IM", "ImageMask"}, {"I", "Interpolate"}, {"W", "Width"}, |
| 141 }; |
| 142 |
| 143 const AbbrPair InlineValueAbbr[] = { |
| 144 {"G", "DeviceGray"}, {"RGB", "DeviceRGB"}, |
| 145 {"CMYK", "DeviceCMYK"}, {"I", "Indexed"}, |
| 146 {"AHx", "ASCIIHexDecode"}, {"A85", "ASCII85Decode"}, |
| 147 {"LZW", "LZWDecode"}, {"Fl", "FlateDecode"}, |
| 148 {"RL", "RunLengthDecode"}, {"CCF", "CCITTFaxDecode"}, |
| 149 {"DCT", "DCTDecode"}, |
| 150 }; |
| 151 |
| 152 struct AbbrReplacementOp { |
| 153 bool is_replace_key; |
| 154 CFX_ByteString key; |
| 155 CFX_ByteStringC replacement; |
| 156 }; |
| 157 |
| 158 CFX_ByteStringC FindFullName(const AbbrPair* table, |
| 159 size_t count, |
| 160 const CFX_ByteStringC& abbr) { |
| 161 auto it = std::find_if(table, table + count, [abbr](const AbbrPair& pair) { |
| 162 return pair.abbr == abbr; |
| 163 }); |
| 164 return it != table + count ? CFX_ByteStringC(it->full_name) |
| 165 : CFX_ByteStringC(); |
| 166 } |
| 167 |
| 168 void ReplaceAbbr(CPDF_Object* pObj) { |
| 169 switch (pObj->GetType()) { |
| 170 case CPDF_Object::DICTIONARY: { |
| 171 CPDF_Dictionary* pDict = pObj->AsDictionary(); |
| 172 std::vector<AbbrReplacementOp> replacements; |
| 173 for (const auto& it : *pDict) { |
| 174 CFX_ByteString key = it.first; |
| 175 CPDF_Object* value = it.second; |
| 176 CFX_ByteStringC fullname = FindFullName( |
| 177 InlineKeyAbbr, FX_ArraySize(InlineKeyAbbr), key.AsStringC()); |
| 178 if (!fullname.IsEmpty()) { |
| 179 AbbrReplacementOp op; |
| 180 op.is_replace_key = true; |
| 181 op.key = key; |
| 182 op.replacement = fullname; |
| 183 replacements.push_back(op); |
| 184 key = fullname; |
| 185 } |
| 186 |
| 187 if (value->IsName()) { |
| 188 CFX_ByteString name = value->GetString(); |
| 189 fullname = FindFullName( |
| 190 InlineValueAbbr, FX_ArraySize(InlineValueAbbr), name.AsStringC()); |
| 191 if (!fullname.IsEmpty()) { |
| 192 AbbrReplacementOp op; |
| 193 op.is_replace_key = false; |
| 194 op.key = key; |
| 195 op.replacement = fullname; |
| 196 replacements.push_back(op); |
| 197 } |
| 198 } else { |
| 199 ReplaceAbbr(value); |
| 200 } |
| 201 } |
| 202 for (const auto& op : replacements) { |
| 203 if (op.is_replace_key) |
| 204 pDict->ReplaceKey(op.key, CFX_ByteString(op.replacement)); |
| 205 else |
| 206 pDict->SetNameFor(op.key, CFX_ByteString(op.replacement)); |
| 207 } |
| 208 break; |
| 209 } |
| 210 case CPDF_Object::ARRAY: { |
| 211 CPDF_Array* pArray = pObj->AsArray(); |
| 212 for (size_t i = 0; i < pArray->GetCount(); i++) { |
| 213 CPDF_Object* pElement = pArray->GetObjectAt(i); |
| 214 if (pElement->IsName()) { |
| 215 CFX_ByteString name = pElement->GetString(); |
| 216 CFX_ByteStringC fullname = FindFullName( |
| 217 InlineValueAbbr, FX_ArraySize(InlineValueAbbr), name.AsStringC()); |
| 218 if (!fullname.IsEmpty()) |
| 219 pArray->SetAt(i, new CPDF_Name(CFX_ByteString(fullname))); |
| 220 } else { |
| 221 ReplaceAbbr(pElement); |
| 222 } |
| 223 } |
| 224 break; |
| 225 } |
| 226 default: |
| 227 break; |
| 228 } |
| 229 } |
| 230 |
| 125 } // namespace | 231 } // namespace |
| 126 | 232 |
| 233 CFX_ByteStringC PDF_FindKeyAbbreviationForTesting(const CFX_ByteStringC& abbr) { |
| 234 return FindFullName(InlineKeyAbbr, FX_ArraySize(InlineKeyAbbr), abbr); |
| 235 } |
| 236 |
| 237 CFX_ByteStringC PDF_FindValueAbbreviationForTesting( |
| 238 const CFX_ByteStringC& abbr) { |
| 239 return FindFullName(InlineValueAbbr, FX_ArraySize(InlineValueAbbr), abbr); |
| 240 } |
| 241 |
| 127 CPDF_StreamContentParser::CPDF_StreamContentParser( | 242 CPDF_StreamContentParser::CPDF_StreamContentParser( |
| 128 CPDF_Document* pDocument, | 243 CPDF_Document* pDocument, |
| 129 CPDF_Dictionary* pPageResources, | 244 CPDF_Dictionary* pPageResources, |
| 130 CPDF_Dictionary* pParentResources, | 245 CPDF_Dictionary* pParentResources, |
| 131 const CFX_Matrix* pmtContentToUser, | 246 const CFX_Matrix* pmtContentToUser, |
| 132 CPDF_PageObjectHolder* pObjHolder, | 247 CPDF_PageObjectHolder* pObjHolder, |
| 133 CPDF_Dictionary* pResources, | 248 CPDF_Dictionary* pResources, |
| 134 CFX_FloatRect* pBBox, | 249 CFX_FloatRect* pBBox, |
| 135 CPDF_AllStates* pStates, | 250 CPDF_AllStates* pStates, |
| 136 int level) | 251 int level) |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 m_pSyntax->GetWordSize() - 1); | 651 m_pSyntax->GetWordSize() - 1); |
| 537 std::unique_ptr<CPDF_Object> pObj(m_pSyntax->ReadNextObject(false, 0)); | 652 std::unique_ptr<CPDF_Object> pObj(m_pSyntax->ReadNextObject(false, 0)); |
| 538 if (!key.IsEmpty()) { | 653 if (!key.IsEmpty()) { |
| 539 uint32_t dwObjNum = pObj ? pObj->GetObjNum() : 0; | 654 uint32_t dwObjNum = pObj ? pObj->GetObjNum() : 0; |
| 540 if (dwObjNum) | 655 if (dwObjNum) |
| 541 pDict->SetReferenceFor(key, m_pDocument, dwObjNum); | 656 pDict->SetReferenceFor(key, m_pDocument, dwObjNum); |
| 542 else | 657 else |
| 543 pDict->SetFor(key, pObj.release()); | 658 pDict->SetFor(key, pObj.release()); |
| 544 } | 659 } |
| 545 } | 660 } |
| 546 PDF_ReplaceAbbr(pDict); | 661 ReplaceAbbr(pDict); |
| 547 CPDF_Object* pCSObj = nullptr; | 662 CPDF_Object* pCSObj = nullptr; |
| 548 if (pDict->KeyExist("ColorSpace")) { | 663 if (pDict->KeyExist("ColorSpace")) { |
| 549 pCSObj = pDict->GetDirectObjectFor("ColorSpace"); | 664 pCSObj = pDict->GetDirectObjectFor("ColorSpace"); |
| 550 if (pCSObj->IsName()) { | 665 if (pCSObj->IsName()) { |
| 551 CFX_ByteString name = pCSObj->GetString(); | 666 CFX_ByteString name = pCSObj->GetString(); |
| 552 if (name != "DeviceRGB" && name != "DeviceGray" && name != "DeviceCMYK") { | 667 if (name != "DeviceRGB" && name != "DeviceGray" && name != "DeviceCMYK") { |
| 553 pCSObj = FindResourceObj("ColorSpace", name); | 668 pCSObj = FindResourceObj("ColorSpace", name); |
| 554 if (pCSObj && pCSObj->IsInline()) { | 669 if (pCSObj && pCSObj->IsInline()) { |
| 555 pCSObj = pCSObj->Clone(); | 670 pCSObj = pCSObj->Clone(); |
| 556 pDict->SetFor("ColorSpace", pCSObj); | 671 pDict->SetFor("ColorSpace", pCSObj); |
| (...skipping 993 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1550 } | 1665 } |
| 1551 default: | 1666 default: |
| 1552 bProcessed = false; | 1667 bProcessed = false; |
| 1553 } | 1668 } |
| 1554 if (!bProcessed) { | 1669 if (!bProcessed) { |
| 1555 m_pSyntax->SetPos(last_pos); | 1670 m_pSyntax->SetPos(last_pos); |
| 1556 return; | 1671 return; |
| 1557 } | 1672 } |
| 1558 } | 1673 } |
| 1559 } | 1674 } |
| OLD | NEW |