test(core): пин w:framePr - фрейм hAnchor=page позиционируется от края страницы (17.4.31)
docx4j 17.3.6 транслирует w:framePr в fo:block-container absolute-position=fixed (left/top) - до апгрейда текст фрейма шёл общим потоком у левого поля. Замер реаудита (x=252pt) был для hAnchor=margin; фикстура frame-pr.docx пинит hAnchor/vAnchor=page x=3600 -> текст на 180pt от края страницы, обычный абзац - 72pt от поля. Регрессия WordPdfConverterTest#framePr_positionedLikeWord. Счётчики: фикстуры 14 docx, тесты 57/885 (догнаны до фактических).
This commit is contained in:
@@ -12,12 +12,16 @@ import org.apache.poi.wp.usermodel.HeaderFooterType;
|
||||
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFramePr;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLvl;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumPr;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSym;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHAnchor;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STNumberFormat;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVAnchor;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STWrap;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFAbstractNum;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFNumbering;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
|
||||
@@ -76,6 +80,7 @@ public final class SampleDocxGenerator {
|
||||
exoticGlyphs(dir.resolve("exotic-glyphs.docx"));
|
||||
tooManyPages(dir.resolve("too-many-pages.docx"));
|
||||
staleCache(dir.resolve("stale-cache.docx"));
|
||||
framePr(dir.resolve("frame-pr.docx"));
|
||||
|
||||
System.out.println("Готово: " + dir.toAbsolutePath());
|
||||
}
|
||||
@@ -381,6 +386,34 @@ public final class SampleDocxGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* w:framePr (Part 1 §17.4.31): фрейм {@code hAnchor/vAnchor="page"},
|
||||
* {@code x=3600 y=1440} twips → левый край текста 72+180=252pt от края
|
||||
* страницы; обычный абзац печатается от поля (72pt). docx4j 17.3.6
|
||||
* транслирует фрейм в абсолютно позиционированный блок FOP (до апгрейда
|
||||
* текст фрейма шёл общим потоком у левого поля).
|
||||
* Регрессия — WordPdfConverterTest#framePr_positionedLikeWord.
|
||||
*/
|
||||
private static void framePr(Path out) throws Exception {
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
heading(doc, "Frame anchored text", "Calibri");
|
||||
XWPFParagraph base = doc.createParagraph();
|
||||
run(base, "BASELINE ordinary line", "Calibri", false, false);
|
||||
XWPFParagraph framed = doc.createParagraph();
|
||||
CTPPr pPr = framed.getCTP().isSetPPr() ? framed.getCTP().getPPr() : framed.getCTP().addNewPPr();
|
||||
CTFramePr frame = pPr.addNewFramePr();
|
||||
frame.setHAnchor(STHAnchor.PAGE);
|
||||
frame.setVAnchor(STVAnchor.PAGE);
|
||||
frame.setX("3600");
|
||||
frame.setY("1440");
|
||||
frame.setW("4320");
|
||||
frame.setH("2880");
|
||||
frame.setWrap(STWrap.NONE);
|
||||
run(framed, "FRAME anchored line", "Calibri", false, false);
|
||||
save(doc, out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Переупаковывает .docx, вставляя {@code w:lastRenderedPageBreak}
|
||||
* перед каждым текстовым тегом {@code w:t} в {@code word/document.xml},
|
||||
|
||||
@@ -2932,4 +2932,50 @@ class WordPdfConverterTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* w:framePr (Part 1 §17.4.31): фрейм {@code hAnchor/vAnchor="page"} с
|
||||
* {@code x=3600 y=1440} twips позиционируется от края страницы — левый
|
||||
* край его текста 180pt (не от поля!); обычный абзац начинается у поля
|
||||
* (72pt). docx4j 17.3.6 транслирует фрейм в {@code fo:block-container
|
||||
* absolute-position="fixed" left top} (до апгрейда текст фрейма шёл общим
|
||||
* потоком). Фикстура — samples/frame-pr.docx.
|
||||
*/
|
||||
@Test
|
||||
void framePr_positionedLikeWord() throws Exception {
|
||||
Path pdf = convertPdf(sampleBytes("frame-pr.docx"), "frame-pr");
|
||||
Map<Long, double[]> lefts = new LinkedHashMap<>();
|
||||
Map<Long, StringBuilder> lineTexts = new LinkedHashMap<>();
|
||||
try (PDDocument doc = Loader.loadPDF(pdf.toFile())) {
|
||||
PDFTextStripper stripper = new PDFTextStripper() {
|
||||
@Override
|
||||
protected void processTextPosition(org.apache.pdfbox.text.TextPosition tp) {
|
||||
String u = tp.getUnicode();
|
||||
if (!u.trim().isEmpty()) {
|
||||
long y = Math.round(tp.getYDirAdj());
|
||||
double[] min = lefts.computeIfAbsent(y, k -> new double[]{Double.MAX_VALUE});
|
||||
min[0] = Math.min(min[0], tp.getXDirAdj());
|
||||
lineTexts.computeIfAbsent(y, k -> new StringBuilder()).append(u);
|
||||
}
|
||||
super.processTextPosition(tp);
|
||||
}
|
||||
};
|
||||
String text = stripper.getText(doc);
|
||||
assertTrue(text.contains("FRAME anchored line"), "текст фрейма напечатан");
|
||||
}
|
||||
Double base = null;
|
||||
Double frame = null;
|
||||
for (Map.Entry<Long, StringBuilder> e : lineTexts.entrySet()) {
|
||||
String line = e.getValue().toString().trim();
|
||||
if (line.startsWith("BASELINE")) {
|
||||
base = lefts.get(e.getKey())[0];
|
||||
} else if (line.startsWith("FRAME")) {
|
||||
frame = lefts.get(e.getKey())[0];
|
||||
}
|
||||
}
|
||||
assertTrue(base != null && frame != null, "строки-маркеры найдены");
|
||||
assertTrue(Math.abs(base - 72.0) < 1.0, "обычный абзац у поля 72pt, реально=" + base);
|
||||
assertTrue(Math.abs(frame - 180.0) < 1.0,
|
||||
"фрейм hAnchor=page: x = 180pt от края страницы, реально=" + frame);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user