Tidy CPDF_StructElement.
- Use a helper function to initialize |m_Type| so it can be const.
- Add GetAltText() method for consistency with GetTitle().
Change-Id: I507e2b5c18f4312eaefc96826a65e84c3a4725c5
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/54595
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_structelement.cpp b/core/fpdfdoc/cpdf_structelement.cpp
index 1e7e5dc..89a5e39 100644
--- a/core/fpdfdoc/cpdf_structelement.cpp
+++ b/core/fpdfdoc/cpdf_structelement.cpp
@@ -17,6 +17,21 @@
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "core/fpdfdoc/cpdf_structtree.h"
+namespace {
+
+ByteString GetStructElementType(CPDF_StructTree* pTree,
+ const CPDF_Dictionary* pDict) {
+ ByteString type = pDict->GetStringFor("S");
+ if (pTree->GetRoleMap()) {
+ ByteString mapped = pTree->GetRoleMap()->GetStringFor(type);
+ if (!mapped.IsEmpty())
+ type = std::move(mapped);
+ }
+ return type;
+}
+
+} // namespace
+
CPDF_StructKid::CPDF_StructKid()
: m_Type(Invalid),
m_PageObjNum(0),
@@ -33,17 +48,16 @@
: m_pTree(pTree),
m_pParent(pParent),
m_pDict(pDict),
- m_Type(pDict->GetStringFor("S")) {
- if (pTree->GetRoleMap()) {
- ByteString mapped = pTree->GetRoleMap()->GetStringFor(m_Type);
- if (!mapped.IsEmpty())
- m_Type = std::move(mapped);
- }
- LoadKids(pDict);
+ m_Type(GetStructElementType(m_pTree.Get(), m_pDict.Get())) {
+ LoadKids(m_pDict.Get());
}
CPDF_StructElement::~CPDF_StructElement() = default;
+WideString CPDF_StructElement::GetAltText() const {
+ return GetDict()->GetUnicodeTextFor("Alt");
+}
+
WideString CPDF_StructElement::GetTitle() const {
return GetDict()->GetUnicodeTextFor("T");
}
diff --git a/core/fpdfdoc/cpdf_structelement.h b/core/fpdfdoc/cpdf_structelement.h
index 359662e..d4981a5 100644
--- a/core/fpdfdoc/cpdf_structelement.h
+++ b/core/fpdfdoc/cpdf_structelement.h
@@ -39,6 +39,7 @@
friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
ByteString GetType() const { return m_Type; }
+ WideString GetAltText() const;
WideString GetTitle() const;
// Never returns nullptr.
@@ -62,7 +63,7 @@
UnownedPtr<CPDF_StructTree> const m_pTree;
UnownedPtr<CPDF_StructElement> const m_pParent;
UnownedPtr<const CPDF_Dictionary> const m_pDict;
- ByteString m_Type;
+ const ByteString m_Type;
std::vector<CPDF_StructKid> m_Kids;
};
diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp
index d0b0972..cfd57a8 100644
--- a/fpdfsdk/fpdf_structtree.cpp
+++ b/fpdfsdk/fpdf_structtree.cpp
@@ -74,9 +74,7 @@
unsigned long buflen) {
CPDF_StructElement* elem =
CPDFStructElementFromFPDFStructElement(struct_element);
- return elem ? WideStringToBuffer(elem->GetDict()->GetUnicodeTextFor("Alt"),
- buffer, buflen)
- : 0;
+ return elem ? WideStringToBuffer(elem->GetAltText(), buffer, buflen) : 0;
}
FPDF_EXPORT int FPDF_CALLCONV