github.com/signintech/pdft@v0.5.0/content_text.go (about) 1 package pdft 2 3 import ( 4 "bytes" 5 6 gopdf "github.com/signintech/pdft/minigopdf" 7 ) 8 9 type FontColor struct { 10 R int 11 G int 12 B int 13 } 14 15 // ContentText text in pdf 16 type ContentText struct { 17 text string 18 fontColor *FontColor 19 fontName string 20 fontStyle int 21 fontSize int 22 pageNum int 23 x float64 24 y float64 25 pdfFontData *PDFFontData 26 w float64 27 h float64 28 align int 29 lineWidth float64 30 pdfProtection *gopdf.PDFProtection 31 } 32 33 func (c *ContentText) setProtection(p *gopdf.PDFProtection) { 34 c.pdfProtection = p 35 } 36 37 func (c *ContentText) protection() *gopdf.PDFProtection { 38 return c.pdfProtection 39 } 40 41 func (c *ContentText) toSteram() (*bytes.Buffer, error) { 42 43 var border = 0 44 if c.lineWidth > 0 { 45 border = Left | Right | Top | Bottom 46 } 47 48 var rgb gopdf.Rgb 49 if c.fontColor != nil { 50 rgb.SetR(uint8(c.fontColor.R)) 51 rgb.SetG(uint8(c.fontColor.G)) 52 rgb.SetB(uint8(c.fontColor.B)) 53 } else { 54 rgb.SetR(1) 55 rgb.SetG(1) 56 rgb.SetB(1) 57 } 58 59 var cc gopdf.CacheContent 60 cc.Setup( 61 &gopdf.Rect{ 62 W: c.w, 63 H: c.h, 64 }, 65 rgb, 66 1.0, 67 c.pdfFontData.fontIndex(), 68 c.fontSize, 69 c.fontStyle, 70 0, 71 c.x, 72 c.y, 73 &c.pdfFontData.font, 74 pageHeight(), 75 gopdf.ContentTypeText, 76 gopdf.CellOption{ 77 Align: c.align, 78 Border: border, 79 }, 80 c.lineWidth, 81 ) 82 83 cc.WriteTextToContent(c.text) 84 buff, err := cc.ToStream(c.protection()) 85 if err != nil { 86 return nil, err 87 } 88 buff.Write([]byte("\r\n")) 89 90 return buff, nil 91 } 92 93 func (c *ContentText) page() int { 94 return c.pageNum 95 } 96 97 func (c *ContentText) measureTextWidth() (float64, error) { 98 var cc gopdf.CacheContent 99 cc.Setup( 100 &gopdf.Rect{ 101 W: c.w, 102 H: c.h, 103 }, 104 gopdf.Rgb{}, 105 1.0, 106 c.pdfFontData.fontIndex(), 107 c.fontSize, 108 c.fontStyle, 109 0, 110 0, 111 0, 112 &c.pdfFontData.font, 113 pageHeight(), 114 gopdf.ContentTypeText, 115 gopdf.CellOption{ 116 Align: c.align, 117 Border: 0, 118 }, 119 c.lineWidth, 120 ) 121 return cc.MeasureTextWidth(c.text) 122 }