1

I have a problem on Roboto font where the Arabic language is not displayed correctly, on the fonts I'm not very prepared but I think it does not support Arabic unicode.

My project deals with a material theme for swing and therefore I would not change fonts for the Arabic language support so I thought that when the theme is loaded I check for a keyboard layaut and if that language is not supported by a robot then change font to loading, this is the only solution I could find but the solution does not work for devices without a keyboard.

Now I would like to ask you two questions:

  • Is it possible to find the roboto font that supports Arabic and the languages of that lineage there?
  • Is there any better solution than mine if I need to change font for support?

---------- Problem Load Font to GraphicsEnvironment ------- I have rewritten fonts of different types and now through a method written by me I go to set the font according to the style and the support of the String but I get this problem when I go to render the look and feel

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
}

setting font on the button

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

problem rendering is enter image description here

  • *"Is there any better solution .. if I need to change font ..?"* See [`Font.canDisplayUpTo(String)`](https://docs.oracle.com/javase/9/docs/api/java/awt/Font.html#canDisplayUpTo-java.lang.String-). See also [this example](https://stackoverflow.com/a/42335976/418556). – Andrew Thompson Aug 24 '18 at 16:03
  • @AndrewThompson And if I did not know what the font should be applied to ?? my problem concerns a look and feel material for swing and so I have to load the right font to set the font to the declaration of the look and feel –  Aug 24 '18 at 16:12
  • *"And if I did not know what the font should be applied to ??"* Did you check the second link? It includes a [mcve] that **demonstrates going through all installed fonts** and listing the ones that support a given `String`! – Andrew Thompson Aug 24 '18 at 16:14
  • @AndrewThompson Yes I had seen it, I'm sorry I expressed myself badly. I wanted to say that that method sees the fonts supported by java right ?? if I had in the folder of my project a fot that supports Arabic and I would like to use that I can not search for the character with the right solution? –  Aug 24 '18 at 16:23
  • *"if I had in the folder of my project a fot that supports Arabic and I would like to use that I can not search for the character with the right solution?"* Sure you can. Just instantiate it first, as seen in [this example](https://stackoverflow.com/a/13718134/418556)! – Andrew Thompson Aug 24 '18 at 16:50
  • @AndrewThompson thanks now I will see a little to understand how to use it all –  Aug 24 '18 at 17:18
  • @AndrewThompson Hello I tried the method you suggested but I found the following error, you place the code on the main page, can you give me a hand? –  Sep 04 '18 at 22:23
  • *"you place the code on the main page"* What reported that error? Your IDE? The SO site? *"can you give me a hand?"* I'm trying, but I won't spoon-feed you (or anyone). Help your self by being more specific and copy/pasting **any** error or exception output. Note that uncompilable code snippet above is not an MCVE. Tip: Get it working with **one** font rather than see it fail with a dozen. – Andrew Thompson Sep 05 '18 at 00:09
  • @AndrewThompson I'm not looking for the solution, but I look for a point on which to solve the problem, I use two Noto and Roboto fonts, in my function that returns a font I search the font that supports the component text and that has the desired style. It works, It does what it needs to do only when I go to assign the font to the JButton in this case, the stucco is rendered as photos. I'm sorry if I'm giving you problems but I'm trying to learn –  Sep 05 '18 at 07:19

1 Answers1

3

'Roboto' is owned by Google... It doesn't support Arabic ... Google uses a fallback font named 'noto font' where 'roboto' isn't supported...

Tariqul Islam
  • 347
  • 4
  • 18
  • So either I change foundations or I find a way to make the change at the moment, right? –  Aug 24 '18 at 15:22