From e6a0d6f599922eee8d1887dcc0eb06d1240cfb4e Mon Sep 17 00:00:00 2001 From: Wei Lin Date: Tue, 17 Nov 2026 00:08:38 +0800 Subject: [PATCH 1/2] feat(go): render single-page DOCX tables, fonts, and images Preserve table geometry, merges, styles, registered and installed font metrics, and package images. Keep unsupported pagination and metafiles on the existing text path. TestIssue93 improves from 0.3331 to 0.9757 with all 27 unrelated issue DOCX outputs byte-identical. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- minipdf-go/README.md | 34 +- minipdf-go/docx.go | 3 + minipdf-go/docx_font.go | 172 +++++ minipdf-go/docx_render.go | 1141 ++++++++++++++++++++++++++++++++ minipdf-go/docx_render_test.go | 367 ++++++++++ minipdf-go/pdf.go | 8 +- 6 files changed, 1717 insertions(+), 8 deletions(-) create mode 100644 minipdf-go/docx_font.go create mode 100644 minipdf-go/docx_render.go create mode 100644 minipdf-go/docx_render_test.go diff --git a/minipdf-go/README.md b/minipdf-go/README.md index c660d926..9d69ca3c 100644 --- a/minipdf-go/README.md +++ b/minipdf-go/README.md @@ -8,7 +8,8 @@ The experimental native Go implementation of MiniPdf. It provides a reusable > The Go implementation is an early foundation and is not feature-equivalent > with MiniPdf for .NET or Rust. It currently renders basic text from XLSX, -> DOCX, and PPTX packages. Complex Office layout is not yet supported. +> DOCX, and PPTX packages, and a supported subset of DOCX tables. Complex +> Office layout is not yet supported. ## Requirements @@ -87,8 +88,11 @@ defer minipdf.ClearRegisteredFonts() ``` Registered `.ttf` fonts are embedded as Type0/CID fonts with ToUnicode maps. -Font subsetting, TrueType Collections, automatic system-font discovery, and -complex-script shaping are not yet implemented. +The structured DOCX table renderer also accepts registered TrueType collections +(the first face), and resolves selected installed Windows fonts, including +SimSun/SimHei and their Chinese family names. Collections are rebuilt as standalone +sfnt fonts before PDF embedding. Font subsetting and complex-script shaping are +not yet implemented. Font binaries are not distributed with MiniPdf. Override the output page size: @@ -120,7 +124,7 @@ minipdf data.xlsx --max-rows 100 --max-columns 12 --landscape | Capability | Go status | |---|---| | XLSX to PDF | Basic cell text and shared strings | -| DOCX to PDF | Basic paragraphs, tabs, line breaks, and explicit page breaks | +| DOCX to PDF | Basic text; supported table documents also preserve grid widths, horizontal/vertical merges, borders, fills, run fonts, highlights, paragraph spacing/alignment, and PNG/JPEG drawings | | PPTX to PDF | Basic slide text | | PDF output | Dependency-free PDF 1.4 writer | | Page size | Office geometry, A4/Letter presets, or custom points | @@ -128,6 +132,28 @@ minipdf data.xlsx --max-rows 100 --max-columns 12 --landscape | Input safety | Bounded ZIP entry count, size, expansion ratio, encryption, and path validation | | Interfaces | Go package with file, byte, and stream APIs; native CLI | +Single-page DOCX documents with non-nested, non-floating table grids can use the +structured renderer. It resolves paragraph/style inheritance and theme fonts, uses glyph +metrics for wrapping, respects East Asian punctuation boundaries, and honors +Word's FE-layout CJK metric compatibility flag. Images use their package +relationships and DrawingML extents; simple VML text-box images use their margin +offsets and text-box insets. Image decoding is limited to 25 million pixels. +Missing or invalid embedded images produce errors rather than disappearing. + +Documents requiring unsupported section changes, numbering, anchored drawings, +exact-height rows, multi-page structured layout, or referenced EMF/WMF images +retain the existing text converter. If measured paragraph, inline-image, or +table flow would cross the first page's content boundary, the entire document +uses the text converter; structured pagination, keep rules, and row splitting +are not implemented. Metafile formats are checked before +structured rendering; malformed PNG/JPEG images still produce decoding errors. +Images inside table cells, complex drawing geometry, conditional +table styles, headers/footers, and complex-script shaping remain unsupported. +Structured text uses matching registered fonts or selected licensed Windows +fonts, with Arial/SimSun glyph fallback when available. Missing Unicode glyphs +produce an explicit error on the structured path. Fonts are embedded without +subsetting, so large CJK fonts can substantially increase PDF size. + XLSX text output skips entirely empty pages within worksheets containing text, including empty horizontal column groups and pages from trailing formatted rows. Blank rows within nonempty pages retain their spacing. Layout-only worksheets diff --git a/minipdf-go/docx.go b/minipdf-go/docx.go index a01df532..9e859415 100644 --- a/minipdf-go/docx.go +++ b/minipdf-go/docx.go @@ -32,6 +32,9 @@ func convertDOCX(input []byte, options ConversionOptions) ([]byte, error) { if err := validateDOCXLayout(effectivePageSize, effectiveMargins); err != nil { return nil, err } + if rendered, supported, err := renderDOCXTables(files, documentXML, effectivePageSize, effectiveMargins, options); supported || err != nil { + return rendered, err + } textPages := make([]textPage, len(pages)) for index, lines := range pages { textPages[index] = textPage{lines: lines, size: pageSize, margins: margins} diff --git a/minipdf-go/docx_font.go b/minipdf-go/docx_font.go new file mode 100644 index 00000000..e13386bc --- /dev/null +++ b/minipdf-go/docx_font.go @@ -0,0 +1,172 @@ +package minipdf + +import ( + "encoding/binary" + "fmt" + "os" + "path/filepath" + "runtime" + "strings" + + "golang.org/x/image/font" + "golang.org/x/image/font/sfnt" + "golang.org/x/image/math/fixed" +) + +// A PDF FontFile2 contains a single sfnt, not the enclosing TTC. Rebuild its +// directory because offsets inside a collection are relative to the TTC file. +func docxCollectionFont(data []byte, index int) ([]byte, error) { + if len(data) < 12 || string(data[:4]) != "ttcf" { + if index == 0 { + return data, nil + } + return nil, fmt.Errorf("font is not a collection") + } + count := int(binary.BigEndian.Uint32(data[8:12])) + if index < 0 || index >= count || count > (len(data)-12)/4 { + return nil, fmt.Errorf("invalid font collection directory") + } + offset := int(binary.BigEndian.Uint32(data[12+index*4 : 16+index*4])) + if offset < 0 || offset > len(data)-12 { + return nil, fmt.Errorf("invalid font collection offset") + } + n := int(binary.BigEndian.Uint16(data[offset+4 : offset+6])) + if n > (len(data)-offset-12)/16 { + return nil, fmt.Errorf("invalid sfnt directory") + } + out := append([]byte(nil), data[offset:offset+12+n*16]...) + for i := 0; i < n; i++ { + record := out[12+i*16 : 28+i*16] + start := int(binary.BigEndian.Uint32(record[8:12])) + length := int(binary.BigEndian.Uint32(record[12:16])) + if start < 0 || length < 0 || start > len(data) || length > len(data)-start { + return nil, fmt.Errorf("invalid sfnt table bounds") + } + binary.BigEndian.PutUint32(record[8:12], uint32(len(out))) + out = append(out, data[start:start+length]...) + for len(out)%4 != 0 { + out = append(out, 0) + } + } + // checkSumAdjustment is defined for the standalone font, not its collection. + for i := 0; i < n; i++ { + r := out[12+i*16 : 28+i*16] + if string(r[:4]) == "head" { + o := int(binary.BigEndian.Uint32(r[8:12])) + if int(binary.BigEndian.Uint32(r[12:16])) < 12 { + return nil, fmt.Errorf("invalid head table") + } + binary.BigEndian.PutUint32(out[o+8:o+12], 0) + var sum uint32 + for j := 0; j < len(out); j += 4 { + sum += binary.BigEndian.Uint32(out[j : j+4]) + } + binary.BigEndian.PutUint32(out[o+8:o+12], 0xB1B0AFBA-sum) + } + } + return out, nil +} + +func loadDOCXFont(name string, bold, italic bool) (*embeddedFont, error) { + variant := name + if bold { + variant += " Bold" + } + if italic { + variant += " Italic" + } + var data []byte + for _, registered := range RegisteredFonts() { + if strings.EqualFold(registered.Name, variant) { + data = registered.Data + break + } + } + if data == nil && runtime.GOOS == "windows" { + var file string + switch strings.ToLower(name) { + case "宋体", "simsun": + file = "simsun.ttc" + case "黑体", "simhei": + file = "simhei.ttf" + case "楷体", "kaiti": + file = "simkai.ttf" + case "仿宋", "fangsong": + file = "simfang.ttf" + case "times new roman": + file = "times.ttf" + if bold { + file = "timesbd.ttf" + } + if italic { + file = "timesi.ttf" + if bold { + file = "timesbi.ttf" + } + } + default: + return readXLSXFont(name, bold, italic) + } + if root := os.Getenv("WINDIR"); root != "" { + var err error + data, err = os.ReadFile(filepath.Join(root, "Fonts", file)) + if os.IsNotExist(err) { + return nil, nil + } + if err != nil { + return nil, err + } + } + } + if data == nil { + return nil, nil + } + data, err := docxCollectionFont(data, 0) + if err != nil { + return nil, fmt.Errorf("DOCX font %q: %w", name, err) + } + parsed, err := sfnt.Parse(data) + if err != nil { + return nil, fmt.Errorf("DOCX font %q: %w", name, err) + } + if postscript, err := parsed.Name(nil, sfnt.NameIDPostScript); err == nil && postscript != "" { + variant = postscript + } + ppem := fixed.Int26_6(parsed.UnitsPerEm()) + metrics, err := parsed.Metrics(nil, ppem, font.HintingNone) + if err != nil { + return nil, err + } + result := &embeddedFont{name: sanitizePDFFontName(variant), data: data, font: parsed, runeToGlyph: make(map[rune]sfnt.GlyphIndex), glyphToRune: make(map[sfnt.GlyphIndex]rune), advances: make([]float64, parsed.NumGlyphs()), ascent: float64(metrics.Ascent) / float64(ppem), descent: float64(metrics.Descent) / float64(ppem)} + for i := range result.advances { + a, err := parsed.GlyphAdvance(nil, sfnt.GlyphIndex(i), ppem, font.HintingNone) + if err != nil { + return nil, err + } + result.advances[i] = float64(a) / float64(ppem) + } + return result, nil +} + +func docxFontCJK(data []byte) bool { + if len(data) < 12 { + return false + } + n := int(binary.BigEndian.Uint16(data[4:6])) + if n > (len(data)-12)/16 { + return false + } + for i := 0; i < n; i++ { + r := data[12+i*16 : 28+i*16] + if string(r[:4]) != "OS/2" { + continue + } + o := int(binary.BigEndian.Uint32(r[8:12])) + length := int(binary.BigEndian.Uint32(r[12:16])) + if length < 86 || o < 0 || o > len(data)-length { + return false + } + return binary.BigEndian.Uint32(data[o+78:o+82])&(0xf<<17) != 0 + } + return false +} diff --git a/minipdf-go/docx_render.go b/minipdf-go/docx_render.go new file mode 100644 index 00000000..aae45569 --- /dev/null +++ b/minipdf-go/docx_render.go @@ -0,0 +1,1141 @@ +package minipdf + +import ( + "bytes" + "encoding/xml" + "fmt" + "image" + _ "image/jpeg" + _ "image/png" + "io" + "math" + "path" + "strconv" + "strings" + "unicode" +) + +type docxNode struct { + name string + attrs map[string]string + children []*docxNode + text string +} + +func parseDOCXNode(data []byte) (*docxNode, error) { + root := &docxNode{} + stack := []*docxNode{root} + dec := xml.NewDecoder(bytes.NewReader(data)) + for { + token, err := dec.Token() + if err == io.EOF { + break + } + if err != nil { + return nil, err + } + switch t := token.(type) { + case xml.StartElement: + n := &docxNode{name: t.Name.Local, attrs: map[string]string{}} + for _, a := range t.Attr { + n.attrs[a.Name.Local] = a.Value + } + p := stack[len(stack)-1] + p.children = append(p.children, n) + stack = append(stack, n) + case xml.EndElement: + stack = stack[:len(stack)-1] + case xml.CharData: + stack[len(stack)-1].text += string(t) + } + } + if len(root.children) == 0 { + return nil, fmt.Errorf("empty DOCX XML") + } + return root.children[0], nil +} +func (n *docxNode) child(name string) *docxNode { + if n != nil { + for _, c := range n.children { + if c.name == name { + return c + } + } + } + return nil +} +func (n *docxNode) attr(key string) string { + if n == nil { + return "" + } + return n.attrs[key] +} +func (n *docxNode) value(name string) string { return n.child(name).attr("val") } +func (n *docxNode) descendants(name string) []*docxNode { + var out []*docxNode + if n != nil { + for _, c := range n.children { + if c.name == name { + out = append(out, c) + } + out = append(out, c.descendants(name)...) + } + } + return out +} +func docxNumber(s string) float64 { + v, _ := strconv.ParseFloat(s, 64) + if math.IsNaN(v) || math.IsInf(v, 0) { + return 0 + } + return v +} +func docxOn(n *docxNode) bool { + return n != nil && n.attr("val") != "0" && n.attr("val") != "false" && n.attr("val") != "off" +} + +type docxRunStyle struct { + size float64 + fonts map[string]string + bold, italic bool + color PDFColor + highlight *PDFColor + lang string +} +type docxParagraphStyle struct { + run docxRunStyle + before, after, line float64 + lineRule, align string + left, right, first float64 + snap bool +} +type docxStyles struct { + nodes map[string]*docxNode + defaults docxParagraphStyle + theme map[string]string + fonts map[string]*embeddedFont + cjkMetrics bool +} + +func (s *docxStyles) applyRun(style docxRunStyle, n *docxNode) docxRunStyle { + f := map[string]string{} + for k, v := range style.fonts { + f[k] = v + } + style.fonts = f + if n == nil { + return style + } + if sz := n.child("sz"); sz != nil && docxNumber(sz.attr("val")) > 0 { + style.size = docxNumber(sz.attr("val")) / 2 + } + if fonts := n.child("rFonts"); fonts != nil { + for _, key := range []string{"ascii", "hAnsi", "eastAsia", "cs"} { + if v := fonts.attr(key); v != "" { + style.fonts[key] = v + delete(style.fonts, key+"Theme") + } + if v := fonts.attr(key + "Theme"); v != "" { + style.fonts[key+"Theme"] = v + delete(style.fonts, key) + } + } + } + if n.child("b") != nil { + style.bold = docxOn(n.child("b")) + } + if n.child("i") != nil { + style.italic = docxOn(n.child("i")) + } + if lang := n.child("lang").attr("eastAsia"); lang != "" { + style.lang = lang + } + if c := n.child("color"); c != nil { + style.color = docxColor(c.attr("val")) + } + if h := n.child("highlight"); h != nil { + style.highlight = nil + colors := map[string]string{"yellow": "FFFF00", "green": "00FF00", "cyan": "00FFFF", "magenta": "FF00FF", "blue": "0000FF", "red": "FF0000", "darkYellow": "808000"} + if v, ok := colors[h.attr("val")]; ok { + c := docxColor(v) + style.highlight = &c + } + } + return style +} +func (s *docxStyles) applyParagraph(style docxParagraphStyle, n *docxNode) docxParagraphStyle { + if n == nil { + return style + } + style.run = s.applyRun(style.run, n.child("rPr")) + if p := n.child("spacing"); p != nil { + for _, item := range []struct { + key string + out *float64 + }{{"before", &style.before}, {"after", &style.after}, {"line", &style.line}} { + if v, ok := p.attrs[item.key]; ok { + *item.out = docxNumber(v) / 20 + } + } + if p.attr("lineRule") != "" { + style.lineRule = p.attr("lineRule") + } + } + if p := n.child("jc"); p != nil { + style.align = p.attr("val") + } + if p := n.child("ind"); p != nil { + for _, item := range []struct { + key string + out *float64 + }{{"left", &style.left}, {"right", &style.right}, {"firstLine", &style.first}} { + if v, ok := p.attrs[item.key]; ok { + *item.out = docxNumber(v) / 20 + } + } + if v, ok := p.attrs["hanging"]; ok { + style.first = -docxNumber(v) / 20 + } + } + if p := n.child("snapToGrid"); p != nil { + style.snap = docxOn(p) + } + return style +} +func (s *docxStyles) inherit(style docxParagraphStyle, id string, seen map[string]bool) docxParagraphStyle { + if seen[id] { + return style + } + seen[id] = true + n := s.nodes[id] + if n == nil { + return style + } + style = s.inherit(style, n.value("basedOn"), seen) + style = s.applyParagraph(style, n.child("pPr")) + style.run = s.applyRun(style.run, n.child("rPr")) + return style +} +func (s *docxStyles) paragraph(n *docxNode) docxParagraphStyle { + style := s.defaults + pr := n.child("pPr") + style = s.inherit(style, pr.value("pStyle"), map[string]bool{}) + return s.applyParagraph(style, pr) +} +func docxColor(value string) PDFColor { + v, err := strconv.ParseUint(value, 16, 24) + if err != nil { + return PDFColorBlack + } + return PDFColor{Red: float64(v>>16) / 255, Green: float64((v>>8)&255) / 255, Blue: float64(v&255) / 255} +} + +func readDOCXStyles(files officePackage) (*docxStyles, error) { + s := &docxStyles{nodes: map[string]*docxNode{}, theme: map[string]string{}, fonts: map[string]*embeddedFont{}, defaults: docxParagraphStyle{run: docxRunStyle{size: 12, fonts: map[string]string{"ascii": "Arial", "eastAsia": "SimSun"}}, snap: true}} + if _, ok := files["word/settings.xml"]; ok { + data, err := files.read("word/settings.xml") + if err != nil { + return nil, err + } + root, err := parseDOCXNode(data) + if err != nil { + return nil, err + } + s.cjkMetrics = docxOn(root.child("compat").child("useFELayout")) + } + if _, ok := files["word/theme/theme1.xml"]; ok { + data, err := files.read("word/theme/theme1.xml") + if err != nil { + return nil, err + } + root, err := parseDOCXNode(data) + if err != nil { + return nil, err + } + for _, group := range []string{"major", "minor"} { + for _, n := range root.descendants(group + "Font") { + s.theme[group+"HAnsi"] = n.child("latin").attr("typeface") + s.theme[group+"Ascii"] = s.theme[group+"HAnsi"] + s.theme[group+"EastAsia"] = n.child("ea").attr("typeface") + for _, f := range n.children { + if f.name == "font" { + s.theme[group+":"+f.attr("script")] = f.attr("typeface") + } + } + } + } + } + if _, ok := files["word/styles.xml"]; ok { + data, err := files.read("word/styles.xml") + if err != nil { + return nil, err + } + root, err := parseDOCXNode(data) + if err != nil { + return nil, err + } + d := root.child("docDefaults") + s.defaults.run = s.applyRun(s.defaults.run, d.child("rPrDefault").child("rPr")) + s.defaults = s.applyParagraph(s.defaults, d.child("pPrDefault").child("pPr")) + normal := "" + for _, n := range root.children { + if n.name == "style" { + s.nodes[n.attr("styleId")] = n + if n.attr("type") == "paragraph" && n.attr("default") == "1" { + normal = n.attr("styleId") + } + } + } + s.defaults = s.inherit(s.defaults, normal, map[string]bool{}) + } + return s, nil +} +func (s *docxStyles) font(style docxRunStyle, r rune) (*embeddedFont, error) { + key := "ascii" + if r > 127 { + key = "hAnsi" + } + if docxAsian(r) { + key = "eastAsia" + } + name := style.fonts[key] + if theme := style.fonts[key+"Theme"]; theme != "" { + name = s.theme[theme] + if strings.HasSuffix(theme, "EastAsia") && name == "" { + script := "Hans" + if strings.HasPrefix(style.lang, "ja") { + script = "Jpan" + } + if strings.HasPrefix(style.lang, "ko") { + script = "Hang" + } + if style.lang == "zh-TW" || style.lang == "zh-HK" { + script = "Hant" + } + name = s.theme[strings.TrimSuffix(theme, "EastAsia")+":"+script] + } + } + if name == "" { + name = "Arial" + if docxAsian(r) { + name = "SimSun" + } + } + cache := fmt.Sprintf("%s/%t/%t", name, style.bold, style.italic) + f, ok := s.fonts[cache] + if !ok { + var err error + f, err = loadDOCXFont(name, style.bold, style.italic) + if err != nil { + return nil, err + } + s.fonts[cache] = f + } + if r > 127 && (f == nil || !f.canEncode(string(r))) { + fallback := "Arial" + if docxAsian(r) { + fallback = "SimSun" + } + fallbackKey := fmt.Sprintf("%s/%t/%t", fallback, style.bold, style.italic) + if fallbackKey != cache { + fallbackFont, ok := s.fonts[fallbackKey] + if !ok { + var err error + fallbackFont, err = loadDOCXFont(fallback, style.bold, style.italic) + if err != nil { + return nil, err + } + s.fonts[fallbackKey] = fallbackFont + } + if fallbackFont != nil && fallbackFont.canEncode(string(r)) { + return fallbackFont, nil + } + } + } + return f, nil +} +func docxAsian(r rune) bool { + return unicode.Is(unicode.Han, r) || unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r) || r >= 0x3000 && r <= 0x303f || r >= 0xff00 && r <= 0xffef || r == 0x25a1 +} + +type docxGlyph struct { + r rune + style docxRunStyle + font *embeddedFont + width, ascent, descent float64 +} +type docxLine struct { + glyphs []docxGlyph + width, height, ascent float64 +} +type docxParagraph struct { + style docxParagraphStyle + lines []docxLine + height float64 +} +type docxImage struct { + op imageOperation + floating bool + left, top float64 +} + +func (s *docxStyles) layoutParagraph(n *docxNode, width, grid float64) (docxParagraph, error) { + p := docxParagraph{style: s.paragraph(n)} + var glyphs []docxGlyph + var visit func(*docxNode, docxRunStyle) error + visit = func(n *docxNode, style docxRunStyle) error { + if n.name == "pPr" || n.name == "rPr" || n.name == "pict" || n.name == "drawing" { + return nil + } + if n.name == "r" { + if id := n.child("rPr").value("rStyle"); id != "" { + inherited := s.inherit(docxParagraphStyle{run: style}, id, map[string]bool{}) + style = inherited.run + } + style = s.applyRun(style, n.child("rPr")) + } + text := "" + if n.name == "t" { + text = n.text + } + if n.name == "tab" { + text = " " + } + if n.name == "br" || n.name == "cr" { + text = "\n" + } + for _, r := range text { + if r == '\n' { + glyphs = append(glyphs, docxGlyph{r: r}) + continue + } + f, err := s.font(style, r) + if err != nil { + return err + } + g := docxGlyph{r: r, style: style, font: f, width: style.size * .5, ascent: style.size * .9, descent: style.size * .25} + if docxAsian(r) { + g.width = style.size + } + if f != nil { + if !f.canEncode(string(r)) { + return fmt.Errorf("DOCX font %q has no glyph for U+%04X", f.name, r) + } + g.width = measureXLSXFont(string(r), style.size, f) + g.ascent = f.ascent * style.size + g.descent = f.descent * style.size + // Writer fntcache.cxx, lcl_ApplyCjkHeightAdjustment: Word's + // FE layout scales metrics for fonts advertising CJK code pages. + if s.cjkMetrics && grid == 0 && docxFontCJK(f.data) { + g.ascent *= 1.27 + g.descent *= 1.27 + } + } else if r > 127 { + return fmt.Errorf("DOCX character U+%04X requires an installed or registered font", r) + } + glyphs = append(glyphs, g) + } + for _, c := range n.children { + if err := visit(c, style); err != nil { + return err + } + } + return nil + } + if err := visit(n, p.style.run); err != nil { + return p, err + } + emptyFont, err := s.font(p.style.run, ' ') + if err != nil { + return p, err + } + // Word's automatic East Asian/Latin spacing is a quarter-em at script boundaries. + for i := 1; i < len(glyphs); i++ { + a, b := glyphs[i-1].r, glyphs[i].r + if (unicode.Is(unicode.Han, a) && docxLatin(b)) || (docxLatin(a) && unicode.Is(unicode.Han, b)) { + glyphs[i-1].width += math.Min(glyphs[i-1].style.size, glyphs[i].style.size) / 4 + } + } + available := math.Max(1, width-p.style.left-p.style.right) + var line docxLine + flush := func() { + if len(line.glyphs) == 0 { + f := emptyFont + line.ascent = p.style.run.size * .9 + line.height = p.style.run.size * 1.3 + if f != nil { + line.ascent = f.ascent * p.style.run.size + line.height = (f.ascent + f.descent) * p.style.run.size + if s.cjkMetrics && grid == 0 && docxFontCJK(f.data) { + line.ascent *= 1.27 + line.height *= 1.27 + } + } + } + line.height = math.Max(line.height, line.ascent) + if grid > 0 && p.style.snap { + h := math.Ceil((line.height-.001)/grid) * grid + line.ascent += (h - line.height) / 2 + line.height = h + } + if p.style.line > 0 { + if p.style.lineRule == "exact" { + line.height = p.style.line + line.ascent = math.Min(line.ascent, line.height*.8) + } else if p.style.lineRule == "atLeast" { + line.height = math.Max(line.height, p.style.line) + } else { + h := line.height * p.style.line / 12 + if grid > 0 && p.style.snap { + h = math.Ceil((h-.001)/grid) * grid + } + line.ascent += (h - line.height) / 2 + line.height = h + } + } + p.height += line.height + p.lines = append(p.lines, line) + line = docxLine{} + } + for i := 0; i < len(glyphs); i++ { + g := glyphs[i] + if g.r == '\n' { + flush() + continue + } + limit := available + if len(p.lines) == 0 { + limit -= p.style.first + } + if line.width+g.width > limit+.01 && len(line.glyphs) > 0 && !(len(line.glyphs) == 1 && (strings.ContainsRune("(([【《「『", line.glyphs[0].r) || strings.ContainsRune("),。!?、;:)]】》」』", g.r))) { + var carry []docxGlyph + last := line.glyphs[len(line.glyphs)-1] + if strings.ContainsRune("(([【《「『", last.r) || strings.ContainsRune("),。!?、;:)]】》」』", g.r) { + carry = []docxGlyph{last} + line.glyphs = line.glyphs[:len(line.glyphs)-1] + line.width -= last.width + } else if docxLatin(g.r) && docxLatin(last.r) { + for j := len(line.glyphs) - 1; j >= 0; j-- { + if unicode.IsSpace(line.glyphs[j].r) { + carry = append(carry, line.glyphs[j+1:]...) + line.glyphs = line.glyphs[:j+1] + line.width = 0 + for _, c := range line.glyphs { + line.width += c.width + } + break + } + } + } + flush() + for _, c := range carry { + line.glyphs = append(line.glyphs, c) + line.width += c.width + line.ascent = math.Max(line.ascent, c.ascent) + line.height = math.Max(line.height, c.ascent+c.descent) + } + } + line.glyphs = append(line.glyphs, g) + line.width += g.width + line.ascent = math.Max(line.ascent, g.ascent) + line.height = math.Max(line.height, g.ascent+g.descent) + } + flush() + p.height += p.style.before + p.style.after + return p, nil +} +func docxLatin(r rune) bool { return r < 128 && (unicode.IsLetter(r) || unicode.IsDigit(r)) } + +func drawDOCXParagraph(page *PDFPage, p docxParagraph, x, top, width float64) { + top += p.style.before + for i, line := range p.lines { + left := x + p.style.left + if i == 0 { + left += p.style.first + } + switch p.style.align { + case "center": + left += (width - p.style.left - p.style.right - line.width) / 2 + case "right": + left += width - p.style.left - p.style.right - line.width + } + baseline := page.Height - top - line.ascent + for _, g := range line.glyphs { + if g.style.highlight != nil { + page.AddRect(left, page.Height-top-line.height, g.width, line.height, *g.style.highlight) + } + page.operations = append(page.operations, textOperation{text: string(g.r), x: left, y: baseline, fontSize: g.style.size, horizontalScale: 100, color: g.style.color, bold: g.style.bold, italic: g.style.italic, embedded: g.font}) + left += g.width + } + top += line.height + } +} + +type docxBorder struct { + style string + width float64 + color PDFColor +} + +func readDOCXBorder(n *docxNode) docxBorder { + if n == nil { + return docxBorder{} + } + return docxBorder{style: n.attr("val"), width: docxNumber(n.attr("sz")) / 8, color: docxColor(n.attr("color"))} +} +func docxBorders(n *docxNode, inherited map[string]docxBorder) map[string]docxBorder { + b := map[string]docxBorder{} + for k, v := range inherited { + b[k] = v + } + if n != nil { + for _, c := range n.children { + b[c.name] = readDOCXBorder(c) + } + } + return b +} + +type docxCell struct { + column, span, row, rowSpan int + continuation bool + width float64 + paras []docxParagraph + contentHeight float64 + margins Margins + borders map[string]docxBorder + align string + fill *PDFColor +} +type docxTable struct { + cells []*docxCell + columns, rows []float64 + width float64 + align string + indent float64 +} + +func (s *docxStyles) table(n *docxNode, available float64) (docxTable, error) { + t := docxTable{align: n.child("tblPr").value("jc")} + pr := n.child("tblPr") + var inherited map[string]docxBorder + tm := Margins{Left: 5.4, Right: 5.4} + var styleChain func(string, map[string]bool) + styleChain = func(id string, seen map[string]bool) { + if seen[id] { + return + } + seen[id] = true + if st := s.nodes[id]; st != nil { + styleChain(st.value("basedOn"), seen) + inherited = docxBorders(st.child("tblPr").child("tblBorders"), inherited) + tm = docxCellMargins(st.child("tblPr").child("tblCellMar"), tm) + } + } + styleChain(pr.value("tblStyle"), map[string]bool{}) + inherited = docxBorders(pr.child("tblBorders"), inherited) + tm = docxCellMargins(pr.child("tblCellMar"), tm) + t.indent = docxNumber(pr.child("tblInd").attr("w")) / 20 + for _, col := range n.child("tblGrid").children { + if col.name == "gridCol" { + t.columns = append(t.columns, docxNumber(col.attr("w"))/20) + } + } + for _, w := range t.columns { + t.width += w + } + if w := pr.child("tblW"); w.attr("type") == "dxa" && docxNumber(w.attr("w")) > 0 { + t.width = docxNumber(w.attr("w")) / 20 + } + if w := pr.child("tblW"); w.attr("type") == "pct" { + t.width = available * docxNumber(w.attr("w")) / 5000 + } + gridWidth := 0.0 + for _, w := range t.columns { + gridWidth += w + } + if gridWidth <= 0 || t.width <= 0 { + return t, fmt.Errorf("DOCX table has no positive grid") + } + for i := range t.columns { + t.columns[i] *= t.width / gridWidth + } + active := map[int]*docxCell{} + for _, row := range n.children { + if row.name != "tr" { + continue + } + ri := len(t.rows) + height := docxNumber(row.child("trPr").child("trHeight").attr("val")) / 20 + t.rows = append(t.rows, height) + column := 0 + for _, node := range row.children { + if node.name != "tc" { + continue + } + cp := node.child("tcPr") + span := int(docxNumber(cp.child("gridSpan").attr("val"))) + if span < 1 { + span = 1 + } + if column+span > len(t.columns) { + return t, fmt.Errorf("DOCX cell span exceeds table grid") + } + cell := &docxCell{column: column, span: span, row: ri, rowSpan: 1, margins: docxCellMargins(cp.child("tcMar"), tm), align: cp.value("vAlign"), borders: map[string]docxBorder{}} + for i := column; i < column+span; i++ { + cell.width += t.columns[i] + } + for _, edge := range []string{"top", "left", "bottom", "right"} { + key := edge + if edge == "top" && ri > 0 { + key = "insideH" + } + if edge == "left" && column > 0 { + key = "insideV" + } + if edge == "right" && column+span < len(t.columns) { + key = "insideV" + } + cell.borders[edge] = inherited[key] + } + cell.borders = docxBorders(cp.child("tcBorders"), cell.borders) + if shd := cp.child("shd"); shd != nil && shd.attr("fill") != "" && shd.attr("fill") != "auto" { + c := docxColor(shd.attr("fill")) + cell.fill = &c + } + if merge := cp.child("vMerge"); merge != nil { + if merge.attr("val") == "restart" { + active[column] = cell + } else if start := active[column]; start != nil && start.span == span { + start.rowSpan++ + cell.continuation = true + start.borders["bottom"] = cell.borders["bottom"] + } else { + return t, fmt.Errorf("DOCX vertical merge has no matching start") + } + } else { + delete(active, column) + } + if !cell.continuation { + for _, pn := range node.children { + if pn.name == "p" { + p, err := s.layoutParagraph(pn, math.Max(1, cell.width-cell.margins.Left-cell.margins.Right), 0) + if err != nil { + return t, err + } + cell.paras = append(cell.paras, p) + cell.contentHeight += p.height + } + } + } + t.cells = append(t.cells, cell) + column += span + } + if column != len(t.columns) { + return t, fmt.Errorf("DOCX row does not cover table grid") + } + } + for _, c := range t.cells { + if c.continuation || c.rowSpan > 1 { + continue + } + needed := c.contentHeight + c.margins.Top + c.margins.Bottom + docxBorderWidth(c.borders["top"]) + docxBorderWidth(c.borders["bottom"]) + t.rows[c.row] = math.Max(t.rows[c.row], needed) + } + for _, c := range t.cells { + if c.continuation || c.rowSpan == 1 { + continue + } + height := 0.0 + for i := c.row; i < c.row+c.rowSpan; i++ { + height += t.rows[i] + } + needed := c.contentHeight + c.margins.Top + c.margins.Bottom + docxBorderWidth(c.borders["top"]) + docxBorderWidth(c.borders["bottom"]) + if needed > height { + extra := (needed - height) / float64(c.rowSpan) + for i := c.row; i < c.row+c.rowSpan; i++ { + t.rows[i] += extra + } + } + } + return t, nil +} +func docxCellMargins(n *docxNode, m Margins) Margins { + for _, item := range []struct { + key string + out *float64 + }{{"left", &m.Left}, {"right", &m.Right}, {"top", &m.Top}, {"bottom", &m.Bottom}} { + if p := n.child(item.key); p != nil { + *item.out = docxNumber(p.attr("w")) / 20 + } + } + return m +} +func docxBorderWidth(b docxBorder) float64 { + if b.style == "" || b.style == "nil" || b.style == "none" { + return 0 + } + return math.Max(.25, b.width) +} +func drawDOCXBorder(page *PDFPage, b docxBorder, x1, y1, x2, y2 float64) { + w := docxBorderWidth(b) + if w == 0 { + return + } + if b.style == "dotted" || b.style == "dotDash" || b.style == "dashed" { + length := math.Hypot(x2-x1, y2-y1) + if length == 0 { + return + } + dash := w * 2 + if b.style == "dashed" { + dash = w * 6 + } + for at := 0.0; at < length; at += dash + w*2 { + end := math.Min(length, at+dash) + page.AddLine(x1+(x2-x1)*at/length, y1+(y2-y1)*at/length, x1+(x2-x1)*end/length, y1+(y2-y1)*end/length, b.color, w) + } + return + } + page.AddLine(x1, y1, x2, y2, b.color, w) +} +func drawDOCXTable(page *PDFPage, t docxTable, x, top float64) { + xs := []float64{x} + ys := []float64{top} + for _, w := range t.columns { + xs = append(xs, xs[len(xs)-1]+w) + } + for _, h := range t.rows { + ys = append(ys, ys[len(ys)-1]+h) + } + for _, c := range t.cells { + if c.continuation { + continue + } + left, right := xs[c.column], xs[c.column+c.span] + upper, lower := ys[c.row], ys[c.row+c.rowSpan] + if c.fill != nil { + page.AddRect(left, page.Height-lower, right-left, lower-upper, *c.fill) + } + drawDOCXBorder(page, c.borders["top"], left, page.Height-upper, right, page.Height-upper) + drawDOCXBorder(page, c.borders["bottom"], left, page.Height-lower, right, page.Height-lower) + drawDOCXBorder(page, c.borders["left"], left, page.Height-upper, left, page.Height-lower) + drawDOCXBorder(page, c.borders["right"], right, page.Height-upper, right, page.Height-lower) + contentTop := upper + c.margins.Top + docxBorderWidth(c.borders["top"]) + space := lower - upper - c.margins.Top - c.margins.Bottom - docxBorderWidth(c.borders["top"]) - docxBorderWidth(c.borders["bottom"]) - c.contentHeight + if c.align == "center" { + contentTop += space / 2 + } + if c.align == "bottom" { + contentTop += space + } + for _, p := range c.paras { + drawDOCXParagraph(page, p, left+c.margins.Left, contentTop, c.width-c.margins.Left-c.margins.Right) + contentTop += p.height + } + } +} + +func readDOCXImages(files officePackage, n *docxNode) ([]docxImage, error) { + if len(n.descendants("blip")) == 0 { + return nil, nil + } + data, err := files.read("word/_rels/document.xml.rels") + if err != nil { + return nil, err + } + rels, err := parseDOCXNode(data) + if err != nil { + return nil, err + } + targets := map[string]string{} + for _, r := range rels.children { + if r.attr("TargetMode") != "External" { + targets[r.attr("Id")] = r.attr("Target") + } + } + var out []docxImage + var visit func(*docxNode, *docxNode) error + visit = func(n, shape *docxNode) error { + if n.name == "shape" { + shape = n + } + if n.name == "drawing" { + blips := n.descendants("blip") + extents := n.descendants("extent") + if len(blips) == 0 || len(extents) == 0 { + return fmt.Errorf("unsupported DOCX drawing") + } + target, ok := targets[blips[0].attr("embed")] + if !ok { + return fmt.Errorf("DOCX drawing image relationship is missing") + } + name := path.Join("word", target) + data, err := files.read(name) + if err != nil { + return err + } + config, _, err := image.DecodeConfig(bytes.NewReader(data)) + if err != nil { + return fmt.Errorf("read DOCX image %s: %w", name, err) + } + if config.Width <= 0 || config.Height <= 0 || int64(config.Width) > 25000000/int64(config.Height) { + return fmt.Errorf("DOCX image exceeds 25 million pixels") + } + source, _, err := image.Decode(bytes.NewReader(data)) + if err != nil { + return fmt.Errorf("decode DOCX image %s: %w", name, err) + } + op, err := newPDFImage(source, source.Bounds()) + if err != nil { + return err + } + op.width = docxNumber(extents[0].attr("cx")) / 12700 + op.height = docxNumber(extents[0].attr("cy")) / 12700 + if op.width <= 0 || op.height <= 0 { + return fmt.Errorf("DOCX image has invalid extent") + } + picture := docxImage{op: op} + if shape != nil { + picture.floating = true + props := map[string]string{} + for _, v := range strings.Split(shape.attr("style"), ";") { + kv := strings.SplitN(v, ":", 2) + if len(kv) == 2 { + props[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1]) + } + } + left, err := docxVMLLength(props["margin-left"]) + if err != nil { + return err + } + top, err := docxVMLLength(props["margin-top"]) + if err != nil { + return err + } + insetLeft, insetTop := 7.2, 3.6 + if inset := shape.child("textbox").attr("inset"); inset != "" { + parts := strings.Split(inset, ",") + if len(parts) != 4 { + return fmt.Errorf("invalid DOCX text-box inset") + } + insetLeft, err = docxVMLLength(parts[0]) + if err != nil { + return err + } + insetTop, err = docxVMLLength(parts[1]) + if err != nil { + return err + } + } + picture.left = left + insetLeft + picture.top = top + insetTop + } + out = append(out, picture) + return nil + } + for _, c := range n.children { + if err := visit(c, shape); err != nil { + return err + } + } + return nil + } + if err := visit(n, nil); err != nil { + return nil, err + } + return out, nil +} + +func docxUnsupportedMetafiles(files officePackage, body *docxNode) (bool, error) { + blips := body.descendants("blip") + if len(blips) == 0 { + return false, nil + } + data, err := files.read("word/_rels/document.xml.rels") + if err != nil { + return false, err + } + rels, err := parseDOCXNode(data) + if err != nil { + return false, err + } + targets := map[string]string{} + for _, rel := range rels.children { + if rel.attr("TargetMode") != "External" { + targets[rel.attr("Id")] = rel.attr("Target") + } + } + unsupported := false + for _, blip := range blips { + target, ok := targets[blip.attr("embed")] + if !ok { + return false, fmt.Errorf("DOCX drawing image relationship is missing") + } + name := path.Join("word", target) + if _, ok := files[name]; !ok { + return false, fmt.Errorf("Office package part %q is missing", name) + } + switch strings.ToLower(path.Ext(target)) { + case ".emf", ".wmf": + unsupported = true + } + } + return unsupported, nil +} + +func docxVMLLength(value string) (float64, error) { + value = strings.TrimSpace(value) + if value == "" { + return 0, nil + } + for _, unit := range []struct { + name string + scale float64 + }{{"pt", 1}, {"in", 72}, {"cm", 72 / 2.54}, {"mm", 72 / 25.4}, {"px", 72.0 / 96}} { + if strings.HasSuffix(value, unit.name) { + n, err := strconv.ParseFloat(strings.TrimSuffix(value, unit.name), 64) + if err != nil || math.IsNaN(n) || math.IsInf(n, 0) { + return 0, fmt.Errorf("invalid DOCX VML length %q", value) + } + return n * unit.scale, nil + } + } + if value == "0" { + return 0, nil + } + return 0, fmt.Errorf("unsupported DOCX VML length %q", value) +} + +// This structured renderer supports single-page table layouts. Page breaking, +// paragraph keep rules, and row splitting remain on the existing text path. +func renderDOCXTables(files officePackage, data []byte, size PageSize, margins Margins, options ConversionOptions) ([]byte, bool, error) { + root, err := parseDOCXNode(data) + if err != nil { + return nil, false, err + } + body := root.child("body") + if body == nil { + return nil, false, nil + } + if len(body.descendants("numPr")) > 0 || len(body.descendants("anchor")) > 0 || len(body.descendants("pageBreakBefore")) > 0 { + return nil, false, nil + } + for _, br := range body.descendants("br") { + if br.attr("type") == "page" { + return nil, false, nil + } + } + for _, height := range body.descendants("trHeight") { + if height.attr("hRule") == "exact" { + return nil, false, nil + } + } + for _, border := range append(body.descendants("tcBorders"), body.descendants("tblBorders")...) { + for _, edge := range border.children { + switch edge.attr("val") { + case "", "nil", "none", "single", "dotted", "dotDash", "dashed": + default: + return nil, false, nil + } + } + } + hasTable := false + for _, n := range body.children { + if n.name != "p" && n.name != "tbl" && n.name != "sectPr" && n.name != "bookmarkStart" && n.name != "bookmarkEnd" { + return nil, false, nil + } + if n.name == "tbl" { + hasTable = true + if len(n.descendants("tbl")) > 0 || len(n.descendants("drawing")) > 0 || n.child("tblPr").child("tblpPr") != nil || n.child("tblGrid") == nil { + return nil, false, nil + } + for _, c := range n.child("tblGrid").children { + if c.name == "gridCol" && docxNumber(c.attr("w")) <= 0 { + return nil, false, nil + } + } + } + if n.name == "p" && (n.child("pPr").child("sectPr") != nil || len(n.descendants("numPr")) > 0) { + return nil, false, nil + } + } + if !hasTable { + return nil, false, nil + } + unsupportedImages, err := docxUnsupportedMetafiles(files, body) + if err != nil { + return nil, true, err + } + if unsupportedImages { + return nil, false, nil + } + s, err := readDOCXStyles(files) + if err != nil { + return nil, true, err + } + grid := docxNumber(body.child("sectPr").child("docGrid").attr("linePitch")) / 20 + doc := NewPDFDocument() + page := doc.AddPage(size.Width, size.Height) + top := margins.Top + previousAfter := 0.0 + available := size.Width - margins.Left - margins.Right + for _, n := range body.children { + switch n.name { + case "p": + p, err := s.layoutParagraph(n, available, grid) + if err != nil { + return nil, true, err + } + p.style.before = math.Max(0, p.style.before-previousAfter) + p.height -= math.Min(previousAfter, s.paragraph(n).before) + if top+p.height > size.Height-margins.Bottom { + return nil, false, nil + } + drawDOCXParagraph(page, p, margins.Left, top, available) + pictures, err := readDOCXImages(files, n) + if err != nil { + return nil, true, err + } + for _, picture := range pictures { + op := picture.op + op.x = margins.Left + picture.left + op.y = size.Height - top - picture.top - op.height + if !picture.floating { + op.x = margins.Left + op.y = size.Height - top - op.height + p.height = math.Max(p.height, op.height) + } + page.operations = append(page.operations, op) + } + if top+p.height > size.Height-margins.Bottom { + return nil, false, nil + } + top += p.height + previousAfter = p.style.after + case "tbl": + t, err := s.table(n, available) + if err != nil { + return nil, true, err + } + height := 0.0 + for _, h := range t.rows { + height += h + } + if top+height > size.Height-margins.Bottom { + return nil, false, nil + } + left := margins.Left + t.indent + if t.align == "center" { + left = margins.Left + (available-t.width)/2 + } + if t.align == "right" { + left = size.Width - margins.Right - t.width + } + drawDOCXTable(page, t, left, top) + top += height + previousAfter = 0 + } + } + return doc.BytesWithOptions(PDFSaveOptions{Compress: options.Compress}), true, nil +} diff --git a/minipdf-go/docx_render_test.go b/minipdf-go/docx_render_test.go new file mode 100644 index 00000000..ebdf0342 --- /dev/null +++ b/minipdf-go/docx_render_test.go @@ -0,0 +1,367 @@ +package minipdf + +import ( + "bytes" + "encoding/binary" + "image" + "image/png" + "math" + "strings" + "testing" + + "golang.org/x/image/font/gofont/goregular" + "golang.org/x/image/font/sfnt" +) + +func docxTestStyles(t *testing.T, styles string) *docxStyles { + t.Helper() + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("Portable Test", goregular.TTF) + files, err := openOfficePackage(officePackageBytes(t, map[string]string{"word/styles.xml": styles})) + if err != nil { + t.Fatal(err) + } + s, err := readDOCXStyles(files) + if err != nil { + t.Fatal(err) + } + return s +} + +func TestDOCXTableGridMergedCellsAndInheritedStyles(t *testing.T) { + s := docxTestStyles(t, ``) + n, err := parseDOCXNode([]byte(`

