add examples and test cases

This commit is contained in:
Simon Waldherr
2018-10-19 20:51:21 +02:00
parent ac541f60b5
commit 4cf804ae09
4 changed files with 34 additions and 9 deletions

View File

@@ -1,10 +1,10 @@
# ZPLGFA Golang Package
[![GoDoc](https://godoc.org/github.com/SimonWaldherr/zplgfa?status.svg)](https://godoc.org/github.com/SimonWaldherr/zplgfa)
[![Go Report Card](https://goreportcard.com/badge/github.com/SimonWaldherr/zplgfa)](https://goreportcard.com/report/github.com/SimonWaldherr/zplgfa)
[![codebeat badge](https://codebeat.co/badges/28d795af-6f9b-453a-94c2-4fafb8b5b0d5)](https://codebeat.co/projects/github-com-simonwaldherr-zplgfa-master)
[![BCH compliance](https://bettercodehub.com/edge/badge/SimonWaldherr/zplgfa?branch=master)](https://bettercodehub.com/results/SimonWaldherr/zplgfa)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/SimonWaldherr/zplgfa/master/LICENSE)
[![GoDoc](https://godoc.org/github.com/SimonWaldherr/zplgfa?status.svg)](https://godoc.org/github.com/SimonWaldherr/zplgfa)
[![Go Report Card](https://goreportcard.com/badge/github.com/SimonWaldherr/zplgfa)](https://goreportcard.com/report/github.com/SimonWaldherr/zplgfa)
[![codebeat badge](https://codebeat.co/badges/28d795af-6f9b-453a-94c2-4fafb8b5b0d5)](https://codebeat.co/projects/github-com-simonwaldherr-zplgfa-master)
[![BCH compliance](https://bettercodehub.com/edge/badge/SimonWaldherr/zplgfa?branch=master)](https://bettercodehub.com/results/SimonWaldherr/zplgfa)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](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
View 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
}

View File

@@ -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
View 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")
}
}