How to get correct text width for oblique font in DirectWrite? - directwrite

I create a IDWriteTextLayout object by the following code,
hr = g_pDWriteFactory->CreateTextLayout(text, textLength, *g_pTextFormat, 200000000, 200000000, g_pTextLayout);
then get the text width by text metric,
DWRITE_TEXT_METRICS dtm;
pTextLayout->GetMetrics(&dtm);
float minHeight = dtm.height;
float minWidth = dtm.widthIncludingTrailingWhitespace;
what confuse me is that whether the style of the font is DWRITE_FONT_STYLE_OBLIQUE or DWRITE_FONT_STYLE_NORMAL, the width of the same string is the same value. Why? I expect that when the font style is DWRITE_FONT_STYLE_OBLIQUE, the width should be bigger. how can i get the correct width for oblique text?
Thanks.

Layout metrics are based on font design metrics, either hinted or not, and not on actual ink box for each glyph. If you want metrics that closer match resulting rendered bitmap size, you can adjust returned layout metrics with data GetOverhangMetrics() returns. But at the end the only way to know exact pixel size of resulting layout is to render it.
So answering your question, returned layout metrics for oblique font are already correct, they just mean slightly different thing than you expect them to.

Related

Scale down UITextView with Transform

I am trying to scale down a UITextView with a custom font in a storyboard with constraints. I can easily animated the bounds and also the font-size. However, since it is a custom font the font-changing becomes quite choppy at larger sizes. I figured this could be done with by changing the transform of the UITextView. However, this does not seem to be working. The text view is self sizing in height, but uses a fixed with constant for width (which animated along with the font size/transform). This go gain control over where the text view line breaks.
I've tried all suggested solutions below without success. Either the view keeps it bounds but lowers in resolution. Or the bounds decrease, but faster than the appeared font size resulting in clipping.
Scaling UITextView using contentScaleFactor property
I've also tried to place the text view in a placeholderview where I also animated the height/width constraint multiplier to make sure it decreases in size.
Solved it by placing my UITextView in a placeholder UIView and setting align center X/Y + Equal width/height constraints to it.
I also animated to multiplier of the width/height contraints between these views to the inverse of the current transform scale. This to compensate for the scaling of the transform.
textViewWidthConstraint = textViewWidthConstraint.withMultiplier(1/scale)
textViewHeightConstraint = textViewHeightConstraint.withMultiplier(1/scale)
placeholderView.transform = CGAffineTransform(scaleX: scale, y: scale)
where function withMultiplier is a rename of the solution presented in https://stackoverflow.com/a/33003217/511299 for editing constraint multipliers.

Swift and autolayout: proportional font size in a label

I have a storyboard with (among other things) a label. Its height is proportional to the screen height. But the text size in the label is always the same. I'd like to also have a proportional font size.
Does anyone know how to do this?
Thank you for your help.
Edit: Maybe I should specify that my label has both width and height proportional to the screen size (the bigger the screen is, the bigger the label is).
I did it programmatically but I'd like to know if it's possible to do it from the storyboard.
click the label and choose the size properties inspector on the right hand side and edit the font size. You can then choose a size of your choosing

How to change the size of the font of a Label to take the maximum size

I have a Label in a Scene. I need to get always the largest font size for the text in it, so that the text always takes the maximum size in the available size of the label.
How can I do that?
final double MAX_FONT_SIZE = 30.0; // define max font size you need
label.setFont(new Font(MAX_FONT_SIZE)); // set to Label
I am not sure if we have a way to set font size according to the size of the container. But you could scale the text vertically or horizontally to fit the container bounds. Check out the following ...
Scaling a textbox without truncating text within it

How do I display same "proportional" text size on different resolutions?

I draw some text on screen using ID3DXFont::DrawText. This text should be displayed the same regardless of screen resolution.
For example, if screen resolution is low, text wrapped and when it is higher text is not wrapped. How can I avoid such situation? I want text size to be connected to screen resolution so if resolution is lower I want the text to be relatively smaller so that no wrapping happens. Is there any way?
Thanks in advance
Below is a logical solution. It doesn't have any of the code or procedures needed to make work in direct X but having done something similar outside of DirectX I wanted to share the logic
Working on whatever default screen resolution you want set the font to be the size needed.
Find the percentage of the screen height the font size you chose takes up.
Then having stored that value when you go to render again, in the final version of the program, calculate the font size based on the screen size and the percentage
What I was working in had functions like GetTextHeight and properties on the font size to allow you to set the height (which in turn set the font size appropriately). So if can find anything similar to this in DirectX than this could be a route for you to take.

Centering fonts in VB6

How do you determine the length of a string of text in Arial Bold font, and then center it in VB6?
We are not using a "label" or "picture box" to print the text to the screen. We are sizing the text on the fly, and allowing the user to scale the size of our application to their liking. We write the text to screen using code.
One way is to have a hidden picture box and setup the font specs of that picture box the way you want.
Then use the TextHeight and TextWidth methods of the PictureBox to take your measurements. The Units will be in whatever scalemode the Picture Box is set to.
If you are printing directly to the printer or form then just set your font FIRST then take your measurements.
To center it
MyText = "Hello World"
<displayarea>.FontName = "Arial"
<displayarea>.FontSize = 14
<displayarea>.FontBold = True
TextWidth = <displayarea>.TextWidth(MyText)
TextLeftCoordinate = <displayarea>.ScaleLeft+<displayarea>.ScaleWidth/2-TextWidth/2
<displayarea>.CurrentX = TextLeftCoordinate
<displayarea>.Print MyText
Substitute displayarea with whatever object you are using.
Based on your updated answer note that the hidden picture box suggestion isn't used to print. It is only get text measurement. However you are printing directly to the form so you just need to use the code example above.
I can't remember the specifics (it's been about 3 years since I last used VB 6), but there's a method on Form called something like "MeasureString". It takes the string, and measures it according to the font settings of the form.
Also, here's a comment posted by Jason Lepack in case I've misunderstood and over-complicated your requirements:
"Labels usually have an alignment property. If you set it to align to center then, regardless of the font face it should center in the label".
There are Win32 GDI functions you can invoke: see for example GetTextExtentPoint32 at http://msdn.microsoft.com/en-us/library/ms534223(VS.85).aspx
Your best option may be Form.TextWidth, which appears to return the width of a string in twips. I've just taken this approach in order to dynamically size a button based on the length of the label that needs to appear inside it.
There is also a corresponding function called Form.TextHeight which would allow you to do the same thing in the vertical dimension.
Make sure that you set the Font property of the form to match the Font property of the control you're intending to measure the text for, otherwise you'll get incorrect results.
Read more at http://msdn.microsoft.com/en-us/library/aa267168(VS.60).aspx

Resources