~sbinet/star-tex

star-tex/cmd/pk2bm/main_test.go -rw-r--r-- 4.3 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 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 (
	"bytes"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"testing"

	"star-tex.org/x/tex/kpath"
)

func TestPK2BM(t *testing.T) {
	ktx := kpath.New()

	fname, err := ktx.Find("cmr10.pk")
	if err != nil {
		t.Fatalf("could not find cmr10: %+v", err)
	}

	pkf, err := ktx.Open(fname)
	if err != nil {
		t.Fatalf("could not open cmr10: %+v", err)
	}
	defer pkf.Close()

	const (
		bitmap = true
		hexmap = false
		h      = 0
		w      = 0
		c      = 'a'
	)

	got := new(bytes.Buffer)
	err = process(got, pkf, rune(c), h, w, bitmap, hexmap)
	if err != nil {
		t.Fatalf("could not process char=%c: %+v", c, err)
	}

	if got, want := got.String(), wantA; got != want {
		t.Fatalf("invalid pkf2bm output:\ngot:\n%s\nwant:\n%s", got, want)
	}
}

func TestTeXPK2BM(t *testing.T) {
	if cmd, err := exec.LookPath("/usr/bin/pk2bm"); err != nil || cmd == "" {
		t.Skipf("skipping comparison against TeX pk2bm")
	}

	const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`!@#$%^&*()_+-=[]{};'\\:\"|/.,<>?"
	const cmdir = "../../internal/tds/fonts/pk/ljfour/public/cm/dpi600"

	files, err := os.ReadDir(cmdir)
	if err != nil {
		t.Fatalf("could not open cm PK dir: %+v", err)
	}

	for _, file := range files {
		cmr10 := filepath.Join(cmdir, file.Name())
		pkf, err := os.ReadFile(cmr10)
		if err != nil {
			t.Fatalf("could not open cmr10 PK file: %+v", err)
		}
		t.Run(cmr10, func(t *testing.T) {

			for _, tc := range []byte(chars) {
				t.Run(string(tc), func(t *testing.T) {
					const (
						bitmap = true
						hexmap = false
						h      = 0
						w      = 0
					)
					got := new(bytes.Buffer)
					err := process(got, bytes.NewReader(pkf), rune(tc), h, w, bitmap, hexmap)
					if err != nil {
						t.Fatalf("could not process char=%c: %+v", tc, err)
					}

					args := []string{
						"-c", string(tc),
						"-H", strconv.Itoa(h),
						"-W", strconv.Itoa(w),
						cmr10,
					}

					switch {
					case bitmap:
						args = append([]string{"-b"}, args...)
					case hexmap:
						args = append([]string{"-h"}, args...)
					}

					want := new(bytes.Buffer)
					cmd := exec.Command(
						"/usr/bin/pk2bm",
						args...,
					)
					cmd.Stdout = want
					cmd.Stderr = want

					err = cmd.Run()
					if err != nil {
						t.Logf("===\n%s\n===\n", want.String())
						t.Fatalf("could not run TeX pk2bm: %+v", err)
					}

					if got, want := got.String(), want.String(); got != want {
						t.Fatalf("invalid pkf2bm output:\ngot:\n%s\nwant:\n%s", got, want)
					}
				})
			}
		})
	}
}

const wantA = `
character : 97 (a)
   height : 39
    width : 38
     xoff : -3
     yoff : 37

  ...........********...................
  ........**************................
  ......*****.......******..............
  .....***............*****.............
  ....*****............******...........
  ...*******............******..........
  ...********...........******..........
  ...********............******.........
  ...********............******.........
  ...********.............******........
  ....******..............******........
  .....****...............******........
  ........................******........
  ........................******........
  ........................******........
  ........................******........
  .................*************........
  .............*****************........
  ..........*********.....******........
  ........*******.........******........
  ......*******...........******........
  ....********............******........
  ...*******..............******........
  ..********..............******........
  .********...............******........
  .*******................******........
  .*******................******......**
  *******.................******......**
  *******.................******......**
  *******.................******......**
  *******................*******......**
  *******................*******......**
  ********..............********......**
  .*******.............***.*****......**
  .********............**...*****....**.
  ..********.........****...*****....**.
  ....*******......****......*********..
  ......**************........*******...
  .........********............*****....
`