Does anyone knows how to detect printable characters in java?
After a while ( trial/error ) I get to this method:
public boolean isPrintableChar( char c ) { Character.UnicodeBlock block = Character.UnicodeBlock.of( c ); return (!Character.isISOControl(c)) && c != KeyEvent.CHAR_UNDEFINED && block != null && block != Character.UnicodeBlock.SPECIALS; }
I'm getting the input via KeyListener and come Ctr-'key' printed an square. With this function seems fairly enough.
Am I missing some char here?
The print(char) method of PrintStream Class in Java is used to print the specified char value on the stream. This char value is taken as a parameter. Parameters: This method accepts a mandatory parameter charValue which is the char value to be written on the stream. Return Value: This method do not returns any value.
Printable characters include letters, digits, and special punctuation such as commas, brackets, and question marks. Unprintable characters correspond to codes that indicate a special function such as a line feed, tab, or carriage return.
US-ASCII is a character set (and an encoding) with some notable features: Values are between 0–127 (x00–x7F) ASCII code-point 32 (decimal) represents a SPACE. ASCII code-point 65 represents the uppercase letter A.
C isprint()The isprint() function checks whether a character is a printable character or not. Those characters that occupies printing space are known as printable characters. Printable characters are just the opposite of control characters which can be checked using iscntrl().
It seems this was the "Font" independent way.
public boolean isPrintableChar( char c ) { Character.UnicodeBlock block = Character.UnicodeBlock.of( c ); return (!Character.isISOControl(c)) && c != KeyEvent.CHAR_UNDEFINED && block != null && block != Character.UnicodeBlock.SPECIALS; }
I'm not perfectly sure whether I understand your problem. But if you want detect if character can be drawn to Graphics object, and if not print some placeholder char you might find usefull:
Font.canDisplay(int)
It will check whether font can display specific codepoint (it is more that check whether font is displayable at all -- since there are chars that are displayable - like ą - but some fonts cant display them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With