Scale equation for bitmap font - font-size

I have a bitmap font in which each character is 32px. I create individual characters with this font and assign them to display objects, of which each object is 32px and the objects scale value is 1.0 (full scale)
Now what I am trying to do is make my font creation routine standardised to accept font sizes in it's function call.
So i can do say :
loadFont( font, size )
So we know 32px is the base font size and that has a scale of 1.0. How would a formula work that can calculate the scale based on the size passed.
For instance, 16px would be 0.5 scale (exactly half the scale of the base font size) that one is easy.
But say I wanted 24px, 18px, 12px and so on?
Thanks in advance

This hardly requires a math guru, it's just a simple ratio. A 24px font would be 24/32 or 0.75 for example.

function loadFont (font, size)
{
scale = size / original_size;
...
}
Is this what you're looking for?

Related

Text isn't scaling properly in frame

I'm making a flowchart app and I've ran into a problem while scaling shapes containing text in them.
Depending on the scale value the relative width of string changes:
scale 6.0: scale 6.0
scale 1.0:
scale 1.0
scale 0.5:
scale 0.5
Text area class has property logFontSize, which represents constant font size (12.0 in this case).
Scaling function (zoomVal is the new scale):
func zoom(zoomVal: CGFloat){
let newSize = self.logFontSize*zoomVal
self.attributes![NSAttributedString.Key.font] = (self.attributes![NSAttributedString.Key.font] as! UIFont).withSize(newSize)
self.attributedString?.setAttributes(self.attributes, range: NSRange(location: 0, length: self.text!.count))
}
I've also checked: there is no correlation between scale value and relation between frame rectangle (red) height and string height (capHeight, lineHeight etc).
How do I scale text so that the relation between its width and its frame's width remains constant?
This is a feature that improves legibility. Text metrics like size and padding do not have simple, linear relationships. They are tweaked in numerous ways at various sizes, and in font-specific ways, to keep text easy to read.
If you want to linearly scale text, you should render it, and then scale the layer it is drawn in. For your use case, likely the best approach would be to compose a static CATextLayer with CAShapeLayers, and then apply a CAAffineTransform to the entire container CALayer to resize it. This will give you smooth zooming, and will be more performant.

How can I get a the default width of a specified font in Delphi?

I'm writing a DXF exporter/importer. The DXF MTEXT entity format supports width factor for a text block (how times it is wider than the default font width). The Windows LogFont record contains the lfWidth field (how many pixel will be the average font width if you select the logfont to create a hFont). How can I get the default width of the used font to calculate the scaling factor back and forth? Is there any WinAPI call?
OK. I have found It. The getTextMetrics fills up a TEXTMETRIC record. It has a tmAveCharWidth. The searched value.

SpriteKit SKLabelNode font size to pixels

What's a clean way to use SKLabelNode's fontSize to map to pixels? I'm trying to place text inside a box that fits in a fixed box proportionately.
According to apple's documentation, 1 font point is equal to 2 pixels.
For example, a 90 pixel font is equal to mytext.fontSize = 90 / 2

Rails Paperclip Plugin - Style Options for Resizing

So, I want to resize images to a FIXED width, but proportional height.
I have been trying a wide range of operators:
380x242#
380x242>
380!x242
380x242<
none of them have the desired effect. Any help? I want it to fill or resize to the 380 width, then resize / shrink the height by the same factor it used to shrink or resize the image to 380 wide.
Try using 380x
This should resize width to 380px and keep original aspect ratio.
For all available options for resizing images go here: http://www.imagemagick.org/script/command-line-processing.php?ImageMagick=lj6pre8q2iautc3ch6nuph1fc2#geometry
"#" is an argument used by Paperclip to know whether or not you expect the pic to be cropped. Using "100x100#" will scale and crop the picture exactly to that size. %#!<> are arguments in the Geometry String used by ImageMagick. One can use the following ImageMagick geometry strings for resizing images:
Ignore Aspect Ratio ('!')
Only Shrink Larger ('>')
Only Enlarge Smaller ('<')
Fill Given Area ('^')
Percentage Resize ('%')
Pixel Count Limit ('#')
According to the ImageMagick documentation for Image Geometry the geometry argument can be
scale% Height and width both scaled by specified percentage
scale-x%xscale-y% Height and width individually scaled by specified percent
width Height automagically selected to preserve aspect ratio
xheight Width automagically selected to preserve aspect ratio
widthxheight Maximum values of height and width given, ratio preserved
widthxheight^ Minimum values of width and height given, ratio preserved
widthxheight! Width and height emphatically given, ignore original ratio
widthxheight> Change only if an image dimension exceeds a specified dim.
widthxheight< Change only if both image dimensions exceed specified dim.
you can use , :show => '786>x447' for fixed width and prorortional height
The resizing options are limited but you can also use paperclip custom processors to resize images dynamically.
Railscasts has a good example of using a custom processor for paperclip, though his example allows a user to crop an image.
http://railscasts.com/episodes/182-cropping-images
You can calculate the height yourself:
newHeight = oldHeight * 380 / oldWidth

How to get font size in delphi

I am looking for a function which should take parameters as Font name, sample character, width, height of the sample character and should return Font Size.
It must look like this:
GetFontSize(<Font Name>, <Sample Character>, <Sample Character Width>,
<Sample Character Height>)
which must return the font size,
Is this possible in delphi?
You may want to take a look at this page that discusses font size and points.
It uses the following to convert between the points and pixel size:
Arial 12pt at 96 dpi:
font size in points 12
font size in pixels = ------------------- × 96 dpi = ---- × 96 = 16 pixels
72 points per inch 72
You could use that for your conversion. You just need to be aware of possible differences in your screen dpi as Blorgbeard stated. Points are usually used to describe the size on paper. You need to decide how you want to map that to the screen. For example many programs will allow you to set a zoom level. That does not change the printed size on paper but does affect the pixel height on the screen.
Depending on what you are trying to accomplish you may need to get the actual sizes of the font. If you are trying to find out exactly how the font is put together take a look at Obtaining Font Metrics The code on that page uses the .Net libraries but it has a good explanation. If you want to get the Text Metrics in Delphi you would use the GetTextMetrics function.
As Rob stated in his comment you can draw text at a specific height by setting the Font.Size property to the negative height in pixels. Windows will then draw the text out at that height. In that case you don't need a specific letter or the width as you have in your prototype function. For a given font size each letter will have a different height and width. I.E. capital letters will be taller than lower case letters and letters like "W" will be wider than letters like "i".
I'm only aware of methods to do the opposite - that is, to get pixel sizes from point sizes.
The only way I can think of is to set up a TCanvas, and repeatedly call GetTextExtent on it, changing the font size each time, until you get a size that's acceptably close to what you want.
Depending on how accurate you need this, you could just convert your desired height in pixels to points - a point is 1/72 of an inch, and your screen is probably 96 pixels per inch (but check GetDeviceCaps to get the real figure). See this question.
That would get you a font size pretty close to the pixel size you want.

Resources