mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
@@ -15,7 +15,11 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hugio"
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
|
||||
"github.com/disintegration/gift"
|
||||
"github.com/spf13/cast"
|
||||
@@ -61,6 +65,21 @@ func (*Filters) Text(text string, options ...interface{}) gift.Filter {
|
||||
tf.y = cast.ToInt(v)
|
||||
case "linespacing":
|
||||
tf.linespacing = cast.ToInt(v)
|
||||
case "font":
|
||||
fontSource, ok1 := v.(hugio.ReadSeekCloserProvider)
|
||||
identifier, ok2 := v.(resource.Identifier)
|
||||
|
||||
if !(ok1 && ok2) {
|
||||
panic(fmt.Sprintf("invalid text font source: %T", v))
|
||||
}
|
||||
|
||||
tf.fontSource = fontSource
|
||||
|
||||
// The input value isn't hashable and will not make a stable key.
|
||||
// Replace it with a string in the map used as basis for the
|
||||
// hash string.
|
||||
opt["font"] = identifier.Key()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -16,9 +16,11 @@ package images
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/disintegration/gift"
|
||||
"github.com/gohugoio/hugo/common/hugio"
|
||||
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
@@ -33,6 +35,7 @@ type textFilter struct {
|
||||
x, y int
|
||||
size float64
|
||||
linespacing int
|
||||
fontSource hugio.ReadSeekCloserProvider
|
||||
}
|
||||
|
||||
func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
|
||||
@@ -43,6 +46,17 @@ func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options)
|
||||
|
||||
// Load and parse font
|
||||
ttf := goregular.TTF
|
||||
if f.fontSource != nil {
|
||||
rs, err := f.fontSource.ReadSeekCloser()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer rs.Close()
|
||||
ttf, err = io.ReadAll(rs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
otf, err := opentype.Parse(ttf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
Reference in New Issue
Block a user