Merged

Wide

Left

Right

`)) + if err != nil { + t.Fatal(err) + } + table, err := s.table(n, 300) + if err != nil { + t.Fatal(err) + } + if table.width != 240 || table.cells[0].rowSpan != 2 || !table.cells[2].continuation || table.cells[1].width != 180 { + t.Fatalf("incorrect table geometry: %+v", table) + } + page := NewPDFDocument().AddPage(400, 500) + drawDOCXTable(page, table, 30, 50) + var merged []textOperation + var highlighted bool + for _, op := range page.operations { + switch op := op.(type) { + case textOperation: + if op.text == "M" { + merged = append(merged, op) + } + case rectOperation: + highlighted = highlighted || op.color == (PDFColor{Red: 1, Green: 1}) + } + } + if len(merged) != 1 || merged[0].x != 30 || merged[0].y >= 450 || merged[0].y <= 410 || !highlighted { + t.Fatalf("merged text or highlight not rendered: %+v, highlight=%t", merged, highlighted) + } + if table.cells[0].borders["left"].width != 1 { + t.Fatal("table style border was lost") + } +} + +func TestDOCXRunFontAndParagraphInheritance(t *testing.T) { + s := docxTestStyles(t, ``) + n, err := parseDOCXNode([]byte(`

AΩ

