mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 06:14:22 +01:00
Merge branch 'use-go-text-typesetting'
This commit is contained in:
BIN
fonts/NotoEmoji.ttf
Normal file
BIN
fonts/NotoEmoji.ttf
Normal file
Binary file not shown.
BIN
fonts/NotoSansDevanagari.ttf
Normal file
BIN
fonts/NotoSansDevanagari.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
fonts/NotoSansJavanese.ttf
Normal file
BIN
fonts/NotoSansJavanese.ttf
Normal file
Binary file not shown.
BIN
fonts/NotoSansSyriac.ttf
Normal file
BIN
fonts/NotoSansSyriac.ttf
Normal file
Binary file not shown.
11
go.mod
11
go.mod
@@ -4,25 +4,28 @@ go 1.21.4
|
||||
|
||||
require (
|
||||
github.com/PuerkitoBio/goquery v1.5.0
|
||||
github.com/apatters/go-wordwrap v1.0.0
|
||||
github.com/dgraph-io/badger/v4 v4.2.0
|
||||
github.com/fiatjaf/eventstore v0.3.3
|
||||
github.com/fiatjaf/khatru v0.2.1
|
||||
github.com/fogleman/gg v1.3.0
|
||||
github.com/go-text/typesetting v0.0.0-20231221124458-48cc05a56658
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
|
||||
github.com/gomarkdown/markdown v0.0.0-20231115200524-a660076da3fd
|
||||
github.com/kelseyhightower/envconfig v1.4.0
|
||||
github.com/microcosm-cc/bluemonday v1.0.24
|
||||
github.com/nbd-wtf/emoji v0.0.3
|
||||
github.com/nbd-wtf/go-nostr v0.27.3
|
||||
github.com/nbd-wtf/nostr-sdk v0.0.4
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||
github.com/pelletier/go-toml v1.9.5
|
||||
github.com/pemistahl/lingua-go v1.4.0
|
||||
github.com/rs/cors v1.10.0
|
||||
github.com/rs/zerolog v1.29.1
|
||||
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef
|
||||
github.com/stretchr/testify v1.8.4
|
||||
github.com/texttheater/golang-levenshtein v1.0.1
|
||||
github.com/tylermmorton/tmpl v0.0.0-20231025031313-5552ee818c6d
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
|
||||
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b
|
||||
golang.org/x/image v0.14.0
|
||||
mvdan.cc/xurls/v2 v2.5.0
|
||||
)
|
||||
@@ -61,6 +64,7 @@ require (
|
||||
github.com/puzpuzpuz/xsync/v2 v2.5.1 // indirect
|
||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
|
||||
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a // indirect
|
||||
github.com/shopspring/decimal v1.3.1 // indirect
|
||||
github.com/tidwall/gjson v1.17.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
@@ -69,6 +73,9 @@ require (
|
||||
go.opencensus.io v0.24.0 // indirect
|
||||
golang.org/x/net v0.17.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
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
replace github.com/go-text/typesetting => github.com/fiatjaf/typesetting v0.0.0-20231228183257-7c3f6f5a0ccc
|
||||
|
||||
27
go.sum
27
go.sum
@@ -7,8 +7,6 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/
|
||||
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
||||
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||
github.com/apatters/go-wordwrap v1.0.0 h1:G6ni4Pt7/I4ED+A5ZvsK8e9XwETD04veDKxEL2QN830=
|
||||
github.com/apatters/go-wordwrap v1.0.0/go.mod h1:3sM7HcArQ+utXnjDQ4d1xjrd8b/wbKuDr/RmbiHgjwI=
|
||||
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
|
||||
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
|
||||
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
|
||||
@@ -72,10 +70,14 @@ github.com/fiatjaf/eventstore v0.3.3 h1:yZgHecBwHCVU+FuLgwz3gJJu27Xq4P8CNld37mQi
|
||||
github.com/fiatjaf/eventstore v0.3.3/go.mod h1:iqGHNXOMz+ztLHXxG13WBXgz/2bG0q/p1L3olof4dYo=
|
||||
github.com/fiatjaf/khatru v0.2.1 h1:NlgjBYH7iJpjFyOJVNEX/E2I1v4d5+KINhA+VxgDr4o=
|
||||
github.com/fiatjaf/khatru v0.2.1/go.mod h1:DsiQEmQmb6/hTXV6/OMcF7C7h19u1tJG5zAgaQVjseY=
|
||||
github.com/fiatjaf/typesetting v0.0.0-20231228183257-7c3f6f5a0ccc h1:8QpOKCVr8jpuvpmLCZUnsZ50faseCym2r6f5crpODKM=
|
||||
github.com/fiatjaf/typesetting v0.0.0-20231228183257-7c3f6f5a0ccc/go.mod h1:d22AnmeKq/on0HNv73UFriMKc4Ez6EqZAofLhAzpSzI=
|
||||
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/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/go-text/typesetting-utils v0.0.0-20231211103740-d9332ae51f04 h1:zBx+p/W2aQYtNuyZNcTfinWvXBQwYtDfme051PR/lAY=
|
||||
github.com/go-text/typesetting-utils v0.0.0-20231211103740-d9332ae51f04/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
|
||||
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/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
||||
@@ -138,8 +140,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
|
||||
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
|
||||
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
|
||||
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||
@@ -150,6 +152,8 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
|
||||
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
|
||||
github.com/nbd-wtf/emoji v0.0.3 h1:YtkT7MVPXvqU1SQjvC/CShlWexnREzqNCxmhUnL00CA=
|
||||
github.com/nbd-wtf/emoji v0.0.3/go.mod h1:tS6D9iI34qwBmWc5g8X7tVDkWXulqbTJRsvsM6QsS88=
|
||||
github.com/nbd-wtf/go-nostr v0.27.3 h1:u9fdP5h+Ap3LcDFD2j6F2buU/xOM9ddMY0LCDcC6ZyY=
|
||||
github.com/nbd-wtf/go-nostr v0.27.3/go.mod h1:e5WOFsKEpslDOxIgK00NqemH7KuAvKIW6sBXeJYCfUs=
|
||||
github.com/nbd-wtf/nostr-sdk v0.0.4 h1:vMCiYpFElKMHPXpZjFVEq4utoTLdTYbkqXVYH1/4uzs=
|
||||
@@ -167,6 +171,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/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/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/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -174,6 +180,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/puzpuzpuz/xsync/v2 v2.5.1 h1:mVGYAvzDSu52+zaGyNjC+24Xw2bQi3kTr4QJ6N9pIIU=
|
||||
github.com/puzpuzpuz/xsync/v2 v2.5.1/go.mod h1:gD2H2krq/w52MfPLE+Uy64TzJDVY7lP2znR9qmR35kU=
|
||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||
github.com/rs/cors v1.10.0 h1:62NOS1h+r8p1mW6FM0FSB0exioXLhd/sh15KpjWBZ+8=
|
||||
github.com/rs/cors v1.10.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
||||
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
@@ -183,10 +191,13 @@ 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/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/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
|
||||
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||
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.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
@@ -219,8 +230,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
|
||||
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
|
||||
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
|
||||
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
@@ -271,6 +282,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.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.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-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
|
||||
622
image_utils.go
Normal file
622
image_utils.go
Normal file
@@ -0,0 +1,622 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"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/nbd-wtf/emoji"
|
||||
"github.com/pemistahl/lingua-go"
|
||||
"github.com/srwiley/rasterx"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
const (
|
||||
nSupportedScripts = 14
|
||||
scaleShift = 6
|
||||
)
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
detector lingua.LanguageDetector
|
||||
scriptRanges []ScriptRange
|
||||
fontMap [nSupportedScripts]font.Face
|
||||
emojiFace font.Face
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
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.Hindi,
|
||||
lingua.Korean,
|
||||
).WithLowAccuracyMode().Build()
|
||||
|
||||
// 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")
|
||||
|
||||
// shaper stuff
|
||||
emojiFont = harfbuzz.NewFont(emojiFace)
|
||||
|
||||
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++ {
|
||||
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 := language.Language("en-us")
|
||||
lang, ok := detector.DetectLanguageOf(string(paragraph))
|
||||
if ok {
|
||||
lng = language.Language(lang.IsoCode639_1().String())
|
||||
} else {
|
||||
lng = defaultLanguageMap[idx]
|
||||
}
|
||||
|
||||
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 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) string {
|
||||
return urlMatcher.ReplaceAllStringFunc(text, func(match string) string {
|
||||
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) {
|
||||
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))
|
||||
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 ; emojiBuffer.Info[e].Codepoint != mainBuffer.Info[m].Codepoint; m++ {
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// convert the shaped text into an output
|
||||
glyphs := make([]shaping.Glyph, len(mainBuffer.Info))
|
||||
for i := 0; i < len(glyphs); i++ {
|
||||
var buf *harfbuzz.Buffer
|
||||
var font *harfbuzz.Font
|
||||
if emojiMask[i] {
|
||||
buf = emojiBuffer
|
||||
font = emojiFont
|
||||
} else {
|
||||
buf = mainBuffer
|
||||
font = mainFont
|
||||
}
|
||||
glyph := buf.Info[i]
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 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 drawShapedRunAt(
|
||||
img draw.Image,
|
||||
fontSize int,
|
||||
clr color.Color,
|
||||
out shaping.Output,
|
||||
emojiMask []bool,
|
||||
maskBaseIndex int,
|
||||
startX,
|
||||
startY int,
|
||||
) (charsWritten int, endingX int) {
|
||||
scale := float32(fontSize) / float32(out.Face.Upem())
|
||||
|
||||
b := img.Bounds()
|
||||
scanner := rasterx.NewScannerGV(b.Dx(), b.Dy(), img, b)
|
||||
f := rasterx.NewFiller(b.Dx(), b.Dy(), scanner)
|
||||
f.SetColor(clr)
|
||||
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())
|
||||
}
|
||||
|
||||
data := face.GlyphData(g.GlyphID)
|
||||
switch format := data.(type) {
|
||||
case api.GlyphOutline:
|
||||
drawOutline(g, format, f, currentScale, xPos, yPos)
|
||||
case nil:
|
||||
continue
|
||||
default:
|
||||
panic("format not supported for glyph")
|
||||
}
|
||||
|
||||
charsWritten++
|
||||
x += fixed266ToFloat(g.XAdvance)
|
||||
}
|
||||
f.Draw()
|
||||
|
||||
return charsWritten, int(math.Ceil(float64(x)))
|
||||
}
|
||||
|
||||
// this draws a font glyph (i.e. a letter) according to instructions and scale and whatever
|
||||
func drawOutline(g shaping.Glyph, 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))
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
||||
// image rendering stuff
|
||||
initializeImageDrawingStuff()
|
||||
|
||||
// eventstore and internal db
|
||||
deinitCache := initCache()
|
||||
defer deinitCache()
|
||||
|
||||
|
||||
408
render_image.go
408
render_image.go
@@ -2,34 +2,26 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"math"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/apatters/go-wordwrap"
|
||||
"github.com/fogleman/gg"
|
||||
"github.com/go-text/typesetting/shaping"
|
||||
"github.com/golang/freetype/truetype"
|
||||
sdk "github.com/nbd-wtf/nostr-sdk"
|
||||
"github.com/nfnt/resize"
|
||||
"golang.org/x/image/font"
|
||||
xfont "golang.org/x/image/font"
|
||||
)
|
||||
|
||||
const (
|
||||
MAX_LINES = 20
|
||||
MAX_CHARS_PER_LINE = 50
|
||||
MAX_CHARS_PER_QUOTE_LINE = 46
|
||||
FONT_SIZE = 7
|
||||
FONT_DPI = 260
|
||||
|
||||
BLOCK = "|"
|
||||
)
|
||||
|
||||
@@ -42,6 +34,8 @@ var (
|
||||
//go:embed fonts/*
|
||||
var fonts embed.FS
|
||||
|
||||
var multiNewlineRe = regexp.MustCompile(`\n\n+`)
|
||||
|
||||
func renderImage(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
|
||||
|
||||
@@ -57,27 +51,21 @@ func renderImage(w http.ResponseWriter, r *http.Request) {
|
||||
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", -1)
|
||||
content := data.event.Content
|
||||
content = strings.Replace(content, "\r\n", "\n", -1)
|
||||
content = multiNewlineRe.ReplaceAllString(content, "\n\n")
|
||||
content = strings.Replace(content, "\t", " ", -1)
|
||||
content = strings.Replace(content, "\r", "", -1)
|
||||
content = shortenURLs(content)
|
||||
|
||||
// this turns the raw event.Content into a series of lines ready to drawn
|
||||
lines := normalizeText(
|
||||
replaceUserReferencesWithNames(r.Context(),
|
||||
renderQuotesAsBlockPrefixedText(r.Context(),
|
||||
content,
|
||||
),
|
||||
paragraphs := replaceUserReferencesWithNames(r.Context(),
|
||||
quotesAsBlockPrefixedText(r.Context(),
|
||||
strings.Split(content, "\n"),
|
||||
),
|
||||
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 {
|
||||
log.Printf("error writing image: %s", err)
|
||||
http.Error(w, "error writing image!", 500)
|
||||
@@ -89,137 +77,16 @@ func renderImage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if err := png.Encode(w, img); err != nil {
|
||||
log.Printf("error encoding image: %s", err)
|
||||
http.Error(w, "error encoding image!", 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeText(input []string, breakWords bool) []string {
|
||||
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) {
|
||||
func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, date string) (image.Image, error) {
|
||||
width := 700
|
||||
height := 525
|
||||
paddingLeft := 25
|
||||
barExtraPadding := 5
|
||||
barExtraPadding := 0
|
||||
gradientRectHeight := 140
|
||||
switch style {
|
||||
case StyleTelegram:
|
||||
paddingLeft += 10
|
||||
@@ -233,37 +100,38 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
||||
img.SetColor(BACKGROUND)
|
||||
img.Clear()
|
||||
img.SetColor(FOREGROUND)
|
||||
|
||||
// main content text
|
||||
textImg := drawText(paragraphs, width-paddingLeft*2, height-20, true)
|
||||
img.DrawImage(textImg, paddingLeft, 20)
|
||||
|
||||
// font for writing the bottom bar stuff
|
||||
fontData, _ := fonts.ReadFile("fonts/NotoSans.ttf")
|
||||
ttf, _ := truetype.Parse(fontData)
|
||||
img.SetFontFace(truetype.NewFace(ttf, &truetype.Options{
|
||||
Size: FONT_SIZE,
|
||||
DPI: FONT_DPI,
|
||||
Hinting: font.HintingFull,
|
||||
Size: 6,
|
||||
DPI: 260,
|
||||
Hinting: xfont.HintingFull,
|
||||
}))
|
||||
|
||||
// Draw note text
|
||||
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
|
||||
// black bar at the bottom
|
||||
barHeight := 70
|
||||
img.SetColor(BAR_BACKGROUND)
|
||||
img.DrawRectangle(0, float64(height-barHeight), float64(width), float64(barHeight))
|
||||
img.Fill()
|
||||
|
||||
// Create a rectangle at the bottom with a gradient from black to transparent
|
||||
gradientRectHeight := 140
|
||||
gradientRectY := height - barHeight - gradientRectHeight
|
||||
for y := 0; y < gradientRectHeight; y++ {
|
||||
alpha := uint8(255 * (math.Pow(float64(y)/float64(gradientRectHeight), 2)))
|
||||
img.SetRGBA255(int(BACKGROUND.R), int(BACKGROUND.G), int(BACKGROUND.B), int(alpha))
|
||||
img.DrawRectangle(0, float64(gradientRectY+y), float64(width), 1)
|
||||
img.Fill()
|
||||
// a rectangle at the bottom with a gradient from black to transparent
|
||||
if len(strings.Join(paragraphs, "\n")) > 141 {
|
||||
gradientRectY := height - barHeight - gradientRectHeight
|
||||
for y := 0; y < gradientRectHeight; y++ {
|
||||
alpha := uint8(255 * (math.Pow(float64(y)/float64(gradientRectHeight), 2)))
|
||||
img.SetRGBA255(int(BACKGROUND.R), int(BACKGROUND.G), int(BACKGROUND.B), int(alpha))
|
||||
img.DrawRectangle(0, float64(gradientRectY+y), float64(width), 1)
|
||||
img.Fill()
|
||||
}
|
||||
}
|
||||
|
||||
// Draw author's image from URL
|
||||
// draw author's name
|
||||
authorTextX := paddingLeft + barExtraPadding
|
||||
if metadata.Picture != "" {
|
||||
authorImage, err := fetchImageFromURL(metadata.Picture)
|
||||
@@ -273,14 +141,13 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
||||
authorTextX += 65
|
||||
}
|
||||
}
|
||||
|
||||
// Draw author's name
|
||||
authorTextY := height - barHeight + 20
|
||||
authorTextY := height - barHeight + 15
|
||||
authorMaxWidth := width/2.0 - paddingLeft*2 - barExtraPadding
|
||||
img.SetColor(color.White)
|
||||
img.DrawStringWrapped(metadata.ShortName(), float64(authorTextX), float64(authorTextY), 0, 0, float64(width*99), 99, gg.AlignLeft)
|
||||
textImg = drawText([]string{metadata.ShortName()}, width, barHeight, false)
|
||||
img.DrawImage(textImg, authorTextX, authorTextY)
|
||||
|
||||
// Create a gradient to cover too long names
|
||||
// a gradient to cover too long names
|
||||
img.SetColor(BAR_BACKGROUND)
|
||||
img.DrawRectangle(float64(authorTextX+authorMaxWidth), float64(height-barHeight), float64(width-authorTextX-authorMaxWidth), float64(barHeight))
|
||||
gradientLenght := 60
|
||||
@@ -291,7 +158,7 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
||||
img.Fill()
|
||||
}
|
||||
|
||||
// Draw the logo
|
||||
// bottom bar logo
|
||||
logo, _ := static.ReadFile("static/logo.png")
|
||||
stampImg, _ := png.Decode(bytes.NewBuffer(logo))
|
||||
stampWidth := stampImg.Bounds().Dx()
|
||||
@@ -304,145 +171,70 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
|
||||
layout := "2006-01-02 15:04:05"
|
||||
parsedTime, _ := time.Parse(layout, date)
|
||||
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.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(height-barHeight+22), 0, 0, float64(240), 1.5, gg.AlignRight)
|
||||
|
||||
return img.Image(), nil
|
||||
}
|
||||
|
||||
// replace nevent and note with their text, as an extra line prefixed by BLOCK
|
||||
// this returns a slice of lines
|
||||
func renderQuotesAsBlockPrefixedText(ctx context.Context, input string) []string {
|
||||
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
|
||||
defer cancel()
|
||||
func drawText(paragraphs []string, width, height int, dynamicResize bool) image.Image {
|
||||
FONT_SIZE := 25
|
||||
color := color.RGBA{R: 255, G: 230, B: 238, A: 255}
|
||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
|
||||
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 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
|
||||
}
|
||||
joinedContent := strings.Join(paragraphs, "\n")
|
||||
if dynamicResize && len(joinedContent) < 141 {
|
||||
FONT_SIZE = 7
|
||||
img := gg.NewContext(width, height)
|
||||
fontData, _ := fonts.ReadFile("fonts/NotoSans.ttf")
|
||||
ttf, _ := truetype.Parse(fontData)
|
||||
i := 1
|
||||
lineSpacing := 1.2
|
||||
for i < 20 {
|
||||
FONT_SIZE += i
|
||||
img.SetFontFace(truetype.NewFace(ttf, &truetype.Options{
|
||||
Size: float64(FONT_SIZE),
|
||||
DPI: 260,
|
||||
}))
|
||||
wrappedContent := strings.Join(img.WordWrap(joinedContent, float64(width-120)), "\n")
|
||||
_, checkHeight := img.MeasureMultilineString(wrappedContent, lineSpacing)
|
||||
if checkHeight > float64(height-70-60*2) {
|
||||
FONT_SIZE -= 1
|
||||
break
|
||||
}
|
||||
rune32 := uint32(rune)
|
||||
for _, r32 := range group.lang.R32 {
|
||||
if rune32 >= r32.Lo && rune32 <= r32.Hi {
|
||||
fontName = group.fontName
|
||||
shouldBreakWords = group.breakWords
|
||||
goto gotLang
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
FONT_SIZE = FONT_SIZE*4 - 2
|
||||
}
|
||||
|
||||
lineNumber := 1
|
||||
for _, paragraph := range paragraphs {
|
||||
rawText := []rune(paragraph)
|
||||
|
||||
shapedRunes, emojiMask := shapeText(rawText, FONT_SIZE)
|
||||
|
||||
var wrapper shaping.LineWrapper
|
||||
it := shaping.NewSliceIterator([]shaping.Output{shapedRunes})
|
||||
lines, _ := wrapper.WrapParagraph(shaping.WrapConfig{}, width, rawText, it)
|
||||
|
||||
totalCharsWritten := 0
|
||||
for _, line := range lines {
|
||||
for _, out := range line {
|
||||
charsWritten, _ := drawShapedRunAt(
|
||||
img,
|
||||
FONT_SIZE,
|
||||
color,
|
||||
out,
|
||||
emojiMask,
|
||||
totalCharsWritten,
|
||||
0,
|
||||
FONT_SIZE*lineNumber*12/10,
|
||||
)
|
||||
totalCharsWritten += charsWritten
|
||||
lineNumber++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
return img
|
||||
}
|
||||
|
||||
@@ -10,3 +10,6 @@ naddr1qqwyummnw3ez64r9vd5z64m9v44kc7fdxgcryvedxycj6vfeqgszak7w562dzerznp222fvrgk
|
||||
nevent1qqsy05v33j3w6klkfhy6taud0d7g7n6e7z9mt0z20aenqfam3lzywrcpzpmhxue69uhnzdps9enrw73wd9hsyg9vxs9ql7jgtz0f3tu9wa65220gck0hl5gs7x7gxtgw34xy60gc2vg30mg0
|
||||
nevent1qqszl72lntw6qdx2dc0fet9yrjpxlvh98ww7w3egm5ey5h0dwwzjg2gpzpmhxue69uhnzdps9enrw73wd9hsygyxl5wgpsraaw7r6xffxaajf49lvk594ung4u2umg4veez5manshc4w8pdp
|
||||
nevent1qqsyxahlr82z786vyhg0u2ycx46wrwwz5tap66udeffxle7phduthmspzpmhxue69uhnzdps9enrw73wd9hsygpcrk7vwyuw4wd8r6q5c4ur0jwky06qxmkqys80xq3nq6z0lj9n3u9zd3r4
|
||||
nevent1qqstj4gq2q9utuazaagzx0cd0n3jlm97p2084rfrvw2f8ytfa8ec2qgpp4mhxue69uhhjctzw5hx6egzypmezxyxju0727078e8epkwu06gle46g2rwzs5mgrll3we2tyxqfzuwew5q
|
||||
nevent1qqs9jhjm7cy3gtwlw3zvgrcras8nkvjs8gz0cvnmepmed4k92z8uxgspzpmhxue69uhkummnw3ezuamfdejsygy0mgsen3pen7khlaqjddqz7867u9tlm0jvp9g7vn07xx4wepml7yux7e7e
|
||||
nevent1qqsq9kxxrsmck2sj5gdu04llcjku5ex65eqyu5ec4a9nyr4ymudezegpp4mhxue69uhkummn9ekx7mqppamhxue69uhkummnw3ezumt0d5q3gamnwvaz7tmwdaehgu3wdau8gu3wv3jhvqgcwaehxw309ac82cnvd93juun9d3shj6twvuhxjmcpz3mhxue69uhhyetvv9ujumn0wd68ytnzvuq3samnwvaz7tmjv4kxz7fwdehhxamgv4ex2tnrdaksygqzm86kwmllcvull62dl2ech6lzrns303h32zwejg4g94z57ssd5gukzwe4
|
||||
23
utils.go
23
utils.go
@@ -504,3 +504,26 @@ func isntRealRelay(url string) bool {
|
||||
substr := []byte(url[6:])
|
||||
return bytes.IndexByte(substr, '/') != -1
|
||||
}
|
||||
|
||||
func maxIndex(slice []int) int {
|
||||
maxIndex := -1
|
||||
maxVal := 0
|
||||
for i, val := range slice {
|
||||
if val > maxVal {
|
||||
maxVal = val
|
||||
maxIndex = i
|
||||
}
|
||||
}
|
||||
return maxIndex
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user