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