I'm implementing some code which requires that I calculate the width of a string of text.
I'm using the CreateNewFont and CalcTextSize methods, but the results for Bold strings
appear to be wrong - too small.
When I place some strings end-to-end based on the CalcTextSize returned width,
they overlap when Bold is used.
I can supply a functioning VB project as an example - but I'll need to email the zip file.
Here's my basic code for the calculations (VB6):
Code: Select all
Private Function getTextSize(ByVal sFName As String, ByVal dSize As Double, ByVal sText As String, ByVal getWidth As Boolean) As Single
Dim dFinal As Double
Dim xWid As Double
Dim xHit As Double
Dim myCreator As IPXC_ContentCreator
Dim xfnt As IPXC_Font
Set myCreator = pDoc.CreateContentCreator
myCreator.SetFontSize dSize
'this requires a "Postscript name" which is difficult to get so it's not used
'Set xfnt = pDoc.CreateNewFont2("ArialMT")
'Font name could be "Arial" or "Arial Bold" for example
Set xfnt = pDoc.CreateNewFont(sFName)
myCreator.SetFont xfnt
myCreator.CalcTextSize dSize, sText, xWid, xHit
If getWidth Then
dFinal = xWid
Else
dFinal = xHit
End If
'echo out
Debug.Print "String>>" & sText & "<<"
Debug.Print "Size>> " & dFinal
getTextSize = dFinal
End Function
Or are the values wrong?
Thanks.