~sbinet/star-tex

ref: 01f50749024c419b3b1800893d74a02994b9ecbb star-tex/cmd/pk2bm/main.go -rw-r--r-- 1.3 KiB
01f50749Sebastien Binet {cmd/pk2bm,font/pkf}: first import 1 year, 1 month ago
                                                                                
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
53
54
55
56
57
58
59
60
// Copyright ©2021 The star-tex Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main // import "star-tex.org/x/tex/cmd/pk2bm"

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"

	"star-tex.org/x/tex/font/pkf"
)

func main() {
	log.SetPrefix("pk2bm: ")
	log.SetFlags(0)

	var (
		cflag  = flag.String("c", "", "character to display")
		hflag  = flag.Int("H", 0, "height of bitmap")
		wflag  = flag.Int("W", 0, "width of bitmap")
		bitmap = flag.Bool("b", false, "generate a bitmap")
		hexmap = flag.Bool("h", false, "generate a hexmap")
	)

	flag.Parse()

	if *hexmap && *bitmap {
		log.Fatalf("you need to chose either -h or -b")
	}

	f, err := os.Open(flag.Arg(0))
	if err != nil {
		log.Fatalf("could not open PK file: %+v", err)
	}
	defer f.Close()

	err = process(os.Stdout, f, rune((*cflag)[0]), *hflag, *wflag, *bitmap, *hexmap)
	if err != nil {
		log.Fatalf("could not process PK file: %+v", err)
	}
}

func process(o io.Writer, r io.Reader, c rune, h, w int, bitmap, hexmap bool) error {
	f, err := pkf.Parse(r)
	if err != nil {
		return fmt.Errorf("could not parse PK font: %w", err)
	}
	switch {
	case bitmap:
		return f.Bitmap(o, c, h, w)
	case hexmap:
		return f.Hexmap(o, c, h, w)
	default:
		return f.Rawmap(o, c, h, w)
	}
}