Printed Line width based on number of characters

Although question is general situation, I am using BKShorts to print a report.

I have a paragraph stored in a TextArea that I need to print (using BKShorts). used the following code to process it into print lines (sInfo is the paragraph, sWords is a string array):

If sInfo <> "" Then
  
  iLine = iLine + (kLineHeight * 3) 
  sWords = Split(sInfo) 
  iCount = sWords.Ubound + 1
  iWord  = 0
  
  While iWord < iCount
    iLength = 0
    sLine   = ""
    While (iLength <= 500) And (iWord < iCount)
      sLine = sLine + sWords(iWord) + " "
      iLength = sLine.Length 
      iWord = iWord + 1
    Wend
    oDetail        = New BKS_Shorts.TextItem
    oDetail.Text   = sLine
    oDetail.Left   = iLeftMargin + 5
    oDetail.Top    = iLine 
    oDetail.Height = kLineHeight 
    oDetail.Width  = 500
    oDetail.Style  = New BKS_Shorts.StyleSet
    oDetail.Style.TextBold  = False
    oDetail.Style.TextSize  = kFontSize
    oDetail.Style.TextAlign = BKS_Shorts.TextAlignment.Left
    oPage.Append(oDetail)
    iLine = iLine + kLineHeight 
    If iLine > kMaxLine Then
      oShortsDocument.AppendPage(oPage)
      oPage = CreateNewPage
      iLine = 90
    End If
    sLine = ""
  Wend
End If 

How do I convert the number of characters in the print line to the physical line width in pixels? Font size is set to 12. In just trying different numbers, I found that if I multiply the length of sLine by 6 it works out but it is surly not ‘elegant’. is there a formula to calculate the line width?

You create a 1,1 picture, set the text style properties and then call the StringWidth method.

Something like this…

Dim p As Picture
Dim pixelLength As Integer

p = New Picture(1, 1)
p.Graphics.TextFont = "System"
p.Graphics.TextSize = 12

pixelLength = Ceil(p.Graphics.StringWidth("some text"))

Thank you. This seems to work perfectly…

Bill

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.