JTextPane and TTF fonts
Hi all.
I'd like use TrueType Font within the JTextPane.
For some reason i can't use it.
Why?
Is there is a way to override some setting or anything.
This is my code
privatevoid setUPFonts(){
Font braille_font;
try{
braille_font = Font.createFont(Font.TRUETYPE_FONT,new File("NewBraille.ttf"));
}catch (FontFormatException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
braille_font = braille_font.deriveFont(Font.TRUETYPE_FONT,30);
textPane2.setFont(braille_font);
}
for some reason after this code is run I still see Java's default font.
Thanx
> Hi all.
> I'd like use TrueType Font within the JTextPane.
> For some reason i can't use it.
> Why?
> Is there is a way to override some setting or
> anything.
> This is my code
>
<snip>
braille_font.deriveFont(Font.TRUETYPE_FONT,30);
FYI: The argument Font.TRUETYPE_FONT
is a not valid argument for the deriveFont(int style, float size)
method. Usebraille_font.deriveFont(30.0f);
instead.
As for your problem, the following code seems to work on my machine. I don't have the specific Font you mention, but if my code runs OK for you, then perhaps there is something wrong with the font itself.
Jim S.
====================
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTextPane;
public class TPFontTest {
private JTextPane tp;
private ArrayList<File> ttfFiles;
public TPFontTest() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tp = new JTextPane();
tp.setText("The quick brown fox jumped over the lazy dog");
f.add(tp);
f.setSize(400,300);
f.setVisible(true);
startCycling();
}
public static void main(String[] args) throws Exception {
new TPFontTest();
}
private void startCycling() {
buildTTFList();
/* Start a Thread to cycle through all the known TTF fonts */
Thread t = new Thread(new Runnable() {
int index = 0;
volatile boolean shouldRun = true;
public void run() {
while (shouldRun) {
cycleFont(index);
index++;
if (index == ttfFiles.size()) {
shouldRun = false;
}
try {
Thread.sleep(20);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
});
t.start();
}
private void buildTTFList() {
File fontDir = new File("C:\\WINDOWS\\Fonts\\");
File[] fontFiles = fontDir.listFiles();
ttfFiles = new ArrayList<File>();
for (File nextFile : fontFiles) {
if (nextFile.getName().toUpperCase().endsWith("TTF")) {
ttfFiles.add(nextFile);
}
}
}
private void cycleFont(final int index) {
Font font = null;
try {
font = Font.createFont(Font.TRUETYPE_FONT, ttfFiles.get(index));
font = font.deriveFont(24.0f);
tp.setFont(font);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Seems your problem in font creation of DefaultStyledDocument.
See getFont() method of the class.
public Font getFont(AttributeSet attr) {
StyleContext styles = (StyleContext) getAttributeContext();
return styles.getFont(attr);
}
//StyleContext getFont
public Font getFont(AttributeSet attr) {
// PENDING(prinz) add cache behavior
int style = Font.PLAIN;
if (StyleConstants.isBold(attr)) {
style |= Font.BOLD;
}
if (StyleConstants.isItalic(attr)) {
style |= Font.ITALIC;
}
String family = StyleConstants.getFontFamily(attr);
int size = StyleConstants.getFontSize(attr);
/**
* if either superscript or subscript is
* is set, we need to reduce the font size
* by 2.
*/
if (StyleConstants.isSuperscript(attr) ||
StyleConstants.isSubscript(attr)) {
size -= 2;
}
return getFont(family, style, size);
}
public Font getFont(String family, int style, int size) {
fontSearch.setValue(family, style, size);
Font f = (Font) fontTable.get(fontSearch);
if (f == null) {
// haven't seen this one yet.
f = new Font(family, style, size);
if (! FontManager.fontSupportsDefaultEncoding(f)) {
f = FontManager.getCompositeFontUIResource(f);
}
FontKey key = new FontKey(family, style, size);
fontTable.put(key, f);
}
return f;
}
As you can see the approach can't recognize your font.
The simplest way to fix is to override getFont() method of the document to return required font.
regards,
Stas