xref: /aosp_15_r20/external/pdfium/core/fpdfdoc/cpdf_annot.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 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 "core/fpdfdoc/cpdf_annot.h"
8 
9 #include <algorithm>
10 #include <utility>
11 
12 #include "constants/annotation_common.h"
13 #include "constants/annotation_flags.h"
14 #include "core/fpdfapi/page/cpdf_form.h"
15 #include "core/fpdfapi/page/cpdf_page.h"
16 #include "core/fpdfapi/page/cpdf_pageimagecache.h"
17 #include "core/fpdfapi/parser/cpdf_array.h"
18 #include "core/fpdfapi/parser/cpdf_boolean.h"
19 #include "core/fpdfapi/parser/cpdf_dictionary.h"
20 #include "core/fpdfapi/parser/cpdf_document.h"
21 #include "core/fpdfapi/parser/cpdf_stream.h"
22 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
23 #include "core/fpdfapi/render/cpdf_rendercontext.h"
24 #include "core/fpdfapi/render/cpdf_renderoptions.h"
25 #include "core/fpdfdoc/cpdf_generateap.h"
26 #include "core/fxge/cfx_fillrenderoptions.h"
27 #include "core/fxge/cfx_graphstatedata.h"
28 #include "core/fxge/cfx_path.h"
29 #include "core/fxge/cfx_renderdevice.h"
30 #include "third_party/base/check.h"
31 
32 namespace {
33 
34 const char kPDFiumKey_HasGeneratedAP[] = "PDFIUM_HasGeneratedAP";
35 
IsTextMarkupAnnotation(CPDF_Annot::Subtype type)36 bool IsTextMarkupAnnotation(CPDF_Annot::Subtype type) {
37   return type == CPDF_Annot::Subtype::HIGHLIGHT ||
38          type == CPDF_Annot::Subtype::SQUIGGLY ||
39          type == CPDF_Annot::Subtype::STRIKEOUT ||
40          type == CPDF_Annot::Subtype::UNDERLINE;
41 }
42 
AnnotGetMatrix(CPDF_Page * pPage,CPDF_Annot * pAnnot,CPDF_Annot::AppearanceMode mode,const CFX_Matrix & mtUser2Device,CFX_Matrix * matrix)43 CPDF_Form* AnnotGetMatrix(CPDF_Page* pPage,
44                           CPDF_Annot* pAnnot,
45                           CPDF_Annot::AppearanceMode mode,
46                           const CFX_Matrix& mtUser2Device,
47                           CFX_Matrix* matrix) {
48   CPDF_Form* pForm = pAnnot->GetAPForm(pPage, mode);
49   if (!pForm)
50     return nullptr;
51 
52   CFX_Matrix form_matrix = pForm->GetDict()->GetMatrixFor("Matrix");
53   CFX_FloatRect form_bbox =
54       form_matrix.TransformRect(pForm->GetDict()->GetRectFor("BBox"));
55   matrix->MatchRect(pAnnot->GetRect(), form_bbox);
56 
57   // Compensate for page rotation.
58   if ((pAnnot->GetFlags() & pdfium::annotation_flags::kNoRotate) &&
59       pPage->GetPageRotation() != 0) {
60     // Rotate annotation rect around top-left angle (according to the
61     // specification).
62     const float offset_x = pAnnot->GetRect().Left();
63     const float offset_y = pAnnot->GetRect().Top();
64     matrix->Concat({1, 0, 0, 1, -offset_x, -offset_y});
65     // GetPageRotation returns value in fractions of pi/2.
66     const float angle = FXSYS_PI / 2 * pPage->GetPageRotation();
67     matrix->Rotate(angle);
68     matrix->Concat({1, 0, 0, 1, offset_x, offset_y});
69   }
70 
71   matrix->Concat(mtUser2Device);
72   return pForm;
73 }
74 
GetAnnotAPInternal(CPDF_Dictionary * pAnnotDict,CPDF_Annot::AppearanceMode eMode,bool bFallbackToNormal)75 RetainPtr<CPDF_Stream> GetAnnotAPInternal(CPDF_Dictionary* pAnnotDict,
76                                           CPDF_Annot::AppearanceMode eMode,
77                                           bool bFallbackToNormal) {
78   RetainPtr<CPDF_Dictionary> pAP =
79       pAnnotDict->GetMutableDictFor(pdfium::annotation::kAP);
80   if (!pAP)
81     return nullptr;
82 
83   const char* ap_entry = "N";
84   if (eMode == CPDF_Annot::AppearanceMode::kDown)
85     ap_entry = "D";
86   else if (eMode == CPDF_Annot::AppearanceMode::kRollover)
87     ap_entry = "R";
88   if (bFallbackToNormal && !pAP->KeyExist(ap_entry))
89     ap_entry = "N";
90 
91   RetainPtr<CPDF_Object> psub = pAP->GetMutableDirectObjectFor(ap_entry);
92   if (!psub)
93     return nullptr;
94 
95   RetainPtr<CPDF_Stream> pStream(psub->AsMutableStream());
96   if (pStream)
97     return pStream;
98 
99   CPDF_Dictionary* pDict = psub->AsMutableDictionary();
100   if (!pDict)
101     return nullptr;
102 
103   ByteString as = pAnnotDict->GetByteStringFor(pdfium::annotation::kAS);
104   if (as.IsEmpty()) {
105     ByteString value = pAnnotDict->GetByteStringFor("V");
106     if (value.IsEmpty()) {
107       RetainPtr<const CPDF_Dictionary> pParentDict =
108           pAnnotDict->GetDictFor("Parent");
109       value = pParentDict ? pParentDict->GetByteStringFor("V") : ByteString();
110     }
111     as = (!value.IsEmpty() && pDict->KeyExist(value)) ? value : "Off";
112   }
113   return pDict->GetMutableStreamFor(as);
114 }
115 
116 }  // namespace
117 
CPDF_Annot(RetainPtr<CPDF_Dictionary> pDict,CPDF_Document * pDocument)118 CPDF_Annot::CPDF_Annot(RetainPtr<CPDF_Dictionary> pDict,
119                        CPDF_Document* pDocument)
120     : m_pAnnotDict(std::move(pDict)),
121       m_pDocument(pDocument),
122       m_nSubtype(StringToAnnotSubtype(
123           m_pAnnotDict->GetByteStringFor(pdfium::annotation::kSubtype))),
124       m_bIsTextMarkupAnnotation(IsTextMarkupAnnotation(m_nSubtype)),
125       m_bHasGeneratedAP(
126           m_pAnnotDict->GetBooleanFor(kPDFiumKey_HasGeneratedAP, false)) {
127   GenerateAPIfNeeded();
128 }
129 
~CPDF_Annot()130 CPDF_Annot::~CPDF_Annot() {
131   ClearCachedAP();
132 }
133 
GenerateAPIfNeeded()134 void CPDF_Annot::GenerateAPIfNeeded() {
135   if (!ShouldGenerateAP())
136     return;
137   if (!CPDF_GenerateAP::GenerateAnnotAP(m_pDocument, m_pAnnotDict.Get(),
138                                         m_nSubtype)) {
139     return;
140   }
141 
142   m_pAnnotDict->SetNewFor<CPDF_Boolean>(kPDFiumKey_HasGeneratedAP, true);
143   m_bHasGeneratedAP = true;
144 }
145 
ShouldGenerateAP() const146 bool CPDF_Annot::ShouldGenerateAP() const {
147   // If AP dictionary exists and defines an appearance for normal mode, we use
148   // the appearance defined in the existing AP dictionary.
149   RetainPtr<const CPDF_Dictionary> pAP =
150       m_pAnnotDict->GetDictFor(pdfium::annotation::kAP);
151   if (pAP && pAP->GetDictFor("N"))
152     return false;
153 
154   return !IsHidden();
155 }
156 
ShouldDrawAnnotation() const157 bool CPDF_Annot::ShouldDrawAnnotation() const {
158   if (IsHidden())
159     return false;
160   return m_bOpenState || m_nSubtype != CPDF_Annot::Subtype::POPUP;
161 }
162 
ClearCachedAP()163 void CPDF_Annot::ClearCachedAP() {
164   m_APMap.clear();
165 }
166 
GetSubtype() const167 CPDF_Annot::Subtype CPDF_Annot::GetSubtype() const {
168   return m_nSubtype;
169 }
170 
RectForDrawing() const171 CFX_FloatRect CPDF_Annot::RectForDrawing() const {
172   bool bShouldUseQuadPointsCoords =
173       m_bIsTextMarkupAnnotation && m_bHasGeneratedAP;
174   if (bShouldUseQuadPointsCoords)
175     return BoundingRectFromQuadPoints(m_pAnnotDict.Get());
176   return m_pAnnotDict->GetRectFor(pdfium::annotation::kRect);
177 }
178 
GetRect() const179 CFX_FloatRect CPDF_Annot::GetRect() const {
180   CFX_FloatRect rect = RectForDrawing();
181   rect.Normalize();
182   return rect;
183 }
184 
GetFlags() const185 uint32_t CPDF_Annot::GetFlags() const {
186   return m_pAnnotDict->GetIntegerFor(pdfium::annotation::kF);
187 }
188 
IsHidden() const189 bool CPDF_Annot::IsHidden() const {
190   return !!(GetFlags() & pdfium::annotation_flags::kHidden);
191 }
192 
GetAnnotAP(CPDF_Dictionary * pAnnotDict,CPDF_Annot::AppearanceMode eMode)193 RetainPtr<CPDF_Stream> GetAnnotAP(CPDF_Dictionary* pAnnotDict,
194                                   CPDF_Annot::AppearanceMode eMode) {
195   DCHECK(pAnnotDict);
196   return GetAnnotAPInternal(pAnnotDict, eMode, true);
197 }
198 
GetAnnotAPNoFallback(CPDF_Dictionary * pAnnotDict,CPDF_Annot::AppearanceMode eMode)199 RetainPtr<CPDF_Stream> GetAnnotAPNoFallback(CPDF_Dictionary* pAnnotDict,
200                                             CPDF_Annot::AppearanceMode eMode) {
201   DCHECK(pAnnotDict);
202   return GetAnnotAPInternal(pAnnotDict, eMode, false);
203 }
204 
GetAPForm(CPDF_Page * pPage,AppearanceMode mode)205 CPDF_Form* CPDF_Annot::GetAPForm(CPDF_Page* pPage, AppearanceMode mode) {
206   RetainPtr<CPDF_Stream> pStream = GetAnnotAP(m_pAnnotDict.Get(), mode);
207   if (!pStream)
208     return nullptr;
209 
210   auto it = m_APMap.find(pStream);
211   if (it != m_APMap.end())
212     return it->second.get();
213 
214   auto pNewForm = std::make_unique<CPDF_Form>(
215       m_pDocument, pPage->GetMutableResources(), pStream);
216   pNewForm->ParseContent();
217 
218   CPDF_Form* pResult = pNewForm.get();
219   m_APMap[pStream] = std::move(pNewForm);
220   return pResult;
221 }
222 
SetPopupAnnotOpenState(bool bOpenState)223 void CPDF_Annot::SetPopupAnnotOpenState(bool bOpenState) {
224   if (m_pPopupAnnot)
225     m_pPopupAnnot->SetOpenState(bOpenState);
226 }
227 
GetPopupAnnotRect() const228 absl::optional<CFX_FloatRect> CPDF_Annot::GetPopupAnnotRect() const {
229   if (!m_pPopupAnnot)
230     return absl::nullopt;
231   return m_pPopupAnnot->GetRect();
232 }
233 
234 // static
RectFromQuadPointsArray(const CPDF_Array * pArray,size_t nIndex)235 CFX_FloatRect CPDF_Annot::RectFromQuadPointsArray(const CPDF_Array* pArray,
236                                                   size_t nIndex) {
237   DCHECK(pArray);
238   DCHECK(nIndex < pArray->size() / 8);
239 
240   // QuadPoints are defined with 4 pairs of numbers
241   // ([ pair0, pair1, pair2, pair3 ]), where
242   // pair0 = top_left
243   // pair1 = top_right
244   // pair2 = bottom_left
245   // pair3 = bottom_right
246   //
247   // On the other hand, /Rect is defined as 2 pairs [pair0, pair1] where:
248   // pair0 = bottom_left
249   // pair1 = top_right.
250 
251   return CFX_FloatRect(
252       pArray->GetFloatAt(4 + nIndex * 8), pArray->GetFloatAt(5 + nIndex * 8),
253       pArray->GetFloatAt(2 + nIndex * 8), pArray->GetFloatAt(3 + nIndex * 8));
254 }
255 
256 // static
BoundingRectFromQuadPoints(const CPDF_Dictionary * pAnnotDict)257 CFX_FloatRect CPDF_Annot::BoundingRectFromQuadPoints(
258     const CPDF_Dictionary* pAnnotDict) {
259   CFX_FloatRect ret;
260   RetainPtr<const CPDF_Array> pArray = pAnnotDict->GetArrayFor("QuadPoints");
261   size_t nQuadPointCount = pArray ? QuadPointCount(pArray.Get()) : 0;
262   if (nQuadPointCount == 0)
263     return ret;
264 
265   ret = RectFromQuadPointsArray(pArray.Get(), 0);
266   for (size_t i = 1; i < nQuadPointCount; ++i) {
267     CFX_FloatRect rect = RectFromQuadPointsArray(pArray.Get(), i);
268     ret.Union(rect);
269   }
270   return ret;
271 }
272 
273 // static
RectFromQuadPoints(const CPDF_Dictionary * pAnnotDict,size_t nIndex)274 CFX_FloatRect CPDF_Annot::RectFromQuadPoints(const CPDF_Dictionary* pAnnotDict,
275                                              size_t nIndex) {
276   RetainPtr<const CPDF_Array> pArray = pAnnotDict->GetArrayFor("QuadPoints");
277   size_t nQuadPointCount = pArray ? QuadPointCount(pArray.Get()) : 0;
278   if (nIndex >= nQuadPointCount)
279     return CFX_FloatRect();
280   return RectFromQuadPointsArray(pArray.Get(), nIndex);
281 }
282 
283 // static
StringToAnnotSubtype(const ByteString & sSubtype)284 CPDF_Annot::Subtype CPDF_Annot::StringToAnnotSubtype(
285     const ByteString& sSubtype) {
286   if (sSubtype == "Text")
287     return CPDF_Annot::Subtype::TEXT;
288   if (sSubtype == "Link")
289     return CPDF_Annot::Subtype::LINK;
290   if (sSubtype == "FreeText")
291     return CPDF_Annot::Subtype::FREETEXT;
292   if (sSubtype == "Line")
293     return CPDF_Annot::Subtype::LINE;
294   if (sSubtype == "Square")
295     return CPDF_Annot::Subtype::SQUARE;
296   if (sSubtype == "Circle")
297     return CPDF_Annot::Subtype::CIRCLE;
298   if (sSubtype == "Polygon")
299     return CPDF_Annot::Subtype::POLYGON;
300   if (sSubtype == "PolyLine")
301     return CPDF_Annot::Subtype::POLYLINE;
302   if (sSubtype == "Highlight")
303     return CPDF_Annot::Subtype::HIGHLIGHT;
304   if (sSubtype == "Underline")
305     return CPDF_Annot::Subtype::UNDERLINE;
306   if (sSubtype == "Squiggly")
307     return CPDF_Annot::Subtype::SQUIGGLY;
308   if (sSubtype == "StrikeOut")
309     return CPDF_Annot::Subtype::STRIKEOUT;
310   if (sSubtype == "Stamp")
311     return CPDF_Annot::Subtype::STAMP;
312   if (sSubtype == "Caret")
313     return CPDF_Annot::Subtype::CARET;
314   if (sSubtype == "Ink")
315     return CPDF_Annot::Subtype::INK;
316   if (sSubtype == "Popup")
317     return CPDF_Annot::Subtype::POPUP;
318   if (sSubtype == "FileAttachment")
319     return CPDF_Annot::Subtype::FILEATTACHMENT;
320   if (sSubtype == "Sound")
321     return CPDF_Annot::Subtype::SOUND;
322   if (sSubtype == "Movie")
323     return CPDF_Annot::Subtype::MOVIE;
324   if (sSubtype == "Widget")
325     return CPDF_Annot::Subtype::WIDGET;
326   if (sSubtype == "Screen")
327     return CPDF_Annot::Subtype::SCREEN;
328   if (sSubtype == "PrinterMark")
329     return CPDF_Annot::Subtype::PRINTERMARK;
330   if (sSubtype == "TrapNet")
331     return CPDF_Annot::Subtype::TRAPNET;
332   if (sSubtype == "Watermark")
333     return CPDF_Annot::Subtype::WATERMARK;
334   if (sSubtype == "3D")
335     return CPDF_Annot::Subtype::THREED;
336   if (sSubtype == "RichMedia")
337     return CPDF_Annot::Subtype::RICHMEDIA;
338   if (sSubtype == "XFAWidget")
339     return CPDF_Annot::Subtype::XFAWIDGET;
340   if (sSubtype == "Redact")
341     return CPDF_Annot::Subtype::REDACT;
342   return CPDF_Annot::Subtype::UNKNOWN;
343 }
344 
345 // static
AnnotSubtypeToString(CPDF_Annot::Subtype nSubtype)346 ByteString CPDF_Annot::AnnotSubtypeToString(CPDF_Annot::Subtype nSubtype) {
347   if (nSubtype == CPDF_Annot::Subtype::TEXT)
348     return "Text";
349   if (nSubtype == CPDF_Annot::Subtype::LINK)
350     return "Link";
351   if (nSubtype == CPDF_Annot::Subtype::FREETEXT)
352     return "FreeText";
353   if (nSubtype == CPDF_Annot::Subtype::LINE)
354     return "Line";
355   if (nSubtype == CPDF_Annot::Subtype::SQUARE)
356     return "Square";
357   if (nSubtype == CPDF_Annot::Subtype::CIRCLE)
358     return "Circle";
359   if (nSubtype == CPDF_Annot::Subtype::POLYGON)
360     return "Polygon";
361   if (nSubtype == CPDF_Annot::Subtype::POLYLINE)
362     return "PolyLine";
363   if (nSubtype == CPDF_Annot::Subtype::HIGHLIGHT)
364     return "Highlight";
365   if (nSubtype == CPDF_Annot::Subtype::UNDERLINE)
366     return "Underline";
367   if (nSubtype == CPDF_Annot::Subtype::SQUIGGLY)
368     return "Squiggly";
369   if (nSubtype == CPDF_Annot::Subtype::STRIKEOUT)
370     return "StrikeOut";
371   if (nSubtype == CPDF_Annot::Subtype::STAMP)
372     return "Stamp";
373   if (nSubtype == CPDF_Annot::Subtype::CARET)
374     return "Caret";
375   if (nSubtype == CPDF_Annot::Subtype::INK)
376     return "Ink";
377   if (nSubtype == CPDF_Annot::Subtype::POPUP)
378     return "Popup";
379   if (nSubtype == CPDF_Annot::Subtype::FILEATTACHMENT)
380     return "FileAttachment";
381   if (nSubtype == CPDF_Annot::Subtype::SOUND)
382     return "Sound";
383   if (nSubtype == CPDF_Annot::Subtype::MOVIE)
384     return "Movie";
385   if (nSubtype == CPDF_Annot::Subtype::WIDGET)
386     return "Widget";
387   if (nSubtype == CPDF_Annot::Subtype::SCREEN)
388     return "Screen";
389   if (nSubtype == CPDF_Annot::Subtype::PRINTERMARK)
390     return "PrinterMark";
391   if (nSubtype == CPDF_Annot::Subtype::TRAPNET)
392     return "TrapNet";
393   if (nSubtype == CPDF_Annot::Subtype::WATERMARK)
394     return "Watermark";
395   if (nSubtype == CPDF_Annot::Subtype::THREED)
396     return "3D";
397   if (nSubtype == CPDF_Annot::Subtype::RICHMEDIA)
398     return "RichMedia";
399   if (nSubtype == CPDF_Annot::Subtype::XFAWIDGET)
400     return "XFAWidget";
401   if (nSubtype == CPDF_Annot::Subtype::REDACT)
402     return "Redact";
403   return ByteString();
404 }
405 
406 // static
QuadPointCount(const CPDF_Array * pArray)407 size_t CPDF_Annot::QuadPointCount(const CPDF_Array* pArray) {
408   return pArray->size() / 8;
409 }
410 
DrawAppearance(CPDF_Page * pPage,CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device,AppearanceMode mode)411 bool CPDF_Annot::DrawAppearance(CPDF_Page* pPage,
412                                 CFX_RenderDevice* pDevice,
413                                 const CFX_Matrix& mtUser2Device,
414                                 AppearanceMode mode) {
415   if (!ShouldDrawAnnotation())
416     return false;
417 
418   // It might happen that by the time this annotation instance was created,
419   // it was flagged as "hidden" (e.g. /F 2), and hence CPDF_GenerateAP decided
420   // to not "generate" its AP.
421   // If for a reason the object is no longer hidden, but still does not have
422   // its "AP" generated, generate it now.
423   GenerateAPIfNeeded();
424 
425   CFX_Matrix matrix;
426   CPDF_Form* pForm = AnnotGetMatrix(pPage, this, mode, mtUser2Device, &matrix);
427   if (!pForm)
428     return false;
429 
430   CPDF_RenderContext context(pPage->GetDocument(),
431                              pPage->GetMutablePageResources(),
432                              pPage->GetPageImageCache());
433   context.AppendLayer(pForm, matrix);
434   context.Render(pDevice, nullptr, nullptr, nullptr);
435   return true;
436 }
437 
DrawInContext(CPDF_Page * pPage,CPDF_RenderContext * pContext,const CFX_Matrix & mtUser2Device,AppearanceMode mode)438 bool CPDF_Annot::DrawInContext(CPDF_Page* pPage,
439                                CPDF_RenderContext* pContext,
440                                const CFX_Matrix& mtUser2Device,
441                                AppearanceMode mode) {
442   if (!ShouldDrawAnnotation())
443     return false;
444 
445   // It might happen that by the time this annotation instance was created,
446   // it was flagged as "hidden" (e.g. /F 2), and hence CPDF_GenerateAP decided
447   // to not "generate" its AP.
448   // If for a reason the object is no longer hidden, but still does not have
449   // its "AP" generated, generate it now.
450   GenerateAPIfNeeded();
451 
452   CFX_Matrix matrix;
453   CPDF_Form* pForm = AnnotGetMatrix(pPage, this, mode, mtUser2Device, &matrix);
454   if (!pForm)
455     return false;
456 
457   pContext->AppendLayer(pForm, matrix);
458   return true;
459 }
460 
DrawBorder(CFX_RenderDevice * pDevice,const CFX_Matrix * pUser2Device)461 void CPDF_Annot::DrawBorder(CFX_RenderDevice* pDevice,
462                             const CFX_Matrix* pUser2Device) {
463   if (GetSubtype() == CPDF_Annot::Subtype::POPUP)
464     return;
465 
466   uint32_t annot_flags = GetFlags();
467   if (annot_flags & pdfium::annotation_flags::kHidden)
468     return;
469 
470   bool bPrinting = pDevice->GetDeviceType() == DeviceType::kPrinter;
471   if (bPrinting && (annot_flags & pdfium::annotation_flags::kPrint) == 0) {
472     return;
473   }
474   if (!bPrinting && (annot_flags & pdfium::annotation_flags::kNoView)) {
475     return;
476   }
477   RetainPtr<const CPDF_Dictionary> pBS = m_pAnnotDict->GetDictFor("BS");
478   char style_char;
479   float width;
480   RetainPtr<const CPDF_Array> pDashArray;
481   if (!pBS) {
482     RetainPtr<const CPDF_Array> pBorderArray =
483         m_pAnnotDict->GetArrayFor(pdfium::annotation::kBorder);
484     style_char = 'S';
485     if (pBorderArray) {
486       width = pBorderArray->GetFloatAt(2);
487       if (pBorderArray->size() == 4) {
488         pDashArray = pBorderArray->GetArrayAt(3);
489         if (!pDashArray) {
490           return;
491         }
492         size_t nLen = pDashArray->size();
493         size_t i = 0;
494         for (; i < nLen; ++i) {
495           RetainPtr<const CPDF_Object> pObj = pDashArray->GetDirectObjectAt(i);
496           if (pObj && pObj->GetInteger()) {
497             break;
498           }
499         }
500         if (i == nLen) {
501           return;
502         }
503         style_char = 'D';
504       }
505     } else {
506       width = 1;
507     }
508   } else {
509     ByteString style = pBS->GetByteStringFor("S");
510     pDashArray = pBS->GetArrayFor("D");
511     style_char = style[0];
512     width = pBS->GetFloatFor("W");
513   }
514   if (width <= 0) {
515     return;
516   }
517   RetainPtr<const CPDF_Array> pColor =
518       m_pAnnotDict->GetArrayFor(pdfium::annotation::kC);
519   uint32_t argb = 0xff000000;
520   if (pColor) {
521     int R = static_cast<int32_t>(pColor->GetFloatAt(0) * 255);
522     int G = static_cast<int32_t>(pColor->GetFloatAt(1) * 255);
523     int B = static_cast<int32_t>(pColor->GetFloatAt(2) * 255);
524     argb = ArgbEncode(0xff, R, G, B);
525   }
526   CFX_GraphStateData graph_state;
527   graph_state.m_LineWidth = width;
528   if (style_char == 'U') {
529     // TODO(https://crbug.com/237527): Handle the "Underline" border style
530     // instead of drawing the rectangle border.
531     return;
532   }
533 
534   if (style_char == 'D') {
535     if (pDashArray) {
536       graph_state.m_DashArray =
537           ReadArrayElementsToVector(pDashArray.Get(), pDashArray->size());
538       if (graph_state.m_DashArray.size() % 2)
539         graph_state.m_DashArray.push_back(graph_state.m_DashArray.back());
540     } else {
541       graph_state.m_DashArray = {3.0f, 3.0f};
542     }
543   }
544 
545   CFX_FloatRect rect = GetRect();
546   rect.Deflate(width / 2, width / 2);
547 
548   CFX_Path path;
549   path.AppendFloatRect(rect);
550   pDevice->DrawPath(path, pUser2Device, &graph_state, argb, argb,
551                     CFX_FillRenderOptions());
552 }
553