John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_transformpage.h" |
| 8 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 9 | #include <memory> |
Henrique Nakashima | 8e052a6 | 2017-06-27 17:14:31 -0400 | [diff] [blame] | 10 | #include <sstream> |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/page/cpdf_clippath.h" |
| 14 | #include "core/fpdfapi/page/cpdf_page.h" |
| 15 | #include "core/fpdfapi/page/cpdf_pageobject.h" |
| 16 | #include "core/fpdfapi/page/cpdf_path.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 17 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 18 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 19 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 20 | #include "core/fpdfapi/parser/cpdf_reference.h" |
| 21 | #include "core/fpdfapi/parser/cpdf_stream.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 22 | #include "core/fxge/cfx_pathdata.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 23 | #include "fpdfsdk/fsdk_define.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 24 | |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 27 | void SetBoundingBox(CPDF_Page* page, |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 28 | const CFX_ByteString& key, |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 29 | float left, |
| 30 | float bottom, |
| 31 | float right, |
| 32 | float top) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 33 | CPDF_Array* pBoundingBoxArray = page->m_pFormDict->SetNewFor<CPDF_Array>(key); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 34 | pBoundingBoxArray->AddNew<CPDF_Number>(left); |
| 35 | pBoundingBoxArray->AddNew<CPDF_Number>(bottom); |
| 36 | pBoundingBoxArray->AddNew<CPDF_Number>(right); |
| 37 | pBoundingBoxArray->AddNew<CPDF_Number>(top); |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 38 | } |
| 39 | |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 40 | bool GetBoundingBox(CPDF_Page* page, |
| 41 | const CFX_ByteString& key, |
| 42 | float* left, |
| 43 | float* bottom, |
| 44 | float* right, |
| 45 | float* top) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 46 | CPDF_Array* pArray = page->m_pFormDict->GetArrayFor(key); |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 47 | if (!pArray) |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 48 | return false; |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 49 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 50 | *left = pArray->GetFloatAt(0); |
| 51 | *bottom = pArray->GetFloatAt(1); |
| 52 | *right = pArray->GetFloatAt(2); |
| 53 | *top = pArray->GetFloatAt(3); |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 54 | return true; |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 59 | DLLEXPORT void STDCALL FPDFPage_SetMediaBox(FPDF_PAGE page, |
| 60 | float left, |
| 61 | float bottom, |
| 62 | float right, |
| 63 | float top) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 64 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 65 | if (!pPage) |
| 66 | return; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 67 | |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 68 | SetBoundingBox(pPage, "MediaBox", left, bottom, right, top); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | DLLEXPORT void STDCALL FPDFPage_SetCropBox(FPDF_PAGE page, |
| 72 | float left, |
| 73 | float bottom, |
| 74 | float right, |
| 75 | float top) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 76 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | if (!pPage) |
| 78 | return; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 79 | |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 80 | SetBoundingBox(pPage, "CropBox", left, bottom, right, top); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetMediaBox(FPDF_PAGE page, |
| 84 | float* left, |
| 85 | float* bottom, |
| 86 | float* right, |
| 87 | float* top) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 88 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 89 | return pPage && GetBoundingBox(pPage, "MediaBox", left, bottom, right, top); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetCropBox(FPDF_PAGE page, |
| 93 | float* left, |
| 94 | float* bottom, |
| 95 | float* right, |
| 96 | float* top) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 97 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 98 | return pPage && GetBoundingBox(pPage, "CropBox", left, bottom, right, top); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, |
| 102 | FS_MATRIX* matrix, |
| 103 | FS_RECTF* clipRect) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 104 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | if (!pPage) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 106 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 107 | |
Henrique Nakashima | 8e052a6 | 2017-06-27 17:14:31 -0400 | [diff] [blame] | 108 | std::ostringstream textBuf; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 109 | textBuf << "q "; |
| 110 | CFX_FloatRect rect(clipRect->left, clipRect->bottom, clipRect->right, |
| 111 | clipRect->top); |
| 112 | rect.Normalize(); |
| 113 | CFX_ByteString bsClipping; |
| 114 | bsClipping.Format("%f %f %f %f re W* n ", rect.left, rect.bottom, |
| 115 | rect.Width(), rect.Height()); |
| 116 | textBuf << bsClipping; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 117 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | CFX_ByteString bsMatix; |
| 119 | bsMatix.Format("%f %f %f %f %f %f cm ", matrix->a, matrix->b, matrix->c, |
| 120 | matrix->d, matrix->e, matrix->f); |
| 121 | textBuf << bsMatix; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 122 | |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 123 | CPDF_Dictionary* pPageDic = pPage->m_pFormDict.Get(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 124 | CPDF_Object* pContentObj = |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 125 | pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | if (!pContentObj) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 127 | pContentObj = pPageDic ? pPageDic->GetArrayFor("Contents") : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 128 | if (!pContentObj) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 129 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 130 | |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 131 | CPDF_Document* pDoc = pPage->m_pDocument.Get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 133 | return false; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 134 | |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 135 | CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>( |
| 136 | nullptr, 0, |
| 137 | pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool())); |
Henrique Nakashima | 8e052a6 | 2017-06-27 17:14:31 -0400 | [diff] [blame] | 138 | pStream->SetData(&textBuf); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 139 | |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 140 | CPDF_Stream* pEndStream = pDoc->NewIndirect<CPDF_Stream>( |
| 141 | nullptr, 0, |
| 142 | pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool())); |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 143 | pEndStream->SetData((const uint8_t*)" Q", 2); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 144 | |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 145 | CPDF_Array* pContentArray = nullptr; |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 146 | CPDF_Array* pArray = ToArray(pContentObj); |
| 147 | if (pArray) { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 148 | pContentArray = pArray; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 149 | pContentArray->InsertNewAt<CPDF_Reference>(0, pDoc, pStream->GetObjNum()); |
| 150 | pContentArray->AddNew<CPDF_Reference>(pDoc, pEndStream->GetObjNum()); |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 151 | } else if (CPDF_Reference* pReference = ToReference(pContentObj)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 152 | CPDF_Object* pDirectObj = pReference->GetDirect(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 153 | if (pDirectObj) { |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 154 | CPDF_Array* pObjArray = pDirectObj->AsArray(); |
| 155 | if (pObjArray) { |
| 156 | pContentArray = pObjArray; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 157 | pContentArray->InsertNewAt<CPDF_Reference>(0, pDoc, |
| 158 | pStream->GetObjNum()); |
| 159 | pContentArray->AddNew<CPDF_Reference>(pDoc, pEndStream->GetObjNum()); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 160 | } else if (pDirectObj->IsStream()) { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 161 | pContentArray = pDoc->NewIndirect<CPDF_Array>(); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 162 | pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum()); |
| 163 | pContentArray->AddNew<CPDF_Reference>(pDoc, pDirectObj->GetObjNum()); |
| 164 | pContentArray->AddNew<CPDF_Reference>(pDoc, pEndStream->GetObjNum()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 165 | pPageDic->SetNewFor<CPDF_Reference>("Contents", pDoc, |
| 166 | pContentArray->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 170 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 171 | // Need to transform the patterns as well. |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 172 | CPDF_Dictionary* pRes = pPageDic->GetDictFor("Resources"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 173 | if (pRes) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 174 | CPDF_Dictionary* pPattenDict = pRes->GetDictFor("Pattern"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | if (pPattenDict) { |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 176 | for (const auto& it : *pPattenDict) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 177 | CPDF_Object* pObj = it.second.get(); |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 178 | if (pObj->IsReference()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 179 | pObj = pObj->GetDirect(); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 180 | |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 181 | CPDF_Dictionary* pDict = nullptr; |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 182 | if (pObj->IsDictionary()) |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 183 | pDict = pObj->AsDictionary(); |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 184 | else if (CPDF_Stream* pObjStream = pObj->AsStream()) |
| 185 | pDict = pObjStream->GetDict(); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 186 | else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 187 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 188 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 189 | CFX_Matrix m = pDict->GetMatrixFor("Matrix"); |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 190 | CFX_Matrix t = *(CFX_Matrix*)matrix; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 191 | m.Concat(t); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 192 | pDict->SetMatrixFor("Matrix", m); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 196 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 197 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 200 | DLLEXPORT void STDCALL |
| 201 | FPDFPageObj_TransformClipPath(FPDF_PAGEOBJECT page_object, |
| 202 | double a, |
| 203 | double b, |
| 204 | double c, |
| 205 | double d, |
| 206 | double e, |
| 207 | double f) { |
| 208 | CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object; |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 209 | if (!pPageObj) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | return; |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 211 | CFX_Matrix matrix((float)a, (float)b, (float)c, (float)d, (float)e, (float)f); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 212 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | // Special treatment to shading object, because the ClipPath for shading |
| 214 | // object is already transformed. |
Wei Li | 7cf13c9 | 2016-02-19 11:53:03 -0800 | [diff] [blame] | 215 | if (!pPageObj->IsShading()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | pPageObj->TransformClipPath(matrix); |
| 217 | pPageObj->TransformGeneralState(matrix); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | DLLEXPORT FPDF_CLIPPATH STDCALL FPDF_CreateClipPath(float left, |
| 221 | float bottom, |
| 222 | float right, |
| 223 | float top) { |
tsepez | fc1d16f | 2016-09-02 15:45:22 -0700 | [diff] [blame] | 224 | CPDF_Path Path; |
| 225 | Path.AppendRect(left, bottom, right, top); |
| 226 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 227 | auto pNewClipPath = pdfium::MakeUnique<CPDF_ClipPath>(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 228 | pNewClipPath->AppendPath(Path, FXFILL_ALTERNATE, false); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 229 | return pNewClipPath.release(); // Caller takes ownership. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 232 | DLLEXPORT void STDCALL FPDF_DestroyClipPath(FPDF_CLIPPATH clipPath) { |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 233 | // Take ownership back from caller and destroy. |
| 234 | std::unique_ptr<CPDF_ClipPath>(static_cast<CPDF_ClipPath*>(clipPath)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Henrique Nakashima | 8e052a6 | 2017-06-27 17:14:31 -0400 | [diff] [blame] | 237 | void OutputPath(std::ostringstream& buf, CPDF_Path path) { |
tsepez | 7d2a8d9 | 2016-06-08 11:51:23 -0700 | [diff] [blame] | 238 | const CFX_PathData* pPathData = path.GetObject(); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 239 | if (!pPathData) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | return; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 241 | |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 242 | const std::vector<FX_PATHPOINT>& pPoints = pPathData->GetPoints(); |
tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 243 | if (path.IsRect()) { |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 244 | CFX_PointF diff = pPoints[2].m_Point - pPoints[0].m_Point; |
| 245 | buf << pPoints[0].m_Point.x << " " << pPoints[0].m_Point.y << " " << diff.x |
| 246 | << " " << diff.y << " re\n"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 247 | return; |
| 248 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 249 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 250 | CFX_ByteString temp; |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 251 | for (size_t i = 0; i < pPoints.size(); i++) { |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 252 | buf << pPoints[i].m_Point.x << " " << pPoints[i].m_Point.y; |
Nicolas Pena | 79365f7 | 2017-02-07 14:21:36 -0500 | [diff] [blame] | 253 | FXPT_TYPE point_type = pPoints[i].m_Type; |
| 254 | if (point_type == FXPT_TYPE::MoveTo) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | buf << " m\n"; |
Nicolas Pena | 79365f7 | 2017-02-07 14:21:36 -0500 | [diff] [blame] | 256 | } else if (point_type == FXPT_TYPE::BezierTo) { |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 257 | buf << " " << pPoints[i + 1].m_Point.x << " " << pPoints[i + 1].m_Point.y |
| 258 | << " " << pPoints[i + 2].m_Point.x << " " << pPoints[i + 2].m_Point.y; |
| 259 | buf << " c"; |
Nicolas Pena | 79365f7 | 2017-02-07 14:21:36 -0500 | [diff] [blame] | 260 | if (pPoints[i + 2].m_CloseFigure) |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 261 | buf << " h"; |
| 262 | buf << "\n"; |
| 263 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 264 | i += 2; |
Nicolas Pena | 79365f7 | 2017-02-07 14:21:36 -0500 | [diff] [blame] | 265 | } else if (point_type == FXPT_TYPE::LineTo) { |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 266 | buf << " l"; |
Nicolas Pena | 79365f7 | 2017-02-07 14:21:36 -0500 | [diff] [blame] | 267 | if (pPoints[i].m_CloseFigure) |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 268 | buf << " h"; |
| 269 | buf << "\n"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 274 | DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, |
| 275 | FPDF_CLIPPATH clipPath) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 276 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 277 | if (!pPage) |
| 278 | return; |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 279 | |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 280 | CPDF_Dictionary* pPageDic = pPage->m_pFormDict.Get(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 281 | CPDF_Object* pContentObj = |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 282 | pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | if (!pContentObj) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 284 | pContentObj = pPageDic ? pPageDic->GetArrayFor("Contents") : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 285 | if (!pContentObj) |
| 286 | return; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 287 | |
Henrique Nakashima | 8e052a6 | 2017-06-27 17:14:31 -0400 | [diff] [blame] | 288 | std::ostringstream strClip; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 289 | CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath; |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 290 | uint32_t i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 291 | for (i = 0; i < pClipPath->GetPathCount(); i++) { |
| 292 | CPDF_Path path = pClipPath->GetPath(i); |
| 293 | int iClipType = pClipPath->GetClipType(i); |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 294 | if (path.GetPoints().empty()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 295 | // Empty clipping (totally clipped out) |
| 296 | strClip << "0 0 m W n "; |
| 297 | } else { |
| 298 | OutputPath(strClip, path); |
| 299 | if (iClipType == FXFILL_WINDING) |
| 300 | strClip << "W n\n"; |
| 301 | else |
| 302 | strClip << "W* n\n"; |
| 303 | } |
| 304 | } |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 305 | CPDF_Document* pDoc = pPage->m_pDocument.Get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | if (!pDoc) |
| 307 | return; |
tsepez | bb577af | 2016-09-21 19:10:19 -0700 | [diff] [blame] | 308 | |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 309 | CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>( |
| 310 | nullptr, 0, |
| 311 | pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool())); |
Henrique Nakashima | 8e052a6 | 2017-06-27 17:14:31 -0400 | [diff] [blame] | 312 | pStream->SetData(&strClip); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 313 | |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 314 | CPDF_Array* pArray = ToArray(pContentObj); |
| 315 | if (pArray) { |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 316 | pArray->InsertNewAt<CPDF_Reference>(0, pDoc, pStream->GetObjNum()); |
| 317 | return; |
| 318 | } |
| 319 | CPDF_Reference* pReference = ToReference(pContentObj); |
| 320 | if (!pReference) |
| 321 | return; |
| 322 | |
| 323 | CPDF_Object* pDirectObj = pReference->GetDirect(); |
| 324 | if (!pDirectObj) |
| 325 | return; |
| 326 | |
| 327 | CPDF_Array* pObjArray = pDirectObj->AsArray(); |
| 328 | if (pObjArray) { |
| 329 | pObjArray->InsertNewAt<CPDF_Reference>(0, pDoc, pStream->GetObjNum()); |
| 330 | return; |
| 331 | } |
| 332 | if (pDirectObj->IsStream()) { |
| 333 | CPDF_Array* pContentArray = pDoc->NewIndirect<CPDF_Array>(); |
| 334 | pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum()); |
| 335 | pContentArray->AddNew<CPDF_Reference>(pDoc, pDirectObj->GetObjNum()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 336 | pPageDic->SetNewFor<CPDF_Reference>("Contents", pDoc, |
| 337 | pContentArray->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 339 | } |