G4P - custom Fonts with GTextField and GTextArea controls
in
Contributed Library Questions
•
5 months ago
I have raised this topic in reponse to a PM since the answer was likely to be useful to others and because Zoho in their wisdom do not allow formatted text or even multiple paragraphs in PMs.
Version 3 of G4P introduced the StyledText class which enabled text used for labels, buttons etc. to be styled e.g. italic, bold, underline etc. It means that G4P now uses the java.awt.Font class for all controls that display text.
To get a similar output on different platforms (Windows, OSX) then the font used must be installed on the computer system running the sketch (or viewing the applet) which limits the range of fonts available.
With GTextField and GTextArea controls the setFont(Font) method can be used to specify the font to use - this requires a java.awt.Font object passed as a parameter. The problem of cross-platform availablity still exists.
The code below demonstrates how you can load and use a custom True Type Font (TTF). The actual font file (has .ttf extension) must be in the data folder which is inside the sketch folder. Also beware that there may be copyright issues when distributing the font.
Version 3 of G4P introduced the StyledText class which enabled text used for labels, buttons etc. to be styled e.g. italic, bold, underline etc. It means that G4P now uses the java.awt.Font class for all controls that display text.
To get a similar output on different platforms (Windows, OSX) then the font used must be installed on the computer system running the sketch (or viewing the applet) which limits the range of fonts available.
With GTextField and GTextArea controls the setFont(Font) method can be used to specify the font to use - this requires a java.awt.Font object passed as a parameter. The problem of cross-platform availablity still exists.
The code below demonstrates how you can load and use a custom True Type Font (TTF). The actual font file (has .ttf extension) must be in the data folder which is inside the sketch folder. Also beware that there may be copyright issues when distributing the font.
- import g4p_controls.*;
- import java.awt.*;
- Font font;
- GTextArea txaTester;
- public void setup() {
- size(400, 400);
- createGUI();
- // For plain text use Font.PLAIN
- font = getFont("consola.ttf", Font.ITALIC | Font.BOLD, 20);
- if (font != null)
- txaTester.setFont(font);
- }
- // This method creats and returns a java.awt.Font object from the TTF font
- // file in the sketch data folder.
- // Returns null if unable to create the font.
- public void createGUI(){
- G4P.messagesEnabled(false);
- G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
- G4P.setCursor(ARROW);
- if(frame != null)
- frame.setTitle("Sketch Window");
- txaTester = new GTextArea(this, 33, 17, 226, 208, G4P.SCROLLBARS_BOTH);
- txaTester.setOpaque(true);
- }
- public Font getFont(String ttf_name, int style, float size) {
- InputStream is = createInput(ttf_name);
- Font awtfont = null;
- try {
- awtfont = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(style, size);
- }
- catch(Exception e) {
- println("Failed to load font " + ttf_name);
- }
- return awtfont;
- }
- public void draw() {
- }