rendering everything with nice script and language detection and font-picking.

This commit is contained in:
fiatjaf
2023-12-25 22:36:26 -03:00
parent 5df944705e
commit a7b56f046b
5 changed files with 275 additions and 161 deletions

Binary file not shown.

BIN
fonts/NotoSansJavanese.ttf Normal file

Binary file not shown.

256
image_utils.go Normal file
View File

@@ -0,0 +1,256 @@
package main
import (
"bytes"
"context"
"image"
"image/color"
"image/draw"
"math"
"net/http"
"strings"
"time"
"github.com/fogleman/gg"
"github.com/go-text/typesetting/di"
"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/language"
"github.com/pemistahl/lingua-go"
)
const nSupportedScripts = 11
var (
supportedScripts = [nSupportedScripts]language.Script{
language.Unknown,
language.Hiragana,
language.Katakana,
language.Hebrew,
language.Thai,
language.Arabic,
language.Devanagari,
language.Bengali,
language.Javanese,
language.Han,
language.Hangul,
}
detector lingua.LanguageDetector
scriptRanges []ScriptRange
fontMap [nSupportedScripts]font.Face
emojiFont font.Face
)
type ScriptRange struct {
Start rune
End rune
Pos int
Script language.Script
}
func initializeImageDrawingStuff() error {
// language detector
detector = lingua.NewLanguageDetectorBuilder().FromLanguages(
lingua.Japanese,
lingua.Persian,
lingua.Chinese,
lingua.Thai,
lingua.Hebrew,
lingua.Arabic,
lingua.Bengali,
lingua.Korean,
).WithLowAccuracyMode().Build()
// script detector material
for ssi, script := range supportedScripts {
for _, srange := range language.ScriptRanges {
if srange.Script == script {
scriptRanges = append(scriptRanges, ScriptRange{
Start: srange.Start,
End: srange.End,
Script: srange.Script,
Pos: ssi,
})
}
}
}
// fonts
loadFont := func(filepath string) font.Face {
fontData, err := fonts.ReadFile(filepath)
face, err := font.ParseTTF(bytes.NewReader(fontData))
if err != nil {
log.Fatal().Err(err).Str("path", filepath).Msg("error loading font on startup")
return nil
}
return face
}
fontMap[0] = loadFont("fonts/NotoSans.ttf")
fontMap[1] = loadFont("fonts/NotoSansJP.ttf")
fontMap[2] = fontMap[1]
fontMap[3] = loadFont("fonts/NotoSansHebrew.ttf")
fontMap[4] = loadFont("fonts/NotoSansThai.ttf")
fontMap[5] = loadFont("fonts/NotoSansArabic.ttf")
fontMap[6] = loadFont("fonts/NotoSansDevanagari.ttf")
fontMap[7] = loadFont("fonts/NotoSansBengali.ttf")
fontMap[8] = loadFont("fonts/NotoSansJavanese.ttf")
fontMap[9] = loadFont("fonts/NotoSansSC.ttf")
fontMap[10] = loadFont("fonts/NotoSansKR.ttf")
emojiFont = loadFont("fonts/NotoEmoji.ttf")
return nil
}
// 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, lines []string) []string {
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
defer cancel()
blocks := make([]string, 0, len(lines)+7)
for _, line := range lines {
matches := nostrNoteNeventMatcher.FindAllStringSubmatchIndex(line, -1)
if len(matches) == 0 {
// no matches, just return text as it is
blocks = append(blocks, line)
continue
}
// one or more matches, return multiple lines
blocks = append(blocks, line[0:matches[0][0]])
i := -1 // matches iteration counter
b := 0 // current block index
for _, match := range matches {
i++
matchText := line[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 := line[matches[i][1]:]
if strings.TrimSpace(remainingText) != "" {
blocks = append(blocks, remainingText)
}
}
return blocks
}
func getLanguageAndScriptAndDirectionAndFont(paragraph []rune) (
language.Language,
language.Script,
di.Direction,
font.Face,
) {
var ranking [nSupportedScripts]int
nLetters := len(paragraph)
threshold := nLetters / 2
var script language.Script
var face font.Face
var idx int
for l := 0; l < nLetters; l++ {
idx := lookupScript(paragraph[l])
ranking[idx]++
if l > threshold && ranking[idx] > threshold {
script = supportedScripts[idx]
face = fontMap[idx]
goto gotScript
}
}
idx = maxIndex(ranking[:])
script = supportedScripts[idx]
face = fontMap[idx]
gotScript:
direction := di.DirectionLTR
if script == language.Arabic {
direction = di.DirectionRTL
}
lng := language.Language("en-us")
lang, ok := detector.DetectLanguageOf(string(paragraph))
if ok {
lng = language.Language(lang.IsoCode639_1().String())
}
return lng, script, direction, face
}
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 lookupScript(r rune) int {
// binary search
for i, j := 0, len(scriptRanges); i < j; {
h := i + (j-i)/2
entry := scriptRanges[h]
if r < entry.Start {
j = h
} else if entry.End < r {
i = h + 1
} else {
return h // position in supportedScripts
}
}
return 0 // unknown
}

View File

@@ -14,16 +14,10 @@ import (
"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"
sdk "github.com/nbd-wtf/nostr-sdk"
"github.com/nfnt/resize"
"github.com/pemistahl/lingua-go"
xfont "golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
@@ -36,53 +30,11 @@ var (
BACKGROUND = color.RGBA{23, 23, 23, 255}
BAR_BACKGROUND = color.RGBA{10, 10, 10, 255}
FOREGROUND = color.RGBA{255, 230, 238, 255}
fontmap *fontscan.FontMap
detector lingua.LanguageDetector
)
//go:embed fonts/*
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) {
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
@@ -104,7 +56,7 @@ func renderImage(w http.ResponseWriter, r *http.Request) {
// this turns the raw event.Content into a series of lines ready to drawn
paragraphs := replaceUserReferencesWithNames(r.Context(),
quotesAsBlockPrefixedText(r.Context(),
content,
strings.Split(content, "\n"),
),
)
@@ -145,8 +97,8 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d
img.SetColor(FOREGROUND)
// main content text
textImg := drawText(paragraphs, width-4*2, height-2*2)
img.DrawImage(textImg, 4, 2)
textImg := drawText(paragraphs, width-25*2, height-20)
img.DrawImage(textImg, 25, 20)
// font for writing the bottom bar stuff
fontData, _ := fonts.ReadFile("fonts/NotoSans.ttf")
@@ -219,12 +171,12 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d
}
func drawText(paragraphs []string, width, height int) image.Image {
const FONT_SIZE = 26
const FONT_SIZE = 25
r := &render.Renderer{
PixScale: 1,
FontSize: FONT_SIZE,
Color: color.White,
Color: color.RGBA{R: 255, G: 230, B: 238, A: 255},
}
img := image.NewNRGBA(image.Rect(0, 0, width, height))
@@ -233,24 +185,18 @@ func drawText(paragraphs []string, width, height int) image.Image {
for _, paragraph := range paragraphs {
text := []rune(paragraph)
detectedLanguage, ok := detector.DetectLanguageOf(paragraph)
var lang language.Language
if ok {
lang = language.NewLanguage(detectedLanguage.IsoCode639_1().String())
}
lang, script, dir, face := getLanguageAndScriptAndDirectionAndFont(text)
shaper := &shaping.HarfbuzzShaper{}
face := fontmap.ResolveFace(text[0])
shapedRunes := shaper.Shape(shaping.Input{
Text: []rune(text),
Text: text,
RunStart: 0,
RunEnd: len([]rune(text)),
RunEnd: len(text),
Face: face,
Size: fixed.I(int(r.FontSize)),
Script: language.LookupScript([]rune(text)[0]),
Script: script,
Language: lang,
Direction: di.DirectionRTL,
Direction: dir,
})
var wrapper shaping.LineWrapper
@@ -258,7 +204,7 @@ func drawText(paragraphs []string, width, height int) image.Image {
for _, line := range lines {
for _, out := range line {
r.DrawShapedRunAt(out, img, 0, FONT_SIZE*i)
r.DrawShapedRunAt(out, img, 0, FONT_SIZE*i*12/10)
i++
}
}

104
utils.go
View File

@@ -7,17 +7,12 @@ import (
"fmt"
"html"
"html/template"
"image"
"image/color"
"image/draw"
"math"
"math/rand"
"net/http"
"regexp"
"strings"
"time"
"github.com/fogleman/gg"
"github.com/microcosm-cc/bluemonday"
"golang.org/x/exp/slices"
"mvdan.cc/xurls/v2"
@@ -510,97 +505,14 @@ func isntRealRelay(url string) bool {
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
func maxIndex(slice []int) int {
maxIndex := 0
maxVal := 0
for i, val := range slice {
if val > maxVal {
maxVal = val
maxIndex = i
}
// 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
return maxIndex
}