I am using custom font in my Swing application and loading it like this:
URL l = ClassLoader.class.getResource("/resource/template/CarroisGothic-Regular.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, l.openStream());
font = font.deriveFont(Font.PLAIN, 13);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
UIManager.put("ComboBox.font", font);
It works fine on systems set where regional settings/language is set to English but for other languages (Russian for example) it displays garbage characters.
This is how I am displaying name of months:
String[] months = new DateFormatSymbols().getMonths();
for (int i = 0; i < months.length && i < 12; i++) {
cmbMonth.addItem(months[i]);
if(i == Calendar.getInstance().get(Calendar.MONTH) + 1) {
cmbMonth.setSelectedIndex(i);
}
}
The font names defined in your markup would be directing the system's cache of fonts and not your application's,this is the reason why your application isn't displaying the desired font(s).
you should try and install the font locally on the machine and use it in your application.
Related
I have a problem on Roboto font where the Arabic language is not displayed correctly, on the fonts I'm not very prepared but I think it does not support Arabic unicode.
My project deals with a material theme for swing and therefore I would not change fonts for the Arabic language support so I thought that when the theme is loaded I check for a keyboard layaut and if that language is not supported by a robot then change font to loading, this is the only solution I could find but the solution does not work for devices without a keyboard.
Now I would like to ask you two questions:
Is it possible to find the roboto font that supports Arabic and the languages of that lineage there?
Is there any better solution than mine if I need to change font for support?
---------- Problem Load Font to GraphicsEnvironment -------
I have rewritten fonts of different types and now through a method written by me I go to set the font according to the style and the support of the String but I get this problem when I go to render the look and feel
static {
try {
//Noto sans
Font black = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Black.ttf"));
Font blackItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-BlackItalic.ttf"));
Font bold = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Bold.ttf"));
Font boldItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-BoldItalic.ttf"));
Font italic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Italic.ttf"));
Font light = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Light.ttf"));
Font lightItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-LightItalic.ttf"));
Font medium = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Medium.ttf"));
Font mediumItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-MediumItalic.ttf"));
Font regular = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Regular.ttf"));
Font thin = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Thin.ttf"));
Font thinItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-ThinItalic.ttf"));
//Register font
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
graphicsEnvironment.registerFont(black);
graphicsEnvironment.registerFont(blackItalic);
graphicsEnvironment.registerFont(bold);
graphicsEnvironment.registerFont(boldItalic);
graphicsEnvironment.registerFont(italic);
graphicsEnvironment.registerFont(light);
graphicsEnvironment.registerFont(lightItalic);
graphicsEnvironment.registerFont(medium);
graphicsEnvironment.registerFont(mediumItalic);
graphicsEnvironment.registerFont(regular);
graphicsEnvironment.registerFont(thin);
graphicsEnvironment.registerFont(thinItalic);
black = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Black.ttf"));
//blackItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/Roboto/Roboto-BlackItalic.ttf"));
bold = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Bold.ttf"));
boldItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-BoldItalic.ttf"));
italic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Italic.ttf"));
light = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Light.ttf"));
lightItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-LightItalic.ttf"));
medium = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Medium.ttf"));
mediumItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-MediumItalic.ttf"));
regular = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Regular.ttf"));
thin = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Thin.ttf"));
thinItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-ThinItalic.ttf"));
//register font
graphicsEnvironment.registerFont(black);
//graphicsEnvironment.registerFont(blackItalic);
graphicsEnvironment.registerFont(bold);
graphicsEnvironment.registerFont(boldItalic);
graphicsEnvironment.registerFont(italic);
graphicsEnvironment.registerFont(light);
graphicsEnvironment.registerFont(lightItalic);
graphicsEnvironment.registerFont(medium);
graphicsEnvironment.registerFont(mediumItalic);
graphicsEnvironment.registerFont(regular);
graphicsEnvironment.registerFont(thin);
graphicsEnvironment.registerFont(thinItalic);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Font getFont(String textCompatibily, String tipeFont){
String familyFontRoboto = "Roboto";
String familyFontNotoSans = "Noto Sans";
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
//System.out.println(familyFontRoboto + " " + tipeFont);
for(Font font : graphicsEnvironment.getAllFonts()){
if(font.canDisplayUpTo(textCompatibily) < 0){
if(font.getName().equals(familyFontRoboto + " " + tipeFont)){
System.out.println("Roboto ok");
System.out.println(font.toString());
return font;
}
}
}
for(Font font : graphicsEnvironment.getAllFonts()){
if(font.canDisplayUpTo(textCompatibily) < 0){
if(font.getName().equals(familyFontNotoSans + " " + tipeFont)){
System.out.println("noto sans ok");
return font;
}
}
}
System.out.print("ops");
return graphicsEnvironment.getAllFonts()[0]; // Noto sans dovrebbe coprire tutto, altrimenti questa cosa si allunga
}
setting font on the button
Font font = MaterialFonts.getFont(button.getText(), "Black");
button.setFont (font);
problem rendering is
enter image description here
'Roboto' is owned by Google... It doesn't support Arabic ... Google uses a fallback font named 'noto font' where 'roboto' isn't supported...
So I am using PDFBox to fill in some pdfs. So far everything was great - I created a form in pdf with Avenir Light font, and I could fill it in. However, the problem that just now showed up, is that when I am trying to fill the pdf using letters such as ł, ą, ć ... I get the following error:
U+0142 is not available in this font's encoding: MacRomanEncoding with differences
with different numbers.
Now, my question is - how can I fix this, so that I can fill the form automatically? When I open the pdf in Acrobat Reader, I can insert those letters, and I dont get any errors. Here is how I set the field:
public void setField(PDDocument document, PDField field, String value ) throws IOException {
if( field != null && value != null) {
try{
field.setValue(value);
} catch (Exception e){
e.printStackTrace();
}
}
else {
System.err.println( "No field found with name:" + field.getPartialName() );
}
}
UPDATE
I've been trying to upload my own Avenir-Light.tff like this:
PDFont font = PDType1Font.HELVETICA;
PDResources res = new PDResources();
COSName fontName = res.add(font);
acroForm.setDefaultResources(res);
String da = "/" + fontName.getName() + " 12 Tf 0 g";
acroForm.setDefaultAppearance(da);
However, this doesn't seem to have any impact on the printed fields, and throws almost the same message:
U+0104 ('Aogonek') is not available in this font Helvetica (generic: ArialMT) encoding: WinAnsiEncoding
PDFBox define 14 standard fonts in PDType1Font :
PDType1Font.TIMES_ROMAN PDType1Font.TIMES_BOLD
PDType1Font.TIMES_ITALI PDType1Font.TIMES_BOLD_ITALIC
PDType1Font.HELVETICA PDType1Font.HELVETICA_BOLD
PDType1Font.HELVETICA_OBLIQUE
PDType1Font.HELVETICA_BOLD_OBLIQUE PDType1Font.COURIER
PDType1Font.COURIER_BOLD PDType1Font.COURIER_OBLIQUE
PDType1Font.COURIER_BOLD_OBLIQUE PDType1Font.SYMBOL
PDType1Font.ZAPF_DINGBATS
So if you want to use Avenir-Light you have to load it from a .ttf file. You can do this as #TilmanHausherr suggested PDType0Font.load(doc, new File("path/Avenir-Light.ttf"), false).
PDFont font = PDType0Font.load(doc, new File("path/Avenir-Light.ttf"), false);
PDResources res = new PDResources();
COSName fontName = res.add(font);
acroForm.setDefaultResources(res);
String da = "/" + fontName.getName() + " 12 Tf 0 g";
acroForm.setDefaultAppearance(da);
Update
Do you know why it also displays a warning if form of: OpenType Layout
tables used in font Avenir-Light are not implemented in PDFBox and
will be ignored?
Avenir-light font uses OpenType Layout tables (Advanced Typographic) that PDFBox does not support yet. This advaned typographics will be ignored
I am using Vector to add elements in a table in a blackberry project. The font does not seem to change programmatically. I have tested it in different screen blackberry phones. In BOLD & CURVE it seems fine, but in large screen phones like 9810 torch, 9790 BOLD, it takes some default font which is very big. Even if I change the font of the phone through setup, it changes the font of LabelFields and TextFields but applying FontFamily font does not reflect on Vector elements.
I am attaching the screenshots from 9800 & 9810...In 9800 it appears fine, in 9810, it looks big
Try this
for (int x = 0; x < vector.size(); x++) {
FriendListObject b = (FriendListObject) vector.elementAt(x);
name_ = b.getf_name().toString();
}
TableRowManager row = new TableRowManager() {
public void paint(Graphics g) {
g.setBackgroundColor(0xa2b8c3);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xe5e7e7);
g.clear();
super.paint(g);
}
};
LabelField name= new LabelField(name_+" :", DrawStyle.ELLIPSIS);
name.setFont(Font.getDefault().derive(Font.PLAIN));
row.add(name);
same situation I am also faced then,I customised fontfamily for each resolution.for example
if (Display.getWidth()==480 && Display.getHeight()==360) {
_custHeadNews = new CustTextField(_newsHead,30,0x05235b,TextField.FOCUSABLE);
_custMainNews = new CustTextField(_newsMain,25,0x666666, RichTextField.FOCUSABLE);
_custMetadata = new CustTextField(_metaData,15,0x666666,TextField.FOCUSABLE);
}
else if (Display.getWidth()==320 && Display.getHeight()==240) {
_custHeadNews = new CustTextField(_newsHead,25,0x05235b,TextField.FOCUSABLE);
_custMainNews = new CustTextField(_newsMain,15,0x666666, RichTextField.FOCUSABLE);
_custMetadata = new CustTextField(_metaData,10,0x666666,TextField.FOCUSABLE);
}
bolded font size for different resolutions.
This issue was solved by making changes in RegionStyles. Initially we had given Font parameter as null. After changing it to appFont1 instead of null, the app font stopped overriding and works fine now.
RegionStyles style = new RegionStyles(BorderFactory.createSimpleBorder(new XYEdges(5,0,0,0), Border.STYLE_TRANSPARENT), appFont1, null,null, RegionStyles.ALIGN_LEFT, RegionStyles.ALIGN_MIDDLE);
I am new to Java and would like to know how to set the font and font color to be used for the next text to be added to a SWT StyledText box.
So for example I have an application that defines "command" and "data" text and each is to be displayed in a different font/color. So let's say I've just added some "command" text. Now how do I set things up so that the next text which will be "data" text is displayed in a different font and color?
I've done a lot of googling, but nothing seems to be helping me.
P.S.: This can't be the most efficient way to do it:
int a = st.getCharCount();
Font font = new Font(shlProtruleModifier.getDisplay(), "Courier", 10, SWT.NORMAL);
StyleRange[] sr = new StyleRange[1];
sr[0] = new StyleRange();
st.append("\r\nWhat the heck?");
sr[0].start = a;
sr[0].length = st.getCharCount() - a;
sr[0].font = font;
sr[0].foreground = SWTResourceManager.getColor(SWT.COLOR_BLACK);
st.replaceStyleRanges(sr[0].start, sr[0].length, sr);
So all I've been able to come up with the following technique that does work,
int a = st.getCharCount();
Font font = new Font(shlProtruleModifier.getDisplay(), "Courier", 10, SWT.NORMAL);
StyleRange[] sr = new StyleRange[1];
sr[0] = new StyleRange();
st.append("\r\nWhat the heck?");
sr[0].start = a;
sr[0].length = st.getCharCount() - a;
sr[0].font = font;
sr[0].foreground = SWTResourceManager.getColor(SWT.COLOR_BLACK);
st.replaceStyleRanges(sr[0].start, sr[0].length, sr);
We have an Applet that can possibly display Chinese text. We are specifying a font for it (Arial), it works fine under both Windows and Mac OSX.
But in Firefox on Linux the Chinese characters are rendered as squares. Is there a way to work around this? Note that we can't assume the existence of a particular font file on the client.
This indicated that the font does not support Chinese characters (which you probably guessed).
You might find the java.awt.Font.canDisplayUpto() method interesting.
http://www.j2ee.me/javase/6/docs/api/java/awt/Font.html#canDisplayUpTo(java.lang.String)
"Indicates whether or not this Font can display a specified String. For strings with Unicode encoding, it is important to know if a particular font can display the string. This method returns an offset into the String str which is the first character this Font cannot display without using the missing glyph code. If the Font can display all characters, -1 is returned."
That's because Arial on Windows and Mac are all Unicode font but it only has Latin-1 charset on Linux. On many Linux distributions, Chinese fonts are optional and there may not be Chinese font available.
A common technique is to searching through all your font to see any of them can display Chinese characters. For example,
static final Font defaultFont =new Font( "Arial Unicode MS", Font.BOLD, 48 );
static private Font[] allFonts;
static final char sampleChineseCharacter = '\u4F60'; // ni3 as in ni3 hao3
public static void loadFonts() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
allFonts = env.getAllFonts();
int nFonts = allFonts != null ? allFonts.length : 0;
fontNames = new String[nFonts];
fontMap = new Hashtable();
String currentFamily = "";
int j = 0;
for ( int i = 0; i < nFonts; i++ ) {
Font font = allFonts[ i ];
System.out.println( allFonts[ i ] );
if ( font.canDisplay( sampleChineseCharacter )) {
currentFamily = font.getFamily();
Object key = fontMap.put( currentFamily, font );
if ( key == null ) {
// The currentFamily hasn't been seen yet.
fontNames[ j ] = currentFamily;
j++;
}
}
}
String tmp[] = fontNames;
fontNames = new String[j];
System.arraycopy( tmp, 0, fontNames, 0, j );
}
You might have to pass the following param in the object tag:
<param name="java_arguments" value="-Dfile.encoding=utf-8" />
I found the code here inadequate for my needs.
I needed to test an unknown input string, to determine what font to use, hence, I needed to check every single character. (see below)
By the way, the font.canDisplayUpTo method will not work. It may approve a font, that can only display some of the characters.
So, just use this code below.
Font[] allFonts;
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
allFonts = env.getAllFonts();
Font targetFont = null;
for ( int i = 0; i < allFonts.length; i++ )
{
Font font = allFonts[ i ];
boolean canDisplayAll = true;
for (char c : text.toCharArray())
{
if (!font.canDisplay(c))
{
canDisplayAll = false;
break;
}
}
if (canDisplayAll)
{
logger.debug("font can display the text " + font.getName());
targetFont = font;
break;
}
else
{
logger.debug("cant display " + font.getName());
}
}