blob: 7e19505c2676125a8dc32dd4f61caa88a5ac9a6e [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 Sinclaire4602322017-02-15 11:07:32 -05009#include <vector>
10
dsinclair41872fa2016-10-04 11:29:35 -070011#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"
dsinclair488b7ad2016-10-04 11:55:50 -070015#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"
dsinclair74a34fc2016-09-29 16:41:42 -070020#include "core/fxge/cfx_pathdata.h"
dsinclair114e46a2016-09-29 17:18:21 -070021#include "fpdfsdk/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070023namespace {
24
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070025void SetBoundingBox(CPDF_Page* page,
tsepez71a452f2016-05-13 17:51:27 -070026 const CFX_ByteString& key,
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070027 float left,
28 float bottom,
29 float right,
30 float top) {
tsepez0e606b52016-11-18 16:22:41 -080031 CPDF_Array* pBoundingBoxArray = page->m_pFormDict->SetNewFor<CPDF_Array>(key);
tsepez8a3aa452016-11-16 12:26:06 -080032 pBoundingBoxArray->AddNew<CPDF_Number>(left);
33 pBoundingBoxArray->AddNew<CPDF_Number>(bottom);
34 pBoundingBoxArray->AddNew<CPDF_Number>(right);
35 pBoundingBoxArray->AddNew<CPDF_Number>(top);
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070036}
37
tsepez71a452f2016-05-13 17:51:27 -070038bool GetBoundingBox(CPDF_Page* page,
39 const CFX_ByteString& key,
40 float* left,
41 float* bottom,
42 float* right,
43 float* top) {
dsinclair38fd8442016-09-15 10:15:32 -070044 CPDF_Array* pArray = page->m_pFormDict->GetArrayFor(key);
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070045 if (!pArray)
tsepez71a452f2016-05-13 17:51:27 -070046 return false;
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070047
Wei Li9b761132016-01-29 15:44:20 -080048 *left = pArray->GetFloatAt(0);
49 *bottom = pArray->GetFloatAt(1);
50 *right = pArray->GetFloatAt(2);
51 *top = pArray->GetFloatAt(3);
tsepez71a452f2016-05-13 17:51:27 -070052 return true;
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070053}
54
55} // namespace
56
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057DLLEXPORT void STDCALL FPDFPage_SetMediaBox(FPDF_PAGE page,
58 float left,
59 float bottom,
60 float right,
61 float top) {
Tom Sepezdb0be962015-10-16 14:00:21 -070062 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 if (!pPage)
64 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070065
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070066 SetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067}
68
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069DLLEXPORT void STDCALL FPDFPage_SetCropBox(FPDF_PAGE page,
70 float left,
71 float bottom,
72 float right,
73 float top) {
Tom Sepezdb0be962015-10-16 14:00:21 -070074 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 if (!pPage)
76 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070078 SetBoundingBox(pPage, "CropBox", left, bottom, right, top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetMediaBox(FPDF_PAGE page,
82 float* left,
83 float* bottom,
84 float* right,
85 float* top) {
Tom Sepezdb0be962015-10-16 14:00:21 -070086 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070087 return pPage && GetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetCropBox(FPDF_PAGE page,
91 float* left,
92 float* bottom,
93 float* right,
94 float* top) {
Tom Sepezdb0be962015-10-16 14:00:21 -070095 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Lei Zhang6ac0d2f2015-10-02 10:08:48 -070096 return pPage && GetBoundingBox(pPage, "CropBox", left, bottom, right, top);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
100 FS_MATRIX* matrix,
101 FS_RECTF* clipRect) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700102 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -0700104 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 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-Malek3f3b45c2014-05-23 17:28:10 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 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 Zhanga6d9f0e2015-06-13 00:48:38 -0700120
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400122 CPDF_Object* pContentObj =
dsinclair38fd8442016-09-15 10:15:32 -0700123 pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 if (!pContentObj)
dsinclair38fd8442016-09-15 10:15:32 -0700125 pContentObj = pPageDic ? pPageDic->GetArrayFor("Contents") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 if (!pContentObj)
tsepez4cf55152016-11-02 14:37:54 -0700127 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 CPDF_Document* pDoc = pPage->m_pDocument;
130 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700131 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700132
tsepez9e05ee12016-11-21 13:19:10 -0800133 CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>(
134 nullptr, 0,
135 pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()));
tsepez698c5712016-09-28 16:47:07 -0700136 pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize());
tsepez70c4afd2016-11-15 11:33:44 -0800137
tsepez9e05ee12016-11-21 13:19:10 -0800138 CPDF_Stream* pEndStream = pDoc->NewIndirect<CPDF_Stream>(
139 nullptr, 0,
140 pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()));
tsepeze6db16e2016-09-19 10:45:09 -0700141 pEndStream->SetData((const uint8_t*)" Q", 2);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400143 CPDF_Array* pContentArray = nullptr;
weilidb444d22016-06-02 15:48:15 -0700144 CPDF_Array* pArray = ToArray(pContentObj);
145 if (pArray) {
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400146 pContentArray = pArray;
tsepez8a3aa452016-11-16 12:26:06 -0800147 pContentArray->InsertNewAt<CPDF_Reference>(0, pDoc, pStream->GetObjNum());
148 pContentArray->AddNew<CPDF_Reference>(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;
tsepez8a3aa452016-11-16 12:26:06 -0800155 pContentArray->InsertNewAt<CPDF_Reference>(0, pDoc,
156 pStream->GetObjNum());
157 pContentArray->AddNew<CPDF_Reference>(pDoc, pEndStream->GetObjNum());
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400158 } else if (pDirectObj->IsStream()) {
tsepez70c4afd2016-11-15 11:33:44 -0800159 pContentArray = pDoc->NewIndirect<CPDF_Array>();
tsepez8a3aa452016-11-16 12:26:06 -0800160 pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum());
161 pContentArray->AddNew<CPDF_Reference>(pDoc, pDirectObj->GetObjNum());
162 pContentArray->AddNew<CPDF_Reference>(pDoc, pEndStream->GetObjNum());
tsepez0e606b52016-11-18 16:22:41 -0800163 pPageDic->SetNewFor<CPDF_Reference>("Contents", pDoc,
164 pContentArray->GetObjNum());
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) {
tsepez0e606b52016-11-18 16:22:41 -0800175 CPDF_Object* pObj = it.second.get();
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
tsepez4cf55152016-11-02 14:37:54 -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();
tsepez4cf55152016-11-02 14:37:54 -0700227 pNewClipPath->AppendPath(Path, FXFILL_ALTERNATE, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 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
Dan Sinclaire4602322017-02-15 11:07:32 -0500240 const std::vector<FX_PATHPOINT>& pPoints = pPathData->GetPoints();
tsepez59601432016-08-29 14:26:57 -0700241 if (path.IsRect()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 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 Zhanga6d9f0e2015-06-13 00:48:38 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 CFX_ByteString temp;
Dan Sinclaire4602322017-02-15 11:07:32 -0500249 for (size_t i = 0; i < pPoints.size(); i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 buf << (pPoints[i].m_PointX) << " " << (pPoints[i].m_PointY);
Nicolas Pena79365f72017-02-07 14:21:36 -0500251 FXPT_TYPE point_type = pPoints[i].m_Type;
252 if (point_type == FXPT_TYPE::MoveTo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 buf << " m\n";
Nicolas Pena79365f72017-02-07 14:21:36 -0500254 } else if (point_type == FXPT_TYPE::BezierTo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 buf << " " << (pPoints[i + 1].m_PointX) << " "
256 << (pPoints[i + 1].m_PointY) << " " << (pPoints[i + 2].m_PointX)
257 << " " << (pPoints[i + 2].m_PointY);
Nicolas Pena79365f72017-02-07 14:21:36 -0500258 if (pPoints[i + 2].m_CloseFigure)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 buf << " c h\n";
260 else
261 buf << " c\n";
262 i += 2;
Nicolas Pena79365f72017-02-07 14:21:36 -0500263 } else if (point_type == FXPT_TYPE::LineTo) {
264 if (pPoints[i].m_CloseFigure)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 buf << " l h\n";
266 else
267 buf << " l\n";
268 }
269 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270}
271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,
273 FPDF_CLIPPATH clipPath) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700274 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 if (!pPage)
276 return;
Lei Zhang6ac0d2f2015-10-02 10:08:48 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400279 CPDF_Object* pContentObj =
dsinclair38fd8442016-09-15 10:15:32 -0700280 pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 if (!pContentObj)
dsinclair38fd8442016-09-15 10:15:32 -0700282 pContentObj = pPageDic ? pPageDic->GetArrayFor("Contents") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 if (!pContentObj)
284 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 CFX_ByteTextBuf strClip;
287 CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath;
tsepezc3255f52016-03-25 14:52:27 -0700288 uint32_t i;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 for (i = 0; i < pClipPath->GetPathCount(); i++) {
290 CPDF_Path path = pClipPath->GetPath(i);
291 int iClipType = pClipPath->GetClipType(i);
Dan Sinclaire4602322017-02-15 11:07:32 -0500292 if (path.GetPoints().empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 // 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 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 CPDF_Document* pDoc = pPage->m_pDocument;
304 if (!pDoc)
305 return;
tsepezbb577af2016-09-21 19:10:19 -0700306
tsepez9e05ee12016-11-21 13:19:10 -0800307 CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>(
308 nullptr, 0,
309 pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()));
tsepez698c5712016-09-28 16:47:07 -0700310 pStream->SetData(strClip.GetBuffer(), strClip.GetSize());
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700311
weilidb444d22016-06-02 15:48:15 -0700312 CPDF_Array* pArray = ToArray(pContentObj);
313 if (pArray) {
tsepez8a3aa452016-11-16 12:26:06 -0800314 pArray->InsertNewAt<CPDF_Reference>(0, pDoc, pStream->GetObjNum());
315 return;
316 }
317 CPDF_Reference* pReference = ToReference(pContentObj);
318 if (!pReference)
319 return;
320
321 CPDF_Object* pDirectObj = pReference->GetDirect();
322 if (!pDirectObj)
323 return;
324
325 CPDF_Array* pObjArray = pDirectObj->AsArray();
326 if (pObjArray) {
327 pObjArray->InsertNewAt<CPDF_Reference>(0, pDoc, pStream->GetObjNum());
328 return;
329 }
330 if (pDirectObj->IsStream()) {
331 CPDF_Array* pContentArray = pDoc->NewIndirect<CPDF_Array>();
332 pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum());
333 pContentArray->AddNew<CPDF_Reference>(pDoc, pDirectObj->GetObjNum());
tsepez0e606b52016-11-18 16:22:41 -0800334 pPageDic->SetNewFor<CPDF_Reference>("Contents", pDoc,
335 pContentArray->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337}