K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2016 The PDFium Authors |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 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 "xfa/fxfa/parser/cxfa_stroke.h" |
| 8 | |
Lei Zhang | a4e12af | 2021-08-04 17:43:18 +0000 | [diff] [blame] | 9 | #include <math.h> |
| 10 | |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 11 | #include <utility> |
| 12 | |
Lei Zhang | d859d57 | 2018-01-17 20:44:26 +0000 | [diff] [blame] | 13 | #include "fxjs/xfa/cjx_object.h" |
Lei Zhang | a715706 | 2024-07-03 17:45:38 +0000 | [diff] [blame] | 14 | #include "xfa/fgas/graphics/cfgas_gecolor.h" |
Tom Sepez | 45eae7b | 2020-10-13 22:50:53 +0000 | [diff] [blame] | 15 | #include "xfa/fgas/graphics/cfgas_gegraphics.h" |
Dan Sinclair | 454ab87 | 2018-01-16 21:20:46 +0000 | [diff] [blame] | 16 | #include "xfa/fxfa/cxfa_ffwidget.h" |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 17 | #include "xfa/fxfa/parser/cxfa_color.h" |
Tom Sepez | 757a6bf | 2020-09-17 17:22:38 +0000 | [diff] [blame] | 18 | #include "xfa/fxfa/parser/cxfa_document.h" |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 19 | #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 Sepez | 45eae7b | 2020-10-13 22:50:53 +0000 | [diff] [blame] | 23 | void XFA_StrokeTypeSetLineDash(CFGAS_GEGraphics* pGraphics, |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 24 | XFA_AttributeValue iStrokeType, |
| 25 | XFA_AttributeValue iCapType) { |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 26 | switch (iStrokeType) { |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 27 | case XFA_AttributeValue::DashDot: { |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 28 | float dashArray[] = {4, 1, 2, 1}; |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 29 | if (iCapType != XFA_AttributeValue::Butt) { |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 30 | dashArray[1] = 2; |
| 31 | dashArray[3] = 2; |
| 32 | } |
Tom Sepez | ef9b629 | 2020-10-06 18:44:53 +0000 | [diff] [blame] | 33 | pGraphics->SetLineDash(0, dashArray); |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 34 | break; |
| 35 | } |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 36 | case XFA_AttributeValue::DashDotDot: { |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 37 | float dashArray[] = {4, 1, 2, 1, 2, 1}; |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 38 | if (iCapType != XFA_AttributeValue::Butt) { |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 39 | dashArray[1] = 2; |
| 40 | dashArray[3] = 2; |
| 41 | dashArray[5] = 2; |
| 42 | } |
Tom Sepez | ef9b629 | 2020-10-06 18:44:53 +0000 | [diff] [blame] | 43 | pGraphics->SetLineDash(0, dashArray); |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 44 | break; |
| 45 | } |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 46 | case XFA_AttributeValue::Dashed: { |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 47 | float dashArray[] = {5, 1}; |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 48 | if (iCapType != XFA_AttributeValue::Butt) |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 49 | dashArray[1] = 2; |
| 50 | |
Tom Sepez | ef9b629 | 2020-10-06 18:44:53 +0000 | [diff] [blame] | 51 | pGraphics->SetLineDash(0, dashArray); |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 52 | break; |
| 53 | } |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 54 | case XFA_AttributeValue::Dotted: { |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 55 | float dashArray[] = {2, 1}; |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 56 | if (iCapType != XFA_AttributeValue::Butt) |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 57 | dashArray[1] = 2; |
| 58 | |
Tom Sepez | ef9b629 | 2020-10-06 18:44:53 +0000 | [diff] [blame] | 59 | pGraphics->SetLineDash(0, dashArray); |
Dan Sinclair | c37fa7d | 2018-01-17 19:29:46 +0000 | [diff] [blame] | 60 | break; |
| 61 | } |
| 62 | default: |
| 63 | pGraphics->SetSolidLineDash(); |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 68 | CXFA_Stroke::CXFA_Stroke(CXFA_Document* pDoc, |
| 69 | XFA_PacketType ePacket, |
Tom Sepez | 0c05758 | 2021-08-17 17:37:23 +0000 | [diff] [blame] | 70 | Mask<XFA_XDPPACKET> validPackets, |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 71 | XFA_ObjectType oType, |
| 72 | XFA_Element eType, |
Tom Sepez | c0fa5f6 | 2019-01-25 20:07:27 +0000 | [diff] [blame] | 73 | pdfium::span<const PropertyData> properties, |
| 74 | pdfium::span<const AttributeData> attributes, |
Tom Sepez | 757a6bf | 2020-09-17 17:22:38 +0000 | [diff] [blame] | 75 | CJX_Object* js_node) |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 76 | : CXFA_Node(pDoc, |
| 77 | ePacket, |
| 78 | validPackets, |
| 79 | oType, |
| 80 | eType, |
| 81 | properties, |
| 82 | attributes, |
Tom Sepez | 757a6bf | 2020-09-17 17:22:38 +0000 | [diff] [blame] | 83 | js_node) {} |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 84 | |
| 85 | CXFA_Stroke::~CXFA_Stroke() = default; |
| 86 | |
| 87 | bool CXFA_Stroke::IsVisible() { |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 88 | XFA_AttributeValue presence = JSObject() |
| 89 | ->TryEnum(XFA_Attribute::Presence, true) |
| 90 | .value_or(XFA_AttributeValue::Visible); |
| 91 | return presence == XFA_AttributeValue::Visible; |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 94 | XFA_AttributeValue CXFA_Stroke::GetCapType() { |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 95 | return JSObject()->GetEnum(XFA_Attribute::Cap); |
| 96 | } |
| 97 | |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 98 | XFA_AttributeValue CXFA_Stroke::GetStrokeType() { |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 99 | return JSObject()->GetEnum(XFA_Attribute::Stroke); |
| 100 | } |
| 101 | |
| 102 | float CXFA_Stroke::GetThickness() const { |
| 103 | return GetMSThickness().ToUnit(XFA_Unit::Pt); |
| 104 | } |
| 105 | |
| 106 | CXFA_Measurement CXFA_Stroke::GetMSThickness() const { |
| 107 | return JSObject()->GetMeasure(XFA_Attribute::Thickness); |
| 108 | } |
| 109 | |
| 110 | void CXFA_Stroke::SetMSThickness(CXFA_Measurement msThinkness) { |
| 111 | JSObject()->SetMeasure(XFA_Attribute::Thickness, msThinkness, false); |
| 112 | } |
| 113 | |
Tom Sepez | 1aacc27 | 2021-08-30 17:33:28 +0000 | [diff] [blame] | 114 | FX_ARGB CXFA_Stroke::GetColor() const { |
| 115 | const auto* pNode = GetChild<CXFA_Color>(0, XFA_Element::Color, false); |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 116 | if (!pNode) |
| 117 | return 0xFF000000; |
| 118 | |
Tom Sepez | b913432 | 2021-07-19 22:11:01 +0000 | [diff] [blame] | 119 | return CXFA_Color::StringToFXARGB( |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 120 | pNode->JSObject()->GetCData(XFA_Attribute::Value).AsStringView()); |
| 121 | } |
| 122 | |
| 123 | void CXFA_Stroke::SetColor(FX_ARGB argb) { |
| 124 | CXFA_Color* pNode = |
Dan Sinclair | 640d8ff | 2018-01-10 16:28:57 +0000 | [diff] [blame] | 125 | JSObject()->GetOrCreateProperty<CXFA_Color>(0, XFA_Element::Color); |
Dan Sinclair | 54f8614 | 2018-01-10 17:03:35 +0000 | [diff] [blame] | 126 | if (!pNode) |
| 127 | return; |
| 128 | |
Lei Zhang | 7b373c4 | 2024-07-03 16:44:28 +0000 | [diff] [blame] | 129 | pNode->JSObject()->SetCData( |
| 130 | XFA_Attribute::Value, |
Lei Zhang | a715706 | 2024-07-03 17:45:38 +0000 | [diff] [blame] | 131 | WideString::FromASCII(CFGAS_GEColor::ColorToString(argb).AsStringView())); |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 134 | XFA_AttributeValue CXFA_Stroke::GetJoinType() { |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 135 | return JSObject()->GetEnum(XFA_Attribute::Join); |
| 136 | } |
| 137 | |
| 138 | bool CXFA_Stroke::IsInverted() { |
| 139 | return JSObject()->GetBoolean(XFA_Attribute::Inverted); |
| 140 | } |
| 141 | |
| 142 | float 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 Sepez | 875651c | 2021-08-23 22:33:59 +0000 | [diff] [blame] | 149 | bool CXFA_Stroke::SameStyles(CXFA_Stroke* stroke, |
| 150 | Mask<SameStyleOption> dwFlags) { |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 151 | if (this == stroke) |
| 152 | return true; |
| 153 | if (fabs(GetThickness() - stroke->GetThickness()) >= 0.01f) |
| 154 | return false; |
Tom Sepez | 875651c | 2021-08-23 22:33:59 +0000 | [diff] [blame] | 155 | if (!(dwFlags & SameStyleOption::kNoPresence) && |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 156 | IsVisible() != stroke->IsVisible()) { |
| 157 | return false; |
| 158 | } |
| 159 | if (GetStrokeType() != stroke->GetStrokeType()) |
| 160 | return false; |
| 161 | if (GetColor() != stroke->GetColor()) |
| 162 | return false; |
Tom Sepez | 875651c | 2021-08-23 22:33:59 +0000 | [diff] [blame] | 163 | if ((dwFlags & CXFA_Stroke::SameStyleOption::kCorner) && |
Dan Sinclair | bf1cf34 | 2018-01-03 15:52:41 -0500 | [diff] [blame] | 164 | fabs(GetRadius() - stroke->GetRadius()) >= 0.01f) { |
| 165 | return false; |
| 166 | } |
| 167 | return true; |
| 168 | } |
Dan Sinclair | 454ab87 | 2018-01-16 21:20:46 +0000 | [diff] [blame] | 169 | |
Tom Sepez | e7faff8 | 2021-04-30 21:18:36 +0000 | [diff] [blame] | 170 | void CXFA_Stroke::Stroke(CFGAS_GEGraphics* pGS, |
| 171 | const CFGAS_GEPath& pPath, |
Dan Sinclair | 454ab87 | 2018-01-16 21:20:46 +0000 | [diff] [blame] | 172 | const CFX_Matrix& matrix) { |
| 173 | if (!IsVisible()) |
| 174 | return; |
| 175 | |
| 176 | float fThickness = GetThickness(); |
| 177 | if (fThickness < 0.001f) |
| 178 | return; |
| 179 | |
Tom Sepez | a716ad7 | 2022-08-15 21:24:56 +0000 | [diff] [blame] | 180 | CFGAS_GEGraphics::StateRestorer restorer(pGS); |
Dan Sinclair | 454ab87 | 2018-01-16 21:20:46 +0000 | [diff] [blame] | 181 | if (IsCorner() && fThickness > 2 * GetRadius()) |
| 182 | fThickness = 2 * GetRadius(); |
| 183 | |
| 184 | pGS->SetLineWidth(fThickness); |
| 185 | pGS->EnableActOnDash(); |
Tom Sepez | a7a7c92 | 2021-08-26 20:36:04 +0000 | [diff] [blame] | 186 | pGS->SetLineCap(CFX_GraphStateData::LineCap::kButt); |
Tom Sepez | 1498ab8 | 2018-12-06 17:56:28 +0000 | [diff] [blame] | 187 | XFA_StrokeTypeSetLineDash(pGS, GetStrokeType(), XFA_AttributeValue::Butt); |
Tom Sepez | 45eae7b | 2020-10-13 22:50:53 +0000 | [diff] [blame] | 188 | pGS->SetStrokeColor(CFGAS_GEColor(GetColor())); |
Tom Sepez | d9c3946 | 2021-05-01 00:02:30 +0000 | [diff] [blame] | 189 | pGS->StrokePath(pPath, matrix); |
Dan Sinclair | 454ab87 | 2018-01-16 21:20:46 +0000 | [diff] [blame] | 190 | } |