From 4cf804ae099740139e9b44f680a87f668ef800a9 Mon Sep 17 00:00:00 2001 From: Simon Waldherr Date: Fri, 19 Oct 2018 20:51:21 +0200 Subject: [PATCH] add examples and test cases --- README.md | 10 +++++----- example_test.go | 13 +++++++++++++ zplgfa.go | 9 +++++---- zplgfa_test.go | 11 +++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 example_test.go create mode 100644 zplgfa_test.go diff --git a/README.md b/README.md index e0eef58..3b81789 100644 --- a/README.md +++ b/README.md @@ -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)). diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..a925e6c --- /dev/null +++ b/example_test.go @@ -0,0 +1,13 @@ +package zplgfa_test + +import ( + "fmt" + "simonwaldherr.de/go/zplgfa" +) + +func ExampleCompressASCII() { + str := zplgfa.CompressASCII("FFFFFFFF000000") + fmt.Print(str) + + // Output: NFL0 +} diff --git a/zplgfa.go b/zplgfa.go index f791224..d728a3f 100644 --- a/zplgfa.go +++ b/zplgfa.go @@ -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)) } } diff --git a/zplgfa_test.go b/zplgfa_test.go new file mode 100644 index 0000000..bde8965 --- /dev/null +++ b/zplgfa_test.go @@ -0,0 +1,11 @@ +package zplgfa + +import ( + "testing" +) + +func Test_CompressASCII(t *testing.T) { + if str := CompressASCII("FFFFFFFF000000"); str != "NFL0" { + t.Fatalf("CompressASCII failed") + } +}