Changing font while drawing paragraph in Java - java

I am rendering a paragraph and, for word wrapping, I am using the LineBreakMeasurer and the TextLayout class.
This is the snippet that I am using , which is easily available online :
void drawParagraph(Graphics2D g, String paragraph, float width) {
LineBreakMeasurer linebreaker = new LineBreakMeasurer(new AttributedString(paragraph)
.getIterator(), g.getFontRenderContext());
int y = 0;
while (linebreaker.getPosition() < paragraph.length()) {
TextLayout textLayout = linebreaker.nextLayout(width);
y += textLayout.getAscent();
textLayout.draw(g, 0, y);
y += textLayout.getDescent() + textLayout.getLeading();
}
}
However, I am facing a problem, when I am trying to change the font.
Although i am changing the font by invoking g.setFont(new Font(...)), the paragraph is not being rendered in that font. However, when I try to use g.drawString(), it is working as expected.
Please help me with this problem.Thank you in advance.

Set the fonts in your AttributedString. For instance:
AttributedString text = new AttributedString(paragraph);
Font emphasis = new Font(Font.SERIF, Font.BOLD, 12);
int emphasisStart = 30;
int emphasisEnd = 42;
text.addAttribute(TextAttribute.FONT, emphasis, emphasisStart, emphasisEnd);
LineBreakMeasurer linebreaker =
new LineBreakMeasurer(text.getIterator(), g.getFontRenderContext());

Related

Generate new Font every time or use pre-generated collection of Fonts?

I have a method which decides what font size to use for drawing a string. I'm using java.awt.Font.
Font size depends on string length and height, and I use do-while loop to decrease font size until the string fits targetHeight. So in general it looks something like this:
private void decideOnFontSize(String text) {
int fontSize = 72;
do {
font = new Font("Arial", Font.PLAIN, fontSize);
// Calculating things...
fontSize -= 2;
} while (textHeight >= targetHeight);
}
With this approach I need to instantiate a new Font object every time when I need a smaller font size. Method decideOnFontSize is a part of a service in a public API, so it could be called pretty often. At first sight, instantiating new Font looks pretty wasteful in this case.
Another approach is to create a pre-defined collection of Fonts and get them as required. So I could create a utility class like this:
public class FontsUtil {
private static Map<Integer, Font> fonts = new HashMap<>();
public static final Integer MAX_FONT_SIZE = 72;
public static final Integer MIN_FONT_SIZE = 10;
static {
String fontName = "Arial";
for(int fontSize = MAX_FONT_SIZE; fontSize >= MIN_FONT_SIZE; fontSize -= 2) {
fonts.put(fontSize, new Font(fontName, Font.PLAIN, fontSize));
}
}
public static Font getFontBySize(Integer fontSize) {
return fonts.get(fontSize);
}
}
...and then I could get these pre-defined Fonts in do-while loop, so my method will look like this:
private void decideOnFontSize(String text) {
int fontSize = FontsUtil.MAX_FONT_SIZE;
do {
font = FontsUtil.getFontBySize(fontSize);
// Calculating things...
fontSize -= 2;
} while (textHeight >= targetHeight);
}
To my mind, the second approach looks better, because (in theory) it consumes less resources. Am I right, or it doesn't really matter?
UPD: More specifically, I create one or more TextLayout objects using LineBreakMeasurer, because I have two initial params to fit: targetWidth and targetHeight. So my do-while looks like this:
List<TextLayout> lines;
int fontSize = font.getSize();
int textHeight;
do {
font = FontsUtil.getFontBySize(fontSize);
g2d.setFont(font);
lines = splitStringIntoPiecesToFitTargetWidth(text, targetWidth, g2d);
textHeight = getTextHeight(lines);
fontSize -= 2;
} while (textHeight >= targetHeight);
*where font is an instance variable
And method splitStringIntoPiecesToFitTargetWidth looks like this:
private List<TextLayout> splitStringIntoPiecesToFitTargetWidth(String string,
int width, Graphics2D g2d) {
List<TextLayout> lines = new ArrayList<>();
AttributedString attributedString = new AttributedString(string);
attributedString.addAttribute(TextAttribute.FONT, font);
LineBreakMeasurer lineBreakMeasurer =
new LineBreakMeasurer(attributedString.getIterator(), g2d.getFontRenderContext());
while (lineBreakMeasurer.getPosition() < string.length()) {
lines.add(lineBreakMeasurer.nextLayout(width));
}
return lines;
}
The latest version of the Font.java source I could find is this one from OpenJDK 8. It defines the following constructor:
public Font(String name, int style, int size) {
this.name = (name != null) ? name : "Default";
this.style = (style & ~0x03) == 0 ? style : 0;
this.size = size;
this.pointSize = size;
}
To be honest, this doesn't feel like it'll be the bane of your existence performance-wise. I would suggest leaving the version that instantiates new fonts, as it reads easier. If performance does show to be a real issue/concern, you can always go with the other option.
P.S. You should benchmark it, just in case. If the difference is noticeable (I'd put the line at anything >50% faster, but it's up to you), maybe the second option is worth it.

