Results 1 to 7 of 7
  1. #1
    Draque is offline Member
    Join Date
    May 2015
    Posts
    3
    Rep Power
    0

    Default Font selection per character

    Hello, all! I've got a pretty specific goal and I was hoping to get some advice on the best way to achieve it.

    The long and the short of the question is this: How do I detect whether a given character exists in a font that I am using?

    Here are the particulars. I have a program that allows users to select their own font for text display (a core element of the functionality requires this). There are situations when a user will enter text including characters that do not exist in the font. I need a way to make this default the non-displayable characters to a preselected, unicode compatible font that I already have.

    The second element of this will be figuring out how to have the AWT elements know to swap out only certain characters when creating the text, and any suggestions there would be welcome as well.

    Sorry that this issue isn't as straightforward as a block of code, but I'm not quite sure where the best place to start is.

    Any help appreciated!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Font selection per character

    You might use the Font.canDisplay() methods to see if the provided character or code point maps to a defined glyph. But that may not solve your problem. The character 'A' or code point 0x41 (in ASCII) may have a defined glyph that displays a triangle or some other symbol, depending on the font.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Draque is offline Member
    Join Date
    May 2015
    Posts
    3
    Rep Power
    0

    Default Re: Font selection per character

    Thanks for the suggestion! That will definitely do what I want. All I need to do is check whether there's any valid representation (rather than just the dreaded square).

    Do you know whether there's any simple way to mix characters from different fonts within a single Swing text field? Text areas and the like are set to interpret via HTML, so they aren't an issue. Thanks for the help!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Font selection per character

    You need to check out the Font class. There are font creation methods that let you construct your own fonts. I have not used them myself so I can't offer any guidance.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Font selection per character

    You have to modify the document itself, not the Font. Below is a rundown of what you need to do. Obvoiously you need to adapt this to your situation, but the AttributeSet is the way to go.
    Java Code:
            DefaultStyledDocument doc = new DefaultStyledDocument();
            JTextPane pane = new JTextPane(doc);
            MutableAttributeSet set = new SimpleAttributeSet();
            StyleConstants.setFontFamily(set, "Arial");
            doc.setCharacterAttributes(fromIndex, length, set, true);
    In practice, after typing, you can use something like this:
    Java Code:
            MutableAttributeSet set = new SimpleAttributeSet();
            StyleConstants.setFontFamily(set, "Arial"); //or some other font that displays the character
            int i;
            while ((i = pane.getFont().canDisplayUpTo(pane.getText())) > -1) {
                doc.setCharacterAttributes(i, 1, set, true);
            }
    I am not sure if this would be a performance hit for a big document, but there is only one way to find out :)
    Last edited by SurfMan; 10-18-2017 at 09:56 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Draque is offline Member
    Join Date
    May 2015
    Posts
    3
    Rep Power
    0

    Default Re: Font selection per character

    Thank you for the suggestion there! My users will absolutely love this once I have it implemented!

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Font selection per character

    Quote Originally Posted by Draque View Post
    Thank you for the suggestion there! My users will absolutely love this once I have it implemented!
    Glad to be of help. Would love to see some screenshots and perhaps some code when you're done. Keep up the good work!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2012, 02:29 PM
  2. Replies: 0
    Last Post: 11-06-2012, 02:09 PM
  3. Replies: 0
    Last Post: 11-06-2012, 02:09 PM
  4. Replies: 0
    Last Post: 08-18-2010, 10:31 AM
  5. Replies: 5
    Last Post: 04-25-2009, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •