aboutsummaryrefslogtreecommitdiff
path: root/ql/ql.go
blob: 3625f4be166cf3676fee5a50852897cbd2487806 (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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Package ql is a Linux driver for Brother QL-series printers.
package ql

// Resources:
//  http://etc.nkadesign.com/Printers/QL550LabelPrinterProtocol
//  https://github.com/torvalds/linux/blob/master/drivers/usb/class/usblp.c
//  http://www.undocprint.org/formats/page_description_languages/brother_p-touch
//  http://www.undocprint.org/formats/communication_protocols/ieee_1284

import (
	"image"
	"regexp"
	"strings"
)

// -----------------------------------------------------------------------------

var deviceIDRegexp = regexp.MustCompile(
	`(?s:\s*([^:,;]+?)\s*:\s*([^:;]*)\s*(?:;|$))`)

type deviceID map[string][]string

// parseIEEE1284DeviceID leniently parses an IEEE 1284 Device ID string
// and returns a map containing a slice of values for each key.
func parseIEEE1284DeviceID(id []byte) deviceID {
	m := make(deviceID)
	for _, kv := range deviceIDRegexp.FindAllStringSubmatch(string(id), -1) {
		var values []string
		for _, v := range strings.Split(kv[2], ",") {
			values = append(values, strings.Trim(v, "\t\n\v\f\r "))
		}
		m[kv[1]] = values
	}
	return m
}

func (id deviceID) Find(key, abbreviation string) []string {
	if values, ok := id[key]; ok {
		return values
	}
	if values, ok := id[abbreviation]; ok {
		return values
	}
	return nil
}

func (id deviceID) FindFirst(key, abbreviation string) string {
	for _, s := range id.Find(key, abbreviation) {
		return s
	}
	return ""
}

// -----------------------------------------------------------------------------

func compatible(id deviceID) bool {
	for _, commandSet := range id.Find("COMMAND SET", "CMD") {
		if commandSet == "PT-CBP" {
			return true
		}
	}
	return false
}

// -----------------------------------------------------------------------------

type mediaSize struct {
	WidthMM  int
	LengthMM int
}

type MediaInfo struct {
	// Note that these are approximates, many pins within the margins will work.
	SideMarginPins int
	PrintAreaPins  int
	// If non-zero, length of the die-cut label print area in 300dpi pins.
	PrintAreaLength int
}

var media = map[mediaSize]MediaInfo{
	// Continuous length tape
	{12, 0}: {29, 106, 0},
	{29, 0}: {6, 306, 0},
	{38, 0}: {12, 413, 0},
	{50, 0}: {12, 554, 0},
	{54, 0}: {0, 590, 0},
	{62, 0}: {12, 696, 0},

	// Die-cut labels
	{17, 54}:  {0, 165, 566},
	{17, 87}:  {0, 165, 956},
	{23, 23}:  {42, 236, 202},
	{29, 42}:  {6, 306, 425},
	{29, 90}:  {6, 306, 991},
	{38, 90}:  {12, 413, 991},
	{39, 48}:  {6, 425, 495},
	{52, 29}:  {0, 578, 271},
	{54, 29}:  {59, 602, 271},
	{60, 86}:  {24, 672, 954},
	{62, 29}:  {12, 696, 271},
	{62, 100}: {12, 696, 1109},

	// Die-cut diameter labels
	{12, 12}: {113, 94, 94},
	{24, 24}: {42, 236, 236},
	{58, 58}: {51, 618, 618},
}

func GetMediaInfo(widthMM, lengthMM int) *MediaInfo {
	if mi, ok := media[mediaSize{widthMM, lengthMM}]; ok {
		return &mi
	}
	return nil
}

// -----------------------------------------------------------------------------

const (
	printBytes = 90
	printPins  = printBytes * 8
)

// pack packs a bool array into a byte array for the printer to print out.
func pack(data [printPins]bool, out *[]byte) {
	for i := 0; i < printBytes; i++ {
		var b byte
		for j := 0; j < 8; j++ {
			b <<= 1
			if data[i*8+j] {
				b |= 1
			}
		}
		*out = append(*out, b)
	}
}

// makeBitmapDataRB converts an image to the printer's red-black raster format.
func makeBitmapDataRB(src image.Image, margin, length int) []byte {
	data, bounds := []byte{}, src.Bounds()
	if bounds.Dy() > length {
		bounds.Max.Y = bounds.Min.Y + length
	}
	if bounds.Dx() > printPins-margin {
		bounds.Max.X = bounds.Min.X + printPins
	}

	redcells, blackcells := [printPins]bool{}, [printPins]bool{}
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		length--

		// The graphics needs to be inverted horizontally, iterating backwards.
		offset := margin
		for x := bounds.Max.X - 1; x >= bounds.Min.X; x-- {
			r, g, b, a := src.At(x, y).RGBA()
			redcells[offset] = r >= 0xc000 && g < 0x4000 && b < 0x4000 &&
				a >= 0x8000
			blackcells[offset] = r < 0x4000 && g < 0x4000 && b < 0x4000 &&
				a >= 0x8000
			offset++
		}

		data = append(data, 'w', 0x01, printBytes)
		pack(blackcells, &data)
		data = append(data, 'w', 0x02, printBytes)
		pack(redcells, &data)
	}
	for ; length > 0; length-- {
		data = append(data, 'w', 0x01, printBytes)
		data = append(data, make([]byte, printBytes)...)
		data = append(data, 'w', 0x02, printBytes)
		data = append(data, make([]byte, printBytes)...)
	}
	return data
}

