Search in sources :

Example 6 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 printCharacter.

@SuppressWarnings("rawtypes")
@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);
    }
    float scale = fontSize / 16;
    fontSize = fontSize - (13 - this.size);
    decorations = sortDecorations(decorations);
    Graphics2D g = image.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setFont(font.deriveFont(this.size));
    int w = g.getFontMetrics().stringWidth(character);
    Font fontToPrint = font.deriveFont(fontSize);
    BufferedImage[] magicCharImages = null;
    boolean bold = false;
    boolean italic = false;
    for (TextDecoration decoration : decorations) {
        switch(decoration) {
            case OBFUSCATED:
                magicCharImages = new BufferedImage[OBFUSCATE_OVERLAP_COUNT];
                for (int i = 0; i < magicCharImages.length; i++) {
                    String magicCharacter = ComponentStringUtils.toMagic(provider, character);
                    magicCharImages[i] = provider.forCharacter(magicCharacter).getCharacterImage(magicCharacter, fontSize, color).orElse(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB));
                }
                break;
            case BOLD:
                bold = true;
                break;
            case ITALIC:
                fontToPrint = fontToPrint.deriveFont(Font.ITALIC);
                italic = true;
                break;
            case STRIKETHROUGH:
                Map attributes = fontToPrint.getAttributes();
                attributes.put(TextAttribute.STRIKETHROUGH, true);
                fontToPrint = new Font(attributes);
                break;
            case UNDERLINED:
                attributes = fontToPrint.getAttributes();
                attributes.put(TextAttribute.UNDERLINE, true);
                fontToPrint = new Font(attributes);
                break;
            default:
                break;
        }
    }
    g.setColor(new Color(color.value()));
    g.setFont(fontToPrint);
    int height = g.getFontMetrics().getHeight() / 2;
    int newW = g.getFontMetrics().stringWidth(character);
    int finalWidth = newW;
    int extraWidth = italic ? 0 : lastItalicExtraWidth;
    if (magicCharImages == null) {
        g.drawString(character, x, y + height);
        if (bold) {
            g.drawString(character, x + (scale * 2) + extraWidth, y + height);
            finalWidth += scale * 2;
        }
    } else {
        for (BufferedImage magicCharImage : magicCharImages) {
            g.drawImage(magicCharImage, x + extraWidth, y, newW, height, null);
        }
        if (bold) {
            for (BufferedImage magicCharImage : magicCharImages) {
                g.drawImage(magicCharImage, (int) (x + (scale * 2)) + extraWidth, y, newW, height, null);
            }
            finalWidth += scale * 2;
        }
    }
    g.dispose();
    float spaceWidth = (float) newW / (float) w;
    return new FontRenderResult(image, finalWidth + extraWidth, height + (int) Math.round(shift.getTranslateY()), (int) Math.round(spaceWidth + shift.getTranslateX()), 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) Map(java.util.Map) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 7 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 getCharacterImage.

@Override
public Optional<BufferedImage> getCharacterImage(String character, float fontSize, TextColor color) {
    if (character.equals(" ")) {
        return Optional.of(getSpaceImage(fontSize));
    }
    Color awtColor = new Color(color.value());
    BufferedImage charImage = charImages.get(character.codePointAt(0)).getFontImage();
    float descent = height - this.ascent - 1;
    charImage = ImageUtils.resizeImageFillHeight(charImage, Math.abs(Math.round(fontSize + (ascent + descent) * scale)));
    charImage = ImageUtils.multiply(charImage, ImageUtils.changeColorTo(ImageUtils.copyImage(charImage), awtColor));
    return Optional.of(charImage);
}
Also used : Color(java.awt.Color) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) BufferedImage(java.awt.image.BufferedImage)

Example 8 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 getCharacterImage.

@Override
public Optional<BufferedImage> getCharacterImage(String character, float fontSize, TextColor color) {
    if (character.equals(" ")) {
        return Optional.of(getSpaceImage(fontSize));
    }
    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();
        charImage = ImageUtils.resizeImageFillHeight(charImage, Math.round(fontSize));
        charImage = ImageUtils.multiply(charImage, ImageUtils.changeColorTo(ImageUtils.copyImage(charImage), awtColor));
        return Optional.of(charImage);
    } else {
        return Optional.empty();
    }
}
Also used : Color(java.awt.Color) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) BufferedImage(java.awt.image.BufferedImage)

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