Skip to content

Commit

Permalink
Use font key, add linespacing, adjust defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbroup committed Dec 4, 2021
1 parent bf43996 commit 332ccea
Show file tree
Hide file tree
Showing 18 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions resources/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,9 @@ func TestImageOperationsGolden(t *testing.T) {
f.Hue(22),
f.Contrast(32.5),
f.Overlay(gopher.(images.ImageSource), 20, 30),
f.Text("No opions"),
f.Text("No options"),
f.Text("This long text is to test line breaks. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."),
f.Text("Hugo rocks!", map[string]interface{}{"x": 10, "y": 20, "size": 15, "color": "#ff0000"}),
f.Text("Hugo rocks!", map[string]interface{}{"x": 30, "y": 30, "size": 15, "color": "#ff0000"}),
}

resized, err := orig.Fill("400x200 center")
Expand Down
20 changes: 13 additions & 7 deletions resources/images/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ func (*Filters) Overlay(src ImageSource, x, y interface{}) gift.Filter {
// Text creates a filter that draws text with the given options.
func (*Filters) Text(text string, options ...interface{}) gift.Filter {
tf := textFilter{
text: text,
color: "#000000",
size: 20,
x: 1,
y: 1,
text: text,
color: "#ffffff",
size: 20,
x: 10,
y: 10,
linespacing: 2,
}

var opt map[string]interface{}
if len(options) > 0 {
opt, err := maps.ToStringMapE(options[0])
var err error
opt, err = maps.ToStringMapE(options[0])
if err != nil {
panic(err)
}
Expand All @@ -61,17 +64,20 @@ func (*Filters) Text(text string, options ...interface{}) gift.Filter {
tf.x = cast.ToInt(v)
case "y":
tf.y = cast.ToInt(v)
case "linespacing":
tf.y = cast.ToInt(v)
}

}

if w, ok := opt["font"].(fontSource); ok {
tf.font = w
opt["font"] = w.Key()
}
}

return filter{
Options: newFilterOpts(text, options),
Options: newFilterOpts(text, opt),
Filter: tf,
}
}
Expand Down
8 changes: 5 additions & 3 deletions resources/images/text.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Hugo Authors. All rights reserved.
// Copyright 2021 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,6 +35,7 @@ type textFilter struct {
text, color string
x, y int
size float64
linespacing int
font fontSource
}

Expand Down Expand Up @@ -93,11 +94,11 @@ func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options)
d.Dot = fixed.P(f.x, f.y)

// Draw text and break line at max width
parts := strings.Split(f.text, " ")
parts := strings.Fields(f.text)
for _, str := range parts {
strWith := font.MeasureString(face, str)
if (d.Dot.X.Ceil() + strWith.Ceil()) >= maxWidth {
y = y + fontHeight
y = y + fontHeight + f.linespacing
d.Dot = fixed.P(f.x, y)
}
d.DrawString(str + " ")
Expand All @@ -110,4 +111,5 @@ func (f textFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {

type fontSource interface {
ReadSeekCloser() (hugio.ReadSeekCloser, error)
Key() string
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

0 comments on commit 332ccea

Please sign in to comment.