Giter VIP home page Giter VIP logo

glyphs's Introduction

glyphs

Apache 2.0 License Maven Central GitHub code size in bytes

Library for convenient management of bitmap textures from Minecraft resourcepacks. Vanilla provides you ability to create bitmap fonts with images of bigger sizes (up to 256x256), but they work as a text - you can't render texture at the necessary place.

glyphs give the opportunity to make decorated chat messages, menus etc. with absolutely positioned textures. It based on @unnamed's "creative" & @Kyori's "adventure" libraries, so glyphs is platform-independent.

Examples

Decorated chat message

Decorated chat message

At the left part of message you can see the texture with Minecraft® logo (all rights belong to their respective owners, that's just an example).

How to reproduce

var spaces = MojangSpacesGlyph.create();
var exampleLogo = ImageGlyph.of(Texture.of(
            Key.key(Glyph.DEFAULT_NAMESPACE, "chat/example_logo"),
            GlyphResources.resourceFromJar("example_logo.png")),
        new TextureProperties(/* height */ 50, /* ascent */ 56));

Collection<FileResource> resources = GlyphCompiler.instance()
        .compile(spaces, exampleLogo);

// Method that generates archive with "creative" library
createResourcepack(resources);

// Indent component with 14 spaces
Component logoIndentComponent = Component.text(" ".repeat(14));

// GlyphComponentBuilder#build returns "Component"'s instance from "adventure" library
Component resultComponent = GlyphComponentBuilder.universal(spaces)
        .append(exampleLogo)
        .build()
        .append(Component.newline().append(logoIndentComponent))
        .append(Component.newline()
            .append(logoIndentComponent)
            .append(Component.text("Glyphs", TextColor.fromHexString("#d84aff"), 
                TextDecoration.BOLD)))
        .append(Component.newline()
            .append(logoIndentComponent)
            .append(Component.text("<--- ", NamedTextColor.GRAY))
            .append(Component.text("Here you can see Minecraft® logo", NamedTextColor.YELLOW)))
        .append(Component.newline().append(logoIndentComponent))
        .append(Component.newline().append(logoIndentComponent));

// and so on to send it to a player

Menu with glyphs

Example menu

That's the simple menu with rendered texts of various sizes and the custom button.

How to reproduce

// Glyphs objects
var spaces = MojangSpacesGlyph.create();

var guiBackground = ImageGlyph.of(Texture.of(
            Key.key(Glyph.DEFAULT_NAMESPACE, "gui/gui_background"),
            GlyphResources.resourceFromJar("gui_background.png")),
        new TextureProperties(/* height */ 256, /* ascent */ 19));

var exampleButton = ImageGlyph.of(Texture.of(
            Key.key(Glyph.DEFAULT_NAMESPACE, "gui/example_button"),
            GlyphResources.resourceFromJar("example_button.png")),
        new TextureProperties(/* height */ 22, /* ascent */ -56));

var font = GlyphResources.minecraftFontGlyphCollection(
        List.of(new TextureProperties(/* height */ 12, /* ascent */ -6),
            new TextureProperties(/* height */ 8, /* ascent */ -24),
            new TextureProperties(/* height */ 8, /* ascent */ -36)));

Collection<FileResource> resources = GlyphCompiler.instance()
        .compile(spaces, guiBackground, exampleButton, font);
resources.addAll(GlyphResources.blankSlotResources());

// Method that generates archive with "creative" library
createResourcepack(resources);

// Building component for inventory title
Component titleComponent = GlyphComponentBuilder.gui(spaces)
        .append(guiBackground)
        .append(/* position */ 131, exampleButton)
        .append(/* position */ 16, font.translate(/* height */ 12, /* ascent */ -6, "Example text"))
        .append(/* position */ 16, font.translate(/* height */ 8, /* ascent */ -24, "Hello "))
        .append(PositionType.RELATIVE, font.translate(/* height */ 8, /* ascent */ -24, "world..."))
        .append(PositionType.ABSOLUTE, /* position */ 16, font.translate(/* height */ 8, /* ascent */ -36, "Hello world...", NamedTextColor.LIGHT_PURPLE))
        .build()
        .append(Component.text("Test menu with glyphs", NamedTextColor.DARK_GRAY, TextDecoration.UNDERLINED));

// and so on to open chest GUI

See full code of Bukkit plugin with both examples.

Usage

You need an implementation of Minecraft server with adventure library support. Also, that's required Minecraft 1.19 and higher to support all the library features.

Add dependency

glyphs is published to Maven Central repository.

Gradle (releases)

repositories {
    mavenCentral()
}

dependencies {
    // Main dependency with GlyphComponentBuilder and GlyphCompiler
    implementation "ru.brikster:glyphs-api:$version"
    // Optional dependency with some frequently used resources (Minecraft font etc.)
    implementation "ru.brikster:glyphs-resources:$version"
}

Gradle (snapshots)

repositories {
    maven { 
        name "ossrh-snapshots"
        url "https://s01.oss.sonatype.org/content/repositories/snapshots/" 
    }
}

dependencies {
    // Main dependency with GlyphComponentBuilder and GlyphCompiler
    implementation "ru.brikster:glyphs-api:$version"
    // Optional dependency with some frequently used resources (Minecraft font etc.)
    implementation "ru.brikster:glyphs-resources:$version"
}

You can check out for latest snapshot versions in gradle.properties or at OSSRH Snapshots.

GitHub Repo stars GitHub forks

glyphs's People

Contributors

brikster avatar deepsourcebot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

glyphs's Issues

дублирование пробелов

при создании Kyori Adventure компонента наблюдается такая ситуация, что пробелы идут "парами", я так понимаю первый отступ - отрицательный оффсет по ширине глифа, второй отступ - оффсет глифа
в итоге это выглядит так:

    {
        "color": "white",
        "font": "glyphs:minecraft_font",
        "text": ""
    },
    {
        "font": "glyphs:spaces",
        "text": "ꊥꊍꉋꈭꈋ"
    },
    {
        "font": "glyphs:spaces",
        "text": "ꊤꊈꉊꈮ"
    },
    {
        "color": "white",
        "font": "fullscreen:keyboard_key3",
        "text": ""
    },

хотелось бы чтобы эти два пробела были объеденины в один, так как пакет и так целую тонну весит

Полупрозрачные элементы ломают всю вёрстку

Не знаю в глифсе ли проблема или в майнкрафте, но я полагаю, что даже если проблема в майне, то на стороне глифса можно правильно обработать данное поведение

Обычное поведение:
image

Полупрозрачный фон у title:
image

Маленькие пробелы и отступы между буквами при разных height

Дефолтные символы имеют height = 8, отступ между буквами = 1, пробел = 4. Если я сделаю размер шрифта 16, то отступы всё так же будут 1, а пробелы 4, хотя должны быть вдвое больше. Эта проблема легко решаема добавляя дополнительные пробелы между символами, но думаю не всем оно надо, так что хотелось бы видеть это как отдельную настройку/реализацию или хз какое тут слово подобрать)

ttf font support

Было бы классно увидеть поддержку TTF шрифтов в glyphs, по сути возможность смещения у них есть, вычислить ширину я думаю не очень сложная задача. К крокозябрам привязываться не вижу смысла, можно просто регать каждый вид аскента как отдельный майнкрфт шрифт

Идея для положительного ascent

Как вариант, при высоком ascent можно изменить шрифт так, чтоб его height был выше, чем надо, заполнив пространство пустыми пикселями и тогда визуально текст будет находиться на нужной высоте

Add support for decorated font glyphs

Right now, you can use font.translate to get text of any size, position, and color. My idea is to also be able to get decorated text, such as bold/italic/etc. Protocol lib could even be used to achieve animated text.

Optimize spaces calculation with hex

я думаю можно добавить альтернативные реализации 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
        )
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.