Аудит раскладки по ECMA-376: jc start/end ренейм всегда, вендорный docx2fo.xslt с патчем w:position
- StrictOoxmlNormalizer: w:jc val=start/end переименовывается и в transitional (docx4j Justification.setXslFO дропает END без fo:text-align — jc=end рендерился по левому краю; Part 4 §14.11.2) - вендорный xslt/docx2fo.xslt = копия docx4j-export-fo + AUDIT-FIX (w:rPr/w:position -> baseline-shift, §17.3.2.24); WordPdfConverter инжектит предкомпилированный Templates (шаг 4.5) - дрейф-гейт vendoredXsltTracksJarCopy (SHA-256 стилиста из jar'а) - регрессии jcEndTransitional_rightAligned, rPrPosition_raisesBaseline - AGENTS.md: шаг 4.5, инвариант стилиста, ограничения из аудита (distribute/suff/lvlJc/tblInd/auto-line-height/kern)
This commit is contained in:
@@ -698,6 +698,94 @@ class WordPdfConverterTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* w:jc val="end" в transitional-документе: docx4j Justification.setXslFO
|
||||
* знает только left/center/right/both, для END {@code fo:text-align} не
|
||||
* пишет вовсе — абзац рендерился по левому краю. StrictOoxmlNormalizer
|
||||
* теперь переименовывает end→right всегда (Part 4 §14.11.2: right≡end).
|
||||
* Регрессия — аудит раскладки (матрица J2).
|
||||
*/
|
||||
@Test
|
||||
void jcEndTransitional_rightAligned() throws Exception {
|
||||
byte[] endDocx = minimalDocx("<w:p><w:pPr><w:jc w:val=\"end\"/></w:pPr>"
|
||||
+ "<w:r><w:t>ENDTEXT</w:t></w:r></w:p>");
|
||||
byte[] rightDocx = minimalDocx("<w:p><w:pPr><w:jc w:val=\"right\"/></w:pPr>"
|
||||
+ "<w:r><w:t>ENDTEXT</w:t></w:r></w:p>");
|
||||
double end = maxRight(convertPdf(endDocx, "t"));
|
||||
double right = maxRight(convertPdf(rightDocx, "t"));
|
||||
assertEquals(right, end, 0.75, "jc=end должен рендериться как jc=right");
|
||||
assertTrue(right > 300, "sanity: jc=right прижимает текст к правому краю, got " + right);
|
||||
}
|
||||
|
||||
/**
|
||||
* w:rPr/w:position=8 (полупункты, положительное поднимает; Part 4
|
||||
* §17.3.2.24): docx4j createBlockForRPr position игнорирует — вендорный
|
||||
* xslt/docx2fo.xslt (AUDIT-FIX-патч) превращает его в baseline-shift.
|
||||
* Регрессия — аудит раскладки (матрица R3).
|
||||
*/
|
||||
@Test
|
||||
void rPrPosition_raisesBaseline() throws Exception {
|
||||
byte[] docx = minimalDocx("<w:p>"
|
||||
+ "<w:r><w:t>P</w:t></w:r>"
|
||||
+ "<w:r><w:rPr><w:position w:val=\"8\"/></w:rPr><w:t>Q</w:t></w:r>"
|
||||
+ "</w:p>");
|
||||
Path pdf = convertPdf(docx, "t");
|
||||
// YDirAdj мерится от верха страницы: поднятая базовая линия — меньше
|
||||
assertEquals(4.0, yOfMarker(pdf, 'P') - yOfMarker(pdf, 'Q'), 0.75,
|
||||
"w:position=8 должен поднимать базовую линию на 4pt");
|
||||
}
|
||||
|
||||
/**
|
||||
* Дрейф-гейт: вендорный xslt/docx2fo.xslt = копия
|
||||
* org/docx4j/convert/out/fo/docx2fo.xslt из docx4j-export-fo +
|
||||
* AUDIT-FIX-патч (w:rPr position). При апгрейде docx4j стилист jar'а
|
||||
* меняется — тест заставляет перевендорить его и перенести патч.
|
||||
*/
|
||||
@Test
|
||||
void vendoredXsltTracksJarCopy() throws Exception {
|
||||
try (InputStream jar = WordPdfConverterTest.class.getResourceAsStream(
|
||||
"/org/docx4j/convert/out/fo/docx2fo.xslt")) {
|
||||
assertNotNull(jar, "нет docx2fo.xslt в docx4j-export-fo");
|
||||
java.security.MessageDigest digest =
|
||||
java.security.MessageDigest.getInstance("SHA-256");
|
||||
StringBuilder hex = new StringBuilder();
|
||||
for (byte b : digest.digest(jar.readAllBytes())) {
|
||||
hex.append(String.format("%02x", b));
|
||||
}
|
||||
assertEquals("cff420cb1bd7ad5d96211c16479db0f7d3a01fec4bf56d01d1bbc7ee4041dda7",
|
||||
hex.toString(), "docx2fo.xslt в docx4j-export-fo изменился:"
|
||||
+ " перевендорить xslt/docx2fo.xslt и перенести AUDIT-FIX-патч");
|
||||
}
|
||||
try (InputStream vendored = WordPdfConverterTest.class.getResourceAsStream(
|
||||
"/xslt/docx2fo.xslt")) {
|
||||
assertNotNull(vendored, "нет вендорного xslt/docx2fo.xslt");
|
||||
assertTrue(new String(vendored.readAllBytes(), StandardCharsets.UTF_8)
|
||||
.contains("AUDIT-FIX position"),
|
||||
"вендорный стилист потерял position-патч");
|
||||
}
|
||||
}
|
||||
|
||||
/** Правый край (X+ширина) самой правой глифы первой страницы. */
|
||||
private static double maxRight(Path pdf) throws IOException {
|
||||
try (PDDocument doc = Loader.loadPDF(pdf.toFile())) {
|
||||
double[] found = {0};
|
||||
PDFTextStripper stripper = new PDFTextStripper() {
|
||||
@Override
|
||||
protected void processTextPosition(
|
||||
org.apache.pdfbox.text.TextPosition position) {
|
||||
found[0] = Math.max(found[0],
|
||||
position.getXDirAdj() + position.getWidth());
|
||||
super.processTextPosition(position);
|
||||
}
|
||||
};
|
||||
stripper.setStartPage(1);
|
||||
stripper.setEndPage(1);
|
||||
stripper.getText(doc);
|
||||
assertTrue(found[0] > 0, "на странице 1 нет текста");
|
||||
return found[0];
|
||||
}
|
||||
}
|
||||
|
||||
/** Строка из кодовых точек (невидимые литералы в исходнике нечитаемы). */
|
||||
private static String invisible(int... cps) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
+8
-5
@@ -289,13 +289,16 @@ class StrictOoxmlNormalizerTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* w:jc val="start" в transitional — JcEnumeration docx4j содержит START/END,
|
||||
* FOP разрешает их по направлению (FO 1.1): рерайт ломал RTL, значение
|
||||
* остаётся как есть.
|
||||
* w:jc val="start" в transitional правится всегда: docx4j Justification.setXslFO
|
||||
* знает только left/center/right/both, для start/end fo:text-align не пишет
|
||||
* вовсе — jc="end" рендерился по левому краю (Part 4 §14.11.2: left≡start,
|
||||
* right≡end; регрессия WordPdfConverterTest#jcEndTransitional_rightAligned).
|
||||
*/
|
||||
@Test
|
||||
void jcStartKeptTransitional() {
|
||||
assertNull(StrictOoxmlNormalizer.rewrite("<w:jc w:val=\"start\"/>", true, false));
|
||||
void jcStartRenamedTransitional() {
|
||||
String r = StrictOoxmlNormalizer.rewrite("<w:jc w:val=\"start\"/>", true, false);
|
||||
assertNotNull(r);
|
||||
assertTrue(r.contains("<w:jc w:val=\"left\"/>"), r);
|
||||
}
|
||||
|
||||
/** А w:tab val="start" правится всегда: STTabJc docx4j 17.3.2 — только left/right. */
|
||||
|
||||
Reference in New Issue
Block a user