I am writing a game using libgdx, and I borrowed the skin.json (and related files) from a tutorial.
The font being used (default) was scaling in an ugly manner on denser screens, so I generated by own very large font - and in the game itself, I scale it to a reasonable size (basically I use BitmapFont.scale). The font I'm now using is 3 times as large as the previous one.
I changed the reference to which font to use in the skin.json file, and as a result, all my buttons, titles and other things have a massive font being shown.
Is there a way of scaling the font in the .json file? Or anywhere else in the code? Skin doesn't have a setFont() functionality, so I can't create a scaled BitmapFont and assign it)
Libgdx changed this back in April 2015 so that the set answer doesn't work anymore. Took a little bit to find the answer, so I thought I'd leave it here for others since this answer pops up first in Google.
this.getSkin().getFont("default-font").getData().setScale(0.33f,0.33f);
Documentation says that any gets will return a handle to the actual object. So changes will persist.
So I changed the font in skin.json to point to my new font.
Then I used this code
this.getSkin().getFont("default-font").setScale(0.33f, 0.33f);
To scale the 'default-font' (as defined in the skin.json) to the scale I wanted (in my case its 0.33f)
As of 2017 i had to use
skin.getFont("default-font").getData().setScale(0.5f);
for future users.
I am looking to apply an alphabet that does not exist in the UTF-Setup currently applied to all of my view elements and Swing components. To display these new characters, would I have to simply have each as its own image and then present the images adjacent to one another as in a character-like pattern, or is there any method by which to import letters from pictures to be added to something like a text area upon acting on a button?
Basically, if I have a pictograph system, may I import these images as characters, or would I have to maintain them as pictures?
To give some specificity, picture, Klingon writing or Dragon language, something that certainly is not defined in the standard packages of Character sets.
Thank you!
Best way I can think of to do this is to create some font file (.ttf, .otf, etc.) representing your special alphabet and then proceed to follow the instructions in this answer here.
Downside is, there really isn't any easy way to create font files. Usually it involves many hours manually tracing symbols using a vector graphic editor and compiling those to a font file.
If your characters are already vector images, then most of the work will have already been done.
I have seen http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/font/GlyphVector.html but I don't know how you would use it to display a glyph on the screen. Lets say you want to print glyph number 1042 (likely to be different in each font and unlikely (but possible) to be the same as Unicode 1042) to the screen. How do you go from the number to the character on screen? Is GlyphVector the way to go or is there a better method?
That GlyphVector class is not available on Android. There don’t seem to be any public API calls in Android graphics that allow access to font glyphs without going through the Unicode encoding.
Your obvious option would seem to be direct parsing of the TrueType font file. Perhaps find some library somewhere that has been ported to Android, or could be so ported (Freetype?).
I'm making a unicode Sinhala-English translator. When I print a Sinhala unicode letter with font size < 100, the letter is not correctly rendered. But when I use font size > 100, all letters are rendered smoothly. I can't find why this happening. On the other hand, all Sinhala unicode fonts do the same & it's not a bug of the font that I used. An Example is shown here.
Please help me.
I'm not aware that Java uses its own font rendering on Windows 7. So it should be a Windows bug but if that was the case, you'd see the problem in all Windows apps - but you see it only in Java.
You may want to try the Java 2D API. For example render the font with 140px and scale the Graphics2D by 0.5 to get 70px fonts.
[Old answer]
You're not saying which OS but my guess is that this is on Linux. If that's the case, then it might be because of "font hinting" in FreeType.
Basically, if a font becomes "too small" to render, the true type file can contain "render hints." In your example, you can see that the large font has smooth edges but the small example is more square-edged. You can see the pixels; anti-aliasing should prevent that.
So my guess is that you're a victim of the Font Hinting patent. The patent in question has expired last year. Try to find a newer version of "freetype" for your system.
This is a long shot, but does anyone know of an algorithm for estimating and categorising text width (for a variable width font) based on its contents?
For example, I'd like to know that iiiiiiii is not as wide as abcdefgh, which in turn is not as wide as WWWWWWWW, even though all three strings are eight characters in length.
This is actually an attempt to build some smarts into a string truncation method, which at the moment is correctly truncating a visually wide string, but is also unnecessarily truncating a visually narrow string, because both strings contain the same number of characters. It's probably sufficient for the algorithm to categorise the input string as narrow, normal or wide and then truncate as appropriate.
This question isn't really language-specific, but if there is an algorithm then I'll implement it in Java. This is for a web application. I'm aware that there are answers on SO that deal with this problem using JavaScript to obtain the width of a containing div element, but I wondered if a server-side solution is possible.
Most GUI frameworks provide some way to calculate text metrics for fonts on given output devices.
Using java.awt.FontMetrics, for example, I believe you can do this:
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
public int measureText(Graphics g, String text) {
g.setFont(new Font("TimesRoman", Font.PLAIN, 12));
FontMetrics metrics = g.getFontMetrics();
return metrics.stringWidth(text);
}
Not tested, but you get the idea.
Under .Net you can use the Graphics.MeasureString method. In C#:
private void MeasureStringMin(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
This worked for me:
AffineTransform af = new AffineTransform();
FontRenderContext fr = new FontRenderContext(af,true,true);
Font f = new Font("Arial", 0, 10); // use exact font
double width= f.getStringBounds("my string", fr).getWidth();
For a web application, you cannot (really) get a proper estimation. Different fonts have different widths, so that this not only depends on the client (browser) and its zoom and DPI settings, but also on the fonts present on that machine (and operating system) or their substitutions.
If you need exact measuring, create a graphic (bitmap, SVG, or even some PDF or whatever) which will be layouted and rendered on the server and not on the client.
There is no reliable server side solution for calculating width of text. (outside of creating an image of the text and probably SVG)
If you try a tool out like browser-shots and run it against relatively basic pages, you'll immediately see why. It's hard to predict how wide even the most mundane examples will turn out, much less if a user decides to zoom in the browser etc...
It's not stated precisely you might want to truncate the string (it might be helpful in giving potential solutions), but a common one is because you want to cut off the text at some point and provide an ellipse.
This can be done reliably on many browser platforms by using a css property, and NO javascript:
http://www.jide.fr/emulate-text-overflowellipsis-in-firefox-with-css
You really have no way of knowing what browser, font settings, screen size etc the client is using. (OK, sometimes the request headers provide an indication, but really nothing consistent or reliable.)
So, I would:
display some sample text in Internet Explorer with default settings on a screen/window size of 1024x768 (this is generally the most common size)
take an average characters/line with this configuration and use that for your estimate.
I've generally found this "good enough", for example, for estimating the number of lines that some text will take up on a browser, in order to estimate how many adverts to show next to the text.
If it was really really crucial to you, then I can imagine a convoluted scheme whereby you initially send some Javascript to the client, which then takes a measurement and sends it back to your server, and you then use this information for future pages for that client. I can't imagine it's usually worth the effort, though.
I think you should choose one of these solutions:
Exact solution: Sum up the width for every character in the string (most APIs will give you this information)
Quick estimate: Either take the maximum or the minimum width and multiply it with the numbers of characters.
Of course some mixed estimate is possible, but I think this is too much effort in the wrong direction.
For a nice* client-side solution, you could try a hybrid CSS-and-Javascript approach as suggested by RichieHindle's answer to my question.
Given that you don't know what font the user will see the page in (they can always override your selection, Ctrl-+ the page, etc), the "right" place to do this is on the browser... although browsers don't make it easy!
* when I say "nice", I mean "probably nice but I haven't actually tried it yet".
This is actually an attempt to build some smarts into a string truncation method [...]
Is it really worth the effort? We had this exact problem. And this was across languages. The fix was to leave it as-is. The complexity of keeping this intelligence up increases rapidly (and probably exponentially) with every language that you add support for. Hence our decision.
[...] an algorithm for estimating and categorising text width (for a variable width font) based on its contents?
Most font-libraries will give you this information. But this is pretty low-level stuff. The basic idea is to pass in a string and get back the width in points.