Skip to content

Commit

Permalink
Fix char size calculation of yRange for bitmap providers
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Mar 29, 2024
1 parent d845407 commit bbc256c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package xyz.xenondevs.nova.data.resources.builder.font.provider.bitmap

import org.joml.Vector2i
import org.joml.Vector2ic
import xyz.xenondevs.nova.util.data.ImageUtils
import java.awt.image.BufferedImage

Expand All @@ -20,16 +22,16 @@ interface BitmapGlyphImageType<T> {
* The values in the returned Pair<Top, Bottom> correspond with the
* y-coordinate of the first non-empty row from the top and bottom side of the image.
*
* @return An integer pair containing the top and bottom borders, or null if the image is completely empty.
* @return A vector containing the top and bottom borders, or null if the image is completely empty.
*/
fun findHorizontalBorders(image: T, width: Int, height: Int): Pair<Int, Int>?
fun findTopBottomBorders(image: T, width: Int, height: Int): Vector2ic?

companion object {

/**
* A glyph texture represented by a one-dimensional array of argb integers.
*/
val INT_ARRAY: BitmapGlyphImageType<IntArray> = object : BitmapGlyphImageType<IntArray> {
val ARGB_ARRAY: BitmapGlyphImageType<IntArray> = object : BitmapGlyphImageType<IntArray> {

// TODO: consider moving these to ImageUtils as well?
override fun findRightBorder(image: IntArray, width: Int, height: Int): Int? {
Expand All @@ -46,7 +48,7 @@ interface BitmapGlyphImageType<T> {
return if (x != -1) x else null
}

override fun findHorizontalBorders(image: IntArray, width: Int, height: Int): Pair<Int, Int>? {
override fun findTopBottomBorders(image: IntArray, width: Int, height: Int): Vector2ic? {
fun isRowEmpty(y: Int): Boolean {
for (x in 0..<width) {
if (image[y * width + x] ushr 24 != 0)
Expand All @@ -62,7 +64,7 @@ interface BitmapGlyphImageType<T> {
var bottom = height - 1
while (bottom > top && isRowEmpty(bottom)) bottom--

return top to bottom
return Vector2i(top, bottom)
}

}
Expand All @@ -72,7 +74,7 @@ interface BitmapGlyphImageType<T> {
*/
val BUFFERED_IMAGE: BitmapGlyphImageType<BufferedImage> = object : BitmapGlyphImageType<BufferedImage> {
override fun findRightBorder(image: BufferedImage, width: Int, height: Int): Int? = ImageUtils.findRightBorder(image)
override fun findHorizontalBorders(image: BufferedImage, width: Int, height: Int): Pair<Int, Int>? = ImageUtils.findHorizontalBorders(image)
override fun findTopBottomBorders(image: BufferedImage, width: Int, height: Int): Vector2ic? = ImageUtils.findTopBottomBorders(image)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ abstract class BitmapProvider<T> internal constructor() : FontProvider() {

var minY = 0f
var maxY = 0f
val horizontalBorders = glyphImageType.findHorizontalBorders(glyph, glyphWidth, glyphHeight)
val horizontalBorders = glyphImageType.findTopBottomBorders(glyph, glyphWidth, glyphHeight)
if (horizontalBorders != null) {
minY = ((minY - ascent) * rescale)
maxY = ((maxY - ascent) * rescale)
minY = ((horizontalBorders.x() - ascent) * rescale)
maxY = ((horizontalBorders.y() - ascent) * rescale)
}

map.put(codePoint, floatArrayOf(width, minY, maxY))
Expand Down Expand Up @@ -239,7 +239,7 @@ abstract class BitmapProvider<T> internal constructor() : FontProvider() {
*/
@JvmName("custom1")
fun custom(file: ResourcePath, codePointGrid: CodePointGrid, glyphGrid: GlyphGrid<IntArray>, height: Int, ascent: Int): BitmapProvider<IntArray> =
Custom(BitmapGlyphImageType.INT_ARRAY, file, codePointGrid, glyphGrid, height, ascent)
Custom(BitmapGlyphImageType.ARGB_ARRAY, file, codePointGrid, glyphGrid, height, ascent)

/**
* Creates a new immutable [BitmapProvider] that references another one, but with a different ascent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ abstract class MutableBitmapProvider<T> : BitmapProvider<T>() {
*/
@JvmName("custom1")
fun custom(file: ResourcePath, codePointGrid: MutableCodePointGrid, glyphGrid: MutableGlyphGrid<IntArray>, height: Int, ascent: Int): MutableBitmapProvider<IntArray> =
Custom(BitmapGlyphImageType.INT_ARRAY, file, codePointGrid, glyphGrid, height, ascent)
Custom(BitmapGlyphImageType.ARGB_ARRAY, file, codePointGrid, glyphGrid, height, ascent)

/**
* Creates a new [BitmapProvider] with a single glyph from a texture.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package xyz.xenondevs.nova.util.data

import org.joml.Vector2i
import org.joml.Vector2ic
import java.awt.Point
import java.awt.image.BufferedImage
import java.awt.image.ColorModel
Expand Down Expand Up @@ -68,17 +70,17 @@ object ImageUtils {
* The values in the returned Pair<Top, Bottom> correspond with the
* y-coordinate of the first non-empty row from the top and bottom side of the image.
*
* @return An integer pair containing the top and bottom borders, or null if the image is completely empty.
* @return A vector containing the top and bottom borders, or null if the image is completely empty.
*/
fun findHorizontalBorders(image: BufferedImage): Pair<Int, Int>? {
fun findTopBottomBorders(image: BufferedImage): Vector2ic? {
var top = 0
while (top < image.height && isRowEmpty(image, top)) top++
if (top == image.height) return null

var bottom = image.height - 1
while (bottom > top && isRowEmpty(image, bottom)) bottom--

return top to bottom
return Vector2i(top, bottom)
}

/**
Expand Down

0 comments on commit bbc256c

Please sign in to comment.