Text doesnot rotate Graphics2D

I checked on site and followed all examples.
I have written the code, the position, font of text changes but the angle or rotation doesnt take place. Any help advice pointers are much appreciated
int x = X;
int y = Y;
int w = 9;
int h = 9;
int fontsize = 7;
Rectangle bounds = new java.awt.Rectangle(x, y, w, h);
ShapeGroup group = new ShapeGroup();
group.setAnchor(bounds);
slide.addShape(group);
Graphics2D graphics = new PPGraphics2D(group);
AffineTransform affineTransform = new AffineTransform();
affineTransform.rotate(50);
java.awt.Shape circle = new Ellipse2D.Float(x, y, 9, 9);
graphics.draw(circle);
graphics.setPaint(colorRating);
graphics.fill(circle);
//graphics.rotate(-Math.PI/2);
graphics.setColor(new Color(159, 78, 15));
Font font = new Font("Arial", Font.BOLD, fontsize);
Font rotatedFont = font.deriveFont(affineTransform);
graphics.setFont(rotatedFont);
graphics.setTransform(affineTransform);
graphics.drawString(siteName, x + 11, y + fontsize + 1);
graphics.setFont(font);
I reported a similar bug to vldocking project: when used on MacOS, tab titles didn’t rotate 90° when the tabs were on the left/right. After investigating further, I submitted a patch, which amounted to one line:
graphics.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Without antialiasing, the text rendering took an optimized path, which skipped the ability to rotate the glyphs.
I don’t know if you are on a MacOS, with the same java implementation. Try setting the antialiasing attribute, and if that doesn’t work, some other rendering hints as well.
Good luck!

Making text width flexible with ttf font

I am inquiring about two things. First I am having an ttf file and it is located in my macair drive. I do not want to add that file into my project structure. How can i import the True_type font from it. I have tried various ways to import it my Java program. e.g. public class TextFixer {
private static String[] names = { "iksswgrg.ttf" }; //this exists on my macair drive and i want to create font from it.
private static Map<String, Font> cache = new ConcurrentHashMap<String, Font>(names.length);
static {
for (String name : names) {
cache.put(name, getFont(name));
}
}
public static Font getFont(String name) {
Font font = null;
if (cache != null) {
if ((font = cache.get(name)) != null) {
return font;
}
}
String fName = "/fonts/" + name;
try {
InputStream is = TextFixer.class.getResourceAsStream(fName);
font = new Font("ttf", 0, 16);
//font = Font.createFont(Font.TRUETYPE_FONT, is);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println(fName + " not loaded. Using serif font.");
font = new Font("serif", Font.PLAIN, 24);
}
return font;
}
2nd part is I want to create a String by using Graphics. First I need to have width that is of 130mm. The height of the displayed box will be the tallest character in the provided string. The font size is between 8 and 16. I have an enterprise project which take care of the height and size of the ttf. The problem i face is: I do not want to use swing/javafx libraries. I want to use Graphics library of Java, use Graphics2D to have a rectangle. How can i set its width to be precisely 130mm? Then I want to make that width flexible with according to the Font. I want to draw a string and the string should get adjusted/being flexible in the provided width. I am able to draw a string through g.drawString() but I am unable to see it on console. As I do not want to use Jframe or any Swing/javaFX libraries.
I know this seems a bit long but I hope I have explained it well enough. I desperately need help. Please let me know if you guys can help me out here.
Thanks in advance
First I am having an ttf file and it is located in my macair drive. I do not want to add that file into my project structure. How can i import the True_type font from it
This is more of a problem to do with "How do you reference a file on the file system" then "How do I load a font", because if you can solve the first, you can solve the second.
File fontFile = new File("some/relative/path/to/your/Font.tff");
or
File fontFile = new File("/some/absolute/path/to/your/Font.tff");
Personally, I like neither, as it it causes too much trouble (working directories, other systems, etc), I prefer to use embedded resources where possible or put the files in a common location.
For example {user.home}/AppData/Local/{application name} on Windows or on Mac you could use {user.home}/Library/Application Support/{application name}, then it doesn't matter where the program is executed from
Loading the font is relatively simple. For my example, I placed the font file in the working directory of the program
System.out.println(new File("Pacifico.ttf").exists());
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("Pacifico.ttf"));
2nd part is I want to create a String by using Graphics. First I need to have width that is of 130mm. The height of the displayed box will be the tallest character in the provided string
This is much more complicated, as images are measured in pixels. In order to know how many pixels make up a given distance, we need to know the DPI of the image.
its 72DPI
Okay then, from that, we can calculate the number of pixels we need
public static double cmToPixel(double cm, double dpi) {
return (dpi / 2.54) * cm;
}
130mm (13 cm) comes out to be 368.503937007874 # 72dpi.
From this, we need to find the font point size for a given piece of text to fit within this range.
Now, there are a number of was you can do this, you could simply start at point 1 and perform a linear progression till you pass the range you're after. It's not exactly fast and, as you increase the size, it can become a little error prone.
I've opted for more of a divide and conquer approach
protected static int widthOfText(String text, Font font, float fontSize, Graphics2D g2d) {
font = font.deriveFont(fontSize);
FontMetrics fm = g2d.getFontMetrics(font);
int textWidth = fm.stringWidth(text);
return textWidth;
}
public static Float pointToFit(double width, String text, Font font, Graphics2D g2d, float min, float max) {
float fontSize = min + ((max - min) / 2f);
font = font.deriveFont(fontSize);
FontMetrics fm = g2d.getFontMetrics(font);
int textWidth = fm.stringWidth(text);
if (fontSize == min || fontSize == max) {
return fontSize;
}
if (textWidth < width) {
return pointToFit(width, text, font, g2d, fontSize, max);
} else if (textWidth > width) {
return pointToFit(width, text, font, g2d, min, fontSize);
}
return fontSize;
}
Important to note, it's not perfect, but it betters a linear progression :P
With this in hand, we can start calculating the required properties we need...
String text = "Happy, Happy, Joy, Joy";
double width = cmToPixel(13.0, 72.0);
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
float fontSize = pointToFit(width, text, font, g2d, 0, (float)width);
font = font.deriveFont(fontSize);
FontMetrics fm = g2d.getFontMetrics(font);
int height = fm.getHeight();
g2d.dispose();
Okay, so this creates a small (1x1) temporary image. We need Graphics context in order to calculate all the other properties. It then calculates the font point size, from it can then calculate the text height
With all that information in hand, we can get around to actually rendering the text...
img = new BufferedImage((int) Math.round(width), height, BufferedImage.TYPE_INT_ARGB);
g2d = img.createGraphics();
g2d.setFont(font);
fm = g2d.getFontMetrics();
g2d.setColor(Color.BLACK);
g2d.drawString(text, 0, fm.getAscent());
g2d.dispose();
Which will eventually output something like this...
I added the red border before I rendered the text so I could see how well it fitted.
Now, this is a really basic example, what this doesn't do is tell you when the text won't fit (ie, the point size is 1 or 0), you'll have to put traps in to catch that yourself
And, because I know you'll probably have lots of fun putting it together, my test code...
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) {
try {
System.out.println(new File("Pacifico.ttf").exists());
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("Pacifico.ttf"));
String text = "Happy, Happy, Joy, Joy";
double width = cmToPixel(13.0, 72.0);
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
float fontSize = pointToFit(width, text, font, g2d, 0, (float) width);
System.out.println(width);
System.out.println(fontSize);
font = font.deriveFont(fontSize);
FontMetrics fm = g2d.getFontMetrics(font);
int height = fm.getHeight();
g2d.dispose();
img = new BufferedImage((int) Math.round(width), height, BufferedImage.TYPE_INT_ARGB);
g2d = img.createGraphics();
g2d.setColor(Color.RED);
g2d.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
g2d.setFont(font);
fm = g2d.getFontMetrics();
g2d.setColor(Color.BLACK);
g2d.drawString(text, 0, fm.getAscent());
g2d.dispose();
JOptionPane.showConfirmDialog(null, new ImageIcon(img));
} catch (IOException | FontFormatException e) {
//Handle exception
}
}
public static Float pointToFit(double width, String text, Font font, Graphics2D g2d) {
return pointToFit(width, text, font, g2d, 0f, Float.MAX_VALUE);
}
protected static int widthOfText(String text, Font font, float fontSize, Graphics2D g2d) {
font = font.deriveFont(fontSize);
FontMetrics fm = g2d.getFontMetrics(font);
int textWidth = fm.stringWidth(text);
return textWidth;
}
public static Float pointToFit(double width, String text, Font font, Graphics2D g2d, float min, float max) {
float fontSize = min + ((max - min) / 2f);
NumberFormat nf = NumberFormat.getInstance();
font = font.deriveFont(fontSize);
FontMetrics fm = g2d.getFontMetrics(font);
int textWidth = fm.stringWidth(text);
if (fontSize == min || fontSize == max) {
return fontSize;
}
if (textWidth < width) {
return pointToFit(width, text, font, g2d, fontSize, max);
} else if (textWidth > width) {
return pointToFit(width, text, font, g2d, min, fontSize);
}
return fontSize;
}
public static double cmToPixel(double cm, double dpi) {
return (dpi / 2.54) * cm;
}
}