`)) + if err != nil { + t.Fatal(err) + } + + p, err := s.layoutParagraph(n, 200, 0) + if err != nil { + t.Fatal(err) + } + if p.style.before != 2 || p.style.after != 4 || p.style.align != "center" { + t.Fatalf("paragraph inheritance lost: %+v", p.style) + } + g := p.lines[0].glyphs + if len(g) != 2 || g[0].style.size != 14 || g[1].style.size != 9 || g[1].font == nil || g[0].style.color.Red != 1 || g[1].style.color.Blue != 1 { + t.Fatalf("run inheritance lost: %+v", g) + } + if math.Abs(p.lines[0].width-(measureXLSXFont("A", 14, g[0].font)+measureXLSXFont("Ω", 9, g[1].font))) > .001 { + t.Fatal("layout did not use glyph advances") + } +} + +func TestDOCXWrappingKeepsLatinWordsTogether(t *testing.T) { + s := docxTestStyles(t, ``) + n, err := parseDOCXNode([]byte(`

alpha beta

`)) + if err != nil { + t.Fatal(err) + } + f, err := s.font(s.defaults.run, 'a') + if err != nil { + t.Fatal(err) + } + p, err := s.layoutParagraph(n, measureXLSXFont("alpha be", 10, f), 0) + if err != nil { + t.Fatal(err) + } + var lines []string + for _, line := range p.lines { + var text strings.Builder + for _, g := range line.glyphs { + text.WriteRune(g.r) + } + lines = append(lines, text.String()) + } + if strings.Join(lines, "|") != "alpha |beta" { + t.Fatalf("Latin word split at arbitrary glyph: %v", lines) + } +} + +func TestDOCXCollectionFontRebasesTables(t *testing.T) { + collection := make([]byte, 16+len(goregular.TTF)) + copy(collection, "ttcf") + binary.BigEndian.PutUint32(collection[4:8], 0x10000) + binary.BigEndian.PutUint32(collection[8:12], 1) + binary.BigEndian.PutUint32(collection[12:16], 16) + copy(collection[16:], goregular.TTF) + n := int(binary.BigEndian.Uint16(collection[20:22])) + for i := 0; i < n; i++ { + r := collection[28+i*16 : 44+i*16] + binary.BigEndian.PutUint32(r[8:12], binary.BigEndian.Uint32(r[8:12])+16) + } + standalone, err := docxCollectionFont(collection, 0) + if err != nil { + t.Fatal(err) + } + f, err := sfnt.Parse(standalone) + if err != nil { + t.Fatal(err) + } + glyph, err := f.GlyphIndex(nil, 'Ω') + if err != nil || glyph == 0 { + t.Fatalf("collection glyph lost: %d %v", glyph, err) + } + if _, err = docxCollectionFont(collection, 1); err == nil { + t.Fatal("out-of-range collection index accepted") + } + broken := bytes.Clone(collection) + binary.BigEndian.PutUint32(broken[12:16], uint32(len(broken))) + if _, err = docxCollectionFont(broken, 0); err == nil { + t.Fatal("invalid collection offset accepted") + } +} + +func TestDOCXEquivalentFontInstancesUseCanonicalPDFResource(t *testing.T) { + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("Portable Test", goregular.TTF) + a, err := loadDOCXFont("Portable Test", false, false) + if err != nil { + t.Fatal(err) + } + b, err := loadDOCXFont("Portable Test", false, false) + if err != nil { + t.Fatal(err) + } + d := NewPDFDocument() + p := d.AddPage(200, 200) + p.operations = append(p.operations, textOperation{text: "A", x: 10, y: 100, fontSize: 12, embedded: a}, textOperation{text: "Ω", x: 30, y: 100, fontSize: 12, embedded: b}) + pdf := d.Bytes() + if a.resource == "" || b.resource != a.resource || bytes.Count(pdf, []byte("/Subtype /Type0")) != 1 || bytes.Contains(pdf, []byte("BT / ")) { + t.Fatal("equivalent font instances have unresolved PDF resources") + } +} + +func TestDOCXMissingImageRelationshipReturnsError(t *testing.T) { + n, err := parseDOCXNode([]byte(`

