Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize spaces calculation with hex #7

Open
MelonHell opened this issue Apr 2, 2023 · 0 comments
Open

Optimize spaces calculation with hex #7

MelonHell opened this issue Apr 2, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@MelonHell
Copy link

MelonHell commented Apr 2, 2023

я думаю можно добавить альтернативные реализации SpacesGlyphResourceProducer для сокращения пробельных символов в глифах

вот пример моей реализации на kotlin, которая сокращает кол-во сиволов в пробелах примерно вдвое

основная суть в том, что тупа добавить больше вариаций пробелов, в данном примере вместо разрядов двоичной системы используются разряды шеснадцетиричной

import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import ru.brikster.glyphs.compile.ArbitraryCharacterFactory
import ru.brikster.glyphs.glyph.EmptyGlyph
import ru.brikster.glyphs.glyph.Glyph
import ru.brikster.glyphs.glyph.exception.ResourceAlreadyProducedException
import ru.brikster.glyphs.glyph.exception.ResourceNotProducedException
import ru.brikster.glyphs.glyph.space.SpacesGlyphResourceProducer
import ru.brikster.glyphs.util.ArrayUtil
import team.unnamed.creative.font.FontProvider
import kotlin.math.abs

class OptimizedSpacesGlyphResourceProducer(
    private val key: Key = Glyph.DEFAULT_SPACES_FONT_KEY
) : SpacesGlyphResourceProducer {
    private var fontProviders: Set<FontProvider>? = null
    override fun fontKey() = key
    private var mapping: MutableMap<Int, Char>? = null

    override fun produced() = fontProviders != null

    override fun produceResources(characterFactory: ArbitraryCharacterFactory) {
        if (fontProviders != null) throw ResourceAlreadyProducedException()
        mapping = HashMap()
        val fontProviderBuilder = FontProvider.space()

        sizeList.forEach { length ->
            fontProviderBuilder.advance(retrieveCharacter(characterFactory, length), length)
            fontProviderBuilder.advance(retrieveCharacter(characterFactory, length * -1), length * -1)
        }

        fontProviders = setOf<FontProvider>(fontProviderBuilder.build())
    }

    @Throws(ResourceNotProducedException::class)
    override fun fontProviders(): Collection<FontProvider> {
        return fontProviders ?: throw ResourceNotProducedException()
    }

    private fun retrieveCharacter(characterFactory: ArbitraryCharacterFactory, length: Int): Char {
        val character = characterFactory.nextCharacter()
        mapping!![length] = character
        return character
    }

    override fun translate(length: Int): Glyph {
        val lengthAbs = abs(length)
        val lengthSign = length.coerceIn(-1, 1)

        if (mapping == null) throw ResourceNotProducedException()
        if (length == 0) return EmptyGlyph.INSTANCE

        val characters: MutableList<Char?> = ArrayList()

        var iter = lengthAbs
        while (iter != 0) {
            val charLength = sizeList.last { it <= iter }
            iter -= charLength
            characters.add(mapping!![charLength * lengthSign]!!)
        }

        return SpacesGlyph(fontKey(), ArrayUtil.toCharArray(characters), length)
    }

    class SpacesGlyph(
        private val key: Key,
        private val characters: CharArray,
        private val width: Int
    ) : Glyph {
        override fun toAdventure(): Component {
            return Component.text(String(characters)).font(key)
        }

        override fun width(): Int = width
    }

    companion object {
        private val sizeList = listOf(
            0x1,
            0x2,
            0x3,
            0x4,
            0x5,
            0x6,
            0x7,
            0x8,
            0x9,
            0xA,
            0xB,
            0xC,
            0xD,
            0xE,
            0xF,
            0x10,
            0x20,
            0x30,
            0x40,
            0x50,
            0x60,
            0x70,
            0x80,
            0x90,
            0xA0,
            0xB0,
            0xC0,
            0xD0,
            0xE0,
            0xF0,
            0x100,
            0x200,
            0x300,
            0x400,
            0x500,
            0x600,
            0x700,
            0x800,
            0x900,
            0xA00,
            0xB00,
            0xC00,
            0xD00,
            0xE00,
            0xF00
        )
    }
}
@Brikster Brikster changed the title оптимизация пробелов Optimize spaces calculation with hex Jun 3, 2023
@Brikster Brikster added the enhancement New feature or request label Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants