From 53d4f0a4526ef996caf5005ae84406a9467423f2 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Wed, 1 Aug 2018 01:28:49 +0000 Subject: Add FPDFText_GetFontName() API This follows the same pattern as DefaultGetFaceName(), so the client has to call this function twice, but allocation of the string buffer happens outside pdfium. Change-Id: I06b7dcd00aca9b9b94799dad3f139617d7f5451e Reviewed-on: https://pdfium-review.googlesource.com/38870 Commit-Queue: Lei Zhang Reviewed-by: Lei Zhang Reviewed-by: Henrique Nakashima --- fpdfsdk/fpdf_edit_embeddertest.cpp | 28 ++++++++++++++++++++++++++++ fpdfsdk/fpdf_edittext.cpp | 22 ++++++++++++++++++++++ fpdfsdk/fpdf_view_c_api_test.c | 1 + public/fpdf_edit.h | 18 ++++++++++++++++++ testing/resources/text_font.pdf | Bin 0 -> 10576 bytes 5 files changed, 69 insertions(+) create mode 100644 testing/resources/text_font.pdf diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp index c9805f50c7..3c2b05ed8a 100644 --- a/fpdfsdk/fpdf_edit_embeddertest.cpp +++ b/fpdfsdk/fpdf_edit_embeddertest.cpp @@ -1764,6 +1764,34 @@ TEST_F(FPDFEditEmbeddertest, TestGetTextRenderMode) { UnloadPage(page); } +TEST_F(FPDFEditEmbeddertest, TestGetTextFontName) { + EXPECT_TRUE(OpenDocument("text_font.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + ASSERT_EQ(1, FPDFPage_CountObjects(page)); + + // FPDFTextObj_GetFontName() positive testing. + FPDF_PAGEOBJECT text = FPDFPage_GetObject(page, 0); + unsigned long size = FPDFTextObj_GetFontName(text, nullptr, 0); + const char kExpectedFontName[] = "Liberation Serif"; + ASSERT_EQ(sizeof(kExpectedFontName), size); + std::vector font_name(size); + ASSERT_EQ(size, FPDFTextObj_GetFontName(text, font_name.data(), size)); + ASSERT_STREQ(kExpectedFontName, font_name.data()); + + // FPDFTextObj_GetFontName() negative testing. + ASSERT_EQ(0U, FPDFTextObj_GetFontName(nullptr, nullptr, 0)); + + font_name.resize(2); + font_name[0] = 'x'; + font_name[1] = '\0'; + size = FPDFTextObj_GetFontName(text, font_name.data(), font_name.size()); + ASSERT_EQ(sizeof(kExpectedFontName), size); + ASSERT_EQ(std::string("x"), std::string(font_name.data())); + + UnloadPage(page); +} + TEST_F(FPDFEditEmbeddertest, TestFormGetObjects) { EXPECT_TRUE(OpenDocument("form_object.pdf")); FPDF_PAGE page = LoadPage(0); diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp index c552d615e4..6aa44b3b20 100644 --- a/fpdfsdk/fpdf_edittext.cpp +++ b/fpdfsdk/fpdf_edittext.cpp @@ -542,6 +542,28 @@ FPDF_EXPORT double FPDF_CALLCONV FPDFTextObj_GetFontSize(FPDF_PAGEOBJECT text) { return pTextObj ? pTextObj->GetFontSize() : 0; } +FPDF_EXPORT unsigned long FPDF_CALLCONV +FPDFTextObj_GetFontName(FPDF_PAGEOBJECT text, + void* buffer, + unsigned long length) { + CPDF_TextObject* pTextObj = CPDFTextObjectFromFPDFPageObject(text); + if (!pTextObj) + return 0; + + CPDF_Font* pPdfFont = pTextObj->GetFont(); + if (!pPdfFont) + return 0; + + CFX_Font* pFont = pPdfFont->GetFont(); + ASSERT(pFont); + + ByteString name = pFont->GetFamilyName(); + unsigned long dwStringLen = name.GetLength() + 1; + if (buffer && length >= dwStringLen) + memcpy(buffer, name.c_str(), dwStringLen); + return dwStringLen; +} + FPDF_EXPORT void FPDF_CALLCONV FPDFFont_Close(FPDF_FONT font) { CPDF_Font* pFont = CPDFFontFromFPDFFont(font); if (!pFont) diff --git a/fpdfsdk/fpdf_view_c_api_test.c b/fpdfsdk/fpdf_view_c_api_test.c index 4faf5bbf0f..91d24fd233 100644 --- a/fpdfsdk/fpdf_view_c_api_test.c +++ b/fpdfsdk/fpdf_view_c_api_test.c @@ -204,6 +204,7 @@ int CheckPDFiumCApi() { CHK(FPDFPath_SetMatrix); CHK(FPDFPath_SetStrokeColor); CHK(FPDFPath_SetStrokeWidth); + CHK(FPDFTextObj_GetFontName); CHK(FPDFTextObj_GetFontSize); CHK(FPDFText_GetMatrix); CHK(FPDFText_GetTextRenderMode); diff --git a/public/fpdf_edit.h b/public/fpdf_edit.h index b97a7adbd9..4d5aa9c48a 100644 --- a/public/fpdf_edit.h +++ b/public/fpdf_edit.h @@ -1256,6 +1256,24 @@ FPDFPageObj_CreateTextObj(FPDF_DOCUMENT document, // Returns one of the FPDF_TEXTRENDERMODE_* flags on success, -1 on error. FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetTextRenderMode(FPDF_PAGEOBJECT text); +// Experimental API. +// Get the font name of a text object. +// +// text - the handle to the text object. +// buffer - the address of a buffer that receives the font name. +// length - the size, in bytes, of |buffer|. +// +// Returns the number of bytes in the font name (including the trailing NUL +// character) on success, 0 on error. +// +// Regardless of the platform, the |buffer| is always in UTF-8 encoding. +// If |length| is less than the returned length, or |buffer| is NULL, |buffer| +// will not be modified. +FPDF_EXPORT unsigned long FPDF_CALLCONV +FPDFTextObj_GetFontName(FPDF_PAGEOBJECT text, + void* buffer, + unsigned long length); + // Experimental API. // Get number of page objects inside |form_object|. // diff --git a/testing/resources/text_font.pdf b/testing/resources/text_font.pdf new file mode 100644 index 0000000000..78f3bcc784 Binary files /dev/null and b/testing/resources/text_font.pdf differ -- cgit v1.2.3