diff --git a/.changeset/textbox-automatic-text-dark-fill.md b/.changeset/textbox-automatic-text-dark-fill.md new file mode 100644 index 0000000000..42c0682a0e --- /dev/null +++ b/.changeset/textbox-automatic-text-dark-fill.md @@ -0,0 +1,5 @@ +--- +'@docx-editor.dev/core': patch +--- + +Text with an automatic color in a text box with a dark fill paints white, as in Word. Light paragraph or cell shading inside such a box turns its automatic text black again, and dark cell shading turns it white. A text box with a light fill or no fill paints its automatic text black, even inside a dark cell, as Word does. diff --git a/docs/site/content/docx-fidelity.mdx b/docs/site/content/docx-fidelity.mdx index b3e7ee935c..ae255e92c4 100644 --- a/docs/site/content/docx-fidelity.mdx +++ b/docs/site/content/docx-fidelity.mdx @@ -59,6 +59,7 @@ The matrix reports editing, rendering, and round-trip support separately. A feat - Raised and lowered text, including inherited `w:position` with subscript or superscript, adds to automatic and minimum line heights. Exact line spacing keeps its height. - An inline picture sits at its run's `w:position` offset from the text baseline. +- Text with an automatic color paints white on dark run shading, paragraph shading, cell shading, or text box fill, and black on light ones. - For equation limits, see [Equations](#equations). ### Paragraphs and styles diff --git a/packages/core/src/output/__tests__/textbox-automatic-text-color.test.ts b/packages/core/src/output/__tests__/textbox-automatic-text-color.test.ts new file mode 100644 index 0000000000..4928634c2b --- /dev/null +++ b/packages/core/src/output/__tests__/textbox-automatic-text-color.test.ts @@ -0,0 +1,186 @@ +// Automatic text in a text box takes its colour from the box's fill: white on a dark fill, +// black on a light one (17.3.2.6 color, CT_Color; the fill is 20.1.8.54 solidFill). + +import { GlobalRegistrator } from '@happy-dom/global-registrator'; +if (!GlobalRegistrator.isRegistered) GlobalRegistrator.register(); + +import { expect, test } from 'bun:test'; +import { createFixedMeasurer, layoutSemanticDocument } from '../../layout/index.ts'; +import type { InlineDrawingLayoutContext } from '../../layout/drawing-layout.ts'; +import { readOoxmlPart } from '@docx-editor.dev/core/store'; +import { + DEFAULT_DRAWING_PROJECTION_LIMITS, + indexInlineDrawingProjectionsInPart, + projectDrawing, +} from '../../store/package/drawing-projection.ts'; +import { paintSemanticLayout } from '../semantic-paint.ts'; + +const NS = + 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" ' + + 'xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" ' + + 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" ' + + 'xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"'; + +function textboxXml(fill: string, story: string): string { + return ( + `` + + '' + + '0' + + '914400' + + '' + + '' + + '' + + '' + + '' + + `` + + `${story}` + + '' + + '' + ); +} + +function paintedRuns(fill: string, story: string): HTMLElement[] { + return paintedTextboxRuns(textboxXml(fill, story)); +} + +function paintedTextboxRuns(documentXml: string): HTMLElement[] { + const loaded = readOoxmlPart(documentXml, { + name: '/word/document.xml', + contentType: 'app/xml', + }); + if (!loaded.ok) throw new Error(loaded.reason); + const part = loaded.part; + const atoms = indexInlineDrawingProjectionsInPart(part); + const drawingLayout: InlineDrawingLayoutContext = { + ownerPartName: part.name, + projectionForAtom: (atomId) => atoms.get(atomId) ?? null, + project: (node) => + atoms.get(node.id) ?? + projectDrawing(node, { ownerPartName: part.name, limits: DEFAULT_DRAWING_PROJECTION_LIMITS }), + resourceOf: () => + Object.freeze({ + kind: 'unrenderable' as const, + partName: null, + mime: 'unknown' as const, + reason: 'unsupported-format' as const, + }), + }; + const layout = layoutSemanticDocument(part, 1, { + measurer: createFixedMeasurer(6, 14), + producer: 'test', + inlineDrawingLayout: drawingLayout, + }); + const container = document.createElement('div'); + document.body.append(container); + paintSemanticLayout(container, layout, { scale: 1 }); + const content = container.querySelector('.docx-drawing-textbox-content'); + expect(content).not.toBeNull(); + const runs = [...content!.querySelectorAll('span')].filter( + (span) => span.textContent && span.children.length === 0 + ); + expect(runs.length).toBeGreaterThan(0); + return runs; +} + +// The colour a run inherits: its own inline colour, or the nearest ancestor's. +function colorOf(element: HTMLElement): string { + for (let at: HTMLElement | null = element; at; at = at.parentElement) { + if (at.style.color) return at.style.color.toLowerCase(); + } + return 'inherited black'; +} +const WHITE = ['#ffffff', 'rgb(255, 255, 255)']; +const BLACK = ['#000000', 'rgb(0, 0, 0)', 'inherited black']; + +test('automatic text on a dark text box fill paints white', () => { + const [run] = paintedRuns( + '990033', + 'Heading' + ); + expect(WHITE).toContain(colorOf(run!)); +}); + +test('automatic text on a light text box fill stays black', () => { + const [run] = paintedRuns('FFFFFF', 'Heading'); + expect(BLACK).toContain(colorOf(run!)); +}); + +test('a run with its own colour keeps it on a dark fill', () => { + const [run] = paintedRuns( + '990033', + 'Heading' + ); + expect(['#ffff00', 'rgb(255, 255, 0)']).toContain(colorOf(run!)); +}); + +test('light paragraph shading inside a dark box turns automatic text black again', () => { + const [run] = paintedRuns( + '990033', + '' + + 'Heading' + ); + expect(BLACK).toContain(colorOf(run!)); +}); + +function cellStory(cellFill: string, run: string): string { + return ( + '' + + '' + + `` + + `${run}` + ); +} + +test('a light-shaded cell inside a dark box paints automatic text black', () => { + const [run] = paintedRuns('990033', cellStory('FFFFFF', 'Answer')); + expect(BLACK).toContain(colorOf(run!)); +}); + +test('a dark-shaded cell inside a light box paints automatic text white', () => { + const [run] = paintedRuns('FFFFFF', cellStory('990033', 'Answer')); + expect(WHITE).toContain(colorOf(run!)); +}); + +test('a run with its own colour keeps it in a light cell inside a dark box', () => { + const [run] = paintedRuns( + '990033', + cellStory('FFFFFF', 'Answer') + ); + expect(['#ffff00', 'rgb(255, 255, 0)']).toContain(colorOf(run!)); +}); + +// An inline text box in a dark-shaded body cell. +function inlineBoxInDarkCellXml(boxFill: string): string { + return ( + `` + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + `${boxFill}` + + 'Answer' + + '' + + '' + + '' + ); +} + +test('a white text box inside a dark-shaded cell paints automatic text black', () => { + const [run] = paintedTextboxRuns( + inlineBoxInDarkCellXml('') + ); + expect(BLACK.filter((color) => color !== 'inherited black')).toContain(colorOf(run!)); +}); + +// Word paints automatic text in an unfilled box black even over a dark cell: the box's text +// follows the box, not the shading around it. +test('an unfilled text box inside a dark-shaded cell paints automatic text black', () => { + const [run] = paintedTextboxRuns(inlineBoxInDarkCellXml('')); + expect(BLACK.filter((color) => color !== 'inherited black')).toContain(colorOf(run!)); +}); diff --git a/packages/core/src/output/semantic-paint-textbox.ts b/packages/core/src/output/semantic-paint-textbox.ts index 83045a8722..f487a5e546 100644 --- a/packages/core/src/output/semantic-paint-textbox.ts +++ b/packages/core/src/output/semantic-paint-textbox.ts @@ -13,6 +13,7 @@ import type { DrawingPaintContext } from './semantic-paint-drawings.ts'; import { applyAccessibility, positionedBox } from './semantic-paint-drawing-box.ts'; import { applyVectorInkReach, vectorShapeFrame } from './semantic-paint-vector-shape.ts'; import { vectorShapeInkClip } from '../layout/vector-shape-ink.ts'; +import { automaticTextColor } from '../layout/ooxml-shading.ts'; const SVG_NAMESPACE = 'http://www.w3.org/2000/svg'; const n = (value: number) => String(Math.round(value * 1000) / 1000); @@ -234,6 +235,9 @@ export function paintTextboxStory( // line that holds an inline box, which carries its paragraph mark's face. content.style.fontWeight = 'normal'; content.style.fontStyle = 'normal'; + // Automatic text (17.3.2.6 color) follows the box's own fill (20.1.8.54 solidFill), not + // shading around the box: white on a dark fill, black on a light fill or none, as Word shows it. + content.style.color = automaticTextColor(story.fillHex) ? '#FFFFFF' : '#000000'; const turn = story.contentRotation; if (turn) { const originX = (turn.pivot.x - story.contentOffset.x) * scale; diff --git a/packages/core/src/output/semantic-paint.ts b/packages/core/src/output/semantic-paint.ts index 6f566943d7..914e208058 100644 --- a/packages/core/src/output/semantic-paint.ts +++ b/packages/core/src/output/semantic-paint.ts @@ -1423,7 +1423,7 @@ function paintFragment( if (fragment.shading && HEX.test(fragment.shading) && fragment.shadingBox) { element.append(paintParagraphShading(document, fragment, scale)); // Automatic text inherits white on dark paragraph shading. - if (automaticTextColor(fragment.shading)) element.style.color = '#FFFFFF'; + element.style.color = automaticTextColor(fragment.shading) ? '#FFFFFF' : '#000000'; } // List markers are layout furniture inside the hanging indent — never model text. if (fragment.marker) { @@ -1760,6 +1760,8 @@ function paintTableCell( if (cell.shading && HEX.test(cell.shading)) { cellElement.style.backgroundColor = `#${cell.shading}`; + // Automatic text takes white on dark cell shading and black on light, over a text box's. + cellElement.style.color = automaticTextColor(cell.shading) ? '#FFFFFF' : '#000000'; } if (cell.revisionShading && ctx.revisionMarkup) { const color = ctx.revisionMarkup.cells[cell.revisionShading];