• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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_clippath.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/page/cpdf_textobject.h"
12 
13 CPDF_ClipPath::CPDF_ClipPath() = default;
14 
15 CPDF_ClipPath::CPDF_ClipPath(const CPDF_ClipPath& that) = default;
16 
17 CPDF_ClipPath& CPDF_ClipPath::operator=(const CPDF_ClipPath& that) = default;
18 
19 CPDF_ClipPath::~CPDF_ClipPath() = default;
20 
GetPathCount() const21 size_t CPDF_ClipPath::GetPathCount() const {
22   return m_Ref.GetObject()->m_PathAndTypeList.size();
23 }
24 
GetPath(size_t i) const25 CPDF_Path CPDF_ClipPath::GetPath(size_t i) const {
26   return m_Ref.GetObject()->m_PathAndTypeList[i].first;
27 }
28 
GetClipType(size_t i) const29 CFX_FillRenderOptions::FillType CPDF_ClipPath::GetClipType(size_t i) const {
30   return m_Ref.GetObject()->m_PathAndTypeList[i].second;
31 }
32 
GetTextCount() const33 size_t CPDF_ClipPath::GetTextCount() const {
34   return m_Ref.GetObject()->m_TextList.size();
35 }
36 
GetText(size_t i) const37 CPDF_TextObject* CPDF_ClipPath::GetText(size_t i) const {
38   return m_Ref.GetObject()->m_TextList[i].get();
39 }
40 
GetClipBox() const41 CFX_FloatRect CPDF_ClipPath::GetClipBox() const {
42   CFX_FloatRect rect;
43   bool bStarted = false;
44   if (GetPathCount() > 0) {
45     rect = GetPath(0).GetBoundingBox();
46     for (size_t i = 1; i < GetPathCount(); ++i) {
47       CFX_FloatRect path_rect = GetPath(i).GetBoundingBox();
48       rect.Intersect(path_rect);
49     }
50     bStarted = true;
51   }
52 
53   CFX_FloatRect layer_rect;
54   bool bLayerStarted = false;
55   for (size_t i = 0; i < GetTextCount(); ++i) {
56     CPDF_TextObject* pTextObj = GetText(i);
57     if (pTextObj) {
58       if (bLayerStarted) {
59         layer_rect.Union(CFX_FloatRect(pTextObj->GetBBox()));
60       } else {
61         layer_rect = CFX_FloatRect(pTextObj->GetBBox());
62         bLayerStarted = true;
63       }
64     } else {
65       if (bStarted) {
66         rect.Intersect(layer_rect);
67       } else {
68         rect = layer_rect;
69         bStarted = true;
70       }
71       bLayerStarted = false;
72     }
73   }
74   return rect;
75 }
76 
AppendPath(CPDF_Path path,CFX_FillRenderOptions::FillType type)77 void CPDF_ClipPath::AppendPath(CPDF_Path path,
78                                CFX_FillRenderOptions::FillType type) {
79   PathData* pData = m_Ref.GetPrivateCopy();
80   pData->m_PathAndTypeList.emplace_back(path, type);
81 }
82 
AppendPathWithAutoMerge(CPDF_Path path,CFX_FillRenderOptions::FillType type)83 void CPDF_ClipPath::AppendPathWithAutoMerge(
84     CPDF_Path path,
85     CFX_FillRenderOptions::FillType type) {
86   PathData* pData = m_Ref.GetPrivateCopy();
87   if (!pData->m_PathAndTypeList.empty()) {
88     const CPDF_Path& old_path = pData->m_PathAndTypeList.back().first;
89     if (old_path.IsRect()) {
90       CFX_PointF point0 = old_path.GetPoint(0);
91       CFX_PointF point2 = old_path.GetPoint(2);
92       CFX_FloatRect old_rect(point0.x, point0.y, point2.x, point2.y);
93       CFX_FloatRect new_rect = path.GetBoundingBox();
94       if (old_rect.Contains(new_rect))
95         pData->m_PathAndTypeList.pop_back();
96     }
97   }
98   AppendPath(path, type);
99 }
100 
AppendTexts(std::vector<std::unique_ptr<CPDF_TextObject>> * pTexts)101 void CPDF_ClipPath::AppendTexts(
102     std::vector<std::unique_ptr<CPDF_TextObject>>* pTexts) {
103   constexpr size_t kMaxTextObjects = 1024;
104   PathData* pData = m_Ref.GetPrivateCopy();
105   if (pData->m_TextList.size() + pTexts->size() <= kMaxTextObjects) {
106     for (size_t i = 0; i < pTexts->size(); i++)
107       pData->m_TextList.push_back(std::move((*pTexts)[i]));
108     pData->m_TextList.push_back(nullptr);
109   }
110   pTexts->clear();
111 }
112 
CopyClipPath(const CPDF_ClipPath & that)113 void CPDF_ClipPath::CopyClipPath(const CPDF_ClipPath& that) {
114   if (*this == that || !that.HasRef())
115     return;
116 
117   for (size_t i = 0; i < that.GetPathCount(); ++i)
118     AppendPath(that.GetPath(i), that.GetClipType(i));
119 }
120 
Transform(const CFX_Matrix & matrix)121 void CPDF_ClipPath::Transform(const CFX_Matrix& matrix) {
122   PathData* pData = m_Ref.GetPrivateCopy();
123   for (auto& obj : pData->m_PathAndTypeList)
124     obj.first.Transform(matrix);
125 
126   for (auto& text : pData->m_TextList) {
127     if (text)
128       text->Transform(matrix);
129   }
130 }
131 
132 CPDF_ClipPath::PathData::PathData() = default;
133 
PathData(const PathData & that)134 CPDF_ClipPath::PathData::PathData(const PathData& that)
135     : m_PathAndTypeList(that.m_PathAndTypeList),
136       m_TextList(that.m_TextList.size()) {
137   for (size_t i = 0; i < that.m_TextList.size(); ++i) {
138     if (that.m_TextList[i])
139       m_TextList[i] = that.m_TextList[i]->Clone();
140   }
141 }
142 
143 CPDF_ClipPath::PathData::~PathData() = default;
144 
Clone() const145 RetainPtr<CPDF_ClipPath::PathData> CPDF_ClipPath::PathData::Clone() const {
146   return pdfium::MakeRetain<CPDF_ClipPath::PathData>(*this);
147 }
148