Commit ec4067ed8ba730da221afb46f9dc988110e3c968

Authored by Marko Tikvić
1 parent 0487cafcd4
Exists in master

accessories

Showing 2 changed files with 43 additions and 16 deletions   Show diff stats
... ... @@ -66,3 +66,21 @@ func InsertLine(lines *[]string, pos int64, l string) {
66 66 func WriteFile(path string, content []byte) error {
67 67 return ioutil.WriteFile(path, content, 0644) // drw-r--r--
68 68 }
  69 +
  70 +func ListDir(path string) (fnames []string, err error) {
  71 + finfo, err := ioutil.ReadDir(path)
  72 + if err != nil {
  73 + return nil, err
  74 + }
  75 +
  76 + for _, f := range finfo {
  77 + fnames = append(fnames, f.Name())
  78 + }
  79 +
  80 + return fnames, nil
  81 +}
  82 +
  83 +func WorkingDir() string {
  84 + dir, _ := os.Getwd()
  85 + return dir
  86 +}
... ...
... ... @@ -102,6 +102,12 @@ type FormatedCell struct {
102 102 Alignment string
103 103 }
104 104  
  105 +func (pdf *Helper) FCell(x, y float64, c FormatedCell) {
  106 + pdf.SetXY(x, y)
  107 + pdf.SetFont(c.Font, c.FontStyle, c.FontSize)
  108 + pdf.CellFormat(c.W, c.H, pdf.ToUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "")
  109 +}
  110 +
105 111 func (pdf *Helper) Column(x, y float64, cells []FormatedCell) {
106 112 pdf.SetXY(x, y)
107 113 for _, c := range cells {
... ... @@ -184,12 +190,11 @@ type PDFCellAligned struct {
184 190 }
185 191  
186 192 func (pdf *Helper) TextLength(txt, family, style string, size float64) float64 {
187   - family, _, _, _ = pdf.setCorrectFontFamily(txt)
  193 + family, _, _, _ = pdf.setCorrectFontFamily(textEncoding(txt))
188 194 return pdf.Fpdf.TextLength(txt, family, style, size)
189 195 }
190 196  
191   -// ToUTF8 ...
192   -func (pdf *Helper) ToUTF8(s string) string {
  197 +func textEncoding(s string) string {
193 198 encoding := latinEncoding
194 199 runes := []rune(s)
195 200 for _, r := range runes {
... ... @@ -198,6 +203,12 @@ func (pdf *Helper) ToUTF8(s string) string {
198 203 break
199 204 }
200 205 }
  206 + return encoding
  207 +}
  208 +
  209 +// ToUTF8 ...
  210 +func (pdf *Helper) ToUTF8(s string) string {
  211 + encoding := textEncoding(s)
201 212 pdf.setCorrectFontFamily(encoding)
202 213 translator, ok := pdf.translators[encoding]
203 214 if !ok {
... ... @@ -209,12 +220,12 @@ func (pdf *Helper) ToUTF8(s string) string {
209 220 func (pdf *Helper) setCorrectFontFamily(enc string) (family, style string, ptSize, unitSize float64) {
210 221 family, style, ptSize, unitSize = pdf.GetFontInfo()
211 222 if enc == cyrillicEncoding {
212   - if !strings.HasSuffix(family, "Cyrillic") {
213   - family += "Cyrillic"
  223 + if !strings.HasSuffix(family, "cyrillic") {
  224 + family += "cyrillic"
214 225 }
215 226 } else {
216   - if strings.HasSuffix(family, "Cyrillic") {
217   - family = strings.TrimSuffix(family, "Cyrillic")
  227 + if strings.HasSuffix(family, "cyrillic") {
  228 + family = strings.TrimSuffix(family, "cyrillic")
218 229 }
219 230 }
220 231 pdf.SetFont(family, style, ptSize)
... ... @@ -254,14 +265,12 @@ func (pdf *Helper) InsertPageNumber(x, y float64, format string) {
254 265 pdf.Column(x, y, []FormatedCell{{10, 1, num, "DejaVuSans", "", 8, NOBORDER, LEFT}})
255 266 }
256 267  
257   -func (pdf *Helper) WriteSuperscript(
258   - txt, script string,
259   - x, y float64,
260   - fontFamily, fontStyle string,
261   - fontSize float64) {
  268 +func (pdf *Helper) SuperscriptText(x, y, cw, ch float64, text, script string) {
  269 + family, style, size, sizeU := pdf.setCorrectFontFamily(textEncoding(text))
  270 +
  271 + pdf.FCell(x, y, FormatedCell{cw, ch, text, family, style, size, NOBORDER, LEFT})
262 272  
263   - scriptOffsetX := x + pdf.TextLength(txt, fontFamily, fontStyle, fontSize)
264   - scriptOffsetY := y - fontSize*0.3
265   - pdf.Column(x, y, []FormatedCell{{5.0, 5.0, txt, fontFamily, fontStyle, 6, NOBORDER, LEFT}})
266   - pdf.Column(scripttxtOffset, scriptOffsetY, []FormatedCell{{5.0, 5.0, script, fontFamily, fontStyle, 6, NOBORDER, LEFT}})
  273 + sx := x + pdf.TextLength(text, family, style, size)
  274 + sy := y - sizeU*0.2
  275 + pdf.FCell(sx, sy, FormatedCell{cw, ch, script, family, style, size - 2, NOBORDER, LEFT})
267 276 }
... ...