blob: 8841c96f23b717670e3fdf41d10d2d4b3b4d7527 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_transformpage.h"
8
Dan Sinclair584b1e62016-03-21 09:15:45 -04009#include "core/fpdfapi/fpdf_page/include/cpdf_clippath.h"
Dan Sinclair455a4192016-03-16 09:48:56 -040010#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclair584b1e62016-03-21 09:15:45 -040011#include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h"
12#include "core/fpdfapi/fpdf_page/include/cpdf_path.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040013#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 Sinclair584b1e62016-03-21 09:15:45 -040017#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
npm660de3c2016-08-08 08:18:29 -070018#include "core/fxge/include/cfx_pathdata.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080019#include "fpdfsdk/include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070021namespace {
22
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070023void SetBoundingBox(CPDF_Page* page,
tsepez71a452f2016-05-13 17:51:27 -070024 const CFX_ByteString& key,
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070025 float left,
26 float bottom,
27 float right,
28 float top) {
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070029 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));
dsinclair38fd8442016-09-15 10:15:32 -070034 page->m_pFormDict->SetFor(key, pBoundingBoxArray);
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070035}
36
tsepez71a452f2016-05-13 17:51:27 -070037bool GetBoundingBox(CPDF_Page* page,
38 const CFX_ByteString& key,
39 float* left,
40 float* bottom,
41 float* right,
42 float* top) {
dsinclair38fd8442016-09-15 10:15:32 -070043 CPDF_Array* pArray = page->m_pFormDict->GetArrayFor(key);
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070044 if (!pArray)
tsepez71a452f2016-05-13 17:51:27 -070045 return false;
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070046
Wei Li9b761132016-01-29 15:44:20 -080047 *left = pArray->GetFloatAt(0);
48 *bottom = pArray->GetFloatAt(1);
49 *right = pArray->GetFloatAt(2);
50 *top = pArray->GetFloatAt(3);
tsepez71a452f2016-05-13 17:51:27 -070051 return true;
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070052}
53
54} // namespace
55
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056DLLEXPORT void STDCALL FPDFPage_SetMediaBox(FPDF_PAGE page,
57 float left,
58 float bottom,
59 float right,
60 float top) {
Tom Sepezdb0be962015-10-16 14:00:21 -070061 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 if (!pPage)
63 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070064
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070065 SetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
67
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068DLLEXPORT void STDCALL FPDFPage_SetCropBox(FPDF_PAGE page,
69 float left,
70 float bottom,
71 float right,
72 float top) {
Tom Sepezdb0be962015-10-16 14:00:21 -070073 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 if (!pPage)
75 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070077 SetBoundingBox(pPage, "CropBox", left, bottom, right, top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetMediaBox(FPDF_PAGE page,
81 float* left,
82 float* bottom,
83 float* right,
84 float* top) {
Tom Sepezdb0be962015-10-16 14:00:21 -070085 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070086 return pPage && GetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087}
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetCropBox(FPDF_PAGE page,
90 float* left,
91 float* bottom,
92 float* right,
93 float* top) {
Tom Sepezdb0be962015-10-16 14:00:21 -070094 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070095 return pPage && GetBoundingBox(pPage, "CropBox", left, bottom, right, top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
99 FS_MATRIX* matrix,
100 FS_RECTF* clipRect) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700101 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (!pPage)
103 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 CFX_ByteTextBuf textBuf;
106 textBuf << "q ";
107 CFX_FloatRect rect(clipRect->left, clipRect->bottom, clipRect->right,
108 clipRect->top);
109 rect.Normalize();
110 CFX_ByteString bsClipping;
111 bsClipping.Format("%f %f %f %f re W* n ", rect.left, rect.bottom,
112 rect.Width(), rect.Height());
113 textBuf << bsClipping;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 CFX_ByteString bsMatix;
116 bsMatix.Format("%f %f %f %f %f %f cm ", matrix->a, matrix->b, matrix->c,
117 matrix->d, matrix->e, matrix->f);
118 textBuf << bsMatix;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700119
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400121 CPDF_Object* pContentObj =
dsinclair38fd8442016-09-15 10:15:32 -0700122 pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 if (!pContentObj)
dsinclair38fd8442016-09-15 10:15:32 -0700124 pContentObj = pPageDic ? pPageDic->GetArrayFor("Contents") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 if (!pContentObj)
126 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 CPDF_Document* pDoc = pPage->m_pDocument;
129 if (!pDoc)
130 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700131
tsepez698c5712016-09-28 16:47:07 -0700132 CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool());
133 CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic);
134 pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize());
135 pDoc->AddIndirectObject(pStream);
136 pDic = new CPDF_Dictionary(pDoc->GetByteStringPool());
137
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400138 CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic);
tsepeze6db16e2016-09-19 10:45:09 -0700139 pEndStream->SetData((const uint8_t*)" Q", 2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 pDoc->AddIndirectObject(pEndStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400142 CPDF_Array* pContentArray = nullptr;
weilidb444d22016-06-02 15:48:15 -0700143 CPDF_Array* pArray = ToArray(pContentObj);
144 if (pArray) {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400145 pContentArray = pArray;
Tom Sepezae51c812015-08-05 12:34:06 -0700146 CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 pContentArray->InsertAt(0, pRef);
tsepezbb577af2016-09-21 19:10:19 -0700148 pContentArray->AddReference(pDoc, pEndStream->GetObjNum());
Dan Sinclairbf81c142015-10-26 16:54:39 -0400149 } else if (CPDF_Reference* pReference = ToReference(pContentObj)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 CPDF_Object* pDirectObj = pReference->GetDirect();
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400151 if (pDirectObj) {
weilidb444d22016-06-02 15:48:15 -0700152 CPDF_Array* pObjArray = pDirectObj->AsArray();
153 if (pObjArray) {
154 pContentArray = pObjArray;
Nico Weber077f1a32015-08-06 15:08:57 -0700155 CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 pContentArray->InsertAt(0, pRef);
tsepezbb577af2016-09-21 19:10:19 -0700157 pContentArray->AddReference(pDoc, pEndStream->GetObjNum());
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400158 } else if (pDirectObj->IsStream()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700159 pContentArray = new CPDF_Array();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 pContentArray->AddReference(pDoc, pStream->GetObjNum());
161 pContentArray->AddReference(pDoc, pDirectObj->GetObjNum());
tsepezbb577af2016-09-21 19:10:19 -0700162 pContentArray->AddReference(pDoc, pEndStream->GetObjNum());
dsinclair38fd8442016-09-15 10:15:32 -0700163 pPageDic->SetReferenceFor("Contents", pDoc,
164 pDoc->AddIndirectObject(pContentArray));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 }
166 }
167 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 // Need to transform the patterns as well.
dsinclair38fd8442016-09-15 10:15:32 -0700170 CPDF_Dictionary* pRes = pPageDic->GetDictFor("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 if (pRes) {
dsinclair38fd8442016-09-15 10:15:32 -0700172 CPDF_Dictionary* pPattenDict = pRes->GetDictFor("Pattern");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 if (pPattenDict) {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800174 for (const auto& it : *pPattenDict) {
175 CPDF_Object* pObj = it.second;
Dan Sinclairbf81c142015-10-26 16:54:39 -0400176 if (pObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 pObj = pObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400178
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800179 CPDF_Dictionary* pDict = nullptr;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400180 if (pObj->IsDictionary())
Dan Sinclairf1251c12015-10-20 16:24:45 -0400181 pDict = pObj->AsDictionary();
weilidb444d22016-06-02 15:48:15 -0700182 else if (CPDF_Stream* pObjStream = pObj->AsStream())
183 pDict = pObjStream->GetDict();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400184 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186
dsinclair38fd8442016-09-15 10:15:32 -0700187 CFX_Matrix m = pDict->GetMatrixFor("Matrix");
Tom Sepez60d909e2015-12-10 15:34:55 -0800188 CFX_Matrix t = *(CFX_Matrix*)matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 m.Concat(t);
dsinclair38fd8442016-09-15 10:15:32 -0700190 pDict->SetMatrixFor("Matrix", m);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 }
192 }
193 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700196}
197
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198DLLEXPORT void STDCALL
199FPDFPageObj_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 Zhang412e9082015-12-14 18:34:00 -0800207 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 return;
Tom Sepez60d909e2015-12-10 15:34:55 -0800209 CFX_Matrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
210 (FX_FLOAT)e, (FX_FLOAT)f);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 // Special treatment to shading object, because the ClipPath for shading
213 // object is already transformed.
Wei Li7cf13c92016-02-19 11:53:03 -0800214 if (!pPageObj->IsShading())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 pPageObj->TransformClipPath(matrix);
216 pPageObj->TransformGeneralState(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217}
218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219DLLEXPORT FPDF_CLIPPATH STDCALL FPDF_CreateClipPath(float left,
220 float bottom,
221 float right,
222 float top) {
tsepezfc1d16f2016-09-02 15:45:22 -0700223 CPDF_Path Path;
224 Path.AppendRect(left, bottom, right, top);
225
Tom Sepezae51c812015-08-05 12:34:06 -0700226 CPDF_ClipPath* pNewClipPath = new CPDF_ClipPath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 pNewClipPath->AppendPath(Path, FXFILL_ALTERNATE, FALSE);
228 return pNewClipPath;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231DLLEXPORT void STDCALL FPDF_DestroyClipPath(FPDF_CLIPPATH clipPath) {
232 delete (CPDF_ClipPath*)clipPath;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233}
234
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235void OutputPath(CFX_ByteTextBuf& buf, CPDF_Path path) {
tsepez7d2a8d92016-06-08 11:51:23 -0700236 const CFX_PathData* pPathData = path.GetObject();
Lei Zhang412e9082015-12-14 18:34:00 -0800237 if (!pPathData)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 FX_PATHPOINT* pPoints = pPathData->GetPoints();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700241
tsepez59601432016-08-29 14:26:57 -0700242 if (path.IsRect()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 buf << (pPoints[0].m_PointX) << " " << (pPoints[0].m_PointY) << " "
244 << (pPoints[2].m_PointX - pPoints[0].m_PointX) << " "
245 << (pPoints[2].m_PointY - pPoints[0].m_PointY) << " re\n";
246 return;
247 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 CFX_ByteString temp;
250 for (int i = 0; i < pPathData->GetPointCount(); i++) {
251 buf << (pPoints[i].m_PointX) << " " << (pPoints[i].m_PointY);
252 int point_type = pPoints[i].m_Flag & FXPT_TYPE;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500253 if (point_type == FXPT_MOVETO) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 buf << " m\n";
Dan Sinclair738b08c2016-03-01 14:45:20 -0500255 } else if (point_type == FXPT_BEZIERTO) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 buf << " " << (pPoints[i + 1].m_PointX) << " "
257 << (pPoints[i + 1].m_PointY) << " " << (pPoints[i + 2].m_PointX)
258 << " " << (pPoints[i + 2].m_PointY);
259 if (pPoints[i + 2].m_Flag & FXPT_CLOSEFIGURE)
260 buf << " c h\n";
261 else
262 buf << " c\n";
263 i += 2;
264 } else if (point_type == FXPT_LINETO) {
265 if (pPoints[i].m_Flag & FXPT_CLOSEFIGURE)
266 buf << " l h\n";
267 else
268 buf << " l\n";
269 }
270 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,
274 FPDF_CLIPPATH clipPath) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700275 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 if (!pPage)
277 return;
Lei Zhang6ac0d2f2015-10-02 10:08:48 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400280 CPDF_Object* pContentObj =
dsinclair38fd8442016-09-15 10:15:32 -0700281 pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 if (!pContentObj)
dsinclair38fd8442016-09-15 10:15:32 -0700283 pContentObj = pPageDic ? pPageDic->GetArrayFor("Contents") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 if (!pContentObj)
285 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 CFX_ByteTextBuf strClip;
288 CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath;
tsepezc3255f52016-03-25 14:52:27 -0700289 uint32_t i;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 for (i = 0; i < pClipPath->GetPathCount(); i++) {
291 CPDF_Path path = pClipPath->GetPath(i);
292 int iClipType = pClipPath->GetClipType(i);
tsepez59601432016-08-29 14:26:57 -0700293 if (path.GetPointCount() == 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 // 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 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 CPDF_Document* pDoc = pPage->m_pDocument;
305 if (!pDoc)
306 return;
tsepezbb577af2016-09-21 19:10:19 -0700307
tsepez698c5712016-09-28 16:47:07 -0700308 CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool());
309 CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic);
310 pStream->SetData(strClip.GetBuffer(), strClip.GetSize());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 pDoc->AddIndirectObject(pStream);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700312
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400313 CPDF_Array* pContentArray = nullptr;
weilidb444d22016-06-02 15:48:15 -0700314 CPDF_Array* pArray = ToArray(pContentObj);
315 if (pArray) {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400316 pContentArray = pArray;
Tom Sepezae51c812015-08-05 12:34:06 -0700317 CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 pContentArray->InsertAt(0, pRef);
Dan Sinclairbf81c142015-10-26 16:54:39 -0400319 } else if (CPDF_Reference* pReference = ToReference(pContentObj)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 CPDF_Object* pDirectObj = pReference->GetDirect();
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400321 if (pDirectObj) {
weilidb444d22016-06-02 15:48:15 -0700322 CPDF_Array* pObjArray = pDirectObj->AsArray();
323 if (pObjArray) {
324 pContentArray = pObjArray;
Nico Weber077f1a32015-08-06 15:08:57 -0700325 CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 pContentArray->InsertAt(0, pRef);
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400327 } else if (pDirectObj->IsStream()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700328 pContentArray = new CPDF_Array();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 pContentArray->AddReference(pDoc, pStream->GetObjNum());
330 pContentArray->AddReference(pDoc, pDirectObj->GetObjNum());
dsinclair38fd8442016-09-15 10:15:32 -0700331 pPageDic->SetReferenceFor("Contents", pDoc,
332 pDoc->AddIndirectObject(pContentArray));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 }
334 }
335 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336}