Repository navigation
Read a range of a text page's character geometry in one call - #126
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The engine reads a page's text geometry one character at a time. For each character it calls
FPDFText_GetTextObjectandEPDFText_GetCharGeometry, and then reads 25 values of the struct, each across the boundary on its own. Crossing into JavaScript that often costs more than the geometry itself: on a 1,310-page text document those calls and reads take 4.6 ms a page on the native runtime and 4.0 ms on WASM, where parsing the page and building its text page take about 1.8 ms together.This adds one call that writes the geometry of a range of characters into one buffer.
Change
EPDF_CHAR_GEOMETRY, the run of consecutive characters with one text object that it is in, and its font size (FPDFText_GetFontSize).object_runcounts from the page's first character whatever the range, so pieces read one after another are the page. Generated characters have no text object and make runs of their own.FPDFText_GetTextdoes, so a caller reads a large page in bounded pieces and can stop between two of them. It returns the number of characters written, 0 when the range names no character, and -1 for an invalid text page.EPDFText_GetCharGeometry: geometry and text stay two reads.EPDFText_GetCharGeometryand the new call share one helper (CharGeometryOf), so a character's geometry is the same from either.static_asserts pin the record's layout (132 bytes).text-geometryrule names the new call.Tests
In
epdf_text_embeddertest.cpp:GetPageGeometryMatchesTheCharCalls: every character of eight documents against the three calls, read in one piece and seven characters at a time. The documents hold upright, turned, sheared, mirrored and vertical text, a font with its own matrix, a Type3 font with a text object per glyph, and a character that prints nothing.GetPageGeometryMatchesTheCharCallsOnActualText: the same for/ActualTexton a page and through a form.GetPageGeometryRunsFollowTheTextObjects: the three runs ofhello_world.pdfand their font sizes, also from a range that starts inside a run.GetPageGeometryReadsARange: a range in the middle, one that ends at the page's end, and ranges that name no character, which write nothing.GetPageGeometryOfAPageWithoutTextandGetPageGeometryInvalidArgs.epdf_c_api_test.clists the new function.Checked
macOS arm64, on this branch (4a52c97 plus the change):
check-c-api.py: ok, 600 functions.check-fork-tags.py --strict: no untagged block.embedpdf_layer_mode_writers.txtis unchanged.On the branch before it was rebased onto #125 (d0edbb1 plus the change):
With the engine's reader switched to the new call (its own pull request in embed-pdf-viewer, once this is released and pinned), the engine answers the same geometry for every page of a 1,310-page text document and of a 29-page CAD set, on WASM and on native. A whole geometry read of a text page, page load included, takes 3.7 ms where it took 7.1 ms on WASM, and 3.6 ms where it took 8.1 ms on native.
Linux x64, in the test image (
scripts/test-pdfium.shwithPDFIUM_TEST_SUITE=all), on this branch: