Никакая особенность AttributedString не работает, кроме зачеркивания - PullRequest
1 голос
/ 31 марта 2019

Я должен написать строку для BufferedImage.Я использую AtrributedString.TextAttribute.STRIKETHROUGH работает.Superscript, Subscript и другие не работают.

public class TextAttributesSuperscript  {

    static String Background = "input.png";
    static int curX = 10;
    static int curY = 50;

    public static void main(String[] args) throws Exception {
        AttributedString attributedString= new AttributedString("this is data. this data should be super script");

        attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.PLAIN, 18));
        attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);


        attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.BOLD, 18), 30,33);
        attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 29,33);
    attributedString.addAttribute(TextAttribute.SUPERSCRIPT,TextAttribute.SUPERSCRIPT_SUPER,30,33);

        final BufferedImage image = ImageIO.read(new File(Background));
        Graphics g = image.getGraphics();
        g.drawString(attributedString.getIterator(), curX, curY);
        g.dispose();
        ImageIO.write(image, "png", new File("output.png"));
    }
}

При выполнении кода выше.Надстрочная часть не работала (текст печатался не как надстрочный)

1 Ответ

0 голосов
/ 01 апреля 2019

Я не совсем уверен, почему ваш код не работает, так как кажется вполне логичным сделать это таким образом.И я не понимаю, почему некоторые атрибуты работают, а некоторые нет.

Но в соответствии с Java 2D Tutorial: Использование текстовых атрибутов для стиля текста , атрибут SUPERSCRIPT должен быть установлен на шрифт , а не на текстсам.То есть.используя Font.deriveFont(Map<Attribute, ?> attributes).

Для меня работает следующее (я немного изменил ваш код, чтобы он не зависел от вашего фонового файла):

public class TextAttributesSuperscript  {

    static int curX = 10;
    static int curY = 50;

    public static void main(String[] args) throws Exception {
        AttributedString attributedString = new AttributedString("this is data. this data should be super script");

        attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.PLAIN, 18));
        attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);

        Font superScript = new Font("TimesRoman", Font.BOLD, 18)
                .deriveFont(Collections.singletonMap(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER));
        attributedString.addAttribute(TextAttribute.FONT, superScript, 30, 33);
        attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 30,33);

        BufferedImage image = new BufferedImage(400, 100, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setColor(Color.WHITE);

        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.drawString(attributedString.getIterator(), curX, curY);
        g.dispose();

        ImageIO.write(image, "png", new File("output.png"));
    }
}
...