Better handling of character length and image height

This commit is contained in:
Marcus Noble 2021-04-09 21:33:42 +01:00
parent 79d7d64e98
commit 80cabdd365
6 changed files with 155 additions and 12 deletions

View File

@ -39,7 +39,7 @@ docker-publish:
.PHONY: run # Run the application .PHONY: run # Run the application
run: run:
@go run main.go @go run .
.PHONY: ci # Perform CI specific tasks to perform on a pull request .PHONY: ci # Perform CI specific tasks to perform on a pull request
ci: ci:

109
chars.go Normal file
View File

@ -0,0 +1,109 @@
package main
import emoji "github.com/tmdvs/Go-Emoji-Utils"
var charWidths = map[string]float64{
" ": 4.9,
"0": 14.333328247070312,
"1": 10.583328247070312,
"2": 13.433334350585938,
"3": 14.01666259765625,
"4": 14.333328247070312,
"5": 13.866668701171875,
"6": 14.199996948242188,
"7": 12.966659545898438,
"8": 14.199996948242188,
"9": 14.199996948242188,
"A": 14.949996948242188,
"B": 14.366668701171875,
"C": 16.183334350585938,
"D": 16.050003051757812,
"E": 13.133331298828125,
"F": 12.566665649414062,
"G": 16.666671752929688,
"H": 16.51666259765625,
"I": 5.600006103515625,
"J": 11.816665649414062,
"K": 14.583328247070312,
"L": 12.5,
"M": 19.550003051757812,
"N": 16.51666259765625,
"O": 17.25,
"P": 13.75,
"Q": 17.25,
"R": 14.23333740234375,
"S": 14.083328247070312,
"T": 13.816665649414062,
"U": 16.433334350585938,
"V": 14.949996948242188,
"W": 21.683334350585938,
"X": 15.033340454101562,
"Y": 14.51666259765625,
"Z": 14.616668701171875,
"a": 11.98333740234375,
"b": 13.216659545898438,
"c": 11.98333740234375,
"d": 13.199996948242188,
"e": 12.25,
"f": 7.4499969482421875,
"g": 13.100006103515625,
"h": 12.850006103515625,
"i": 5.133331298828125,
"j": 5.133331298828125,
"k": 11.633331298828125,
"l": 5.133331298828125,
"m": 18.966659545898438,
"n": 12.583328247070312,
"o": 12.683334350585938,
"p": 13.100006103515625,
"q": 13.100006103515625,
"r": 7.5833282470703125,
"s": 11.166671752929688,
"t": 7.4166717529296875,
"u": 12.583328247070312,
"v": 11.583328247070312,
"w": 16.916671752929688,
"x": 11.25,
"y": 11.649993896484375,
"z": 11.26666259765625,
"!": 6.5666656494140625,
"@": 20.550003051757812,
"£": 14.333328247070312,
"#": 14.300003051757812,
"$": 14.333328247070312,
"%": 18.416671752929688,
"^": 9.683334350585938,
"*": 9.683334350585938,
"(": 7.883331298828125,
")": 7.883331298828125,
"-": 10.300003051757812,
"_": 9.73333740234375,
"=": 14.333328247070312,
"+": 14.333328247070312,
"[": 7.883331298828125,
"]": 7.883331298828125,
"{": 7.883331298828125,
"}": 7.883331298828125,
";": 6.26666259765625,
":": 6.26666259765625,
"'": 5.26666259765625,
"\"": 9.5,
",": 6.26666259765625,
".": 6.26666259765625,
"/": 7.76666259765625,
"?": 11.649993896484375,
"`": 11.833328247070312,
}
func getCharWidth(char string) float64 {
width, ok := charWidths[char]
if !ok {
if len(emoji.FindAll(char)) > 0 {
width = 24
} else {
width = 8
}
}
return width
}

1
go.mod
View File

@ -11,5 +11,6 @@ require (
github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17 // indirect github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17 // indirect
github.com/grokify/html-strip-tags-go v0.0.1 github.com/grokify/html-strip-tags-go v0.0.1
github.com/joho/godotenv v1.3.0 github.com/joho/godotenv v1.3.0
github.com/tmdvs/Go-Emoji-Utils v1.1.0
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
) )

2
go.sum
View File

@ -14,6 +14,8 @@ github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q
github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78= github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/tmdvs/Go-Emoji-Utils v1.1.0 h1:gtPix7HZPrd49+MNDcuRLvv4xVNxCE5wgjqyuvmbyYg=
github.com/tmdvs/Go-Emoji-Utils v1.1.0/go.mod h1:J82i2WeGn+Kz+T3s5v9+i/OJlvevIVfGZ6qXgqiNWBc=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew= golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

