aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2019-04-13 00:33:45 +0200
committerPřemysl Janouch <p@janouch.name>2019-04-13 00:34:40 +0200
commitb287ba55007455571b2d17603914f4f3325eeb54 (patch)
tree744023f2412b9d198eb4e0635de9e9ebf07af0fe
parentfab0a52189baadd854d293ed076cdf7459d5b7f6 (diff)
downloadsklad-b287ba55007455571b2d17603914f4f3325eeb54.tar.gz
sklad-b287ba55007455571b2d17603914f4f3325eeb54.tar.xz
sklad-b287ba55007455571b2d17603914f4f3325eeb54.zip
Split out imgutil
-rw-r--r--imgutil/imgutil.go57
-rw-r--r--label-tool/main.go59
2 files changed, 60 insertions, 56 deletions
diff --git a/imgutil/imgutil.go b/imgutil/imgutil.go
new file mode 100644
index 0000000..bd9122b
--- /dev/null
+++ b/imgutil/imgutil.go
@@ -0,0 +1,57 @@
+package imgutil
+
+import (
+ "image"
+ "image/color"
+)
+
+// Scale is a scaling image.Image wrapper.
+type Scale struct {
+ Image image.Image
+ Scale int
+}
+
+// ColorModel implements image.Image.
+func (s *Scale) ColorModel() color.Model {
+ return s.Image.ColorModel()
+}
+
+// Bounds implements image.Image.
+func (s *Scale) Bounds() image.Rectangle {
+ r := s.Image.Bounds()
+ return image.Rect(r.Min.X*s.Scale, r.Min.Y*s.Scale,
+ r.Max.X*s.Scale, r.Max.Y*s.Scale)
+}
+
+// At implements image.Image.
+func (s *Scale) At(x, y int) color.Color {
+ if x < 0 {
+ x = x - s.Scale + 1
+ }
+ if y < 0 {
+ y = y - s.Scale + 1
+ }
+ return s.Image.At(x/s.Scale, y/s.Scale)
+}
+
+// LeftRotate is a 90 degree rotating image.Image wrapper.
+type LeftRotate struct {
+ Image image.Image
+}
+
+// ColorModel implements image.Image.
+func (lr *LeftRotate) ColorModel() color.Model {
+ return lr.Image.ColorModel()
+}
+
+// Bounds implements image.Image.
+func (lr *LeftRotate) Bounds() image.Rectangle {
+ r := lr.Image.Bounds()
+ // Min is inclusive, Max is exclusive.
+ return image.Rect(r.Min.Y, -(r.Max.X - 1), r.Max.Y, -(r.Min.X - 1))
+}
+
+// At implements image.Image.
+func (lr *LeftRotate) At(x, y int) color.Color {
+ return lr.Image.At(-y, x)
+}
diff --git a/label-tool/main.go b/label-tool/main.go
index 35c0667..a51a4c8 100644
--- a/label-tool/main.go
+++ b/label-tool/main.go
@@ -4,7 +4,6 @@ import (
"errors"
"html/template"
"image"
- "image/color"
"image/draw"
"image/png"
"log"
@@ -16,62 +15,10 @@ import (
"github.com/boombuler/barcode/qr"
"janouch.name/sklad/bdf"
+ "janouch.name/sklad/imgutil"
"janouch.name/sklad/ql"
)
-// scaler is a scaling image.Image wrapper.
-type scaler struct {
- image image.Image
- scale int
-}
-
-// ColorModel implements image.Image.
-func (s *scaler) ColorModel() color.Model {
- return s.image.ColorModel()
-}
-
-// Bounds implements image.Image.
-func (s *scaler) Bounds() image.Rectangle {
- r := s.image.Bounds()
- return image.Rect(r.Min.X*s.scale, r.Min.Y*s.scale,
- r.Max.X*s.scale, r.Max.Y*s.scale)
-}
-
-// At implements image.Image.
-func (s *scaler) At(x, y int) color.Color {
- if x < 0 {
- x = x - s.scale + 1
- }
- if y < 0 {
- y = y - s.scale + 1
- }
- return s.image.At(x/s.scale, y/s.scale)
-}
-
-// leftRotate is a 90 degree rotating image.Image wrapper.
-type leftRotate struct {
- image image.Image
-}
-
-// ColorModel implements image.Image.
-func (lr *leftRotate) ColorModel() color.Model {
- return lr.image.ColorModel()
-}
-
-// Bounds implements image.Image.
-func (lr *leftRotate) Bounds() image.Rectangle {
- r := lr.image.Bounds()
- // Min is inclusive, Max is exclusive.
- return image.Rect(r.Min.Y, -(r.Max.X - 1), r.Max.Y, -(r.Min.X - 1))
-}
-
-// At implements image.Image.
-func (lr *leftRotate) At(x, y int) color.Color {
- return lr.image.At(-y, x)
-}
-
-// -----------------------------------------------------------------------------
-
var font *bdf.Font
func genLabelForHeight(text string, height, scale int) image.Image {
@@ -81,7 +28,7 @@ func genLabelForHeight(text string, height, scale int) image.Image {
draw.Draw(textImg, textRect, image.White, image.ZP, draw.Src)
font.DrawString(textImg, image.ZP, text)
- scaledTextImg := scaler{image: textImg, scale: scale}
+ scaledTextImg := imgutil.Scale{Image: textImg, Scale: scale}
scaledTextRect := scaledTextImg.Bounds()
remains := height - scaledTextRect.Dy() - 20
@@ -238,7 +185,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
var label image.Image
if mediaInfo != nil {
- label = &leftRotate{image: genLabelForHeight(
+ label = &imgutil.LeftRotate{Image: genLabelForHeight(
params.Text, mediaInfo.PrintAreaPins, params.Scale)}
if r.FormValue("print") != "" {
if err := printer.Print(label); err != nil {