Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7595471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:40:49+00:00 2026-05-30T21:40:49+00:00

I’m using g.drawString(str, x, y) to draw a String with a Graphics2D object g

  • 0

I’m using g.drawString(str, x, y) to draw a String with a Graphics2D object g. The current font of g does not cover all the characters of str (I have e.g. Chinese chars in there). On Mac OS X, a fallback font seems to be automatically used, but not on Windows, where black square outlines appear instead of the wanted characters.

  • Why is the behavior different depending on the platform?
  • How do I specify a fallback font (or several fallback fonts) in case of missing characters?

(For instance, one of the nice fonts there.)

Update/More Info

So, the original font that doesn’t support all characters is not one of the JVM’s logical fonts, but is a bundled font that comes with my app and was obtained with Font.createFont(). So, adding fonts to the JRE’s lib/fonts/fallback folder doesn’t work here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:40:50+00:00Added an answer on May 30, 2026 at 9:40 pm

    We could attribute string to switch font on “bad” symbols and use Graphics2D.drawString(AttributedCharacterIterator iterator, int x, int y) to render result. Advantage: this will work with any font. Drawback: without some sort of caching of intermediate objects this will work slower and dirtier.

    So, i suggest using AttributedString with main font attribute on whole string:

    AttributedString astr = new AttributedString(text);
    astr.addAttribute(TextAttribute.FONT, mainFont, 0, textLength);
    

    and with fallback font on specific parts:

    astr.addAttribute(TextAttribute.FONT, fallbackFont, fallbackBegin, fallbackEnd);
    

    The rendering itself:

    g2d.drawString(astr.getIterator(), 20, 30);
    

    The result (Physical “Segoe Print” as main font, logical “Serif” as fallback):

    enter image description here

    Complete supposed-to-be-SSCCE code:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.font.TextAttribute;
    import java.text.AttributedString;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    public class FontTest extends JFrame {
    
        public FontTest() {
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(new TestStringComponent());
            pack();
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new FontTest().setVisible(true);
                }
            });
        }
    }
    
    class TestStringComponent extends JComponent {
    
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
    
            g.setColor(getForeground());
    
            Font mainFont = new Font("Segoe Print", Font.PLAIN, 25);
            Font fallbackFont = new Font("Serif", Font.PLAIN, 25);
    
            String s = "Test 漢鼎繁古印 Test 漢鼎繁古印 Test";
    
            g2d.drawString(createFallbackString(s, mainFont, fallbackFont).getIterator(), 20, 30);
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(500, 40);
        }
    
        private AttributedString createFallbackString(String text, Font mainFont, Font fallbackFont) {
            AttributedString result = new AttributedString(text);
    
            int textLength = text.length(); 
            result.addAttribute(TextAttribute.FONT, mainFont, 0, textLength);
    
            boolean fallback = false;
            int fallbackBegin = 0;
            for (int i = 0; i < text.length(); i++) {
                boolean curFallback = !mainFont.canDisplay(text.charAt(i));
                if (curFallback != fallback) {
                    fallback = curFallback;
                    if (fallback) {
                        fallbackBegin = i;
                    } else {
                        result.addAttribute(TextAttribute.FONT, fallbackFont, fallbackBegin, i);
                    }
                }
            }
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.