Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/fpdfdoc/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ source_set("fpdfdoc") {
"cpdf_action.h",
"cpdf_annot.cpp",
"cpdf_annot.h",
# EmbedPDF: the one reading of an annotation's border (/BS, or else
# /Border), for the appearance generator and the EPDFAnnot_* readers.
"cpdf_annot_border.cpp",
"cpdf_annot_border.h",
"cpdf_annot_rotation.cpp",
"cpdf_annot_rotation.h",
"cpdf_embed_metadata.cpp",
Expand Down Expand Up @@ -156,6 +160,7 @@ source_set("fpdfdoc") {
pdfium_unittest_source_set("unittests") {
sources = [
"cpdf_action_unittest.cpp",
"cpdf_annot_border_unittest.cpp",
"cpdf_annot_unittest.cpp",
"cpdf_annotlist_unittest.cpp",
"cpdf_bafontmap_unittest.cpp",
Expand Down
51 changes: 51 additions & 0 deletions core/fpdfdoc/cpdf_annot_border.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2026 CloudPDF LTD
// SPDX-License-Identifier: Apache-2.0

#include "core/fpdfdoc/cpdf_annot_border.h"

#include "constants/annotation_common.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"

namespace fpdfdoc {

AnnotBorder ReadAnnotBorder(const CPDF_Dictionary* annot_dict) {
AnnotBorder border;

RetainPtr<const CPDF_Dictionary> border_style = annot_dict->GetDictFor("BS");
if (border_style) {
if (border_style->KeyExist("W")) {
border.width = border_style->GetFloatFor("W");
}
const CPDF_Annot::BorderStyle style =
CPDF_Annot::StringToBorderStyle(border_style->GetByteStringFor("S"));
if (style != CPDF_Annot::BorderStyle::kUnknown) {
border.style = style;
}
border.dash_array = border_style->GetArrayFor("D");
if (border.style == CPDF_Annot::BorderStyle::kDashed && border.dash_array &&
border.dash_array->IsEmpty()) {
border.style = CPDF_Annot::BorderStyle::kSolid;
}
return border;
}

RetainPtr<const CPDF_Array> border_array =
annot_dict->GetArrayFor(pdfium::annotation::kBorder);
if (!border_array) {
return border;
}
if (border_array->size() > 2) {
border.width = border_array->GetFloatAt(2);
}
// The dash array is the fourth and last entry.
if (border_array->size() == 4) {
border.dash_array = border_array->GetArrayAt(3);
if (border.dash_array && !border.dash_array->IsEmpty()) {
border.style = CPDF_Annot::BorderStyle::kDashed;
}
}
return border;
}

} // namespace fpdfdoc
43 changes: 43 additions & 0 deletions core/fpdfdoc/cpdf_annot_border.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2026 CloudPDF LTD
// SPDX-License-Identifier: Apache-2.0

#ifndef CORE_FPDFDOC_CPDF_ANNOT_BORDER_H_
#define CORE_FPDFDOC_CPDF_ANNOT_BORDER_H_

#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfdoc/cpdf_annot.h"
#include "core/fxcrt/retain_ptr.h"

class CPDF_Dictionary;

namespace fpdfdoc {

// An annotation's border, read by the one rule of ISO 32000-1 12.5.2: its
// /BS when it has one, and /Border is then not read at all; else
// /Border [h v w [dash]]; else a solid border 1 wide.
//
// Our code reads a border here and nowhere else: the appearance generator
// draws every kind from this reading, and EPDFAnnot_GetBorderStyle() and the
// dash pattern readers report it. So a border is drawn as it reads,
// whichever key holds it.
struct AnnotBorder {
// /BS /W, or /Border's third number; 1 when the key that is read has none.
float width = 1.0f;

// /BS /S, solid when it is left out or is no name ISO 32000 defines. A
// /Border is dashed when its dash array holds a pattern. A dashed border
// whose pattern is an empty array draws a solid line (8.4.3.6) and is
// solid here. Never kUnknown.
CPDF_Annot::BorderStyle style = CPDF_Annot::BorderStyle::kSolid;

// The pattern as stored, whatever the style: /BS /D, or /Border's dash
// array. Null when none is stored; a dashed border then draws ISO's
// default dash.
RetainPtr<const CPDF_Array> dash_array;
};

AnnotBorder ReadAnnotBorder(const CPDF_Dictionary* annot_dict);

} // namespace fpdfdoc

#endif // CORE_FPDFDOC_CPDF_ANNOT_BORDER_H_
217 changes: 217 additions & 0 deletions core/fpdfdoc/cpdf_annot_border_unittest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Copyright 2026 CloudPDF LTD
// SPDX-License-Identifier: Apache-2.0

#include "core/fpdfdoc/cpdf_annot_border.h"

#include <initializer_list>
#include <optional>
#include <vector>

#include "constants/annotation_common.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_name.h"
#include "core/fpdfapi/parser/cpdf_number.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

using BorderStyle = CPDF_Annot::BorderStyle;

RetainPtr<CPDF_Array> Numbers(std::initializer_list<float> values) {
auto array = pdfium::MakeRetain<CPDF_Array>();
for (float value : values) {
array->AppendNew<CPDF_Number>(value);
}
return array;
}

// /BS on a new annotation: /W |width|, /S |style| and /D |dash|, each when
// given.
RetainPtr<CPDF_Dictionary> WithBorderStyle(
std::optional<float> width,
const char* style,
std::optional<std::vector<float>> dash) {
auto annot_dict = pdfium::MakeRetain<CPDF_Dictionary>();
auto border_style = annot_dict->SetNewFor<CPDF_Dictionary>("BS");
if (width.has_value()) {
border_style->SetNewFor<CPDF_Number>("W", width.value());
}
if (style) {
border_style->SetNewFor<CPDF_Name>("S", style);
}
if (dash.has_value()) {
auto dash_array = border_style->SetNewFor<CPDF_Array>("D");
for (float value : dash.value()) {
dash_array->AppendNew<CPDF_Number>(value);
}
}
return annot_dict;
}

// /Border |border| on a new annotation.
RetainPtr<CPDF_Dictionary> WithBorderArray(RetainPtr<CPDF_Array> border) {
auto annot_dict = pdfium::MakeRetain<CPDF_Dictionary>();
annot_dict->SetFor(pdfium::annotation::kBorder, std::move(border));
return annot_dict;
}

// The pattern |border| stores; empty when it stores none or an empty one.
std::vector<float> StoredPattern(const fpdfdoc::AnnotBorder& border) {
std::vector<float> values;
if (border.dash_array) {
for (size_t i = 0; i < border.dash_array->size(); ++i) {
values.push_back(border.dash_array->GetFloatAt(i));
}
}
return values;
}

} // namespace

TEST(CPDFAnnotBorderTest, NeitherKeyIsSolidAndOneWide) {
auto annot_dict = pdfium::MakeRetain<CPDF_Dictionary>();
const fpdfdoc::AnnotBorder border =
fpdfdoc::ReadAnnotBorder(annot_dict.Get());
EXPECT_EQ(1.0f, border.width);
EXPECT_EQ(BorderStyle::kSolid, border.style);
EXPECT_FALSE(border.dash_array);
}

TEST(CPDFAnnotBorderTest, BorderStyleWidthAndNames) {
// What /BS leaves out takes its default: 1 wide, solid.
fpdfdoc::AnnotBorder border = fpdfdoc::ReadAnnotBorder(
WithBorderStyle(std::nullopt, nullptr, std::nullopt).Get());
EXPECT_EQ(1.0f, border.width);
EXPECT_EQ(BorderStyle::kSolid, border.style);

border = fpdfdoc::ReadAnnotBorder(
WithBorderStyle(0.0f, nullptr, std::nullopt).Get());
EXPECT_EQ(0.0f, border.width);

struct NameCase {
const char* name;
BorderStyle style;
};
static constexpr NameCase kNames[] = {
{"S", BorderStyle::kSolid},
{"D", BorderStyle::kDashed},
{"B", BorderStyle::kBeveled},
{"I", BorderStyle::kInset},
{"U", BorderStyle::kUnderline},
// A name ISO 32000 doesn't define is the default, solid, whatever its
// first letter.
{"Dashed", BorderStyle::kSolid},
{"Underline", BorderStyle::kSolid},
{"X", BorderStyle::kSolid},
};
for (const NameCase& name_case : kNames) {
SCOPED_TRACE(name_case.name);
border = fpdfdoc::ReadAnnotBorder(
WithBorderStyle(2.5f, name_case.name, std::nullopt).Get());
EXPECT_EQ(name_case.style, border.style);
// The width as written, for a beveled or inset border too.
EXPECT_EQ(2.5f, border.width);
}
}

TEST(CPDFAnnotBorderTest, BorderStylePattern) {
// Dashed without /D: no pattern stored, and it draws the default dash.
fpdfdoc::AnnotBorder border =
fpdfdoc::ReadAnnotBorder(WithBorderStyle(2, "D", std::nullopt).Get());
EXPECT_EQ(BorderStyle::kDashed, border.style);
EXPECT_FALSE(border.dash_array);

border = fpdfdoc::ReadAnnotBorder(
WithBorderStyle(2, "D", std::vector<float>{4, 2}).Get());
EXPECT_EQ(BorderStyle::kDashed, border.style);
EXPECT_EQ((std::vector<float>{4, 2}), StoredPattern(border));

// An empty pattern is a solid line (ISO 32000 8.4.3.6).
border = fpdfdoc::ReadAnnotBorder(
WithBorderStyle(2, "D", std::vector<float>{}).Get());
EXPECT_EQ(BorderStyle::kSolid, border.style);
ASSERT_TRUE(border.dash_array);
EXPECT_TRUE(border.dash_array->IsEmpty());

// A pattern is stored whatever the style; only a dashed border draws it.
for (const char* style : {"S", "B", "I", "U"}) {
SCOPED_TRACE(style);
border = fpdfdoc::ReadAnnotBorder(
WithBorderStyle(2, style, std::vector<float>{4, 2}).Get());
EXPECT_NE(BorderStyle::kDashed, border.style);
EXPECT_EQ((std::vector<float>{4, 2}), StoredPattern(border));
}
}

TEST(CPDFAnnotBorderTest, BorderArray) {
fpdfdoc::AnnotBorder border =
fpdfdoc::ReadAnnotBorder(WithBorderArray(Numbers({0, 0, 5})).Get());
EXPECT_EQ(5.0f, border.width);
EXPECT_EQ(BorderStyle::kSolid, border.style);
EXPECT_FALSE(border.dash_array);

border = fpdfdoc::ReadAnnotBorder(WithBorderArray(Numbers({0, 0, 0})).Get());
EXPECT_EQ(0.0f, border.width);

// The corner radii are not part of what is read.
border = fpdfdoc::ReadAnnotBorder(WithBorderArray(Numbers({3, 3, 5})).Get());
EXPECT_EQ(5.0f, border.width);
EXPECT_EQ(BorderStyle::kSolid, border.style);

// Too short to hold a width: the default.
border = fpdfdoc::ReadAnnotBorder(WithBorderArray(Numbers({0, 0})).Get());
EXPECT_EQ(1.0f, border.width);

// A dash array with a pattern makes the border dashed.
RetainPtr<CPDF_Array> dashed = Numbers({0, 0, 5});
dashed->Append(Numbers({4, 2}));
border = fpdfdoc::ReadAnnotBorder(WithBorderArray(dashed).Get());
EXPECT_EQ(5.0f, border.width);
EXPECT_EQ(BorderStyle::kDashed, border.style);
EXPECT_EQ((std::vector<float>{4, 2}), StoredPattern(border));

// An empty one is a solid line.
RetainPtr<CPDF_Array> empty_dash = Numbers({0, 0, 5});
empty_dash->Append(Numbers({}));
border = fpdfdoc::ReadAnnotBorder(WithBorderArray(empty_dash).Get());
EXPECT_EQ(BorderStyle::kSolid, border.style);
ASSERT_TRUE(border.dash_array);
EXPECT_TRUE(border.dash_array->IsEmpty());

// A fourth entry that is no array is no dash.
border =
fpdfdoc::ReadAnnotBorder(WithBorderArray(Numbers({0, 0, 5, 7})).Get());
EXPECT_EQ(5.0f, border.width);
EXPECT_EQ(BorderStyle::kSolid, border.style);
EXPECT_FALSE(border.dash_array);

// The dash array is the last of four entries: with more, the width is
// still read and no dash is.
RetainPtr<CPDF_Array> five = Numbers({0, 0, 5});
five->Append(Numbers({4, 2}));
five->AppendNew<CPDF_Number>(9);
border = fpdfdoc::ReadAnnotBorder(WithBorderArray(five).Get());
EXPECT_EQ(5.0f, border.width);
EXPECT_EQ(BorderStyle::kSolid, border.style);
EXPECT_FALSE(border.dash_array);
}

TEST(CPDFAnnotBorderTest, BorderStyleWinsOverBorderArray) {
// An empty /BS is the default border; /Border beside it is not read.
RetainPtr<CPDF_Array> dashed = Numbers({0, 0, 5});
dashed->Append(Numbers({4, 2}));
RetainPtr<CPDF_Dictionary> annot_dict =
WithBorderStyle(std::nullopt, nullptr, std::nullopt);
annot_dict->SetFor(pdfium::annotation::kBorder, dashed);
fpdfdoc::AnnotBorder border = fpdfdoc::ReadAnnotBorder(annot_dict.Get());
EXPECT_EQ(1.0f, border.width);
EXPECT_EQ(BorderStyle::kSolid, border.style);
EXPECT_FALSE(border.dash_array);

// A /BS that is no dictionary is no /BS.
annot_dict->SetNewFor<CPDF_Number>("BS", 4);
border = fpdfdoc::ReadAnnotBorder(annot_dict.Get());
EXPECT_EQ(5.0f, border.width);
EXPECT_EQ(BorderStyle::kDashed, border.style);
}
Loading