From 73d9ea782768ce9c4303c86e03716967041cf03f Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Thu, 19 Nov 2026 01:15:22 +0200 Subject: [PATCH] Read a range of a text page's character geometry in one call The engine reads a page's text geometry one character at a time: for each character a call to FPDFText_GetTextObject and one to EPDFText_GetCharGeometry, and then 25 values of the struct, each read across the boundary on its own. Crossing into JavaScript that often costs more than the geometry itself. On a 1,310-page text document the calls and reads take 4.6 ms a page on the native runtime and 4.0 ms on WASM, more than parsing the page and building its text page together. EPDFText_GetPageGeometry(text_page, start_index, count, buffer) writes the geometry of a range of characters into one buffer. Each EPDF_PAGE_CHAR_GEOMETRY holds what the three calls answer for a character: its EPDF_CHAR_GEOMETRY, the run of consecutive characters with one text object it is in (object_run), and its font size. Runs count from the page's first character whatever the range, so pieces read one after another are the page. It carries no text, like the call it stands beside: geometry and text stay two reads. It takes a start and a count, as FPDFText_GetText does, so a caller reads a large page in bounded pieces and can stop between two of them. EPDFText_GetCharGeometry and the new call share one helper, so a character's geometry is the same from either. Tests: the records against the three calls for every character of eight documents (upright, turned, sheared, mirrored and vertical text, a font matrix, a Type3 font with a text object per glyph, a character that prints nothing) and of /ActualText on a page and through a form, read in one piece and in pieces; the runs of hello_world.pdf; ranges that end at the page's end or name no character; a page without text; an invalid text page. --- docs/embedpdf/fork-ledger.md | 11 +- fpdfsdk/epdf_c_api_test.c | 1 + fpdfsdk/epdf_text_embeddertest.cpp | 218 +++++++++++++++++++++++++++++ fpdfsdk/fpdf_text.cpp | 154 +++++++++++++------- public/epdf_text.h | 32 +++++ 5 files changed, 363 insertions(+), 53 deletions(-) diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index 66d49957f4..b39e3954e8 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -351,11 +351,12 @@ accept it in the report. ## Text ### text-geometry -**Rule.** Text pages expose each character's geometry (loose and tight boxes, quads, matrix), and -text markup reads oriented quads. -**Why.** Selection and markup on rotated and skewed text. -**Where.** `CPDF_TextPage`, `fpdf_text.cpp`, `EPDFText_GetCharGeometry`, quad reading in -`cpdf_annot.cpp`. +**Rule.** Text pages expose each character's geometry (loose and tight boxes, quads, matrix), one +character at a time or a range of characters in one call, and text markup reads oriented quads. +**Why.** Selection and markup on rotated and skewed text. A call per character costs more than the +geometry itself once it crosses into JavaScript, so a range of characters also comes in one call. +**Where.** `CPDF_TextPage`, `fpdf_text.cpp`, `EPDFText_GetCharGeometry`, +`EPDFText_GetPageGeometry`, quad reading in `cpdf_annot.cpp`. **Guarded by.** `epdf_text_bounds_embeddertest.cpp`, the text embedder tests. **When upstream touches this.** `CPDF_TextPage` character processing. diff --git a/fpdfsdk/epdf_c_api_test.c b/fpdfsdk/epdf_c_api_test.c index 28f5d75a4d..e714f176cb 100644 --- a/fpdfsdk/epdf_c_api_test.c +++ b/fpdfsdk/epdf_c_api_test.c @@ -659,6 +659,7 @@ int CheckEmbedPDFCApi(void) { // epdf_text.h CHK(EPDFText_GetCharGeometry); CHK(EPDFText_GetCharToTextMap); + CHK(EPDFText_GetPageGeometry); CHK(EPDFText_GetTextFull); // epdf_view.h diff --git a/fpdfsdk/epdf_text_embeddertest.cpp b/fpdfsdk/epdf_text_embeddertest.cpp index 8b64c96f0e..424d0d1252 100644 --- a/fpdfsdk/epdf_text_embeddertest.cpp +++ b/fpdfsdk/epdf_text_embeddertest.cpp @@ -5,7 +5,9 @@ #include "public/epdf_text.h" +#include #include +#include #include #include @@ -55,6 +57,53 @@ CFX_FloatRect BoundsFromQuad(const FS_QUADPOINTSF& quad) { return bounds; } +// EPDFText_GetPageGeometry gives, for every character, what the calls that +// take one character give, read in one piece and |piece| characters at a time. +void ExpectPageGeometryMatchesTheCharCalls(FPDF_TEXTPAGE text_page, int piece) { + const int char_count = FPDFText_CountChars(text_page); + ASSERT_GT(char_count, 0); + + std::vector records(static_cast(char_count)); + ASSERT_EQ(char_count, + EPDFText_GetPageGeometry(text_page, 0, char_count, records.data())); + + FPDF_PAGEOBJECT run_object = nullptr; + uint32_t run = 0; + for (int i = 0; i < char_count; ++i) { + const EPDF_PAGE_CHAR_GEOMETRY& record = records[static_cast(i)]; + + EPDF_CHAR_GEOMETRY geometry = {}; + ASSERT_TRUE(EPDFText_GetCharGeometry(text_page, i, &geometry)) << i; + EXPECT_EQ(0, memcmp(&geometry, &record.geometry, sizeof(geometry))) << i; + + FPDF_PAGEOBJECT object = FPDFText_GetTextObject(text_page, i); + if (i > 0 && object != run_object) { + ++run; + } + run_object = object; + EXPECT_EQ(run, record.object_run) << i; + + EXPECT_EQ(static_cast(FPDFText_GetFontSize(text_page, i)), + record.font_size) + << i; + } + + // A piece's runs count from the page's first character, so the pieces put + // together are the page. + std::vector buffer(static_cast(piece)); + std::vector pieces; + for (int start = 0; start < char_count; start += piece) { + const int expected = std::min(piece, char_count - start); + ASSERT_EQ(expected, + EPDFText_GetPageGeometry(text_page, start, piece, buffer.data())) + << start; + pieces.insert(pieces.end(), buffer.begin(), buffer.begin() + expected); + } + ASSERT_EQ(records.size(), pieces.size()); + EXPECT_EQ(0, memcmp(records.data(), pieces.data(), + records.size() * sizeof(EPDF_PAGE_CHAR_GEOMETRY))); +} + } // namespace class EPDFTextEmbedderTest : public EmbedderTest {}; @@ -429,6 +478,175 @@ TEST_F(EPDFTextEmbedderTest, GetVerticalCharGeometry) { EXPECT_FLOAT_EQ(legacy_loose.top, geometry.loose_box.top); } +// Upright, turned, sheared, mirrored and vertical text, a font with its own +// matrix, a Type3 font whose every glyph is a text object, and a character +// that prints nothing. +TEST_F(EPDFTextEmbedderTest, GetPageGeometryMatchesTheCharCalls) { + static constexpr const char* kDocuments[] = { + "hello_world.pdf", + "rotated_text.pdf", + "sheared_text.pdf", + "mirrored_text.pdf", + "embedpdf_vertical_text.pdf", + "font_matrix.pdf", + "embedpdf_chrome_type3_text.pdf", + "bug_1139.pdf", + }; + for (const char* document : kDocuments) { + SCOPED_TRACE(document); + ASSERT_TRUE(OpenDocument(document)); + { + ScopedPage page = LoadScopedPage(0); + ASSERT_TRUE(page); + ScopedFPDFTextPage text_page(FPDFText_LoadPage(page.get())); + ASSERT_TRUE(text_page); + ExpectPageGeometryMatchesTheCharCalls(text_page.get(), 7); + } + CloseDocument(); + } +} + +// /ActualText characters are synthesized: they stand in for the glyphs drawn, +// on the page and through a form. +TEST_F(EPDFTextEmbedderTest, GetPageGeometryMatchesTheCharCallsOnActualText) { + static constexpr char kSpan[] = + "/Span << /ActualText >> BDC (abcd) Tj EMC"; + const std::string pdf = MakePdf({ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 400] /Contents 4 0 R " + "/Resources << /Font << /F1 5 0 R >> /XObject << /Fm 6 0 R >> >> >>", + Stream(std::string("BT /F1 24 Tf 40 300 Td ") + kSpan + + " ET q 1 0 0 1 20 50 cm /Fm Do Q"), + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + Stream("/Type /XObject /Subtype /Form /BBox [0 0 200 100] " + "/Matrix [2 0 0 2 0 0] /Resources << /Font << /F1 5 0 R >> >>", + std::string("BT /F1 12 Tf 10 25 Td ") + kSpan + " ET"), + }); + embedpdf_test::ScopedTestDocument doc( + embedpdf_test::LoadMemDocumentForTest(pdf.data(), pdf.size(), nullptr)); + ASSERT_TRUE(doc); + ScopedFPDFPage page(FPDF_LoadPage(doc.get(), 0)); + ASSERT_TRUE(page); + ScopedFPDFTextPage text_page(FPDFText_LoadPage(page.get())); + ASSERT_TRUE(text_page); + + EPDF_PAGE_CHAR_GEOMETRY first = {}; + ASSERT_EQ(1, EPDFText_GetPageGeometry(text_page.get(), 0, 1, &first)); + EXPECT_TRUE(first.geometry.flags & EPDF_CHARGEO_SYNTHESIZED); + + ExpectPageGeometryMatchesTheCharCalls(text_page.get(), 3); +} + +// hello_world.pdf draws "Hello, world!" at 12 points and "Goodbye, world!" at +// 16, each as one text object. The line break between them is two generated +// characters, which have no text object: three runs. +TEST_F(EPDFTextEmbedderTest, GetPageGeometryRunsFollowTheTextObjects) { + ASSERT_TRUE(OpenDocument("hello_world.pdf")); + ScopedPage page = LoadScopedPage(0); + ASSERT_TRUE(page); + ScopedFPDFTextPage text_page(FPDFText_LoadPage(page.get())); + ASSERT_TRUE(text_page); + + static constexpr int kHello = std::char_traits::length("Hello, world!"); + static constexpr int kBreak = std::char_traits::length("\r\n"); + static constexpr int kGoodbye = + std::char_traits::length("Goodbye, world!"); + const int char_count = FPDFText_CountChars(text_page.get()); + ASSERT_EQ(kHello + kBreak + kGoodbye, char_count); + + std::vector records(static_cast(char_count)); + ASSERT_EQ(char_count, EPDFText_GetPageGeometry(text_page.get(), 0, char_count, + records.data())); + for (int i = 0; i < char_count; ++i) { + const uint32_t run = i < kHello ? 0u : i < kHello + kBreak ? 1u : 2u; + EXPECT_EQ(run, records[static_cast(i)].object_run) << i; + } + EXPECT_FLOAT_EQ(12.0f, records[0].font_size); + EXPECT_FLOAT_EQ(16.0f, + records[static_cast(kHello + kBreak)].font_size); + + // From the second character of the line break: its run is still the second + // of the page, and "Goodbye" keeps its own font size. + EPDF_PAGE_CHAR_GEOMETRY tail[3] = {}; + ASSERT_EQ(3, EPDFText_GetPageGeometry(text_page.get(), kHello + 1, 3, tail)); + EXPECT_EQ(1u, tail[0].object_run); + EXPECT_EQ(2u, tail[1].object_run); + EXPECT_EQ(2u, tail[2].object_run); + EXPECT_FLOAT_EQ(16.0f, tail[1].font_size); +} + +// A range gives the characters it names, stops at the last character, and +// writes nothing when it names none. +TEST_F(EPDFTextEmbedderTest, GetPageGeometryReadsARange) { + ASSERT_TRUE(OpenDocument("hello_world.pdf")); + ScopedPage page = LoadScopedPage(0); + ASSERT_TRUE(page); + ScopedFPDFTextPage text_page(FPDFText_LoadPage(page.get())); + ASSERT_TRUE(text_page); + + const int char_count = FPDFText_CountChars(text_page.get()); + ASSERT_GT(char_count, 8); + + static constexpr uint32_t kUntouched = 0xffffffffu; + EPDF_PAGE_CHAR_GEOMETRY untouched = {}; + untouched.object_run = kUntouched; + std::vector records(5, untouched); + + // Three characters from the third on. + ASSERT_EQ(3, EPDFText_GetPageGeometry(text_page.get(), 2, 3, records.data())); + for (int i = 0; i < 3; ++i) { + EPDF_CHAR_GEOMETRY geometry = {}; + ASSERT_TRUE(EPDFText_GetCharGeometry(text_page.get(), 2 + i, &geometry)); + EXPECT_EQ(0, memcmp(&geometry, &records[static_cast(i)].geometry, + sizeof(geometry))) + << i; + } + EXPECT_EQ(kUntouched, records[3].object_run); + + // Five asked for where two are left. + records.assign(5, untouched); + ASSERT_EQ(2, EPDFText_GetPageGeometry(text_page.get(), char_count - 2, 5, + records.data())); + EPDF_CHAR_GEOMETRY last = {}; + ASSERT_TRUE(EPDFText_GetCharGeometry(text_page.get(), char_count - 1, &last)); + EXPECT_EQ(0, memcmp(&last, &records[1].geometry, sizeof(last))); + EXPECT_EQ(kUntouched, records[2].object_run); + + // No character named: nothing is written. + records.assign(5, untouched); + EXPECT_EQ(0, EPDFText_GetPageGeometry(text_page.get(), char_count, 5, + records.data())); + EXPECT_EQ(0, EPDFText_GetPageGeometry(text_page.get(), char_count + 1, 5, + records.data())); + EXPECT_EQ(0, + EPDFText_GetPageGeometry(text_page.get(), -1, 5, records.data())); + EXPECT_EQ(0, EPDFText_GetPageGeometry(text_page.get(), 0, 0, records.data())); + EXPECT_EQ(0, + EPDFText_GetPageGeometry(text_page.get(), 0, -1, records.data())); + EXPECT_EQ(kUntouched, records[0].object_run); + EXPECT_EQ(0, EPDFText_GetPageGeometry(text_page.get(), 0, 5, nullptr)); +} + +TEST_F(EPDFTextEmbedderTest, GetPageGeometryOfAPageWithoutText) { + ASSERT_TRUE(OpenDocument("font_size_zero.pdf")); + ScopedPage page = LoadScopedPage(0); + ASSERT_TRUE(page); + ScopedFPDFTextPage text_page(FPDFText_LoadPage(page.get())); + ASSERT_TRUE(text_page); + + EPDF_PAGE_CHAR_GEOMETRY record = {}; + record.object_run = 0xffffffffu; + EXPECT_EQ(0, EPDFText_GetPageGeometry(text_page.get(), 0, 1, &record)); + EXPECT_EQ(0xffffffffu, record.object_run); +} + +TEST_F(EPDFTextEmbedderTest, GetPageGeometryInvalidArgs) { + EPDF_PAGE_CHAR_GEOMETRY record = {}; + EXPECT_EQ(-1, EPDFText_GetPageGeometry(nullptr, 0, 0, nullptr)); + EXPECT_EQ(-1, EPDFText_GetPageGeometry(nullptr, 0, 1, &record)); +} + TEST_F(EPDFTextEmbedderTest, ReadPurityRenderType3Font) { ASSERT_TRUE(OpenDocument("bug_1591.pdf")); ScopedPage page = LoadScopedPage(0); diff --git a/fpdfsdk/fpdf_text.cpp b/fpdfsdk/fpdf_text.cpp index 625cfc7d37..59a514c076 100644 --- a/fpdfsdk/fpdf_text.cpp +++ b/fpdfsdk/fpdf_text.cpp @@ -47,6 +47,11 @@ static_assert(offsetof(EPDF_CHAR_MAP_ANCHOR, text_offset) == 4); static_assert(offsetof(EPDF_CHAR_GEOMETRY, matrix) == 96); static_assert(offsetof(EPDF_CHAR_GEOMETRY, flags) == 120); +static_assert(sizeof(EPDF_PAGE_CHAR_GEOMETRY) == 132); +static_assert(offsetof(EPDF_PAGE_CHAR_GEOMETRY, geometry) == 0); +static_assert(offsetof(EPDF_PAGE_CHAR_GEOMETRY, object_run) == 124); +static_assert(offsetof(EPDF_PAGE_CHAR_GEOMETRY, font_size) == 128); + constexpr float kUprightTolerance = 1e-4f; constexpr float kSingularTolerance = 1e-6f; @@ -59,7 +64,8 @@ CPDF_TextPage* GetTextPageForValidIndex(FPDF_TEXTPAGE text_page, int index) { return static_cast(index) < textpage->size() ? textpage : nullptr; } -// EmbedPDF(epdf-api): helpers for EPDFText_GetCharGeometry. +// EmbedPDF(epdf-api): helpers for EPDFText_GetCharGeometry and +// EPDFText_GetPageGeometry. bool HasUsableBasis(const CFX_Matrix& matrix) { if (!std::isfinite(matrix.a) || !std::isfinite(matrix.b) || !std::isfinite(matrix.c) || !std::isfinite(matrix.d)) { @@ -111,6 +117,60 @@ FS_QUADPOINTSF QuadFromPoints(const std::array& points) { points[2].x, points[2].y, points[3].x, points[3].y}; } +// One character's geometry. |index| is a character of |textpage|. +EPDF_CHAR_GEOMETRY CharGeometryOf(const CPDF_TextPage& textpage, int index) { + const CPDF_TextPage::CharInfo& charinfo = textpage.GetCharInfo(index); + EPDF_CHAR_GEOMETRY result = {}; + result.matrix = FSMatrixFromCFXMatrix(charinfo.matrix()); + result.loose_box = FSRectFFromCFXFloatRect(charinfo.loose_char_box()); + result.tight_box = FSRectFFromCFXFloatRect(charinfo.char_box()); + + if (!charinfo.char_box().IsEmpty()) { + result.flags |= EPDF_CHARGEO_HAS_TIGHT_BOX; + } + if (charinfo.unicode() == L' ') { + result.flags |= EPDF_CHARGEO_SPACE; + } + if (charinfo.char_type() == CPDF_TextPage::CharType::kGenerated || + charinfo.char_type() == CPDF_TextPage::CharType::kPiece || + charinfo.char_type() == CPDF_TextPage::CharType::kActualText) { + result.flags |= EPDF_CHARGEO_SYNTHESIZED; + } + if (IsUpright(charinfo.matrix())) { + result.flags |= EPDF_CHARGEO_UPRIGHT; + } + + std::optional local_geometry = + textpage.GetCharLocalGeometry(index); + if (local_geometry && HasUsableBasis(charinfo.matrix())) { + const std::array loose_points = + TransformLocalBox(local_geometry->loose_box, charinfo.matrix()); + result.loose_quad = QuadFromPoints(loose_points); + result.loose_box = FSRectFFromCFXFloatRect(BoundsOfPoints(loose_points)); + result.flags |= EPDF_CHARGEO_HAS_LOOSE_QUAD; + + if (local_geometry->tight_box) { + const std::array tight_points = + TransformLocalBox(*local_geometry->tight_box, charinfo.matrix()); + result.tight_quad = QuadFromPoints(tight_points); + result.tight_box = FSRectFFromCFXFloatRect(BoundsOfPoints(tight_points)); + result.flags |= EPDF_CHARGEO_HAS_TIGHT_BOX | EPDF_CHARGEO_HAS_TIGHT_QUAD; + } + } + + const CFX_FloatRect result_loose = CFXFloatRectFromFSRectF(result.loose_box); + if (result_loose.IsEmpty()) { + result.flags |= EPDF_CHARGEO_EMPTY; + result.flags &= ~(EPDF_CHARGEO_HAS_LOOSE_QUAD | + EPDF_CHARGEO_HAS_TIGHT_QUAD | EPDF_CHARGEO_HAS_TIGHT_BOX); + result.loose_quad = {}; + result.tight_quad = {}; + result.tight_box = {}; + } + + return result; +} + } // namespace FPDF_EXPORT FPDF_TEXTPAGE FPDF_CALLCONV FPDFText_LoadPage(FPDF_PAGE page) { @@ -363,8 +423,8 @@ FPDFText_GetLooseCharBox(FPDF_TEXTPAGE text_page, int index, FS_RECTF* rect) { return true; } -// EmbedPDF(epdf-api): EPDFText_GetCharGeometry, EPDFText_GetTextFull and -// EPDFText_GetCharToTextMap. +// EmbedPDF(epdf-api): EPDFText_GetCharGeometry, EPDFText_GetPageGeometry, +// EPDFText_GetTextFull and EPDFText_GetCharToTextMap. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFText_GetCharGeometry(FPDF_TEXTPAGE text_page, int index, @@ -377,57 +437,55 @@ EPDFText_GetCharGeometry(FPDF_TEXTPAGE text_page, return false; } - const CPDF_TextPage::CharInfo& charinfo = textpage->GetCharInfo(index); - EPDF_CHAR_GEOMETRY result = {}; - result.matrix = FSMatrixFromCFXMatrix(charinfo.matrix()); - result.loose_box = FSRectFFromCFXFloatRect(charinfo.loose_char_box()); - result.tight_box = FSRectFFromCFXFloatRect(charinfo.char_box()); + *geometry = CharGeometryOf(*textpage, index); + return true; +} - if (!charinfo.char_box().IsEmpty()) { - result.flags |= EPDF_CHARGEO_HAS_TIGHT_BOX; - } - if (charinfo.unicode() == L' ') { - result.flags |= EPDF_CHARGEO_SPACE; - } - if (charinfo.char_type() == CPDF_TextPage::CharType::kGenerated || - charinfo.char_type() == CPDF_TextPage::CharType::kPiece || - charinfo.char_type() == CPDF_TextPage::CharType::kActualText) { - result.flags |= EPDF_CHARGEO_SYNTHESIZED; +FPDF_EXPORT int FPDF_CALLCONV +EPDFText_GetPageGeometry(FPDF_TEXTPAGE text_page, + int start_index, + int count, + EPDF_PAGE_CHAR_GEOMETRY* buffer) { + CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); + if (!textpage) { + return -1; } - if (IsUpright(charinfo.matrix())) { - result.flags |= EPDF_CHARGEO_UPRIGHT; + const int char_count = textpage->CountChars(); + if (!buffer || count <= 0 || start_index < 0 || start_index >= char_count) { + return 0; } - - std::optional local_geometry = - textpage->GetCharLocalGeometry(index); - if (local_geometry && HasUsableBasis(charinfo.matrix())) { - const std::array loose_points = - TransformLocalBox(local_geometry->loose_box, charinfo.matrix()); - result.loose_quad = QuadFromPoints(loose_points); - result.loose_box = FSRectFFromCFXFloatRect(BoundsOfPoints(loose_points)); - result.flags |= EPDF_CHARGEO_HAS_LOOSE_QUAD; - - if (local_geometry->tight_box) { - const std::array tight_points = - TransformLocalBox(*local_geometry->tight_box, charinfo.matrix()); - result.tight_quad = QuadFromPoints(tight_points); - result.tight_box = FSRectFFromCFXFloatRect(BoundsOfPoints(tight_points)); - result.flags |= EPDF_CHARGEO_HAS_TIGHT_BOX | EPDF_CHARGEO_HAS_TIGHT_QUAD; + const int written = std::min(count, char_count - start_index); + // SAFETY: required from caller. `buffer` must hold `count` entries, and + // `written` is at most `count`. + pdfium::span out = + UNSAFE_BUFFERS(pdfium::span(buffer, static_cast(written))); + + // A run's characters share a text object, and so a font size. Runs count + // from the page's first character, whatever the range read. + const CPDF_TextObject* run_object = textpage->GetCharInfo(0).text_object(); + uint32_t run = 0; + for (int i = 1; i <= start_index; ++i) { + const CPDF_TextObject* object = + textpage->GetCharInfo(static_cast(i)).text_object(); + if (object != run_object) { + ++run; + run_object = object; } } - - const CFX_FloatRect result_loose = CFXFloatRectFromFSRectF(result.loose_box); - if (result_loose.IsEmpty()) { - result.flags |= EPDF_CHARGEO_EMPTY; - result.flags &= ~(EPDF_CHARGEO_HAS_LOOSE_QUAD | - EPDF_CHARGEO_HAS_TIGHT_QUAD | EPDF_CHARGEO_HAS_TIGHT_BOX); - result.loose_quad = {}; - result.tight_quad = {}; - result.tight_box = {}; + float font_size = textpage->GetCharFontSize(static_cast(start_index)); + for (int i = 0; i < written; ++i) { + const int index = start_index + i; + const CPDF_TextObject* object = + textpage->GetCharInfo(static_cast(index)).text_object(); + if (object != run_object) { + ++run; + run_object = object; + font_size = textpage->GetCharFontSize(static_cast(index)); + } + out[static_cast(i)] = {CharGeometryOf(*textpage, index), run, + font_size}; } - - *geometry = result; - return true; + return written; } FPDF_EXPORT int FPDF_CALLCONV EPDFText_GetTextFull(FPDF_TEXTPAGE text_page, diff --git a/public/epdf_text.h b/public/epdf_text.h index 31365ab9c1..d7f6696702 100644 --- a/public/epdf_text.h +++ b/public/epdf_text.h @@ -50,6 +50,38 @@ EPDFText_GetCharGeometry(FPDF_TEXTPAGE text_page, int index, EPDF_CHAR_GEOMETRY* geometry); +// Experimental EmbedPDF Extension API. +// One character of EPDFText_GetPageGeometry. +// geometry what EPDFText_GetCharGeometry gives for the character. +// object_run the run of consecutive characters with one text object that +// the character is in, counted from the page's first character: +// 0 there, and one more each time FPDFText_GetTextObject +// changes from a character to the next. A generated character +// has no text object, so generated characters make runs of +// their own. +// font_size what FPDFText_GetFontSize gives for the character. +typedef struct EPDF_PAGE_CHAR_GEOMETRY_ { + EPDF_CHAR_GEOMETRY geometry; + uint32_t object_run; + float font_size; +} EPDF_PAGE_CHAR_GEOMETRY; + +// Experimental EmbedPDF Extension API. +// The geometry of |count| characters of a text page from |start_index| on, in +// one call and in character order: for each character, what +// EPDFText_GetCharGeometry, FPDFText_GetTextObject and FPDFText_GetFontSize +// answer one character at a time. It carries no text. +// +// |buffer| must hold |count| entries. Returns the number of characters +// written: fewer than |count| when the page ends first, and 0 when +// |start_index| isn't a character of the page, |count| isn't positive or +// |buffer| is null. Returns -1 for an invalid |text_page|. +FPDF_EXPORT int FPDF_CALLCONV +EPDFText_GetPageGeometry(FPDF_TEXTPAGE text_page, + int start_index, + int count, + EPDF_PAGE_CHAR_GEOMETRY* buffer); + // Experimental EmbedPDF Extension API. // One anchor of the character->text mapping emitted by // EPDFText_GetCharToTextMap: from |char_index| onward (until the next