diff --git a/file_util.go b/file_util.go index a560afa..d6e4553 100644 --- a/file_util.go +++ b/file_util.go @@ -66,3 +66,21 @@ func InsertLine(lines *[]string, pos int64, l string) { func WriteFile(path string, content []byte) error { return ioutil.WriteFile(path, content, 0644) // drw-r--r-- } + +func ListDir(path string) (fnames []string, err error) { + finfo, err := ioutil.ReadDir(path) + if err != nil { + return nil, err + } + + for _, f := range finfo { + fnames = append(fnames, f.Name()) + } + + return fnames, nil +} + +func WorkingDir() string { + dir, _ := os.Getwd() + return dir +} diff --git a/pdfhelper/pdf.go b/pdfhelper/pdf.go index 66bd4ac..a1ccacb 100644 --- a/pdfhelper/pdf.go +++ b/pdfhelper/pdf.go @@ -102,6 +102,12 @@ type FormatedCell struct { Alignment string } +func (pdf *Helper) FCell(x, y float64, c FormatedCell) { + pdf.SetXY(x, y) + pdf.SetFont(c.Font, c.FontStyle, c.FontSize) + pdf.CellFormat(c.W, c.H, pdf.ToUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") +} + func (pdf *Helper) Column(x, y float64, cells []FormatedCell) { pdf.SetXY(x, y) for _, c := range cells { @@ -184,12 +190,11 @@ type PDFCellAligned struct { } func (pdf *Helper) TextLength(txt, family, style string, size float64) float64 { - family, _, _, _ = pdf.setCorrectFontFamily(txt) + family, _, _, _ = pdf.setCorrectFontFamily(textEncoding(txt)) return pdf.Fpdf.TextLength(txt, family, style, size) } -// ToUTF8 ... -func (pdf *Helper) ToUTF8(s string) string { +func textEncoding(s string) string { encoding := latinEncoding runes := []rune(s) for _, r := range runes { @@ -198,6 +203,12 @@ func (pdf *Helper) ToUTF8(s string) string { break } } + return encoding +} + +// ToUTF8 ... +func (pdf *Helper) ToUTF8(s string) string { + encoding := textEncoding(s) pdf.setCorrectFontFamily(encoding) translator, ok := pdf.translators[encoding] if !ok { @@ -209,12 +220,12 @@ func (pdf *Helper) ToUTF8(s string) string { func (pdf *Helper) setCorrectFontFamily(enc string) (family, style string, ptSize, unitSize float64) { family, style, ptSize, unitSize = pdf.GetFontInfo() if enc == cyrillicEncoding { - if !strings.HasSuffix(family, "Cyrillic") { - family += "Cyrillic" + if !strings.HasSuffix(family, "cyrillic") { + family += "cyrillic" } } else { - if strings.HasSuffix(family, "Cyrillic") { - family = strings.TrimSuffix(family, "Cyrillic") + if strings.HasSuffix(family, "cyrillic") { + family = strings.TrimSuffix(family, "cyrillic") } } pdf.SetFont(family, style, ptSize) @@ -254,14 +265,12 @@ func (pdf *Helper) InsertPageNumber(x, y float64, format string) { pdf.Column(x, y, []FormatedCell{{10, 1, num, "DejaVuSans", "", 8, NOBORDER, LEFT}}) } -func (pdf *Helper) WriteSuperscript( - txt, script string, - x, y float64, - fontFamily, fontStyle string, - fontSize float64) { +func (pdf *Helper) SuperscriptText(x, y, cw, ch float64, text, script string) { + family, style, size, sizeU := pdf.setCorrectFontFamily(textEncoding(text)) + + pdf.FCell(x, y, FormatedCell{cw, ch, text, family, style, size, NOBORDER, LEFT}) - scriptOffsetX := x + pdf.TextLength(txt, fontFamily, fontStyle, fontSize) - scriptOffsetY := y - fontSize*0.3 - pdf.Column(x, y, []FormatedCell{{5.0, 5.0, txt, fontFamily, fontStyle, 6, NOBORDER, LEFT}}) - pdf.Column(scripttxtOffset, scriptOffsetY, []FormatedCell{{5.0, 5.0, script, fontFamily, fontStyle, 6, NOBORDER, LEFT}}) + sx := x + pdf.TextLength(text, family, style, size) + sy := y - sizeU*0.2 + pdf.FCell(sx, sy, FormatedCell{cw, ch, script, family, style, size - 2, NOBORDER, LEFT}) }