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 #ifndef CORE_INCLUDE_FPDFAPI_FPDF_PAGEOBJ_H_ 8 #define CORE_INCLUDE_FPDFAPI_FPDF_PAGEOBJ_H_ 9 10 #include "../fxge/fx_ge.h" 11 #include "fpdf_resource.h" 12 13 class CPDF_Path; 14 class CPDF_ClipPathData; 15 class CPDF_ClipPath; 16 class CPDF_ColorStateData; 17 class CPDF_ColorState; 18 class CPDF_GraphState; 19 class CPDF_TextStateData; 20 class CPDF_TextState; 21 class CPDF_GeneralStateData; 22 class CPDF_GeneralState; 23 class CPDF_ContentMarkItem; 24 class CPDF_ContentMark; 25 class CPDF_GraphicStates; 26 class CPDF_PageObject; 27 class CPDF_TextObject; 28 class CPDF_PathObject; 29 class CPDF_ImageObject; 30 class CPDF_ShadingObject; 31 class CPDF_FormObject; 32 class CPDF_InlineImages; 33 typedef CFX_PathData CPDF_PathData; 34 class CPDF_Path : public CFX_CountRef<CFX_PathData> 35 { 36 public: 37 38 39 40 GetPointCount()41 int GetPointCount() 42 { 43 return m_pObject->m_PointCount; 44 } 45 GetFlag(int index)46 int GetFlag(int index) 47 { 48 return m_pObject->m_pPoints[index].m_Flag; 49 } 50 GetPointX(int index)51 FX_FLOAT GetPointX(int index) 52 { 53 return m_pObject->m_pPoints[index].m_PointX; 54 } 55 GetPointY(int index)56 FX_FLOAT GetPointY(int index) 57 { 58 return m_pObject->m_pPoints[index].m_PointY; 59 } 60 61 62 63 GetPoints()64 FX_PATHPOINT* GetPoints() 65 { 66 return m_pObject->m_pPoints; 67 } 68 69 GetBoundingBox()70 CFX_FloatRect GetBoundingBox() const 71 { 72 return m_pObject->GetBoundingBox(); 73 } 74 GetBoundingBox(FX_FLOAT line_width,FX_FLOAT miter_limit)75 CFX_FloatRect GetBoundingBox(FX_FLOAT line_width, FX_FLOAT miter_limit) const 76 { 77 return m_pObject->GetBoundingBox(line_width, miter_limit); 78 } 79 Transform(const CFX_AffineMatrix * pMatrix)80 void Transform(const CFX_AffineMatrix* pMatrix) 81 { 82 GetModify()->Transform(pMatrix); 83 } 84 Append(CPDF_Path src,const CFX_AffineMatrix * pMatrix)85 void Append(CPDF_Path src, const CFX_AffineMatrix* pMatrix) 86 { 87 m_pObject->Append(src.m_pObject, pMatrix); 88 } 89 AppendRect(FX_FLOAT left,FX_FLOAT bottom,FX_FLOAT right,FX_FLOAT top)90 void AppendRect(FX_FLOAT left, FX_FLOAT bottom, FX_FLOAT right, FX_FLOAT top) 91 { 92 m_pObject->AppendRect(left, bottom, right, top); 93 } 94 IsRect()95 FX_BOOL IsRect() const 96 { 97 return m_pObject->IsRect(); 98 } 99 }; 100 class CPDF_ClipPathData 101 { 102 public: 103 104 CPDF_ClipPathData(); 105 106 CPDF_ClipPathData(const CPDF_ClipPathData&); 107 108 ~CPDF_ClipPathData(); 109 110 void SetCount(int path_count, int text_count); 111 public: 112 113 int m_PathCount; 114 115 CPDF_Path* m_pPathList; 116 117 FX_BYTE* m_pTypeList; 118 119 int m_TextCount; 120 121 CPDF_TextObject** m_pTextList; 122 }; 123 class CPDF_ClipPath : public CFX_CountRef<CPDF_ClipPathData> 124 { 125 public: 126 GetPathCount()127 FX_DWORD GetPathCount() const 128 { 129 return m_pObject->m_PathCount; 130 } 131 GetPath(int i)132 CPDF_Path GetPath(int i) const 133 { 134 return m_pObject->m_pPathList[i]; 135 } 136 GetClipType(int i)137 int GetClipType(int i) const 138 { 139 return m_pObject->m_pTypeList[i]; 140 } 141 GetTextCount()142 FX_DWORD GetTextCount() const 143 { 144 return m_pObject->m_TextCount; 145 } 146 GetText(int i)147 CPDF_TextObject* GetText(int i) const 148 { 149 return m_pObject->m_pTextList[i]; 150 } 151 152 CFX_FloatRect GetClipBox() const; 153 154 void AppendPath(CPDF_Path path, int type, FX_BOOL bAutoMerge); 155 156 void DeletePath(int layer_index); 157 158 void AppendTexts(CPDF_TextObject** pTexts, int count); 159 160 void Transform(const CFX_AffineMatrix& matrix); 161 }; 162 class CPDF_ColorStateData 163 { 164 public: 165 CPDF_ColorStateData()166 CPDF_ColorStateData(): m_FillRGB(0), m_StrokeRGB(0) {} 167 168 CPDF_ColorStateData(const CPDF_ColorStateData& src); 169 170 void Default(); 171 172 CPDF_Color m_FillColor; 173 174 FX_DWORD m_FillRGB; 175 176 CPDF_Color m_StrokeColor; 177 178 FX_DWORD m_StrokeRGB; 179 }; 180 class CPDF_ColorState : public CFX_CountRef<CPDF_ColorStateData> 181 { 182 public: 183 GetFillColor()184 CPDF_Color* GetFillColor() const 185 { 186 return m_pObject ? &m_pObject->m_FillColor : NULL; 187 } 188 GetStrokeColor()189 CPDF_Color* GetStrokeColor() const 190 { 191 return m_pObject ? &m_pObject->m_StrokeColor : NULL; 192 } 193 194 void SetFillColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, int nValues); 195 196 void SetStrokeColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, int nValues); 197 198 void SetFillPattern(CPDF_Pattern* pattern, FX_FLOAT* pValue, int nValues); 199 200 void SetStrokePattern(CPDF_Pattern* pattern, FX_FLOAT* pValue, int nValues); 201 private: 202 void SetColor(CPDF_Color& color, FX_DWORD& rgb, CPDF_ColorSpace* pCS, FX_FLOAT* pValue, int nValues); 203 }; 204 typedef CFX_GraphStateData CPDF_GraphStateData; 205 class CPDF_GraphState : public CFX_CountRef<CFX_GraphStateData> 206 { 207 public: 208 }; 209 class CPDF_TextStateData 210 { 211 public: 212 213 CPDF_TextStateData(); 214 215 CPDF_TextStateData(const CPDF_TextStateData& src); 216 217 ~CPDF_TextStateData(); 218 219 CPDF_Font* m_pFont; 220 221 CPDF_Document* m_pDocument; 222 223 FX_FLOAT m_FontSize; 224 225 FX_FLOAT m_CharSpace; 226 227 FX_FLOAT m_WordSpace; 228 229 FX_FLOAT m_Matrix[4]; 230 231 int m_TextMode; 232 233 FX_FLOAT m_CTM[4]; 234 }; 235 class CPDF_TextState : public CFX_CountRef<CPDF_TextStateData> 236 { 237 public: 238 GetFont()239 CPDF_Font* GetFont() const 240 { 241 return m_pObject->m_pFont; 242 } 243 244 void SetFont(CPDF_Font* pFont); 245 GetFontSize()246 FX_FLOAT GetFontSize() const 247 { 248 return m_pObject->m_FontSize; 249 } 250 GetMatrix()251 FX_FLOAT* GetMatrix() const 252 { 253 return m_pObject->m_Matrix; 254 } 255 256 257 258 FX_FLOAT GetFontSizeV() const; 259 260 FX_FLOAT GetFontSizeH() const; 261 262 FX_FLOAT GetBaselineAngle() const; 263 264 FX_FLOAT GetShearAngle() const; 265 266 }; 267 class CPDF_TransferFunc; 268 class CPDF_GeneralStateData 269 { 270 public: 271 272 CPDF_GeneralStateData(); 273 274 CPDF_GeneralStateData(const CPDF_GeneralStateData& src); 275 ~CPDF_GeneralStateData(); 276 277 void SetBlendMode(FX_BSTR blend_mode); 278 279 char m_BlendMode[16]; 280 281 int m_BlendType; 282 283 CPDF_Object* m_pSoftMask; 284 285 FX_FLOAT m_SMaskMatrix[6]; 286 287 FX_FLOAT m_StrokeAlpha; 288 289 FX_FLOAT m_FillAlpha; 290 291 CPDF_Object* m_pTR; 292 293 CPDF_TransferFunc* m_pTransferFunc; 294 295 CFX_Matrix m_Matrix; 296 297 int m_RenderIntent; 298 299 FX_BOOL m_StrokeAdjust; 300 301 FX_BOOL m_AlphaSource; 302 303 FX_BOOL m_TextKnockout; 304 305 FX_BOOL m_StrokeOP; 306 307 FX_BOOL m_FillOP; 308 309 int m_OPMode; 310 311 CPDF_Object* m_pBG; 312 313 CPDF_Object* m_pUCR; 314 315 CPDF_Object* m_pHT; 316 317 FX_FLOAT m_Flatness; 318 319 FX_FLOAT m_Smoothness; 320 }; 321 class CPDF_GeneralState : public CFX_CountRef<CPDF_GeneralStateData> 322 { 323 public: 324 325 void SetRenderIntent(const CFX_ByteString& ri); 326 GetBlendType()327 int GetBlendType() const 328 { 329 return m_pObject ? m_pObject->m_BlendType : FXDIB_BLEND_NORMAL; 330 } 331 GetAlpha(FX_BOOL bStroke)332 int GetAlpha(FX_BOOL bStroke) const 333 { 334 return m_pObject ? FXSYS_round((bStroke ? m_pObject->m_StrokeAlpha : m_pObject->m_FillAlpha) * 255) : 255; 335 } 336 }; 337 class CPDF_ContentMarkItem 338 { 339 public: 340 341 typedef enum { 342 None, 343 PropertiesDict, 344 DirectDict, 345 MCID 346 } ParamType; 347 348 CPDF_ContentMarkItem(); 349 350 CPDF_ContentMarkItem(const CPDF_ContentMarkItem& src); 351 352 ~CPDF_ContentMarkItem(); 353 GetName()354 inline const CFX_ByteString& GetName() const 355 { 356 return m_MarkName; 357 } 358 GetParamType()359 inline ParamType GetParamType() const 360 { 361 return m_ParamType; 362 } 363 GetParam()364 inline void* GetParam() const 365 { 366 return m_pParam; 367 } 368 369 inline FX_BOOL HasMCID() const; 370 SetName(const CFX_ByteString & name)371 inline void SetName(const CFX_ByteString& name) 372 { 373 m_MarkName = name; 374 } 375 SetParam(ParamType type,void * param)376 inline void SetParam(ParamType type, void* param) 377 { 378 m_ParamType = type; 379 m_pParam = param; 380 } 381 private: 382 383 CFX_ByteString m_MarkName; 384 385 ParamType m_ParamType; 386 387 void* m_pParam; 388 }; 389 class CPDF_ContentMarkData 390 { 391 public: 392 CPDF_ContentMarkData()393 CPDF_ContentMarkData() { } 394 395 CPDF_ContentMarkData(const CPDF_ContentMarkData& src); 396 CountItems()397 inline int CountItems() const 398 { 399 return m_Marks.GetSize(); 400 } 401 GetItem(int index)402 inline CPDF_ContentMarkItem& GetItem(int index) const 403 { 404 return m_Marks[index]; 405 } 406 407 int GetMCID() const; 408 409 void AddMark(const CFX_ByteString& name, CPDF_Dictionary* pDict, FX_BOOL bDictNeedClone); 410 411 void DeleteLastMark(); 412 private: 413 414 CFX_ObjectArray<CPDF_ContentMarkItem> m_Marks; 415 }; 416 class CPDF_ContentMark : public CFX_CountRef<CPDF_ContentMarkData> 417 { 418 public: 419 GetMCID()420 int GetMCID() const 421 { 422 return m_pObject ? m_pObject->GetMCID() : -1; 423 } 424 425 FX_BOOL HasMark(FX_BSTR mark) const; 426 427 FX_BOOL LookupMark(FX_BSTR mark, CPDF_Dictionary*& pDict) const; 428 }; 429 #define PDFPAGE_TEXT 1 430 #define PDFPAGE_PATH 2 431 #define PDFPAGE_IMAGE 3 432 #define PDFPAGE_SHADING 4 433 #define PDFPAGE_FORM 5 434 #define PDFPAGE_INLINES 6 435 class CPDF_GraphicStates 436 { 437 public: 438 439 void CopyStates(const CPDF_GraphicStates& src); 440 441 void DefaultStates(); 442 443 CPDF_ClipPath m_ClipPath; 444 445 CPDF_GraphState m_GraphState; 446 447 CPDF_ColorState m_ColorState; 448 449 CPDF_TextState m_TextState; 450 451 CPDF_GeneralState m_GeneralState; 452 }; 453 class CPDF_PageObject : public CPDF_GraphicStates 454 { 455 public: 456 static CPDF_PageObject* Create(int type); 457 virtual ~CPDF_PageObject(); 458 459 CPDF_PageObject* Clone() const; 460 461 void Copy(const CPDF_PageObject* pSrcObject); 462 463 virtual void Transform(const CFX_AffineMatrix& matrix) = 0; 464 465 466 467 void RemoveClipPath(); 468 469 void AppendClipPath(CPDF_Path path, int type, FX_BOOL bAutoMerge); 470 471 void CopyClipPath(CPDF_PageObject* pObj); 472 473 void TransformClipPath(CFX_AffineMatrix& matrix); 474 475 void TransformGeneralState(CFX_AffineMatrix& matrix); 476 477 SetColorState(CPDF_ColorState state)478 void SetColorState(CPDF_ColorState state) 479 { 480 m_ColorState = state; 481 } 482 483 FX_RECT GetBBox(const CFX_AffineMatrix* pMatrix) const; 484 485 int m_Type; 486 487 FX_FLOAT m_Left; 488 489 FX_FLOAT m_Right; 490 491 FX_FLOAT m_Top; 492 493 FX_FLOAT m_Bottom; 494 495 CPDF_ContentMark m_ContentMark; 496 protected: 497 CopyData(const CPDF_PageObject * pSrcObject)498 virtual void CopyData(const CPDF_PageObject* pSrcObject) {} 499 500 void RecalcBBox(); 501 CPDF_PageObject()502 CPDF_PageObject() {} 503 504 }; 505 struct CPDF_TextObjectItem { 506 507 FX_DWORD m_CharCode; 508 509 FX_FLOAT m_OriginX; 510 511 FX_FLOAT m_OriginY; 512 }; 513 class CPDF_TextObject : public CPDF_PageObject 514 { 515 public: 516 517 CPDF_TextObject(); 518 519 virtual ~CPDF_TextObject(); 520 CountItems()521 int CountItems() const 522 { 523 return m_nChars; 524 } 525 526 void GetItemInfo(int index, CPDF_TextObjectItem* pInfo) const; 527 528 int CountChars() const; 529 530 void GetCharInfo(int index, FX_DWORD& charcode, FX_FLOAT& kerning) const; 531 void GetCharInfo(int index, CPDF_TextObjectItem* pInfo) const; 532 533 void GetCharRect(int index, CFX_FloatRect& rect) const; 534 535 536 FX_FLOAT GetCharWidth(FX_DWORD charcode) const; 537 FX_FLOAT GetSpaceCharWidth() const; 538 GetPosX()539 FX_FLOAT GetPosX() const 540 { 541 return m_PosX; 542 } 543 GetPosY()544 FX_FLOAT GetPosY() const 545 { 546 return m_PosY; 547 } 548 549 void GetTextMatrix(CFX_AffineMatrix* pMatrix) const; 550 GetFont()551 CPDF_Font* GetFont() const 552 { 553 return m_TextState.GetFont(); 554 } 555 GetFontSize()556 FX_FLOAT GetFontSize() const 557 { 558 return m_TextState.GetFontSize(); 559 } 560 561 void SetEmpty(); 562 563 void SetText(const CFX_ByteString& text); 564 565 void SetText(CFX_ByteString* pStrs, FX_FLOAT* pKerning, int nSegs); 566 567 void SetText(int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pKernings); 568 569 void SetPosition(FX_FLOAT x, FX_FLOAT y); 570 571 void SetTextState(CPDF_TextState TextState); 572 virtual void Transform(const CFX_AffineMatrix& matrix); 573 574 void CalcCharPos(FX_FLOAT* pPosArray) const; 575 576 577 578 void SetData(int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos, FX_FLOAT x, FX_FLOAT y); 579 GetData(int & nChars,FX_DWORD * & pCharCodes,FX_FLOAT * & pCharPos)580 void GetData(int& nChars, FX_DWORD*& pCharCodes, FX_FLOAT*& pCharPos) 581 { 582 nChars = m_nChars; 583 pCharCodes = m_pCharCodes; 584 pCharPos = m_pCharPos; 585 } 586 587 RecalcPositionData()588 void RecalcPositionData() 589 { 590 CalcPositionData(NULL, NULL, 1); 591 } 592 protected: 593 virtual void CopyData(const CPDF_PageObject* pSrcObject); 594 595 FX_FLOAT m_PosX; 596 597 FX_FLOAT m_PosY; 598 599 int m_nChars; 600 601 FX_DWORD* m_pCharCodes; 602 603 FX_FLOAT* m_pCharPos; 604 605 void SetSegments(const CFX_ByteString* pStrs, FX_FLOAT* pKerning, int nSegs); 606 607 void CalcPositionData(FX_FLOAT* pTextAdvanceX, FX_FLOAT* pTextAdvanceY, FX_FLOAT horz_scale, int level = 0); 608 friend class CPDF_StreamContentParser; 609 friend class CPDF_RenderStatus; 610 friend class CPDF_QuickDrawer; 611 friend class CPDF_TextRenderer; 612 friend class CTextPage; 613 friend class CPDF_ContentGenerator; 614 }; 615 class CPDF_PathObject : public CPDF_PageObject 616 { 617 public: 618 CPDF_PathObject()619 CPDF_PathObject() 620 { 621 m_Type = PDFPAGE_PATH; 622 } 623 ~CPDF_PathObject()624 virtual ~CPDF_PathObject() {} 625 virtual void Transform(const CFX_AffineMatrix& maxtrix); 626 627 void SetGraphState(CPDF_GraphState GraphState); 628 629 CPDF_Path m_Path; 630 631 int m_FillType; 632 633 FX_BOOL m_bStroke; 634 635 CFX_AffineMatrix m_Matrix; 636 637 638 void CalcBoundingBox(); 639 protected: 640 virtual void CopyData(const CPDF_PageObject* pSrcObjet); 641 }; 642 class CPDF_ImageObject : public CPDF_PageObject 643 { 644 public: 645 646 CPDF_ImageObject(); 647 648 virtual ~CPDF_ImageObject(); 649 virtual void Transform(const CFX_AffineMatrix& matrix); 650 651 CPDF_Image* m_pImage; 652 653 CFX_AffineMatrix m_Matrix; 654 655 void CalcBoundingBox(); 656 private: 657 virtual void CopyData(const CPDF_PageObject* pSrcObjet); 658 }; 659 class CPDF_ShadingObject : public CPDF_PageObject 660 { 661 public: 662 663 CPDF_ShadingObject(); 664 665 virtual ~CPDF_ShadingObject(); 666 667 CPDF_ShadingPattern* m_pShading; 668 669 CFX_AffineMatrix m_Matrix; 670 671 CPDF_Page* m_pPage; 672 virtual void Transform(const CFX_AffineMatrix& matrix); 673 674 void CalcBoundingBox(); 675 protected: 676 virtual void CopyData(const CPDF_PageObject* pSrcObjet); 677 }; 678 class CPDF_FormObject : public CPDF_PageObject 679 { 680 public: 681 CPDF_FormObject()682 CPDF_FormObject() 683 { 684 m_Type = PDFPAGE_FORM; 685 m_pForm = NULL; 686 } 687 688 virtual ~CPDF_FormObject(); 689 virtual void Transform(const CFX_AffineMatrix& matrix); 690 691 CPDF_Form* m_pForm; 692 693 CFX_AffineMatrix m_FormMatrix; 694 695 void CalcBoundingBox(); 696 protected: 697 virtual void CopyData(const CPDF_PageObject* pSrcObjet); 698 }; 699 class CPDF_InlineImages : public CPDF_PageObject 700 { 701 public: 702 703 CPDF_InlineImages(); 704 705 virtual ~CPDF_InlineImages(); 706 707 CPDF_Stream* m_pStream; 708 709 CFX_DIBitmap* m_pBitmap; 710 711 CFX_ArrayTemplate<CFX_AffineMatrix> m_Matrices; 712 713 void AddMatrix(CFX_AffineMatrix& matrix); 714 protected: Transform(const CFX_AffineMatrix & matrix)715 virtual void Transform(const CFX_AffineMatrix& matrix) {} CopyData(const CPDF_PageObject * pSrcObjet)716 virtual void CopyData(const CPDF_PageObject* pSrcObjet) {} 717 }; 718 719 #endif // CORE_INCLUDE_FPDFAPI_FPDF_PAGEOBJ_H_ 720