mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
fix(layout): make a paragraph's last line as tall as its text, and draw Calibri Light in Carlito in CI's LibreOffice (#3593)
* fix(layout): make a paragraph's last line as tall as its text, not its mark The layout made the last line of every paragraph at least as tall as the paragraph's mark. Word and LibreOffice don't: a line of 8-point text in a paragraph whose mark is 11 points is 8 points tall, as a document of probes saved as a PDF from Word 16 for Mac and laid out by LibreOffice 26.8 showed. Text given a smaller size than its style's, as a TextRun's size does, was laid out too tall, which put a heading of the text-and-spacing demo on the page after Word's. The mark now only sets the height of a line with no text on it, such as an empty paragraph's, or a page break's at the end of a paragraph. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken * ci: draw Calibri Light in Carlito in the shape demos' LibreOffice LibreOffice has no font for Calibri Light, the heading font of Office's theme and of docx's default heading styles, and drew it in Noto Serif, whose lines are 12% taller. So the layout comparison checked docx/layout, which measures Calibri Light as Calibri, against headings taller than Word's: Calibri Light is as tall as Calibri. A fontconfig alias makes LibreOffice draw it in Carlito, as wide and tall as Calibri. The title of charts-in-templates, in the template's heading font, is now drawn in Carlito, so its reference image is drawn again. The other 30 pages don't change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -24,6 +24,10 @@ RUN sed -i -e '/^URIs:/d' -e 's|^# \(http://snapshot.debian.org/.*\)|URIs: \1|'
|
||||
&& ln -s /opt/libreoffice*/program/soffice /usr/local/bin/soffice \
|
||||
&& rm -rf /tmp/LibreOffice_* /var/lib/apt/lists/*
|
||||
|
||||
# LibreOffice has no font for Calibri Light, the heading font of Office's theme and of docx's default styles, and would
|
||||
# draw it in Noto Serif, whose lines are taller
|
||||
COPY fonts.conf /etc/fonts/local.conf
|
||||
|
||||
# LibreOffice keeps its settings in the home directory
|
||||
ENV HOME=/tmp
|
||||
WORKDIR /work
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0"?>
|
||||
<!DOCTYPE fontconfig SYSTEM "urn:fontconfig:fonts.dtd">
|
||||
<!-- Calibri Light, Office's heading font, is drawn in Carlito, which is as tall as it and close to its widths -->
|
||||
<fontconfig>
|
||||
<alias binding="same">
|
||||
<family>Calibri Light</family>
|
||||
<prefer>
|
||||
<family>Carlito</family>
|
||||
</prefer>
|
||||
</alias>
|
||||
</fontconfig>
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 43 KiB After Width: | Height: | Size: 42 KiB |
@@ -24,9 +24,10 @@ describe("layoutLines", () => {
|
||||
expect(heightsOf([text("aaaa bbbbbb")])).to.deep.equal([10, 10]);
|
||||
});
|
||||
|
||||
it("should make each line as tall as the tallest text on it, and the last as tall as the paragraph's mark too", () => {
|
||||
it("should make each line as tall as the tallest text on it, whatever the size of the paragraph's mark", () => {
|
||||
expect(heightsOf([text("aaaa "), text("bbbb", 20), text(" cccc")])).to.deep.equal([20, 10]);
|
||||
expect(heightsOf([text("aaaa bbbb cccc")], 100, { markFont: { size: 14 } })).to.deep.equal([10, 14]);
|
||||
expect(heightsOf([text("aaaa bbbb cccc")], 100, { markFont: { size: 14 } })).to.deep.equal([10, 10]);
|
||||
expect(heightsOf([text("aaaa bbbb cccc", 8)], 100, { markFont: { size: 11 } })).to.deep.equal([8, 8]);
|
||||
});
|
||||
|
||||
it("should give an empty paragraph one line as tall as its mark, or its spaces", () => {
|
||||
@@ -82,8 +83,12 @@ describe("layoutLines", () => {
|
||||
);
|
||||
expect(lines).to.deep.equal([
|
||||
{ height: 10, markers: [], breakAfter: "page" },
|
||||
{ height: 12, markers: ["after"], breakAfter: "column" },
|
||||
{ height: 10, markers: ["after"], breakAfter: "column" },
|
||||
]);
|
||||
// With no text on its line, the break's line is as tall as the mark
|
||||
expect(
|
||||
layoutLines([{ type: "break", kind: "page", font: {} }], { width: 100, measurer: MEASURER, markFont: { size: 12 } }),
|
||||
).to.deep.equal([{ height: 12, markers: [], breakAfter: "page" }]);
|
||||
});
|
||||
|
||||
it("should put the text after a page break at the end of a paragraph, or a line break, on a line of its own", () => {
|
||||
@@ -94,7 +99,7 @@ describe("layoutLines", () => {
|
||||
});
|
||||
expect(lines).to.deep.equal([
|
||||
{ height: 10, markers: [], breakAfter: "page" },
|
||||
{ height: 12, markers: [] },
|
||||
{ height: 10, markers: [] },
|
||||
]);
|
||||
expect(heightsOf([text("aa"), { type: "break", kind: "line", font: {} }], 100, { markFont: { size: 12 } })).to.deep.equal([10, 12]);
|
||||
});
|
||||
|
||||
@@ -58,7 +58,7 @@ export type LineLayoutOptions = {
|
||||
readonly tabStops?: readonly TabStop[];
|
||||
/** The distance between the document's default tab stops, in points. Default is half an inch */
|
||||
readonly defaultTabStop?: number;
|
||||
/** The font of the paragraph's mark, which sets the height of an empty paragraph and of its last line */
|
||||
/** The font of the paragraph's mark, which sets the height of a line with no text on it, such as an empty paragraph's */
|
||||
readonly markFont?: TextFont;
|
||||
readonly measurer?: TextMeasurer;
|
||||
};
|
||||
@@ -358,9 +358,10 @@ export const layoutLines = (
|
||||
}
|
||||
|
||||
if (!end) {
|
||||
finish(line, undefined, line.started ? markHeight : 0);
|
||||
// The mark adds nothing to the height of a line with text, as Word and LibreOffice lay it out
|
||||
finish(line);
|
||||
} else {
|
||||
const breakHeight = Math.max(measurer.measureLineHeight(end.font), isLast ? markHeight : 0);
|
||||
const breakHeight = Math.max(measurer.measureLineHeight(end.font), isLast && !line.started ? markHeight : 0);
|
||||
finish({ ...line, natural: Math.max(line.natural, breakHeight), started: true }, end.kind === "line" ? undefined : end.kind);
|
||||
}
|
||||
first = false;
|
||||
|
||||
Reference in New Issue
Block a user