Skip to content

Commit

Permalink
More correct font width.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Oct 7, 2024
1 parent 341ae9f commit e21fb2d
Show file tree
Hide file tree
Showing 7 changed files with 114,611 additions and 48,630 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public interface TextLayout extends Layout {
@NotNull
@Unmodifiable
Map<Character, Integer> charWidth();
Map<Integer, Integer> charWidth();

@NotNull
TextAlign align();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface HealthBarText extends HealthBarConfiguration {
@NotNull
@Unmodifiable
Map<Character, Integer> chatWidth();
Map<Integer, Integer> chatWidth();

@NotNull
@Unmodifiable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TextLayoutImpl(
TextManagerImpl.text(this).ifNull("Unable to find this text: $this")
}
private val height = (text.height().toDouble() * scale()).roundToInt().toHeight()
private val textWidth = Collections.unmodifiableMap(HashMap<Char, Int>().apply {
private val textWidth = Collections.unmodifiableMap(HashMap<Int, Int>().apply {
val div = height.toDouble() / text.height().toDouble()
text.chatWidth().forEach {
put(it.key, (it.value * div).roundToInt())
Expand All @@ -50,7 +50,7 @@ class TextLayoutImpl(
section.getString("pattern").ifNull("Unable to find 'pattern' command.")
)

override fun charWidth(): Map<Char, Int> = textWidth
override fun charWidth(): Map<Int, Int> = textWidth
override fun align(): TextAlign = align
override fun pattern(): Function<HealthBarData, String> = pattern

Expand Down Expand Up @@ -131,9 +131,9 @@ class TextLayoutImpl(
var i = 0
if (s.hasDecoration(TextDecoration.BOLD)) i++
if (s.hasDecoration(TextDecoration.ITALIC)) i++
it.content().sumOf { c ->
if (c == ' ') 4 else (textWidth[c] ?: 0) + 1 + i
}
it.content().codePoints().map { c ->
if (c == ' '.code) 4 else (textWidth[c] ?: 0) + 1 + i
}.sum()
} ?: 0) + component.children().sumOf {
length(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ object TextManagerImpl: TextManager, BetterHealthBerManager {
override fun text(name: String): HealthBarText? = textMap[name]

override fun start() {
val charWidth = HashMap<Char, Int>()
val charWidth = HashMap<Int, Int>()
PLUGIN.getResource("width.txt")?.let {
InputStreamReader(it, StandardCharsets.UTF_8).buffered().use { reader ->
reader.readLines().forEach { line ->
if (line.length < 2) return@forEach
val split = line.split(':')
if (split.size < 2) return@forEach
runCatching {
charWidth[line[0]] = line.substring(1).toInt()
charWidth[split[0].toInt(16)] = split[1].toInt()
}
}
}
Expand Down Expand Up @@ -68,7 +69,7 @@ object TextManagerImpl: TextManager, BetterHealthBerManager {
}

private class CharImage(
val char: Char,
val char: Int,
val image: BufferedImage
): Comparable<CharImage> {
override fun equals(other: Any?): Boolean {
Expand All @@ -91,9 +92,9 @@ object TextManagerImpl: TextManager, BetterHealthBerManager {

private fun parseFont(path: String, font: Font): HealthBarTextImpl {
val imageMap = TreeMap<Int, MutableSet<CharImage>>()
val charWidth = HashMap<Char, Int>()
val charWidth = HashMap<Int, Int>()

fun register(char: Char, image: BufferedImage) {
fun register(char: Int, image: BufferedImage) {
synchronized(imageMap) {
imageMap.computeIfAbsent(image.width) {
TreeSet()
Expand All @@ -106,12 +107,12 @@ object TextManagerImpl: TextManager, BetterHealthBerManager {

val height = (font.size.toDouble() * 1.4).roundToInt()

(Char.MIN_VALUE..Char.MAX_VALUE).filter {
(0..0x10FFFF).filter {
font.canDisplay(it)
}.forEachAsync {
BufferedImage(font.size, height, BufferedImage.TYPE_INT_ARGB).apply {
createGraphics().run {
fill(font.createGlyphVector(frc, it.toString()).getOutline(0F, font.size.toFloat()))
fill(font.createGlyphVector(frc, it.parseChar()).getOutline(0F, font.size.toFloat()))
dispose()
}
}.removeEmptyWidth()?.let { image ->
Expand All @@ -128,7 +129,7 @@ object TextManagerImpl: TextManager, BetterHealthBerManager {
target.createGraphics().run {
image.forEachIndexed { index, charImage ->
drawImage(charImage.image, it.key * (index % SPLIT_SIZE), height * (index / SPLIT_SIZE), null)
sb.append(charImage.char)
sb.appendCodePoint(charImage.char)
if ((index + 1) % SPLIT_SIZE == 0) {
array.add(JsonPrimitive(sb.toString()))
sb.setLength(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@ import kr.toxicity.healthbar.util.PLUGIN
import kr.toxicity.healthbar.util.asyncTaskTimer
import org.bukkit.entity.Player
import java.util.*
import java.util.concurrent.ConcurrentHashMap

class HealthBarPlayerImpl(
private val player: Player
): HealthBarPlayer {
override fun player(): Player = player
override fun compareTo(other: HealthBarPlayer): Int = player.uniqueId.compareTo(other.player().uniqueId)

private val updaterMap = HashMap<UUID, HealthBarUpdaterGroup>()
private val updaterMap = ConcurrentHashMap<UUID, HealthBarUpdaterGroup>()
private val task = asyncTaskTimer(1, 1) {
synchronized(updaterMap) {
val iterator = updaterMap.values.iterator()
synchronized(iterator) {
while (iterator.hasNext()) {
val next = iterator.next()
if (!next.update()) iterator.remove()
}
}
updaterMap.values.removeIf {
!it.update()
}
}

Expand All @@ -42,14 +37,9 @@ class HealthBarPlayerImpl(
}

override fun clear() {
synchronized(updaterMap) {
val iterator = updaterMap.values.iterator()
synchronized(iterator) {
while (iterator.hasNext()) {
iterator.next().remove()
}
}
updaterMap.clear()
updaterMap.values.removeIf {
it.update()
true
}
}

Expand All @@ -69,11 +59,9 @@ class HealthBarPlayerImpl(
entity
)
if (!healthBar.condition().apply(data)) return
synchronized(updaterMap) {
updaterMap.computeIfAbsent(entity.entity().uniqueId) {
HealthBarUpdaterGroupImpl(this, entity)
}.addHealthBar(data)
}
updaterMap.computeIfAbsent(entity.entity().uniqueId) {
HealthBarUpdaterGroupImpl(this, entity)
}.addHealthBar(data)
}

override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import kr.toxicity.healthbar.api.text.TextBitmap

class HealthBarTextImpl(
private val path: String,
private val charWidth: Map<Char, Int>,
private val charWidth: Map<Int, Int>,
private val bitmap: List<TextBitmap>,
private val height: Int,
): HealthBarText {
override fun path(): String = path
override fun chatWidth(): Map<Char, Int> = charWidth
override fun chatWidth(): Map<Int, Int> = charWidth
override fun bitmap(): List<TextBitmap> = bitmap
override fun height(): Int = height
}
Loading

0 comments on commit e21fb2d

Please sign in to comment.