aboutsummaryrefslogtreecommitdiff
path: root/text.go
blob: a925f9151149ae39627441642f436167feec94bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package robo

import (
	"bufio"
	"io"
	"log"
)

type Font map[rune]Glyph

type Glyph struct {
	S Set
	W Unit
}

type Set []Path

func Print(c *bufio.Writer, in io.Reader, scale Unit) {
	var off Point

	scanner := bufio.NewScanner(in)
	for scanner.Scan() {
		font.putchar(c, scanner.Text(), scale, &off)
	}
	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
}

func (f Font) putchar(c *bufio.Writer, s string, scale Unit, off *Point) {
	for _, ch := range s {
		gl, ok := f[ch]
		if ok {
			if off.Y+gl.W*scale >= 4000 {
				off.X += height * scale
				off.Y = 0
			}
			off.Offset(c)
			for _, p := range gl.S {
				p.Scale(scale).Line(c)
				//p.Scale(scale).Curve(c, 0)
			}
			off.Y += gl.W * scale
		} else if ch == '\t' {
			tab := 8 * f['W'].W * scale // widest char
			pos := int(off.Y / tab)
			off.Y = Unit(pos+1) * tab
		}
	}
	off.X += height * scale
	off.Y = 0
}