@@ -11,6 +11,8 @@ import (
1111 "strings"
1212)
1313
14+ var pdfIndirectRefPattern = regexp .MustCompile (`(\d+)\s+(\d+)\s+R` )
15+
1416// PDFMetadata describes the extra PDF metadata velocity.report wants to stamp
1517// onto Typst-generated reports.
1618type PDFMetadata struct {
@@ -92,10 +94,7 @@ func applyPDFMetadata(pdf []byte, meta PDFMetadata) ([]byte, error) {
9294 if err != nil {
9395 return nil , err
9496 }
95- metadataRef , ok , err := parseIndirectRef (rootDict , "Metadata" )
96- if err != nil {
97- return nil , err
98- }
97+ metadataRef , ok := parseIndirectRef (rootDict , "Metadata" )
9998 if ok {
10099 metadataOffset , found := xrefOffsets [metadataRef .number ]
101100 if ! found {
@@ -253,28 +252,16 @@ func parseLastTrailerDict(pdf []byte) ([]byte, error) {
253252}
254253
255254func parsePDFTrailer (dict []byte ) (pdfTrailer , error ) {
256- size , ok , err := parsePDFInt (dict , "Size" )
257- if err != nil {
258- return pdfTrailer {}, err
259- }
255+ size , ok := parsePDFInt (dict , "Size" )
260256 if ! ok {
261257 return pdfTrailer {}, fmt .Errorf ("pdf trailer missing Size" )
262258 }
263- root , ok , err := parseIndirectRef (dict , "Root" )
264- if err != nil {
265- return pdfTrailer {}, err
266- }
259+ root , ok := parseIndirectRef (dict , "Root" )
267260 if ! ok {
268261 return pdfTrailer {}, fmt .Errorf ("pdf trailer missing Root" )
269262 }
270- info , hasInfo , err := parseIndirectRef (dict , "Info" )
271- if err != nil {
272- return pdfTrailer {}, err
273- }
274- id , _ , err := parsePDFRawValue (dict , "ID" , `\[[^\]]+\]` )
275- if err != nil {
276- return pdfTrailer {}, err
277- }
263+ info , hasInfo := parseIndirectRef (dict , "Info" )
264+ id , _ := parsePDFRawValue (dict , "ID" , `\[[^\]]+\]` )
278265
279266 trailer := pdfTrailer {size : size , root : root , id : id }
280267 if hasInfo {
@@ -396,49 +383,33 @@ func extractPDFStreamObject(obj []byte) ([]byte, []byte, error) {
396383 return obj [dictStart :dictEnd ], stream , nil
397384}
398385
399- func parseIndirectRef (dict []byte , key string ) (pdfRef , bool , error ) {
400- value , ok , err := parsePDFRawValue (dict , key , `(\d+)\s+(\d+)\s+R` )
401- if err != nil || ! ok {
402- return pdfRef {}, ok , err
403- }
404- re := regexp .MustCompile (`(\d+)\s+(\d+)\s+R` )
405- match := re .FindStringSubmatch (value )
406- if len (match ) != 3 {
407- return pdfRef {}, false , fmt .Errorf ("malformed pdf reference for %s" , key )
408- }
409- number , err := strconv .Atoi (match [1 ])
410- if err != nil {
411- return pdfRef {}, false , fmt .Errorf ("parse %s object number: %w" , key , err )
412- }
413- generation , err := strconv .Atoi (match [2 ])
414- if err != nil {
415- return pdfRef {}, false , fmt .Errorf ("parse %s generation: %w" , key , err )
386+ func parseIndirectRef (dict []byte , key string ) (pdfRef , bool ) {
387+ value , ok := parsePDFRawValue (dict , key , `(\d+)\s+(\d+)\s+R` )
388+ if ! ok {
389+ return pdfRef {}, false
416390 }
417- return pdfRef {number : number , generation : generation }, true , nil
391+ match := pdfIndirectRefPattern .FindStringSubmatch (value )
392+ number , _ := strconv .Atoi (match [1 ])
393+ generation , _ := strconv .Atoi (match [2 ])
394+ return pdfRef {number : number , generation : generation }, true
418395}
419396
420- func parsePDFInt (dict []byte , key string ) (int , bool , error ) {
421- value , ok , err := parsePDFRawValue (dict , key , `(\d+)` )
422- if err != nil || ! ok {
423- return 0 , ok , err
424- }
425- parsed , err := strconv .Atoi (value )
426- if err != nil {
427- return 0 , false , fmt .Errorf ("parse %s: %w" , key , err )
397+ func parsePDFInt (dict []byte , key string ) (int , bool ) {
398+ value , ok := parsePDFRawValue (dict , key , `(\d+)` )
399+ if ! ok {
400+ return 0 , false
428401 }
429- return parsed , true , nil
402+ parsed , _ := strconv .Atoi (value )
403+ return parsed , true
430404}
431405
432- func parsePDFRawValue (dict []byte , key , pattern string ) (string , bool , error ) {
406+ func parsePDFRawValue (dict []byte , key , pattern string ) (string , bool ) {
433407 re := regexp .MustCompile (`/` + regexp .QuoteMeta (key ) + `\s+(` + pattern + `)` )
434408 match := re .FindSubmatch (dict )
435409 if len (match ) == 0 {
436- return "" , false , nil
410+ return "" , false
437411 }
438- if len (match ) < 2 {
439- return "" , false , fmt .Errorf ("malformed pdf value for %s" , key )
440- }
441- return string (match [1 ]), true , nil
412+ return string (match [1 ]), true
442413}
443414
444415func replaceOrAddPDFString (dict []byte , key , value string ) []byte {
@@ -475,10 +446,7 @@ func insertIntoPDFDict(dict, entry []byte) []byte {
475446}
476447
477448func replaceOrAddXMLTag (doc []byte , tag , value string ) ([]byte , error ) {
478- escaped , err := escapeXMLText (value )
479- if err != nil {
480- return nil , err
481- }
449+ escaped := escapeXMLText (value )
482450 replacement := []byte (fmt .Sprintf ("<%s>%s</%s>" , tag , escaped , tag ))
483451 re := regexp .MustCompile (`(?s)<` + regexp .QuoteMeta (tag ) + `>.*?</` + regexp .QuoteMeta (tag ) + `>` )
484452 if re .Match (doc ) {
@@ -515,12 +483,10 @@ func escapePDFString(value string) string {
515483 return out .String ()
516484}
517485
518- func escapeXMLText (value string ) ( string , error ) {
486+ func escapeXMLText (value string ) string {
519487 var out bytes.Buffer
520- if err := xml .EscapeText (& out , []byte (value )); err != nil {
521- return "" , err
522- }
523- return out .String (), nil
488+ _ = xml .EscapeText (& out , []byte (value ))
489+ return out .String ()
524490}
525491
526492func skipPDFWhitespace (pdf []byte , index int ) int {
0 commit comments