-
Notifications
You must be signed in to change notification settings - Fork 152
refactor: centralizing anchored graphic placement #3619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VladaHarbour
wants to merge
4
commits into
main
Choose a base branch
from
sd-3343_image-wrapping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+805
−324
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e99623a
refactor: centralizing anchored graphic placement
VladaHarbour b688659
fix: apply offset when pre-registered fallback has no paragraph context
VladaHarbour 648609f
fix: add test coverage
VladaHarbour 7937fa9
fix: visual testing regression
VladaHarbour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
170 changes: 170 additions & 0 deletions
170
packages/layout-engine/contracts/src/graphic-placement.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { resolveAnchoredGraphicY, resolveAnchoredGraphicX } from './graphic-placement.js'; | ||
|
|
||
| const yBase = { | ||
| objectHeight: 100, | ||
| contentTop: 72, | ||
| contentBottom: 720, | ||
| pageBottomMargin: 72, | ||
| }; | ||
|
|
||
| const columns = { width: 200, gap: 20, count: 2 }; | ||
| const margins = { left: 72, right: 72 }; | ||
| const pageWidth = 600; | ||
| const objectWidth = 80; | ||
|
|
||
| describe('resolveAnchoredGraphicY', () => { | ||
| it('positions margin-relative top with offset', () => { | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| anchor: { vRelativeFrom: 'margin', alignV: 'top', offsetV: 10 }, | ||
| }), | ||
| ).toBe(82); | ||
| }); | ||
|
|
||
| it('positions page-relative bottom with page margin', () => { | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| anchor: { vRelativeFrom: 'page', alignV: 'bottom', offsetV: 5 }, | ||
| }), | ||
| ).toBe(720 + 72 - 100 + 5); | ||
| }); | ||
|
|
||
| it('positions paragraph-relative center on first line', () => { | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| anchor: { vRelativeFrom: 'paragraph', alignV: 'center', offsetV: 0 }, | ||
| anchorParagraphY: 200, | ||
| firstLineHeight: 24, | ||
| }), | ||
| ).toBe(200 + (24 - 100) / 2); | ||
| }); | ||
|
|
||
| it('uses pre-registered fallback when vRelativeFrom is paragraph without paragraph context', () => { | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| anchor: { vRelativeFrom: 'paragraph', offsetV: 20 }, | ||
| preRegisteredFallbackToContentTop: true, | ||
| }), | ||
| ).toBe(92); | ||
| }); | ||
|
|
||
| it('ignores paragraph alignV when pre-registered fallback has no paragraph context', () => { | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| anchor: { vRelativeFrom: 'paragraph', alignV: 'center', offsetV: 0 }, | ||
| preRegisteredFallbackToContentTop: true, | ||
| }), | ||
| ).toBe(72); | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| objectHeight: 50, | ||
| anchor: { vRelativeFrom: 'paragraph', alignV: 'bottom', offsetV: 10 }, | ||
| preRegisteredFallbackToContentTop: true, | ||
| }), | ||
| ).toBe(82); | ||
| }); | ||
|
|
||
| it('legacy undefined vRelativeFrom uses anchor paragraph Y plus offsetV', () => { | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| anchor: { offsetV: 15 }, | ||
| anchorParagraphY: 300, | ||
| }), | ||
| ).toBe(315); | ||
| }); | ||
|
|
||
| it('legacy undefined vRelativeFrom with preRegisteredFallbackToContentTop uses contentTop', () => { | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| anchor: { alignV: 'center', offsetV: 20 }, | ||
| anchorParagraphY: 300, | ||
| preRegisteredFallbackToContentTop: true, | ||
| }), | ||
| ).toBe(92); | ||
| }); | ||
|
|
||
| it('legacy undefined vRelativeFrom does not use paragraph alignV without vRelativeFrom paragraph', () => { | ||
| expect( | ||
| resolveAnchoredGraphicY({ | ||
| ...yBase, | ||
| anchor: { alignV: 'bottom', offsetV: 0 }, | ||
| anchorParagraphY: 200, | ||
| firstLineHeight: 24, | ||
| }), | ||
| ).toBe(200); | ||
| }); | ||
| }); | ||
|
|
||
| describe('resolveAnchoredGraphicX', () => { | ||
| const columnIndex = 1; | ||
| const columnLeft = margins.left + columnIndex * (columns.width + columns.gap); | ||
|
|
||
| describe('column-relative (default)', () => { | ||
| it.each([ | ||
| { alignH: 'left' as const, offsetH: 10, expected: columnLeft + 10 }, | ||
| { alignH: 'center' as const, offsetH: 5, expected: columnLeft + (columns.width - objectWidth) / 2 + 5 }, | ||
| { alignH: 'right' as const, offsetH: 3, expected: columnLeft + columns.width - objectWidth - 3 }, | ||
| ])('alignH=$alignH offsetH=$offsetH', ({ alignH, offsetH, expected }) => { | ||
| expect(resolveAnchoredGraphicX({ alignH, offsetH }, columnIndex, columns, objectWidth, margins, pageWidth)).toBe( | ||
| expected, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('margin-relative', () => { | ||
| const baseX = margins.left; | ||
| const availableWidth = pageWidth - margins.left - margins.right; | ||
|
|
||
| it.each([ | ||
| { alignH: 'left' as const, offsetH: 10, expected: baseX + 10 }, | ||
| { alignH: 'center' as const, offsetH: 5, expected: baseX + (availableWidth - objectWidth) / 2 + 5 }, | ||
| { alignH: 'right' as const, offsetH: 3, expected: baseX + availableWidth - objectWidth - 3 }, | ||
| ])('alignH=$alignH offsetH=$offsetH', ({ alignH, offsetH, expected }) => { | ||
| expect( | ||
| resolveAnchoredGraphicX( | ||
| { hRelativeFrom: 'margin', alignH, offsetH }, | ||
| columnIndex, | ||
| columns, | ||
| objectWidth, | ||
| margins, | ||
| pageWidth, | ||
| ), | ||
| ).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe('page-relative', () => { | ||
| const baseX = 0; | ||
| const availableWidth = pageWidth; | ||
|
|
||
| it.each([ | ||
| { alignH: 'left' as const, offsetH: 10, expected: baseX + 10 }, | ||
| { alignH: 'center' as const, offsetH: 5, expected: baseX + (availableWidth - objectWidth) / 2 + 5 }, | ||
| { alignH: 'right' as const, offsetH: 3, expected: baseX + availableWidth - objectWidth - 3 }, | ||
| ])('alignH=$alignH offsetH=$offsetH', ({ alignH, offsetH, expected }) => { | ||
| expect( | ||
| resolveAnchoredGraphicX( | ||
| { hRelativeFrom: 'page', alignH, offsetH }, | ||
| columnIndex, | ||
| columns, | ||
| objectWidth, | ||
| margins, | ||
| pageWidth, | ||
| ), | ||
| ).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| it('defaults alignH to left and offsetH to zero', () => { | ||
| expect(resolveAnchoredGraphicX({}, 0, columns, objectWidth, margins, pageWidth)).toBe(margins.left); | ||
| }); | ||
| }); | ||
155 changes: 155 additions & 0 deletions
155
packages/layout-engine/contracts/src/graphic-placement.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| type AnchorVRelative = 'paragraph' | 'page' | 'margin'; | ||
| type AnchorHRelative = 'column' | 'page' | 'margin'; | ||
| type AnchorAlignH = 'left' | 'center' | 'right'; | ||
| type AnchorAlignV = 'top' | 'center' | 'bottom'; | ||
|
|
||
| export type ColumnLayoutForAnchor = { | ||
| width: number; | ||
| gap: number; | ||
| count: number; | ||
| }; | ||
|
|
||
| /** | ||
| * Inputs for resolving the paint Y of an anchored image, drawing, or floating table. | ||
| * `offsetV` is applied inside this function; callers must pass the resolved value to | ||
| * text-wrap registration without adding `offsetV` again. | ||
| */ | ||
| export type ResolveAnchoredGraphicYInput = { | ||
| anchor?: { | ||
| vRelativeFrom?: AnchorVRelative; | ||
| alignV?: AnchorAlignV; | ||
| offsetV?: number; | ||
| }; | ||
| objectHeight: number; | ||
| contentTop: number; | ||
| contentBottom: number; | ||
| /** Bottom page margin in px (used when vRelativeFrom is `page`). */ | ||
| pageBottomMargin?: number; | ||
| /** | ||
| * Anchor paragraph top Y (body cursor when laying out the anchor paragraph). | ||
| * Used for `paragraph` and legacy (undefined vRelativeFrom) positioning. | ||
| */ | ||
| anchorParagraphY?: number; | ||
| /** First line height of the anchor paragraph (paragraph-relative alignV). */ | ||
| firstLineHeight?: number; | ||
| /** | ||
| * When true, anchor has no host paragraph (pre-registered / paragraphless layout). | ||
| * For `vRelativeFrom: 'paragraph'`, use `contentTop + offsetV` instead of alignV on a | ||
| * synthetic paragraph (defaults would wrongly center/bottom against contentTop). | ||
| */ | ||
| preRegisteredFallbackToContentTop?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Resolve the vertical paint position for an anchored graphic (image, drawing, or table). | ||
| */ | ||
| export function resolveAnchoredGraphicY(input: ResolveAnchoredGraphicYInput): number { | ||
| const { | ||
| anchor, | ||
| objectHeight, | ||
| contentTop, | ||
| contentBottom, | ||
| pageBottomMargin = 0, | ||
| anchorParagraphY = contentTop, | ||
| firstLineHeight = 0, | ||
| preRegisteredFallbackToContentTop = false, | ||
| } = input; | ||
|
|
||
| const offsetV = anchor?.offsetV ?? 0; | ||
| const vRelativeFrom = anchor?.vRelativeFrom; | ||
| const alignV = anchor?.alignV; | ||
| const contentHeight = Math.max(0, contentBottom - contentTop); | ||
|
|
||
| if (vRelativeFrom === 'margin') { | ||
| if (alignV === 'bottom') { | ||
| return contentBottom - objectHeight + offsetV; | ||
| } | ||
| if (alignV === 'center') { | ||
| return contentTop + (contentHeight - objectHeight) / 2 + offsetV; | ||
| } | ||
| return contentTop + offsetV; | ||
| } | ||
|
|
||
| if (vRelativeFrom === 'page') { | ||
| const pageHeight = contentBottom + pageBottomMargin; | ||
| if (alignV === 'bottom') { | ||
| return pageHeight - objectHeight + offsetV; | ||
| } | ||
| if (alignV === 'center') { | ||
| return (pageHeight - objectHeight) / 2 + offsetV; | ||
| } | ||
| return offsetV; | ||
| } | ||
|
|
||
| if (vRelativeFrom === 'paragraph') { | ||
| if (preRegisteredFallbackToContentTop) { | ||
| return contentTop + offsetV; | ||
| } | ||
| const baseAnchorY = anchorParagraphY; | ||
| if (alignV === 'bottom') { | ||
| return baseAnchorY + firstLineHeight - objectHeight + offsetV; | ||
| } | ||
| if (alignV === 'center') { | ||
| return baseAnchorY + (firstLineHeight - objectHeight) / 2 + offsetV; | ||
| } | ||
| return baseAnchorY + offsetV; | ||
|
VladaHarbour marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (preRegisteredFallbackToContentTop) { | ||
| return contentTop + offsetV; | ||
| } | ||
|
|
||
| return anchorParagraphY + offsetV; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve horizontal paint position for an anchored graphic. | ||
| */ | ||
| export function resolveAnchoredGraphicX( | ||
| anchor: { | ||
| hRelativeFrom?: AnchorHRelative; | ||
| alignH?: AnchorAlignH; | ||
| offsetH?: number; | ||
| }, | ||
| columnIndex: number, | ||
| columns: ColumnLayoutForAnchor, | ||
| objectWidth: number, | ||
| margins?: { left?: number; right?: number }, | ||
| pageWidth?: number, | ||
| ): number { | ||
| const alignH = anchor.alignH ?? 'left'; | ||
| const offsetH = anchor.offsetH ?? 0; | ||
|
|
||
| const marginLeft = Math.max(0, margins?.left ?? 0); | ||
| const marginRight = Math.max(0, margins?.right ?? 0); | ||
| const contentWidth = pageWidth != null ? Math.max(1, pageWidth - (marginLeft + marginRight)) : columns.width; | ||
|
|
||
| const contentLeft = marginLeft; | ||
| const columnLeft = contentLeft + columnIndex * (columns.width + columns.gap); | ||
|
|
||
| const relativeFrom = anchor.hRelativeFrom ?? 'column'; | ||
|
|
||
| let baseX: number; | ||
| let availableWidth: number; | ||
| if (relativeFrom === 'page') { | ||
| baseX = 0; | ||
| availableWidth = pageWidth != null ? pageWidth : contentWidth + marginLeft + marginRight; | ||
| } else if (relativeFrom === 'margin') { | ||
| baseX = contentLeft; | ||
| availableWidth = contentWidth; | ||
| } else { | ||
| baseX = columnLeft; | ||
| availableWidth = columns.width; | ||
| } | ||
|
|
||
| if (alignH === 'left') { | ||
| return baseX + offsetH; | ||
| } | ||
| if (alignH === 'right') { | ||
| return baseX + availableWidth - objectWidth - offsetH; | ||
| } | ||
| if (alignH === 'center') { | ||
| return baseX + (availableWidth - objectWidth) / 2 + offsetH; | ||
| } | ||
| return baseX; | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.