First actual commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Parser struct {
|
||||
Charmap map[int]PrinterChar
|
||||
}
|
||||
|
||||
// charSize returns the number of bits on given char in UTF-8
|
||||
func charSize(b byte) int {
|
||||
toRet := -1
|
||||
|
||||
if b>>7 == 0b0 {
|
||||
toRet = 1
|
||||
} else if b>>5 == 0b110 {
|
||||
toRet = 2
|
||||
} else if b>>4 == 0b1110 {
|
||||
toRet = 3
|
||||
} else if b>>3 == 0b11110 {
|
||||
toRet = 4
|
||||
} else if b>>2 == 0b111110 {
|
||||
toRet = 5
|
||||
} else if b>>1 == 0b1111110 {
|
||||
toRet = 6
|
||||
}
|
||||
|
||||
return toRet
|
||||
}
|
||||
|
||||
// Initialize the charmap
|
||||
func (p *Parser) Initialize() {
|
||||
p.Charmap = buildTables()
|
||||
}
|
||||
|
||||
// process will replace unicode characters of the message by the correct
|
||||
// character of the correct code page for the thermal printer to print
|
||||
func (p *Parser) Process(m string) []byte {
|
||||
message := make([]byte, 0)
|
||||
for k := 0; k < len([]byte(m)); k++ {
|
||||
size := charSize(m[k])
|
||||
|
||||
toAppend := []byte{'X'}
|
||||
if size == 1 {
|
||||
// Check if its an escape sequence
|
||||
if int(m[k]) == '\\' {
|
||||
if int(m[k+1]) == 'n' {
|
||||
toAppend = []byte{0x0A}
|
||||
} else if int(m[k+1]) == 't' {
|
||||
toAppend = []byte{0x09}
|
||||
}
|
||||
k += 1
|
||||
} else {
|
||||
toAppend = []byte{m[k]}
|
||||
}
|
||||
} else {
|
||||
char, _ := utf8.DecodeRune([]byte(m[k : k+(size)]))
|
||||
_, ok := p.Charmap[int(char)]
|
||||
if ok {
|
||||
toAppend = []byte{
|
||||
0x1B,
|
||||
0x74,
|
||||
byte(p.Charmap[int(char)].Table),
|
||||
byte(p.Charmap[int(char)].Hex),
|
||||
}
|
||||
}
|
||||
|
||||
k += (size - 1)
|
||||
}
|
||||
|
||||
message = append(message, toAppend...)
|
||||
}
|
||||
|
||||
return message
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
// "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestProcessWithASCII(t *testing.T) {
|
||||
data := "m"
|
||||
expect := []byte("m")
|
||||
|
||||
var p Parser
|
||||
res := p.Process(data)
|
||||
|
||||
if !bytes.Equal(res, expect) {
|
||||
t.Errorf("%x is different of %x\n", res, expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessWithUTF8(t *testing.T) {
|
||||
data := "€"
|
||||
expect := []byte{0x1B, 0x74, 0x10, 0x80}
|
||||
|
||||
var p Parser
|
||||
p.Initialize()
|
||||
res := p.Process(data)
|
||||
|
||||
if !bytes.Equal(res, expect) {
|
||||
t.Errorf("%x is different of %x\n", res, expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessUnavailableChar(t *testing.T) {
|
||||
data := "�"
|
||||
expect := []byte("X")
|
||||
|
||||
var p Parser
|
||||
res := p.Process(data)
|
||||
|
||||
if !bytes.Equal(res, expect) {
|
||||
t.Errorf("%x is different of %x\n", res, expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessNewLineChar(t *testing.T) {
|
||||
data := "\\n"
|
||||
expect := []byte{0x0A}
|
||||
|
||||
var p Parser
|
||||
res := p.Process(data)
|
||||
|
||||
if !bytes.Equal(res, expect) {
|
||||
t.Errorf("%x is different of %x\n", res, expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessNewTabChar(t *testing.T) {
|
||||
data := "\\t"
|
||||
expect := []byte{0x09}
|
||||
|
||||
var p Parser
|
||||
res := p.Process(data)
|
||||
|
||||
if !bytes.Equal(res, expect) {
|
||||
t.Errorf("%x is different of %x\n", res, expect)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"unicode/utf8"
|
||||
|
||||
iconv "github.com/djimenez/iconv-go"
|
||||
)
|
||||
|
||||
var (
|
||||
CAPABILITES = "/home/esilva/Desktop/projetos/escpos-printer-db/dist/capabilities.json"
|
||||
)
|
||||
|
||||
type PrinterEncodings struct {
|
||||
IconvName string `json:"iconv"`
|
||||
Data []string `json:"data"`
|
||||
}
|
||||
|
||||
type PrinterProfile struct {
|
||||
CodePages map[string]string `json:"codePages"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Capabilites struct {
|
||||
Profiles map[string]PrinterProfile `json:"profiles"`
|
||||
Encodings map[string]PrinterEncodings `json:"encodings"`
|
||||
}
|
||||
|
||||
type PrinterChar struct {
|
||||
Table int
|
||||
Hex int
|
||||
Utf string
|
||||
}
|
||||
|
||||
func buildTables() map[int]PrinterChar {
|
||||
capabilities := loadCapabilities()
|
||||
|
||||
data := make(map[int]PrinterChar)
|
||||
for n, enc := range capabilities.Profiles["HZ-8360"].CodePages {
|
||||
// Check if code page is present in the available encodings
|
||||
_, ok := capabilities.Encodings[enc]
|
||||
if ok && len(capabilities.Encodings[enc].IconvName) > 0 {
|
||||
// Open a new iconv converter for CP037 to UTF-8
|
||||
conv, err := iconv.NewConverter(enc, "UTF-8")
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
defer conv.Close()
|
||||
|
||||
// Iterate over all byte values (128-255) and convert them to UTF-8
|
||||
for i := 128; i < 256; i++ {
|
||||
// Convert byte value to UTF-8 string
|
||||
in := []byte{byte(i)}
|
||||
out := make([]byte, 4)
|
||||
_, _, err := conv.Convert(in, out)
|
||||
|
||||
// This usually happens when the table location is empty
|
||||
if err != nil {
|
||||
out = []byte(" ")
|
||||
}
|
||||
|
||||
// Transform it into a rune
|
||||
char, _ := utf8.DecodeRune(out)
|
||||
tableNum, _ := strconv.Atoi(n)
|
||||
|
||||
// Create the new char
|
||||
newChar := PrinterChar{
|
||||
Table: tableNum,
|
||||
Hex: i,
|
||||
Utf: string(out),
|
||||
}
|
||||
|
||||
// If the char is already present we only attribute the current char
|
||||
// if the table num is lower
|
||||
_, ok := data[int(char)]
|
||||
if ok && data[int(char)].Table > tableNum {
|
||||
data[int(char)] = newChar
|
||||
}
|
||||
|
||||
// If its not present we just add it
|
||||
if !ok {
|
||||
data[int(char)] = newChar
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func loadCapabilities() Capabilites {
|
||||
file, err := os.ReadFile(CAPABILITES)
|
||||
if err != nil {
|
||||
fmt.Println("failed to open charmap: ", err)
|
||||
panic("")
|
||||
}
|
||||
|
||||
var c Capabilites
|
||||
err = json.Unmarshal([]byte(file), &c)
|
||||
if err != nil {
|
||||
fmt.Println("failed to unmarshal: ", err)
|
||||
panic("")
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
|
||||
iconv "github.com/djimenez/iconv-go"
|
||||
)
|
||||
|
||||
func PrintTable(table string) {
|
||||
defChar := " "
|
||||
|
||||
// Open a new iconv converter for CP037 to UTF-8
|
||||
conv, err := iconv.NewConverter(table, "UTF-8")
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer conv.Close()
|
||||
|
||||
// Iterate over all byte values (0-255) and convert them to UTF-8
|
||||
counter := 0
|
||||
for i := 128; i < 256; i++ {
|
||||
// Convert byte value to UTF-8 string
|
||||
|
||||
in := []byte{byte(i)}
|
||||
out := make([]byte, 4)
|
||||
_, _, err := conv.Convert(in, out)
|
||||
|
||||
if err != nil {
|
||||
out = []byte(defChar)
|
||||
}
|
||||
|
||||
// We have to transform out into a rune
|
||||
char, _ := utf8.DecodeRune(out)
|
||||
|
||||
fmt.Printf("[%c] ", char)
|
||||
counter++
|
||||
if counter == 16 {
|
||||
counter = 0
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ShowAll() {
|
||||
tables := buildTables()
|
||||
fmt.Println(tables)
|
||||
}
|
||||
Reference in New Issue
Block a user