use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration 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);
}
use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration 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()]));
}
use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration 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);
}
}
use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration 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);
}
use of com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration 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);
}
Aggregations