github.com/signintech/pdft@v0.5.0/crawl_result_font.go (about) 1 package pdft 2 3 import ( 4 "bytes" 5 "fmt" 6 "strconv" 7 "strings" 8 "unicode" 9 ) 10 11 type crawlResultFonts []crawlResultFont 12 13 // Split a font string like '/F1' or '/TT2' into the prefix and index 14 func splitFont(s string) (string, int, error) { 15 prefix := "" 16 for i, r := range s { 17 if unicode.IsDigit(r) { 18 s := strings.ReplaceAll(s, "_", "") 19 val, err := strconv.Atoi(s[i:]) 20 if err != nil { 21 return "", 0, err 22 } 23 return prefix, val, nil 24 } 25 prefix += string(r) 26 } 27 return "", 0, fmt.Errorf("No font index") 28 } 29 30 func (c *crawlResultFonts) parse(propVal *[]byte) error { 31 var props PDFObjPropertiesData 32 err := readProperties(propVal, &props) 33 if err != nil { 34 return err 35 } 36 37 for _, prop := range props { 38 var crFont crawlResultFont 39 40 prefix, fontIndex, err := splitFont(strings.TrimSpace(prop.key)) 41 if err != nil { 42 return err 43 } 44 objID, _, err := prop.asDictionary() 45 if err != nil { 46 return err 47 } 48 crFont.prefix = prefix 49 crFont.fontIndex = fontIndex 50 crFont.fontObjID = objID 51 *c = append(*c, crFont) 52 } 53 return nil 54 } 55 56 func (c *crawlResultFonts) String() string { 57 var buff bytes.Buffer 58 for _, f := range *c { 59 buff.WriteString(fmt.Sprintf("/%s%d %d 0 R\n", f.prefix, f.fontIndex, f.fontObjID)) 60 } 61 return buff.String() 62 } 63 64 func (c *crawlResultFonts) append(fontIndex int, fontObjID int) { 65 var crFont crawlResultFont 66 crFont.fontIndex = fontIndex 67 crFont.fontObjID = fontObjID 68 crFont.prefix = "F" 69 *c = append(*c, crFont) 70 } 71 72 func (c *crawlResultFonts) maxFontIndex() int { 73 max := 0 74 for _, f := range *c { 75 if f.fontIndex > max { 76 max = f.fontIndex 77 } 78 } 79 return max 80 } 81 82 type crawlResultFont struct { 83 fontIndex int 84 fontObjID int 85 prefix string 86 }