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 | 584b1e6 | 2016-03-21 09:15:45 -0400 | [diff] [blame] | 9 | #include "core/fpdfapi/fpdf_page/include/cpdf_clippath.h" |
Dan Sinclair | 455a419 | 2016-03-16 09:48:56 -0400 | [diff] [blame] | 10 | #include "core/fpdfapi/fpdf_page/include/cpdf_page.h" |
Dan Sinclair | 584b1e6 | 2016-03-21 09:15:45 -0400 | [diff] [blame] | 11 | #include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h" |
| 12 | #include "core/fpdfapi/fpdf_page/include/cpdf_path.h" |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame] | 13 | #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 14 | #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 15 | #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" |
| 16 | #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" |
Dan Sinclair | 584b1e6 | 2016-03-21 09:15:45 -0400 | [diff] [blame] | 17 | #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 18 | #include "fpdfsdk/include/fsdk_define.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 19 | |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 20 | namespace { |
| 21 | |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 22 | void SetBoundingBox(CPDF_Page* page, |
| 23 | const CFX_ByteStringC& key, |
| 24 | float left, |
| 25 | float bottom, |
| 26 | float right, |
| 27 | float top) { |
| 28 | CPDF_Dictionary* pPageDict = page->m_pFormDict; |
| 29 | CPDF_Array* pBoundingBoxArray = new CPDF_Array; |
| 30 | pBoundingBoxArray->Add(new CPDF_Number(left)); |
| 31 | pBoundingBoxArray->Add(new CPDF_Number(bottom)); |
| 32 | pBoundingBoxArray->Add(new CPDF_Number(right)); |
| 33 | pBoundingBoxArray->Add(new CPDF_Number(top)); |
| 34 | pPageDict->SetAt(key, pBoundingBoxArray); |
| 35 | } |
| 36 | |
| 37 | FPDF_BOOL GetBoundingBox(CPDF_Page* page, |
| 38 | const CFX_ByteStringC& key, |
| 39 | float* left, |
| 40 | float* bottom, |
| 41 | float* right, |
| 42 | float* top) { |
| 43 | CPDF_Dictionary* pPageDict = page->m_pFormDict; |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 44 | CPDF_Array* pArray = pPageDict->GetArrayBy(key); |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 45 | if (!pArray) |
| 46 | return FALSE; |
| 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); |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 52 | return TRUE; |
| 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) |
| 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 = |
| 123 | pPageDic ? pPageDic->GetElement("Contents") : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 124 | if (!pContentObj) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 125 | pContentObj = pPageDic ? pPageDic->GetArrayBy("Contents") : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | if (!pContentObj) |
| 127 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 128 | |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 129 | CPDF_Dictionary* pDic = new CPDF_Dictionary; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 130 | CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize(), FALSE, FALSE); |
| 132 | CPDF_Document* pDoc = pPage->m_pDocument; |
| 133 | if (!pDoc) |
| 134 | return FALSE; |
| 135 | pDoc->AddIndirectObject(pStream); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 136 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | pDic = new CPDF_Dictionary; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 138 | CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 139 | pEndStream->SetData((const uint8_t*)" Q", 2, FALSE, FALSE); |
| 140 | pDoc->AddIndirectObject(pEndStream); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 141 | |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 142 | CPDF_Array* pContentArray = nullptr; |
| 143 | if (CPDF_Array* pArray = ToArray(pContentObj)) { |
| 144 | pContentArray = pArray; |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 145 | CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | pContentArray->InsertAt(0, pRef); |
| 147 | pContentArray->AddReference(pDoc, pEndStream); |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 148 | } else if (CPDF_Reference* pReference = ToReference(pContentObj)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 149 | CPDF_Object* pDirectObj = pReference->GetDirect(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 150 | if (pDirectObj) { |
| 151 | if (CPDF_Array* pArray = pDirectObj->AsArray()) { |
| 152 | pContentArray = pArray; |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 153 | CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | pContentArray->InsertAt(0, pRef); |
| 155 | pContentArray->AddReference(pDoc, pEndStream); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 156 | } else if (pDirectObj->IsStream()) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 157 | pContentArray = new CPDF_Array(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 158 | pContentArray->AddReference(pDoc, pStream->GetObjNum()); |
| 159 | pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); |
| 160 | pContentArray->AddReference(pDoc, pEndStream); |
| 161 | pPageDic->SetAtReference("Contents", pDoc, |
| 162 | pDoc->AddIndirectObject(pContentArray)); |
| 163 | } |
| 164 | } |
| 165 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 166 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | // Need to transform the patterns as well. |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 168 | CPDF_Dictionary* pRes = pPageDic->GetDictBy("Resources"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | if (pRes) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 170 | CPDF_Dictionary* pPattenDict = pRes->GetDictBy("Pattern"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 171 | if (pPattenDict) { |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 172 | for (const auto& it : *pPattenDict) { |
| 173 | CPDF_Object* pObj = it.second; |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 174 | if (pObj->IsReference()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | pObj = pObj->GetDirect(); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 176 | |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 177 | CPDF_Dictionary* pDict = nullptr; |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 178 | if (pObj->IsDictionary()) |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 179 | pDict = pObj->AsDictionary(); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 180 | else if (CPDF_Stream* pStream = pObj->AsStream()) |
| 181 | pDict = pStream->GetDict(); |
| 182 | else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 184 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 185 | CFX_Matrix m = pDict->GetMatrixBy("Matrix"); |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 186 | CFX_Matrix t = *(CFX_Matrix*)matrix; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 187 | m.Concat(t); |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 188 | pDict->SetAtMatrix("Matrix", m); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 192 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | DLLEXPORT void STDCALL |
| 197 | FPDFPageObj_TransformClipPath(FPDF_PAGEOBJECT page_object, |
| 198 | double a, |
| 199 | double b, |
| 200 | double c, |
| 201 | double d, |
| 202 | double e, |
| 203 | double f) { |
| 204 | CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object; |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 205 | if (!pPageObj) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | return; |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 207 | CFX_Matrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d, |
| 208 | (FX_FLOAT)e, (FX_FLOAT)f); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 209 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | // Special treatment to shading object, because the ClipPath for shading |
| 211 | // object is already transformed. |
Wei Li | 7cf13c9 | 2016-02-19 11:53:03 -0800 | [diff] [blame] | 212 | if (!pPageObj->IsShading()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | pPageObj->TransformClipPath(matrix); |
| 214 | pPageObj->TransformGeneralState(matrix); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | DLLEXPORT FPDF_CLIPPATH STDCALL FPDF_CreateClipPath(float left, |
| 218 | float bottom, |
| 219 | float right, |
| 220 | float top) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 221 | CPDF_ClipPath* pNewClipPath = new CPDF_ClipPath(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 222 | pNewClipPath->GetModify(); |
| 223 | CPDF_Path Path; |
| 224 | Path.GetModify(); |
| 225 | Path.AppendRect(left, bottom, right, top); |
| 226 | pNewClipPath->AppendPath(Path, FXFILL_ALTERNATE, FALSE); |
| 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) { |
| 235 | const CFX_PathData* pPathData = path; |
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 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 239 | FX_PATHPOINT* pPoints = pPathData->GetPoints(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 240 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 241 | if (path.IsRect()) { |
| 242 | buf << (pPoints[0].m_PointX) << " " << (pPoints[0].m_PointY) << " " |
| 243 | << (pPoints[2].m_PointX - pPoints[0].m_PointX) << " " |
| 244 | << (pPoints[2].m_PointY - pPoints[0].m_PointY) << " re\n"; |
| 245 | return; |
| 246 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 247 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 248 | CFX_ByteString temp; |
| 249 | for (int i = 0; i < pPathData->GetPointCount(); i++) { |
| 250 | buf << (pPoints[i].m_PointX) << " " << (pPoints[i].m_PointY); |
| 251 | int point_type = pPoints[i].m_Flag & FXPT_TYPE; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 252 | if (point_type == FXPT_MOVETO) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 253 | buf << " m\n"; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 254 | } else if (point_type == FXPT_BEZIERTO) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | buf << " " << (pPoints[i + 1].m_PointX) << " " |
| 256 | << (pPoints[i + 1].m_PointY) << " " << (pPoints[i + 2].m_PointX) |
| 257 | << " " << (pPoints[i + 2].m_PointY); |
| 258 | if (pPoints[i + 2].m_Flag & FXPT_CLOSEFIGURE) |
| 259 | buf << " c h\n"; |
| 260 | else |
| 261 | buf << " c\n"; |
| 262 | i += 2; |
| 263 | } else if (point_type == FXPT_LINETO) { |
| 264 | if (pPoints[i].m_Flag & FXPT_CLOSEFIGURE) |
| 265 | buf << " l h\n"; |
| 266 | else |
| 267 | buf << " l\n"; |
| 268 | } |
| 269 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 272 | DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, |
| 273 | FPDF_CLIPPATH clipPath) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 274 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 275 | if (!pPage) |
| 276 | return; |
Lei Zhang | 6ac0d2f | 2015-10-02 10:08:48 -0700 | [diff] [blame] | 277 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 278 | CPDF_Dictionary* pPageDic = pPage->m_pFormDict; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 279 | CPDF_Object* pContentObj = |
| 280 | pPageDic ? pPageDic->GetElement("Contents") : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | if (!pContentObj) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 282 | pContentObj = pPageDic ? pPageDic->GetArrayBy("Contents") : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | if (!pContentObj) |
| 284 | return; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 285 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 286 | CFX_ByteTextBuf strClip; |
| 287 | CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath; |
| 288 | FX_DWORD i; |
| 289 | for (i = 0; i < pClipPath->GetPathCount(); i++) { |
| 290 | CPDF_Path path = pClipPath->GetPath(i); |
| 291 | int iClipType = pClipPath->GetClipType(i); |
| 292 | if (path.GetPointCount() == 0) { |
| 293 | // Empty clipping (totally clipped out) |
| 294 | strClip << "0 0 m W n "; |
| 295 | } else { |
| 296 | OutputPath(strClip, path); |
| 297 | if (iClipType == FXFILL_WINDING) |
| 298 | strClip << "W n\n"; |
| 299 | else |
| 300 | strClip << "W* n\n"; |
| 301 | } |
| 302 | } |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 303 | CPDF_Dictionary* pDic = new CPDF_Dictionary; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 304 | CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 305 | pStream->SetData(strClip.GetBuffer(), strClip.GetSize(), FALSE, FALSE); |
| 306 | CPDF_Document* pDoc = pPage->m_pDocument; |
| 307 | if (!pDoc) |
| 308 | return; |
| 309 | pDoc->AddIndirectObject(pStream); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 310 | |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 311 | CPDF_Array* pContentArray = nullptr; |
| 312 | if (CPDF_Array* pArray = ToArray(pContentObj)) { |
| 313 | pContentArray = pArray; |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 314 | CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | pContentArray->InsertAt(0, pRef); |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 316 | } else if (CPDF_Reference* pReference = ToReference(pContentObj)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | CPDF_Object* pDirectObj = pReference->GetDirect(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 318 | if (pDirectObj) { |
| 319 | if (CPDF_Array* pArray = pDirectObj->AsArray()) { |
| 320 | pContentArray = pArray; |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 321 | CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | pContentArray->InsertAt(0, pRef); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 323 | } else if (pDirectObj->IsStream()) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 324 | pContentArray = new CPDF_Array(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 325 | pContentArray->AddReference(pDoc, pStream->GetObjNum()); |
| 326 | pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); |
| 327 | pPageDic->SetAtReference("Contents", pDoc, |
| 328 | pDoc->AddIndirectObject(pContentArray)); |
| 329 | } |
| 330 | } |
| 331 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 332 | } |