-
Aug 4th, 2020, 07:52 AM
#1
Thread Starter
Hyperactive Member
Adjust font size
Hello, this question was probably asked many times, but here it goes again, I want to wrap a text to a PictureBox (not a PictureBox to a text), i.e. I would like to know how to indicate the font size to be moderately similar at the height and width of a PictureBox, all this without having to resort to a loop with TextWidth or TextHeight, I'm not looking for an exact precision but one that fits visually pleasing
how the fontsize scale is interpreted, with respect to a pixel scale
-
Aug 4th, 2020, 08:04 AM
#2
Re: Adjust font size
I always use API and pixels.
the API GetTextExtentPoint32 will help u get the needed sizes. and we can use the API DrawText or the API TextOut or API ExtTextOut to write the text, after we calculated the sizes.
another way is using the "Printer" functions, Printer.TextWidth & Printer.TextHeight but Im thinking that you are already doing that?
-
Aug 4th, 2020, 08:39 AM
#3
Thread Starter
Hyperactive Member
Re: Adjust font size
I think my question was very silly, it was as simple as this what I was looking for, thanks anyway and sorry for the trouble
Code:
Option Explicit
Private Sub Form_Load()
Me.ScaleMode = vbPixels
Me.FontName = "Segoe UI"
End Sub
Private Sub Form_Resize()
Cls
Me.Font.Size = Me.ScaleHeight / 2
Me.Print "A"
End Sub
-
Aug 4th, 2020, 10:10 AM
#4
Re: Adjust font size
StdFont.Size is always in Points. Never Pixels.
You also "Me, Me, Me" a lot but then you omit it from Cls. Weird.
-
Aug 4th, 2020, 11:26 AM
#5
Re: Adjust font size
Here's a possible solution, using built-in StdFont scaling
Code:
Dim sngRatio As Single, oFont As IFont
Set oFont = Picture1.Font
' next 2 lines are used should the font have already been scaled at least once, to return to base-line scale
oFont.SetRatio 96, 2540
Set Picture1.Font = oFont
sngRatio = Picture1.ScaleHeight / Picture1.TextHeight("Xyz")
oFont.SetRatio sngRatio * 96, 2540
Set Picture1.Font = oFont
-
Aug 5th, 2020, 11:35 PM
#6
Thread Starter
Hyperactive Member
Re: Adjust font size
Interesting, i didn't know the SetRatio property of ifont
question, that represented 2540?
-
Aug 6th, 2020, 12:20 AM
#7
Re: Adjust font size
Originally Posted by LeandroA
Interesting, i didn't know the SetRatio property of ifont
question, that represented 2540?
See this msdn article.
https://docs.microsoft.com/en-us/win...ifont-setratio
Just FYI. For DPI aware applications, I use the Font.SetRatio quite a bit, passing its parameters as: DPI, 2540
-
Aug 6th, 2020, 05:04 AM
#8
Re: Adjust font size
Code:
Const HIMETRICS_PER_INCH = 2540
-
Aug 6th, 2020, 05:23 AM
#9
Re: Adjust font size
Code:
Dim sngRatio As Single, oFont As IFont
Set oFont = Picture1.Font
' next 2 lines are used should the font have already been scaled at least once, to return to base-line scale
oFont.SetRatio 96, 2540
Set Picture1.Font = oFont
sngRatio = Picture1.ScaleHeight / Picture1.TextHeight("Xyz")
oFont.SetRatio sngRatio * 96, 2540
Set Picture1.Font = oFont
The lines in red look like errors, functionally no-ops. The two object references already point to the same instance. Set does not make a clone.
Or have I missed something obvious?
-
Aug 6th, 2020, 06:00 AM
#10
Re: Adjust font size
Originally Posted by dilettante
Code:
Dim sngRatio As Single, oFont As IFont
Set oFont = Picture1.Font
' next 2 lines are used should the font have already been scaled at least once, to return to base-line scale
oFont.SetRatio 96, 2540
Set Picture1.Font = oFont
sngRatio = Picture1.ScaleHeight / Picture1.TextHeight("Xyz")
oFont.SetRatio sngRatio * 96, 2540
Set Picture1.Font = oFont
The lines in red look like errors, functionally no-ops. The two object references already point to the same instance. Set does not make a clone.
Or have I missed something obvious?
I tested when I saw the code and strangely ObjPtr on the PictureBox Font returns a different pointer than ObjPtr on the oFont variable.
Still, if you change some property on the oFont variable the font os the PictureBox is also updated.
My guess was that the PictureBox must be doing a copy of the font object but still holding a variable intenally with events to get the changes of the original font object and to apply to them the copy (just a guess).
-
Aug 6th, 2020, 06:59 AM
#11
Re: Adjust font size
Originally Posted by Eduardo-
I tested when I saw the code and strangely ObjPtr on the PictureBox Font returns a different pointer than ObjPtr on the oFont variable.
This is normal. Check out this snippet
Code:
Dim oFont As StdFont
Dim pFont As IFont
Dim pUnk As IUnknown
Dim pDisp1 As Object
Dim pDisp2 As Object
Set oFont = New StdFont
Set pFont = oFont
Set pUnk = oFont
Debug.Print ObjPtr(oFont)
Debug.Print ObjPtr(pFont)
Debug.Print ObjPtr(pUnk)
Set pDisp1 = oFont
Set pDisp2 = pUnk
Debug.Print ObjPtr(pDisp1)
Debug.Print ObjPtr(pDisp2)
I get this
Code:
126825932
126825928
126825928
126825932
126825932
PictureBox.Font is declared StdFont, not IFont. The IFont interface is IUnknown based, not IDispatch. StdFont is a coclass, declared with IFontDisp and FontEvents both dispinterfaces. (Dispinterfaces are IDispatch based.)
cheers,
</wqw>
-
Aug 6th, 2020, 07:10 AM
#12
Re: Adjust font size
Ah, OK, I understand. Now I remember that when different interfaces point to the same object, ObjPtr returns different pointers.
Edit: better to use Is to know whether it is the same object.
Code:
Debug.Print ObjPtr(Picture1.Font), ObjPtr(oFont), Picture1.Font Is oFont
Last edited by Eduardo-; Aug 6th, 2020 at 07:16 AM.
-
Aug 6th, 2020, 09:26 AM
#13
Re: Adjust font size
Interesting ... so the "Is" operator is smarter than just comparing object addresses.
To expand on wqweto's example:
Code:
Dim oFont As StdFont
Dim pFont As IFont
Set oFont = New StdFont
Set pFont = oFont
Debug.Print ObjPtr(oFont) ' Reports the pointer.
Debug.Print ObjPtr(pFont) ' Reports ObjPtr(oFont) - 4
Debug.Print oFont Is pFont ' Reports "True"
EDIT: Or, said differently, apparently the "Is" operator is smart enough to figure out if it's the same object, regardless of how many interfaces that object has.
Last edited by Elroy; Aug 6th, 2020 at 09:31 AM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Aug 6th, 2020, 02:08 PM
#14
Re: Adjust font size
Originally Posted by Elroy
EDIT: Or, said differently, apparently the "Is" operator is smart enough to figure out if it's the same object, regardless of how many interfaces that object has.
IS method performs a QueryInterface call. Basically, VB is asking does ThatObject implement/inherit ThisObject or vice versa. In order to be true, at some point ThatObject must have been created/retrieved from ThisObject or vice versa.
Per COM rules: "It Must Be Possible to Query Successfully for Any Interface on an Object from Any Other Interface."
Details can be found here, if interested: https://docs.microsoft.com/en-us/win...ace.%20More%20
-
Aug 6th, 2020, 02:11 PM
#15
Re: Adjust font size
Originally Posted by Elroy
EDIT: Or, said differently, apparently the "Is" operator is smart enough to figure out if it's the same object, regardless of how many interfaces that object has.
Is operator first casts to IUnknown and then compares the ObjPtr of these references.
Check out Rules for Implementing QueryInterface article. The first one is so called Identity rule: "For any given object instance, a call to QueryInterface with IID_IUnknown must always return the same physical pointer value" and this is basicly what the Is operator uses.
Mind that this rule does *not* hold for IDispatch casts for instance, it's only for IUnknown casts. I.e. an instance of a coclass with two interfaces (IAnimal and IFrog) can when pointers to both these interfaces are cast to (QueryInterface'd for) IDispatch return different "physical pointer value" (a.k.a. addresses, a.k.a ObjPtr).
Edit: LaVolpe beat me to the link :-))
cheers,
</wqw>
-
Aug 6th, 2020, 03:24 PM
#16
Re: Adjust font size
Originally Posted by wqweto
Is operator first casts to IUnknown and then compares the ObjPtr of these references.
That actually makes perfect sense. And, the link states...
For any given object instance, a call to QueryInterface with IID_IUnknown must always return the same physical pointer value.
...which basically just affirms what you said.
All stuff that's good to know that I hadn't really thought about before. I typically use the "Is" operator, but I could have seen myself comparing ObjPtr() values and not thinking anything of it.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Nov 29th, 2020, 01:09 PM
#17
Addicted Member
Re: Adjust font size
I'm having a similar problem as OP and as he seems to have found his solution I thought to chip in here rather than starting anew topic. My difference is that I'm trying to find the Picturebox.font.size that would make a known amount of characters and objects fill a line in the box horizontally in a visually pleasing way, as the form and picturebox are resized.
I have worked out a reasonably acceptable routine to accomplish this by using picturebox.TextWidth(txt) and increasing/decreasing the fontsize in steps of 0.25 - however - I have found that the property doesn't respond to these changes as applied and e.g. only changes from 11.25 to 12, simply ignoring 11.50 and 11.75 and this makes the "jumps" rather big and not always with a visually pleasant result.
Is this something that could be overcome by use of API? which I'm not a stranger to but never used it in relation to fonts before. Btw, I'm actually drawing a whole table on the picturebox but it's only the width and fontsize that poses a problem.
M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?
-
Nov 29th, 2020, 08:35 PM
#18
Fanatic Member
Re: Adjust font size
7edm:
The jumps you are seeing are normal based on the Font selected.
API will have no effect.
Only solution for increment jumps -- based on Font -- is make your own for those in betweens if needed.
-
Nov 30th, 2020, 03:04 AM
#19
Addicted Member
Re: Adjust font size
thanks for the reply vb6forever, so you are saying that the uneven jumps/gaps in fontsize has to do with the font selected, in this case Arial (TF), and not the picturebox control? So when you select a font (and size) in e.g. Word, which gives you integer options like 9,10,11,12... the size actually in use by the document are not the same I thought TrueFonts where scalable?
M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?
-
Nov 30th, 2020, 10:19 AM
#20
Fanatic Member
Re: Adjust font size
fontsize has to do with the font selected
Yes
I thought TrueFonts where scalable?
They are.
Been awhile since I messed with fonts, but if you enum a particular font (other than true type) you will see size jumps depending on the font. This is because no glyph (font) exists (was created) for that font at that size.
For True Type, if I recall correctly -- hopefully someone will confirm or refute -- while scalable, the user still must create the font at that size.
So M$ gives you a set of stock sizes, which is what you get when you enum a Type Type Font. However is one wants other than a stock size you must create it. It is Not a automatic thing when you enum that it will show up. So when you set TF Arial 8.5, it will be available for use. If you do an enum at that point (after setting the size) not sure whether it in fact will show up.
Will try and confirm today to make sure this is correct.
Here's one you can try out:
http://vbnet.mvps.org/index.html?cod...fontsample.htm
Last edited by vb6forever; Nov 30th, 2020 at 10:58 AM.
-
Nov 30th, 2020, 04:10 PM
#21
Addicted Member
Re: Adjust font size
Originally Posted by vb6forever
Been awhile since I messed with fonts, but if you enum a particular font (other than true type) you will see size jumps depending on the font. This is because no glyph (font) exists (was created) for that font at that size.
This has been clear to me all the way, I just thought that with true type font's it was different, that they scaled in size seamlessly, so you could set they to 9.19 if you so liked. In fact, if you edit text in a PDF document with Acrobat you often get this kind of suggestions for font sizes.
Originally Posted by vb6forever
For True Type, if I recall correctly -- hopefully someone will confirm or refute -- while scalable, the user still must create the font at that size.
So create as in create a completely new font (file), giving it a new name etc. or create in terms of expanding the current font file?
Originally Posted by vb6forever
So M$ gives you a set of stock sizes, which is what you get when you enum a Type Type Font. However is one wants other than a stock size you must create it. It is Not a automatic thing when you enum that it will show up. So when you set TF Arial 8.5, it will be available for use. If you do an enum at that point (after setting the size) not sure whether it in fact will show up.
This is completely new, and a bit shocking, to me, even that I'm not exactly new to new to neither computers nor programing. So during all these years when e.g. in M$ Word I have trimmed a font size to 9.5 to make it fit into a certain space, that size has actually not been there... or has just scaled virtually or something...
I have never really bothered about font sizes to this granularity before, meaning checked that if I set it to 11 pt that it actually came out as that, not until I recently dug deeper into dynamically changing content in a resizable form.
Originally Posted by vb6forever
Thanks a lot, will dig into it and see what I can find out. As of now it seems that I only can get out of Arial TF the sizes 8.5, 9.75, 10.50, 11.15 & 12 when debug.print my own code, so will be interesting to what the code at that url will reveal.
M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?
-
Nov 30th, 2020, 04:40 PM
#22
Fanatic Member
Re: Adjust font size
So during all these years when e.g. in M$ Word I have trimmed a font size to 9.5 to make it fit into a certain space, that size has actually not been there... or has just scaled virtually or something...
Yes, by scaling, you are in effect are creating a new True Type Font for use in the place you want to use it.
BUT -- and this is where I said I needed to confirm -- if one Enums after that TT Font has been created (scaled to a new size), I'm not sure whether or Not it will show in the Enum TTF list sizes for that Font Name.
Play with code at the link I gave you and Enum (create a list for both before and after a new TT font is created (scaled)) and see if the new TT Font size shows in the Enum after list.
Last edited by vb6forever; Nov 30th, 2020 at 04:46 PM.
-
Nov 30th, 2020, 05:52 PM
#23
Re: Adjust font size
Originally Posted by vb6forever
Yes, by scaling, you are in effect are creating a new True Type Font for use in the place you want to use it.
The effect, 7edm described - is not really caused by the "stuff contained in a TTF- or OpenType-FontFile".
These Files contain their Glyph-descriptions as "Vector-Paths" (which are scalable to any size in Double- or Single-Coords).
The "jumping in certain, discrete intervals" (in TextHeight, as well as in TextWidth) -
is instead caused by the Font-Rendering-APIs, due to their "restriction", to:
- render a certain Glyph to "whole Pixels" (to exact Pixel-Boundaries)
- so that the ClearType-stuff can kick in afterwards (SubPixel-based Antialiasing via an LCDs "RGB-Slots")
Certain FontRendering-libs (as e.g. DirectWrite, GDI+, FreeType or Cairo) allow:
- a Font-Rendering (Glyph-Antialiasing) without respecting PixelBoundaries (from any TTF- or OpenType-Font)
- but then the antialiasing has to make the compromise, of saying "good bye" to ClearType (the SubPixel-Antialiasing)
The latter point (free-scaling of Text-Renderings, but surrendering ClearType-Antialiasing)
has caused quite a disturbance in the developer-communities (when it was made the default at first, in IE9 - or early .NET-Winforms).
The TextOutput is then just looking "more blurry", because generic "greyscale-" antialiasing has to be used for SubPixel-precise outputs.
@7edm:
So, you will have to decide yourself whether:
1) you want SubPixel-precise TextWidth- (or TextHeight-)matchings (arbitrary Scaling) - e.g. when "fitting some stuff into a surrounding box"
.. (but then the TextGlyph-Outputs will look less sharp)
2) you want sharp (ClearType-enhanced) TextGlyph-Outputs
.. (but then have to live with slight, discrete jumps when you scale the FontSize, because PixelBoundaries have to be met by each Vector-Glyph).
Here is an extreme example, of "exact fitting of a certain Text-String onto the length of a given BaseLine-Path"
(no ClearType on the Glyphs, only "normal AntiAliasing").
So for the above example, it doesn't matter, whether the BaseLine-Path is a straight Line (e.g. defined by only two ControlPoints),
or constructed as a BSpline-Curve (defined via 7 ControlPoints, as seen in the ScreenShot).
If there's interest, I can post an example-project (which then will need the Cairo-COM-wrapper-lib).
HTH
Olaf
-
Nov 30th, 2020, 07:06 PM
#24
Fanatic Member
Re: Adjust font size
Olaf
The "jumping in certain, discrete intervals" (in TextHeight, as well as in TextWidth) -
is instead caused by the Font-Rendering-APIs, due to their "restriction", to:
- render a certain Glyph to "whole Pixels" (to exact Pixel-Boundaries)
- so that the ClearType-stuff can kick in afterwards (SubPixel-based Antialiasing via an LCDs "RGB-Slots")
-- Thanks for your detailed explanation and clearing things up.
As always learned something.
7edm:
Re:
Yes, by scaling, you are in effect are creating a new True Type Font for use in the place you want to use it.
Since TTF, vector created was implied.
Sorry if I mislead you any.
Last edited by vb6forever; Nov 30th, 2020 at 07:18 PM.
-
Dec 1st, 2020, 06:20 AM
#25
Addicted Member
Re: Adjust font size
Thanks guys!
I'm pounding my options here to decide of the best way forwards, and Olaf I do use RC5 and have done since version 3, which was named slightly different I recall. Anyhow, I have never got around to really explore the GUI stuff and Cairo for various reasons.
I suppose the best way to give a better picture of what I'm doing here is to simply post a screen shot, so here it is:
I'm still cutting and dicing in the code trying various things, so will hold back on posting any for now until I can do it with a more specific and relevant question. If I cannot make it to fill the width by adjusting fontsize I could always center it on the space trowing a frame border around it. Btw, I am aware that a further scaling up proportionally would make the height come short, but I have a sort of pagination routine in place to deal with that, splitting it on more than one picturebox.
Apparently, a lot to explore here so will see what knowledge I can gain on my own before starting to ask further questions.
M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?
-
Dec 1st, 2020, 11:23 AM
#26
Fanatic Member
Re: Adjust font size
7edm:
Sheet looks like astrology.
If I cannot make it to fill the width by adjusting fontsize I could always center it on the space trowing a frame border around it
Appears to me you are just placing a string (value symbol value) in each cell. Looking at the sheet,
vertical alignment looks fine. Since the string is composed of several characters, don't get where your going with
your font adjustment needs and "fill the width"?
-
Dec 1st, 2020, 12:48 PM
#27
Addicted Member
Re: Adjust font size
Astrology, yes, it is a dynamic Table of (astrological) Houses. What I want with "fill the width" has to do with resizing of the form where I first of all want to make sure all the 12 house columns are shown, but also if the form is made larger that the content also enlarge within this criterion. Then I obviously want it to look nicer that it does currently, probably by drawing a frame around the table.
I never really expected to get a perfect fit with the width, it was just that while testing out a routine for best possible scaling I noticed this with the font sized. Like if I assigned .font.size = 11, font.size actually showed 11.25 after the assignment and a value of 11.50 bumped it to 12 and so on. It just triggered my curiosity of what was going on and if there was a problem to be solved, and well IF it was solvable. Now I think I will probably move on and "take what I get" and make that look good by center align the table within the available space. In short, I need it to work out in various resolutions as well, not just by the resize factor, and well there is also the factor of DPI to take into account and I haven't even got there yet ;-) So I will rest my case here as what's next doesn't really relate to the O.P. issue and will open a new topic if needed.
M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|