how to manage formatting of text when read a save file? - java

i have a java applet application in which i use rich text area . i write URDU the national language of PAKISTAN. i managed to do so with uni codes. the problem is, when i write urdu in text area and select a font and color for each line it do all of this but when i save this file using UTF-8 encoding and then open it again it shows all text formatted as i choose format of last line.
my requirement is to open file as it is saved. i mean each file should have same formatting as i done before saving.
I'm still suffering with this problem even after bounty can any one help! dated 07-06-2010.

See, when you actually format text using some font and color, it will generate some RTF/HTML code right? You should try to get the RTF/HTML of the text area so that all your formatting can be saved in a file.
Basically all its a text file, so you need to get it with all code right?
Check this link for RTF formatted text saving mechanism.
Java JTextPane RTF Save
Also check HTMLEditorKit for more info.
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/html/HTMLEditorKit.html
thanks.

UTF-8 is an assignment of codes to characters. For convenience a decision was made that the lowest 127 codes are the same in ASCII and UTF-8. For all characters the codes differ.
UTF-8 Fonts have a character map (cmap) which assigned unicode code to their glyphs. There are very few fonts that cover large portions of the unicode range (Arial Unicode and Gentium i know, there are some others), and to get full coverage in a rendering solution, you have to mix fonts.
To be able to display arbitray Unicode texts, you therefore have to create a set of fonts with one as the default font and fallback fonts for the unicode characters that are not contained the default font. Back to Java and your Textpane: If you select a font for a given part of text in your Textpane, this only means that to render the text glyph are used from the selected font. But the text itself is not associated with the font in any means.
So you have two options:
You don't just store the UTF-8 text,
but also information about the
selected font, or
more interesting:
You store the text simply as UTF-8
and apply fonts after loading the
text into your textpanel!

Related

Emoji inside Whatsapp chat

I am having a Whatsapp chat txt file inside of which emojis have been replaced by such text------> "���"
I want to convert that text to specifics emojis. How can I do that and use it inside a java application?
As #Abhishek pointed out, you need to use a different type of encoding. Whatsapp does backup with UTF-8 converting the emoticons into string representations. If you want to see the real emoji, you will have to use Unicode instead. Unicode contains sections which specify emoji as "characters". They're regular characters, you only need a font which can display them. Also see the Unicode Emoji FAQ.
In a text file, characters are basically encoded as numbers in the form of bytes. To display those visually on a computer screen you need a font which contains the visual glyph to render this character. Since the process is always numeric identifier → font → visible glyph, it should be pretty obvious that a "character" can be anything visual, including emoji or any other image.
Maybe all you need is a a font which contains the visual glyph to render these characters. See this for reference.

java print in colour in text file

hi there I have a Java program I wanna know how to print the result of this program in a text file without losing it's colour .I mean the out put is in colour and I want to have the colourfull printed result Thanks
You can't.
Plain text is just that. There is no formatting in a plain text file that lets you specify color/font/size.
However, if you are displaying the text in a Bash shell or have configured your windows command console correctly, you could use ANSI Escape Codes to format the text.
You can't. Textfiles don't have colors.
You could wrap them in HTML tags and style them with css. (there are probably libaries that do that for you). This HTML file can be viewed with a webbrowser.
You could also use ANSI escape code to format your text (e.g. https://github.com/fusesource/jansi)

How to use Character.UnicodeBlock while I set some strings in a JEditorpane

I need to display some Bengali characters. I've tried to set the font to a Bengali Unicode font but It does not work properly. The last hope to fulfill my project is to use Character.UnicodeBlock. But I do not have any idea about it. Is it really possible to get the actual display of any Unicode character in Java? How can I use Character.UnicodeBlock in a component?
I assume Java 7.
First one needs a Unicode font.
If this font goes into the Windows fonts, and there is no name clash with an already existing font, everything should work.
Otherwise one might store the font as resource file in the application:
InputStream fontIn = getClass().getResourceAsStream("/.../... .ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
After this jEditorPane.setFont(font) should work. Mind, the text in the JEditorPane should not be HTML, where own fonts might be set.
It is tricky, because of font substitution, on font decoding using names.
Another problem might be hard-coded strings in the java source: the encoding of the java source (the editor) must be the same as is used by the javac compiler. For international projects best both UTF-8 (javac -encoding=UTF-8 ...). To test whether there is a problem with that one can test with "\u099C" for জ.

How to change the fontsize when writing to a text file

I am writing information to a text file, how do i I change the font size for Data that i am writing to the text file. also how do you set the data to be written in bold or underlined.
Actually, the .txt format does not contain formatting such as font size, bond, italics, etc.
You probably want to use a markup language like html, or another file format, such as rtf, pdf or something else.
Here's some information on learning html: w3schools.com
And a java library for writing rtf: srw library
And a java library for writing pdf: pdfbox
Good luck.
Plaintext files do not have format and style options.
For this you would have to write the data in a different format. E.g. html if you want to view it in a browser later.
But the format you chose greatly depends upon how and by whom those data should be read.
Supposing you are editing your text in a JTextArea you might do this:
this.textArea.setFont(new Font("Arial", Font.PLAIN, 12));
//the code comments itself
if you want bold then write Font.BOLD
As someone already told you, unfortunately plain .txt files don't contain formatting. If you need to format the text, you should change format and try HTML.
As all people say .txt files don't have format options.
I want to clarify something
It's possible have a txt file with specific format.
The way to do it is with a specific viewer:
You can add style, text size with notepad
You can add underlines or line spacing with notepad++
depending on you txt viewer you can postformat.
Then a possible solution would be POSTFORMATING, it works, for example if you want to print your file, only change setting on your viewer and you could obtain the result that you are looking for.
I'm working in this way for printing my txt reports.

Printing Unicode characters in java

I'm developing a Sinhala-English Unicode translator on Java. When I print a Unicode character in a JTextPane, it only shows a blank box. But when I copy that box to the notepad in windows it shows me the letter.
The problem is that Java not showing the Unicode characters instead windows.
How can I fix this problem ?
It's likely that the font you are using in your JTextPane does not fully support the Unicode range that you are trying to display. Try setting the text area's font to something more Unicode-friendly (see the row labeled "Sinhala (80: 0D80–0DFF)").

Categories