|
| 1 | +type AnchorVRelative = 'paragraph' | 'page' | 'margin'; |
| 2 | +type AnchorHRelative = 'column' | 'page' | 'margin'; |
| 3 | +type AnchorAlignH = 'left' | 'center' | 'right'; |
| 4 | +type AnchorAlignV = 'top' | 'center' | 'bottom'; |
| 5 | + |
| 6 | +export type ColumnLayoutForAnchor = { |
| 7 | + width: number; |
| 8 | + gap: number; |
| 9 | + count: number; |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | + * Inputs for resolving the paint Y of an anchored image, drawing, or floating table. |
| 14 | + * `offsetV` is applied inside this function; callers must pass the resolved value to |
| 15 | + * text-wrap registration without adding `offsetV` again. |
| 16 | + */ |
| 17 | +export type ResolveAnchoredGraphicYInput = { |
| 18 | + anchor?: { |
| 19 | + vRelativeFrom?: AnchorVRelative; |
| 20 | + alignV?: AnchorAlignV; |
| 21 | + offsetV?: number; |
| 22 | + }; |
| 23 | + objectHeight: number; |
| 24 | + contentTop: number; |
| 25 | + contentBottom: number; |
| 26 | + /** Bottom page margin in px (used when vRelativeFrom is `page`). */ |
| 27 | + pageBottomMargin?: number; |
| 28 | + /** |
| 29 | + * Anchor paragraph top Y (body cursor when laying out the anchor paragraph). |
| 30 | + * Used for `paragraph` and legacy (undefined vRelativeFrom) positioning. |
| 31 | + */ |
| 32 | + anchorParagraphY?: number; |
| 33 | + /** First line height of the anchor paragraph (paragraph-relative alignV). */ |
| 34 | + firstLineHeight?: number; |
| 35 | + /** |
| 36 | + * When true and vRelativeFrom is not margin/page, fall back to contentTop + offsetV |
| 37 | + * (page/margin pre-registered anchors that lack paragraph context). |
| 38 | + */ |
| 39 | + preRegisteredFallbackToContentTop?: boolean; |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Resolve the vertical paint position for an anchored graphic (image, drawing, or table). |
| 44 | + */ |
| 45 | +export function resolveAnchoredGraphicY(input: ResolveAnchoredGraphicYInput): number { |
| 46 | + const { |
| 47 | + anchor, |
| 48 | + objectHeight, |
| 49 | + contentTop, |
| 50 | + contentBottom, |
| 51 | + pageBottomMargin = 0, |
| 52 | + anchorParagraphY = contentTop, |
| 53 | + firstLineHeight = 0, |
| 54 | + preRegisteredFallbackToContentTop = false, |
| 55 | + } = input; |
| 56 | + |
| 57 | + const offsetV = anchor?.offsetV ?? 0; |
| 58 | + const vRelativeFrom = anchor?.vRelativeFrom; |
| 59 | + const alignV = anchor?.alignV; |
| 60 | + const contentHeight = Math.max(0, contentBottom - contentTop); |
| 61 | + |
| 62 | + if (vRelativeFrom === 'margin') { |
| 63 | + if (alignV === 'bottom') { |
| 64 | + return contentBottom - objectHeight + offsetV; |
| 65 | + } |
| 66 | + if (alignV === 'center') { |
| 67 | + return contentTop + (contentHeight - objectHeight) / 2 + offsetV; |
| 68 | + } |
| 69 | + return contentTop + offsetV; |
| 70 | + } |
| 71 | + |
| 72 | + if (vRelativeFrom === 'page') { |
| 73 | + const pageHeight = contentBottom + pageBottomMargin; |
| 74 | + if (alignV === 'bottom') { |
| 75 | + return pageHeight - objectHeight + offsetV; |
| 76 | + } |
| 77 | + if (alignV === 'center') { |
| 78 | + return (pageHeight - objectHeight) / 2 + offsetV; |
| 79 | + } |
| 80 | + return offsetV; |
| 81 | + } |
| 82 | + |
| 83 | + if (vRelativeFrom === 'paragraph') { |
| 84 | + const baseAnchorY = anchorParagraphY; |
| 85 | + if (alignV === 'bottom') { |
| 86 | + return baseAnchorY + firstLineHeight - objectHeight + offsetV; |
| 87 | + } |
| 88 | + if (alignV === 'center') { |
| 89 | + return baseAnchorY + (firstLineHeight - objectHeight) / 2 + offsetV; |
| 90 | + } |
| 91 | + return baseAnchorY + offsetV; |
| 92 | + } |
| 93 | + |
| 94 | + if (preRegisteredFallbackToContentTop) { |
| 95 | + return contentTop + offsetV; |
| 96 | + } |
| 97 | + |
| 98 | + return anchorParagraphY + offsetV; |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Y coordinate where paragraph text begins (after spacing-before collapse). |
| 103 | + */ |
| 104 | +export function computeParagraphContentStartY( |
| 105 | + cursorY: number, |
| 106 | + spacingBefore: number, |
| 107 | + appliedSpacingBefore: boolean, |
| 108 | + trailingSpacing: number | undefined, |
| 109 | +): number { |
| 110 | + if (appliedSpacingBefore || spacingBefore <= 0) { |
| 111 | + return cursorY; |
| 112 | + } |
| 113 | + const prevTrailing = trailingSpacing ?? 0; |
| 114 | + return cursorY + Math.max(spacingBefore - prevTrailing, 0); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Paragraph text start Y including contextual-spacing rewind from the previous paragraph. |
| 119 | + */ |
| 120 | +export function computeParagraphLayoutStartY(input: { |
| 121 | + cursorY: number; |
| 122 | + spacingBefore: number; |
| 123 | + trailingSpacing?: number; |
| 124 | + suppressSpacingBefore?: boolean; |
| 125 | + rewindTrailingFromPrevious?: boolean; |
| 126 | +}): number { |
| 127 | + let y = input.cursorY; |
| 128 | + let trailingForCollapse = input.trailingSpacing; |
| 129 | + if (input.rewindTrailingFromPrevious) { |
| 130 | + const prevTrailing = input.trailingSpacing ?? 0; |
| 131 | + if (prevTrailing > 0) { |
| 132 | + y -= prevTrailing; |
| 133 | + // Match layout-paragraph.ts: after rewind, trailingSpacing is cleared before |
| 134 | + // spacing-before is applied — do not collapse against the rewound gap again. |
| 135 | + trailingForCollapse = 0; |
| 136 | + } |
| 137 | + } |
| 138 | + const effectiveSpacingBefore = input.suppressSpacingBefore ? 0 : input.spacingBefore; |
| 139 | + return computeParagraphContentStartY(y, effectiveSpacingBefore, effectiveSpacingBefore === 0, trailingForCollapse); |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Resolve horizontal paint position for an anchored graphic. |
| 144 | + */ |
| 145 | +export function resolveAnchoredGraphicX( |
| 146 | + anchor: { |
| 147 | + hRelativeFrom?: AnchorHRelative; |
| 148 | + alignH?: AnchorAlignH; |
| 149 | + offsetH?: number; |
| 150 | + }, |
| 151 | + columnIndex: number, |
| 152 | + columns: ColumnLayoutForAnchor, |
| 153 | + objectWidth: number, |
| 154 | + margins?: { left?: number; right?: number }, |
| 155 | + pageWidth?: number, |
| 156 | +): number { |
| 157 | + const alignH = anchor.alignH ?? 'left'; |
| 158 | + const offsetH = anchor.offsetH ?? 0; |
| 159 | + |
| 160 | + const marginLeft = Math.max(0, margins?.left ?? 0); |
| 161 | + const marginRight = Math.max(0, margins?.right ?? 0); |
| 162 | + const contentWidth = pageWidth != null ? Math.max(1, pageWidth - (marginLeft + marginRight)) : columns.width; |
| 163 | + |
| 164 | + const contentLeft = marginLeft; |
| 165 | + const columnLeft = contentLeft + columnIndex * (columns.width + columns.gap); |
| 166 | + |
| 167 | + const relativeFrom = anchor.hRelativeFrom ?? 'column'; |
| 168 | + |
| 169 | + let baseX: number; |
| 170 | + let availableWidth: number; |
| 171 | + if (relativeFrom === 'page') { |
| 172 | + baseX = 0; |
| 173 | + availableWidth = pageWidth != null ? pageWidth : contentWidth + marginLeft + marginRight; |
| 174 | + } else if (relativeFrom === 'margin') { |
| 175 | + baseX = contentLeft; |
| 176 | + availableWidth = contentWidth; |
| 177 | + } else { |
| 178 | + baseX = columnLeft; |
| 179 | + availableWidth = columns.width; |
| 180 | + } |
| 181 | + |
| 182 | + if (alignH === 'left') { |
| 183 | + return baseX + offsetH; |
| 184 | + } |
| 185 | + if (alignH === 'right') { |
| 186 | + return baseX + availableWidth - objectWidth - offsetH; |
| 187 | + } |
| 188 | + if (alignH === 'center') { |
| 189 | + return baseX + (availableWidth - objectWidth) / 2 + offsetH; |
| 190 | + } |
| 191 | + return baseX; |
| 192 | +} |
0 commit comments