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