mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-18 14:54:24 +01:00
basically revert the previous commit.
This commit is contained in:
@@ -12,14 +12,11 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/dmolesUC3/emoji"
|
||||
"github.com/fogleman/gg"
|
||||
"github.com/go-text/typesetting/di"
|
||||
"github.com/go-text/typesetting/font"
|
||||
"github.com/go-text/typesetting/language"
|
||||
"github.com/go-text/typesetting/shaping"
|
||||
"github.com/pemistahl/lingua-go"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
const nSupportedScripts = 13
|
||||
@@ -328,81 +325,3 @@ func shortenURLs(text string) string {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
sdk "github.com/nbd-wtf/nostr-sdk"
|
||||
"github.com/nfnt/resize"
|
||||
xfont "golang.org/x/image/font"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -183,23 +184,24 @@ func drawText(paragraphs []string, width, height int) image.Image {
|
||||
i := 1
|
||||
for _, paragraph := range paragraphs {
|
||||
rawText := []rune(paragraph)
|
||||
if len(rawText) == 0 {
|
||||
rawText = []rune{' '}
|
||||
}
|
||||
|
||||
lang, script, dir, face := getLanguageAndScriptAndDirectionAndFont(rawText)
|
||||
iterator := &shapedOutputIterator{
|
||||
rawText: rawText,
|
||||
shaper: &shaping.HarfbuzzShaper{},
|
||||
fontSize: FONT_SIZE,
|
||||
language: lang,
|
||||
script: script,
|
||||
direction: dir,
|
||||
face: face,
|
||||
}
|
||||
shaper := &shaping.HarfbuzzShaper{}
|
||||
|
||||
shapedRunes := shaper.Shape(shaping.Input{
|
||||
Text: rawText,
|
||||
RunStart: 0,
|
||||
RunEnd: len(rawText),
|
||||
Face: face,
|
||||
Size: fixed.I(int(r.FontSize)),
|
||||
Script: script,
|
||||
Language: lang,
|
||||
Direction: dir,
|
||||
})
|
||||
|
||||
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 _, out := range line {
|
||||
|
||||
Reference in New Issue
Block a user