aboutsummaryrefslogtreecommitdiff
path: root/ql/ql.go
blob: ec84656cbebb3e57bc9b906e6ab17aa15c6dbf3a (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
// 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
)

// makeBitmapData converts an image to the printer's raster format.
func makeBitmapData(src image.Image, margin, length int) (data []byte) {
	bounds := 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 == 0 && g == 0 && b == 0 && a >= 0x8000
			offset++
		}

		data = append(data, 'g', 0x00, printBytes)
		for i := 0; i < printBytes; i++ {
			var b byte
			for j := 0; j < 8; j++ {
				b <<= 1
				if pixels[i*8+j] {
					b |= 1
				}
			}
			data = append(data, b)
		}
	}
	for ; length > 0; length-- {
		data = append(data, 'g', 0x00, printBytes)
		data = append(data, make([]byte, printBytes)...)
	}
	return
}

func makePrintData(status *Status, image image.Image) (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.
	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.
	data = append(data, makeBitmapData(image, mediaInfo.SideMarginPins, dy)...)

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