`)) + if err != nil { + t.Fatal(err) + } + files, err := openOfficePackage(officePackageBytes(t, map[string]string{"word/_rels/document.xml.rels": ""})) + if err != nil { + t.Fatal(err) + } + _, err = readDOCXImages(files, n) + if err == nil || !strings.Contains(err.Error(), "relationship") { + t.Fatalf("missing image was silently discarded: %v", err) + } +} + +func TestDOCXPackagePictureExtentAndTextBoxInsets(t *testing.T) { + var encoded bytes.Buffer + if err := png.Encode(&encoded, image.NewRGBA(image.Rect(0, 0, 2, 3))); err != nil { + t.Fatal(err) + } + files, err := openOfficePackage(officePackageBytes(t, map[string]string{"word/_rels/document.xml.rels": ``, "word/media/picture.png": encoded.String()})) + if err != nil { + t.Fatal(err) + } + n, err := parseDOCXNode([]byte(`

`)) + if err != nil { + t.Fatal(err) + } + images, err := readDOCXImages(files, n) + if err != nil { + t.Fatal(err) + } + if len(images) != 1 || !images[0].floating || images[0].left != 75 || images[0].top != 6 || images[0].op.width != 20 || images[0].op.height != 30 || images[0].op.pixelWidth != 2 { + t.Fatalf("picture geometry lost: %+v", images) + } +} + +func TestDOCXFELayoutScalesCJKCapableFontMetrics(t *testing.T) { + data := bytes.Clone(goregular.TTF) + n := int(binary.BigEndian.Uint16(data[4:6])) + found := false + for i := 0; i < n; i++ { + r := data[12+i*16 : 28+i*16] + if string(r[:4]) == "OS/2" { + o := int(binary.BigEndian.Uint32(r[8:12])) + if binary.BigEndian.Uint32(r[12:16]) < 86 { + t.Skip("portable test font has no code-page capabilities") + } + binary.BigEndian.PutUint32(data[o+78:o+82], 1<<18) + found = true + } + } + if !found { + t.Fatal("test font has no OS/2 table") + } + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("CJK capable", data) + s := &docxStyles{nodes: map[string]*docxNode{}, fonts: map[string]*embeddedFont{}, theme: map[string]string{}, defaults: docxParagraphStyle{run: docxRunStyle{size: 10, fonts: map[string]string{"ascii": "CJK capable"}}}} + root, err := parseDOCXNode([]byte(`

