images: Text filter that draws text with the given options (#9239)

Fixes #9238
This commit is contained in:
Paul van Brouwershaven
2021-12-07 11:29:55 +01:00
committed by GitHub
parent 5538507e90
commit 283394a4fd
15 changed files with 160 additions and 2 deletions

View File

@@ -674,6 +674,9 @@ func TestImageOperationsGolden(t *testing.T) {
f.Hue(22),
f.Contrast(32.5),
f.Overlay(gopher.(images.ImageSource), 20, 30),
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": 30, "y": 30, "size": 15, "color": "#ff0000"}),
}
resized, err := orig.Fill("400x200 center")

View File

@@ -15,6 +15,8 @@
package images
import (
"github.com/gohugoio/hugo/common/maps"
"github.com/disintegration/gift"
"github.com/spf13/cast"
)
@@ -33,6 +35,43 @@ 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: "#ffffff",
size: 20,
x: 10,
y: 10,
linespacing: 2,
}
var opt map[string]interface{}
if len(options) > 0 {
opt := maps.MustToParamsAndPrepare(options[0])
for option, v := range opt {
switch option {
case "color":
tf.color = cast.ToString(v)
case "size":
tf.size = cast.ToFloat64(v)
case "x":
tf.x = cast.ToInt(v)
case "y":
tf.y = cast.ToInt(v)
case "linespacing":
tf.linespacing = cast.ToInt(v)
}
}
}
return filter{
Options: newFilterOpts(text, opt),
Filter: tf,
}
}
// Brightness creates a filter that changes the brightness of an image.
// The percentage parameter must be in range (-100, 100).
func (*Filters) Brightness(percentage interface{}) gift.Filter {

94
resources/images/text.go Normal file
View File

@@ -0,0 +1,94 @@
// 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.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package images
import (
"image"
"image/draw"
"strings"
"github.com/disintegration/gift"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/opentype"
"golang.org/x/image/math/fixed"
)
var _ gift.Filter = (*textFilter)(nil)
type textFilter struct {
text, color string
x, y int
size float64
linespacing int
}
func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
color, err := hexStringToColor(f.color)
if err != nil {
panic(err)
}
// Load and parse font
ttf := goregular.TTF
otf, err := opentype.Parse(ttf)
if err != nil {
panic(err)
}
// Set font options
face, err := opentype.NewFace(otf, &opentype.FaceOptions{
Size: f.size,
DPI: 72,
Hinting: font.HintingNone,
})
if err != nil {
panic(err)
}
d := font.Drawer{
Dst: dst,
Src: image.NewUniform(color),
Face: face,
}
gift.New().Draw(dst, src)
// Draw text, consider and include linebreaks
maxWidth := dst.Bounds().Dx() - 20
fontHeight := face.Metrics().Ascent.Ceil()
// Correct y position based on font and size
f.y = f.y + fontHeight
// Start position
y := f.y
d.Dot = fixed.P(f.x, f.y)
// Draw text and break line at max width
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 + f.linespacing
d.Dot = fixed.P(f.x, y)
}
d.DrawString(str + " ")
}
}
func (f textFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
return image.Rect(0, 0, srcBounds.Dx(), srcBounds.Dy())
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB