~sbinet/star-tex

star-tex/cmd/tfm2pl/main.go -rw-r--r-- 2.0 KiB
9bb27e8aSebastien Binet ci: bump to freebsd/latest 8 months 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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.

// tfm2pl converts a TFM file to human-readable property list file or standard output.
//
// Usage: tfm2pl [options] file.tfm [file.pl]
//
// ex:
//  $> tfm2pl testdata/simple.tfm
//  $> tfm2pl testdata/simple.tfm out.pl
package main // import "star-tex.org/x/tex/cmd/tfm2pl"

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

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

func init() {
	log.SetPrefix("tfm2pl: ")
	log.SetFlags(0)

	flag.Usage = func() {
		fmt.Fprintf(
			os.Stderr,
			`Usage: tfm2pl [options] file.tfm [file.pl]

tfm2pl converts a TFM file to human-readable property list file or standard output.

ex:
 $> tfm2pl testdata/simple.tfm
 $> tfm2pl testdata/simple.tfm out.pl

options:
`,
		)
		flag.PrintDefaults()
	}
}

func main() {
	flag.Parse()

	if flag.NArg() < 1 {
		flag.Usage()
		log.Fatalf("missing path to input TFM file")
	}
	xmain(flag.Args())
}

func xmain(args []string) {

	fname := args[0]

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

	var (
		o     io.Writer = os.Stdout
		oname           = ""
	)

	if len(args) > 1 {
		oname = args[1]
		pl, err := os.Create(oname)
		if err != nil {
			log.Fatalf("could not create output PL file %q: %+v", oname, err)
		}
		defer func() {
			err := pl.Close()
			if err != nil {
				log.Fatalf("could not close output PL file %q: %+v", oname, err)
			}
		}()
		o = pl
	}

	err = process(o, f)
	if err != nil {
		log.Fatalf("could not process TFM file %q: %+v", fname, err)
	}
}

func process(w io.Writer, r io.Reader) error {
	fnt, err := tfm.Parse(r)
	if err != nil {
		return fmt.Errorf("could not parse TFM file: %w", err)
	}

	txt, err := fnt.MarshalText()
	if err != nil {
		return fmt.Errorf("could not marshal TFM font to PL: %w", err)
	}

	_, err = w.Write(txt)
	if err != nil {
		return fmt.Errorf("could not write PL text: %w", err)
	}

	return nil
}