Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Screen position for multi-byte characters

807589
Member Posts: 49,060 Green Ribbon
Hi all,
Some of the asian language charsets has multi-byte characters, and each of these characters might occupy different screen positions (by screen position i mean the pixel space occupied). This becomes a huge problem in setting the number of columns to display. Is there anyway we can pre-determine the screen-position occupied by each character.
thanks
Manivannan
Some of the asian language charsets has multi-byte characters, and each of these characters might occupy different screen positions (by screen position i mean the pixel space occupied). This becomes a huge problem in setting the number of columns to display. Is there anyway we can pre-determine the screen-position occupied by each character.
thanks
Manivannan
Comments
-
It's a problem with all proportional-spaced fonts, including western fonts -- the "M" is much wider than the "I", especially in a san-serif font.
As a result, you always have to set column widths for an estimated number of characters. The method [Font.getMaxCharBounds()|http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#getMaxCharBounds(java.awt.font.FontRenderContext)] will give you the maximum space that any character in the font will occupy, and you an either base you column size on that or some fraction of that. -
Edit: Sorry, I misread your question and answered a slightly different one. Feel free to ignore the following text ;-)
I assume you're talking about Swing.
First solution: Don't handle it yourself, simply use a Label that draws it as it wishes.
If you want to draw it yourself, use drawString(), which handles that for you.
If you still need to know more detail, try Font.createGlyphVector(), which will tell you exactly how Java will position the characters. -
Hi,
Thanks a lot for answering, but I am not using Swings. I am trying to display some Japanese characters, which is actually causing the problem -
Thanks a lot for answering, but I am not using Swings. I am trying to display some Japanese characters, which is actually causing the problemAnd what, pray tell, are you using to display those characters?
-
Hi,
thanks a lot for answering. My problem is specific to some unicode Characters. I havent faced any issue with ASCII so far. Can Font.getMaxCharBounds() be using for different charsets too? -
I am trying to display them in Windows Command line
-
Vannan wrote:Java is purely unicode and doesn't differentiate between charsets at that level. A Java String object is always represented in UTF-16 internally and can be handled as if it where pure Unicode bliss.
thanks a lot for answering. My problem is specific to some unicode Characters. I havent faced any issue with ASCII so far. Can Font.getMaxCharBounds() be using for different charsets too?
So this question doesn't make any sense.I am trying to display them in Windows Command lineForget it. The Windows console can display some ISO-8859 variations at most and most definitely can't correctly handle those characters. Choose another output format. -
I am trying to display them in Windows Command lineJava has absolutely no control over how the Windows displays characters on the command line, nor any ability to discover the fonts used to do so.
Unless your particular Windows implementation uses a fixed-width font that supports all characters needed, you are as JoachimSauer said, out of luck. If it does support such a character set, you will need to ensure that the default encoding for your Java program is the same as you Windows installation. -
@JoachimSauer
"Java is purely unicode and doesn't differentiate between charsets at that level. A Java String object is always represented in UTF-16 internally and can be handled as if it where pure Unicode bliss."
My problem occurs when I try to display those characters on a Terminal (it can be a linux Terminal also).
I have to display the Jap characters with a proper alignment, say in a space of 30 columns and truncate if any.
The problem is because each of the characters might occupy different space positions in the terminal. So i cant predetermine the number of characters to truncate or number of white spaces to append (so that it gets aligned in a column of say 30 spaces) unless there is a way to do so in JAVA. Initially i thought number of bytes in the character will be proportional to the space position occupied but its not the case.
My output for eg shud be:
Col A------ ColB------ ColC------
abcdefgh asdasdwa asdasdas
Am I clear? -
There is no "number of bytes" in a pure Java String (no, getBytes() only returns the bytes in your platforms default encoding, it has nothing to do with how the String is stored in memory).
Also, as you noticed the length of the String in bytes by some specified encoding doesn't necessarily correlate with its visible width.
If you need such advanced layouting then you should really choose a different output medium instead of the console. I'd suggest HTML output for example (in which case the Browser will handle all of the complex width-measurement).
Also, I'd be very surprised if the Windows console can display those double-width characters at all (some (most modern) Linux terminals can display all Unicode characters for which they can find a Font).
If you absolutely want to do it on the console, then find a copy of the unicode standard and find out which characters in there are defined to be Double-Width characters and calculate it yourself (or be very pessimistic and assume every character is double-width).
This discussion has been closed.