add examples and test cases
This commit is contained in:
10
README.md
10
README.md
@@ -1,10 +1,10 @@
|
|||||||
# ZPLGFA Golang Package
|
# ZPLGFA Golang Package
|
||||||
|
|
||||||
[](https://godoc.org/github.com/SimonWaldherr/zplgfa)
|
[](https://godoc.org/github.com/SimonWaldherr/zplgfa)
|
||||||
[](https://goreportcard.com/report/github.com/SimonWaldherr/zplgfa)
|
[](https://goreportcard.com/report/github.com/SimonWaldherr/zplgfa)
|
||||||
[](https://codebeat.co/projects/github-com-simonwaldherr-zplgfa-master)
|
[](https://codebeat.co/projects/github-com-simonwaldherr-zplgfa-master)
|
||||||
[](https://bettercodehub.com/results/SimonWaldherr/zplgfa)
|
[](https://bettercodehub.com/results/SimonWaldherr/zplgfa)
|
||||||
[](https://raw.githubusercontent.com/SimonWaldherr/zplgfa/master/LICENSE)
|
[](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)).
|
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
|
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)
|
in = strings.ToUpper(in)
|
||||||
var curChar string
|
var curChar string
|
||||||
var lastChar string
|
var lastChar string
|
||||||
@@ -112,7 +113,7 @@ func compressASCII(in string) string {
|
|||||||
curChar = string(in[i])
|
curChar = string(in[i])
|
||||||
}
|
}
|
||||||
if lastChar != curChar {
|
if lastChar != curChar {
|
||||||
if i-lastCharSince > 8 {
|
if i-lastCharSince > 4 {
|
||||||
repCode = getRepeatCode(i-lastCharSince, lastChar)
|
repCode = getRepeatCode(i-lastCharSince, lastChar)
|
||||||
output += repCode
|
output += repCode
|
||||||
} else {
|
} else {
|
||||||
@@ -175,7 +176,7 @@ func ConvertToGraphicField(source image.Image, graphicType GraphicType) string {
|
|||||||
case ASCII:
|
case ASCII:
|
||||||
GraphicFieldData += fmt.Sprintln(hexstr)
|
GraphicFieldData += fmt.Sprintln(hexstr)
|
||||||
case CompressedASCII:
|
case CompressedASCII:
|
||||||
curLine := compressASCII(hexstr)
|
curLine := CompressASCII(hexstr)
|
||||||
if lastLine == curLine {
|
if lastLine == curLine {
|
||||||
GraphicFieldData += ":"
|
GraphicFieldData += ":"
|
||||||
} else {
|
} else {
|
||||||
@@ -186,7 +187,7 @@ func ConvertToGraphicField(source image.Image, graphicType GraphicType) string {
|
|||||||
GraphicFieldData += fmt.Sprintf("%s", line)
|
GraphicFieldData += fmt.Sprintf("%s", line)
|
||||||
default:
|
default:
|
||||||
graphicType = CompressedASCII
|
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