Skip to content

Commit

Permalink
chore: Yolo
Browse files Browse the repository at this point in the history
  • Loading branch information
LichtHund committed Jul 7, 2024
1 parent 54453fc commit a777ea4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
25 changes: 17 additions & 8 deletions backend/src/main/kotlin/banner/BannerMaker.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.triumphteam.backend.banner

import dev.triumphteam.website.splitSentence
import dev.triumphteam.website.trimAround
import java.awt.Font
import java.awt.RenderingHints
import java.awt.image.BufferedImage
Expand Down Expand Up @@ -31,19 +33,26 @@ public class BannerMaker {

graphics.apply {
// Change the quality of the strings rendered
setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,
)
setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB)

// First write the bold part
font = fontBold.deriveFont(64f)
title?.let { drawString(it, 66, 429) }
font = fontBold.deriveFont(45f)
title?.let { drawString(it.trimAround(contextLength = 20), 66, 429) }

// Then the rest
font = fontRegular.deriveFont(30f)
font = fontRegular.deriveFont(20f)
drawString(group, 66, 359)
subTitle?.let { drawString(it, 66, 479) }
subTitle?.let {
var location = 479
val lineSize = 50

val lines = it.splitSentence(maxLength = 50)

lines.forEach { line ->
drawString(line, 66, location)
location += lineSize
}
}

// Then the icon
drawImage(icon, 712, 170, null)
Expand Down
25 changes: 25 additions & 0 deletions common/src/main/kotlin/StringExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,28 @@ public fun String.highlightWord(words: List<String>): String {
"<b>${it.value}</b>"
}
}

public fun String.splitSentence(maxLength: Int = 40): List<String> {
val words = split(" ")
val lines = mutableListOf<String>()
var currentLine = mutableListOf<String>()

for (word in words) {
// Check if adding the next word would exceed the max length
if (currentLine.sumOf { it.length } + currentLine.size + word.length <= maxLength) {
currentLine.add(word)
} else {
// Join the current line into a string and add to lines
lines.add(currentLine.joinToString(" "))
// Start a new line with the current word
currentLine = mutableListOf(word)
}
}

// Add the last line to lines
if (currentLine.isNotEmpty()) {
lines.add(currentLine.joinToString(" "))
}

return lines
}

0 comments on commit a777ea4

Please sign in to comment.