-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
191 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package dev.triumphteam.backend.banner | ||
|
||
import java.awt.Font | ||
import java.awt.RenderingHints | ||
import java.awt.image.BufferedImage | ||
import java.io.File | ||
import javax.imageio.ImageIO | ||
|
||
|
||
public class BannerMaker { | ||
|
||
private val template = | ||
requireNotNull(BannerMaker::class.java.classLoader.getResourceAsStream("board/board-template.png")) { | ||
"Could not find banner template!" | ||
} | ||
|
||
private val templateImage = ImageIO.read(template) | ||
private val fontRegular = resourceFont("board/poppins.ttf") | ||
private val fontBold = resourceFont("board/bold-poppins.ttf") | ||
|
||
public fun create( | ||
icon: BufferedImage, | ||
group: String, | ||
title: String?, | ||
subTitle: String?, | ||
output: File, | ||
) { | ||
|
||
val image = templateImage.deepCopy() | ||
val graphics = image.createGraphics() | ||
|
||
graphics.apply { | ||
// Change the quality of the strings rendered | ||
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) } | ||
|
||
// Then the rest | ||
font = fontRegular.deriveFont(30f) | ||
drawString(group, 66, 359) | ||
subTitle?.let { drawString(it, 66, 479) } | ||
|
||
// Then the icon | ||
drawImage(icon, 712, 170, null) | ||
|
||
// Finally dispose | ||
dispose() | ||
} | ||
|
||
ImageIO.write(image, "png", output) | ||
} | ||
|
||
private fun resourceFont(path: String): Font { | ||
val input = requireNotNull(BannerMaker::class.java.classLoader.getResourceAsStream(path)) { | ||
"Could not find font on path '$path'!" | ||
} | ||
|
||
return requireNotNull(Font.createFont(Font.PLAIN, input)) { | ||
"Could not create font on path '$path'!" | ||
} | ||
} | ||
|
||
public fun BufferedImage.deepCopy(): BufferedImage { | ||
val cm = colorModel | ||
val isAlphaPremultiplied = cm.isAlphaPremultiplied | ||
val raster = copyData(null) | ||
return BufferedImage(cm, raster, isAlphaPremultiplied, null) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.triumphteam.website.docs.markdown | ||
|
||
import org.commonmark.node.AbstractVisitor | ||
import org.commonmark.node.HtmlBlock | ||
import org.commonmark.node.Node | ||
|
||
private val headerPattern = "(?<=<center><h1>)[^<]+(?=</h1></center>)".toRegex() | ||
private val paragraphPattern = "(?<=<center><p>)[^<]+(?=</p></center>)".toRegex() | ||
|
||
public class BannerDataRenderer : AbstractVisitor() { | ||
|
||
private var header: String? = null | ||
private var paragraph: String? = null | ||
|
||
public fun render(node: Node): Pair<String?, String?> { | ||
node.accept(this) | ||
|
||
return header to paragraph | ||
} | ||
|
||
override fun visit(block: HtmlBlock) { | ||
val literal = block.literal | ||
|
||
val headerMatch = headerPattern.find(literal) | ||
if (headerMatch != null) { | ||
if (header != null) return | ||
header = headerMatch.value | ||
return | ||
} | ||
|
||
val paragraphMatch = headerPattern.find(literal) ?: return | ||
if (paragraph != null) return | ||
paragraph = paragraphMatch.value | ||
} | ||
} |