mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-18 14:54:24 +01:00
use go-text/typesetting library to render text better: rendering farsi.
This commit is contained in:
BIN
fonts/NotoEmoji.ttf
Normal file
BIN
fonts/NotoEmoji.ttf
Normal file
Binary file not shown.
7
go.mod
7
go.mod
@@ -41,6 +41,8 @@ require (
|
|||||||
github.com/dgraph-io/ristretto v0.1.1 // indirect
|
github.com/dgraph-io/ristretto v0.1.1 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/fasthttp/websocket v1.5.3 // indirect
|
github.com/fasthttp/websocket v1.5.3 // indirect
|
||||||
|
github.com/go-text/render v0.0.0-20230619120952-35bccb6164b8 // indirect
|
||||||
|
github.com/go-text/typesetting v0.0.0-20231221124458-48cc05a56658 // indirect
|
||||||
github.com/gobwas/httphead v0.1.0 // indirect
|
github.com/gobwas/httphead v0.1.0 // indirect
|
||||||
github.com/gobwas/pool v0.2.1 // indirect
|
github.com/gobwas/pool v0.2.1 // indirect
|
||||||
github.com/gobwas/ws v1.3.1 // indirect
|
github.com/gobwas/ws v1.3.1 // indirect
|
||||||
@@ -56,11 +58,15 @@ require (
|
|||||||
github.com/mailru/easyjson v0.7.7 // indirect
|
github.com/mailru/easyjson v0.7.7 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
|
github.com/pemistahl/lingua-go v1.4.0 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/puzpuzpuz/xsync/v2 v2.5.1 // indirect
|
github.com/puzpuzpuz/xsync/v2 v2.5.1 // indirect
|
||||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
|
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
|
||||||
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a // indirect
|
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a // indirect
|
||||||
|
github.com/shopspring/decimal v1.3.1 // indirect
|
||||||
|
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirect
|
||||||
|
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect
|
||||||
github.com/tidwall/gjson v1.17.0 // indirect
|
github.com/tidwall/gjson v1.17.0 // indirect
|
||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
github.com/tidwall/pretty v1.2.1 // indirect
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
@@ -69,6 +75,7 @@ require (
|
|||||||
go.opencensus.io v0.24.0 // indirect
|
go.opencensus.io v0.24.0 // indirect
|
||||||
golang.org/x/net v0.17.0 // indirect
|
golang.org/x/net v0.17.0 // indirect
|
||||||
golang.org/x/sys v0.14.0 // indirect
|
golang.org/x/sys v0.14.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
google.golang.org/protobuf v1.31.0 // indirect
|
google.golang.org/protobuf v1.31.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
16
go.sum
16
go.sum
@@ -76,6 +76,12 @@ github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
|||||||
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
||||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||||
|
github.com/go-text/render v0.0.0-20230619120952-35bccb6164b8 h1:VkKnvzbvHqgEfm351rfr8Uclu5fnwq8HP2ximUzJsBM=
|
||||||
|
github.com/go-text/render v0.0.0-20230619120952-35bccb6164b8/go.mod h1:h29xCucjNsDcYb7+0rJokxVwYAq+9kQ19WiFuBKkYtc=
|
||||||
|
github.com/go-text/typesetting v0.0.0-20230616162802-9c17dd34aa4a h1:VjN8ttdfklC0dnAdKbZqGNESdERUxtE3l8a/4Grgarc=
|
||||||
|
github.com/go-text/typesetting v0.0.0-20230616162802-9c17dd34aa4a/go.mod h1:evDBbvNR/KaVFZ2ZlDSOWWXIUKq0wCOEtzLxRM8SG3k=
|
||||||
|
github.com/go-text/typesetting v0.0.0-20231221124458-48cc05a56658 h1:KeDKnC99J3l5qJK4zV13Au2UwPn4N20TnIlM0YvILj8=
|
||||||
|
github.com/go-text/typesetting v0.0.0-20231221124458-48cc05a56658/go.mod h1:d22AnmeKq/on0HNv73UFriMKc4Ez6EqZAofLhAzpSzI=
|
||||||
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
||||||
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
|
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
|
||||||
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
||||||
@@ -167,6 +173,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
|
|||||||
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
||||||
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||||
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||||
|
github.com/pemistahl/lingua-go v1.4.0 h1:ifYhthrlW7iO4icdubwlduYnmwU37V1sbNrwhKBR4rM=
|
||||||
|
github.com/pemistahl/lingua-go v1.4.0/go.mod h1:ECuM1Hp/3hvyh7k8aWSqNCPlTxLemFZsRjocUf3KgME=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
@@ -183,6 +191,12 @@ github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1Avp
|
|||||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
|
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
|
||||||
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a h1:iLcLb5Fwwz7g/DLK89F+uQBDeAhHhwdzB5fSlVdhGcM=
|
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a h1:iLcLb5Fwwz7g/DLK89F+uQBDeAhHhwdzB5fSlVdhGcM=
|
||||||
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a/go.mod h1:wozgYq9WEBQBaIJe4YZ0qTSFAMxmcwBhQH0fO0R34Z0=
|
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a/go.mod h1:wozgYq9WEBQBaIJe4YZ0qTSFAMxmcwBhQH0fO0R34Z0=
|
||||||
|
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
|
||||||
|
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||||
|
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c h1:km8GpoQut05eY3GiYWEedbTT0qnSxrCjsVbb7yKY1KE=
|
||||||
|
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q=
|
||||||
|
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef h1:Ch6Q+AZUxDBCVqdkI8FSpFyZDtCVBc2VmejdNrm5rRQ=
|
||||||
|
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef/go.mod h1:nXTWP6+gD5+LUJ8krVhhoeHjvHTutPxMYl5SvkcnJNE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
@@ -271,6 +285,8 @@ golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -74,6 +74,10 @@ func main() {
|
|||||||
tailwindDebugStuff = template.HTML(fmt.Sprintf("<script src=\"https://cdn.tailwindcss.com?plugins=typography\"></script><script>\n%s</script><style type=\"text/tailwindcss\">%s</style>", config, style))
|
tailwindDebugStuff = template.HTML(fmt.Sprintf("<script src=\"https://cdn.tailwindcss.com?plugins=typography\"></script><script>\n%s</script><style type=\"text/tailwindcss\">%s</style>", config, style))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// image rendering stuff
|
||||||
|
initializeImageDrawingStuff()
|
||||||
|
|
||||||
|
// eventstore and internal db
|
||||||
deinitCache := initCache()
|
deinitCache := initCache()
|
||||||
defer deinitCache()
|
defer deinitCache()
|
||||||
|
|
||||||
|
|||||||
406
render_image.go
406
render_image.go
@@ -2,34 +2,33 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/draw"
|
|
||||||
"image/png"
|
"image/png"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
|
||||||
|
|
||||||
"github.com/apatters/go-wordwrap"
|
|
||||||
"github.com/fogleman/gg"
|
"github.com/fogleman/gg"
|
||||||
|
"github.com/go-text/render"
|
||||||
|
"github.com/go-text/typesetting/di"
|
||||||
|
"github.com/go-text/typesetting/font"
|
||||||
|
"github.com/go-text/typesetting/fontscan"
|
||||||
|
"github.com/go-text/typesetting/language"
|
||||||
|
"github.com/go-text/typesetting/opentype/api/metadata"
|
||||||
|
"github.com/go-text/typesetting/shaping"
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
sdk "github.com/nbd-wtf/nostr-sdk"
|
sdk "github.com/nbd-wtf/nostr-sdk"
|
||||||
"github.com/nfnt/resize"
|
"github.com/nfnt/resize"
|
||||||
"golang.org/x/image/font"
|
"github.com/pemistahl/lingua-go"
|
||||||
|
xfont "golang.org/x/image/font"
|
||||||
|
"golang.org/x/image/math/fixed"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MAX_LINES = 20
|
|
||||||
MAX_CHARS_PER_LINE = 50
|
|
||||||
MAX_CHARS_PER_QUOTE_LINE = 46
|
|
||||||
FONT_SIZE = 7
|
|
||||||
FONT_DPI = 260
|
|
||||||
|
|
||||||
BLOCK = "|"
|
BLOCK = "|"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,11 +36,53 @@ var (
|
|||||||
BACKGROUND = color.RGBA{23, 23, 23, 255}
|
BACKGROUND = color.RGBA{23, 23, 23, 255}
|
||||||
BAR_BACKGROUND = color.RGBA{10, 10, 10, 255}
|
BAR_BACKGROUND = color.RGBA{10, 10, 10, 255}
|
||||||
FOREGROUND = color.RGBA{255, 230, 238, 255}
|
FOREGROUND = color.RGBA{255, 230, 238, 255}
|
||||||
|
|
||||||
|
fontmap *fontscan.FontMap
|
||||||
|
detector lingua.LanguageDetector
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed fonts/*
|
//go:embed fonts/*
|
||||||
var fonts embed.FS
|
var fonts embed.FS
|
||||||
|
|
||||||
|
func initializeImageDrawingStuff() error {
|
||||||
|
// language detector
|
||||||
|
detector = lingua.NewLanguageDetectorBuilder().FromLanguages(
|
||||||
|
lingua.Japanese,
|
||||||
|
lingua.Persian,
|
||||||
|
lingua.Chinese,
|
||||||
|
lingua.Thai,
|
||||||
|
lingua.Hebrew,
|
||||||
|
).WithLowAccuracyMode().Build()
|
||||||
|
|
||||||
|
// fonts
|
||||||
|
dir, err := fonts.ReadDir("fonts")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error reading fonts/ dir: %w", err)
|
||||||
|
}
|
||||||
|
fontmap = fontscan.NewFontMap(nil)
|
||||||
|
for _, entry := range dir {
|
||||||
|
filepath := "fonts/" + entry.Name()
|
||||||
|
fontData, err := fonts.ReadFile(filepath)
|
||||||
|
face, err := font.ParseTTF(bytes.NewReader(fontData))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error loading font %s: %w", filepath, err)
|
||||||
|
}
|
||||||
|
fontmap.AddFace(face,
|
||||||
|
fontscan.Location{File: filepath},
|
||||||
|
metadata.Description{
|
||||||
|
Family: "Noto",
|
||||||
|
Aspect: metadata.Aspect{
|
||||||
|
Style: metadata.StyleNormal,
|
||||||
|
Weight: metadata.WeightNormal,
|
||||||
|
Stretch: metadata.StretchNormal,
|
||||||
|
},
|
||||||
|
IsMonospace: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func renderImage(w http.ResponseWriter, r *http.Request) {
|
func renderImage(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
|
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
|
||||||
|
|
||||||
@@ -57,27 +98,17 @@ func renderImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the font and language specifics based on the characters used
|
|
||||||
font, breakWords, err := getLanguage(data.event.Content)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "error getting font: "+err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
content := strings.Replace(data.event.Content, "\n\n\n\n", "\n\n", -1)
|
content := strings.Replace(data.event.Content, "\n\n\n\n", "\n\n", -1)
|
||||||
content = strings.Replace(data.event.Content, "\n\n\n", "\n\n", -1)
|
content = strings.Replace(data.event.Content, "\n\n\n", "\n\n", -1)
|
||||||
|
|
||||||
// this turns the raw event.Content into a series of lines ready to drawn
|
// this turns the raw event.Content into a series of lines ready to drawn
|
||||||
lines := normalizeText(
|
paragraphs := replaceUserReferencesWithNames(r.Context(),
|
||||||
replaceUserReferencesWithNames(r.Context(),
|
quotesAsBlockPrefixedText(r.Context(),
|
||||||
renderQuotesAsBlockPrefixedText(r.Context(),
|
|
||||||
content,
|
content,
|
||||||
),
|
),
|
||||||
),
|
|
||||||
breakWords,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
img, err := drawImage(lines, font, getPreviewStyle(r), data.metadata, data.createdAt)
|
img, err := drawImage(paragraphs, getPreviewStyle(r), data.metadata, data.createdAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error writing image: %s", err)
|
log.Printf("error writing image: %s", err)
|
||||||
http.Error(w, "error writing image!", 500)
|
http.Error(w, "error writing image!", 500)
|
||||||
@@ -94,128 +125,7 @@ func renderImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeText(input []string, breakWords bool) []string {
|
func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, date string) (image.Image, error) {
|
||||||
lines := make([]string, 0, MAX_LINES)
|
|
||||||
l := 0 // global line counter
|
|
||||||
|
|
||||||
for _, block := range input {
|
|
||||||
block := strings.TrimRight(block, "\n")
|
|
||||||
quoting := false
|
|
||||||
maxChars := MAX_CHARS_PER_LINE
|
|
||||||
if strings.HasPrefix(block, BLOCK+" ") {
|
|
||||||
quoting = true
|
|
||||||
maxChars = MAX_CHARS_PER_QUOTE_LINE // on quote lines we tolerate less characters
|
|
||||||
block = block[len(BLOCK)+1:]
|
|
||||||
lines = append(lines, "") // add an empty line before each quote
|
|
||||||
l++
|
|
||||||
}
|
|
||||||
for _, line := range strings.Split(block, "\n") {
|
|
||||||
if l == MAX_LINES {
|
|
||||||
// escape and return here if we're over max lines
|
|
||||||
return lines
|
|
||||||
}
|
|
||||||
|
|
||||||
// turn a single line into multiple if it is long enough -- carefully splitting on word ends
|
|
||||||
wrappedLines := strings.Split(wordwrap.Wrap(maxChars, strings.TrimSpace(line)), "\n")
|
|
||||||
|
|
||||||
// now we go over all these lines and further split them if necessary
|
|
||||||
// in japanese, for example, we must break the words otherwise nothing works
|
|
||||||
var sublines []string
|
|
||||||
if breakWords {
|
|
||||||
sublines = make([]string, 0, len(wrappedLines))
|
|
||||||
for _, wline := range wrappedLines {
|
|
||||||
// split until we have a bunch of lines all under maxChars
|
|
||||||
for {
|
|
||||||
if len(wline) > maxChars {
|
|
||||||
// we can't split exactly at maxChars because that would break utf-8 runes
|
|
||||||
// so we do this range mess to try to grab where the last rune in the line ends
|
|
||||||
subline := make([]rune, 0, maxChars)
|
|
||||||
var i int
|
|
||||||
var r rune
|
|
||||||
for i, r = range wline {
|
|
||||||
if i > maxChars {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
subline = append(subline, r)
|
|
||||||
}
|
|
||||||
sublines = append(sublines, string(subline))
|
|
||||||
wline = wline[i:]
|
|
||||||
} else {
|
|
||||||
sublines = append(sublines, wline)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sublines = wrappedLines
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, subline := range sublines {
|
|
||||||
// if a line has a word so big that it would overflow (like a nevent), hide it with an ellipsis
|
|
||||||
if len([]rune(subline)) > maxChars {
|
|
||||||
subline = subline[0:maxChars-1] + "…"
|
|
||||||
}
|
|
||||||
|
|
||||||
if quoting {
|
|
||||||
subline = BLOCK + " " + subline
|
|
||||||
}
|
|
||||||
|
|
||||||
lines = append(lines, subline)
|
|
||||||
l++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lines
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchImageFromURL(url string) (image.Image, error) {
|
|
||||||
response, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer response.Body.Close()
|
|
||||||
|
|
||||||
img, _, err := image.Decode(response.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return img, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func roundImage(img image.Image) image.Image {
|
|
||||||
bounds := img.Bounds()
|
|
||||||
diameter := math.Min(float64(bounds.Dx()), float64(bounds.Dy()))
|
|
||||||
radius := diameter / 2
|
|
||||||
|
|
||||||
// Create a new context for the mask
|
|
||||||
mask := gg.NewContext(bounds.Dx(), bounds.Dy())
|
|
||||||
mask.SetColor(color.Black) // Set the mask color to fully opaque
|
|
||||||
mask.DrawCircle(float64(bounds.Dx())/2, float64(bounds.Dy())/2, radius)
|
|
||||||
mask.ClosePath()
|
|
||||||
mask.Fill()
|
|
||||||
|
|
||||||
// Apply the circular mask to the original image
|
|
||||||
result := image.NewRGBA(bounds)
|
|
||||||
maskImg := mask.Image()
|
|
||||||
draw.DrawMask(result, bounds, img, image.Point{}, maskImg, image.Point{}, draw.Over)
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func cropToSquare(img image.Image) image.Image {
|
|
||||||
bounds := img.Bounds()
|
|
||||||
size := int(math.Min(float64(bounds.Dx()), float64(bounds.Dy())))
|
|
||||||
squareImg := image.NewRGBA(image.Rect(0, 0, size, size))
|
|
||||||
for x := 0; x < size; x++ {
|
|
||||||
for y := 0; y < size; y++ {
|
|
||||||
squareImg.Set(x, y, img.At(x+(bounds.Dx()-size)/2, y+(bounds.Dy()-size)/2))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return squareImg
|
|
||||||
}
|
|
||||||
|
|
||||||
func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.ProfileMetadata, date string) (image.Image, error) {
|
|
||||||
width := 700
|
width := 700
|
||||||
height := 525
|
height := 525
|
||||||
paddingLeft := 25
|
paddingLeft := 25
|
||||||
@@ -233,27 +143,27 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
|||||||
img.SetColor(BACKGROUND)
|
img.SetColor(BACKGROUND)
|
||||||
img.Clear()
|
img.Clear()
|
||||||
img.SetColor(FOREGROUND)
|
img.SetColor(FOREGROUND)
|
||||||
|
|
||||||
|
// main content text
|
||||||
|
textImg := drawText(paragraphs, width-4*2, height-2*2)
|
||||||
|
img.DrawImage(textImg, 4, 2)
|
||||||
|
|
||||||
|
// font for writing the bottom bar stuff
|
||||||
|
fontData, _ := fonts.ReadFile("fonts/NotoSans.ttf")
|
||||||
|
ttf, _ := truetype.Parse(fontData)
|
||||||
img.SetFontFace(truetype.NewFace(ttf, &truetype.Options{
|
img.SetFontFace(truetype.NewFace(ttf, &truetype.Options{
|
||||||
Size: FONT_SIZE,
|
Size: 6,
|
||||||
DPI: FONT_DPI,
|
DPI: 260,
|
||||||
Hinting: font.HintingFull,
|
Hinting: xfont.HintingFull,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Draw note text
|
// black bar at the bottom
|
||||||
lineSpacing := 0.3
|
|
||||||
lineHeight := float64(FONT_SIZE)*FONT_DPI/72.0 + float64(FONT_SIZE)*lineSpacing*FONT_DPI/72.0
|
|
||||||
for i, line := range lines {
|
|
||||||
y := float64(i)*lineHeight + 50 // Calculate the Y position for each line
|
|
||||||
img.DrawString(line, float64(paddingLeft), y) // Draw the line at the calculated Y position
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw black bar at the bottom
|
|
||||||
barHeight := 70
|
barHeight := 70
|
||||||
img.SetColor(BAR_BACKGROUND)
|
img.SetColor(BAR_BACKGROUND)
|
||||||
img.DrawRectangle(0, float64(height-barHeight), float64(width), float64(barHeight))
|
img.DrawRectangle(0, float64(height-barHeight), float64(width), float64(barHeight))
|
||||||
img.Fill()
|
img.Fill()
|
||||||
|
|
||||||
// Create a rectangle at the bottom with a gradient from black to transparent
|
// a rectangle at the bottom with a gradient from black to transparent
|
||||||
gradientRectHeight := 140
|
gradientRectHeight := 140
|
||||||
gradientRectY := height - barHeight - gradientRectHeight
|
gradientRectY := height - barHeight - gradientRectHeight
|
||||||
for y := 0; y < gradientRectHeight; y++ {
|
for y := 0; y < gradientRectHeight; y++ {
|
||||||
@@ -263,7 +173,7 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
|||||||
img.Fill()
|
img.Fill()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw author's image from URL
|
// draw author's name
|
||||||
authorTextX := paddingLeft + barExtraPadding
|
authorTextX := paddingLeft + barExtraPadding
|
||||||
if metadata.Picture != "" {
|
if metadata.Picture != "" {
|
||||||
authorImage, err := fetchImageFromURL(metadata.Picture)
|
authorImage, err := fetchImageFromURL(metadata.Picture)
|
||||||
@@ -273,14 +183,12 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
|||||||
authorTextX += 65
|
authorTextX += 65
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw author's name
|
|
||||||
authorTextY := height - barHeight + 20
|
authorTextY := height - barHeight + 20
|
||||||
authorMaxWidth := width/2.0 - paddingLeft*2 - barExtraPadding
|
authorMaxWidth := width/2.0 - paddingLeft*2 - barExtraPadding
|
||||||
img.SetColor(color.White)
|
img.SetColor(color.White)
|
||||||
img.DrawStringWrapped(metadata.ShortName(), float64(authorTextX), float64(authorTextY), 0, 0, float64(width*99), 99, gg.AlignLeft)
|
img.DrawStringWrapped(metadata.ShortName(), float64(authorTextX), float64(authorTextY), 0, 0, float64(width*99), 99, gg.AlignLeft)
|
||||||
|
|
||||||
// Create a gradient to cover too long names
|
// a gradient to cover too long names
|
||||||
img.SetColor(BAR_BACKGROUND)
|
img.SetColor(BAR_BACKGROUND)
|
||||||
img.DrawRectangle(float64(authorTextX+authorMaxWidth), float64(height-barHeight), float64(width-authorTextX-authorMaxWidth), float64(barHeight))
|
img.DrawRectangle(float64(authorTextX+authorMaxWidth), float64(height-barHeight), float64(width-authorTextX-authorMaxWidth), float64(barHeight))
|
||||||
gradientLenght := 60
|
gradientLenght := 60
|
||||||
@@ -291,7 +199,7 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
|||||||
img.Fill()
|
img.Fill()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the logo
|
// bottom bar logo
|
||||||
logo, _ := static.ReadFile("static/logo.png")
|
logo, _ := static.ReadFile("static/logo.png")
|
||||||
stampImg, _ := png.Decode(bytes.NewBuffer(logo))
|
stampImg, _ := png.Decode(bytes.NewBuffer(logo))
|
||||||
stampWidth := stampImg.Bounds().Dx()
|
stampWidth := stampImg.Bounds().Dx()
|
||||||
@@ -304,145 +212,57 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
|||||||
layout := "2006-01-02 15:04:05"
|
layout := "2006-01-02 15:04:05"
|
||||||
parsedTime, _ := time.Parse(layout, date)
|
parsedTime, _ := time.Parse(layout, date)
|
||||||
formattedDate := parsedTime.Format("Jan 02, 2006")
|
formattedDate := parsedTime.Format("Jan 02, 2006")
|
||||||
img.SetFontFace(truetype.NewFace(ttf, &truetype.Options{
|
|
||||||
Size: FONT_SIZE - 1,
|
|
||||||
DPI: FONT_DPI,
|
|
||||||
Hinting: font.HintingFull,
|
|
||||||
}))
|
|
||||||
img.SetColor(color.RGBA{160, 160, 160, 255})
|
img.SetColor(color.RGBA{160, 160, 160, 255})
|
||||||
img.DrawStringWrapped(formattedDate, float64(width-paddingLeft-stampWidth-260), float64(authorTextY+3), 0, 0, float64(240), 1.5, gg.AlignRight)
|
img.DrawStringWrapped(formattedDate, float64(width-paddingLeft-stampWidth-260), float64(authorTextY+3), 0, 0, float64(240), 1.5, gg.AlignRight)
|
||||||
|
|
||||||
return img.Image(), nil
|
return img.Image(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace nevent and note with their text, as an extra line prefixed by BLOCK
|
func drawText(paragraphs []string, width, height int) image.Image {
|
||||||
// this returns a slice of lines
|
const FONT_SIZE = 26
|
||||||
func renderQuotesAsBlockPrefixedText(ctx context.Context, input string) []string {
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
blocks := make([]string, 0, 8)
|
r := &render.Renderer{
|
||||||
matches := nostrNoteNeventMatcher.FindAllStringSubmatchIndex(input, -1)
|
PixScale: 1,
|
||||||
|
FontSize: FONT_SIZE,
|
||||||
if len(matches) == 0 {
|
Color: color.White,
|
||||||
// no matches, just return text as it is
|
|
||||||
blocks = append(blocks, input)
|
|
||||||
return blocks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// one or more matches, return multiple lines
|
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||||
blocks = append(blocks, input[0:matches[0][0]])
|
|
||||||
i := -1 // matches iteration counter
|
i := 1
|
||||||
b := 0 // current block index
|
for _, paragraph := range paragraphs {
|
||||||
for _, match := range matches {
|
text := []rune(paragraph)
|
||||||
|
|
||||||
|
detectedLanguage, ok := detector.DetectLanguageOf(paragraph)
|
||||||
|
var lang language.Language
|
||||||
|
if ok {
|
||||||
|
lang = language.NewLanguage(detectedLanguage.IsoCode639_1().String())
|
||||||
|
}
|
||||||
|
|
||||||
|
shaper := &shaping.HarfbuzzShaper{}
|
||||||
|
|
||||||
|
face := fontmap.ResolveFace(text[0])
|
||||||
|
shapedRunes := shaper.Shape(shaping.Input{
|
||||||
|
Text: []rune(text),
|
||||||
|
RunStart: 0,
|
||||||
|
RunEnd: len([]rune(text)),
|
||||||
|
Face: face,
|
||||||
|
Size: fixed.I(int(r.FontSize)),
|
||||||
|
Script: language.LookupScript([]rune(text)[0]),
|
||||||
|
Language: lang,
|
||||||
|
Direction: di.DirectionRTL,
|
||||||
|
})
|
||||||
|
|
||||||
|
var wrapper shaping.LineWrapper
|
||||||
|
lines, _ := wrapper.WrapParagraph(shaping.WrapConfig{}, width, []rune(text), shaping.NewSliceIterator([]shaping.Output{shapedRunes}))
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
for _, out := range line {
|
||||||
|
r.DrawShapedRunAt(out, img, 0, FONT_SIZE*i)
|
||||||
i++
|
i++
|
||||||
|
}
|
||||||
matchText := input[match[0]:match[1]]
|
}
|
||||||
submatch := nostrNoteNeventMatcher.FindStringSubmatch(matchText)
|
|
||||||
nip19 := submatch[0][6:]
|
|
||||||
|
|
||||||
event, _, err := getEvent(ctx, nip19, nil)
|
|
||||||
if err != nil {
|
|
||||||
// error case concat this to previous block
|
|
||||||
blocks[b] += matchText
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add a new block with the quoted text
|
return img
|
||||||
blocks = append(blocks, BLOCK+" "+event.Content)
|
|
||||||
|
|
||||||
// increase block count
|
|
||||||
b++
|
|
||||||
}
|
|
||||||
// add remaining text after the last match
|
|
||||||
remainingText := input[matches[i][1]:]
|
|
||||||
if strings.TrimSpace(remainingText) != "" {
|
|
||||||
blocks = append(blocks, remainingText)
|
|
||||||
}
|
|
||||||
|
|
||||||
return blocks
|
|
||||||
}
|
|
||||||
|
|
||||||
func getLanguage(text string) (*truetype.Font, bool, error) {
|
|
||||||
fontName := "fonts/NotoSans.ttf"
|
|
||||||
shouldBreakWords := false
|
|
||||||
|
|
||||||
for _, group := range []struct {
|
|
||||||
lang *unicode.RangeTable
|
|
||||||
fontName string
|
|
||||||
breakWords bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
unicode.Han,
|
|
||||||
"fonts/NotoSansSC.ttf",
|
|
||||||
true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
unicode.Katakana,
|
|
||||||
"fonts/NotoSansJP.ttf",
|
|
||||||
true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
unicode.Hiragana,
|
|
||||||
"fonts/NotoSansJP.ttf",
|
|
||||||
true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
unicode.Hangul,
|
|
||||||
"fonts/NotoSansKR.ttf",
|
|
||||||
true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
unicode.Arabic,
|
|
||||||
"fonts/NotoSansArabic.ttf",
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
unicode.Hebrew,
|
|
||||||
"fonts/NotoSansHebrew.ttf",
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
unicode.Bengali,
|
|
||||||
"fonts/NotoSansBengali.ttf",
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
unicode.Thai,
|
|
||||||
"fonts/NotoSansThai.ttf",
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
} {
|
|
||||||
for _, rune := range text {
|
|
||||||
rune16 := uint16(rune)
|
|
||||||
for _, r16 := range group.lang.R16 {
|
|
||||||
if rune16 >= r16.Lo && rune16 <= r16.Hi {
|
|
||||||
fontName = group.fontName
|
|
||||||
shouldBreakWords = group.breakWords
|
|
||||||
goto gotLang
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rune32 := uint32(rune)
|
|
||||||
for _, r32 := range group.lang.R32 {
|
|
||||||
if rune32 >= r32.Lo && rune32 <= r32.Hi {
|
|
||||||
fontName = group.fontName
|
|
||||||
shouldBreakWords = group.breakWords
|
|
||||||
goto gotLang
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gotLang:
|
|
||||||
fontData, err := fonts.ReadFile(fontName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
font, err := truetype.Parse(fontData)
|
|
||||||
if err != nil {
|
|
||||||
return nil, false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return font, shouldBreakWords, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
100
utils.go
100
utils.go
@@ -7,12 +7,17 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/draw"
|
||||||
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fogleman/gg"
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
"mvdan.cc/xurls/v2"
|
"mvdan.cc/xurls/v2"
|
||||||
@@ -504,3 +509,98 @@ func isntRealRelay(url string) bool {
|
|||||||
substr := []byte(url[6:])
|
substr := []byte(url[6:])
|
||||||
return bytes.IndexByte(substr, '/') != -1
|
return bytes.IndexByte(substr, '/') != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// quotesAsBlockPrefixedText replaces nostr:nevent1... and note with their text, as an extra line
|
||||||
|
// prefixed by BLOCK this returns a slice of lines
|
||||||
|
func quotesAsBlockPrefixedText(ctx context.Context, input string) []string {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
blocks := make([]string, 0, 8)
|
||||||
|
matches := nostrNoteNeventMatcher.FindAllStringSubmatchIndex(input, -1)
|
||||||
|
|
||||||
|
if len(matches) == 0 {
|
||||||
|
// no matches, just return text as it is
|
||||||
|
blocks = append(blocks, input)
|
||||||
|
return blocks
|
||||||
|
}
|
||||||
|
|
||||||
|
// one or more matches, return multiple lines
|
||||||
|
blocks = append(blocks, input[0:matches[0][0]])
|
||||||
|
i := -1 // matches iteration counter
|
||||||
|
b := 0 // current block index
|
||||||
|
for _, match := range matches {
|
||||||
|
i++
|
||||||
|
|
||||||
|
matchText := input[match[0]:match[1]]
|
||||||
|
submatch := nostrNoteNeventMatcher.FindStringSubmatch(matchText)
|
||||||
|
nip19 := submatch[0][6:]
|
||||||
|
|
||||||
|
event, _, err := getEvent(ctx, nip19, nil)
|
||||||
|
if err != nil {
|
||||||
|
// error case concat this to previous block
|
||||||
|
blocks[b] += matchText
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// add a new block with the quoted text
|
||||||
|
blocks = append(blocks, BLOCK+" "+event.Content)
|
||||||
|
|
||||||
|
// increase block count
|
||||||
|
b++
|
||||||
|
}
|
||||||
|
// add remaining text after the last match
|
||||||
|
remainingText := input[matches[i][1]:]
|
||||||
|
if strings.TrimSpace(remainingText) != "" {
|
||||||
|
blocks = append(blocks, remainingText)
|
||||||
|
}
|
||||||
|
|
||||||
|
return blocks
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchImageFromURL(url string) (image.Image, error) {
|
||||||
|
response, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
img, _, err := image.Decode(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return img, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func roundImage(img image.Image) image.Image {
|
||||||
|
bounds := img.Bounds()
|
||||||
|
diameter := math.Min(float64(bounds.Dx()), float64(bounds.Dy()))
|
||||||
|
radius := diameter / 2
|
||||||
|
|
||||||
|
// Create a new context for the mask
|
||||||
|
mask := gg.NewContext(bounds.Dx(), bounds.Dy())
|
||||||
|
mask.SetColor(color.Black) // Set the mask color to fully opaque
|
||||||
|
mask.DrawCircle(float64(bounds.Dx())/2, float64(bounds.Dy())/2, radius)
|
||||||
|
mask.ClosePath()
|
||||||
|
mask.Fill()
|
||||||
|
|
||||||
|
// Apply the circular mask to the original image
|
||||||
|
result := image.NewRGBA(bounds)
|
||||||
|
maskImg := mask.Image()
|
||||||
|
draw.DrawMask(result, bounds, img, image.Point{}, maskImg, image.Point{}, draw.Over)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func cropToSquare(img image.Image) image.Image {
|
||||||
|
bounds := img.Bounds()
|
||||||
|
size := int(math.Min(float64(bounds.Dx()), float64(bounds.Dy())))
|
||||||
|
squareImg := image.NewRGBA(image.Rect(0, 0, size, size))
|
||||||
|
for x := 0; x < size; x++ {
|
||||||
|
for y := 0; y < size; y++ {
|
||||||
|
squareImg.Set(x, y, img.At(x+(bounds.Dx()-size)/2, y+(bounds.Dy()-size)/2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return squareImg
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user