PDF a11y: new API for font-weight
The font-weight is now exposed by FPDFText_GetWeight().
Bug: chromium:985604
Change-Id: Ic7161bf75e2c0f278a4e90567fbbaf42b16666e1
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/59670
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 28e5ffb..4b1ddcc 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -436,3 +436,12 @@
}
return false;
}
+
+int CPDF_Font::GetFontWeight() const {
+ pdfium::base::CheckedNumeric<int> safeStemV(m_StemV);
+ if (m_StemV < 140)
+ safeStemV *= 5;
+ else
+ safeStemV = safeStemV * 4 + 140;
+ return safeStemV.ValueOrDefault(FXFONT_FW_NORMAL);
+}
diff --git a/core/fpdfapi/font/cpdf_font.h b/core/fpdfapi/font/cpdf_font.h
index 1641bb3..db776fd 100644
--- a/core/fpdfapi/font/cpdf_font.h
+++ b/core/fpdfapi/font/cpdf_font.h
@@ -111,6 +111,7 @@
uint32_t FallbackFontFromCharcode(uint32_t charcode);
int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
int GetFontFlags() const { return m_Flags; }
+ int GetFontWeight() const;
virtual uint32_t GetCharWidthF(uint32_t charcode) = 0;
virtual FX_RECT GetCharBBox(uint32_t charcode) = 0;
diff --git a/core/fpdfapi/font/cpdf_simplefont.cpp b/core/fpdfapi/font/cpdf_simplefont.cpp
index 546ed28..cff773a 100644
--- a/core/fpdfapi/font/cpdf_simplefont.cpp
+++ b/core/fpdfapi/font/cpdf_simplefont.cpp
@@ -262,14 +262,8 @@
if (i == 256 && width)
m_Flags |= FXFONT_FIXED_PITCH;
}
- pdfium::base::CheckedNumeric<int> safeStemV(m_StemV);
- if (m_StemV < 140)
- safeStemV *= 5;
- else
- safeStemV = safeStemV * 4 + 140;
- m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags,
- safeStemV.ValueOrDefault(FXFONT_FW_NORMAL), m_ItalicAngle, 0,
- false);
+ m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags, GetFontWeight(),
+ m_ItalicAngle, 0, false);
}
bool CPDF_SimpleFont::IsUnicodeCompatible() const {
diff --git a/fpdfsdk/fpdf_text.cpp b/fpdfsdk/fpdf_text.cpp
index 793d224..1b39fea 100644
--- a/fpdfsdk/fpdf_text.cpp
+++ b/fpdfsdk/fpdf_text.cpp
@@ -118,6 +118,20 @@
return length;
}
+FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetFontWeight(FPDF_TEXTPAGE text_page,
+ int index) {
+ CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
+ if (!textpage)
+ return -1;
+
+ FPDF_CHAR_INFO charinfo;
+ textpage->GetCharInfo(index, &charinfo);
+ if (!charinfo.m_pTextObj)
+ return -1;
+
+ return charinfo.m_pTextObj->GetFont()->GetFontWeight();
+}
+
FPDF_EXPORT double FPDF_CALLCONV FPDFText_GetCharAngle(FPDF_TEXTPAGE text_page,
int index) {
CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
diff --git a/fpdfsdk/fpdf_text_embeddertest.cpp b/fpdfsdk/fpdf_text_embeddertest.cpp
index 521831e..8f00b60 100644
--- a/fpdfsdk/fpdf_text_embeddertest.cpp
+++ b/fpdfsdk/fpdf_text_embeddertest.cpp
@@ -1251,3 +1251,26 @@
FPDFText_ClosePage(text_page);
UnloadPage(page);
}
+
+TEST_F(FPDFTextEmbedderTest, GetFontWeight) {
+ ASSERT_TRUE(OpenDocument("font_weight.pdf"));
+ FPDF_PAGE page = LoadPage(0);
+ ASSERT_TRUE(page);
+
+ FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
+ ASSERT_TRUE(text_page);
+
+ EXPECT_EQ(1, FPDFText_CountChars(text_page));
+
+ EXPECT_EQ(-1, FPDFText_GetFontWeight(nullptr, 0));
+ EXPECT_EQ(-1, FPDFText_GetFontWeight(text_page, -1));
+ EXPECT_EQ(-1, FPDFText_GetFontWeight(text_page, 314));
+
+ // The font used for this text only specifies /StemV (80); the weight value
+ // that is returned should be calculated from that (80*5 == 400).
+ int weight = FPDFText_GetFontWeight(text_page, 0);
+ EXPECT_EQ(400, weight);
+
+ FPDFText_ClosePage(text_page);
+ UnloadPage(page);
+}
diff --git a/fpdfsdk/fpdf_view_c_api_test.c b/fpdfsdk/fpdf_view_c_api_test.c
index 396b6d9..6add7a9 100644
--- a/fpdfsdk/fpdf_view_c_api_test.c
+++ b/fpdfsdk/fpdf_view_c_api_test.c
@@ -338,6 +338,7 @@
CHK(FPDFText_GetCharOrigin);
CHK(FPDFText_GetFontInfo);
CHK(FPDFText_GetFontSize);
+ CHK(FPDFText_GetFontWeight);
CHK(FPDFText_GetRect);
CHK(FPDFText_GetSchCount);
CHK(FPDFText_GetSchResultIndex);
diff --git a/public/fpdf_text.h b/public/fpdf_text.h
index 63793bb..6e454d1 100644
--- a/public/fpdf_text.h
+++ b/public/fpdf_text.h
@@ -113,6 +113,21 @@
int* flags);
// Experimental API.
+// Function: FPDFText_GetFontWeight
+// Get the font weight of a particular character.
+// Parameters:
+// text_page - Handle to a text page information structure.
+// Returned by FPDFText_LoadPage function.
+// index - Zero-based index of the character.
+// Return value:
+// On success, return the font weight of the particular character. If
+// |text_page| is invalid, if |index| is out of bounds, or if the
+// character's text object is undefined, return -1.
+//
+FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetFontWeight(FPDF_TEXTPAGE text_page,
+ int index);
+
+// Experimental API.
// Function: FPDFText_GetCharAngle
// Get character rotation angle.
// Parameters:
diff --git a/testing/resources/font_weight.in b/testing/resources/font_weight.in
new file mode 100644
index 0000000..fa7336b
--- /dev/null
+++ b/testing/resources/font_weight.in
@@ -0,0 +1,58 @@
+{{header}}
+{{object 1 0}} <<
+ /Type /Catalog
+ /Pages 2 0 R
+>>
+endobj
+{{object 2 0}} <<
+ /Type /Pages
+ /MediaBox [ 0 0 200 200 ]
+ /Count 1
+ /Kids [ 3 0 R ]
+>>
+endobj
+{{object 3 0}} <<
+ /Type /Page
+ /Parent 2 0 R
+ /Resources <<
+ /Font <<
+ /F1 4 0 R
+ >>
+ >>
+ /Contents 6 0 R
+>>
+endobj
+{{object 4 0}} <<
+ /Type /Font
+ /Subtype /Type1
+ /BaseFont /Helvetica
+ /FontDescriptor 5 0 R
+>>
+endobj
+{{object 5 0}} <<
+ /Type /FontDescriptor
+ /FontName /Helvetica
+ /StemV 80
+ /ItalicAngle 0
+ /Ascent 776
+ /Flags 0
+ /FontBBox [-250 -236 2827 1000]
+ /CapHeight 763
+ /Descent -223
+>>
+endobj
+{{object 6 0}} <<
+ {{streamlen}}
+>>
+stream
+BT
+0 30 Td
+/F1 12 Tf
+(1) Tj
+ET
+endstream
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/font_weight.pdf b/testing/resources/font_weight.pdf
new file mode 100644
index 0000000..851aa49
--- /dev/null
+++ b/testing/resources/font_weight.pdf
@@ -0,0 +1,71 @@
+%PDF-1.7
+% ò¤ô
+1 0 obj <<
+ /Type /Catalog
+ /Pages 2 0 R
+>>
+endobj
+2 0 obj <<
+ /Type /Pages
+ /MediaBox [ 0 0 200 200 ]
+ /Count 1
+ /Kids [ 3 0 R ]
+>>
+endobj
+3 0 obj <<
+ /Type /Page
+ /Parent 2 0 R
+ /Resources <<
+ /Font <<
+ /F1 4 0 R
+ >>
+ >>
+ /Contents 6 0 R
+>>
+endobj
+4 0 obj <<
+ /Type /Font
+ /Subtype /Type1
+ /BaseFont /Helvetica
+ /FontDescriptor 5 0 R
+>>
+endobj
+5 0 obj <<
+ /Type /FontDescriptor
+ /FontName /Helvetica
+ /StemV 80
+ /ItalicAngle 0
+ /Ascent 776
+ /Flags 0
+ /FontBBox [-250 -236 2827 1000]
+ /CapHeight 763
+ /Descent -223
+>>
+endobj
+6 0 obj <<
+ /Length 31
+>>
+stream
+BT
+0 30 Td
+/F1 12 Tf
+(1) Tj
+ET
+endstream
+endobj
+xref
+0 7
+0000000000 65535 f
+0000000015 00000 n
+0000000068 00000 n
+0000000161 00000 n
+0000000287 00000 n
+0000000387 00000 n
+0000000576 00000 n
+trailer <<
+ /Root 1 0 R
+ /Size 7
+>>
+startxref
+658
+%%EOF