A

`)) + if err != nil { + t.Fatal(err) + } + plain, err := s.layoutParagraph(root, 100, 0) + if err != nil { + t.Fatal(err) + } + s.cjkMetrics = true + adjusted, err := s.layoutParagraph(root, 100, 0) + if err != nil { + t.Fatal(err) + } + if math.Abs(adjusted.lines[0].height/plain.lines[0].height-1.27) > .001 || adjusted.lines[0].width != plain.lines[0].width { + t.Fatal("FE compatibility did not scale vertical metrics independently of glyph width") + } +} + +func TestDOCXEastAsianPunctuationDoesNotStartOrEndWrappedLine(t *testing.T) { + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + f, err := loadDOCXFont("SimSun", false, false) + if err != nil { + t.Fatal(err) + } + if f == nil { + t.Skip("requires licensed installed SimSun") + } + s := &docxStyles{nodes: map[string]*docxNode{}, fonts: map[string]*embeddedFont{"SimSun/false/false": f}, theme: map[string]string{}, defaults: docxParagraphStyle{run: docxRunStyle{size: 8, fonts: map[string]string{"eastAsia": "SimSun"}}}} + n, err := parseDOCXNode([]byte(`

机房(扶梯上机舱)

`)) + if err != nil { + t.Fatal(err) + } + p, err := s.layoutParagraph(n, 24.9, 0) + if err != nil { + t.Fatal(err) + } + var lines []string + for _, line := range p.lines { + var text strings.Builder + for _, g := range line.glyphs { + text.WriteRune(g.r) + } + lines = append(lines, text.String()) + } + if strings.Join(lines, "|") != "机房|(扶梯|上机|舱)" { + t.Fatalf("invalid East Asian punctuation break: %v", lines) + } +} + +func TestDOCXUnsupportedMetafilesRetainLegacyConversion(t *testing.T) { + document := `

Text preserved

Cell preserved

` + for _, extension := range []string{"emf", "WMF", "png", "jpeg"} { + t.Run(extension, func(t *testing.T) { + name := "word/media/image." + extension + input := officePackageBytes(t, map[string]string{ + "word/document.xml": document, + "word/_rels/document.xml.rels": ``, + name: "not a raster image", + }) + files, err := openOfficePackage(input) + if err != nil { + t.Fatal(err) + } + _, supported, err := renderDOCXTables(files, []byte(document), PageSizeA4, Margins{Left: 40, Top: 40, Right: 40, Bottom: 40}, ConversionOptions{}) + if extension == "png" || extension == "jpeg" { + if !supported || err == nil || !strings.Contains(err.Error(), "DOCX image") { + t.Fatalf("malformed raster image was swallowed: supported=%t, err=%v", supported, err) + } + return + } + if supported || err != nil { + t.Fatalf("metafile should retain legacy conversion: supported=%t, err=%v", supported, err) + } + pdf, err := ConvertBytesToPDF(input) + assertPDFContains(t, pdf, err, "Text preserved", "Cell preserved") + }) + } +} + +func TestDOCXStructuredPaginationRetainsLegacyConversion(t *testing.T) { + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("Portable Test", goregular.TTF) + const table = `

Table

` + for _, test := range []struct { + name, tail string + supported bool + }{ + {"table overflow", table, false}, + {"paragraph overflow", `

Tail

`, false}, + {"single-page flow", `

Tail

`, true}, + } { + t.Run(test.name, func(t *testing.T) { + data := []byte(`` + table + test.tail + ``) + input := officePackageBytes(t, map[string]string{ + "word/document.xml": string(data), + "word/styles.xml": ``, + }) + files, err := openOfficePackage(input) + if err != nil { + t.Fatal(err) + } + size := PageSize{Width: 200, Height: 100} + margins := Margins{Left: 10, Top: 10, Right: 10, Bottom: 10} + options := ConversionOptions{PageSize: &size, Margins: &margins} + _, supported, err := renderDOCXTables(files, data, size, margins, options) + if err != nil || supported != test.supported { + t.Fatalf("single-page capability: supported=%t, err=%v", supported, err) + } + if test.supported { + return + } + pages, sourceSize, sourceMargins, err := extractDOCX(data) + if err != nil { + t.Fatal(err) + } + var textPages []textPage + for _, lines := range pages { + textPages = append(textPages, textPage{lines: lines, size: sourceSize, margins: sourceMargins}) + } + want := renderTextPages(textPages, options) + got, err := ConvertBytesToPDFWithOptions(input, options) + if err != nil || !bytes.Equal(got, want) { + t.Fatalf("pagination fallback changed legacy PDF: %v", err) + } + }) + } +} + +func TestDOCXUnsupportedLayoutKeepsTextConverter(t *testing.T) { + data := []byte(`

First
Second