// makeBitmapData converts an image to the printer's raster format.
func makeBitmapData(src image.Image, rb bool, margin, length int) []byte {
	// It's a necessary nuisance, so just copy and paste.
	if rb {
		return makeBitmapDataRB(src, margin, length)
	}

	data, bounds := []byte{}, src.Bounds()
	if bounds.Dy() > length {
		bounds.Max.Y = bounds.Min.Y + length
	}
	if bounds.Dx() > printPins-margin {
		bounds.Max.X = bounds.Min.X + printPins
	}

	pixels := [printPins]bool{}
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		length--

		// The graphics needs to be inverted horizontally, iterating backwards.
		offset := margin
		for x := bounds.Max.X - 1; x >= bounds.Min.X; x-- {
			r, g, b, a := src.At(x, y).RGBA()
			pixels[offset] = r < 0x4000 && g < 0x4000 && b < 0x4000 &&
				a >= 0x8000
			offset++
		}

		data = append(data, 'g', 0x00, printBytes)
		pack(pixels, &data)
	}
	for ; length > 0; length-- {
		data = append(data, 'g', 0x00, printBytes)
		data = append(data, make([]byte, printBytes)...)
	}
	return data
}

// XXX: It would be preferrable to know for certain if this is a red-black tape,
// because the printer refuses to print on a mismatch.
func makePrintData(status *Status, image image.Image, rb bool) (data []byte) {
	mediaInfo := GetMediaInfo(
		status.MediaWidthMM(),
		status.MediaLengthMM(),
	)
	if mediaInfo == nil {
		return nil
	}

	// Raster mode.
	// Should be the only supported mode for QL-800.
	data = append(data, 0x1b, 0x69, 0x61, 0x01)

	// Automatic status mode (though it's the default).
	data = append(data, 0x1b, 0x69, 0x21, 0x00)

	// Print information command.
	dy := image.Bounds().Dy()
	if mediaInfo.PrintAreaLength != 0 {
		dy = mediaInfo.PrintAreaLength
	}

	mediaType := byte(0x0a)
	if status.MediaLengthMM() != 0 {
		mediaType = byte(0x0b)
	}

	data = append(data, 0x1b, 0x69, 0x7a, 0x02|0x04|0x40|0x80, mediaType,
		byte(status.MediaWidthMM()), byte(status.MediaLengthMM()),
		byte(dy), byte(dy>>8), byte(dy>>16), byte(dy>>24), 0, 0x00)

	// Auto cut, each 1 label.
	data = append(data, 0x1b, 0x69, 0x4d, 0x40)
	data = append(data, 0x1b, 0x69, 0x41, 0x01)

	// Cut at end (though it's the default).
	// Not sure what it means, doesn't seem to have any effect to turn it off.
	if rb {
		data = append(data, 0x1b, 0x69, 0x4b, 0x08|0x01)
	} else {
		data = append(data, 0x1b, 0x69, 0x4b, 0x08)
	}

	if status.MediaLengthMM() != 0 {
		// 3mm margins along the direction of feed. 0x23 = 35 dots, the minimum.
		data = append(data, 0x1b, 0x69, 0x64, 0x23, 0x00)
	} else {
		// May not set anything other than zero.
		data = append(data, 0x1b, 0x69, 0x64, 0x00, 0x00)
	}

	// Compression mode: no compression.
	// Should be the only supported mode for QL-800.
	data = append(data, 0x4d, 0x00)

	// The graphics data itself.
	bitmapData := makeBitmapData(image, rb, mediaInfo.SideMarginPins, dy)
	data = append(data, bitmapData...)

	// Print command with feeding.
	return append(data, 0x1a)
}