blob: 2fb03063930819d973281d00e165f2497f0c0b96 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2016 The PDFium Authors
Dan Sinclairbf1cf342018-01-03 15:52:41 -05002// 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 "xfa/fxfa/parser/cxfa_stroke.h"
8
Lei Zhanga4e12af2021-08-04 17:43:18 +00009#include <math.h>
10
Dan Sinclairbf1cf342018-01-03 15:52:41 -050011#include <utility>
12
Lei Zhangd859d572018-01-17 20:44:26 +000013#include "fxjs/xfa/cjx_object.h"
Lei Zhanga7157062024-07-03 17:45:38 +000014#include "xfa/fgas/graphics/cfgas_gecolor.h"
Tom Sepez45eae7b2020-10-13 22:50:53 +000015#include "xfa/fgas/graphics/cfgas_gegraphics.h"
Dan Sinclair454ab872018-01-16 21:20:46 +000016#include "xfa/fxfa/cxfa_ffwidget.h"
Dan Sinclairbf1cf342018-01-03 15:52:41 -050017#include "xfa/fxfa/parser/cxfa_color.h"
Tom Sepez757a6bf2020-09-17 17:22:38 +000018#include "xfa/fxfa/parser/cxfa_document.h"
Dan Sinclairbf1cf342018-01-03 15:52:41 -050019#include "xfa/fxfa/parser/cxfa_measurement.h"
20#include "xfa/fxfa/parser/cxfa_node.h"
21#include "xfa/fxfa/parser/xfa_utils.h"
22
Tom Sepez45eae7b2020-10-13 22:50:53 +000023void XFA_StrokeTypeSetLineDash(CFGAS_GEGraphics* pGraphics,
Tom Sepez1498ab82018-12-06 17:56:28 +000024 XFA_AttributeValue iStrokeType,
25 XFA_AttributeValue iCapType) {
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000026 switch (iStrokeType) {
Tom Sepez1498ab82018-12-06 17:56:28 +000027 case XFA_AttributeValue::DashDot: {
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000028 float dashArray[] = {4, 1, 2, 1};
Tom Sepez1498ab82018-12-06 17:56:28 +000029 if (iCapType != XFA_AttributeValue::Butt) {
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000030 dashArray[1] = 2;
31 dashArray[3] = 2;
32 }
Tom Sepezef9b6292020-10-06 18:44:53 +000033 pGraphics->SetLineDash(0, dashArray);
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000034 break;
35 }
Tom Sepez1498ab82018-12-06 17:56:28 +000036 case XFA_AttributeValue::DashDotDot: {
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000037 float dashArray[] = {4, 1, 2, 1, 2, 1};
Tom Sepez1498ab82018-12-06 17:56:28 +000038 if (iCapType != XFA_AttributeValue::Butt) {
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000039 dashArray[1] = 2;
40 dashArray[3] = 2;
41 dashArray[5] = 2;
42 }
Tom Sepezef9b6292020-10-06 18:44:53 +000043 pGraphics->SetLineDash(0, dashArray);
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000044 break;
45 }
Tom Sepez1498ab82018-12-06 17:56:28 +000046 case XFA_AttributeValue::Dashed: {
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000047 float dashArray[] = {5, 1};
Tom Sepez1498ab82018-12-06 17:56:28 +000048 if (iCapType != XFA_AttributeValue::Butt)
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000049 dashArray[1] = 2;
50
Tom Sepezef9b6292020-10-06 18:44:53 +000051 pGraphics->SetLineDash(0, dashArray);
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000052 break;
53 }
Tom Sepez1498ab82018-12-06 17:56:28 +000054 case XFA_AttributeValue::Dotted: {
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000055 float dashArray[] = {2, 1};
Tom Sepez1498ab82018-12-06 17:56:28 +000056 if (iCapType != XFA_AttributeValue::Butt)
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000057 dashArray[1] = 2;
58
Tom Sepezef9b6292020-10-06 18:44:53 +000059 pGraphics->SetLineDash(0, dashArray);
Dan Sinclairc37fa7d2018-01-17 19:29:46 +000060 break;
61 }
62 default:
63 pGraphics->SetSolidLineDash();
64 break;
65 }
66}
67
Dan Sinclairbf1cf342018-01-03 15:52:41 -050068CXFA_Stroke::CXFA_Stroke(CXFA_Document* pDoc,
69 XFA_PacketType ePacket,
Tom Sepez0c057582021-08-17 17:37:23 +000070 Mask<XFA_XDPPACKET> validPackets,
Dan Sinclairbf1cf342018-01-03 15:52:41 -050071 XFA_ObjectType oType,
72 XFA_Element eType,
Tom Sepezc0fa5f62019-01-25 20:07:27 +000073 pdfium::span<const PropertyData> properties,
74 pdfium::span<const AttributeData> attributes,
Tom Sepez757a6bf2020-09-17 17:22:38 +000075 CJX_Object* js_node)
Dan Sinclairbf1cf342018-01-03 15:52:41 -050076 : CXFA_Node(pDoc,
77 ePacket,
78 validPackets,
79 oType,
80 eType,
81 properties,
82 attributes,
Tom Sepez757a6bf2020-09-17 17:22:38 +000083 js_node) {}
Dan Sinclairbf1cf342018-01-03 15:52:41 -050084
85CXFA_Stroke::~CXFA_Stroke() = default;
86
87bool CXFA_Stroke::IsVisible() {
Tom Sepez1498ab82018-12-06 17:56:28 +000088 XFA_AttributeValue presence = JSObject()
89 ->TryEnum(XFA_Attribute::Presence, true)
90 .value_or(XFA_AttributeValue::Visible);
91 return presence == XFA_AttributeValue::Visible;
Dan Sinclairbf1cf342018-01-03 15:52:41 -050092}
93
Tom Sepez1498ab82018-12-06 17:56:28 +000094XFA_AttributeValue CXFA_Stroke::GetCapType() {
Dan Sinclairbf1cf342018-01-03 15:52:41 -050095 return JSObject()->GetEnum(XFA_Attribute::Cap);
96}
97
Tom Sepez1498ab82018-12-06 17:56:28 +000098XFA_AttributeValue CXFA_Stroke::GetStrokeType() {
Dan Sinclairbf1cf342018-01-03 15:52:41 -050099 return JSObject()->GetEnum(XFA_Attribute::Stroke);
100}
101
102float CXFA_Stroke::GetThickness() const {
103 return GetMSThickness().ToUnit(XFA_Unit::Pt);
104}
105
106CXFA_Measurement CXFA_Stroke::GetMSThickness() const {
107 return JSObject()->GetMeasure(XFA_Attribute::Thickness);
108}
109
110void CXFA_Stroke::SetMSThickness(CXFA_Measurement msThinkness) {
111 JSObject()->SetMeasure(XFA_Attribute::Thickness, msThinkness, false);
112}
113
Tom Sepez1aacc272021-08-30 17:33:28 +0000114FX_ARGB CXFA_Stroke::GetColor() const {
115 const auto* pNode = GetChild<CXFA_Color>(0, XFA_Element::Color, false);
Dan Sinclairbf1cf342018-01-03 15:52:41 -0500116 if (!pNode)
117 return 0xFF000000;
118
Tom Sepezb9134322021-07-19 22:11:01 +0000119 return CXFA_Color::StringToFXARGB(
Dan Sinclairbf1cf342018-01-03 15:52:41 -0500120 pNode->JSObject()->GetCData(XFA_Attribute::Value).AsStringView());
121}
122
123void CXFA_Stroke::SetColor(FX_ARGB argb) {
124 CXFA_Color* pNode =
Dan Sinclair640d8ff2018-01-10 16:28:57 +0000125 JSObject()->GetOrCreateProperty<CXFA_Color>(0, XFA_Element::Color);
Dan Sinclair54f86142018-01-10 17:03:35 +0000126 if (!pNode)
127 return;
128
Lei Zhang7b373c42024-07-03 16:44:28 +0000129 pNode->JSObject()->SetCData(
130 XFA_Attribute::Value,
Lei Zhanga7157062024-07-03 17:45:38 +0000131 WideString::FromASCII(CFGAS_GEColor::ColorToString(argb).AsStringView()));
Dan Sinclairbf1cf342018-01-03 15:52:41 -0500132}
133
Tom Sepez1498ab82018-12-06 17:56:28 +0000134XFA_AttributeValue CXFA_Stroke::GetJoinType() {
Dan Sinclairbf1cf342018-01-03 15:52:41 -0500135 return JSObject()->GetEnum(XFA_Attribute::Join);
136}
137
138bool CXFA_Stroke::IsInverted() {
139 return JSObject()->GetBoolean(XFA_Attribute::Inverted);
140}
141
142float CXFA_Stroke::GetRadius() const {
143 return JSObject()
144 ->TryMeasure(XFA_Attribute::Radius, true)
145 .value_or(CXFA_Measurement(0, XFA_Unit::In))
146 .ToUnit(XFA_Unit::Pt);
147}
148
Tom Sepez875651c2021-08-23 22:33:59 +0000149bool CXFA_Stroke::SameStyles(CXFA_Stroke* stroke,
150 Mask<SameStyleOption> dwFlags) {
Dan Sinclairbf1cf342018-01-03 15:52:41 -0500151 if (this == stroke)
152 return true;
153 if (fabs(GetThickness() - stroke->GetThickness()) >= 0.01f)
154 return false;
Tom Sepez875651c2021-08-23 22:33:59 +0000155 if (!(dwFlags & SameStyleOption::kNoPresence) &&
Dan Sinclairbf1cf342018-01-03 15:52:41 -0500156 IsVisible() != stroke->IsVisible()) {
157 return false;
158 }
159 if (GetStrokeType() != stroke->GetStrokeType())
160 return false;
161 if (GetColor() != stroke->GetColor())
162 return false;
Tom Sepez875651c2021-08-23 22:33:59 +0000163 if ((dwFlags & CXFA_Stroke::SameStyleOption::kCorner) &&
Dan Sinclairbf1cf342018-01-03 15:52:41 -0500164 fabs(GetRadius() - stroke->GetRadius()) >= 0.01f) {
165 return false;
166 }
167 return true;
168}
Dan Sinclair454ab872018-01-16 21:20:46 +0000169
Tom Sepeze7faff82021-04-30 21:18:36 +0000170void CXFA_Stroke::Stroke(CFGAS_GEGraphics* pGS,
171 const CFGAS_GEPath& pPath,
Dan Sinclair454ab872018-01-16 21:20:46 +0000172 const CFX_Matrix& matrix) {
173 if (!IsVisible())
174 return;
175
176 float fThickness = GetThickness();
177 if (fThickness < 0.001f)
178 return;
179
Tom Sepeza716ad72022-08-15 21:24:56 +0000180 CFGAS_GEGraphics::StateRestorer restorer(pGS);
Dan Sinclair454ab872018-01-16 21:20:46 +0000181 if (IsCorner() && fThickness > 2 * GetRadius())
182 fThickness = 2 * GetRadius();
183
184 pGS->SetLineWidth(fThickness);
185 pGS->EnableActOnDash();
Tom Sepeza7a7c922021-08-26 20:36:04 +0000186 pGS->SetLineCap(CFX_GraphStateData::LineCap::kButt);
Tom Sepez1498ab82018-12-06 17:56:28 +0000187 XFA_StrokeTypeSetLineDash(pGS, GetStrokeType(), XFA_AttributeValue::Butt);
Tom Sepez45eae7b2020-10-13 22:50:53 +0000188 pGS->SetStrokeColor(CFGAS_GEColor(GetColor()));
Tom Sepezd9c39462021-05-01 00:02:30 +0000189 pGS->StrokePath(pPath, matrix);
Dan Sinclair454ab872018-01-16 21:20:46 +0000190}