1 // Copyright 2014 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 "xfa/fwl/theme/cfwl_checkboxtp.h"
8
9 #include "core/fxge/cfx_pathdata.h"
10 #include "core/fxge/render_defines.h"
11 #include "third_party/base/ptr_util.h"
12 #include "xfa/fde/cfde_textout.h"
13 #include "xfa/fwl/cfwl_checkbox.h"
14 #include "xfa/fwl/cfwl_themebackground.h"
15 #include "xfa/fwl/cfwl_themetext.h"
16 #include "xfa/fwl/cfwl_widget.h"
17 #include "xfa/fxgraphics/cxfa_gecolor.h"
18 #include "xfa/fxgraphics/cxfa_gepath.h"
19
20 namespace {
21
22 constexpr int kSignPath = 100;
23
ScaleBezierPoint(const CFX_PointF & point)24 CFX_PointF ScaleBezierPoint(const CFX_PointF& point) {
25 CFX_PointF scaled_point(point);
26 scaled_point.x *= FX_BEZIER;
27 scaled_point.y *= FX_BEZIER;
28 return scaled_point;
29 }
30
31 } // namespace
32
33 CFWL_CheckBoxTP::CFWL_CheckBoxTP() = default;
34
~CFWL_CheckBoxTP()35 CFWL_CheckBoxTP::~CFWL_CheckBoxTP() {
36 if (m_pCheckPath)
37 m_pCheckPath->Clear();
38 }
39
DrawText(const CFWL_ThemeText & pParams)40 void CFWL_CheckBoxTP::DrawText(const CFWL_ThemeText& pParams) {
41 EnsureTTOInitialized();
42 m_pTextOut->SetTextColor(pParams.m_dwStates & CFWL_PartState_Disabled
43 ? FWLTHEME_CAPACITY_TextDisColor
44 : FWLTHEME_CAPACITY_TextColor);
45 CFWL_WidgetTP::DrawText(pParams);
46 }
47
DrawSignCheck(CXFA_Graphics * pGraphics,const CFX_RectF & rtSign,FX_ARGB argbFill,const CFX_Matrix & matrix)48 void CFWL_CheckBoxTP::DrawSignCheck(CXFA_Graphics* pGraphics,
49 const CFX_RectF& rtSign,
50 FX_ARGB argbFill,
51 const CFX_Matrix& matrix) {
52 if (!m_pCheckPath)
53 InitCheckPath(rtSign.width);
54
55 CFX_Matrix mt;
56 mt.Translate(rtSign.left, rtSign.top);
57 mt.Concat(matrix);
58 pGraphics->SaveGraphState();
59 pGraphics->SetFillColor(CXFA_GEColor(argbFill));
60 pGraphics->FillPath(m_pCheckPath.get(), FXFILL_WINDING, &mt);
61 pGraphics->RestoreGraphState();
62 }
63
DrawSignCircle(CXFA_Graphics * pGraphics,const CFX_RectF & rtSign,FX_ARGB argbFill,const CFX_Matrix & matrix)64 void CFWL_CheckBoxTP::DrawSignCircle(CXFA_Graphics* pGraphics,
65 const CFX_RectF& rtSign,
66 FX_ARGB argbFill,
67 const CFX_Matrix& matrix) {
68 CXFA_GEPath path;
69 path.AddEllipse(rtSign);
70 pGraphics->SaveGraphState();
71 pGraphics->SetFillColor(CXFA_GEColor(argbFill));
72 pGraphics->FillPath(&path, FXFILL_WINDING, &matrix);
73 pGraphics->RestoreGraphState();
74 }
75
DrawSignCross(CXFA_Graphics * pGraphics,const CFX_RectF & rtSign,FX_ARGB argbFill,const CFX_Matrix & matrix)76 void CFWL_CheckBoxTP::DrawSignCross(CXFA_Graphics* pGraphics,
77 const CFX_RectF& rtSign,
78 FX_ARGB argbFill,
79 const CFX_Matrix& matrix) {
80 CXFA_GEPath path;
81 float fRight = rtSign.right();
82 float fBottom = rtSign.bottom();
83 path.AddLine(rtSign.TopLeft(), CFX_PointF(fRight, fBottom));
84 path.AddLine(CFX_PointF(rtSign.left, fBottom),
85 CFX_PointF(fRight, rtSign.top));
86
87 pGraphics->SaveGraphState();
88 pGraphics->SetStrokeColor(CXFA_GEColor(argbFill));
89 pGraphics->SetLineWidth(1.0f);
90 pGraphics->StrokePath(&path, &matrix);
91 pGraphics->RestoreGraphState();
92 }
93
DrawSignDiamond(CXFA_Graphics * pGraphics,const CFX_RectF & rtSign,FX_ARGB argbFill,const CFX_Matrix & matrix)94 void CFWL_CheckBoxTP::DrawSignDiamond(CXFA_Graphics* pGraphics,
95 const CFX_RectF& rtSign,
96 FX_ARGB argbFill,
97 const CFX_Matrix& matrix) {
98 CXFA_GEPath path;
99 float fWidth = rtSign.width;
100 float fHeight = rtSign.height;
101 float fBottom = rtSign.bottom();
102 path.MoveTo(CFX_PointF(rtSign.left + fWidth / 2, rtSign.top));
103 path.LineTo(CFX_PointF(rtSign.left, rtSign.top + fHeight / 2));
104 path.LineTo(CFX_PointF(rtSign.left + fWidth / 2, fBottom));
105 path.LineTo(CFX_PointF(rtSign.right(), rtSign.top + fHeight / 2));
106 path.LineTo(CFX_PointF(rtSign.left + fWidth / 2, rtSign.top));
107
108 pGraphics->SaveGraphState();
109 pGraphics->SetFillColor(CXFA_GEColor(argbFill));
110 pGraphics->FillPath(&path, FXFILL_WINDING, &matrix);
111 pGraphics->RestoreGraphState();
112 }
113
DrawSignSquare(CXFA_Graphics * pGraphics,const CFX_RectF & rtSign,FX_ARGB argbFill,const CFX_Matrix & matrix)114 void CFWL_CheckBoxTP::DrawSignSquare(CXFA_Graphics* pGraphics,
115 const CFX_RectF& rtSign,
116 FX_ARGB argbFill,
117 const CFX_Matrix& matrix) {
118 CXFA_GEPath path;
119 path.AddRectangle(rtSign.left, rtSign.top, rtSign.width, rtSign.height);
120 pGraphics->SaveGraphState();
121 pGraphics->SetFillColor(CXFA_GEColor(argbFill));
122 pGraphics->FillPath(&path, FXFILL_WINDING, &matrix);
123 pGraphics->RestoreGraphState();
124 }
125
DrawSignStar(CXFA_Graphics * pGraphics,const CFX_RectF & rtSign,FX_ARGB argbFill,const CFX_Matrix & matrix)126 void CFWL_CheckBoxTP::DrawSignStar(CXFA_Graphics* pGraphics,
127 const CFX_RectF& rtSign,
128 FX_ARGB argbFill,
129 const CFX_Matrix& matrix) {
130 CXFA_GEPath path;
131 float fBottom = rtSign.bottom();
132 float fRadius =
133 (rtSign.top - fBottom) / (1 + static_cast<float>(cos(FX_PI / 5.0f)));
134 CFX_PointF ptCenter((rtSign.left + rtSign.right()) / 2.0f,
135 (rtSign.top + fBottom) / 2.0f);
136
137 CFX_PointF points[5];
138 float fAngel = FX_PI / 10.0f;
139 for (int32_t i = 0; i < 5; i++) {
140 points[i] =
141 ptCenter + CFX_PointF(fRadius * static_cast<float>(cos(fAngel)),
142 fRadius * static_cast<float>(sin(fAngel)));
143 fAngel += FX_PI * 2 / 5.0f;
144 }
145
146 path.MoveTo(points[0]);
147 int32_t nNext = 0;
148 for (int32_t j = 0; j < 5; j++) {
149 nNext += 2;
150 if (nNext >= 5)
151 nNext -= 5;
152
153 path.LineTo(points[nNext]);
154 }
155 pGraphics->SaveGraphState();
156 pGraphics->SetFillColor(CXFA_GEColor(argbFill));
157 pGraphics->FillPath(&path, FXFILL_WINDING, &matrix);
158 pGraphics->RestoreGraphState();
159 }
160
InitCheckPath(float fCheckLen)161 void CFWL_CheckBoxTP::InitCheckPath(float fCheckLen) {
162 if (!m_pCheckPath) {
163 m_pCheckPath = pdfium::MakeUnique<CXFA_GEPath>();
164
165 float fWidth = kSignPath;
166 float fHeight = -kSignPath;
167 float fBottom = kSignPath;
168 CFX_PointF pt1(fWidth / 15.0f, fBottom + fHeight * 2 / 5.0f);
169 CFX_PointF pt2(fWidth / 4.5f, fBottom + fHeight / 16.0f);
170 CFX_PointF pt3(fWidth / 3.0f, fBottom);
171 CFX_PointF pt4(fWidth * 14 / 15.0f, fBottom + fHeight * 15 / 16.0f);
172 CFX_PointF pt5(fWidth / 3.6f, fBottom + fHeight / 3.5f);
173 CFX_PointF pt12(fWidth / 7.0f, fBottom + fHeight * 2 / 7.0f);
174 CFX_PointF pt21(fWidth / 5.0f, fBottom + fHeight / 5.0f);
175 CFX_PointF pt23(fWidth / 4.4f, fBottom + fHeight * 0 / 16.0f);
176 CFX_PointF pt32(fWidth / 4.0f, fBottom);
177 CFX_PointF pt34(fWidth * (1 / 7.0f + 7 / 15.0f),
178 fBottom + fHeight * 4 / 5.0f);
179 CFX_PointF pt43(fWidth * (1 / 7.0f + 7 / 15.0f),
180 fBottom + fHeight * 4 / 5.0f);
181 CFX_PointF pt45(fWidth * 7 / 15.0f, fBottom + fHeight * 8 / 7.0f);
182 CFX_PointF pt54(fWidth / 3.4f, fBottom + fHeight / 3.5f);
183 CFX_PointF pt51(fWidth / 3.6f, fBottom + fHeight / 4.0f);
184 CFX_PointF pt15(fWidth / 3.5f, fBottom + fHeight * 3.5f / 5.0f);
185 m_pCheckPath->MoveTo(pt1);
186
187 CFX_PointF p1 = ScaleBezierPoint(pt12 - pt1);
188 CFX_PointF p2 = ScaleBezierPoint(pt21 - pt2);
189 m_pCheckPath->BezierTo(pt1 + p1, pt2 + p2, pt2);
190
191 p1 = ScaleBezierPoint(pt23 - pt2);
192 p2 = ScaleBezierPoint(pt32 - pt3);
193 m_pCheckPath->BezierTo(pt2 + p1, pt3 + p2, pt3);
194
195 p1 = ScaleBezierPoint(pt34 - pt3);
196 p2 = ScaleBezierPoint(pt43 - pt4);
197 m_pCheckPath->BezierTo(pt3 + p1, pt4 + p2, pt4);
198
199 p1 = ScaleBezierPoint(pt45 - pt4);
200 p2 = ScaleBezierPoint(pt54 - pt5);
201 m_pCheckPath->BezierTo(pt4 + p1, pt5 + p2, pt5);
202
203 p1 = ScaleBezierPoint(pt51 - pt5);
204 p2 = ScaleBezierPoint(pt15 - pt1);
205 m_pCheckPath->BezierTo(pt5 + p1, pt1 + p2, pt1);
206
207 float fScale = fCheckLen / kSignPath;
208 CFX_Matrix mt;
209 mt.Scale(fScale, fScale);
210
211 m_pCheckPath->TransformBy(mt);
212 }
213 }
214
DrawBackground(const CFWL_ThemeBackground & pParams)215 void CFWL_CheckBoxTP::DrawBackground(const CFWL_ThemeBackground& pParams) {
216 if (pParams.m_iPart != CFWL_Part::CheckBox)
217 return;
218
219 if ((pParams.m_dwStates & CFWL_PartState_Checked) ||
220 (pParams.m_dwStates & CFWL_PartState_Neutral)) {
221 DrawCheckSign(pParams.m_pWidget, pParams.m_pGraphics.Get(),
222 pParams.m_rtPart, pParams.m_dwStates, pParams.m_matrix);
223 }
224 }
225
DrawCheckSign(CFWL_Widget * pWidget,CXFA_Graphics * pGraphics,const CFX_RectF & pRtBox,int32_t iState,const CFX_Matrix & matrix)226 void CFWL_CheckBoxTP::DrawCheckSign(CFWL_Widget* pWidget,
227 CXFA_Graphics* pGraphics,
228 const CFX_RectF& pRtBox,
229 int32_t iState,
230 const CFX_Matrix& matrix) {
231 CFX_RectF rtSign(pRtBox);
232 uint32_t dwColor = iState & CFWL_PartState_Neutral ? 0xFFA9A9A9 : 0xFF000000;
233
234 uint32_t dwStyle = pWidget->GetStylesEx();
235 rtSign.Deflate(rtSign.width / 4, rtSign.height / 4);
236 switch (dwStyle & FWL_STYLEEXT_CKB_SignShapeMask) {
237 case FWL_STYLEEXT_CKB_SignShapeCheck:
238 DrawSignCheck(pGraphics, rtSign, dwColor, matrix);
239 break;
240 case FWL_STYLEEXT_CKB_SignShapeCircle:
241 DrawSignCircle(pGraphics, rtSign, dwColor, matrix);
242 break;
243 case FWL_STYLEEXT_CKB_SignShapeCross:
244 DrawSignCross(pGraphics, rtSign, dwColor, matrix);
245 break;
246 case FWL_STYLEEXT_CKB_SignShapeDiamond:
247 DrawSignDiamond(pGraphics, rtSign, dwColor, matrix);
248 break;
249 case FWL_STYLEEXT_CKB_SignShapeSquare:
250 DrawSignSquare(pGraphics, rtSign, dwColor, matrix);
251 break;
252 case FWL_STYLEEXT_CKB_SignShapeStar:
253 DrawSignStar(pGraphics, rtSign, dwColor, matrix);
254 break;
255 default:
256 break;
257 }
258 }
259