Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 83 additions & 17 deletions minipdf-go/office.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,94 @@ func renderTextPages(pages []textPage, options ConversionOptions) []byte {
if options.Margins != nil {
margins = *options.Margins
}
addTextPages(document, sourcePage.lines, pageSize, margins, sourcePage.noWrap, sourcePage.skipEmpty)
addTextPages(document, sourcePage.lines, pageSize, margins, sourcePage.noWrap, sourcePage.skipEmpty, 0)
}
return document.BytesWithOptions(PDFSaveOptions{Compress: options.Compress})
}

func addTextPages(document *PDFDocument, lines []string, pageSize PageSize, margins Margins, noWrap, skipEmpty bool) {
const (
fontSize = 11.0
leading = 15.0
)
// minimumTextFitZoom is the lowest zoom percentage LibreOffice Calc applies
// when it fits a sheet to a page count (ZOOM_MIN in ScPrintFunc::CalcZoom).
const minimumTextFitZoom = 10

// Font size and line spacing of unscaled plain text pages, in points.
const (
baseFontSize = 11.0
baseLeading = 15.0
)

// addTextPages lays lines out as pages of text. A positive fitHeight shrinks
// the text, down to minimumTextFitZoom, until it fits that many pages.
func addTextPages(document *PDFDocument, lines []string, pageSize PageSize, margins Margins, noWrap, skipEmpty bool, fitHeight int) {
if margins == (Margins{}) {
margins = Margins{Left: 54, Top: 54, Right: 54, Bottom: 54}
}
wrapped := make([]string, 0, len(lines))
for _, line := range lines {
if noWrap {
wrapped = append(wrapped, line)
} else {
wrapped = append(wrapped, wrapText(line, textCharactersPerLine(pageSize, margins))...)
wrapAt := func(zoom int) []string {
wrapped := make([]string, 0, len(lines))
for _, line := range lines {
if noWrap {
wrapped = append(wrapped, line)
} else {
wrapped = append(wrapped, wrapText(line, textCharactersPerLine(pageSize, margins, baseFontSize*float64(zoom)/100))...)
}
}
if len(wrapped) == 0 {
wrapped = append(wrapped, "")
}
return wrapped
}
if len(wrapped) == 0 {
wrapped = append(wrapped, "")
zoom := 100
if fitHeight > 0 {
// Like ScPrintFunc::CalcZoom, keep 100% when the content already fits
// and otherwise search for the largest zoom that does.
// Count pages with the same arithmetic as the layout loop below, so
// the check and the layout cannot disagree about a page break.
fits := func(zoom int) bool {
leading := baseLeading * float64(zoom) / 100
pages := 0
pageHasText := false
if !skipEmpty {
pages = 1
pageHasText = true
}
y := pageSize.Height - margins.Top
for _, line := range wrapAt(zoom) {
if y < margins.Bottom {
pageHasText = false
y = pageSize.Height - margins.Top
}
if (!skipEmpty || strings.TrimSpace(line) != "") && !pageHasText {
pages++
pageHasText = true
if pages > fitHeight {
return false
}
}
y -= leading
}
return pages <= fitHeight
}
if !fits(zoom) {
if skipEmpty {
// Visible page counts are not monotonic: moving a page break
// can merge or separate sparse text around skipped empty pages.
// Check percentages in descending order to find the largest fit.
for zoom--; zoom > minimumTextFitZoom && !fits(zoom); zoom-- {
}
} else {
zoom = minimumTextFitZoom
for tooLarge := 100; tooLarge-zoom > 1; {
if middle := (zoom + tooLarge) / 2; fits(middle) {
zoom = middle
} else {
tooLarge = middle
}
}
}
}
}
wrapped := wrapAt(zoom)
fontSize := baseFontSize * float64(zoom) / 100
leading := baseLeading * float64(zoom) / 100

var page *PDFPage
if !skipEmpty {
Expand All @@ -183,8 +247,7 @@ func addTextPages(document *PDFDocument, lines []string, pageSize PageSize, marg
}
}

func textCharactersPerLine(pageSize PageSize, margins Margins) int {
const fontSize = 11.0
func textCharactersPerLine(pageSize PageSize, margins Margins, fontSize float64) int {
if margins == (Margins{}) {
margins = Margins{Left: 54, Top: 54, Right: 54, Bottom: 54}
}
Expand All @@ -211,7 +274,10 @@ func wrapText(text string, limit int) []string {
}
}
lines = append(lines, strings.TrimSpace(string(runes[:breakAt])))
runes = []rune(strings.TrimLeftFunc(string(runes[breakAt:]), unicode.IsSpace))
runes = runes[breakAt:]
for len(runes) > 0 && unicode.IsSpace(runes[0]) {
runes = runes[1:]
}
}
lines = append(lines, string(runes))
}
Expand Down
Loading
Loading