From 497bf5e39cb4a8e36bb349d09a36207aac4a9875 Mon Sep 17 00:00:00 2001 From: Daniele Tonon Date: Tue, 2 Jan 2024 18:23:13 +0100 Subject: [PATCH 1/5] Update font-size calculation including height of text-image --- render_image.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/render_image.go b/render_image.go index c053c11..a3aa7ec 100644 --- a/render_image.go +++ b/render_image.go @@ -112,13 +112,13 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d // main content text addedSize := 0 - if np := len(paragraphs); np < 4 { + if np := len(paragraphs); np < 6 { nchars := 0 for _, par := range paragraphs { nchars += len([]rune(par)) } largeness := math.Pow(float64(nchars), 0.6) + math.Pow(float64(np-1), 0.8) - addedSize = int(240.0 / largeness) + addedSize = int(240.0 / largeness * math.Pow(float64(height/525), 2.2)) } textImg := drawText(paragraphs, fontSize+addedSize, width-paddingLeft*2, height-20) img.DrawImage(textImg, paddingLeft, 20) From e8d51410889711de6e74ab970284c461b6b4fec7 Mon Sep 17 00:00:00 2001 From: Daniele Tonon Date: Tue, 2 Jan 2024 18:29:55 +0100 Subject: [PATCH 2/5] Update logic to show the cut gradient S --- render_image.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/render_image.go b/render_image.go index a3aa7ec..a30dfcb 100644 --- a/render_image.go +++ b/render_image.go @@ -120,7 +120,7 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d largeness := math.Pow(float64(nchars), 0.6) + math.Pow(float64(np-1), 0.8) addedSize = int(240.0 / largeness * math.Pow(float64(height/525), 2.2)) } - textImg := drawText(paragraphs, fontSize+addedSize, width-paddingLeft*2, height-20) + textImg, overflowingText := drawText(paragraphs, fontSize+addedSize, width-paddingLeft*2, height-20) img.DrawImage(textImg, paddingLeft, 20) // font for writing the date @@ -139,7 +139,7 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d img.Fill() // a rectangle at the bottom with a gradient from black to transparent - if len(strings.Join(paragraphs, "\n")) > 141 { + if overflowingText { gradientRectY := height - barHeight - gradientRectHeight for y := 0; y < gradientRectHeight; y++ { alpha := uint8(255 * (math.Pow(float64(y)/float64(gradientRectHeight), 2))) @@ -162,7 +162,7 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d authorTextY := height - barHeight + 15 authorMaxWidth := width/2.0 - paddingLeft*2 - barExtraPadding img.SetColor(color.White) - textImg = drawText([]string{metadata.ShortName()}, fontSize, width, barHeight) + textImg, _ = drawText([]string{metadata.ShortName()}, fontSize, width, barHeight) img.DrawImage(textImg, authorTextX, authorTextY) // a gradient to cover too long names @@ -195,7 +195,7 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d return img.Image(), nil } -func drawText(paragraphs []string, fontSize int, width, height int) image.Image { +func drawText(paragraphs []string, fontSize int, width, height int) (image.Image, bool) { img := image.NewNRGBA(image.Rect(0, 0, width, height)) lineNumber := 1 @@ -233,5 +233,10 @@ func drawText(paragraphs []string, fontSize int, width, height int) image.Image } } - return img + overflow := false + if fontSize*lineNumber*12/10 > height { + overflow = true + } + + return img, overflow } From 772838717195f110958428a88cc811b6ad7a6dc4 Mon Sep 17 00:00:00 2001 From: Daniele Tonon Date: Wed, 3 Jan 2024 08:04:49 +0100 Subject: [PATCH 3/5] Update font-size calculation, again --- render_image.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/render_image.go b/render_image.go index a30dfcb..9b48870 100644 --- a/render_image.go +++ b/render_image.go @@ -112,13 +112,19 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d // main content text addedSize := 0 + zoom := 1.0 if np := len(paragraphs); np < 6 { nchars := 0 + blankLines := 0 for _, par := range paragraphs { nchars += len([]rune(par)) + if par == "" { + blankLines++ + } } - largeness := math.Pow(float64(nchars), 0.6) + math.Pow(float64(np-1), 0.8) - addedSize = int(240.0 / largeness * math.Pow(float64(height/525), 2.2)) + largeness := math.Pow(float64(nchars), 0.60) + math.Pow(float64(np-blankLines), 1) + math.Pow(float64(blankLines), 0.7) + zoom = float64(math.Pow(float64(height)/366.0-(float64(blankLines+1)/10), 1.2)) + addedSize = int(200.0 / largeness * zoom) } textImg, overflowingText := drawText(paragraphs, fontSize+addedSize, width-paddingLeft*2, height-20) img.DrawImage(textImg, paddingLeft, 20) From 3e7f755f69afe85d9c07b335b8eeb477332c96ff Mon Sep 17 00:00:00 2001 From: Daniele Tonon Date: Wed, 3 Jan 2024 08:05:42 +0100 Subject: [PATCH 4/5] Update cut gradient calculation, again --- render_image.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/render_image.go b/render_image.go index 9b48870..edfbb4a 100644 --- a/render_image.go +++ b/render_image.go @@ -96,6 +96,7 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d paddingLeft := 25 barExtraPadding := 0 gradientRectHeight := 140 + barHeight := 70 switch style { case StyleTelegram: paddingLeft += 10 @@ -126,7 +127,7 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d zoom = float64(math.Pow(float64(height)/366.0-(float64(blankLines+1)/10), 1.2)) addedSize = int(200.0 / largeness * zoom) } - textImg, overflowingText := drawText(paragraphs, fontSize+addedSize, width-paddingLeft*2, height-20) + textImg, overflowingText := drawText(paragraphs, int(float64(fontSize+addedSize)), width-paddingLeft*2, height-20-barHeight) img.DrawImage(textImg, paddingLeft, 20) // font for writing the date @@ -139,7 +140,6 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d })) // black bar at the bottom - barHeight := 70 img.SetColor(BAR_BACKGROUND) img.DrawRectangle(0, float64(height-barHeight), float64(width), float64(barHeight)) img.Fill() @@ -240,6 +240,7 @@ func drawText(paragraphs []string, fontSize int, width, height int) (image.Image } overflow := false + lineNumber-- if fontSize*lineNumber*12/10 > height { overflow = true } From 33d02097a156e0d0fbe024d304f0481cf3e8bfa6 Mon Sep 17 00:00:00 2001 From: Daniele Tonon Date: Wed, 3 Jan 2024 08:13:06 +0100 Subject: [PATCH 5/5] Optimize text rendering, avoiding useless prints --- render_image.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/render_image.go b/render_image.go index edfbb4a..bed2027 100644 --- a/render_image.go +++ b/render_image.go @@ -234,16 +234,14 @@ func drawText(paragraphs []string, fontSize int, width, height int) (image.Image fontSize*lineNumber*12/10, ) totalCharsWritten += charsWritten + + if fontSize*lineNumber*12/10 > height { + return img, true + } lineNumber++ } } } - overflow := false - lineNumber-- - if fontSize*lineNumber*12/10 > height { - overflow = true - } - - return img, overflow + return img, false }