Repository navigation
feat(layout): add Preformatted, which keeps every space and tab - #662
Merged
Merged
Conversation
A check that runs before drawing needs the glyph a character would get, but GetGlyphId adds it to the subset and the ToUnicode map, so a refused save left registrations for the retry to inherit. PeekGlyphId answers the same and changes nothing.
A paragraph collapses runs of spaces and draws a tab as a space, so command blocks, configuration and ASCII tables lose their columns. Preformatted keeps every space, expands tabs to stops counted in grapheme clusters, wraps a too-wide line with a vector marker, and refuses what cannot be drawn faithfully: control characters at the constructor, and under a PDF/A or PDF/UA claim an unembedded font or a character with no glyph at the save, before any layout runs.
# Conflicts: # src/VellumPdf.Layout/Core/TextStyle.cs # src/VellumPdf.Layout/Document.cs # src/VellumPdf.Layout/LayoutDiagnosticCode.cs # src/VellumPdf.Layout/PublicAPI.Unshipped.txt
Preformatted drew its runs with GetGlyphIds and ShowGlyphs, which lists a glyph 0 with no character. It now selects the font and shows each run through TextEmitter, so the report names the character as for a paragraph. The pre-layout refusals still run first under a claim.
A FontSize of 1e39 saved, wrote an operand past the 3.403e38 limit of ISO 32000-1 Annex C Table C.1, and failed PDF/A-2 6.1.13. The constructor now refuses a size above 1e30, which leaves a factor of about 3.4e8 below the limit. Also qualify the CHANGELOG column claim for zero-width characters, drop a sentence about a DrawContext no caller can build, replace raw characters in test sources with escapes, widen the allocation bound test to the minimum of five samples, and add tests for the scalar cut of a show run, the exact-width line, CR LF line numbers and the scalar named for a non-BMP character.
… rules The 1e30 ceiling is a margin below the 3.403 x 10^38 limit, not the boundary, and only a zero-width character that forms a cluster of its own shifts later columns. Reword the CHANGELOG, the type docs and the refusal message to say so, and drop raw characters from test code.
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.
Closes #591
What changed
A paragraph collapses runs of spaces and draws a tab as a space, so command blocks, configuration and ASCII tables lose their columns.
Preformattedkeeps them.New public API in
VellumPdf.Layout:Preformatted(string text, TextStyle? style = null)withText,Style,TabWidth(1 to 32, default 8),MarginsandBreakBefore.Document.Add(Preformatted).LayoutDiagnosticCode.PreformattedLineWrapped = 104(a warning) andLayoutElementKind.Preformatted = 11.Behaviour:
"a\n"is one line,"a\n\n"two,""one empty line.Pstructure element per drawn piece. Nothing in the character stream separates two lines; this is documented on the type.NotdefGlyph(102, an error), as for a paragraph, soStrictDiagnosticsrefuses that save. The runs go throughTextEmitter, which lists the character. Under a claim the pre-layout refusal below runs first, so nothing is listed.Document.SetDefaultFontdoes not apply.Refusals, all on the new type:
ArgumentException(text) for control characters other than tab, CR and LF, for U+2028 and U+2029, and for an unpaired surrogate, naming the index and code point. It throwsArgumentException(style) for a font size that is not finite, is below 0.000005 or is above 1e30 (a number past 3.403 × 10^38 fails ISO 32000-1, Annex C, Table C.1, and PDF/A-2 6.1.13), a non-finite leading, a colour channel outside 0 to 1, aStandard14value the enumeration does not name, Symbol, ZapfDingbats, and anyLinkUri.TabWidth,MarginsandBreakBeforerefuse bad values from theirinitaccessors withArgumentOutOfRangeException.InvalidOperationExceptionwhen the content box is no wider than zero or shorter than one line. Under a PDF/A or PDF/UA-1 claim it also throws when the font is not embedded (ISO 19005-2:2011, 6.2.11.4.1; ISO 14289-1:2014, 7.21.4.1) or has no glyph for a character, a tab's spaces included (6.2.11.8; 7.21.8). Under PDF/A-2a, PDF/A-2u and PDF/UA-1 it also refuses U+FEFF and U+FFFE even when the font maps them (6.2.11.7.2; 7.21.7). These run beforeHasStartedLayoutis set, so the document stays saveable once the named setting changes, and a retry saves once.TrueTypeFontEmbedder.PeekGlyphId(internal), so a refused save leaves nothing in the subset for a retry to inherit.Doc updates: XML docs on every new member, one sentence on
TextStyle.FontSize,Leading,Color,FontRef,LinkUriandColorRgb,PageBreakMode(the element honoursBreakBeforeand refuses a form feed), theDocument.Diagnosticscode list, the capability table in both copies, and the CHANGELOG.No deliberate output change: a document without the new element saves to the same bytes. Two new pins (untagged and tagged, with a paragraph, heading, list, table and both bands) were recorded before the element existed and also pass on the parent commit.
Follow-up, not in this PR: v2.5 adds a
P > Codeopt-in for text that is code. This PR tags each piece asP, because a preformatted block need not be code.2.3.3 against this build
The type is new, so there is no 2.3.3 column for it. For existing input:
Preformatted(paragraph, heading, list, table, both bands; untagged and tagged)PreformattedByteIdentityTests, also passing on the parent commitParagraph("\tingesprongen\tmet tabs\n vier spaties dubbel"), Courier 10(ingesprongen met tabs) Tj,(vier spaties dubbel) Tjpdftotext26.08.0 on a hand-drawn page with the same expanded text:-layoutprintedvier spaties dubbel(the two double spaces collapsed), while-fixed 6returned every line exactly, shifted right by 12 columns (72 pt margin over a 6 pt pitch). So the tests use-fixed. The poppler pinned in CI is 24.02; it was not run locally, and the assertions compare non-blank lines only, so a different blank-line policy cannot fail them.Tests
At 3b1daea (main at 9747913 merged in), Layout.Tests, Release,
REQUIRE_ORACLES=1,REQUIRE_VERAPDF=1, veraPDF 1.30.4, qpdf 12.4.2, poppler 26.08.0: 3,548 tests over both frameworks, 3,544 pass, 4 skip (the two Linux-only pipe tests on each framework), 0 fail. Kernel.Tests: 5,328 pass over both frameworks. Build has 0 warnings,dotnet format --verify-no-changes, the clean-room check and the roadmap check pass.New test classes:
PreformattedTests(tabs, clusters, line ends, widths equalMeasureStringbit for bit),PreformattedWrapTests(marker operators, report, break position, pages, show-string runs),PreformattedRefusalTests(every UTF-16 code unit against the refused set, each style andinitrefusal, exact messages, retry),PreformattedTaggedTests,PreformattedBoundTests,PreformattedOracleTests,PreformattedByteIdentityTestsand, in Kernel.Tests,PeekGlyphIdTests(every scalar value, in LiberationMono and DejaVu Sans).Oracles:
pdftotext -fixed 6on Courier and LiberationMono documents; veraPDF 1.30.4 with an empty failed-rule list on PDF/A-2u, PDF/A-2a and PDF/UA-1 documents holding tabs, wrapped lines and several pages; qpdf--checkreporting "No syntax or stream encoding errors found" on the 2u and ua1 files. Each test first asserts the saved file holds the expanded text and no tab byte.A footer takes room from every page: 200 lines of Courier 10 give pages of 58, 58, 58 and 26 lines with no footer, and 56, 56, 56 and 32 with a
{page}/{pages}footer, in one measuring pass either way.Mutations
Each mutant was applied in a detached worktree, built, run against the new test classes, and reverted. Every one fails at least one test.
TabWidthspacesATabAddsTheSpacesToTheNextStopcases, the two-fixedoracle tests (9 tests)AClusterIsOneColumn_whateverItsLength,AClusterOfTwoUnits_doesNotShortenTheTabLinesEndWhereTheTextEndsThem..., exact-stream and-fixedtests (11 tests)-fixedtests (12 tests)WideLines_wrapAtTheMarkerReserve...,ABlockOfWrappedLines_...(6 tests)WideLines_wrapAtTheMarkerReserve..., marker operator tests (6 tests)WideLines_wrapAtTheMarkerReserve...,AWrappedLine_thatStraddlesAPageEnd...TheBreak_fallsBeforeACluster_notInsideItEMCTheMarker_isAnArtifactOutsideTheTaggedParagraph...,ABlockOfWrappedLines_keepsItsMarkersOutside..., the UA-1 veraPDF testTheFirstFailingCheckWins_geometryBeforeFontBeforeCharactersACharacterTheFontLacks_isRefusedUnderAClaim...,TheCharacterCheckCountsATabsSpaces...,ARefusedClaimThenNoClaim...AByteOrderMark_isAccepted_underPdfA2bHasStartedLayout = trueAContentBoxShorterThanOneLine...,AContentBoxNoWiderThanZero...,ARefusedClaimThenNoClaim...ErrorTheWrapIsReported_onceForTheElement_asAWarning_andStrictModeSavesAWrappedLine_thatStraddlesAPageEnd_hasItsMarkerOnTheFirstPage_andIsReportedOnceWithAFooter_...(WrapCalls), two bound testsALongShowString_isCutIntoRunsOf4096Scalars_withNoTmBetweenARefusedStyle_isRefusedAtTheConstructor(red above 1, green below 0)PeekGlyphIdregistersPeekGlyphIdTests.Handle_peekLeavesTheSubsetAsItWas(3 of 4 cases fail)Bounds
Allocation is read with
GC.GetAllocatedBytesForCurrentThread(); no test asserts a time.TabWidth32 (32,000 and 320,000 columns)WrapCalls == 1The tests assert the ratio stays within 15 and that the larger input costs more than the smaller.