50
main.go
View File

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"log" "log"
"math"
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
@ -125,20 +124,51 @@ func getTweet(w http.ResponseWriter, r *http.Request) {
return template.HTML(in) return template.HTML(in)
}, },
"calculateHeight": func(tweet anaconda.Tweet) string { "calculateHeight": func(tweet anaconda.Tweet) string {
height := 205.0 height := 64.0 /* Avatar */ + 20 /* footer */ + 46 /* test margin */ + 32 /* margin */
lines := math.Floor(float64(len(strip.StripTags(tweet.FullText))) / 40) lineWidth := 0.0
height += lines * 20 tweetText := strings.ReplaceAll(tweet.FullText, "<br /><br />", " \n ")
tweetText = strip.StripTags(tweetText)
words := strings.Split(tweetText, " ")
for _, word := range words {
if strings.Contains(word, "\n") {
height += 28
lineWidth = 0
continue
}
if tweet.InReplyToScreenName != "" { chars := strings.Split(word, "")
height += 45 wordWidth := 0.0
for _, char := range chars {
wordWidth += getCharWidth(char)
}
if lineWidth+wordWidth > 443 {
height += 28
lineWidth = wordWidth
} else {
lineWidth += wordWidth
}
}
if lineWidth > 0 {
height += 28
} }
height += float64(strings.Count(tweet.FullText, "<br />") * 20) if tweet.InReplyToScreenName != "" {
height += 34
}
for _, pic := range tweet.ExtendedEntities.Media { height += float64(strings.Count(tweet.FullText, "<br /><br />") * 28)
ratio := float64(pic.Sizes.Small.W) / 464
height += float64(pic.Sizes.Small.H) / ratio if len(tweet.ExtendedEntities.Media) >= 1 {
ratio := float64(tweet.ExtendedEntities.Media[0].Sizes.Small.W) / 464
height += (float64(tweet.ExtendedEntities.Media[0].Sizes.Small.H) / ratio) + 5
}
if len(tweet.ExtendedEntities.Media) > 1 {
for i := range tweet.ExtendedEntities.Media {
tweet.ExtendedEntities.Media[i].Sizes.Small.W = 225
}
} }
return fmt.Sprintf("%dpx", int64(height)) return fmt.Sprintf("%dpx", int64(height))

View File

@ -14,6 +14,7 @@
.media-tweetsvg{border-radius:2%; max-width:100%;border-radius: 2%; border-style: solid; border-width: .1em; border-color: #ddd;} .media-tweetsvg{border-radius:2%; max-width:100%;border-radius: 2%; border-style: solid; border-width: .1em; border-color: #ddd;}
time.tweetsvg{font-size:15px;margin:0;margin-left: 2px;padding-bottom:1rem;color:rgb(101, 119, 134);text-decoration:none;} time.tweetsvg{font-size:15px;margin:0;margin-left: 2px;padding-bottom:1rem;color:rgb(101, 119, 134);text-decoration:none;}
.tweetsvg.reply{font-size:15px;color:rgb(110, 118, 125);} .tweetsvg.reply{font-size:15px;color:rgb(110, 118, 125);}
.tweetsvg.footer{display:block;}
</style> </style>
<blockquote class="tweetsvg" xmlns="http://www.w3.org/1999/xhtml"> <blockquote class="tweetsvg" xmlns="http://www.w3.org/1999/xhtml">
<a rel="noopener" target="_blank" class="tweetsvg" href="https://twitter.com/{{ .User.ScreenName }}/"><img class="avatar-tweetsvg" alt="" src="data:image/jpeg;base64,{{ base64 .User.ProfileImageUrlHttps }}" /></a> <a rel="noopener" target="_blank" class="tweetsvg" href="https://twitter.com/{{ .User.ScreenName }}/"><img class="avatar-tweetsvg" alt="" src="data:image/jpeg;base64,{{ base64 .User.ProfileImageUrlHttps }}" /></a>
@ -36,7 +37,7 @@
{{ end }} {{ end }}
{{ end }} {{ end }}
<a rel="noopener" target="_blank" class="tweetsvg" href="https://twitter.com/{{ .User.ScreenName }}/status/{{ .Id }}"> <a rel="noopener" target="_blank" class="tweetsvg footer" href="https://twitter.com/{{ .User.ScreenName }}/status/{{ .Id }}">
<time class="tweetsvg" datetime="{{ isoDate .CreatedAt }}">{{ humanDate .CreatedAt }}</time> <time class="tweetsvg" datetime="{{ isoDate .CreatedAt }}">{{ humanDate .CreatedAt }}</time>
</a> </a>
</blockquote> </blockquote>

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB