Calculate a fitting text size - java

I want to calculate the size my Font needs, so my text will be displayed fully in one line without clipping.
Example
|-100px---HERE'S MY TEXT---100px-|
I have the DPI and all there stuff. Testing isn't a way, I am using Libgdx and the text ist display with an Button (Scene2D ui).

Call BitmapFont.getBounds(). The TextBounds it returns will tell you what you need. You can try a medium font, the go up or down based on how big or small the bounds are. I use this method to help scale UI sizes from old Droid 1s up to the new HD displays.
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html

Related

Is there a way to draw Strings in libgdx without BitmapFonts?

I want to draw Strings in my Libgdx game but i cant use BitMap Fonts because the scale of my game is to smal to use them.
It sounds like you mean the scale of your viewport is too small to show fonts correctly. There are two solutions. The first is better for legibility while the second is quick and dirty.
One is to use a second viewport for the UI that has an appropriate scale for text. You would first call gameViewport.apply(), draw the game, and end the batch. Then use uiViewport.apply() and then draw the UI. The downside with this method would be if you want to draw text that aligns with moving objects in the game, you would have to use the two viewports to convert coordinates. Otherwise, this is the ideal method to get a crisp looking UI. Ideally you would use a ScreenViewport and select a font size at runtime based on the screen dimensions, either by shipping your game with multiple versions of the font at different scales, or by using FreeTypeFontGenerator.
The second method is to scale down all your text. First call bitmapFont.setUseIntegerPositions(false) do it won't round off positions to integers. Then call bitmapFont.setScale() with however much you want to shrink it to fit in your game viewport.
There is a gdx-freetype project:
https://www.badlogicgames.com/wordpress/?p=2300
and it uses TrueType fonts as source to generate bitmap font on the fly.
Not sure how stable this is - didn't use it.

Is it possible to determine the largest font size that will fit inside of a JLabel based on the label's height?

I'm making a program that uses Swing components for the GUI and I want it to look good on as many different resolutions as possible without actually defining specific parameters for each one.
I already have it set up so that a JLabel takes up a constant proportion of the screen in terms of width and height, but while the size stays roughly the same, the size of the text can change dramatically because at the moment I have it so the font size is set to a constant value that has nothing to do with resolution (other than that it's just what looks good on my screen).
To elaborate, if you're dealing with similar resolutions like the 2 most common I've found my friends to have (1366x768 and 1920x1080), it's not that big of a deal, but now I've found one of my friends has a resolution of 3840x2160. The components are in the same location and are the same sizes with their resolution as someone with 1920x1080, but the text is 1/4 the size.
So how can I find the largest font size that will fit inside of any given JLabel?
(Just for maximum clarification, I'm not concerned with whether or not the string in the label is too long to be displayed completely, I'm solely focusing on how the text fits vertically)

How to add a bounded piece of text in Swing and test for overflow?

I want to display pieces of text of various sizes in a size-bounded (fullscreen) Swing application.
I tried to use a JTextField, but its size increases vertically when the text is too long, making the text going out of the bounds of the screen.
Considering this, I want:
to bound the size of the text field such that it does not overextends when setting the text in and
check whether the text is too long for this given size (e.g. for lowering the size of the font).
Can you help me to overcome these issues, please?

JPanel to be set to A4 size

I'm creating a text editor in Java that will have output to PDF. In order to sync data that appears in my program and the output PDF as close as I can, I'd have to have a JPanel which has the same size as an A4 paper (or at least to be in scale). I have tried converting its mm dimension (297x210) to pixels, but opening a regular size A4 document in PDF results in a bigger page than my JPanel.
Would certain size in pixels match the size of every document (.doc,.pdf,etc) created in A4 size (displaying it at 100%), or are there variations from program to program?
I'm trying to make sense of the whole conversion deal... Do I have to visually match the size in the Adobe reader, or is there some kind of factor that you multply with page size in inches or milimeters? Is there anyone who knows how does the whole page size format deal works?
In my experience, there is no such thing as a printer standard. The behavior of printers or software like Adobe Acrobat Reader vary dramatically. Some might automatically shrink your A4-sized panel to fit in a single page with margins while others might print off 4 pages with a single pixel column on page two and a single pixel row on page 3.
Unfortunately you must go about it by trial and error. Make it work for Acrobat Reader and then try printing it out and seeing if it comes out the same. It helps to provide any and all hints as to how to format the page. Lacking these hints, much of this software tends to guess about what your intentions are.
have look at 2D Graphics tutorial an to take Graphics to the BufferedImage, becasue direct output from Swing GUI to creates 1pixes == 1 DPI
there are
Working with Images
Printing
a few examples
From my experience all printable sizes use 72 pixels=1 inch based measurements. There could be difference because default win DPI=96.

Automatically scaling font to fit panel

I have an Swing app whose main panel is divided into a 3x2 grid of charts, and the app can be resized, with the charts (JFreeChart) auto scaling. One of these panels I would like to display the Apdex rating in, which is just text (e.g. '0.89 [0.5]*'). We use the application to display on a monitor visible to everyone, and scale multiple instances of the app that monitor different data centers. Scaling the Apdex font size to fit available panel space is what I'm after.
Any good ideas?
After re-reading and rethinking the question I would suggest for you to try calculating it yourself by use of FontMatrics stringWidth with the string and iteratively increasing the font size until you can, i.e. the size evaluated by you versus the available space.
A ready algorithm would be nice but I didn't hear of any.
Good luck, Boro.
I'd render it off-screen at some suitably large point size, as shown here, and then down-sample it using AffineTransformOp, as shown here.

Categories