• 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_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 uint8_t 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,uint8_t type,bool bAutoMerge)77 void CPDF_ClipPath::AppendPath(CPDF_Path path, uint8_t type, bool bAutoMerge) {
78   PathData* pData = m_Ref.GetPrivateCopy();
79   if (!pData->m_PathAndTypeList.empty() && bAutoMerge) {
80     const CPDF_Path& old_path = pData->m_PathAndTypeList.back().first;
81     if (old_path.IsRect()) {
82       CFX_PointF point0 = old_path.GetPoint(0);
83       CFX_PointF point2 = old_path.GetPoint(2);
84       CFX_FloatRect old_rect(point0.x, point0.y, point2.x, point2.y);
85       CFX_FloatRect new_rect = path.GetBoundingBox();
86       if (old_rect.Contains(new_rect))
87         pData->m_PathAndTypeList.pop_back();
88     }
89   }
90   pData->m_PathAndTypeList.push_back(std::make_pair(path, type));
91 }
92 
AppendTexts(std::vector<std::unique_ptr<CPDF_TextObject>> * pTexts)93 void CPDF_ClipPath::AppendTexts(
94     std::vector<std::unique_ptr<CPDF_TextObject>>* pTexts) {
95   constexpr size_t kMaxTextObjects = 1024;
96   PathData* pData = m_Ref.GetPrivateCopy();
97   if (pData->m_TextList.size() + pTexts->size() <= kMaxTextObjects) {
98     for (size_t i = 0; i < pTexts->size(); i++)
99       pData->m_TextList.push_back(std::move((*pTexts)[i]));
100     pData->m_TextList.push_back(nullptr);
101   }
102   pTexts->clear();
103 }
104 
CopyClipPath(const CPDF_ClipPath & that)105 void CPDF_ClipPath::CopyClipPath(const CPDF_ClipPath& that) {
106   if (*this == that || !that.HasRef())
107     return;
108 
109   for (size_t i = 0; i < that.GetPathCount(); ++i)
110     AppendPath(that.GetPath(i), that.GetClipType(i), /*bAutoMerge=*/false);
111 }
112 
Transform(const CFX_Matrix & matrix)113 void CPDF_ClipPath::Transform(const CFX_Matrix& matrix) {
114   PathData* pData = m_Ref.GetPrivateCopy();
115   for (auto& obj : pData->m_PathAndTypeList)
116     obj.first.Transform(matrix);
117 
118   for (auto& text : pData->m_TextList) {
119     if (text)
120       text->Transform(matrix);
121   }
122 }
123 
124 CPDF_ClipPath::PathData::PathData() = default;
125 
PathData(const PathData & that)126 CPDF_ClipPath::PathData::PathData(const PathData& that) {
127   m_PathAndTypeList = that.m_PathAndTypeList;
128 
129   m_TextList.resize(that.m_TextList.size());
130   for (size_t i = 0; i < that.m_TextList.size(); ++i) {
131     if (that.m_TextList[i])
132       m_TextList[i] = that.m_TextList[i]->Clone();
133   }
134 }
135 
136 CPDF_ClipPath::PathData::~PathData() = default;
137 
Clone() const138 RetainPtr<CPDF_ClipPath::PathData> CPDF_ClipPath::PathData::Clone() const {
139   return pdfium::MakeRetain<CPDF_ClipPath::PathData>(*this);
140 }
141