Skip to content

Commit

Permalink
dictgen: Added Description func to ImageHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Feb 21, 2020
1 parent aa108b2 commit b403da2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
11 changes: 2 additions & 9 deletions cmd/dictgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,8 @@ func main() {
if e != nil {
fmt.Fprintf(os.Stderr, " Using encryption.\n")
}
switch v := ih.(type) {
case *dictgen.ImageHandlerBase64:
fmt.Fprintf(os.Stderr, " Using image method: optimize and encode as base64 data URL (max_width=%d, max_height=%d, grayscale=%t, jpeg_quality=%d).\n", v.MaxSize.X, v.MaxSize.Y, !v.NoGrayscale, v.JPEGQuality)
case *dictgen.ImageHandlerEmbed:
fmt.Fprintf(os.Stderr, " Using image method: add to dictzip as-is (warning: nickel is buggy with this as of firmware 4.19.14123).\n")
case *dictgen.ImageHandlerRemove:
fmt.Fprintf(os.Stderr, " Using image method: remove images.\n")
default:
fmt.Fprintf(os.Stderr, " Using image method: %#v.\n", v)
if ih != nil {
fmt.Fprintf(os.Stderr, " Using image method: %s.\n", ih.Description())
}
if err := tdf.WriteDictzip(dw, ih, dictgen.ImageFuncFilesystem); err != nil {
f.Close()
Expand Down
58 changes: 41 additions & 17 deletions dictgen/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type ImageHandler interface {
// addition, custom CSS (which must not contain any double quotes) can be
// returned to be set on the img tag.
Transform(src string, ir io.Reader, dw *kobodict.Writer) (nsrc string, css string, err error)

// Description returns a human-readable description of what the handler does.
Description() string
}

// ImageHandlerRemove removes images from the dicthtml.
Expand All @@ -55,6 +58,11 @@ func (*ImageHandlerRemove) Transform(string, io.Reader, *kobodict.Writer) (strin
return "", "", nil
}

// Description implements ImageHandler.
func (*ImageHandlerRemove) Description() string {
return "remove images"
}

// ImageHandlerEmbed adds the images to the dictzip without any additional
// modifications. Usually, this would be the best choice, but unfortunately,
// it is too buggy as of firmware 4.19.14123.
Expand All @@ -78,6 +86,11 @@ func (*ImageHandlerEmbed) Transform(src string, ir io.Reader, dw *kobodict.Write
return "dict:///" + fn, "", nil
}

// Description implements ImageHandler.
func (*ImageHandlerEmbed) Description() string {
return "add to dictzip as-is (warning: nickel is buggy with this as of firmware 4.19.14123)"
}

// ImageHandlerBase64 optimizes the image and encodes it as base64. This is the
// most compatible option, but it comes at the expense of space and speed. In
// addition, if there are too many images, it can lead to nickel running out of
Expand All @@ -100,37 +113,42 @@ type ImageHandlerBase64 struct {
JPEGQuality int
}

func (ih *ImageHandlerBase64) params() (maxWidth, maxHeight int, noGrayscale bool, jpegQuality int) {
mw, mh := float64(ih.MaxSize.X), float64(ih.MaxSize.Y)
if mw < 1 {
mw = 1000
}
if mh < 1 {
mh = 1000
}
ng := ih.NoGrayscale
jq := ih.JPEGQuality
if jq == 0 {
jq = 60
}
return int(mw), int(mh), ng, jq
}

// Transform implements ImageHandler.
func (ih *ImageHandlerBase64) Transform(src string, ir io.Reader, dw *kobodict.Writer) (string, string, error) {
mw, mh, ng, jq := ih.params()

// decode the image
img, err := imaging.Decode(ir)
if err != nil {
return "", "", fmt.Errorf("ImageHandlerBase64: decode image: %w", err)
}

// resize it
mw, mh := float64(ih.MaxSize.X), float64(ih.MaxSize.Y)
if mw < 1 {
mw = 1000
}
if mh < 1 {
mh = 1000
}
ow, oh := float64(img.Bounds().Dx()), float64(img.Bounds().Dy())
sf := math.Min(mw/ow, mh/oh)
nw, nh := ow*sf, oh*sf
img = imaging.Resize(img, int(nw), int(nh), imaging.Lanczos)
sf := math.Min(float64(mw)/ow, float64(mh)/oh)
img = imaging.Resize(img, int(ow*sf), int(oh*sf), imaging.Lanczos)

// make it grayscale
if !ih.NoGrayscale {
if ng {
img = imaging.Grayscale(img)
}

// set the quality
jq := ih.JPEGQuality
if jq == 0 {
jq = 60
}

// encode the image
buf := bytes.NewBuffer(nil)
bw := base64.NewEncoder(base64.StdEncoding, buf)
Expand All @@ -146,6 +164,12 @@ func (ih *ImageHandlerBase64) Transform(src string, ir io.Reader, dw *kobodict.W
return "data:image/jpeg;base64," + buf.String(), css, nil
}

// Description implements ImageHandler.
func (ih *ImageHandlerBase64) Description() string {
mw, mh, ng, jq := ih.params()
return fmt.Sprintf("optimize and encode as base64 data URL (max_width=%d, max_height=%d, grayscale=%t, jpeg_quality=%d)", mw, mh, ng, jq)
}

var imgTagRe = regexp.MustCompile(`(<img)(\s+(?:[^>]*\s+)?src\s*=\s*['"]+)([^'"]+)(['"][^>]*>)`)

// transformHTMLImages transforms img tags in the specified HTML, using
Expand Down

1 comment on commit b403da2

@pgaskin
Copy link
Owner Author

@pgaskin pgaskin commented on b403da2 Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to #1

Please sign in to comment.