Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / pdfium / core / src / fpdfapi / fpdf_page / fpdf_page.cpp
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 "../../../include/fpdfapi/fpdf_page.h"
8 #include "../../../include/fpdfapi/fpdf_module.h"
9 #include "pageint.h"
10 void CPDF_PageObject::Release()
11 {
12     delete this;
13 }
14 CPDF_PageObject* CPDF_PageObject::Create(int type)
15 {
16     switch (type) {
17         case PDFPAGE_TEXT:
18             return FX_NEW CPDF_TextObject;
19         case PDFPAGE_IMAGE:
20             return FX_NEW CPDF_ImageObject;
21         case PDFPAGE_PATH:
22             return FX_NEW CPDF_PathObject;
23         case PDFPAGE_SHADING:
24             return FX_NEW CPDF_ShadingObject;
25         case PDFPAGE_FORM:
26             return FX_NEW CPDF_FormObject;
27     }
28     return NULL;
29 }
30 CPDF_PageObject* CPDF_PageObject::Clone() const
31 {
32     CPDF_PageObject* pObj = Create(m_Type);
33     pObj->Copy(this);
34     return pObj;
35 }
36 void CPDF_PageObject::Copy(const CPDF_PageObject* pSrc)
37 {
38     if (m_Type != pSrc->m_Type) {
39         return;
40     }
41     CopyData(pSrc);
42     CopyStates(*pSrc);
43     m_Left = pSrc->m_Left;
44     m_Right = pSrc->m_Right;
45     m_Top = pSrc->m_Top;
46     m_Bottom = pSrc->m_Bottom;
47 }
48 void CPDF_PageObject::AppendClipPath(CPDF_Path path, int type, FX_BOOL bAutoMerge)
49 {
50     m_ClipPath.AppendPath(path, type, bAutoMerge);
51 }
52 void CPDF_PageObject::CopyClipPath(CPDF_PageObject* pObj)
53 {
54     m_ClipPath = pObj->m_ClipPath;
55 }
56 void CPDF_PageObject::RemoveClipPath()
57 {
58     m_ClipPath.SetNull();
59 }
60 void CPDF_PageObject::RecalcBBox()
61 {
62     switch (m_Type) {
63         case PDFPAGE_TEXT:
64             ((CPDF_TextObject*)this)->RecalcPositionData();
65             break;
66         case PDFPAGE_PATH:
67             ((CPDF_PathObject*)this)->CalcBoundingBox();
68             break;
69         case PDFPAGE_SHADING:
70             ((CPDF_ShadingObject*)this)->CalcBoundingBox();
71             break;
72     }
73 }
74 void CPDF_PageObject::TransformClipPath(CFX_AffineMatrix& matrix)
75 {
76     if (m_ClipPath.IsNull()) {
77         return;
78     }
79     m_ClipPath.GetModify();
80     m_ClipPath.Transform(matrix);
81 }
82 void CPDF_PageObject::TransformGeneralState(CFX_AffineMatrix& matrix)
83 {
84     if(m_GeneralState.IsNull()) {
85         return;
86     }
87     CPDF_GeneralStateData* pGS = m_GeneralState.GetModify();
88     pGS->m_Matrix.Concat(matrix);
89 }
90 FX_RECT CPDF_PageObject::GetBBox(const CFX_AffineMatrix* pMatrix) const
91 {
92     CFX_FloatRect rect(m_Left, m_Bottom, m_Right, m_Top);
93     if (pMatrix) {
94         pMatrix->TransformRect(rect);
95     }
96     return rect.GetOutterRect();
97 }
98 CPDF_TextObject::CPDF_TextObject()
99 {
100     m_Type = PDFPAGE_TEXT;
101     m_pCharCodes = NULL;
102     m_pCharPos = NULL;
103     m_nChars = 0;
104     m_PosX = m_PosY = 0;
105 }
106 CPDF_TextObject::~CPDF_TextObject()
107 {
108     if (m_nChars > 1 && m_pCharCodes) {
109         FX_Free(m_pCharCodes);
110     }
111     if (m_pCharPos) {
112         FX_Free(m_pCharPos);
113     }
114 }
115 void CPDF_TextObject::GetItemInfo(int index, CPDF_TextObjectItem* pInfo) const
116 {
117     pInfo->m_CharCode = m_nChars == 1 ? (FX_DWORD)(FX_UINTPTR)m_pCharCodes : m_pCharCodes[index];
118     pInfo->m_OriginX = index ? m_pCharPos[index - 1] : 0;
119     pInfo->m_OriginY = 0;
120     if (pInfo->m_CharCode == -1) {
121         return;
122     }
123     CPDF_Font* pFont = m_TextState.GetFont();
124     if (pFont->GetFontType() != PDFFONT_CIDFONT) {
125         return;
126     }
127     if (!((CPDF_CIDFont*)pFont)->IsVertWriting()) {
128         return;
129     }
130     FX_WORD CID = ((CPDF_CIDFont*)pFont)->CIDFromCharCode(pInfo->m_CharCode);
131     pInfo->m_OriginY = pInfo->m_OriginX;
132     pInfo->m_OriginX = 0;
133     short vx, vy;
134     ((CPDF_CIDFont*)pFont)->GetVertOrigin(CID, vx, vy);
135     FX_FLOAT fontsize = m_TextState.GetFontSize();
136     pInfo->m_OriginX -= fontsize * vx / 1000;
137     pInfo->m_OriginY -= fontsize * vy / 1000;
138 }
139 int CPDF_TextObject::CountChars() const
140 {
141     if (m_nChars == 1) {
142         return 1;
143     }
144     int count = 0;
145     for (int i = 0; i < m_nChars; i ++)
146         if (m_pCharCodes[i] != (FX_DWORD) - 1) {
147             count ++;
148         }
149     return count;
150 }
151 void CPDF_TextObject::GetCharInfo(int index, FX_DWORD& charcode, FX_FLOAT& kerning) const
152 {
153     if (m_nChars == 1) {
154         charcode = (FX_DWORD)(FX_UINTPTR)m_pCharCodes;
155         kerning = 0;
156         return;
157     }
158     int count = 0;
159     for (int i = 0; i < m_nChars; i ++) {
160         if (m_pCharCodes[i] != (FX_DWORD) - 1) {
161             if (count == index) {
162                 charcode = m_pCharCodes[i];
163                 if (i == m_nChars - 1 || m_pCharCodes[i + 1] != (FX_DWORD) - 1) {
164                     kerning = 0;
165                 } else {
166                     kerning = m_pCharPos[i];
167                 }
168                 return;
169             }
170             count ++;
171         }
172     }
173 }
174 void CPDF_TextObject::GetCharInfo(int index, CPDF_TextObjectItem* pInfo) const
175 {
176     if (m_nChars == 1) {
177         GetItemInfo(0, pInfo);
178         return;
179     }
180     int count = 0;
181     for (int i = 0; i < m_nChars; i ++) {
182         FX_DWORD charcode = m_pCharCodes[i];
183         if (charcode == (FX_DWORD) - 1) {
184             continue;
185         }
186         if (count == index) {
187             GetItemInfo(i, pInfo);
188             break;
189         }
190         count ++;
191     }
192 }
193 void CPDF_TextObject::CopyData(const CPDF_PageObject* pSrc)
194 {
195     const CPDF_TextObject* pSrcObj = (const CPDF_TextObject*)pSrc;
196     if (m_nChars > 1 && m_pCharCodes) {
197         FX_Free(m_pCharCodes);
198         m_pCharCodes = NULL;
199     }
200     if (m_pCharPos) {
201         FX_Free(m_pCharPos);
202         m_pCharPos = NULL;
203     }
204     m_nChars = pSrcObj->m_nChars;
205     if (m_nChars > 1) {
206         m_pCharCodes = FX_Alloc(FX_DWORD, m_nChars);
207         m_pCharPos = FX_Alloc(FX_FLOAT, m_nChars - 1);
208         int i;
209         for (i = 0; i < m_nChars; i ++) {
210             m_pCharCodes[i] = pSrcObj->m_pCharCodes[i];
211         }
212         for (i = 0; i < m_nChars - 1; i ++) {
213             m_pCharPos[i] = pSrcObj->m_pCharPos[i];
214         }
215     } else {
216         m_pCharCodes = pSrcObj->m_pCharCodes;
217     }
218     m_PosX = pSrcObj->m_PosX;
219     m_PosY = pSrcObj->m_PosY;
220 }
221 void CPDF_TextObject::GetTextMatrix(CFX_AffineMatrix* pMatrix) const
222 {
223     FX_FLOAT* pTextMatrix = m_TextState.GetMatrix();
224     pMatrix->Set(pTextMatrix[0], pTextMatrix[2], pTextMatrix[1], pTextMatrix[3], m_PosX, m_PosY);
225 }
226 void CPDF_TextObject::SetSegments(const CFX_ByteString* pStrs, FX_FLOAT* pKerning, int nsegs)
227 {
228     if (m_nChars > 1 && m_pCharCodes) {
229         FX_Free(m_pCharCodes);
230         m_pCharCodes = NULL;
231     }
232     if (m_pCharPos) {
233         FX_Free(m_pCharPos);
234         m_pCharPos = NULL;
235     }
236     CPDF_Font* pFont = m_TextState.GetFont();
237     m_nChars = 0;
238     for (int i = 0; i < nsegs; i ++) {
239         m_nChars += pFont->CountChar(pStrs[i], pStrs[i].GetLength());
240     }
241     m_nChars += nsegs - 1;
242     if (m_nChars > 1) {
243         m_pCharCodes = FX_Alloc(FX_DWORD, m_nChars);
244         m_pCharPos = FX_Alloc(FX_FLOAT, m_nChars - 1);
245         int index = 0;
246         for (int i = 0; i < nsegs; i ++) {
247             FX_LPCSTR segment = pStrs[i];
248             int offset = 0, len = pStrs[i].GetLength();
249             while (offset < len) {
250                 m_pCharCodes[index++] = pFont->GetNextChar(segment, offset);
251             }
252             if (i != nsegs - 1) {
253                 m_pCharPos[index - 1] = pKerning[i];
254                 m_pCharCodes[index ++] = (FX_DWORD) - 1;
255             }
256         }
257     } else {
258         int offset = 0;
259         m_pCharCodes = (FX_DWORD*)(FX_UINTPTR)pFont->GetNextChar(pStrs[0], offset);
260     }
261 }
262 void CPDF_TextObject::SetText(const CFX_ByteString& str)
263 {
264     SetSegments(&str, NULL, 1);
265     RecalcPositionData();
266 }
267 void CPDF_TextObject::SetEmpty()
268 {
269     if (m_nChars > 1 && m_pCharCodes) {
270         FX_Free(m_pCharCodes);
271     }
272     if (m_nChars > 1 && m_pCharPos) {
273         FX_Free(m_pCharPos);
274     }
275     m_nChars = 0;
276     m_pCharCodes = NULL;
277     m_pCharPos = NULL;
278     m_Left = m_Right = m_PosX;
279     m_Top = m_Bottom = m_PosY;
280 }
281 void CPDF_TextObject::SetText(CFX_ByteString* pStrs, FX_FLOAT* pKerning, int nSegs)
282 {
283     SetSegments(pStrs, pKerning, nSegs);
284     RecalcPositionData();
285 }
286 void CPDF_TextObject::SetText(int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pKernings)
287 {
288     if (m_nChars > 1 && m_pCharCodes) {
289         FX_Free(m_pCharCodes);
290         m_pCharCodes = NULL;
291     }
292     if (m_pCharPos) {
293         FX_Free(m_pCharPos);
294         m_pCharPos = NULL;
295     }
296     int nKernings = 0;
297     int i;
298     for (i = 0; i < nChars - 1; i ++)
299         if (pKernings[i] != 0) {
300             nKernings ++;
301         }
302     m_nChars = nChars + nKernings;
303     if (m_nChars > 1) {
304         m_pCharCodes = FX_Alloc(FX_DWORD, m_nChars);
305         m_pCharPos = FX_Alloc(FX_FLOAT, m_nChars - 1);
306         int index = 0;
307         for (int i = 0; i < nChars; i ++) {
308             m_pCharCodes[index++] = pCharCodes[i];
309             if (pKernings[i] != 0 && i != nChars - 1) {
310                 m_pCharCodes[index] = (FX_DWORD) - 1;
311                 m_pCharPos[index - 1] = pKernings[i];
312                 index ++;
313             }
314         }
315     } else {
316         m_pCharCodes = (FX_DWORD*)(FX_UINTPTR)pCharCodes[0];
317     }
318     RecalcPositionData();
319 }
320 FX_FLOAT CPDF_TextObject::GetCharWidth(FX_DWORD charcode) const
321 {
322     FX_FLOAT fontsize = m_TextState.GetFontSize() / 1000;
323     CPDF_Font* pFont = m_TextState.GetFont();
324     FX_BOOL bVertWriting = FALSE;
325     CPDF_CIDFont* pCIDFont = pFont->GetCIDFont();
326     if (pCIDFont) {
327         bVertWriting = pCIDFont->IsVertWriting();
328     }
329     if (!bVertWriting) {
330         return pFont->GetCharWidthF(charcode, 0) * fontsize;
331     } else {
332         FX_WORD CID = pCIDFont->CIDFromCharCode(charcode);
333         return pCIDFont->GetVertWidth(CID) * fontsize;
334     }
335 }
336 FX_FLOAT CPDF_TextObject::GetSpaceCharWidth() const
337 {
338     CPDF_Font* pFont = m_TextState.GetFont();
339     FX_DWORD charCode = m_TextState.GetFont()->CharCodeFromUnicode(32);
340     if (charCode != (FX_DWORD) - 1) {
341         return GetCharWidth(charCode);
342     }
343     FX_FLOAT fontSize = m_TextState.GetFontSize() / 4000.0f;
344     FX_BOOL bVertWriting = FALSE;
345     CPDF_CIDFont* pCIDFont = pFont->GetCIDFont();
346     if (pCIDFont) {
347         bVertWriting = pCIDFont->IsVertWriting();
348     }
349     FX_RECT fontRect;
350     pFont->GetFontBBox(fontRect);
351     fontSize *= bVertWriting ? (FX_FLOAT)fontRect.Height() : (FX_FLOAT)fontRect.Width();
352     return fontSize;
353 }
354 void CPDF_TextObject::GetCharRect(int index, CFX_FloatRect& rect) const
355 {
356     CPDF_Font* pFont = m_TextState.GetFont();
357     FX_BOOL bVertWriting = FALSE;
358     CPDF_CIDFont* pCIDFont = pFont->GetCIDFont();
359     if (pCIDFont) {
360         bVertWriting = pCIDFont->IsVertWriting();
361     }
362     FX_FLOAT fontsize = m_TextState.GetFontSize() / 1000;
363     int count = 0;
364     for (int i = 0; i < m_nChars; i ++) {
365         FX_DWORD charcode = m_nChars == 1 ? (FX_DWORD)(FX_UINTPTR)m_pCharCodes : m_pCharCodes[i];
366         if (charcode == (FX_DWORD) - 1) {
367             continue;
368         }
369         if( count != index) {
370             count++;
371             continue;
372         }
373         FX_FLOAT curpos = i > 0 ? m_pCharPos[i - 1] : 0;
374         FX_RECT char_rect;
375         pFont->GetCharBBox(charcode, char_rect, 0);
376         if (!bVertWriting) {
377             rect.left = curpos + char_rect.left * fontsize;
378             rect.right = curpos + char_rect.right * fontsize;
379             rect.top = char_rect.top * fontsize;
380             rect.bottom = char_rect.bottom * fontsize;
381         } else {
382             FX_WORD CID = pCIDFont->CIDFromCharCode(charcode);
383             short vx, vy;
384             pCIDFont->GetVertOrigin(CID, vx, vy);
385             char_rect.left -= vx;
386             char_rect.right -= vx;
387             char_rect.top -= vy;
388             char_rect.bottom -= vy;
389             rect.left = char_rect.left * fontsize;
390             rect.right = char_rect.right * fontsize;
391             rect.top = curpos + char_rect.top * fontsize;
392             rect.bottom = curpos + char_rect.bottom * fontsize;
393         }
394         return;
395     }
396 }
397 void CPDF_TextObject::CalcPositionData(FX_FLOAT* pTextAdvanceX, FX_FLOAT* pTextAdvanceY, FX_FLOAT horz_scale, int level)
398 {
399     FX_FLOAT curpos = 0;
400     FX_FLOAT min_x = 10000 * 1.0f, max_x = -10000 * 1.0f, min_y = 10000 * 1.0f, max_y = -10000 * 1.0f;
401     CPDF_Font* pFont = m_TextState.GetFont();
402     FX_BOOL bVertWriting = FALSE;
403     CPDF_CIDFont* pCIDFont = pFont->GetCIDFont();
404     if (pCIDFont) {
405         bVertWriting = pCIDFont->IsVertWriting();
406     }
407     FX_FLOAT fontsize = m_TextState.GetFontSize();
408     for (int i = 0; i < m_nChars; i ++) {
409         FX_DWORD charcode = m_nChars == 1 ? (FX_DWORD)(FX_UINTPTR)m_pCharCodes : m_pCharCodes[i];
410         if (charcode == (FX_DWORD) - 1) {
411             curpos -= FXSYS_Mul(m_pCharPos[i - 1], fontsize) / 1000;
412             continue;
413         }
414         if (i) {
415             m_pCharPos[i - 1] = curpos;
416         }
417         FX_RECT char_rect;
418         pFont->GetCharBBox(charcode, char_rect, level);
419         FX_FLOAT charwidth;
420         if (!bVertWriting) {
421             if (min_y > char_rect.top) {
422                 min_y = (FX_FLOAT)char_rect.top;
423             }
424             if (max_y < char_rect.top) {
425                 max_y = (FX_FLOAT)char_rect.top;
426             }
427             if (min_y > char_rect.bottom) {
428                 min_y = (FX_FLOAT)char_rect.bottom;
429             }
430             if (max_y < char_rect.bottom) {
431                 max_y = (FX_FLOAT)char_rect.bottom;
432             }
433             FX_FLOAT char_left = curpos + char_rect.left * fontsize / 1000;
434             FX_FLOAT char_right = curpos + char_rect.right * fontsize / 1000;
435             if (min_x > char_left) {
436                 min_x = char_left;
437             }
438             if (max_x < char_left) {
439                 max_x = char_left;
440             }
441             if (min_x > char_right) {
442                 min_x = char_right;
443             }
444             if (max_x < char_right) {
445                 max_x = char_right;
446             }
447             charwidth = pFont->GetCharWidthF(charcode, level) * fontsize / 1000;
448         } else {
449             FX_WORD CID = pCIDFont->CIDFromCharCode(charcode);
450             short vx, vy;
451             pCIDFont->GetVertOrigin(CID, vx, vy);
452             char_rect.left -= vx;
453             char_rect.right -= vx;
454             char_rect.top -= vy;
455             char_rect.bottom -= vy;
456             if (min_x > char_rect.left) {
457                 min_x = (FX_FLOAT)char_rect.left;
458             }
459             if (max_x < char_rect.left) {
460                 max_x = (FX_FLOAT)char_rect.left;
461             }
462             if (min_x > char_rect.right) {
463                 min_x = (FX_FLOAT)char_rect.right;
464             }
465             if (max_x < char_rect.right) {
466                 max_x = (FX_FLOAT)char_rect.right;
467             }
468             FX_FLOAT char_top = curpos + char_rect.top * fontsize / 1000;
469             FX_FLOAT char_bottom = curpos + char_rect.bottom * fontsize / 1000;
470             if (min_y > char_top) {
471                 min_y = char_top;
472             }
473             if (max_y < char_top) {
474                 max_y = char_top;
475             }
476             if (min_y > char_bottom) {
477                 min_y = char_bottom;
478             }
479             if (max_y < char_bottom) {
480                 max_y = char_bottom;
481             }
482             charwidth = pCIDFont->GetVertWidth(CID) * fontsize / 1000;
483         }
484         curpos += charwidth;
485         if (charcode == ' ' && (pCIDFont == NULL || pCIDFont->GetCharSize(32) == 1)) {
486             curpos += m_TextState.GetObject()->m_WordSpace;
487         }
488         curpos += m_TextState.GetObject()->m_CharSpace;
489     }
490     if (bVertWriting) {
491         if (pTextAdvanceX) {
492             *pTextAdvanceX = 0;
493         }
494         if (pTextAdvanceY) {
495             *pTextAdvanceY = curpos;
496         }
497         min_x = min_x * fontsize / 1000;
498         max_x = max_x * fontsize / 1000;
499     } else {
500         if (pTextAdvanceX) {
501             *pTextAdvanceX = FXSYS_Mul(curpos, horz_scale);
502         }
503         if (pTextAdvanceY) {
504             *pTextAdvanceY = 0;
505         }
506         min_y = min_y * fontsize / 1000;
507         max_y = max_y * fontsize / 1000;
508     }
509     CFX_AffineMatrix matrix;
510     GetTextMatrix(&matrix);
511     m_Left = min_x;
512     m_Right = max_x;
513     m_Bottom = min_y;
514     m_Top = max_y;
515     matrix.TransformRect(m_Left, m_Right, m_Top, m_Bottom);
516     int textmode = m_TextState.GetObject()->m_TextMode;
517     if (textmode == 1 || textmode == 2 || textmode == 5 || textmode == 6) {
518         FX_FLOAT half_width = m_GraphState.GetObject()->m_LineWidth / 2;
519         m_Left -= half_width;
520         m_Right += half_width;
521         m_Top += half_width;
522         m_Bottom -= half_width;
523     }
524 }
525 void CPDF_TextObject::CalcCharPos(FX_FLOAT* pPosArray) const
526 {
527     CPDF_Font* pFont = m_TextState.GetFont();
528     FX_BOOL bVertWriting = FALSE;
529     CPDF_CIDFont* pCIDFont = pFont->GetCIDFont();
530     if (pCIDFont) {
531         bVertWriting = pCIDFont->IsVertWriting();
532     }
533     FX_FLOAT fontsize = m_TextState.GetFontSize();
534     int index = 0;
535     for (int i = 0; i < m_nChars; i ++) {
536         FX_DWORD charcode = m_nChars == 1 ? (FX_DWORD)(FX_UINTPTR)m_pCharCodes : m_pCharCodes[i];
537         if (charcode == (FX_DWORD) - 1) {
538             continue;
539         }
540         pPosArray[index++] = i ? m_pCharPos[i - 1] : 0;
541         FX_FLOAT charwidth;
542         if (bVertWriting) {
543             FX_WORD CID = pCIDFont->CIDFromCharCode(charcode);
544             charwidth = pCIDFont->GetVertWidth(CID) * fontsize / 1000;
545         } else {
546             charwidth = pFont->GetCharWidthF(charcode) * fontsize / 1000;
547         }
548         pPosArray[index] = pPosArray[index - 1] + charwidth;
549         index++;
550     }
551 }
552 void CPDF_TextObject::Transform(const CFX_AffineMatrix& matrix)
553 {
554     m_TextState.GetModify();
555     CFX_AffineMatrix text_matrix;
556     GetTextMatrix(&text_matrix);
557     text_matrix.Concat(matrix);
558     FX_FLOAT* pTextMatrix = m_TextState.GetMatrix();
559     pTextMatrix[0] = text_matrix.GetA();
560     pTextMatrix[1] = text_matrix.GetC();
561     pTextMatrix[2] = text_matrix.GetB();
562     pTextMatrix[3] = text_matrix.GetD();
563     m_PosX = text_matrix.GetE();
564     m_PosY = text_matrix.GetF();
565     CalcPositionData(NULL, NULL, 0);
566 }
567 void CPDF_TextObject::SetPosition(FX_FLOAT x, FX_FLOAT y)
568 {
569     FX_FLOAT dx = x - m_PosX;
570     FX_FLOAT dy = y - m_PosY;
571     m_PosX = x;
572     m_PosY = y;
573     m_Left += dx;
574     m_Right += dx;
575     m_Top += dy;
576     m_Bottom += dy;
577 }
578 void CPDF_TextObject::SetData(int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos, FX_FLOAT x, FX_FLOAT y)
579 {
580     ASSERT(m_nChars == 0);
581     m_nChars = nChars;
582     m_PosX = x;
583     m_PosY = y;
584     if (nChars == 0) {
585         return;
586     }
587     if (nChars == 1) {
588         m_pCharCodes = (FX_DWORD*)(FX_UINTPTR) * pCharCodes;
589     } else {
590         m_pCharCodes = FX_Alloc(FX_DWORD, nChars);
591         FXSYS_memcpy32(m_pCharCodes, pCharCodes, sizeof(FX_DWORD)*nChars);
592         m_pCharPos = FX_Alloc(FX_FLOAT, nChars - 1);
593         FXSYS_memcpy32(m_pCharPos, pCharPos, sizeof(FX_FLOAT) * (nChars - 1));
594     }
595     RecalcPositionData();
596 }
597 void CPDF_TextObject::SetTextState(CPDF_TextState TextState)
598 {
599     m_TextState = TextState;
600     CalcPositionData(NULL, NULL, 0);
601 }
602 CPDF_ShadingObject::CPDF_ShadingObject()
603 {
604     m_pShading = NULL;
605     m_Type = PDFPAGE_SHADING;
606 }
607 CPDF_ShadingObject::~CPDF_ShadingObject()
608 {
609     CPDF_ShadingPattern* pShading = m_pShading;
610     if (pShading && pShading->m_pDocument) {
611         pShading->m_pDocument->GetPageData()->ReleasePattern(pShading->m_pShadingObj);
612     }
613 }
614 void CPDF_ShadingObject::CopyData(const CPDF_PageObject* pSrc)
615 {
616     CPDF_ShadingObject* pSrcObj = (CPDF_ShadingObject*)pSrc;
617     m_pShading = pSrcObj->m_pShading;
618     if (m_pShading && m_pShading->m_pDocument) {
619         CPDF_DocPageData* pDocPageData = m_pShading->m_pDocument->GetPageData();
620         m_pShading = (CPDF_ShadingPattern*)pDocPageData->GetPattern(m_pShading->m_pShadingObj, m_pShading->m_bShadingObj, &m_pShading->m_ParentMatrix);
621     }
622     m_Matrix = pSrcObj->m_Matrix;
623 }
624 void CPDF_ShadingObject::Transform(const CFX_AffineMatrix& matrix)
625 {
626     if (!m_ClipPath.IsNull()) {
627         m_ClipPath.GetModify();
628         m_ClipPath.Transform(matrix);
629     }
630     m_Matrix.Concat(matrix);
631     if (!m_ClipPath.IsNull()) {
632         CalcBoundingBox();
633     } else {
634         matrix.TransformRect(m_Left, m_Right, m_Top, m_Bottom);
635     }
636 }
637 void CPDF_ShadingObject::CalcBoundingBox()
638 {
639     if (m_ClipPath.IsNull()) {
640         return;
641     }
642     CFX_FloatRect rect = m_ClipPath.GetClipBox();
643     m_Left = rect.left;
644     m_Bottom = rect.bottom;
645     m_Right = rect.right;
646     m_Top = rect.top;
647 }
648 CPDF_FormObject::~CPDF_FormObject()
649 {
650     if (m_pForm) {
651         delete m_pForm;
652     }
653 }
654 void CPDF_FormObject::Transform(const CFX_AffineMatrix& matrix)
655 {
656     m_FormMatrix.Concat(matrix);
657     CalcBoundingBox();
658 }
659 void CPDF_FormObject::CopyData(const CPDF_PageObject* pSrc)
660 {
661     const CPDF_FormObject* pSrcObj = (const CPDF_FormObject*)pSrc;
662     if (m_pForm) {
663         delete m_pForm;
664     }
665     m_pForm = pSrcObj->m_pForm->Clone();
666     m_FormMatrix = pSrcObj->m_FormMatrix;
667 }
668 void CPDF_FormObject::CalcBoundingBox()
669 {
670     CFX_FloatRect form_rect = m_pForm->CalcBoundingBox();
671     form_rect.Transform(&m_FormMatrix);
672     m_Left = form_rect.left;
673     m_Bottom = form_rect.bottom;
674     m_Right = form_rect.right;
675     m_Top = form_rect.top;
676 }
677 CPDF_PageObjects::CPDF_PageObjects(FX_BOOL bReleaseMembers) : m_ObjectList(128)
678 {
679     m_bBackgroundAlphaNeeded = FALSE;
680     m_bReleaseMembers = bReleaseMembers;
681     m_ParseState = PDF_CONTENT_NOT_PARSED;
682     m_pParser = NULL;
683     m_pFormStream = NULL;
684     m_pResources = NULL;
685 }
686 CPDF_PageObjects::~CPDF_PageObjects()
687 {
688     if (m_pParser) {
689         delete m_pParser;
690     }
691     if (!m_bReleaseMembers) {
692         return;
693     }
694     FX_POSITION pos = m_ObjectList.GetHeadPosition();
695     while (pos) {
696         CPDF_PageObject* pPageObj = (CPDF_PageObject*)m_ObjectList.GetNext(pos);
697         if (!pPageObj) {
698             continue;
699         }
700         pPageObj->Release();
701     }
702 }
703 void CPDF_PageObjects::ContinueParse(IFX_Pause* pPause)
704 {
705     if (m_pParser == NULL) {
706         return;
707     }
708     m_pParser->Continue(pPause);
709     if (m_pParser->GetStatus() == CPDF_ContentParser::Done) {
710         m_ParseState = PDF_CONTENT_PARSED;
711         delete m_pParser;
712         m_pParser = NULL;
713     }
714 }
715 int CPDF_PageObjects::EstimateParseProgress() const
716 {
717     if (m_pParser == NULL) {
718         return m_ParseState == PDF_CONTENT_PARSED ? 100 : 0;
719     }
720     return m_pParser->EstimateProgress();
721 }
722 FX_POSITION CPDF_PageObjects::InsertObject(FX_POSITION posInsertAfter, CPDF_PageObject* pNewObject)
723 {
724     if (posInsertAfter == NULL) {
725         return m_ObjectList.AddHead(pNewObject);
726     } else {
727         return m_ObjectList.InsertAfter(posInsertAfter, pNewObject);
728     }
729 }
730 int CPDF_PageObjects::GetObjectIndex(CPDF_PageObject* pObj) const
731 {
732     int index = 0;
733     FX_POSITION pos = m_ObjectList.GetHeadPosition();
734     while (pos) {
735         CPDF_PageObject* pThisObj = (CPDF_PageObject*)m_ObjectList.GetNext(pos);
736         if (pThisObj == pObj) {
737             return index;
738         }
739         index ++;
740     }
741     return -1;
742 }
743 CPDF_PageObject* CPDF_PageObjects::GetObjectByIndex(int index) const
744 {
745     FX_POSITION pos = m_ObjectList.FindIndex(index);
746     if (pos == NULL) {
747         return NULL;
748     }
749     return (CPDF_PageObject*)m_ObjectList.GetAt(pos);
750 }
751 void CPDF_PageObjects::Transform(const CFX_AffineMatrix& matrix)
752 {
753     FX_POSITION pos = m_ObjectList.GetHeadPosition();
754     while (pos) {
755         CPDF_PageObject* pObj = (CPDF_PageObject*)m_ObjectList.GetNext(pos);
756         pObj->Transform(matrix);
757     }
758 }
759 CFX_FloatRect CPDF_PageObjects::CalcBoundingBox() const
760 {
761     if (m_ObjectList.GetCount() == 0) {
762         return CFX_FloatRect(0, 0, 0, 0);
763     }
764     FX_FLOAT left, right, top, bottom;
765     left = bottom = 1000000 * 1.0f;
766     right = top = -1000000 * 1.0f;
767     FX_POSITION pos = m_ObjectList.GetHeadPosition();
768     while (pos) {
769         CPDF_PageObject* pObj = (CPDF_PageObject*)m_ObjectList.GetNext(pos);
770         if (left > pObj->m_Left) {
771             left = pObj->m_Left;
772         }
773         if (right < pObj->m_Right) {
774             right = pObj->m_Right;
775         }
776         if (top < pObj->m_Top) {
777             top = pObj->m_Top;
778         }
779         if (bottom > pObj->m_Bottom) {
780             bottom = pObj->m_Bottom;
781         }
782     }
783     return CFX_FloatRect(left, bottom, right, top);
784 }
785 void CPDF_PageObjects::LoadTransInfo()
786 {
787     if (m_pFormDict == NULL) {
788         return;
789     }
790     CPDF_Dictionary* pGroup = m_pFormDict->GetDict(FX_BSTRC("Group"));
791     if (pGroup == NULL) {
792         return;
793     }
794     if (pGroup->GetString(FX_BSTRC("S")) != FX_BSTRC("Transparency")) {
795         return;
796     }
797     m_Transparency |= PDFTRANS_GROUP;
798     if (pGroup->GetInteger(FX_BSTRC("I"))) {
799         m_Transparency |= PDFTRANS_ISOLATED;
800     }
801     if (pGroup->GetInteger(FX_BSTRC("K"))) {
802         m_Transparency |= PDFTRANS_KNOCKOUT;
803     }
804 }
805 void CPDF_PageObjects::ClearCacheObjects()
806 {
807     m_ParseState = PDF_CONTENT_NOT_PARSED;
808     if (m_pParser) {
809         delete m_pParser;
810     }
811     m_pParser = NULL;
812     if (m_bReleaseMembers) {
813         FX_POSITION pos = m_ObjectList.GetHeadPosition();
814         while (pos) {
815             CPDF_PageObject* pPageObj = (CPDF_PageObject*)m_ObjectList.GetNext(pos);
816             if (!pPageObj) {
817                 continue;
818             }
819             pPageObj->Release();
820         }
821     }
822     m_ObjectList.RemoveAll();
823 }
824 CPDF_Page::CPDF_Page()
825 {
826     m_pPageRender = NULL;
827 }
828 void CPDF_Page::Load(CPDF_Document* pDocument, CPDF_Dictionary* pPageDict, FX_BOOL bPageCache)
829 {
830     m_pDocument = (CPDF_Document*)pDocument;
831     m_pFormDict = pPageDict;
832     if (bPageCache) {
833         m_pPageRender = CPDF_ModuleMgr::Get()->GetRenderModule()->CreatePageCache(this);
834     }
835     if (pPageDict == NULL) {
836         m_PageWidth = m_PageHeight = 100 * 1.0f;
837         m_pPageResources = m_pResources = NULL;
838         return;
839     }
840     CPDF_Object* pageAttr = GetPageAttr(FX_BSTRC("Resources"));
841     m_pResources = pageAttr ? pageAttr->GetDict() : NULL;
842     m_pPageResources = m_pResources;
843     CPDF_Object* pRotate = GetPageAttr(FX_BSTRC("Rotate"));
844     int rotate = 0;
845     if (pRotate) {
846         rotate = pRotate->GetInteger() / 90 % 4;
847     }
848     if (rotate < 0) {
849         rotate += 4;
850     }
851     CPDF_Array* pMediaBox, *pCropBox;
852     pMediaBox = (CPDF_Array*)GetPageAttr(FX_BSTRC("MediaBox"));
853     CFX_FloatRect mediabox;
854     if (pMediaBox) {
855         mediabox = pMediaBox->GetRect();
856         mediabox.Normalize();
857     }
858     if (mediabox.IsEmpty()) {
859         mediabox = CFX_FloatRect(0, 0, 612, 792);
860     }
861     pCropBox = (CPDF_Array*)GetPageAttr(FX_BSTRC("CropBox"));
862     if (pCropBox) {
863         m_BBox = pCropBox->GetRect();
864         m_BBox.Normalize();
865     }
866     if (m_BBox.IsEmpty()) {
867         m_BBox = mediabox;
868     } else {
869         m_BBox.Intersect(mediabox);
870     }
871     if (rotate % 2) {
872         m_PageHeight = m_BBox.right - m_BBox.left;
873         m_PageWidth = m_BBox.top - m_BBox.bottom;
874     } else {
875         m_PageWidth = m_BBox.right - m_BBox.left;
876         m_PageHeight = m_BBox.top - m_BBox.bottom;
877     }
878     switch (rotate) {
879         case 0:
880             m_PageMatrix.Set(1.0f, 0, 0, 1.0f, -m_BBox.left, -m_BBox.bottom);
881             break;
882         case 1:
883             m_PageMatrix.Set(0, -1.0f, 1.0f, 0, -m_BBox.bottom, m_BBox.right);
884             break;
885         case 2:
886             m_PageMatrix.Set(-1.0f, 0, 0, -1.0f, m_BBox.right, m_BBox.top);
887             break;
888         case 3:
889             m_PageMatrix.Set(0, 1.0f, -1.0f, 0, m_BBox.top, -m_BBox.left);
890             break;
891     }
892     m_Transparency = PDFTRANS_ISOLATED;
893     LoadTransInfo();
894 }
895 void CPDF_Page::StartParse(CPDF_ParseOptions* pOptions, FX_BOOL bReParse)
896 {
897     if (bReParse) {
898         ClearCacheObjects();
899     }
900     if (m_ParseState == PDF_CONTENT_PARSED || m_ParseState == PDF_CONTENT_PARSING) {
901         return;
902     }
903     m_pParser = FX_NEW CPDF_ContentParser;
904     m_pParser->Start(this, pOptions);
905     m_ParseState = PDF_CONTENT_PARSING;
906 }
907 void CPDF_Page::ParseContent(CPDF_ParseOptions* pOptions, FX_BOOL bReParse)
908 {
909     StartParse(pOptions, bReParse);
910     ContinueParse(NULL);
911 }
912 CPDF_Page::~CPDF_Page()
913 {
914     if (m_pPageRender) {
915         CPDF_RenderModuleDef* pModule = CPDF_ModuleMgr::Get()->GetRenderModule();
916         pModule->DestroyPageCache(m_pPageRender);
917     }
918 }
919 CPDF_Object* FPDFAPI_GetPageAttr(CPDF_Dictionary* pPageDict, FX_BSTR name)
920 {
921     int level = 0;
922     while (1) {
923         CPDF_Object* pObj = pPageDict->GetElementValue(name);
924         if (pObj) {
925             return pObj;
926         }
927         CPDF_Dictionary* pParent = pPageDict->GetDict(FX_BSTRC("Parent"));
928         if (!pParent || pParent == pPageDict) {
929             return NULL;
930         }
931         pPageDict = pParent;
932         level ++;
933         if (level == 1000) {
934             return NULL;
935         }
936     }
937 }
938 CPDF_Object* CPDF_Page::GetPageAttr(FX_BSTR name) const
939 {
940     return FPDFAPI_GetPageAttr(m_pFormDict, name);
941 }
942 CPDF_Form::CPDF_Form(CPDF_Document* pDoc, CPDF_Dictionary* pPageResources, CPDF_Stream* pFormStream, CPDF_Dictionary* pParentResources)
943 {
944     m_pDocument = pDoc;
945     m_pFormStream = pFormStream;
946     m_pFormDict = pFormStream ? pFormStream->GetDict() : NULL;
947     m_pResources = m_pFormDict->GetDict(FX_BSTRC("Resources"));
948     m_pPageResources = pPageResources;
949     if (m_pResources == NULL) {
950         m_pResources = pParentResources;
951     }
952     if (m_pResources == NULL) {
953         m_pResources = pPageResources;
954     }
955     m_Transparency = 0;
956     LoadTransInfo();
957 }
958 CPDF_Form::~CPDF_Form()
959 {
960 }
961 void CPDF_Form::StartParse(CPDF_AllStates* pGraphicStates, CFX_AffineMatrix* pParentMatrix,
962                            CPDF_Type3Char* pType3Char, CPDF_ParseOptions* pOptions, int level)
963 {
964     if (m_ParseState == PDF_CONTENT_PARSED || m_ParseState == PDF_CONTENT_PARSING) {
965         return;
966     }
967     m_pParser = FX_NEW CPDF_ContentParser;
968     m_pParser->Start(this, pGraphicStates, pParentMatrix, pType3Char, pOptions, level);
969     m_ParseState = PDF_CONTENT_PARSING;
970 }
971 void CPDF_Form::ParseContent(CPDF_AllStates* pGraphicStates, CFX_AffineMatrix* pParentMatrix,
972                              CPDF_Type3Char* pType3Char, CPDF_ParseOptions* pOptions, int level)
973 {
974     StartParse(pGraphicStates, pParentMatrix, pType3Char, pOptions, level);
975     ContinueParse(NULL);
976 }
977 CPDF_Form* CPDF_Form::Clone() const
978 {
979     CPDF_Form* pClone = FX_NEW CPDF_Form(m_pDocument, m_pPageResources, m_pFormStream, m_pResources);
980     FX_POSITION pos = m_ObjectList.GetHeadPosition();
981     while (pos) {
982         CPDF_PageObject* pObj = (CPDF_PageObject*)m_ObjectList.GetNext(pos);
983         pClone->m_ObjectList.AddTail(pObj->Clone());
984     }
985     return pClone;
986 }
987 void CPDF_Page::GetDisplayMatrix(CFX_AffineMatrix& matrix, int xPos, int yPos,
988                                  int xSize, int ySize, int iRotate) const
989 {
990     if (m_PageWidth == 0 || m_PageHeight == 0) {
991         return;
992     }
993     CFX_AffineMatrix display_matrix;
994     int x0, y0, x1, y1, x2, y2;
995     iRotate %= 4;
996     switch (iRotate) {
997         case 0:
998             x0 = xPos;
999             y0 = yPos + ySize;
1000             x1 = xPos;
1001             y1 = yPos;
1002             x2 = xPos + xSize;
1003             y2 = yPos + ySize;
1004             break;
1005         case 1:
1006             x0 = xPos;
1007             y0 = yPos;
1008             x1 = xPos + xSize;
1009             y1 = yPos;
1010             x2 = xPos;
1011             y2 = yPos + ySize;
1012             break;
1013         case 2:
1014             x0 = xPos + xSize;
1015             y0 = yPos;
1016             x1 = xPos + xSize;
1017             y1 = yPos + ySize;
1018             x2 = xPos;
1019             y2 = yPos;
1020             break;
1021         case 3:
1022             x0 = xPos + xSize;
1023             y0 = yPos + ySize;
1024             x1 = xPos;
1025             y1 = yPos + ySize;
1026             x2 = xPos + xSize;
1027             y2 = yPos;
1028             break;
1029     }
1030     display_matrix.Set(FXSYS_Div((FX_FLOAT)(x2 - x0), m_PageWidth),
1031                        FXSYS_Div((FX_FLOAT)(y2 - y0), m_PageWidth),
1032                        FXSYS_Div((FX_FLOAT)(x1 - x0), m_PageHeight),
1033                        FXSYS_Div((FX_FLOAT)(y1 - y0), m_PageHeight),
1034                        (FX_FLOAT)x0, (FX_FLOAT)y0);
1035     matrix = m_PageMatrix;
1036     matrix.Concat(display_matrix);
1037 }
1038 CPDF_ParseOptions::CPDF_ParseOptions()
1039 {
1040     m_bTextOnly = FALSE;
1041     m_bMarkedContent = TRUE;
1042     m_bSeparateForm = TRUE;
1043     m_bDecodeInlineImage = FALSE;
1044 }