| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 | |
| 7 #include "core/src/fpdfapi/fpdf_page/pageint.h" | |
| 8 | |
| 9 #include <algorithm> | |
| 10 | |
| 11 #include "core/include/fpdfapi/cpdf_array.h" | |
| 12 #include "core/include/fpdfapi/cpdf_dictionary.h" | |
| 13 #include "core/include/fpdfapi/cpdf_document.h" | |
| 14 #include "core/include/fpdfapi/fpdf_page.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const int kSingleCoordinatePair = 1; | |
| 19 const int kTensorCoordinatePairs = 16; | |
| 20 const int kCoonsCoordinatePairs = 12; | |
| 21 | |
| 22 const int kSingleColorPerPatch = 1; | |
| 23 const int kQuadColorsPerPatch = 4; | |
| 24 | |
| 25 ShadingType ToShadingType(int type) { | |
| 26 return (type > static_cast<int>(kInvalidShading) && | |
| 27 type < static_cast<int>(kMaxShading)) | |
| 28 ? static_cast<ShadingType>(type) | |
| 29 : kInvalidShading; | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 CPDF_Pattern::CPDF_Pattern(PatternType type, | |
| 35 CPDF_Document* pDoc, | |
| 36 CPDF_Object* pObj, | |
| 37 const CFX_Matrix* pParentMatrix) | |
| 38 : m_PatternType(type), | |
| 39 m_pDocument(pDoc), | |
| 40 m_pPatternObj(pObj), | |
| 41 m_bForceClear(FALSE) { | |
| 42 if (pParentMatrix) | |
| 43 m_ParentMatrix = *pParentMatrix; | |
| 44 } | |
| 45 CPDF_Pattern::~CPDF_Pattern() {} | |
| 46 CPDF_TilingPattern::CPDF_TilingPattern(CPDF_Document* pDoc, | |
| 47 CPDF_Object* pPatternObj, | |
| 48 const CFX_Matrix* parentMatrix) | |
| 49 : CPDF_Pattern(TILING, pDoc, pPatternObj, parentMatrix) { | |
| 50 CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); | |
| 51 m_Pattern2Form = pDict->GetMatrixBy("Matrix"); | |
| 52 m_bColored = pDict->GetIntegerBy("PaintType") == 1; | |
| 53 if (parentMatrix) { | |
| 54 m_Pattern2Form.Concat(*parentMatrix); | |
| 55 } | |
| 56 m_pForm = NULL; | |
| 57 } | |
| 58 CPDF_TilingPattern::~CPDF_TilingPattern() { | |
| 59 delete m_pForm; | |
| 60 m_pForm = NULL; | |
| 61 } | |
| 62 FX_BOOL CPDF_TilingPattern::Load() { | |
| 63 if (m_pForm) | |
| 64 return TRUE; | |
| 65 | |
| 66 CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); | |
| 67 if (!pDict) | |
| 68 return FALSE; | |
| 69 | |
| 70 m_bColored = pDict->GetIntegerBy("PaintType") == 1; | |
| 71 m_XStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumberBy("XStep")); | |
| 72 m_YStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumberBy("YStep")); | |
| 73 | |
| 74 CPDF_Stream* pStream = m_pPatternObj->AsStream(); | |
| 75 if (!pStream) | |
| 76 return FALSE; | |
| 77 | |
| 78 m_pForm = new CPDF_Form(m_pDocument, NULL, pStream); | |
| 79 m_pForm->ParseContent(NULL, &m_ParentMatrix, NULL, NULL); | |
| 80 m_BBox = pDict->GetRectBy("BBox"); | |
| 81 return TRUE; | |
| 82 } | |
| 83 CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc, | |
| 84 CPDF_Object* pPatternObj, | |
| 85 FX_BOOL bShading, | |
| 86 const CFX_Matrix* parentMatrix) | |
| 87 : CPDF_Pattern(SHADING, | |
| 88 pDoc, | |
| 89 bShading ? nullptr : pPatternObj, | |
| 90 parentMatrix), | |
| 91 m_ShadingType(kInvalidShading), | |
| 92 m_bShadingObj(bShading), | |
| 93 m_pShadingObj(pPatternObj), | |
| 94 m_pCS(nullptr), | |
| 95 m_pCountedCS(nullptr), | |
| 96 m_nFuncs(0) { | |
| 97 if (!bShading) { | |
| 98 CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); | |
| 99 m_Pattern2Form = pDict->GetMatrixBy("Matrix"); | |
| 100 m_pShadingObj = pDict->GetElementValue("Shading"); | |
| 101 if (parentMatrix) | |
| 102 m_Pattern2Form.Concat(*parentMatrix); | |
| 103 } | |
| 104 for (int i = 0; i < FX_ArraySize(m_pFunctions); ++i) | |
| 105 m_pFunctions[i] = nullptr; | |
| 106 } | |
| 107 | |
| 108 CPDF_ShadingPattern::~CPDF_ShadingPattern() { | |
| 109 for (int i = 0; i < m_nFuncs; ++i) | |
| 110 delete m_pFunctions[i]; | |
| 111 | |
| 112 CPDF_ColorSpace* pCS = m_pCountedCS ? m_pCountedCS->get() : NULL; | |
| 113 if (pCS && m_pDocument) | |
| 114 m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray()); | |
| 115 } | |
| 116 | |
| 117 FX_BOOL CPDF_ShadingPattern::Load() { | |
| 118 if (m_ShadingType != kInvalidShading) | |
| 119 return TRUE; | |
| 120 | |
| 121 CPDF_Dictionary* pShadingDict = | |
| 122 m_pShadingObj ? m_pShadingObj->GetDict() : NULL; | |
| 123 if (!pShadingDict) { | |
| 124 return FALSE; | |
| 125 } | |
| 126 if (m_nFuncs) { | |
| 127 for (int i = 0; i < m_nFuncs; i++) | |
| 128 delete m_pFunctions[i]; | |
| 129 m_nFuncs = 0; | |
| 130 } | |
| 131 CPDF_Object* pFunc = pShadingDict->GetElementValue("Function"); | |
| 132 if (pFunc) { | |
| 133 if (CPDF_Array* pArray = pFunc->AsArray()) { | |
| 134 m_nFuncs = std::min<int>(pArray->GetCount(), 4); | |
| 135 | |
| 136 for (int i = 0; i < m_nFuncs; i++) { | |
| 137 m_pFunctions[i] = CPDF_Function::Load(pArray->GetElementValue(i)); | |
| 138 } | |
| 139 } else { | |
| 140 m_pFunctions[0] = CPDF_Function::Load(pFunc); | |
| 141 m_nFuncs = 1; | |
| 142 } | |
| 143 } | |
| 144 CPDF_Object* pCSObj = pShadingDict->GetElementValue("ColorSpace"); | |
| 145 if (!pCSObj) { | |
| 146 return FALSE; | |
| 147 } | |
| 148 CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData(); | |
| 149 m_pCS = pDocPageData->GetColorSpace(pCSObj, NULL); | |
| 150 if (m_pCS) { | |
| 151 m_pCountedCS = pDocPageData->FindColorSpacePtr(m_pCS->GetArray()); | |
| 152 } | |
| 153 | |
| 154 m_ShadingType = ToShadingType(pShadingDict->GetIntegerBy("ShadingType")); | |
| 155 | |
| 156 // We expect to have a stream if our shading type is a mesh. | |
| 157 if (IsMeshShading() && !ToStream(m_pShadingObj)) | |
| 158 return FALSE; | |
| 159 | |
| 160 return TRUE; | |
| 161 } | |
| 162 FX_BOOL CPDF_MeshStream::Load(CPDF_Stream* pShadingStream, | |
| 163 CPDF_Function** pFuncs, | |
| 164 int nFuncs, | |
| 165 CPDF_ColorSpace* pCS) { | |
| 166 m_Stream.LoadAllData(pShadingStream); | |
| 167 m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize()); | |
| 168 m_pFuncs = pFuncs; | |
| 169 m_nFuncs = nFuncs; | |
| 170 m_pCS = pCS; | |
| 171 CPDF_Dictionary* pDict = pShadingStream->GetDict(); | |
| 172 m_nCoordBits = pDict->GetIntegerBy("BitsPerCoordinate"); | |
| 173 m_nCompBits = pDict->GetIntegerBy("BitsPerComponent"); | |
| 174 m_nFlagBits = pDict->GetIntegerBy("BitsPerFlag"); | |
| 175 if (!m_nCoordBits || !m_nCompBits) { | |
| 176 return FALSE; | |
| 177 } | |
| 178 FX_DWORD nComps = pCS->CountComponents(); | |
| 179 if (nComps > 8) { | |
| 180 return FALSE; | |
| 181 } | |
| 182 m_nComps = nFuncs ? 1 : nComps; | |
| 183 if (((int)m_nComps < 0) || m_nComps > 8) { | |
| 184 return FALSE; | |
| 185 } | |
| 186 m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1; | |
| 187 m_CompMax = (1 << m_nCompBits) - 1; | |
| 188 CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); | |
| 189 if (!pDecode || pDecode->GetCount() != 4 + m_nComps * 2) { | |
| 190 return FALSE; | |
| 191 } | |
| 192 m_xmin = pDecode->GetNumberAt(0); | |
| 193 m_xmax = pDecode->GetNumberAt(1); | |
| 194 m_ymin = pDecode->GetNumberAt(2); | |
| 195 m_ymax = pDecode->GetNumberAt(3); | |
| 196 for (FX_DWORD i = 0; i < m_nComps; i++) { | |
| 197 m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4); | |
| 198 m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5); | |
| 199 } | |
| 200 return TRUE; | |
| 201 } | |
| 202 FX_DWORD CPDF_MeshStream::GetFlag() { | |
| 203 return m_BitStream.GetBits(m_nFlagBits) & 0x03; | |
| 204 } | |
| 205 void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y) { | |
| 206 if (m_nCoordBits == 32) { | |
| 207 x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * | |
| 208 (m_xmax - m_xmin) / (double)m_CoordMax); | |
| 209 y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * | |
| 210 (m_ymax - m_ymin) / (double)m_CoordMax); | |
| 211 } else { | |
| 212 x = m_xmin + | |
| 213 m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax; | |
| 214 y = m_ymin + | |
| 215 m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax; | |
| 216 } | |
| 217 } | |
| 218 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) { | |
| 219 FX_DWORD i; | |
| 220 FX_FLOAT color_value[8]; | |
| 221 for (i = 0; i < m_nComps; i++) { | |
| 222 color_value[i] = m_ColorMin[i] + | |
| 223 m_BitStream.GetBits(m_nCompBits) * | |
| 224 (m_ColorMax[i] - m_ColorMin[i]) / m_CompMax; | |
| 225 } | |
| 226 if (m_nFuncs) { | |
| 227 static const int kMaxResults = 8; | |
| 228 FX_FLOAT result[kMaxResults]; | |
| 229 int nResults; | |
| 230 FXSYS_memset(result, 0, sizeof(result)); | |
| 231 for (FX_DWORD i = 0; i < m_nFuncs; i++) { | |
| 232 if (m_pFuncs[i] && m_pFuncs[i]->CountOutputs() <= kMaxResults) { | |
| 233 m_pFuncs[i]->Call(color_value, 1, result, nResults); | |
| 234 } | |
| 235 } | |
| 236 m_pCS->GetRGB(result, r, g, b); | |
| 237 } else { | |
| 238 m_pCS->GetRGB(color_value, r, g, b); | |
| 239 } | |
| 240 } | |
| 241 FX_DWORD CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, | |
| 242 CFX_Matrix* pObject2Bitmap) { | |
| 243 FX_DWORD flag = GetFlag(); | |
| 244 GetCoords(vertex.x, vertex.y); | |
| 245 pObject2Bitmap->Transform(vertex.x, vertex.y); | |
| 246 GetColor(vertex.r, vertex.g, vertex.b); | |
| 247 m_BitStream.ByteAlign(); | |
| 248 return flag; | |
| 249 } | |
| 250 FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, | |
| 251 int count, | |
| 252 CFX_Matrix* pObject2Bitmap) { | |
| 253 for (int i = 0; i < count; i++) { | |
| 254 if (m_BitStream.IsEOF()) { | |
| 255 return FALSE; | |
| 256 } | |
| 257 GetCoords(vertex[i].x, vertex[i].y); | |
| 258 pObject2Bitmap->Transform(vertex[i].x, vertex[i].y); | |
| 259 GetColor(vertex[i].r, vertex[i].g, vertex[i].b); | |
| 260 m_BitStream.ByteAlign(); | |
| 261 } | |
| 262 return TRUE; | |
| 263 } | |
| 264 | |
| 265 CFX_FloatRect GetShadingBBox(CPDF_Stream* pStream, | |
| 266 ShadingType type, | |
| 267 const CFX_Matrix* pMatrix, | |
| 268 CPDF_Function** pFuncs, | |
| 269 int nFuncs, | |
| 270 CPDF_ColorSpace* pCS) { | |
| 271 if (!pStream || !pStream->IsStream() || !pFuncs || !pCS) | |
| 272 return CFX_FloatRect(0, 0, 0, 0); | |
| 273 | |
| 274 CPDF_MeshStream stream; | |
| 275 if (!stream.Load(pStream, pFuncs, nFuncs, pCS)) | |
| 276 return CFX_FloatRect(0, 0, 0, 0); | |
| 277 | |
| 278 CFX_FloatRect rect; | |
| 279 bool bStarted = false; | |
| 280 bool bGouraud = type == kFreeFormGouraudTriangleMeshShading || | |
| 281 type == kLatticeFormGouraudTriangleMeshShading; | |
| 282 | |
| 283 int point_count = kSingleCoordinatePair; | |
| 284 if (type == kTensorProductPatchMeshShading) | |
| 285 point_count = kTensorCoordinatePairs; | |
| 286 else if (type == kCoonsPatchMeshShading) | |
| 287 point_count = kCoonsCoordinatePairs; | |
| 288 | |
| 289 int color_count = kSingleColorPerPatch; | |
| 290 if (type == kCoonsPatchMeshShading || type == kTensorProductPatchMeshShading) | |
| 291 color_count = kQuadColorsPerPatch; | |
| 292 | |
| 293 while (!stream.m_BitStream.IsEOF()) { | |
| 294 FX_DWORD flag = 0; | |
| 295 if (type != kLatticeFormGouraudTriangleMeshShading) | |
| 296 flag = stream.GetFlag(); | |
| 297 | |
| 298 if (!bGouraud && flag) { | |
| 299 point_count -= 4; | |
| 300 color_count -= 2; | |
| 301 } | |
| 302 | |
| 303 for (int i = 0; i < point_count; i++) { | |
| 304 FX_FLOAT x, y; | |
| 305 stream.GetCoords(x, y); | |
| 306 if (bStarted) { | |
| 307 rect.UpdateRect(x, y); | |
| 308 } else { | |
| 309 rect.InitRect(x, y); | |
| 310 bStarted = TRUE; | |
| 311 } | |
| 312 } | |
| 313 stream.m_BitStream.SkipBits(stream.m_nComps * stream.m_nCompBits * | |
| 314 color_count); | |
| 315 if (bGouraud) | |
| 316 stream.m_BitStream.ByteAlign(); | |
| 317 } | |
| 318 rect.Transform(pMatrix); | |
| 319 return rect; | |
| 320 } | |
| OLD | NEW |