`) + _, supported, err := renderDOCXTables(nil, data, PageSizeA4, Margins{Left: 40, Right: 40, Top: 40, Bottom: 40}, ConversionOptions{}) + if err != nil || supported { + t.Fatalf("unsupported explicit pagination was not retained on legacy path: %t, %v", supported, err) + } +} diff --git a/minipdf-go/pdf.go b/minipdf-go/pdf.go index fa906f0c..1fc51638 100644 --- a/minipdf-go/pdf.go +++ b/minipdf-go/pdf.go @@ -235,13 +235,13 @@ func (document *PDFDocument) BytesWithOptions(options PDFSaveOptions) []byte { if font == embedded { continue } - for alias, canonical := range seenFonts { - alias.resource = canonical.resource - alias.objectID = canonical.objectID - } font.resource = fmt.Sprintf("FU%d", index+1) font.objectID = appendEmbeddedFontObjects(&objects, font) } + for alias, canonical := range seenFonts { + alias.resource = canonical.resource + alias.objectID = canonical.objectID + } italicResources := "" if hasItalic { regular := appendPDFObject(&objects, []byte("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Oblique /Encoding /WinAnsiEncoding >>")) From 3cf00185fa74be1ad23bc6d7e5766bd82ef2be53 Mon Sep 17 00:00:00 2001 From: Wei Lin Date: Tue, 17 Nov 2026 00:54:33 +0800 Subject: [PATCH 2/2] fix(go): address DOCX renderer review findings Bound border drawing, preserve font emphasis and alias glyph maps, honor Korean font selection and document grid types, correct cell borders and image targets, and retain legacy behavior for unsupported layouts and missing host fonts. Add focused regression tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- minipdf-go/README.md | 15 +- minipdf-go/docx_font.go | 44 +++++- minipdf-go/docx_render.go | 126 ++++++++++++--- minipdf-go/docx_render_test.go | 273 +++++++++++++++++++++++++++++++++ minipdf-go/font.go | 19 +-- minipdf-go/pdf.go | 40 ++++- 6 files changed, 481 insertions(+), 36 deletions(-) diff --git a/minipdf-go/README.md b/minipdf-go/README.md index 9d69ca3c..8b836be6 100644 --- a/minipdf-go/README.md +++ b/minipdf-go/README.md @@ -141,17 +141,26 @@ offsets and text-box insets. Image decoding is limited to 25 million pixels. Missing or invalid embedded images produce errors rather than disappearing. Documents requiring unsupported section changes, numbering, anchored drawings, +text-bearing VML text boxes, omitted row grid columns (`gridBefore`/`gridAfter`), exact-height rows, multi-page structured layout, or referenced EMF/WMF images retain the existing text converter. If measured paragraph, inline-image, or table flow would cross the first page's content boundary, the entire document uses the text converter; structured pagination, keep rules, and row splitting -are not implemented. Metafile formats are checked before +are not implemented. Tables extending beyond the physical page width also retain +text conversion. Metafile formats are checked before structured rendering; malformed PNG/JPEG images still produce decoding errors. +Linked-only images and inline paragraphs mixing images with text, multiple images, +or explicit line breaks also retain text conversion. Single image-only paragraphs +honor alignment, indentation, and paragraph spacing. Line-grid snapping requires +`docGrid` type `lines` or `linesAndChars`; an omitted/default type does not snap. +Dashed borders use a single native PDF stroke, without retained segment expansion. Images inside table cells, complex drawing geometry, conditional table styles, headers/footers, and complex-script shaping remain unsupported. Structured text uses matching registered fonts or selected licensed Windows -fonts, with Arial/SimSun glyph fallback when available. Missing Unicode glyphs -produce an explicit error on the structured path. Fonts are embedded without +fonts, with Arial/SimSun glyph fallback when available. Requested bold/italic is +synthesized only when the loaded face lacks that emphasis. An unavailable host +font required for Unicode text retains legacy conversion; a present font with +missing glyphs still produces an explicit error on the structured path. Fonts are embedded without subsetting, so large CJK fonts can substantially increase PDF size. XLSX text output skips entirely empty pages within worksheets containing text, diff --git a/minipdf-go/docx_font.go b/minipdf-go/docx_font.go index e13386bc..40ce4420 100644 --- a/minipdf-go/docx_font.go +++ b/minipdf-go/docx_font.go @@ -82,6 +82,14 @@ func loadDOCXFont(name string, bold, italic bool) (*embeddedFont, error) { break } } + if data == nil && (bold || italic) { + for _, registered := range RegisteredFonts() { + if strings.EqualFold(registered.Name, name) { + data = registered.Data + break + } + } + } if data == nil && runtime.GOOS == "windows" { var file string switch strings.ToLower(name) { @@ -105,7 +113,14 @@ func loadDOCXFont(name string, bold, italic bool) (*embeddedFont, error) { } } default: - return readXLSXFont(name, bold, italic) + f, err := readXLSXFont(name, bold, italic) + if err == nil && f == nil && (bold || italic) { + f, err = readXLSXFont(name, false, false) + } + if f != nil { + f.nativeBold, f.nativeItalic = docxFontEmphasis(f.data) + } + return f, err } if root := os.Getenv("WINDIR"); root != "" { var err error @@ -138,6 +153,7 @@ func loadDOCXFont(name string, bold, italic bool) (*embeddedFont, error) { return nil, err } result := &embeddedFont{name: sanitizePDFFontName(variant), data: data, font: parsed, runeToGlyph: make(map[rune]sfnt.GlyphIndex), glyphToRune: make(map[sfnt.GlyphIndex]rune), advances: make([]float64, parsed.NumGlyphs()), ascent: float64(metrics.Ascent) / float64(ppem), descent: float64(metrics.Descent) / float64(ppem)} + result.nativeBold, result.nativeItalic = docxFontEmphasis(data) for i := range result.advances { a, err := parsed.GlyphAdvance(nil, sfnt.GlyphIndex(i), ppem, font.HintingNone) if err != nil { @@ -148,6 +164,32 @@ func loadDOCXFont(name string, bold, italic bool) (*embeddedFont, error) { return result, nil } +func docxFontEmphasis(data []byte) (bold, italic bool) { + if len(data) < 12 { + return + } + n := int(binary.BigEndian.Uint16(data[4:6])) + if n > (len(data)-12)/16 { + return + } + for i := 0; i < n; i++ { + r := data[12+i*16 : 28+i*16] + o, length := int(binary.BigEndian.Uint32(r[8:12])), int(binary.BigEndian.Uint32(r[12:16])) + if o < 0 || length < 0 || o > len(data)-length { + continue + } + if string(r[:4]) == "OS/2" && length >= 64 { + flags := binary.BigEndian.Uint16(data[o+62 : o+64]) + return flags&(1<<5) != 0, flags&1 != 0 + } + if string(r[:4]) == "head" && length >= 46 { + flags := binary.BigEndian.Uint16(data[o+44 : o+46]) + bold, italic = flags&1 != 0, flags&2 != 0 + } + } + return +} + func docxFontCJK(data []byte) bool { if len(data) < 12 { return false diff --git a/minipdf-go/docx_render.go b/minipdf-go/docx_render.go index aae45569..b08241be 100644 --- a/minipdf-go/docx_render.go +++ b/minipdf-go/docx_render.go @@ -3,6 +3,7 @@ package minipdf import ( "bytes" "encoding/xml" + "errors" "fmt" "image" _ "image/jpeg" @@ -15,6 +16,9 @@ import ( "unicode" ) +var errDOCXFontUnavailable = errors.New("DOCX font unavailable") +var errDOCXTableLayoutUnsupported = errors.New("DOCX table layout unsupported") + type docxNode struct { name string attrs map[string]string @@ -355,7 +359,7 @@ func (s *docxStyles) font(style docxRunStyle, r rune) (*embeddedFont, error) { return f, nil } func docxAsian(r rune) bool { - return unicode.Is(unicode.Han, r) || unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r) || r >= 0x3000 && r <= 0x303f || r >= 0xff00 && r <= 0xffef || r == 0x25a1 + return unicode.Is(unicode.Han, r) || unicode.Is(unicode.Hangul, r) || unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r) || r >= 0x3000 && r <= 0x303f || r >= 0xff00 && r <= 0xffef || r == 0x25a1 } type docxGlyph struct { @@ -431,7 +435,7 @@ func (s *docxStyles) layoutParagraph(n *docxNode, width, grid float64) (docxPara g.descent *= 1.27 } } else if r > 127 { - return fmt.Errorf("DOCX character U+%04X requires an installed or registered font", r) + return fmt.Errorf("DOCX character U+%04X requires an installed or registered font: %w", r, errDOCXFontUnavailable) } glyphs = append(glyphs, g) } @@ -564,7 +568,7 @@ func drawDOCXParagraph(page *PDFPage, p docxParagraph, x, top, width float64) { if g.style.highlight != nil { page.AddRect(left, page.Height-top-line.height, g.width, line.height, *g.style.highlight) } - page.operations = append(page.operations, textOperation{text: string(g.r), x: left, y: baseline, fontSize: g.style.size, horizontalScale: 100, color: g.style.color, bold: g.style.bold, italic: g.style.italic, embedded: g.font}) + page.operations = append(page.operations, textOperation{text: string(g.r), x: left, y: baseline, fontSize: g.style.size, horizontalScale: 100, color: g.style.color, bold: g.style.bold, italic: g.style.italic, embedded: g.font, synthesize: true}) left += g.width } top += line.height @@ -661,6 +665,12 @@ func (s *docxStyles) table(n *docxNode, available float64) (docxTable, error) { t.columns[i] *= t.width / gridWidth } active := map[int]*docxCell{} + rowCount := 0 + for _, row := range n.children { + if row.name == "tr" { + rowCount++ + } + } for _, row := range n.children { if row.name != "tr" { continue @@ -679,7 +689,7 @@ func (s *docxStyles) table(n *docxNode, available float64) (docxTable, error) { span = 1 } if column+span > len(t.columns) { - return t, fmt.Errorf("DOCX cell span exceeds table grid") + return t, fmt.Errorf("DOCX cell span exceeds table grid: %w", errDOCXTableLayoutUnsupported) } cell := &docxCell{column: column, span: span, row: ri, rowSpan: 1, margins: docxCellMargins(cp.child("tcMar"), tm), align: cp.value("vAlign"), borders: map[string]docxBorder{}} for i := column; i < column+span; i++ { @@ -690,6 +700,9 @@ func (s *docxStyles) table(n *docxNode, available float64) (docxTable, error) { if edge == "top" && ri > 0 { key = "insideH" } + if edge == "bottom" && ri < rowCount-1 { + key = "insideH" + } if edge == "left" && column > 0 { key = "insideV" } @@ -711,7 +724,7 @@ func (s *docxStyles) table(n *docxNode, available float64) (docxTable, error) { cell.continuation = true start.borders["bottom"] = cell.borders["bottom"] } else { - return t, fmt.Errorf("DOCX vertical merge has no matching start") + return t, fmt.Errorf("DOCX vertical merge has no matching start: %w", errDOCXTableLayoutUnsupported) } } else { delete(active, column) @@ -732,7 +745,7 @@ func (s *docxStyles) table(n *docxNode, available float64) (docxTable, error) { column += span } if column != len(t.columns) { - return t, fmt.Errorf("DOCX row does not cover table grid") + return t, fmt.Errorf("DOCX row does not cover table grid: %w", errDOCXTableLayoutUnsupported) } } for _, c := range t.cells { @@ -783,18 +796,11 @@ func drawDOCXBorder(page *PDFPage, b docxBorder, x1, y1, x2, y2 float64) { return } if b.style == "dotted" || b.style == "dotDash" || b.style == "dashed" { - length := math.Hypot(x2-x1, y2-y1) - if length == 0 { - return - } dash := w * 2 if b.style == "dashed" { dash = w * 6 } - for at := 0.0; at < length; at += dash + w*2 { - end := math.Min(length, at+dash) - page.AddLine(x1+(x2-x1)*at/length, y1+(y2-y1)*at/length, x1+(x2-x1)*end/length, y1+(y2-y1)*end/length, b.color, w) - } + page.operations = append(page.operations, lineOperation{x1: x1, y1: y1, x2: x2, y2: y2, color: b.color, width: w, dash: []float64{dash, w * 2}}) return } page.AddLine(x1, y1, x2, y2, b.color, w) @@ -870,7 +876,7 @@ func readDOCXImages(files officePackage, n *docxNode) ([]docxImage, error) { if !ok { return fmt.Errorf("DOCX drawing image relationship is missing") } - name := path.Join("word", target) + name := xlsxResolveRelationshipTarget("word/document.xml", target) data, err := files.read(name) if err != nil { return err @@ -968,17 +974,29 @@ func docxUnsupportedMetafiles(files officePackage, body *docxNode) (bool, error) } unsupported := false for _, blip := range blips { + if blip.attr("embed") == "" && blip.attr("link") != "" { + unsupported = true + continue + } target, ok := targets[blip.attr("embed")] if !ok { return false, fmt.Errorf("DOCX drawing image relationship is missing") } - name := path.Join("word", target) + name := xlsxResolveRelationshipTarget("word/document.xml", target) if _, ok := files[name]; !ok { return false, fmt.Errorf("Office package part %q is missing", name) } switch strings.ToLower(path.Ext(target)) { case ".emf", ".wmf": unsupported = true + case ".png", ".jpg", ".jpeg": + data, err := files.read(name) + if err != nil { + return false, err + } + if _, _, err := image.DecodeConfig(bytes.NewReader(data)); err != nil { + return false, fmt.Errorf("read DOCX image %s: %w", name, err) + } } } return unsupported, nil @@ -1021,6 +1039,16 @@ func renderDOCXTables(files officePackage, data []byte, size PageSize, margins M if len(body.descendants("numPr")) > 0 || len(body.descendants("anchor")) > 0 || len(body.descendants("pageBreakBefore")) > 0 { return nil, false, nil } + if len(body.descendants("gridBefore")) > 0 || len(body.descendants("gridAfter")) > 0 { + return nil, false, nil + } + for _, box := range body.descendants("txbxContent") { + for _, text := range box.descendants("t") { + if text.text != "" { + return nil, false, nil + } + } + } for _, br := range body.descendants("br") { if br.attr("type") == "page" { return nil, false, nil @@ -1070,11 +1098,17 @@ func renderDOCXTables(files officePackage, data []byte, size PageSize, margins M if unsupportedImages { return nil, false, nil } + if docxUnsupportedInlineFlow(body) { + return nil, false, nil + } s, err := readDOCXStyles(files) if err != nil { return nil, true, err } - grid := docxNumber(body.child("sectPr").child("docGrid").attr("linePitch")) / 20 + grid := 0.0 + if g := body.child("sectPr").child("docGrid"); g.attr("type") == "lines" || g.attr("type") == "linesAndChars" { + grid = docxNumber(g.attr("linePitch")) / 20 + } doc := NewPDFDocument() page := doc.AddPage(size.Width, size.Height) top := margins.Top @@ -1085,6 +1119,9 @@ func renderDOCXTables(files officePackage, data []byte, size PageSize, margins M case "p": p, err := s.layoutParagraph(n, available, grid) if err != nil { + if errors.Is(err, errDOCXFontUnavailable) { + return nil, false, nil + } return nil, true, err } p.style.before = math.Max(0, p.style.before-previousAfter) @@ -1102,9 +1139,18 @@ func renderDOCXTables(files officePackage, data []byte, size PageSize, margins M op.x = margins.Left + picture.left op.y = size.Height - top - picture.top - op.height if !picture.floating { - op.x = margins.Left - op.y = size.Height - top - op.height - p.height = math.Max(p.height, op.height) + contentWidth := available - p.style.left - p.style.right - p.style.first + if op.width > contentWidth { + return nil, false, nil + } + op.x = margins.Left + p.style.left + p.style.first + if p.style.align == "center" { + op.x += (contentWidth - op.width) / 2 + } else if p.style.align == "right" { + op.x += contentWidth - op.width + } + op.y = size.Height - top - p.style.before - op.height + p.height = p.style.before + math.Max(p.height-p.style.before-p.style.after, op.height) + p.style.after } page.operations = append(page.operations, op) } @@ -1116,6 +1162,9 @@ func renderDOCXTables(files officePackage, data []byte, size PageSize, margins M case "tbl": t, err := s.table(n, available) if err != nil { + if errors.Is(err, errDOCXFontUnavailable) || errors.Is(err, errDOCXTableLayoutUnsupported) { + return nil, false, nil + } return nil, true, err } height := 0.0 @@ -1132,6 +1181,9 @@ func renderDOCXTables(files officePackage, data []byte, size PageSize, margins M if t.align == "right" { left = size.Width - margins.Right - t.width } + if left < 0 || left+t.width > size.Width { + return nil, false, nil + } drawDOCXTable(page, t, left, top) top += height previousAfter = 0 @@ -1139,3 +1191,37 @@ func renderDOCXTables(files officePackage, data []byte, size PageSize, margins M } return doc.BytesWithOptions(PDFSaveOptions{Compress: options.Compress}), true, nil } + +func docxUnsupportedInlineFlow(body *docxNode) bool { + for _, p := range body.descendants("p") { + inlineCount := 0 + var countInline func(*docxNode) + countInline = func(n *docxNode) { + // VML pictures have floating placement, not paragraph inline flow. + if n.name == "pict" { + return + } + if n.name == "drawing" && n.child("inline") != nil { + inlineCount++ + } + for _, c := range n.children { + countInline(c) + } + } + countInline(p) + if inlineCount > 1 { + return true + } + if inlineCount == 1 { + if len(p.descendants("br")) > 0 || len(p.descendants("cr")) > 0 || len(p.descendants("tab")) > 0 { + return true + } + for _, text := range p.descendants("t") { + if text.text != "" { + return true + } + } + } + } + return false +} diff --git a/minipdf-go/docx_render_test.go b/minipdf-go/docx_render_test.go index ebdf0342..f2c7f5d1 100644 --- a/minipdf-go/docx_render_test.go +++ b/minipdf-go/docx_render_test.go @@ -3,12 +3,15 @@ package minipdf import ( "bytes" "encoding/binary" + "errors" + "fmt" "image" "image/png" "math" "strings" "testing" + "golang.org/x/image/font/gofont/gobolditalic" "golang.org/x/image/font/gofont/goregular" "golang.org/x/image/font/sfnt" ) @@ -167,6 +170,10 @@ func TestDOCXEquivalentFontInstancesUseCanonicalPDFResource(t *testing.T) { if a.resource == "" || b.resource != a.resource || bytes.Count(pdf, []byte("/Subtype /Type0")) != 1 || bytes.Contains(pdf, []byte("BT / ")) { t.Fatal("equivalent font instances have unresolved PDF resources") } + encoded, ok := b.encode("Ω") + if !ok || !bytes.Contains(pdf, []byte("BT /"+a.resource+" 12 Tf 0 0 0 rg 30 100 Td <"+encoded+"> Tj")) || bytes.Contains(pdf, []byte("30 100 Td (?)")) { + t.Fatal("alias Unicode text did not use the canonical embedded glyph resource") + } } func TestDOCXMissingImageRelationshipReturnsError(t *testing.T) { @@ -365,3 +372,269 @@ func TestDOCXUnsupportedLayoutKeepsTextConverter(t *testing.T) { t.Fatalf("unsupported explicit pagination was not retained on legacy path: %t, %v", supported, err) } } + +func TestDOCXDashRenderingHasBoundedOperations(t *testing.T) { + for _, style := range []string{"dotted", "dotDash", "dashed"} { + page := NewPDFDocument().AddPage(612, 792) + drawDOCXBorder(page, docxBorder{style: style, width: .25}, 10, 10, float64(2147483647)/20, 10) + if len(page.operations) != 1 { + t.Fatalf("%s retained %d operations for off-page geometry", style, len(page.operations)) + } + var stream bytes.Buffer + page.operations[0].appendPDF(&stream, nil) + if !strings.Contains(stream.String(), "] 0 d ") || bytes.Count(stream.Bytes(), []byte(" l S")) != 1 || !strings.HasSuffix(stream.String(), "Q\n") { + t.Fatalf("dash stroke is not a single isolated native PDF operation: %s", &stream) + } + } + s := docxTestStyles(t, docxReviewStyles) + data := []byte(`

