• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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/fpdfapi/page/cpdf_allstates.h"
8 
9 #include <algorithm>
10 #include <utility>
11 #include <vector>
12 
13 #include "core/fpdfapi/font/cpdf_font.h"
14 #include "core/fpdfapi/page/cpdf_pageobjectholder.h"
15 #include "core/fpdfapi/page/cpdf_streamcontentparser.h"
16 #include "core/fpdfapi/parser/cpdf_array.h"
17 #include "core/fpdfapi/parser/cpdf_dictionary.h"
18 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
19 #include "core/fxge/cfx_graphstatedata.h"
20 #include "third_party/base/compiler_specific.h"
21 #include "third_party/base/stl_util.h"
22 
23 CPDF_AllStates::CPDF_AllStates() = default;
24 
25 CPDF_AllStates::~CPDF_AllStates() = default;
26 
Copy(const CPDF_AllStates & src)27 void CPDF_AllStates::Copy(const CPDF_AllStates& src) {
28   CopyStates(src);
29   m_TextMatrix = src.m_TextMatrix;
30   m_ParentMatrix = src.m_ParentMatrix;
31   m_CTM = src.m_CTM;
32   m_TextPos = src.m_TextPos;
33   m_TextLinePos = src.m_TextLinePos;
34   m_TextLeading = src.m_TextLeading;
35   m_TextRise = src.m_TextRise;
36   m_TextHorzScale = src.m_TextHorzScale;
37 }
38 
SetLineDash(const CPDF_Array * pArray,float phase,float scale)39 void CPDF_AllStates::SetLineDash(const CPDF_Array* pArray,
40                                  float phase,
41                                  float scale) {
42   std::vector<float> dashes = ReadArrayElementsToVector(pArray, pArray->size());
43   m_GraphState.SetLineDash(std::move(dashes), phase, scale);
44 }
45 
ProcessExtGS(CPDF_Dictionary * pGS,CPDF_StreamContentParser * pParser)46 void CPDF_AllStates::ProcessExtGS(CPDF_Dictionary* pGS,
47                                   CPDF_StreamContentParser* pParser) {
48   CPDF_DictionaryLocker locker(pGS);
49   for (const auto& it : locker) {
50     const ByteString& key_str = it.first;
51     CPDF_Object* pElement = it.second.Get();
52     CPDF_Object* pObject = pElement ? pElement->GetDirect() : nullptr;
53     if (!pObject)
54       continue;
55 
56     uint32_t key = key_str.GetID();
57     switch (key) {
58       case FXBSTR_ID('L', 'W', 0, 0):
59         m_GraphState.SetLineWidth(pObject->GetNumber());
60         break;
61       case FXBSTR_ID('L', 'C', 0, 0):
62         m_GraphState.SetLineCap(
63             static_cast<CFX_GraphStateData::LineCap>(pObject->GetInteger()));
64         break;
65       case FXBSTR_ID('L', 'J', 0, 0):
66         m_GraphState.SetLineJoin(
67             static_cast<CFX_GraphStateData::LineJoin>(pObject->GetInteger()));
68         break;
69       case FXBSTR_ID('M', 'L', 0, 0):
70         m_GraphState.SetMiterLimit(pObject->GetNumber());
71         break;
72       case FXBSTR_ID('D', 0, 0, 0): {
73         CPDF_Array* pDash = pObject->AsArray();
74         if (!pDash)
75           break;
76 
77         CPDF_Array* pArray = pDash->GetArrayAt(0);
78         if (!pArray)
79           break;
80 
81         SetLineDash(pArray, pDash->GetNumberAt(1), 1.0f);
82         break;
83       }
84       case FXBSTR_ID('R', 'I', 0, 0):
85         m_GeneralState.SetRenderIntent(pObject->GetString());
86         break;
87       case FXBSTR_ID('F', 'o', 'n', 't'): {
88         CPDF_Array* pFont = pObject->AsArray();
89         if (!pFont)
90           break;
91 
92         m_TextState.SetFontSize(pFont->GetNumberAt(1));
93         m_TextState.SetFont(pParser->FindFont(pFont->GetStringAt(0)));
94         break;
95       }
96       case FXBSTR_ID('T', 'R', 0, 0):
97         if (pGS->KeyExist("TR2")) {
98           continue;
99         }
100         FALLTHROUGH;
101       case FXBSTR_ID('T', 'R', '2', 0):
102         m_GeneralState.SetTR(pObject && !pObject->IsName() ? pObject : nullptr);
103         break;
104       case FXBSTR_ID('B', 'M', 0, 0): {
105         CPDF_Array* pArray = pObject->AsArray();
106         m_GeneralState.SetBlendMode(pArray ? pArray->GetStringAt(0)
107                                            : pObject->GetString());
108         if (m_GeneralState.GetBlendType() > BlendMode::kMultiply)
109           pParser->GetPageObjectHolder()->SetBackgroundAlphaNeeded(true);
110         break;
111       }
112       case FXBSTR_ID('S', 'M', 'a', 's'):
113         if (ToDictionary(pObject)) {
114           m_GeneralState.SetSoftMask(pObject);
115           m_GeneralState.SetSMaskMatrix(pParser->GetCurStates()->m_CTM);
116         } else {
117           m_GeneralState.SetSoftMask(nullptr);
118         }
119         break;
120       case FXBSTR_ID('C', 'A', 0, 0):
121         m_GeneralState.SetStrokeAlpha(
122             pdfium::clamp(pObject->GetNumber(), 0.0f, 1.0f));
123         break;
124       case FXBSTR_ID('c', 'a', 0, 0):
125         m_GeneralState.SetFillAlpha(
126             pdfium::clamp(pObject->GetNumber(), 0.0f, 1.0f));
127         break;
128       case FXBSTR_ID('O', 'P', 0, 0):
129         m_GeneralState.SetStrokeOP(!!pObject->GetInteger());
130         if (!pGS->KeyExist("op"))
131           m_GeneralState.SetFillOP(!!pObject->GetInteger());
132         break;
133       case FXBSTR_ID('o', 'p', 0, 0):
134         m_GeneralState.SetFillOP(!!pObject->GetInteger());
135         break;
136       case FXBSTR_ID('O', 'P', 'M', 0):
137         m_GeneralState.SetOPMode(pObject->GetInteger());
138         break;
139       case FXBSTR_ID('B', 'G', 0, 0):
140         if (pGS->KeyExist("BG2")) {
141           continue;
142         }
143         FALLTHROUGH;
144       case FXBSTR_ID('B', 'G', '2', 0):
145         m_GeneralState.SetBG(pObject);
146         break;
147       case FXBSTR_ID('U', 'C', 'R', 0):
148         if (pGS->KeyExist("UCR2")) {
149           continue;
150         }
151         FALLTHROUGH;
152       case FXBSTR_ID('U', 'C', 'R', '2'):
153         m_GeneralState.SetUCR(pObject);
154         break;
155       case FXBSTR_ID('H', 'T', 0, 0):
156         m_GeneralState.SetHT(pObject);
157         break;
158       case FXBSTR_ID('F', 'L', 0, 0):
159         m_GeneralState.SetFlatness(pObject->GetNumber());
160         break;
161       case FXBSTR_ID('S', 'M', 0, 0):
162         m_GeneralState.SetSmoothness(pObject->GetNumber());
163         break;
164       case FXBSTR_ID('S', 'A', 0, 0):
165         m_GeneralState.SetStrokeAdjust(!!pObject->GetInteger());
166         break;
167       case FXBSTR_ID('A', 'I', 'S', 0):
168         m_GeneralState.SetAlphaSource(!!pObject->GetInteger());
169         break;
170       case FXBSTR_ID('T', 'K', 0, 0):
171         m_GeneralState.SetTextKnockout(!!pObject->GetInteger());
172         break;
173     }
174   }
175   m_GeneralState.SetMatrix(m_CTM);
176 }
177