Wrapping/Rotating text on a image using graphics2D

void multiLine (int x, int y, String label, Graphics2D g) {
AffineTransform fontAT = new AffineTransform();
Font theFont = g.getFont();
fontAT.rotate(-Math.PI / 2);
Font theDerivedFont = theFont.deriveFont(fontAT);
g.setFont(theDerivedFont);
AttributedString attrStr = new AttributedString(label);
// Get iterator for string:
AttributedCharacterIterator characterIterator = attrStr.getIterator();
// Get font context from graphics:
FontRenderContext fontRenderContext = g.getFontRenderContext();
// Create measurer:
LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator,
fontRenderContext);
while (measurer.getPosition() < characterIterator.getEndIndex()) {
TextLayout textLayout = measurer.nextLayout(200);
y += textLayout.getAscent(); //Have tried changing y to x
textLayout.draw(g, x, y);
y += textLayout.getDescent() + textLayout.getLeading(); //Have tried changing y to x
}
g.setFont(theFont);
}
I am expecting this to print lines vertically but it does not , any ideas about how can I resolve this.
This outputs texts horizontally wrapped.
Edit: Changed the question to correctly reflect what I am trying to achieve
Reduce your wrapping width. 200 is too large, so it is not wrapping. Set it to 0, if you want each character on a new line.
TextLayout textLayout = measurer.nextLayout(0);

Testing whether a Font is monospaced in Java

I'm trying list all of the monospaced fonts available on a user's machine. I can get all of the font families in Swing via:
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
Is there a way to figure out which of these are monospaced?
Thanks in advance.
A simpler method that doesn't require making a BufferedImage to get a Graphics object etc.:
Font fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
List<Font> monoFonts1 = new ArrayList<>();
FontRenderContext frc = new FontRenderContext(null, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT);
for (Font font : fonts) {
Rectangle2D iBounds = font.getStringBounds("i", frc);
Rectangle2D mBounds = font.getStringBounds("m", frc);
if (iBounds.getWidth() == mBounds.getWidth()) {
monoFonts1.add(font);
}
}
You could use the getWidths() method of the FontMetrics class. According to the JavaDoc:
Gets the advance widths of the first 256 characters in the Font. The advance is the distance from the leftmost point to the rightmost point on the character's baseline. Note that the advance of a String is not necessarily the sum of the advances of its characters.
You could use the charWidth(char) method of the FontMetrics class. For example:
Set<String> monospaceFontFamilyNames = new HashSet<String>();
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontFamilyNames = graphicsEnvironment.getAvailableFontFamilyNames();
BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = bufferedImage.createGraphics();
for (String fontFamilyName : fontFamilyNames) {
boolean isMonospaced = true;
int fontStyle = Font.PLAIN;
int fontSize = 12;
Font font = new Font(fontFamilyName, fontStyle, fontSize);
FontMetrics fontMetrics = graphics.getFontMetrics(font);
int firstCharacterWidth = 0;
boolean hasFirstCharacterWidth = false;
for (int codePoint = 0; codePoint < 128; codePoint++) {
if (Character.isValidCodePoint(codePoint) && (Character.isLetter(codePoint) || Character.isDigit(codePoint))) {
char character = (char) codePoint;
int characterWidth = fontMetrics.charWidth(character);
if (hasFirstCharacterWidth) {
if (characterWidth != firstCharacterWidth) {
isMonospaced = false;
break;
}
} else {
firstCharacterWidth = characterWidth;
hasFirstCharacterWidth = true;
}
}
}
if (isMonospaced) {
monospaceFontFamilyNames.add(fontFamilyName);
}
}
graphics.dispose();
Compare the drawn lengths of several characters (m, i, 1, . should be a good set).
For monospaced fonts they will all be equal, for variable width fonts they won't.
According to this response, Java doesn't know too much about underlying font details, so you'd have to do some comparisons of the font's dimensions.
Probably not applicable for your case, but if you simply want to set the font to a monospaced font, use the logical font name:
Font mono = new Font("Monospaced", Font.PLAIN, 12);
This will be a guaranteed monospaced font on your system.

Categories