`) + files, err := openOfficePackage(officePackageBytes(t, map[string]string{"word/styles.xml": docxReviewStyles})) + if err != nil { + t.Fatal(err) + } + root, err := parseDOCXNode(data) + if err != nil { + t.Fatal(err) + } + table, err := s.table(root.child("body").child("tbl"), 500) + if err != nil { + t.Fatal(err) + } + page := NewPDFDocument().AddPage(612, 792) + drawDOCXTable(page, table, 40, 40) + if len(page.operations) != 1 { + t.Fatalf("full extreme-grid table retained %d operations", len(page.operations)) + } + _, supported, err := renderDOCXTables(files, data, PageSizeA4, Margins{Left: 40, Right: 40, Top: 40, Bottom: 40}, ConversionOptions{}) + if supported || err != nil { + t.Fatalf("table outside physical page did not decline native layout: %t %v", supported, err) + } +} + +func TestDOCXSynthesizesOnlyMissingFontEmphasis(t *testing.T) { + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("Portable Test", goregular.TTF) + for _, native := range []bool{false, true} { + if native { + RegisterFont("Portable Test Bold Italic", gobolditalic.TTF) + } + f, err := loadDOCXFont("Portable Test", true, true) + if err != nil || f == nil { + t.Fatalf("styled registered font unavailable: %v", err) + } + if f.nativeBold != native || f.nativeItalic != native { + t.Fatalf("native emphasis incorrectly identified: %+v", f) + } + f.collect("Ω") + f.resource = "FU1" + var stream bytes.Buffer + op := textOperation{text: "Ω", x: 10, y: 20, fontSize: 12, bold: true, italic: true, color: PDFColor{Red: 1}, embedded: f, synthesize: true} + op.appendPDF(&stream, nil) + if !strings.Contains(stream.String(), "/FU1 12 Tf") || strings.Contains(stream.String(), "(?)") { + t.Fatal("emphasis lost Unicode embedding") + } + if native { + if strings.Contains(stream.String(), "2 Tr") || strings.Contains(stream.String(), " Tm") { + t.Fatal("native bold italic face was double-synthesized") + } + } else if !strings.Contains(stream.String(), "2 Tr") || !strings.Contains(stream.String(), "0.21256 1 10 20 Tm") || !strings.Contains(stream.String(), "1 0 0 RG") || !strings.HasSuffix(stream.String(), "Q\n") { + t.Fatalf("missing face was not synthesized with isolated stroke/slant: %s", &stream) + } + } +} + +func TestDOCXHangulUsesEastAsianAndKoreanThemeFonts(t *testing.T) { + data := bytes.Clone(goregular.TTF) + original, err := sfnt.Parse(data) + if err != nil { + t.Fatal(err) + } + glyph, err := original.GlyphIndex(nil, 'Ω') + if err != nil { + t.Fatal(err) + } + // Supply a synthetic cmap for the portable font; no host Korean font is required. + cmap := make([]byte, 40) + binary.BigEndian.PutUint16(cmap[2:4], 1) + binary.BigEndian.PutUint16(cmap[4:6], 3) + binary.BigEndian.PutUint16(cmap[6:8], 10) + binary.BigEndian.PutUint32(cmap[8:12], 12) + binary.BigEndian.PutUint16(cmap[12:14], 12) + binary.BigEndian.PutUint32(cmap[16:20], 28) + binary.BigEndian.PutUint32(cmap[24:28], 1) + binary.BigEndian.PutUint32(cmap[28:32], 0xac00) + binary.BigEndian.PutUint32(cmap[32:36], 0xac00) + binary.BigEndian.PutUint32(cmap[36:40], uint32(glyph)) + for i, n := 0, int(binary.BigEndian.Uint16(data[4:6])); i < n; i++ { + r := data[12+i*16 : 28+i*16] + if string(r[:4]) == "cmap" { + binary.BigEndian.PutUint32(r[8:12], uint32(len(data))) + binary.BigEndian.PutUint32(r[12:16], uint32(len(cmap))) + } + } + data = append(data, cmap...) + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("Korean Test", data) + s := &docxStyles{fonts: map[string]*embeddedFont{}, theme: map[string]string{"minor:Hang": "Korean Test"}} + for _, fonts := range []map[string]string{{"eastAsia": "Korean Test", "hAnsi": "Wrong font"}, {"eastAsiaTheme": "minorEastAsia", "hAnsi": "Wrong font"}} { + f, err := s.font(docxRunStyle{fonts: fonts, lang: "ko-KR"}, '가') + if err != nil || f == nil || !bytes.Equal(f.data, data) || !f.canEncode("가") { + t.Fatalf("Hangul did not select the East Asian Korean font: %v", err) + } + } +} + +func TestDOCXInteriorBottomBordersAndMergeFinalEdge(t *testing.T) { + s := docxTestStyles(t, ``) + for _, inside := range []string{"none", "dashed"} { + xml := `

