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