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 _FPDF_PAGEOBJ_H_ 8 #define _FPDF_PAGEOBJ_H_ 9 #ifndef _FPDF_RESOURCE_ 10 #include "fpdf_resource.h" 11 #endif 12 #ifndef _FX_GE_H_ 13 #include "../fxge/fx_ge.h" 14 #endif 15 class CPDF_Path; 16 class CPDF_ClipPathData; 17 class CPDF_ClipPath; 18 class CPDF_ColorStateData; 19 class CPDF_ColorState; 20 class CPDF_GraphState; 21 class CPDF_TextStateData; 22 class CPDF_TextState; 23 class CPDF_GeneralStateData; 24 class CPDF_GeneralState; 25 class CPDF_ContentMarkItem; 26 class CPDF_ContentMark; 27 class CPDF_GraphicStates; 28 class CPDF_PageObject; 29 class CPDF_TextObject; 30 class CPDF_PathObject; 31 class CPDF_ImageObject; 32 class CPDF_ShadingObject; 33 class CPDF_FormObject; 34 class CPDF_InlineImages; 35 typedef CFX_PathData CPDF_PathData; 36 class CPDF_Path : public CFX_CountRef<CFX_PathData> 37 { 38 public: 39 40 41 42 GetPointCount()43 int GetPointCount() 44 { 45 return m_pObject->m_PointCount; 46 } 47 GetFlag(int index)48 int GetFlag(int index) 49 { 50 return m_pObject->m_pPoints[index].m_Flag; 51 } 52 GetPointX(int index)53 FX_FLOAT GetPointX(int index) 54 { 55 return m_pObject->m_pPoints[index].m_PointX; 56 } 57 GetPointY(int index)58 FX_FLOAT GetPointY(int index) 59 { 60 return m_pObject->m_pPoints[index].m_PointY; 61 } 62 63 64 65 GetPoints()66 FX_PATHPOINT* GetPoints() 67 { 68 return m_pObject->m_pPoints; 69 } 70 71 GetBoundingBox()72 CFX_FloatRect GetBoundingBox() const 73 { 74 return m_pObject->GetBoundingBox(); 75 } 76 GetBoundingBox(FX_FLOAT line_width,FX_FLOAT miter_limit)77 CFX_FloatRect GetBoundingBox(FX_FLOAT line_width, FX_FLOAT miter_limit) const 78 { 79 return m_pObject->GetBoundingBox(line_width, miter_limit); 80 } 81 Transform(const CFX_AffineMatrix * pMatrix)82 void Transform(const CFX_AffineMatrix* pMatrix) 83 { 84 GetModify()->Transform(pMatrix); 85 } 86 Append(CPDF_Path src,const CFX_AffineMatrix * pMatrix)87 void Append(CPDF_Path src, const CFX_AffineMatrix* pMatrix) 88 { 89 m_pObject->Append(src.m_pObject, pMatrix); 90 } 91 AppendRect(FX_FLOAT left,FX_FLOAT bottom,FX_FLOAT right,FX_FLOAT top)92 void AppendRect(FX_FLOAT left, FX_FLOAT bottom, FX_FLOAT right, FX_FLOAT top) 93 { 94 m_pObject->AppendRect(left, bottom, right, top); 95 } 96 IsRect()97 FX_BOOL IsRect() const 98 { 99 return m_pObject->IsRect(); 100 } 101 }; 102 class CPDF_ClipPathData : public CFX_Object 103 { 104 public: 105 106 CPDF_ClipPathData(); 107 108 CPDF_ClipPathData(const CPDF_ClipPathData&); 109 110 ~CPDF_ClipPathData(); 111 112 void SetCount(int path_count, int text_count); 113 public: 114 115 int m_PathCount; 116 117 CPDF_Path* m_pPathList; 118 119 FX_BYTE* m_pTypeList; 120 121 int m_TextCount; 122 123 CPDF_TextObject** m_pTextList; 124 }; 125 class CPDF_ClipPath : public CFX_CountRef<CPDF_ClipPathData> 126 { 127 public: 128 GetPathCount()129 FX_DWORD GetPathCount() const 130 { 131 return m_pObject->m_PathCount; 132 } 133 GetPath(int i)134 CPDF_Path GetPath(int i) const 135 { 136 return m_pObject->m_pPathList[i]; 137 } 138 GetClipType(int i)139 int GetClipType(int i) const 140 { 141 return m_pObject->m_pTypeList[i]; 142 } 143 GetTextCount()144 FX_DWORD GetTextCount() const 145 { 146 return m_pObject->m_TextCount; 147 } 148 GetText(int i)149 CPDF_TextObject* GetText(int i) const 150 { 151 return m_pObject->m_pTextList[i]; 152 } 153 154 CFX_FloatRect GetClipBox() const; 155 156 void AppendPath(CPDF_Path path, int type, FX_BOOL bAutoMerge); 157 158 void DeletePath(int layer_index); 159 160 void AppendTexts(CPDF_TextObject** pTexts, int count); 161 162 void Transform(const CFX_AffineMatrix& matrix); 163 }; 164 class CPDF_ColorStateData : public CFX_Object 165 { 166 public: 167 CPDF_ColorStateData()168 CPDF_ColorStateData() {} 169 170 CPDF_ColorStateData(const CPDF_ColorStateData& src); 171 172 void Default(); 173 174 CPDF_Color m_FillColor; 175 176 FX_DWORD m_FillRGB; 177 178 CPDF_Color m_StrokeColor; 179 180 FX_DWORD m_StrokeRGB; 181 }; 182 class CPDF_ColorState : public CFX_CountRef<CPDF_ColorStateData> 183 { 184 public: 185 GetFillColor()186 CPDF_Color* GetFillColor() const 187 { 188 return m_pObject ? &m_pObject->m_FillColor : NULL; 189 } 190 GetStrokeColor()191 CPDF_Color* GetStrokeColor() const 192 { 193 return m_pObject ? &m_pObject->m_StrokeColor : NULL; 194 } 195 196 void SetFillColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, int nValues); 197 198 void SetStrokeColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, int nValues); 199 200 void SetFillPattern(CPDF_Pattern* pattern, FX_FLOAT* pValue, int nValues); 201 202 void SetStrokePattern(CPDF_Pattern* pattern, FX_FLOAT* pValue, int nValues); 203 private: 204 void SetColor(CPDF_Color& color, FX_DWORD& rgb, CPDF_ColorSpace* pCS, FX_FLOAT* pValue, int nValues); 205 }; 206 typedef CFX_GraphStateData CPDF_GraphStateData; 207 class CPDF_GraphState : public CFX_CountRef<CFX_GraphStateData> 208 { 209 public: 210 }; 211 class CPDF_TextStateData : public CFX_Object 212 { 213 public: 214 215 CPDF_TextStateData(); 216 217 CPDF_TextStateData(const CPDF_TextStateData& src); 218 219 ~CPDF_TextStateData(); 220 221 CPDF_Font* m_pFont; 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 : public CFX_Object 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 : public CFX_Object 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 : public CFX_Object 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 : public CFX_Object 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 457 static CPDF_PageObject* Create(int type); 458 459 void Release(); 460 461 CPDF_PageObject* Clone() const; 462 463 void Copy(const CPDF_PageObject* pSrcObject); 464 465 virtual void Transform(const CFX_AffineMatrix& matrix) = 0; 466 467 468 469 void RemoveClipPath(); 470 471 void AppendClipPath(CPDF_Path path, int type, FX_BOOL bAutoMerge); 472 473 void CopyClipPath(CPDF_PageObject* pObj); 474 475 void TransformClipPath(CFX_AffineMatrix& matrix); 476 477 void TransformGeneralState(CFX_AffineMatrix& matrix); 478 479 SetColorState(CPDF_ColorState state)480 void SetColorState(CPDF_ColorState state) 481 { 482 m_ColorState = state; 483 } 484 485 FX_RECT GetBBox(const CFX_AffineMatrix* pMatrix) const; 486 487 int m_Type; 488 489 FX_FLOAT m_Left; 490 491 FX_FLOAT m_Right; 492 493 FX_FLOAT m_Top; 494 495 FX_FLOAT m_Bottom; 496 497 CPDF_ContentMark m_ContentMark; 498 protected: 499 CopyData(const CPDF_PageObject * pSrcObject)500 virtual void CopyData(const CPDF_PageObject* pSrcObject) {} 501 502 void RecalcBBox(); 503 CPDF_PageObject()504 CPDF_PageObject() {} 505 ~CPDF_PageObject()506 virtual ~CPDF_PageObject() {} 507 }; 508 struct CPDF_TextObjectItem : public CFX_Object { 509 510 FX_DWORD m_CharCode; 511 512 FX_FLOAT m_OriginX; 513 514 FX_FLOAT m_OriginY; 515 }; 516 class CPDF_TextObject : public CPDF_PageObject 517 { 518 public: 519 520 CPDF_TextObject(); 521 522 virtual ~CPDF_TextObject(); 523 CountItems()524 int CountItems() const 525 { 526 return m_nChars; 527 } 528 529 void GetItemInfo(int index, CPDF_TextObjectItem* pInfo) const; 530 531 int CountChars() const; 532 533 void GetCharInfo(int index, FX_DWORD& charcode, FX_FLOAT& kerning) const; 534 void GetCharInfo(int index, CPDF_TextObjectItem* pInfo) const; 535 536 void GetCharRect(int index, CFX_FloatRect& rect) const; 537 538 539 FX_FLOAT GetCharWidth(FX_DWORD charcode) const; 540 FX_FLOAT GetSpaceCharWidth() const; 541 GetPosX()542 FX_FLOAT GetPosX() const 543 { 544 return m_PosX; 545 } 546 GetPosY()547 FX_FLOAT GetPosY() const 548 { 549 return m_PosY; 550 } 551 552 void GetTextMatrix(CFX_AffineMatrix* pMatrix) const; 553 GetFont()554 CPDF_Font* GetFont() const 555 { 556 return m_TextState.GetFont(); 557 } 558 GetFontSize()559 FX_FLOAT GetFontSize() const 560 { 561 return m_TextState.GetFontSize(); 562 } 563 564 void SetEmpty(); 565 566 void SetText(const CFX_ByteString& text); 567 568 void SetText(CFX_ByteString* pStrs, FX_FLOAT* pKerning, int nSegs); 569 570 void SetText(int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pKernings); 571 572 void SetPosition(FX_FLOAT x, FX_FLOAT y); 573 574 void SetTextState(CPDF_TextState TextState); 575 virtual void Transform(const CFX_AffineMatrix& matrix); 576 577 void CalcCharPos(FX_FLOAT* pPosArray) const; 578 579 580 581 void SetData(int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos, FX_FLOAT x, FX_FLOAT y); 582 GetData(int & nChars,FX_DWORD * & pCharCodes,FX_FLOAT * & pCharPos)583 void GetData(int& nChars, FX_DWORD*& pCharCodes, FX_FLOAT*& pCharPos) 584 { 585 nChars = m_nChars; 586 pCharCodes = m_pCharCodes; 587 pCharPos = m_pCharPos; 588 } 589 590 RecalcPositionData()591 void RecalcPositionData() 592 { 593 CalcPositionData(NULL, NULL, 1); 594 } 595 protected: 596 virtual void CopyData(const CPDF_PageObject* pSrcObject); 597 598 FX_FLOAT m_PosX; 599 600 FX_FLOAT m_PosY; 601 602 int m_nChars; 603 604 FX_DWORD* m_pCharCodes; 605 606 FX_FLOAT* m_pCharPos; 607 608 void SetSegments(const CFX_ByteString* pStrs, FX_FLOAT* pKerning, int nSegs); 609 610 void CalcPositionData(FX_FLOAT* pTextAdvanceX, FX_FLOAT* pTextAdvanceY, FX_FLOAT horz_scale, int level = 0); 611 friend class CPDF_StreamContentParser; 612 friend class CPDF_RenderStatus; 613 friend class CPDF_QuickDrawer; 614 friend class CPDF_TextRenderer; 615 friend class CTextPage; 616 friend class CPDF_ContentGenerator; 617 }; 618 class CPDF_PathObject : public CPDF_PageObject 619 { 620 public: 621 CPDF_PathObject()622 CPDF_PathObject() 623 { 624 m_Type = PDFPAGE_PATH; 625 } 626 ~CPDF_PathObject()627 virtual ~CPDF_PathObject() {} 628 virtual void Transform(const CFX_AffineMatrix& maxtrix); 629 630 void SetGraphState(CPDF_GraphState GraphState); 631 632 CPDF_Path m_Path; 633 634 int m_FillType; 635 636 FX_BOOL m_bStroke; 637 638 CFX_AffineMatrix m_Matrix; 639 640 641 void CalcBoundingBox(); 642 protected: 643 virtual void CopyData(const CPDF_PageObject* pSrcObjet); 644 }; 645 class CPDF_ImageObject : public CPDF_PageObject 646 { 647 public: 648 649 CPDF_ImageObject(); 650 651 virtual ~CPDF_ImageObject(); 652 virtual void Transform(const CFX_AffineMatrix& matrix); 653 654 CPDF_Image* m_pImage; 655 656 CFX_AffineMatrix m_Matrix; 657 658 void CalcBoundingBox(); 659 private: 660 virtual void CopyData(const CPDF_PageObject* pSrcObjet); 661 }; 662 class CPDF_ShadingObject : public CPDF_PageObject 663 { 664 public: 665 666 CPDF_ShadingObject(); 667 668 virtual ~CPDF_ShadingObject(); 669 670 CPDF_ShadingPattern* m_pShading; 671 672 CFX_AffineMatrix m_Matrix; 673 674 CPDF_Page* m_pPage; 675 virtual void Transform(const CFX_AffineMatrix& matrix); 676 677 void CalcBoundingBox(); 678 protected: 679 virtual void CopyData(const CPDF_PageObject* pSrcObjet); 680 }; 681 class CPDF_FormObject : public CPDF_PageObject 682 { 683 public: 684 CPDF_FormObject()685 CPDF_FormObject() 686 { 687 m_Type = PDFPAGE_FORM; 688 m_pForm = NULL; 689 } 690 691 virtual ~CPDF_FormObject(); 692 virtual void Transform(const CFX_AffineMatrix& matrix); 693 694 CPDF_Form* m_pForm; 695 696 CFX_AffineMatrix m_FormMatrix; 697 698 void CalcBoundingBox(); 699 protected: 700 virtual void CopyData(const CPDF_PageObject* pSrcObjet); 701 }; 702 class CPDF_InlineImages : public CPDF_PageObject 703 { 704 public: 705 706 CPDF_InlineImages(); 707 708 virtual ~CPDF_InlineImages(); 709 710 CPDF_Stream* m_pStream; 711 712 CFX_DIBitmap* m_pBitmap; 713 714 CFX_ArrayTemplate<CFX_AffineMatrix> m_Matrices; 715 716 void AddMatrix(CFX_AffineMatrix& matrix); 717 protected: Transform(const CFX_AffineMatrix & matrix)718 virtual void Transform(const CFX_AffineMatrix& matrix) {} CopyData(const CPDF_PageObject * pSrcObjet)719 virtual void CopyData(const CPDF_PageObject* pSrcObjet) {} 720 }; 721 #endif 722