` + n, err := parseDOCXNode([]byte(xml)) + if err != nil { + t.Fatal(err) + } + table, err := s.table(n, 300) + if err != nil { + t.Fatal(err) + } + if table.cells[1].borders["bottom"].style != inside || table.cells[0].borders["bottom"].style != "single" || table.cells[3].borders["bottom"].width != 2 || table.cells[3].borders["top"].style != "dotted" { + t.Fatalf("interior/outer/override/merge edges lost for %s", inside) + } + } +} + +const docxReviewTable = `

Cell

` +const docxReviewStyles = `` + +func docxReviewPackage(t *testing.T, body string, parts map[string]string) (officePackage, []byte, []byte) { + t.Helper() + if parts == nil { + parts = map[string]string{} + } + data := []byte(`` + body + ``) + parts["word/document.xml"] = string(data) + parts["word/styles.xml"] = docxReviewStyles + input := officePackageBytes(t, parts) + files, err := openOfficePackage(input) + if err != nil { + t.Fatal(err) + } + return files, data, input +} + +func TestDOCXUnsupportedReviewLayoutsRetainLegacy(t *testing.T) { + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("Portable Test", goregular.TTF) + var encoded bytes.Buffer + if err := png.Encode(&encoded, image.NewRGBA(image.Rect(0, 0, 2, 3))); err != nil { + t.Fatal(err) + } + picture := `` + for _, test := range []struct{ name, body string }{ + {"gridBefore", strings.Replace(docxReviewTable, "", ``, 1)}, + {"gridAfter", strings.Replace(docxReviewTable, "", ``, 1)}, + {"span", strings.Replace(docxReviewTable, "", ``, 1)}, + {"merge", strings.Replace(docxReviewTable, "", ``, 1)}, + {"text box", `

Box text

` + docxReviewTable}, + {"mixed inline text", `

Before` + picture + `

` + docxReviewTable}, + {"multiple inline images", `

` + picture + picture + `

` + docxReviewTable}, + {"inline break", `


` + picture + `

` + docxReviewTable}, + {"linked image", `

` + strings.Replace(picture, `embed="picture"`, `link="external"`, 1) + `

` + docxReviewTable}, + } { + t.Run(test.name, func(t *testing.T) { + files, data, input := docxReviewPackage(t, test.body, map[string]string{"word/_rels/document.xml.rels": ``, "word/media/image.png": encoded.String()}) + _, supported, err := renderDOCXTables(files, data, PageSizeA4, Margins{Left: 40, Top: 40, Right: 40, Bottom: 40}, ConversionOptions{}) + if supported || err != nil { + t.Fatalf("layout did not fall back: supported=%t err=%v", supported, err) + } + pdf, err := ConvertBytesToPDF(input) + if err != nil { + t.Fatal(err) + } + pages, size, margins, err := extractDOCX(data) + if err != nil { + t.Fatal(err) + } + var legacy []textPage + var text []string + for _, lines := range pages { + legacy = append(legacy, textPage{lines: lines, size: size, margins: margins}) + text = append(text, lines...) + } + if !bytes.Equal(pdf, renderTextPages(legacy, ConversionOptions{})) || !strings.Contains(strings.Join(text, "\n"), "Cell") { + t.Fatal("fallback changed legacy text conversion") + } + if test.name == "text box" && !strings.Contains(strings.Join(text, "\n"), "Box text") { + t.Fatal("fallback lost text-box text") + } + }) + } +} + +func TestDOCXImageOnlyAlignmentAndAbsoluteTargets(t *testing.T) { + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("Portable Test", goregular.TTF) + var encoded bytes.Buffer + if err := png.Encode(&encoded, image.NewRGBA(image.Rect(0, 0, 2, 3))); err != nil { + t.Fatal(err) + } + for _, test := range []struct { + align string + x float64 + }{{"left", 40}, {"center", 90}, {"right", 140}} { + body := `

` + docxReviewTable + files, data, _ := docxReviewPackage(t, body, map[string]string{"word/_rels/document.xml.rels": ``, "word/media/image.png": encoded.String()}) + pdf, supported, err := renderDOCXTables(files, data, PageSize{Width: 200, Height: 300}, Margins{Left: 40, Top: 40, Right: 40, Bottom: 40}, ConversionOptions{}) + if err != nil || !supported || !bytes.Contains(pdf, []byte(fmt.Sprintf("20 0 0 30 %s 225 cm", pdfNumber(test.x)))) { + t.Fatalf("%s image lost absolute target/spacing/alignment: supported=%t err=%v", test.align, supported, err) + } + } +} + +func TestDOCXGridTypeControlsLineSnapping(t *testing.T) { + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + RegisterFont("Portable Test", goregular.TTF) + var plain []byte + for _, typ := range []string{"", "default", "snapToChars", "lines", "linesAndChars"} { + files, data, _ := docxReviewPackage(t, `

Text

`+docxReviewTable+``, nil) + pdf, supported, err := renderDOCXTables(files, data, PageSizeA4, Margins{Left: 40, Top: 40, Right: 40, Bottom: 40}, ConversionOptions{}) + if err != nil || !supported { + t.Fatalf("grid %s failed: %v", typ, err) + } + if typ == "" { + plain = pdf + } + if (typ == "default" || typ == "snapToChars") && !bytes.Equal(pdf, plain) { + t.Fatalf("inactive grid %s altered paragraph layout", typ) + } + if (typ == "lines" || typ == "linesAndChars") && bytes.Equal(pdf, plain) { + t.Fatalf("active grid %s did not snap lines", typ) + } + } +} + +func TestDOCXUnavailableFontsFallbackButMissingGlyphsFail(t *testing.T) { + ClearRegisteredFonts() + t.Cleanup(ClearRegisteredFonts) + t.Setenv("WINDIR", "") + for _, body := range []string{`

é

` + docxReviewTable, strings.Replace(docxReviewTable, "Cell", "é", 1)} { + files, data, input := docxReviewPackage(t, body, nil) + _, supported, err := renderDOCXTables(files, data, PageSizeA4, Margins{Left: 40, Top: 40, Right: 40, Bottom: 40}, ConversionOptions{}) + if err != nil || supported { + t.Fatalf("missing environment font did not fall back: %t %v", supported, err) + } + if _, err := ConvertBytesToPDF(input); err != nil { + t.Fatalf("legacy font fallback failed: %v", err) + } + } + RegisterFont("Portable Test", goregular.TTF) + files, data, _ := docxReviewPackage(t, strings.Replace(docxReviewTable, "Cell", "\U0001f600", 1), nil) + _, supported, err := renderDOCXTables(files, data, PageSizeA4, Margins{Left: 40, Top: 40, Right: 40, Bottom: 40}, ConversionOptions{}) + if !supported || err == nil || errors.Is(err, errDOCXFontUnavailable) || !strings.Contains(err.Error(), "has no glyph") { + t.Fatalf("present font's missing glyph was silently downgraded: %t %v", supported, err) + } +} diff --git a/minipdf-go/font.go b/minipdf-go/font.go index fae73b63..31944ffd 100644 --- a/minipdf-go/font.go +++ b/minipdf-go/font.go @@ -13,15 +13,16 @@ import ( ) type embeddedFont struct { - name string - data []byte - font *sfnt.Font - runeToGlyph map[rune]sfnt.GlyphIndex - glyphToRune map[sfnt.GlyphIndex]rune - objectID int - resource string - advances []float64 - ascent, descent float64 + name string + data []byte + font *sfnt.Font + runeToGlyph map[rune]sfnt.GlyphIndex + glyphToRune map[sfnt.GlyphIndex]rune + objectID int + resource string + advances []float64 + ascent, descent float64 + nativeBold, nativeItalic bool } func prepareEmbeddedFont(pages []*PDFPage) *embeddedFont { diff --git a/minipdf-go/pdf.go b/minipdf-go/pdf.go index 1fc51638..8c987282 100644 --- a/minipdf-go/pdf.go +++ b/minipdf-go/pdf.go @@ -84,6 +84,7 @@ type textOperation struct { bold bool italic bool embedded *embeddedFont + synthesize bool } func (operation textOperation) appendPDF(buffer *bytes.Buffer, embedded *embeddedFont) { @@ -96,17 +97,37 @@ func (operation textOperation) appendPDF(buffer *bytes.Buffer, embedded *embedde } if embedded != nil { if encoded, ok := embedded.encode(operation.text); ok { - fmt.Fprintf(buffer, "BT /%s %s Tf %s%s %s %s rg %s %s Td <%s> Tj ET\n", + syntheticBold := operation.synthesize && operation.bold && !embedded.nativeBold + syntheticItalic := operation.synthesize && operation.italic && !embedded.nativeItalic + synthetic := syntheticBold || syntheticItalic + position := pdfNumber(operation.x) + " " + pdfNumber(operation.y) + " Td" + if synthetic { + buffer.WriteString("q ") + if syntheticBold { + fmt.Fprintf(buffer, "%s w %s %s %s RG ", pdfNumber(operation.fontSize/30), pdfNumber(operation.color.Red), pdfNumber(operation.color.Green), pdfNumber(operation.color.Blue)) + } + if syntheticItalic { + position = "1 0 0.21256 1 " + pdfNumber(operation.x) + " " + pdfNumber(operation.y) + " Tm" + } + } + mode := "" + if syntheticBold { + mode = "2 Tr " + } + fmt.Fprintf(buffer, "BT /%s %s Tf %s%s%s %s %s rg %s <%s> Tj ET\n", embedded.resource, pdfNumber(operation.fontSize), horizontalScale, + mode, pdfNumber(operation.color.Red), pdfNumber(operation.color.Green), pdfNumber(operation.color.Blue), - pdfNumber(operation.x), - pdfNumber(operation.y), + position, encoded, ) + if synthetic { + buffer.WriteString("Q\n") + } return } } @@ -154,9 +175,17 @@ type lineOperation struct { x1, y1, x2, y2 float64 color PDFColor width float64 + dash []float64 } func (operation lineOperation) appendPDF(buffer *bytes.Buffer, _ *embeddedFont) { + if len(operation.dash) > 0 { + buffer.WriteString("q [") + for _, length := range operation.dash { + buffer.WriteString(pdfNumber(length) + " ") + } + buffer.WriteString("] 0 d ") + } fmt.Fprintf(buffer, "%s %s %s RG %s w %s %s m %s %s l S\n", pdfNumber(operation.color.Red), pdfNumber(operation.color.Green), @@ -167,6 +196,9 @@ func (operation lineOperation) appendPDF(buffer *bytes.Buffer, _ *embeddedFont) pdfNumber(operation.x2), pdfNumber(operation.y2), ) + if len(operation.dash) > 0 { + buffer.WriteString("Q\n") + } } func (document *PDFDocument) Bytes() []byte { @@ -241,6 +273,8 @@ func (document *PDFDocument) BytesWithOptions(options PDFSaveOptions) []byte { for alias, canonical := range seenFonts { alias.resource = canonical.resource alias.objectID = canonical.objectID + alias.runeToGlyph = canonical.runeToGlyph + alias.glyphToRune = canonical.glyphToRune } italicResources := "" if hasItalic {