Roboto无法正确显示阿拉伯语

时间:2018-08-24 13:59:00

标签: java swing fonts roboto

我在Roboto字体上有一个问题,在我不是很准备的字体上,阿拉伯语无法正确显示,但是我认为它不支持阿拉伯语unicode。

我的项目处理的是摇摆的实质主题,因此我不会更改阿拉伯语支持的字体,因此我认为当主题加载后,我会检查键盘布局,如果机器人不支持该语言,则将字体更改为加载,这是我唯一能找到的解决方案,但是该解决方案不适用于没有键盘的设备。

现在我想问你两个问题:

  • 是否可以在那里找到支持阿拉伯语和该世系语言的roboto字体?
  • 如果我需要更改字体以获得支持,是否有比我更好的解决方案?

----------问题将字体加载到GraphicsEnvironment ------- 我已经重写了不同类型的字体,现在通过我编写的方法,我根据String的样式和支持来设置字体,但是在渲染外观时遇到了这个问题

static {
    try {
        //Noto sans
        Font black = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Black.ttf"));
        Font blackItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-BlackItalic.ttf"));
        Font bold = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Bold.ttf"));
        Font boldItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-BoldItalic.ttf"));
        Font italic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Italic.ttf"));
        Font light = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Light.ttf"));
        Font lightItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-LightItalic.ttf"));
        Font medium = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Medium.ttf"));
        Font mediumItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-MediumItalic.ttf"));
        Font regular = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Regular.ttf"));
        Font thin = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Thin.ttf"));
        Font thinItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-ThinItalic.ttf"));
        //Register font
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        graphicsEnvironment.registerFont(black);
        graphicsEnvironment.registerFont(blackItalic);
        graphicsEnvironment.registerFont(bold);
        graphicsEnvironment.registerFont(boldItalic);
        graphicsEnvironment.registerFont(italic);
        graphicsEnvironment.registerFont(light);
        graphicsEnvironment.registerFont(lightItalic);
        graphicsEnvironment.registerFont(medium);
        graphicsEnvironment.registerFont(mediumItalic);
        graphicsEnvironment.registerFont(regular);
        graphicsEnvironment.registerFont(thin);
        graphicsEnvironment.registerFont(thinItalic);

        black = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Black.ttf"));
        //blackItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/Roboto/Roboto-BlackItalic.ttf"));
        bold = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Bold.ttf"));
        boldItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-BoldItalic.ttf"));
        italic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Italic.ttf"));
        light = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Light.ttf"));
        lightItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-LightItalic.ttf"));
        medium = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Medium.ttf"));
        mediumItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-MediumItalic.ttf"));
        regular = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Regular.ttf"));
        thin = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Thin.ttf"));
        thinItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-ThinItalic.ttf"));

        //register font
        graphicsEnvironment.registerFont(black);
        //graphicsEnvironment.registerFont(blackItalic);
        graphicsEnvironment.registerFont(bold);
        graphicsEnvironment.registerFont(boldItalic);
        graphicsEnvironment.registerFont(italic);
        graphicsEnvironment.registerFont(light);
        graphicsEnvironment.registerFont(lightItalic);
        graphicsEnvironment.registerFont(medium);
        graphicsEnvironment.registerFont(mediumItalic);
        graphicsEnvironment.registerFont(regular);
        graphicsEnvironment.registerFont(thin);
        graphicsEnvironment.registerFont(thinItalic);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static Font getFont(String textCompatibily, String tipeFont){
    String familyFontRoboto = "Roboto";
    String familyFontNotoSans = "Noto Sans";
    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    //System.out.println(familyFontRoboto + " " + tipeFont);
    for(Font font : graphicsEnvironment.getAllFonts()){
        if(font.canDisplayUpTo(textCompatibily) < 0){
            if(font.getName().equals(familyFontRoboto + " " + tipeFont)){
                System.out.println("Roboto ok");
                System.out.println(font.toString());
                return font;
            }
        }
    }
    for(Font font : graphicsEnvironment.getAllFonts()){
        if(font.canDisplayUpTo(textCompatibily) < 0){
            if(font.getName().equals(familyFontNotoSans + " " + tipeFont)){
                System.out.println("noto sans ok");
                return font;
            }
        }
    }
    System.out.print("ops");
    return graphicsEnvironment.getAllFonts()[0]; // Noto sans dovrebbe coprire tutto, altrimenti questa cosa si allunga
}

在按钮上设置字体

Font font = MaterialFonts.getFont(button.getText(), "Black");
    button.setFont (font);

问题呈现为 enter image description here

1 个答案:

答案 0 :(得分:0)

'Roboto'由Google拥有...不支持阿拉伯语... Google使用名为'noto font'的后备字体,其中不支持'roboto'...