forked from fiatjaf/njump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_utils.go
866 lines (764 loc) · 25 KB
/
image_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
package main
import (
"bytes"
"context"
"fmt"
"image"
"image/color"
"image/draw"
_ "image/gif"
_ "image/jpeg"
"image/png"
_ "image/png"
"math"
"net/http"
"net/url"
"os"
"os/exec"
"slices"
"strings"
"sync"
"time"
"unicode"
"github.com/fogleman/gg"
"github.com/go-text/typesetting/di"
"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/harfbuzz"
"github.com/go-text/typesetting/language"
"github.com/go-text/typesetting/opentype/api"
"github.com/go-text/typesetting/shaping"
"github.com/golang/freetype/truetype"
"github.com/nbd-wtf/emoji"
"github.com/nfnt/resize"
"github.com/srwiley/rasterx"
"golang.org/x/image/math/fixed"
_ "golang.org/x/image/webp"
)
const (
nSupportedScripts = 14
scaleShift = 6
)
// highlighting stuff
type hlstate int
const (
hlNormal hlstate = 0
hlLink hlstate = 1
hlMention hlstate = 2
hlHashtag hlstate = 3
)
var (
supportedScripts = [nSupportedScripts]language.Script{
language.Unknown,
language.Latin,
language.Cyrillic,
language.Hiragana,
language.Katakana,
language.Hebrew,
language.Thai,
language.Arabic,
language.Devanagari,
language.Bengali,
language.Javanese,
language.Han,
language.Hangul,
language.Syriac,
}
scriptRanges []ScriptRange
fontMap [nSupportedScripts]font.Face
emojiFace font.Face
dateFont *truetype.Font
defaultLanguageMap = [nSupportedScripts]language.Language{
"en-us",
"en-us",
"ru",
"ja",
"ja",
"he",
"th",
"ar",
"hi",
"bn",
"jv",
"zh",
"ko",
"syr",
}
directionMap = [nSupportedScripts]di.Direction{
di.DirectionLTR,
di.DirectionLTR,
di.DirectionLTR,
di.DirectionLTR,
di.DirectionLTR,
di.DirectionRTL,
di.DirectionLTR,
di.DirectionRTL,
di.DirectionLTR,
di.DirectionLTR,
di.DirectionLTR,
di.DirectionLTR,
di.DirectionLTR,
di.DirectionRTL,
}
shaperLock sync.Mutex
mainBuffer = harfbuzz.NewBuffer()
emojiBuffer = harfbuzz.NewBuffer()
fontCache = make(map[font.Face]*harfbuzz.Font)
emojiFont *harfbuzz.Font
lettersAndNumbers = &unicode.RangeTable{}
)
type ScriptRange struct {
Start rune
End rune
Pos int
Script language.Script
}
func initializeImageDrawingStuff() error {
// script detector material
for _, srange := range language.ScriptRanges {
for ssi, script := range supportedScripts {
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] = fontMap[0]
fontMap[2] = fontMap[0]
fontMap[3] = loadFont("fonts/NotoSansJP.ttf")
fontMap[4] = fontMap[3]
fontMap[5] = loadFont("fonts/NotoSansHebrew.ttf")
fontMap[6] = loadFont("fonts/NotoSansThai.ttf")
fontMap[7] = loadFont("fonts/NotoSansArabic.ttf")
fontMap[8] = loadFont("fonts/NotoSansDevanagari.ttf")
fontMap[9] = loadFont("fonts/NotoSansBengali.ttf")
fontMap[10] = loadFont("fonts/NotoSansJavanese.ttf")
fontMap[11] = loadFont("fonts/NotoSansSC.ttf")
fontMap[12] = loadFont("fonts/NotoSansKR.ttf")
emojiFace = loadFont("fonts/NotoEmoji.ttf")
fontData, _ := fonts.ReadFile("fonts/NotoSans.ttf")
dateFont, _ = truetype.Parse(fontData)
// shaper stuff
emojiFont = harfbuzz.NewFont(emojiFace)
// highlighting stuff
lettersAndNumbers.LatinOffset = unicode.Latin.LatinOffset + unicode.Number.LatinOffset
lettersAndNumbers.R16 = make([]unicode.Range16, len(unicode.Latin.R16)+len(unicode.Number.R16)+1)
copy(lettersAndNumbers.R16, unicode.Latin.R16)
copy(lettersAndNumbers.R16[len(unicode.Latin.R16):], unicode.Number.R16)
lettersAndNumbers.R16[len(unicode.Latin.R16)+len(unicode.Number.R16)] = unicode.Range16{
Lo: uint16(THIN_SPACE),
Hi: uint16(THIN_SPACE),
Stride: 1,
}
slices.SortFunc(lettersAndNumbers.R16, func(a, b unicode.Range16) int { return int(a.Lo) - int(b.Lo) })
lettersAndNumbers.R32 = make([]unicode.Range32, len(unicode.Latin.R32)+len(unicode.Number.R32))
copy(lettersAndNumbers.R32, unicode.Latin.R32)
copy(lettersAndNumbers.R32[len(unicode.Latin.R32):], unicode.Number.R32)
slices.SortFunc(lettersAndNumbers.R32, func(a, b unicode.Range32) int { return int(a.Lo) - int(b.Lo) })
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, false)
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++ {
rnidx := lookupScript(paragraph[l])
ranking[rnidx]++
if idx > 0 && l > threshold && ranking[rnidx] > threshold {
idx = rnidx
goto gotScriptIndex
}
}
idx = maxIndex(ranking[2:] /* skip Unknown and Latin because they are the default */)
idx += 2 // add back the skipped indexes (if maxIndex returns -1 this will default us to 1, latin)
gotScriptIndex:
script = supportedScripts[idx]
face = fontMap[idx]
direction := directionMap[idx]
lng := defaultLanguageMap[idx]
return lng, script, direction, face
}
func fetchImageFromURL(ctx context.Context, url string) (image.Image, error) {
ctx, cancel := context.WithTimeout(ctx, time.Millisecond*350)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch image from %s: %w", url, err)
}
defer response.Body.Close()
img, _, err := image.Decode(response.Body)
if err != nil {
return nil, fmt.Errorf("failed to decode image from %s: %w", url, 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 entry.Pos // position in supportedScripts
}
}
return 0 // unknown
}
// shortenURLs takes a text content and returns the same content, but with all big URLs like https://image.nostr.build/0993112ab590e04b978ad32002005d42c289d43ea70d03dafe9ee99883fb7755.jpg#m=image%2Fjpeg&dim=1361x1148&blurhash=%3B7Jjhw00.l.QEh%3FuIA-pMe00%7EVjXX8x%5DE2xuSgtQcr%5E%2500%3FHxD%24%25%25Ms%2Bt%2B-%3BVZK59a%252MyD%2BV%5BI.8%7Ds%3B%25Lso-oi%5ENINHnjI%3BR*%3DdM%7BX7%25MIUtksn%24LM%7BMySeR%25R*%251M%7DRkv%23RjtjS%239as%3AxDnO%251&x=61be75a3e3e0cc88e7f0e625725d66923fdd777b3b691a1c7072ba494aef188d shortened to something like https://image.nostr.build/.../...7755.jpg
func shortenURLs(text string, skipImages bool) string {
return urlMatcher.ReplaceAllStringFunc(text, func(match string) string {
if skipImages && isMediaURL(match) {
return match // Skip media URLs
}
if len(match) < 50 {
return match
}
parsed, err := url.Parse(match)
if err != nil {
return match
}
parsed.Fragment = ""
if len(parsed.RawQuery) > 10 {
parsed.RawQuery = ""
}
pathParts := strings.Split(parsed.Path, "/")
nParts := len(pathParts)
lastPart := pathParts[nParts-1]
if len(lastPart) > 12 {
pathParts[nParts-1] = "…" + lastPart[len(lastPart)-11:]
}
if nParts > 2 {
pathParts[1] = "…"
pathParts[2] = pathParts[nParts-1]
pathParts = pathParts[0:2]
}
parsed.Path = "/////"
urlStr := parsed.String()
return strings.Replace(urlStr, "/////", strings.Join(pathParts, "/"), 1)
})
}
// beware: this is all very hacky and I don't know what I am doing!
// this function is copied from go-text/typesetting/shaping's HarfbuzzShaper and adapted to not require a "class",
// to rely on our dirty globals like fontCache, shaperLock and mainBuffer; it also uses a custom function to
// determine language, script, direction and font face internally instead of taking a shaping.Input argument --
// but also, the most important change was to make it "shape" the same text, twice, with the default font and with
// the emoji font, then build an output of glyphs containing normal glyphs for when the referenced rune is not an
// emoji and an emoji glyph for when it is.
func shapeText(rawText []rune, fontSize int) (shaping.Output, []bool, []hlstate) {
lang, script, dir, face := getLanguageAndScriptAndDirectionAndFont(rawText)
shaperLock.Lock()
defer shaperLock.Unlock()
// load or get main font from cache
mainFont, ok := fontCache[face]
if !ok {
mainFont = harfbuzz.NewFont(face)
fontCache[face] = mainFont
}
// define this only once
input := shaping.Input{
Text: rawText,
RunStart: 0,
RunEnd: len(rawText),
Face: face,
Size: fixed.I(int(fontSize)),
Script: script,
Language: lang,
Direction: dir,
}
// shape stuff for both normal text and emojis
for _, params := range []struct {
font *harfbuzz.Font
buf *harfbuzz.Buffer
}{
{mainFont, mainBuffer},
{emojiFont, emojiBuffer},
} {
params.buf.Clear() // clear before using
runes, start, end := input.Text, input.RunStart, input.RunEnd
if end < start {
panic("end < start")
}
start = clamp(start, 0, len(runes))
end = clamp(end, 0, len(runes))
params.buf.AddRunes(runes, start, end-start)
params.buf.Props.Direction = input.Direction.Harfbuzz()
params.buf.Props.Language = input.Language
params.buf.Props.Script = input.Script
// adjust the user provided fields
params.font.XScale = int32(input.Size.Ceil()) << scaleShift
params.font.YScale = params.font.XScale
// actually use harfbuzz to shape the text.
params.buf.Shape(params.font, nil)
}
// this will be used to determine whether a given glyph is an emoji or not when rendering
emojiMask := make([]bool, len(emojiBuffer.Info))
if len(mainBuffer.Info) > len(emojiBuffer.Info) {
// remove from mainBuffer characters that are not present in emojiBuffer
newMainBufferInfo := make([]harfbuzz.GlyphInfo, len(emojiBuffer.Info))
newMainBufferPos := make([]harfbuzz.GlyphPosition, len(emojiBuffer.Info))
outer:
for e, m := 0, 0; e < len(emojiBuffer.Info); {
ec := emojiBuffer.Info[e].Codepoint
if ec == mainBuffer.Info[m].Codepoint {
newMainBufferInfo[e] = mainBuffer.Info[m]
newMainBufferPos[e] = mainBuffer.Pos[m]
if emoji.IsEmoji(ec) || emoji.IsTag(ec) || emoji.IsRegionalIndicator(ec) {
emojiMask[e] = true
}
e++
m++
} else {
m++
for ; m < len(mainBuffer.Info) && ec != mainBuffer.Info[m].Codepoint; m++ {
// we increase m until mainBuffer catches up with emojiBuffer
// if we reach the end of mainBuffer and that never happens, then that means it was actually
// emojiBuffer that had to catch up with mainBuffer -- but we don't handle this for now
// we just break out of the outer loop and render whatever we had ignoring emojis
if len(mainBuffer.Info) < m {
newMainBufferInfo = mainBuffer.Info
newMainBufferPos = mainBuffer.Pos
emojiMask = make([]bool, len(emojiBuffer.Info))
log.Debug().Interface("raw", rawText).Msg("unexpected mismatch between main and emoji buffers")
break outer
}
}
}
}
mainBuffer.Info = newMainBufferInfo
mainBuffer.Pos = newMainBufferPos
} else {
// just go through the glyphs and decide which ones are emojis
for e := range emojiBuffer.Info {
ec := emojiBuffer.Info[e].Codepoint
if emoji.IsEmoji(ec) || emoji.IsTag(ec) || emoji.IsRegionalIndicator(ec) {
emojiMask[e] = true
}
}
}
// this will be used to determine if we'll use a different color when rendering a glyph or not
hlMask := make([]hlstate, len(emojiBuffer.Info))
var hlState hlstate = hlNormal
hlSkip := 0 // this will cause us to skip the highlighting parsing phase for the next x glyphs
// convert the shaped text into an output
glyphs := make([]shaping.Glyph, len(mainBuffer.Info))
for i := 0; i < len(glyphs); i++ {
// deciding if we'll render this as emoji or not
var buf *harfbuzz.Buffer
var font *harfbuzz.Font
if emojiMask[i] {
buf = emojiBuffer
font = emojiFont
} else {
buf = mainBuffer
font = mainFont
}
// current glyph specs
glyph := buf.Info[i]
// naïve text highlighting
if hlSkip > 0 {
// skip once
hlSkip--
} else {
switch hlState {
case hlNormal:
if glyph.Codepoint == '#' &&
len(buf.Info) > i+1 &&
unicode.Is(lettersAndNumbers, buf.Info[i+1].Codepoint) {
hlState = hlHashtag
hlSkip = 1 // we already know the next character is a letter in the hashtag, so skip it
} else if glyph.Codepoint == 'h' &&
len(buf.Info) > i+1 &&
buf.Info[i+1].Codepoint == 't' &&
buf.Info[i+2].Codepoint == 't' &&
buf.Info[i+3].Codepoint == 'p' {
if buf.Info[i+4].Codepoint == 's' &&
buf.Info[i+5].Codepoint == ':' &&
buf.Info[i+6].Codepoint == '/' &&
buf.Info[i+7].Codepoint == '/' &&
buf.Info[i+8].Codepoint != ' ' {
hlState = hlLink
hlSkip = 8 // we already know the next 8 characters are 'ttps://_', so skip them
} else if buf.Info[i+4].Codepoint == ':' &&
buf.Info[i+5].Codepoint == '/' &&
buf.Info[i+6].Codepoint == '/' &&
buf.Info[i+7].Codepoint != ' ' {
hlState = hlLink
hlSkip = 7 // we already know the next 8 characters are 'ttp://_', so skip them
}
} else if glyph.Codepoint == INVISIBLE_SPACE &&
len(buf.Info) > i+1 &&
(unicode.Is(lettersAndNumbers, buf.Info[i+1].Codepoint) || emojiMask[i+1]) {
hlState = hlMention
hlSkip = 1 // we already know the next character is a letter or emoji
}
case hlLink:
if glyph.Codepoint == ' ' ||
glyph.Codepoint == ',' {
hlState = hlNormal
}
case hlMention:
if !unicode.Is(lettersAndNumbers, glyph.Codepoint) && !emojiMask[i] {
hlState = hlNormal
}
case hlHashtag:
if !unicode.Is(lettersAndNumbers, glyph.Codepoint) {
hlState = hlNormal
}
}
}
hlMask[i] = hlState
// ~
glyphs[i] = shaping.Glyph{
ClusterIndex: glyph.Cluster,
GlyphID: glyph.Glyph,
Mask: glyph.Mask,
}
extents, ok := font.GlyphExtents(glyph.Glyph)
if !ok {
continue
}
glyphs[i].Width = fixed.I(int(extents.Width)) >> scaleShift
glyphs[i].Height = fixed.I(int(extents.Height)) >> scaleShift
glyphs[i].XBearing = fixed.I(int(extents.XBearing)) >> scaleShift
glyphs[i].YBearing = fixed.I(int(extents.YBearing)) >> scaleShift
glyphs[i].XAdvance = fixed.I(int(buf.Pos[i].XAdvance)) >> scaleShift
glyphs[i].YAdvance = fixed.I(int(buf.Pos[i].YAdvance)) >> scaleShift
glyphs[i].XOffset = fixed.I(int(buf.Pos[i].XOffset)) >> scaleShift
glyphs[i].YOffset = fixed.I(int(buf.Pos[i].YOffset)) >> scaleShift
}
countClusters(glyphs, input.RunEnd, input.Direction.Progression())
out := shaping.Output{
Glyphs: glyphs,
Direction: input.Direction,
Face: input.Face,
Size: input.Size,
}
out.Runes.Offset = input.RunStart
out.Runes.Count = input.RunEnd - input.RunStart
fontExtents := mainFont.ExtentsForDirection(out.Direction.Harfbuzz())
out.LineBounds = shaping.Bounds{
Ascent: fixed.I(int(fontExtents.Ascender)) >> scaleShift,
Descent: fixed.I(int(fontExtents.Descender)) >> scaleShift,
Gap: fixed.I(int(fontExtents.LineGap)) >> scaleShift,
}
out.RecalculateAll()
return out, emojiMask, hlMask
}
// this function is copied from go-text/typesetting/shaping because shapeText needs it
func countClusters(glyphs []shaping.Glyph, textLen int, dir di.Progression) {
currentCluster := -1
runesInCluster := 0
glyphsInCluster := 0
previousCluster := textLen
for i := range glyphs {
g := glyphs[i].ClusterIndex
if g != currentCluster {
// If we're processing a new cluster, count the runes and glyphs
// that compose it.
runesInCluster = 0
glyphsInCluster = 1
currentCluster = g
nextCluster := -1
glyphCountLoop:
for k := i + 1; k < len(glyphs); k++ {
if glyphs[k].ClusterIndex == g {
glyphsInCluster++
} else {
nextCluster = glyphs[k].ClusterIndex
break glyphCountLoop
}
}
if nextCluster == -1 {
nextCluster = textLen
}
switch dir {
case di.FromTopLeft:
runesInCluster = nextCluster - currentCluster
case di.TowardTopLeft:
runesInCluster = previousCluster - currentCluster
}
previousCluster = g
}
glyphs[i].GlyphCount = glyphsInCluster
glyphs[i].RuneCount = runesInCluster
}
}
// this function is copied from go-text/render, but adapted to not require a "class" to be instantiated and also,
// more importantly, to take an emojiMask parameter, with the same length as out.Glyphs, to determine when a
// glyph should be rendered with the emoji font instead of with the default font
func drawShapedBlockAt(
img draw.Image,
fontSize int,
colors [4]color.Color,
out shaping.Output,
emojiMask []bool,
hlMask []hlstate,
maskBaseIndex int,
startX,
startY int,
) (charsWritten int, endingX int) {
scale := float32(fontSize) / float32(out.Face.Upem())
b := img.Bounds()
var fillers [4]*rasterx.Filler
for i := range fillers {
scanner := rasterx.NewScannerGV(b.Dx(), b.Dy(), img, b)
fillers[i] = rasterx.NewFiller(b.Dx(), b.Dy(), scanner)
fillers[i].SetColor(colors[i])
}
x := float32(startX)
y := float32(startY)
for i, g := range out.Glyphs {
xPos := x + fixed266ToFloat(g.XOffset)
yPos := y - fixed266ToFloat(g.YOffset)
face := out.Face
currentScale := scale
if emojiMask[maskBaseIndex+i] {
face = emojiFace
currentScale = float32(fontSize) / float32(face.Upem())
}
f := fillers[hlMask[maskBaseIndex+i]]
data := face.GlyphData(g.GlyphID)
switch format := data.(type) {
case api.GlyphOutline:
drawOutline(format, f, currentScale, xPos, yPos)
case nil:
continue
default:
panic("format not supported for glyph")
}
charsWritten++
x += fixed266ToFloat(g.XAdvance)
}
for _, filler := range fillers {
filler.Draw()
}
return charsWritten, int(math.Ceil(float64(x)))
}
func drawImageAt(ctx context.Context, img draw.Image, imageUrl string, startY int) int {
srcImg, err := fetchImageFromURL(ctx, imageUrl)
if err != nil {
return -1
}
// Resize the fetched image to fit the width of the destination image (img)
width := img.Bounds().Dx()
resizedImg := resize.Resize(uint(width), 0, srcImg, resize.Lanczos3)
destY := startY
destHeight := resizedImg.Bounds().Dy()
destRect := image.Rect(0, destY, width, destY+destHeight)
draw.Draw(img, destRect, resizedImg, image.Point{X: 0, Y: 0}, draw.Src)
return startY + destHeight
}
func drawVideoAt(img draw.Image, videoUrl string, startY int) int {
tempImagePath := "temp_frame.jpg"
cmd := exec.Command("ffmpeg", "-i", videoUrl, "-vframes", "1", "-f", "image2", tempImagePath)
cmd.Stdout = nil
cmd.Stderr = nil
if err := cmd.Run(); err != nil {
return -1
}
frame, _ := os.Open(tempImagePath)
defer os.Remove(tempImagePath)
defer frame.Close()
imgData, _, err := image.Decode(frame)
if err != nil {
return -1
}
width := img.Bounds().Dx()
resizedFrame := resize.Resize(uint(width), 0, imgData, resize.Lanczos3)
// draw the play icon on the center of the frame
videoFrame := image.NewRGBA(resizedFrame.Bounds())
draw.Draw(videoFrame, videoFrame.Bounds(), resizedFrame, image.Point{}, draw.Src)
iconFile, _ := static.ReadFile("static/play.png")
stampImg, _ := png.Decode(bytes.NewBuffer(iconFile))
videoWidth := videoFrame.Bounds().Dx()
videoHeight := videoFrame.Bounds().Dy()
iconWidth := stampImg.Bounds().Dx()
iconHeight := stampImg.Bounds().Dy()
posX := (videoWidth - iconWidth) / 2
posY := (videoHeight - iconHeight) / 2
destRect := image.Rect(posX, posY, posX+iconWidth, posY+iconHeight)
draw.Draw(videoFrame, destRect, stampImg, image.Point{}, draw.Over)
// draw the modified video frame onto the main canvas
destRect = image.Rect(0, startY, img.Bounds().Dx(), startY+videoFrame.Bounds().Dy())
draw.Draw(img, destRect, videoFrame, image.Point{}, draw.Src)
return startY + videoFrame.Bounds().Dy()
}
func drawMediaAt(ctx context.Context, img draw.Image, mediaUrl string, startY int) int {
if isImageURL(mediaUrl) {
return drawImageAt(ctx, img, mediaUrl, startY)
} else if isVideoURL(mediaUrl) {
return drawVideoAt(img, mediaUrl, startY)
} else {
return startY
}
}
func isImageURL(input string) bool {
parsedURL, err := url.Parse(input)
if err != nil {
return false // unable to parse URL, consider it non-image URL
}
// extract the path (excluding query string and hash fragment)
path := parsedURL.Path
imageExtensions := []string{".jpg", ".jpeg", ".png", ".gif", ".bmp"}
for _, ext := range imageExtensions {
if strings.HasSuffix(strings.ToLower(path), ext) {
return true // URL points to a valid image
}
}
return false
}
func isVideoURL(input string) bool {
parsedURL, err := url.Parse(input)
if err != nil {
return false // unable to parse URL, consider it non-video URL
}
// Extract the path (excluding query string and hash fragment)
path := parsedURL.Path
imageExtensions := []string{".mp4", ".mov"}
for _, ext := range imageExtensions {
if strings.HasSuffix(strings.ToLower(path), ext) {
return true // URL points to a valid image
}
}
return false
}
func isMediaURL(input string) bool {
return isImageURL(input) || isVideoURL(input)
}
func containsMedia(paragraphs []string) bool {
for _, paragraph := range paragraphs {
if isMediaURL(paragraph) {
return true
}
}
return false
}
// this draws a font glyph (i.e. a letter) according to instructions and scale and whatever
func drawOutline(bitmap api.GlyphOutline, f *rasterx.Filler, scale float32, x, y float32) {
for _, s := range bitmap.Segments {
switch s.Op {
case api.SegmentOpMoveTo:
f.Start(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)})
case api.SegmentOpLineTo:
f.Line(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)})
case api.SegmentOpQuadTo:
f.QuadBezier(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)},
fixed.Point26_6{X: floatToFixed266(s.Args[1].X*scale + x), Y: floatToFixed266(-s.Args[1].Y*scale + y)})
case api.SegmentOpCubeTo:
f.CubeBezier(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)},
fixed.Point26_6{X: floatToFixed266(s.Args[1].X*scale + x), Y: floatToFixed266(-s.Args[1].Y*scale + y)},
fixed.Point26_6{X: floatToFixed266(s.Args[2].X*scale + x), Y: floatToFixed266(-s.Args[2].Y*scale + y)})
}
}
f.Stop(true)
}
func fixed266ToFloat(i fixed.Int26_6) float32 {
return float32(float64(i) / 64)
}
func floatToFixed266(f float32) fixed.Int26_6 {
return fixed.Int26_6(int(float64(f) * 64))
}
// clamp ensures val is in the inclusive range [low,high].
func clamp(val, low, high int) int {
if val < low {
return low
}
if val > high {
return high
}
return val
}