basically revert the previous commit.

This commit is contained in:
fiatjaf
2023-12-26 11:05:40 -03:00
parent 86f6a09ea3
commit 124dc390d4
2 changed files with 15 additions and 94 deletions

View File

@@ -12,14 +12,11 @@ import (
"strings" "strings"
"time" "time"
"github.com/dmolesUC3/emoji"
"github.com/fogleman/gg" "github.com/fogleman/gg"
"github.com/go-text/typesetting/di" "github.com/go-text/typesetting/di"
"github.com/go-text/typesetting/font" "github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/language" "github.com/go-text/typesetting/language"
"github.com/go-text/typesetting/shaping"
"github.com/pemistahl/lingua-go" "github.com/pemistahl/lingua-go"
"golang.org/x/image/math/fixed"
) )
const nSupportedScripts = 13 const nSupportedScripts = 13
@@ -328,81 +325,3 @@ func shortenURLs(text string) string {
return strings.Replace(urlStr, "/////", strings.Join(pathParts, "/"), 1) return strings.Replace(urlStr, "/////", strings.Join(pathParts, "/"), 1)
}) })
} }
type shapedOutputIterator struct {
rawText []rune
idx int
savedIdx int
shaper *shaping.HarfbuzzShaper
fontSize int
face font.Face
language language.Language
script language.Script
direction di.Direction
}
var _ shaping.RunIterator = (*shapedOutputIterator)(nil)
func (it *shapedOutputIterator) Next() (int, shaping.Output, bool) {
idx, nextIdx, run, ok := it.readNext()
if ok {
it.idx = nextIdx
}
return idx, run, ok
}
func (it *shapedOutputIterator) Peek() (int, shaping.Output, bool) {
idx, _, out, more := it.readNext()
return idx, out, more
}
func (it *shapedOutputIterator) readNext() (int, int, shaping.Output, bool) {
if it.idx >= len(it.rawText) {
return it.idx, -1, shaping.Output{}, false
}
// if the next character is an emoji then return a block of emojis
if emoji.IsEmoji(it.rawText[it.idx]) {
shapedEmoji := it.shaper.Shape(shaping.Input{
Text: it.rawText,
RunStart: it.idx,
RunEnd: it.idx + 1,
Face: emojiFont,
Size: fixed.I(int(it.fontSize)),
Script: it.script,
Language: it.language,
Direction: it.direction,
})
return it.idx, it.idx + 1, shapedEmoji, true
}
// otherwise we consume runes until we find an emoji and return everything
var runesConsumed int = 0
for r, rn := range it.rawText[it.idx:] {
if emoji.IsEmoji(rn) {
// reached an emoji, stop now
break
}
runesConsumed = r
}
shapedRunes := it.shaper.Shape(shaping.Input{
Text: it.rawText,
RunStart: it.idx,
RunEnd: it.idx + runesConsumed + 1,
Face: it.face,
Size: fixed.I(int(it.fontSize)),
Script: it.script,
Language: it.language,
Direction: it.direction,
})
return it.idx, it.idx + runesConsumed + 1, shapedRunes, true
}
func (it *shapedOutputIterator) Save() {
it.savedIdx = it.idx
}
func (it *shapedOutputIterator) Restore() {
it.idx = it.savedIdx
}

View File

@@ -19,6 +19,7 @@ import (
sdk "github.com/nbd-wtf/nostr-sdk" sdk "github.com/nbd-wtf/nostr-sdk"
"github.com/nfnt/resize" "github.com/nfnt/resize"
xfont "golang.org/x/image/font" xfont "golang.org/x/image/font"
"golang.org/x/image/math/fixed"
) )
const ( const (
@@ -183,23 +184,24 @@ func drawText(paragraphs []string, width, height int) image.Image {
i := 1 i := 1
for _, paragraph := range paragraphs { for _, paragraph := range paragraphs {
rawText := []rune(paragraph) rawText := []rune(paragraph)
if len(rawText) == 0 {
rawText = []rune{' '}
}
lang, script, dir, face := getLanguageAndScriptAndDirectionAndFont(rawText) lang, script, dir, face := getLanguageAndScriptAndDirectionAndFont(rawText)
iterator := &shapedOutputIterator{ shaper := &shaping.HarfbuzzShaper{}
rawText: rawText,
shaper: &shaping.HarfbuzzShaper{}, shapedRunes := shaper.Shape(shaping.Input{
fontSize: FONT_SIZE, Text: rawText,
language: lang, RunStart: 0,
script: script, RunEnd: len(rawText),
direction: dir, Face: face,
face: face, Size: fixed.I(int(r.FontSize)),
} Script: script,
Language: lang,
Direction: dir,
})
var wrapper shaping.LineWrapper var wrapper shaping.LineWrapper
lines, _ := wrapper.WrapParagraph(shaping.WrapConfig{}, width, rawText, iterator) it := shaping.NewSliceIterator([]shaping.Output{shapedRunes})
lines, _ := wrapper.WrapParagraph(shaping.WrapConfig{}, width, rawText, it)
for _, line := range lines { for _, line := range lines {
for _, out := range line { for _, out := range line {