fpdf_pageobj.h revision ee451cb395940862dad63c85adfe8f2fd55e864c
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
15class CPDF_Path;
16class CPDF_ClipPathData;
17class CPDF_ClipPath;
18class CPDF_ColorStateData;
19class CPDF_ColorState;
20class CPDF_GraphState;
21class CPDF_TextStateData;
22class CPDF_TextState;
23class CPDF_GeneralStateData;
24class CPDF_GeneralState;
25class CPDF_ContentMarkItem;
26class CPDF_ContentMark;
27class CPDF_GraphicStates;
28class CPDF_PageObject;
29class CPDF_TextObject;
30class CPDF_PathObject;
31class CPDF_ImageObject;
32class CPDF_ShadingObject;
33class CPDF_FormObject;
34class CPDF_InlineImages;
35typedef CFX_PathData CPDF_PathData;
36class CPDF_Path : public CFX_CountRef<CFX_PathData>
37{
38public:
39
40
41
42
43    int					GetPointCount()
44    {
45        return m_pObject->m_PointCount;
46    }
47
48    int					GetFlag(int index)
49    {
50        return m_pObject->m_pPoints[index].m_Flag;
51    }
52
53    FX_FLOAT			GetPointX(int index)
54    {
55        return m_pObject->m_pPoints[index].m_PointX;
56    }
57
58    FX_FLOAT			GetPointY(int index)
59    {
60        return m_pObject->m_pPoints[index].m_PointY;
61    }
62
63
64
65
66    FX_PATHPOINT*		GetPoints()
67    {
68        return m_pObject->m_pPoints;
69    }
70
71
72    CFX_FloatRect		GetBoundingBox() const
73    {
74        return m_pObject->GetBoundingBox();
75    }
76
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
82    void				Transform(const CFX_AffineMatrix* pMatrix)
83    {
84        GetModify()->Transform(pMatrix);
85    }
86
87    void				Append(CPDF_Path src, const CFX_AffineMatrix* pMatrix)
88    {
89        m_pObject->Append(src.m_pObject, pMatrix);
90    }
91
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
97    FX_BOOL				IsRect() const
98    {
99        return m_pObject->IsRect();
100    }
101};
102class CPDF_ClipPathData : public CFX_Object
103{
104public:
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);
113public:
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};
125class CPDF_ClipPath : public CFX_CountRef<CPDF_ClipPathData>
126{
127public:
128
129    FX_DWORD			GetPathCount() const
130    {
131        return m_pObject->m_PathCount;
132    }
133
134    CPDF_Path			GetPath(int i) const
135    {
136        return m_pObject->m_pPathList[i];
137    }
138
139    int					GetClipType(int i) const
140    {
141        return m_pObject->m_pTypeList[i];
142    }
143
144    FX_DWORD			GetTextCount() const
145    {
146        return m_pObject->m_TextCount;
147    }
148
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};
164class CPDF_ColorStateData : public CFX_Object
165{
166public:
167
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};
182class CPDF_ColorState : public CFX_CountRef<CPDF_ColorStateData>
183{
184public:
185
186    CPDF_Color*			GetFillColor() const
187    {
188        return m_pObject ? &m_pObject->m_FillColor : NULL;
189    }
190
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);
203private:
204    void				SetColor(CPDF_Color& color, FX_DWORD& rgb, CPDF_ColorSpace* pCS, FX_FLOAT* pValue, int nValues);
205};
206typedef CFX_GraphStateData CPDF_GraphStateData;
207class CPDF_GraphState : public CFX_CountRef<CFX_GraphStateData>
208{
209public:
210};
211class CPDF_TextStateData : public CFX_Object
212{
213public:
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};
235class CPDF_TextState : public CFX_CountRef<CPDF_TextStateData>
236{
237public:
238
239    CPDF_Font*			GetFont() const
240    {
241        return m_pObject->m_pFont;
242    }
243
244    void				SetFont(CPDF_Font* pFont);
245
246    FX_FLOAT			GetFontSize() const
247    {
248        return m_pObject->m_FontSize;
249    }
250
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};
267class CPDF_TransferFunc;
268class CPDF_GeneralStateData : public CFX_Object
269{
270public:
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};
321class CPDF_GeneralState : public CFX_CountRef<CPDF_GeneralStateData>
322{
323public:
324
325    void				SetRenderIntent(const CFX_ByteString& ri);
326
327    int					GetBlendType() const
328    {
329        return m_pObject ? m_pObject->m_BlendType : FXDIB_BLEND_NORMAL;
330    }
331
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};
337class CPDF_ContentMarkItem : public CFX_Object
338{
339public:
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
354    inline const CFX_ByteString&	GetName() const
355    {
356        return m_MarkName;
357    }
358
359    inline ParamType	GetParamType() const
360    {
361        return m_ParamType;
362    }
363
364    inline void*		GetParam() const
365    {
366        return m_pParam;
367    }
368
369    inline FX_BOOL		HasMCID() const;
370
371    inline void			SetName(const CFX_ByteString& name)
372    {
373        m_MarkName = name;
374    }
375
376    inline void			SetParam(ParamType type, void* param)
377    {
378        m_ParamType = type;
379        m_pParam = param;
380    }
381private:
382
383    CFX_ByteString		m_MarkName;
384
385    ParamType			m_ParamType;
386
387    void*				m_pParam;
388};
389class CPDF_ContentMarkData : public CFX_Object
390{
391public:
392
393    CPDF_ContentMarkData() { }
394
395    CPDF_ContentMarkData(const CPDF_ContentMarkData& src);
396
397    inline int			CountItems() const
398    {
399        return m_Marks.GetSize();
400    }
401
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();
412private:
413
414    CFX_ObjectArray<CPDF_ContentMarkItem>	m_Marks;
415};
416class CPDF_ContentMark : public CFX_CountRef<CPDF_ContentMarkData>
417{
418public:
419
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
435class CPDF_GraphicStates : public CFX_Object
436{
437public:
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};
453class CPDF_PageObject : public CPDF_GraphicStates
454{
455public:
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
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;
498protected:
499
500    virtual void		CopyData(const CPDF_PageObject* pSrcObject) {}
501
502    void				RecalcBBox();
503
504    CPDF_PageObject() {}
505
506    virtual ~CPDF_PageObject() {}
507};
508struct CPDF_TextObjectItem : public CFX_Object {
509
510    FX_DWORD			m_CharCode;
511
512    FX_FLOAT			m_OriginX;
513
514    FX_FLOAT			m_OriginY;
515};
516class CPDF_TextObject : public CPDF_PageObject
517{
518public:
519
520    CPDF_TextObject();
521
522    virtual ~CPDF_TextObject();
523
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
542    FX_FLOAT			GetPosX() const
543    {
544        return m_PosX;
545    }
546
547    FX_FLOAT			GetPosY() const
548    {
549        return m_PosY;
550    }
551
552    void				GetTextMatrix(CFX_AffineMatrix* pMatrix) const;
553
554    CPDF_Font*			GetFont() const
555    {
556        return m_TextState.GetFont();
557    }
558
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
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
591    void				RecalcPositionData()
592    {
593        CalcPositionData(NULL, NULL, 1);
594    }
595protected:
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};
618class CPDF_PathObject : public CPDF_PageObject
619{
620public:
621
622    CPDF_PathObject()
623    {
624        m_Type = PDFPAGE_PATH;
625    }
626
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();
642protected:
643    virtual void		CopyData(const CPDF_PageObject* pSrcObjet);
644};
645class CPDF_ImageObject : public CPDF_PageObject
646{
647public:
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();
659private:
660    virtual void		CopyData(const CPDF_PageObject* pSrcObjet);
661};
662class CPDF_ShadingObject : public CPDF_PageObject
663{
664public:
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();
678protected:
679    virtual void		CopyData(const CPDF_PageObject* pSrcObjet);
680};
681class CPDF_FormObject : public CPDF_PageObject
682{
683public:
684
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();
699protected:
700    virtual void		CopyData(const CPDF_PageObject* pSrcObjet);
701};
702class CPDF_InlineImages : public CPDF_PageObject
703{
704public:
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);
717protected:
718    virtual void		Transform(const CFX_AffineMatrix& matrix) {}
719    virtual void		CopyData(const CPDF_PageObject* pSrcObjet) {}
720};
721#endif
722