Skip to content

Commit 551f87e

Browse files
committed
Fix #767
1 parent 1f3886c commit 551f87e

File tree

7 files changed

+51
-135
lines changed

7 files changed

+51
-135
lines changed

pkg/font/metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path"
2525
"path/filepath"
26+
"runtime/debug"
2627
"strconv"
2728
"strings"
2829
"sync"
@@ -277,6 +278,7 @@ func CharWidth(fontName string, r rune) int {
277278
ttf, ok := UserFontMetrics[fontName]
278279
if !ok {
279280
fmt.Fprintf(os.Stderr, "pdfcpu: user font not loaded: %s\n", fontName)
281+
debug.PrintStack()
280282
os.Exit(1)
281283
}
282284

pkg/pdfcpu/font/fontDict.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,6 @@ func coreFontDict(xRefTable *model.XRefTable, coreFontName string) (*types.Indir
232232
if coreFontName != "Symbol" && coreFontName != "ZapfDingbats" {
233233
d.InsertName("Encoding", "WinAnsiEncoding")
234234
}
235-
// if coreFontName == "Helvetica" {
236-
// indRef, err := PDFDocEncoding(xRefTable)
237-
// if err != nil {
238-
// return nil, err
239-
// }
240-
// d.Insert("Encoding", *indRef)
241-
// } else if coreFontName != "Symbol" && coreFontName != "ZapfDingbats" {
242-
// d.InsertName("Encoding", "WinAnsiEncoding")
243-
// }
244235
return xRefTable.IndRefForNewObject(d)
245236
}
246237

pkg/pdfcpu/form/export.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@ func extractComboBox(xRefTable *model.XRefTable, page int, d types.Dict, id, nam
289289
if err != nil {
290290
return nil, err
291291
}
292-
cb.Default = s
292+
cb.Default = strings.TrimSpace(s)
293293
}
294294

295295
if sl := d.StringLiteralEntry("V"); sl != nil {
296296
s, err := types.StringLiteralToString(*sl)
297297
if err != nil {
298298
return nil, err
299299
}
300-
cb.Value = s
300+
cb.Value = strings.TrimSpace(s)
301301
}
302302

303303
opts, err := parseOptions(xRefTable, d)
@@ -438,14 +438,14 @@ func extractListBox(xRefTable *model.XRefTable, page int, d types.Dict, id, name
438438
if err != nil {
439439
return nil, err
440440
}
441-
lb.Defaults = []string{s}
441+
lb.Defaults = []string{strings.TrimSpace(s)}
442442
}
443443
if sl := d.StringLiteralEntry("V"); sl != nil {
444444
s, err := types.StringLiteralToString(*sl)
445445
if err != nil {
446446
return nil, err
447447
}
448-
lb.Values = []string{s}
448+
lb.Values = []string{strings.TrimSpace(s)}
449449
}
450450
} else {
451451
ss, err := parseStringLiteralArray(xRefTable, d, "DV")

pkg/pdfcpu/form/fill.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strconv"
2323
"strings"
2424

25+
"github.com/pdfcpu/pdfcpu/pkg/font"
2526
pdffont "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/font"
2627
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
2728
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives"
@@ -1099,6 +1100,34 @@ func fillWidgetAnnots(
10991100
return nil
11001101
}
11011102

1103+
func setupFillFonts(xRefTable *model.XRefTable) error {
1104+
d, err := primitives.FormFontResDict(xRefTable)
1105+
if err != nil {
1106+
return err
1107+
}
1108+
1109+
m := xRefTable.FillFonts
1110+
1111+
if d == nil {
1112+
// setup/reuse Helvetica and add to m
1113+
return nil
1114+
}
1115+
1116+
for k, v := range d {
1117+
indRef := v.(types.IndirectRef)
1118+
fontName, _, err := primitives.FormFontNameAndLangForID(xRefTable, indRef)
1119+
if err != nil {
1120+
return err
1121+
}
1122+
1123+
if font.IsCoreFont(fontName) || font.IsUserFont(fontName) {
1124+
m[k] = indRef
1125+
}
1126+
}
1127+
1128+
return nil
1129+
}
1130+
11021131
// FillForm populates form fields as provided by fillDetails and also supports virtual image fields.
11031132
func FillForm(
11041133
ctx *model.Context,
@@ -1116,6 +1145,10 @@ func FillForm(
11161145
fonts := map[string]types.IndirectRef{}
11171146
indRefs := map[types.IndirectRef]bool{}
11181147

1148+
if err := setupFillFonts(xRefTable); err != nil {
1149+
return false, nil, err
1150+
}
1151+
11191152
var ok bool
11201153

11211154
for i := 1; i <= xRefTable.PageCount; i++ {

pkg/pdfcpu/model/xreftable.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ type XRefTable struct {
169169
AppendOnly bool
170170

171171
// Fonts
172-
UsedGIDs map[string]map[uint16]bool
172+
UsedGIDs map[string]map[uint16]bool
173+
FillFonts map[string]types.IndirectRef
173174
}
174175

175176
// NewXRefTable creates a new XRefTable.
@@ -187,6 +188,7 @@ func newXRefTable(conf *Configuration) (xRefTable *XRefTable) {
187188
ValidateLinks: conf.ValidateLinks,
188189
URIs: map[int]map[string]string{},
189190
UsedGIDs: map[string]map[uint16]bool{},
191+
FillFonts: map[string]types.IndirectRef{},
190192
Conf: conf,
191193
}
192194
}

pkg/pdfcpu/primitives/font.go

Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,20 @@ func FormFontResDict(xRefTable *model.XRefTable) (types.Dict, error) {
187187
return xRefTable.DereferenceDict(o)
188188
}
189189

190-
func formFontIndRef(xRefTable *model.XRefTable, fontID string) (*types.IndirectRef, error) {
191-
d, err := FormFontResDict(xRefTable)
192-
if err != nil {
193-
return nil, err
190+
func formFontIndRef(xRefTable *model.XRefTable, fontID string) *types.IndirectRef {
191+
192+
indRef, ok := xRefTable.FillFonts[fontID]
193+
if ok {
194+
return &indRef
194195
}
195196

196-
for k, v := range d {
197-
//fmt.Printf("%s %s\n", k, v)
197+
for k, v := range xRefTable.FillFonts {
198198
if strings.HasPrefix(k, fontID) || strings.HasPrefix(fontID, k) {
199-
indRef, _ := v.(types.IndirectRef)
200-
return &indRef, nil
199+
return &v
201200
}
202201
}
203202

204-
return nil, nil
203+
return nil
205204
}
206205

207206
func FontIndRef(fName string, ctx *model.Context, fonts map[string]types.IndirectRef) (*types.IndirectRef, error) {
@@ -234,87 +233,6 @@ func FontIndRef(fName string, ctx *model.Context, fonts map[string]types.Indirec
234233
return nil, nil
235234
}
236235

237-
func ensureCorrectFontIndRef(
238-
ctx *model.Context,
239-
fontIndRef **types.IndirectRef,
240-
fName string,
241-
fonts map[string]types.IndirectRef) error {
242-
243-
d, err := ctx.DereferenceDict(**fontIndRef)
244-
if err != nil {
245-
return err
246-
}
247-
248-
if enc := d.NameEntry("Encoding"); enc != nil && *enc == "Identity-H" {
249-
indRef, ok := fonts[fName]
250-
if !ok {
251-
fonts[fName] = **fontIndRef
252-
return nil
253-
}
254-
if indRef != **fontIndRef {
255-
return errors.Errorf("pdfcpu: %s: duplicate fontDicts", fName)
256-
}
257-
return nil
258-
}
259-
260-
indRef, err := FontIndRef(fName, ctx, fonts)
261-
if err != nil {
262-
return err
263-
}
264-
if indRef != nil {
265-
*fontIndRef = indRef
266-
}
267-
268-
return nil
269-
}
270-
271-
func fontFromAcroDict(xRefTable *model.XRefTable, fontIndRef *types.IndirectRef, fName, fLang *string, fontID string) error {
272-
273-
// Use DA fontId from Acrodict
274-
275-
s := xRefTable.Form.StringEntry("DA")
276-
if s == nil {
277-
if fName != nil {
278-
return errors.Errorf("pdfcpu: unsupported font: %s", *fName)
279-
}
280-
return errors.Errorf("pdfcpu: unsupported fontID: %s", fontID)
281-
}
282-
283-
da := strings.Fields(*s)
284-
rootFontID := ""
285-
286-
for i := 0; i < len(da); i++ {
287-
if da[i] == "Tf" {
288-
if i >= 2 {
289-
rootFontID = da[i-2][1:]
290-
}
291-
break
292-
}
293-
}
294-
295-
if rootFontID == "" {
296-
if fName != nil {
297-
return errors.Errorf("pdfcpu: unsupported font: %s", *fName)
298-
}
299-
return errors.Errorf("pdfcpu: unsupported fontID: %s", fontID)
300-
}
301-
302-
fontID = rootFontID
303-
indRef, err := formFontIndRef(xRefTable, fontID)
304-
if err != nil {
305-
return err
306-
}
307-
308-
*fontIndRef = *indRef
309-
310-
*fName, *fLang, err = FormFontNameAndLangForID(xRefTable, *indRef)
311-
if err != nil {
312-
return err
313-
}
314-
315-
return nil
316-
}
317-
318236
func ensureUTF8FormFont(ctx *model.Context, fonts map[string]types.IndirectRef) (string, string, string, *types.IndirectRef, error) {
319237

320238
// TODO Make name of UTF-8 userfont part of pdfcpu configs.
@@ -357,11 +275,7 @@ func extractFormFontDetails(
357275

358276
if len(fontID) > 0 {
359277

360-
fontIndRef, err = formFontIndRef(xRefTable, fontID)
361-
if err != nil {
362-
return "", "", "", nil, err
363-
}
364-
278+
fontIndRef = formFontIndRef(xRefTable, fontID)
365279
if fontIndRef != nil {
366280
fName, fLang, err = FormFontNameAndLangForID(xRefTable, *fontIndRef)
367281
if err != nil {

pkg/pdfcpu/primitives/textField.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -447,32 +447,6 @@ func (tf *TextField) renderN(xRefTable *model.XRefTable) ([]byte, error) {
447447
}
448448

449449
tf.renderLines(xRefTable, boWidth, lh, w, y, lines, buf)
450-
//cjk := pdffont.CJK(f.Script, f.Lang)
451-
452-
// for i := 0; i < len(lines); i++ {
453-
// s := lines[i]
454-
// lineBB := model.CalcBoundingBox(s, 0, 0, f.Name, f.Size)
455-
// s = model.PrepBytes(xRefTable, s, f.Name, !cjk, f.RTL())
456-
// x := 2 * boWidth
457-
// if x == 0 {
458-
// x = 2
459-
// }
460-
// switch tf.HorAlign {
461-
// case types.AlignCenter:
462-
// x = w/2 - lineBB.Width()/2
463-
// case types.AlignRight:
464-
// x = w - lineBB.Width() - 2
465-
// }
466-
// fmt.Fprint(buf, "BT ")
467-
// if i == 0 {
468-
// fmt.Fprintf(buf, "/%s %d Tf %.2f %.2f %.2f RG %.2f %.2f %.2f rg ",
469-
// tf.fontID, f.Size,
470-
// f.col.R, f.col.G, f.col.B,
471-
// f.col.R, f.col.G, f.col.B)
472-
// }
473-
// fmt.Fprintf(buf, "%.2f %.2f Td (%s) Tj ET ", x, y, s)
474-
// y -= lh
475-
// }
476450

477451
if len(lines) > 0 {
478452
fmt.Fprint(buf, "Q ")

0 commit comments

Comments
 (0)