Using external fonts in a Java Applet - java

Is it possible to use a font not on the user's machine for text displayed in a java applet.
Like linking to a ttf font file in the same location as the java applet almost in an #fontface fashion.

You can use Font.createFont with fontFormat TRUETYPE_FONT:
Font f = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf") );
As also described in Sun-Tutorial Working with Text APIs the returned font size is 1 pt, you can change this afterwards:
f = font.deriveFont(12f);

Related

IOException When Loading Java Font

I'm implementing a GUI for a chess program I'm writing for a class. To make it look polished, I want to utilize a Chess Piece Font I obtained from the internet.
The file, chess.ttf, is located at the path Chess/resources/chess.ttf. I utilize the following code per Oracle's instructions (https://docs.oracle.com/javase/tutorial/2d/text/fonts.html):
try {
File file = new File("resources/chess.ttf");
//Returned font is of pt size 1
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, file));
//Derive and return a 12 pt version:
//Need to use float otherwise
//it would be interpreted as style
return font.deriveFont(12f);
}
catch (IOException|FontFormatException e) {
System.out.println("-Font Error-");
return null;
}
However, this throws an IOException. I ran getAbsolutePath() on the file and received /Users/[me]/eclipse-workspace/Chess/resources/chess.ttf so I'm assuming the file is being loaded correctly. Does anyone know what's going wrong with my code?
edit: Problem resolved? I tried InputStreams as suggested but that didn't work, yet upon reverting my code the computer finally stopped throwing IOExceptions. Isn't code the best?
You'd better copy the ttf using build script like ant or maven /Users/[me]/eclipse-workspace/Chess/bin/resources
ClassLoader loader = Thread.currentThread().getContextClassLoader();
System.out.println(loader.getResource(".").getPath()); // for tracing the working folder only
java.io.InputStream ins = loader.getResourceAsStream("resources/chess.ttf");
//Returned font is of pt size 1
Font font = Font.createFont(Font.TRUETYPE_FONT, ins);

iText 7 - Loading font from resources\classpath in war file

I am using iText7 trying to load a font which is in src/main/resources/pdf/fonts, when I give the full path to the font on my filesystem and run my code from a main method I don't have any problems, the issues occur when I am running the code inside a war file with tomcat.
I have tried a few different ways to load the font but none of them seem to work.
Either iText complains that the font is not recognised com.itextpdf.io.IOException: font.is.not.recognized (example one below) or I get a null pointer exception with example two (below) as the code is unable to load the font using the supplied path (although this works when giving the full file system path and running via a main method).
I can see that the font is definitely being included in the war file in the correct directory (/WEB-INF/classes/pdf/fonts). Below is what I am trying to do:
private static final String FONT = "/pdf/fonts/Frutiger-LT-Std-55-Roman_18821.ttf";
try (InputStream is = MyClass.class.getClassLoader().getResourceAsStream(FONT))
{
byte[] fontBytes = IOUtils.toByteArray(is);
final PdfFont font = PdfFontFactory.createFont(fontBytes, PdfEncodings.IDENTITY_H);
}
Or:
private static final String FONT = "/pdf/fonts/Frutiger-LT-Std-55-Roman_18821.ttf";
final PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
I am not sure what it is that I need to do to load the font for iText to use when running in a web app.
I am thinking that maybe as the font does seem to have been read into the input stream that I need to somehow register the font with iText before passing it the byte array..

How to hide confirmConversion dialog when open docx file with help 2003

I use eclipse swt in my application. It allows me open word files (doc, docx, rtf).
But I cannot hide "Confirm Conversion at Open" dialog programmaticaly when open docx file (doc or rtf open fine). Windows XP SP3, Microsoft Word 2003 SP3 and FileFormatConverters installed on machine.
In VBA this dialog hides when set property Application.Options.ConfirmConversions = false. Or sets property when open file:
Documents.Open FileName:="file_path_name", ConfirmConversions = false.
I set this property with help OleAutomation, but this doesn't work.
Bellow part of code that I use:
shell = SWT_AWT.new_Shell(SwtHelper.display, canvas);
shell.setLayout(new FillLayout());
try {
frame = new OleFrame(shell, SWT.NONE);
clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document", file);
clientSite.doVerb(OLE.OLEIVERB_SHOW);
} catch (SWTException e) {}
This code work fine
How I can hide Confirm Conversion dialog when open docx using swt and word 2003?
Thanks.
You should try Word.Application instead of Word.Document.
There is huge difference whether you use Word.Document or Word.Application.
Word.Application will let you pass arguments to ole.
Please have a look at my comment given here: https://stackoverflow.com/a/17021512/1285811

How to get the window size of the active/focused application using java?

I have a program written in JAVA that creates a JPG file at a specified path whenever PRINTSCREEN is pressed. This program runs in background and uses the JIntellitype API.
My problem is that:
screensize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
is the only available function I could find to get the screen size which I later use to get a buffered image like this:
bf = screenRobot.createScreenCapture(screensize);
where bf is a BufferedImage data type.
Is there any way that I could get the screensize of the window that is currently active so that unwanted content is not saved in the JPG file?
Frame.getX()
Frame.getY()
Frame.getWidth()
Frame.getHeight()

Use resource font directly in Java

How to use resource font directly in Java?
To load a font (.ttf) from file, have a look at Load font from ttf file.
Key lines being:
InputStream is = DemoFonts.class.getResourceAsStream(fName);
font = Font.createFont(Font.TRUETYPE_FONT, is);
The font could then be used for a JLabel through the usual setFont method.

Categories

Resources