add examples and test cases
This commit is contained in:
10
README.md
10
README.md
@@ -1,10 +1,10 @@
|
||||
# ZPLGFA Golang Package
|
||||
|
||||
[](https://godoc.org/github.com/SimonWaldherr/zplgfa)
|
||||
[](https://goreportcard.com/report/github.com/SimonWaldherr/zplgfa)
|
||||
[](https://codebeat.co/projects/github-com-simonwaldherr-zplgfa-master)
|
||||
[](https://bettercodehub.com/results/SimonWaldherr/zplgfa)
|
||||
[](https://raw.githubusercontent.com/SimonWaldherr/zplgfa/master/LICENSE)
|
||||
[](https://godoc.org/github.com/SimonWaldherr/zplgfa)
|
||||
[](https://goreportcard.com/report/github.com/SimonWaldherr/zplgfa)
|
||||
[](https://codebeat.co/projects/github-com-simonwaldherr-zplgfa-master)
|
||||
[](https://bettercodehub.com/results/SimonWaldherr/zplgfa)
|
||||
[](https://raw.githubusercontent.com/SimonWaldherr/zplgfa/master/LICENSE)
|
||||
|
||||
The ZPLGFA Golang package implements some functions to convert PNG, JPEG and GIF files to ZPL compatible ^GF-elements ([Graphic Fields](https://www.zebra.com/us/en/support-downloads/knowledge-articles/gf-graphic-field-zpl-command.html)).
|
||||
|
||||
|
||||
13
example_test.go
Normal file
13
example_test.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package zplgfa_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"simonwaldherr.de/go/zplgfa"
|
||||
)
|
||||
|
||||
func ExampleCompressASCII() {
|
||||
str := zplgfa.CompressASCII("FFFFFFFF000000")
|
||||
fmt.Print(str)
|
||||
|
||||
// Output: NFL0
|
||||
}
|
||||
@@ -87,7 +87,8 @@ func getRepeatCode(repeatCount int, char string) string {
|
||||
return repeatStr
|
||||
}
|
||||
|
||||
func compressASCII(in string) string {
|
||||
// CompressASCII compresses the ASCII data of a ZPL Graphic Field using RLE
|
||||
func CompressASCII(in string) string {
|
||||
in = strings.ToUpper(in)
|
||||
var curChar string
|
||||
var lastChar string
|
||||
@@ -112,7 +113,7 @@ func compressASCII(in string) string {
|
||||
curChar = string(in[i])
|
||||
}
|
||||
if lastChar != curChar {
|
||||
if i-lastCharSince > 8 {
|
||||
if i-lastCharSince > 4 {
|
||||
repCode = getRepeatCode(i-lastCharSince, lastChar)
|
||||
output += repCode
|
||||
} else {
|
||||
@@ -175,7 +176,7 @@ func ConvertToGraphicField(source image.Image, graphicType GraphicType) string {
|
||||
case ASCII:
|
||||
GraphicFieldData += fmt.Sprintln(hexstr)
|
||||
case CompressedASCII:
|
||||
curLine := compressASCII(hexstr)
|
||||
curLine := CompressASCII(hexstr)
|
||||
if lastLine == curLine {
|
||||
GraphicFieldData += ":"
|
||||
} else {
|
||||
@@ -186,7 +187,7 @@ func ConvertToGraphicField(source image.Image, graphicType GraphicType) string {
|
||||
GraphicFieldData += fmt.Sprintf("%s", line)
|
||||
default:
|
||||
graphicType = CompressedASCII
|
||||
GraphicFieldData += fmt.Sprintln(compressASCII(hexstr))
|
||||
GraphicFieldData += fmt.Sprintln(CompressASCII(hexstr))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
11
zplgfa_test.go
Normal file
11
zplgfa_test.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package zplgfa
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_CompressASCII(t *testing.T) {
|
||||
if str := CompressASCII("FFFFFFFF000000"); str != "NFL0" {
|
||||
t.Fatalf("CompressASCII failed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user