Skip to content

Commit a966a9c

Browse files
committed
Fix #1047
1 parent 6a2b4f7 commit a966a9c

File tree

4 files changed

+31
-57
lines changed

4 files changed

+31
-57
lines changed

pkg/pdfcpu/form/fill.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -988,16 +988,24 @@ func fillTextField(
988988

989989
comb := ff != nil && primitives.FieldFlags(*ff)&primitives.FieldComb > 0
990990

991+
maxLen := 0
992+
991993
kids := d.ArrayEntry("Kids")
992994
if len(kids) > 0 {
995+
996+
i := d.IntEntry("MaxLen")
997+
if i != nil {
998+
maxLen = *i
999+
}
1000+
9931001
for _, o := range kids {
9941002

9951003
d, err := ctx.DereferenceDict(o)
9961004
if err != nil {
9971005
return err
9981006
}
9991007

1000-
if err := primitives.EnsureTextFieldAP(ctx, d, vNew, multiLine, comb, fonts); err != nil {
1008+
if err := primitives.EnsureTextFieldAP(ctx, d, vNew, multiLine, comb, maxLen, fonts); err != nil {
10011009
return err
10021010
}
10031011

@@ -1007,7 +1015,7 @@ func fillTextField(
10071015
return nil
10081016
}
10091017

1010-
if err := primitives.EnsureTextFieldAP(ctx, d, vNew, multiLine, comb, fonts); err != nil {
1018+
if err := primitives.EnsureTextFieldAP(ctx, d, vNew, multiLine, comb, maxLen, fonts); err != nil {
10111019
return err
10121020
}
10131021

pkg/pdfcpu/form/form.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ func resetTx(ctx *model.Context, d types.Dict, fonts map[string]types.IndirectRe
14251425
ff := d.IntEntry("Ff")
14261426
multiLine := ff != nil && uint(primitives.FieldFlags(*ff))&uint(primitives.FieldMultiline) > 0
14271427
comb := ff != nil && uint(primitives.FieldFlags(*ff))&uint(primitives.FieldComb) > 0
1428-
err = primitives.EnsureTextFieldAP(ctx, d, s, multiLine, comb, fonts)
1428+
err = primitives.EnsureTextFieldAP(ctx, d, s, multiLine, comb, 0, fonts)
14291429
}
14301430

14311431
return err

pkg/pdfcpu/model/parseContent.go

+8-44
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,21 @@ func nextContentToken(pre string, line *string, prn PageResourceNames) (string,
302302
return t, nil
303303
}
304304

305-
func resourceNameAtPos1(s, name string, prn PageResourceNames) (string, bool) {
305+
func colorSpace(s, name string, prn PageResourceNames) bool {
306306
if strings.HasPrefix(s, "cs") || strings.HasPrefix(s, "CS") {
307307
if !types.MemberOf(name, []string{"DeviceGray", "DeviceRGB", "DeviceCMYK", "Pattern"}) {
308308
prn["ColorSpace"][name] = true
309309
if log.ParseEnabled() {
310310
log.Parse.Printf("ColorSpace[%s]\n", name)
311311
}
312312
}
313+
return true
314+
}
315+
return false
316+
}
317+
318+
func resourceNameAtPos1(s, name string, prn PageResourceNames) (string, bool) {
319+
if colorSpace(s, name, prn) {
313320
return s[2:], true
314321
}
315322

@@ -353,49 +360,6 @@ func resourceNameAtPos1(s, name string, prn PageResourceNames) (string, bool) {
353360
return s[3:], true
354361
}
355362

356-
// switch s {
357-
// case "cs", "CS":
358-
// if !types.MemberOf(name, []string{"DeviceGray", "DeviceRGB", "DeviceCMYK", "Pattern"}) {
359-
// prn["ColorSpace"][name] = true
360-
// if log.ParseEnabled() {
361-
// log.Parse.Printf("ColorSpace[%s]\n", name)
362-
// }
363-
// }
364-
// return "", true
365-
366-
// case "gs":
367-
// prn["ExtGState"][name] = true
368-
// if log.ParseEnabled() {
369-
// log.Parse.Printf("ExtGState[%s]\n", name)
370-
// }
371-
// return "", true
372-
373-
// case "Do":
374-
// prn["XObject"][name] = true
375-
// if log.ParseEnabled() {
376-
// log.Parse.Printf("XObject[%s]\n", name)
377-
// }
378-
// return "", true
379-
380-
// case "sh":
381-
// prn["Shading"][name] = true
382-
// if log.ParseEnabled() {
383-
// log.Parse.Printf("Shading[%s]\n", name)
384-
// }
385-
// return "", true
386-
387-
// case "scn", "SCN":
388-
// prn["Pattern"][name] = true
389-
// if log.ParseEnabled() {
390-
// log.Parse.Printf("Pattern[%s]\n", name)
391-
// }
392-
// return "", true
393-
394-
// case "ri", "BMC", "MP":
395-
// return "", true
396-
397-
// }
398-
399363
return "", false
400364
}
401365

pkg/pdfcpu/primitives/textField.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -918,15 +918,17 @@ func NewTextField(
918918
v string,
919919
multiLine bool,
920920
comb bool,
921+
maxLen int,
921922
fontIndRef *types.IndirectRef,
922923
fonts map[string]types.IndirectRef) (*TextField, *types.IndirectRef, error) {
923924

924925
tf := &TextField{Value: v, Multiline: multiLine, Comb: comb}
925926

926927
i := d.IntEntry("MaxLen") // Inheritable!
927928
if i != nil {
928-
tf.MaxLen = *i
929+
maxLen = *i
929930
}
931+
tf.MaxLen = maxLen
930932

931933
bb, err := ctx.RectForArray(d.ArrayEntry("Rect"))
932934
if err != nil {
@@ -963,14 +965,14 @@ func NewTextField(
963965
return tf, fontIndRef, nil
964966
}
965967

966-
func renderTextFieldAP(ctx *model.Context, d types.Dict, v string, multiLine, comb bool, fonts map[string]types.IndirectRef) error {
968+
func renderTextFieldAP(ctx *model.Context, d types.Dict, v string, multiLine, comb bool, maxLen int, fonts map[string]types.IndirectRef) error {
967969
if ap := d.DictEntry("AP"); ap != nil {
968970
if err := ctx.DeleteObject(ap); err != nil {
969971
return err
970972
}
971973
}
972974

973-
tf, fontIndRef, err := NewTextField(ctx, d, v, multiLine, comb, nil, fonts)
975+
tf, fontIndRef, err := NewTextField(ctx, d, v, multiLine, comb, maxLen, nil, fonts)
974976
if err != nil {
975977
return err
976978
}
@@ -1036,15 +1038,15 @@ func fontAttrs(ctx *model.Context, fd types.Dict, fontID, text string, fonts map
10361038
return fontID, name, lang, fontIndRef, nil
10371039
}
10381040

1039-
func EnsureTextFieldAP(ctx *model.Context, d types.Dict, text string, multiLine, comb bool, fonts map[string]types.IndirectRef) error {
1041+
func EnsureTextFieldAP(ctx *model.Context, d types.Dict, text string, multiLine, comb bool, maxLen int, fonts map[string]types.IndirectRef) error {
10401042
ap := d.DictEntry("AP")
10411043
if ap == nil {
1042-
return renderTextFieldAP(ctx, d, text, multiLine, comb, fonts)
1044+
return renderTextFieldAP(ctx, d, text, multiLine, comb, maxLen, fonts)
10431045
}
10441046

10451047
irN := ap.IndirectRefEntry("N")
10461048
if irN == nil {
1047-
return renderTextFieldAP(ctx, d, text, multiLine, comb, fonts)
1049+
return renderTextFieldAP(ctx, d, text, multiLine, comb, maxLen, fonts)
10481050
}
10491051

10501052
sd, _, err := ctx.DereferenceStreamDict(*irN)
@@ -1054,20 +1056,20 @@ func EnsureTextFieldAP(ctx *model.Context, d types.Dict, text string, multiLine,
10541056

10551057
obj, ok := sd.Find("Resources")
10561058
if !ok {
1057-
return renderTextFieldAP(ctx, d, text, multiLine, comb, fonts)
1059+
return renderTextFieldAP(ctx, d, text, multiLine, comb, maxLen, fonts)
10581060
}
10591061

10601062
d1, err := ctx.DereferenceDict(obj)
10611063
if err != nil {
10621064
return err
10631065
}
10641066
if d1 == nil {
1065-
return renderTextFieldAP(ctx, d, text, multiLine, comb, fonts)
1067+
return renderTextFieldAP(ctx, d, text, multiLine, comb, maxLen, fonts)
10661068
}
10671069

10681070
fd := d1.DictEntry("Font")
10691071
if fd == nil {
1070-
return renderTextFieldAP(ctx, d, text, multiLine, comb, fonts)
1072+
return renderTextFieldAP(ctx, d, text, multiLine, comb, maxLen, fonts)
10711073
}
10721074

10731075
s := d.StringEntry("DA")
@@ -1090,7 +1092,7 @@ func EnsureTextFieldAP(ctx *model.Context, d types.Dict, text string, multiLine,
10901092

10911093
fillFont := formFontIndRef(ctx.XRefTable, fontID) != nil
10921094

1093-
tf, _, err := NewTextField(ctx, d, text, multiLine, comb, fontIndRef, fonts)
1095+
tf, _, err := NewTextField(ctx, d, text, multiLine, comb, maxLen, fontIndRef, fonts)
10941096
if err != nil {
10951097
return err
10961098
}

0 commit comments

Comments
 (0)