Skip to content

FontRenderer.Entry -> FontRenderer.Rectangle #681

@Runemoro

Description

@Runemoro

A FontRenderer "Entry" is actually just a colored rectangle used for rendering the underline and strikethrough effects on text.

FontRenderer.Entry and FontRenderer.renderStringAtPos
    static class Entry {
        protected final float x1;
        protected final float y1;
        protected final float x2;
        protected final float y2;
        protected final float red;
        protected final float green;
        protected final float blue;
        protected final float alpha;

        private Entry(float x1, float y1, float x2, float y2, float red, float green, float blue, float alpha) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.red = red;
            this.green = green;
            this.blue = blue;
            this.alpha = alpha;
        }

        public void render(BufferBuilder buffer) {
            buffer.pos(x1, y1, 0.0D).color(red, green, blue, alpha).endVertex();
            buffer.pos(x2, y1, 0.0D).color(red, green, blue, alpha).endVertex();
            buffer.pos(x2, y2, 0.0D).color(red, green, blue, alpha).endVertex();
            buffer.pos(x1, y2, 0.0D).color(red, green, blue, alpha).endVertex();
        }
    }
    private float renderStringAtPos(String text, float x, float y, int color, boolean isShadow) {
        float colorMultiplier = isShadow ? 0.25F : 1.0F;
        float r = (color >> 16 & 255) / 255.0F * colorMultiplier;
        float g = (color >> 8 & 255) / 255.0F * colorMultiplier;
        float b = (color & 255) / 255.0F * colorMultiplier;
        float a = (color >> 24 & 255) / 255.0F;

        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder buffer = tessellator.getBuffer();
        ResourceLocation currentTexture = null;
        buffer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
        boolean obfuscated = false;
        boolean bold = false;
        boolean italic = false;
        boolean underline = false;
        boolean strikethrough = false;
        List<FontRenderer.Entry> lines = Lists.<FontRenderer.Entry>newArrayList();

        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);

            if (c == 167 && i + 1 < text.length()) {
                TextFormatting formatting = TextFormatting.fromFormattingCode(text.charAt(i + 1));

                if (formatting != null) {
                    if (formatting.isNormalStyle()) {
                        obfuscated = false;
                        bold = false;
                        strikethrough = false;
                        underline = false;
                        italic = false;
                    }

                    if (formatting.getColor() != null) {
                        color = formatting.getColor();
                        r = (color >> 16 & 255) / 255.0F * colorMultiplier;
                        g = (color >> 8 & 255) / 255.0F * colorMultiplier;
                        b = (color & 255) / 255.0F * colorMultiplier;
                    } else if (formatting == TextFormatting.OBFUSCATED) {
                        obfuscated = true;
                    } else if (formatting == TextFormatting.BOLD) {
                        bold = true;
                    } else if (formatting == TextFormatting.STRIKETHROUGH) {
                        strikethrough = true;
                    } else if (formatting == TextFormatting.UNDERLINE) {
                        underline = true;
                    } else if (formatting == TextFormatting.ITALIC) {
                        italic = true;
                    }
                }

                i++;
                continue;
            }

            IGlyph glyph = font.getGlyph(c);

            if (obfuscated && c != ' ') {
                glyph = font.obfuscate(glyph);
            }

            float width = glyph.getWidth();
            ResourceLocation glyphTexture = glyph.getTextureLocation();

            if (glyphTexture != null) {
                if (currentTexture != glyphTexture) {
                    tessellator.draw();
                    renderEngine.bindTexture(glyphTexture);
                    buffer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
                    currentTexture = glyphTexture;
                }

                float boldXOffset = bold ? glyph.getBoldXOffset() : 0.0F;
                float offset = isShadow ? glyph.getShadowOffset() : 0.0F;
                renderGlpyh(glyph, bold, italic, boldXOffset, x + offset, y + offset, buffer, r, g, b, a);
                width += boldXOffset;
            }

            float offset = isShadow ? 1.0F : 0.0F;

            if (strikethrough) {
                lines.add(new FontRenderer.Entry(
                        x + offset - 1.0F, y + offset + FONT_HEIGHT / 2.0F,
                        x + offset + width, y + offset + FONT_HEIGHT / 2.0F - 1.0F,
                        r, g, b, a
                ));
            }

            if (underline) {
                lines.add(new FontRenderer.Entry(
                        x + offset - 1.0F, y + offset + FONT_HEIGHT,
                        x + offset + width, y + offset + FONT_HEIGHT - 1.0F,
                        r, g, b, a
                ));
            }

            x += width;
        }

        tessellator.draw();

        if (!lines.isEmpty()) {
            GlStateManager.disableTexture2D();
            buffer.begin(7, DefaultVertexFormats.POSITION_COLOR);

            for (FontRenderer.Entry line : lines) {
                line.render(buffer);
            }

            tessellator.draw();
            GlStateManager.enableTexture2D();
        }

        return x;
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions