Search in sources :

Example 1 with TextColor

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class BitmapFont method printCharacter.

@Override
public FontRenderResult printCharacter(BufferedImage image, String character, int x, int y, float fontSize, int lastItalicExtraWidth, TextColor color, List<TextDecoration> decorations) {
    if (character.equals(" ")) {
        return printSpace(image, x, y, fontSize, lastItalicExtraWidth, color, decorations);
    }
    decorations = sortDecorations(decorations);
    Color awtColor = new Color(color.value());
    BufferedImage charImage = charImages.get(character.codePointAt(0)).getFontImage();
    int originalW = charImage.getWidth();
    float scale = fontSize / 8;
    float ascent = this.ascent - 7;
    float descent = height - this.ascent - 1;
    int fillHeight = (int) Math.floor(fontSize + (ascent + descent) * scale);
    charImage = ImageUtils.resizeImageFillHeight(charImage, Math.abs(fillHeight));
    int w = charImage.getWidth();
    int h = charImage.getHeight();
    charImage = ImageUtils.multiply(charImage, ImageUtils.changeColorTo(ImageUtils.copyImage(charImage), awtColor));
    int beforeTransformW = w;
    double accuratePixelSize = (double) beforeTransformW / (double) originalW;
    int pixelSize = (int) Math.round(accuratePixelSize);
    int strikeSize = (int) (fontSize / 8.0);
    int boldSize = (int) (fontSize / 16.0 * 3);
    int italicExtraWidth = 0;
    boolean italic = false;
    for (TextDecoration decoration : decorations) {
        switch(decoration) {
            case OBFUSCATED:
                charImage = new BufferedImage(charImage.getWidth(), charImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = charImage.createGraphics();
                for (int i = 0; i < OBFUSCATE_OVERLAP_COUNT; i++) {
                    String magicCharacter = ComponentStringUtils.toMagic(provider, character);
                    BufferedImage magicImage = provider.forCharacter(magicCharacter).getCharacterImage(magicCharacter, fontSize, color).orElse(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB));
                    g.drawImage(magicImage, 0, 0, charImage.getWidth(), charImage.getHeight(), null);
                }
                g.dispose();
                break;
            case BOLD:
                BufferedImage boldImage = new BufferedImage(charImage.getWidth() + 2, charImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
                for (int x0 = 0; x0 < charImage.getWidth(); x0++) {
                    for (int y0 = 0; y0 < charImage.getHeight(); y0++) {
                        int pixelColor = charImage.getRGB(x0, y0);
                        int alpha = ColorUtils.getAlpha(pixelColor);
                        if (alpha != 0) {
                            for (int i = 0; i < boldSize; i++) {
                                boldImage.setRGB(x0 + i, y0, pixelColor);
                            }
                        }
                    }
                }
                charImage = boldImage;
                w += boldSize - 1;
                break;
            case ITALIC:
                int extraWidth = (int) ((double) charImage.getHeight() * (4.0 / 14.0));
                BufferedImage italicImage = new BufferedImage(charImage.getWidth() + extraWidth * 2, charImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
                g = italicImage.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g.transform(AffineTransform.getShearInstance(ITALIC_SHEAR_X, 0));
                g.drawImage(charImage, extraWidth, 0, null);
                g.dispose();
                charImage = italicImage;
                italicExtraWidth = (int) Math.round(-ITALIC_SHEAR_X * h);
                italic = true;
                break;
            case STRIKETHROUGH:
                charImage = ImageUtils.expandCenterAligned(charImage, 0, 0, 0, pixelSize * this.scale);
                g = charImage.createGraphics();
                g.setColor(awtColor);
                g.fillRect(0, (int) (fontSize / 2), w, strikeSize);
                g.dispose();
                break;
            case UNDERLINED:
                charImage = ImageUtils.expandCenterAligned(charImage, 0, strikeSize * 2, 0, pixelSize * this.scale);
                g = charImage.createGraphics();
                g.setColor(awtColor);
                g.fillRect(0, (int) (fontSize), w, strikeSize);
                g.dispose();
                break;
            default:
                break;
        }
    }
    Graphics2D g = image.createGraphics();
    int extraWidth = italic ? 0 : lastItalicExtraWidth;
    int sign = fillHeight >= 0 ? 1 : -1;
    int spaceWidth = (int) Math.floor(accuratePixelSize * this.scale);
    if (sign > 0) {
        g.drawImage(charImage, x + extraWidth, (int) (y - ascent * scale), null);
    } else {
        g.drawImage(ImageUtils.flipVertically(charImage), x + extraWidth, y, -w, -h, null);
        spaceWidth = (int) Math.ceil(spaceWidth + accuratePixelSize * 8) + 1;
    }
    g.dispose();
    return new FontRenderResult(image, w * sign + extraWidth, h, spaceWidth, italicExtraWidth);
}
Also used : TextDecoration(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration) Color(java.awt.Color) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 2 with TextColor

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class CharacterDataArray method fromComponent.

public static CharacterDataArray fromComponent(Component component, boolean legacyRGB) {
    List<CharacterData> data = new LinkedList<>();
    StringBuilder sb = new StringBuilder();
    component = ComponentFlattening.flatten(component);
    for (Component each : component.children()) {
        Key font = each.style().font();
        if (font == null) {
            font = Key.key("minecraft:default");
        }
        TextColor color = each.color();
        if (color == null) {
            color = NamedTextColor.WHITE;
        }
        List<TextDecoration> decorations = each.decorations().entrySet().stream().filter(entry -> entry.getValue().equals(State.TRUE)).map(entry -> entry.getKey()).collect(Collectors.toList());
        String content;
        if (each instanceof TextComponent) {
            content = ChatColorUtils.filterIllegalColorCodes(((TextComponent) each).content(), legacyRGB);
        } else {
            content = ChatColorUtils.filterIllegalColorCodes(PlainTextComponentSerializer.plainText().serialize(each), legacyRGB);
        }
        if (content.isEmpty()) {
            continue;
        }
        CharacterData characterData = new CharacterData(font, color, decorations);
        for (char c : content.toCharArray()) {
            data.add(characterData);
            sb.append(c);
        }
    }
    return new CharacterDataArray(sb.toString().toCharArray(), data.toArray(new CharacterData[data.size()]));
}
Also used : TextDecoration(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration) ComponentFlattening(com.loohp.interactivechat.utils.ComponentFlattening) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) State(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration.State) Collectors(java.util.stream.Collectors) Key(com.loohp.interactivechat.libs.net.kyori.adventure.key.Key) List(java.util.List) TextComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent) PlainTextComponentSerializer(com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer) TextDecoration(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration) ChatColorUtils(com.loohp.interactivechat.utils.ChatColorUtils) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) NamedTextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor) LinkedList(java.util.LinkedList) TextComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent) LinkedList(java.util.LinkedList) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) NamedTextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) TextComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent) Key(com.loohp.interactivechat.libs.net.kyori.adventure.key.Key)

Example 3 with TextColor

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class TrueTypeFont method getCharacterImage.

@Override
public Optional<BufferedImage> getCharacterImage(String character, float fontSize, TextColor color) {
    if (character.equals(" ")) {
        return Optional.of(getSpaceImage(fontSize));
    }
    BufferedImage image = new BufferedImage((int) (10 * fontSize), (int) (10 * fontSize), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    Font fontToPrint = font.deriveFont(fontSize);
    g.setColor(new Color(color.value()));
    g.setFont(fontToPrint);
    int height = g.getFontMetrics().getHeight() / 2;
    g.drawString(character, 0, height);
    image = ImageUtils.copyAndGetSubImage(image, 0, 0, g.getFontMetrics().stringWidth(character), height);
    g.dispose();
    return Optional.of(image);
}
Also used : Color(java.awt.Color) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 4 with TextColor

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class LegacyUnicodeFont method printCharacter.

@Override
public FontRenderResult printCharacter(BufferedImage image, String character, int x, int y, float fontSize, int lastItalicExtraWidth, TextColor color, List<TextDecoration> decorations) {
    if (character.equals(" ")) {
        return printSpace(image, x, y, fontSize, lastItalicExtraWidth, color, decorations);
    }
    decorations = sortDecorations(decorations);
    Color awtColor = new Color(color.value());
    Optional<FontTextureResource> optCharImage = charImages.get(character.codePointAt(0));
    if (optCharImage == null) {
        optCharImage = MISSING_CHARACTER;
    }
    if (optCharImage.isPresent()) {
        BufferedImage charImage = optCharImage.get().getFontImage();
        int originalW = charImage.getWidth();
        charImage = ImageUtils.resizeImageFillHeight(charImage, (int) Math.floor(fontSize));
        int w = charImage.getWidth();
        int h = charImage.getHeight();
        charImage = ImageUtils.multiply(charImage, ImageUtils.changeColorTo(ImageUtils.copyImage(charImage), awtColor));
        int beforeTransformW = w;
        double accuratePixelSize = (double) beforeTransformW / (double) originalW;
        int pixelSize = (int) Math.round(accuratePixelSize);
        int strikeSize = (int) (fontSize / 8);
        int boldSize = (int) (fontSize / 16.0 * 2);
        int italicExtraWidth = 0;
        boolean italic = false;
        for (TextDecoration decoration : decorations) {
            switch(decoration) {
                case OBFUSCATED:
                    charImage = new BufferedImage(charImage.getWidth(), charImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g = charImage.createGraphics();
                    for (int i = 0; i < OBFUSCATE_OVERLAP_COUNT; i++) {
                        String magicCharacter = ComponentStringUtils.toMagic(provider, character);
                        BufferedImage magicImage = provider.forCharacter(magicCharacter).getCharacterImage(magicCharacter, fontSize, color).orElse(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB));
                        g.drawImage(magicImage, 0, 0, charImage.getWidth(), charImage.getHeight(), null);
                    }
                    g.dispose();
                    break;
                case BOLD:
                    BufferedImage boldImage = new BufferedImage(charImage.getWidth() + 2, charImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
                    for (int x0 = 0; x0 < charImage.getWidth(); x0++) {
                        for (int y0 = 0; y0 < charImage.getHeight(); y0++) {
                            int pixelColor = charImage.getRGB(x0, y0);
                            int alpha = ColorUtils.getAlpha(pixelColor);
                            if (alpha != 0) {
                                for (int i = 0; i < boldSize; i++) {
                                    boldImage.setRGB(x0 + i, y0, pixelColor);
                                }
                            }
                        }
                    }
                    charImage = boldImage;
                    w += boldSize - 1;
                    break;
                case ITALIC:
                    int extraWidth = (int) ((double) charImage.getHeight() * (4.0 / 14.0));
                    BufferedImage italicImage = new BufferedImage(charImage.getWidth() + extraWidth * 2, charImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
                    g = italicImage.createGraphics();
                    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                    g.transform(AffineTransform.getShearInstance(ITALIC_SHEAR_X, 0));
                    g.drawImage(charImage, extraWidth, 0, null);
                    g.dispose();
                    charImage = italicImage;
                    italicExtraWidth = (int) Math.round(-ITALIC_SHEAR_X * h);
                    italic = true;
                    break;
                case STRIKETHROUGH:
                    charImage = ImageUtils.expandCenterAligned(charImage, 0, 0, 0, pixelSize);
                    g = charImage.createGraphics();
                    g.setColor(awtColor);
                    g.fillRect(0, (int) (fontSize / 2), w, strikeSize);
                    g.dispose();
                    break;
                case UNDERLINED:
                    charImage = ImageUtils.expandCenterAligned(charImage, 0, strikeSize * 2, 0, pixelSize);
                    g = charImage.createGraphics();
                    g.setColor(awtColor);
                    g.fillRect(0, (int) (fontSize), w, strikeSize);
                    g.dispose();
                    break;
                default:
                    break;
            }
        }
        Graphics2D g = image.createGraphics();
        int extraWidth = italic ? 0 : lastItalicExtraWidth;
        g.drawImage(charImage, x + extraWidth, y, null);
        g.dispose();
        return new FontRenderResult(image, w + extraWidth, h, (int) Math.floor(accuratePixelSize + 1), italicExtraWidth);
    } else {
        return new FontRenderResult(image, 0, 0, 0, 0);
    }
}
Also used : TextDecoration(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration) Color(java.awt.Color) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 5 with TextColor

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class MinecraftFont method printSpace.

protected final FontRenderResult printSpace(BufferedImage image, int x, int y, float fontSize, int lastItalicExtraWidth, TextColor color, List<TextDecoration> decorations) {
    decorations = sortDecorations(decorations);
    Color awtColor = new Color(color.value());
    BufferedImage charImage = SPACE_CHAR;
    int originalW = charImage.getWidth();
    charImage = ImageUtils.resizeImageFillHeight(charImage, Math.round(fontSize));
    int w = charImage.getWidth();
    int h = charImage.getHeight();
    charImage = ImageUtils.multiply(charImage, ImageUtils.changeColorTo(ImageUtils.copyImage(charImage), awtColor));
    int beforeTransformW = w;
    int pixelSize = Math.round((float) beforeTransformW / (float) originalW);
    int strikeSize = (int) (fontSize / 8);
    int boldSize = (int) (fontSize / 16.0 * 2);
    int italicExtraWidth = 0;
    boolean italic = false;
    for (TextDecoration decoration : decorations) {
        switch(decoration) {
            case BOLD:
                BufferedImage boldImage = new BufferedImage(charImage.getWidth() + 2, charImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
                for (int x0 = 0; x0 < charImage.getWidth(); x0++) {
                    for (int y0 = 0; y0 < charImage.getHeight(); y0++) {
                        int pixelColor = charImage.getRGB(x0, y0);
                        int alpha = ColorUtils.getAlpha(pixelColor);
                        if (alpha != 0) {
                            for (int i = 0; i < boldSize; i++) {
                                boldImage.setRGB(x0 + i, y0, pixelColor);
                            }
                        }
                    }
                }
                charImage = boldImage;
                w += boldSize - 1;
                break;
            case ITALIC:
                int extraWidth = (int) ((double) charImage.getHeight() * (4.0 / 14.0));
                BufferedImage italicImage = new BufferedImage(charImage.getWidth() + extraWidth * 2, charImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = italicImage.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g.transform(AffineTransform.getShearInstance(ITALIC_SHEAR_X, 0));
                g.drawImage(charImage, extraWidth, 0, null);
                g.dispose();
                charImage = italicImage;
                italicExtraWidth = (int) Math.round(-ITALIC_SHEAR_X * h);
                italic = true;
                break;
            case STRIKETHROUGH:
                charImage = ImageUtils.expandCenterAligned(charImage, 0, 0, 0, pixelSize);
                g = charImage.createGraphics();
                g.setColor(awtColor);
                g.fillRect(0, (int) (fontSize / 2), charImage.getWidth(), strikeSize);
                g.dispose();
                break;
            case UNDERLINED:
                charImage = ImageUtils.expandCenterAligned(charImage, 0, strikeSize * 2, 0, pixelSize);
                g = charImage.createGraphics();
                g.setColor(awtColor);
                g.fillRect(0, (int) (fontSize), charImage.getWidth(), strikeSize);
                g.dispose();
                break;
            default:
                break;
        }
    }
    Graphics2D g = image.createGraphics();
    int extraWidth = italic ? 0 : lastItalicExtraWidth;
    g.drawImage(charImage, x + extraWidth, y, null);
    g.dispose();
    return new FontRenderResult(image, w + extraWidth, h, pixelSize, italicExtraWidth);
}
Also used : TextDecoration(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration) Color(java.awt.Color) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

TextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor)8 Color (java.awt.Color)7 BufferedImage (java.awt.image.BufferedImage)7 TextDecoration (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration)5 Graphics2D (java.awt.Graphics2D)5 Font (java.awt.Font)2 Key (com.loohp.interactivechat.libs.net.kyori.adventure.key.Key)1 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)1 TextComponent (com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent)1 NamedTextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor)1 State (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration.State)1 PlainTextComponentSerializer (com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer)1 ChatColorUtils (com.loohp.interactivechat.utils.ChatColorUtils)1 ComponentFlattening (com.loohp.interactivechat.utils.ComponentFlattening)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1