Android Question Printer MeasureStringWidth

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I've been using the old PDFWriter to make PDFs and don't know when I could move my users to the PdfDocument as that would force them to API 19 and besides if it ain't broke don't fix it

Anyway. I've been using the following routine to give me an idea of a string width so when I am make my PDF I can center, align, justify text.

B4X:
Public  Sub GetTextWidth(Text As String, TextSize As Int, MonoSpace As Boolean) As Int
  Dim FmtLabel  As Label
  Dim JavaObj  As JavaObject

  FmtLabel.Initialize("")
  FmtLabel.TextSize = TextSize
  FmtLabel.Text  = Text
       
       If MonoSpace Then FmtLabel.Typeface = Typeface.CreateNew(Typeface.MONOSPACE, Typeface.STYLE_NORMAL)

  JavaObj = FmtLabel
  JavaObj.RunMethod("setPadding",  Array As Object(0, 0, 0, 0))
  JavaObj.RunMethod("setIncludeFontPadding", Array As Object(False))

  Dim Width As Int = mCanvas.MeasureStringWidth(FmtLabel.Text, FmtLabel.Typeface, FmtLabel.TextSize)
               
       Return Width / mLayoutValues.Scale
End Sub

ONLY the other day did I realize on devices with different Scale values were the PDFs not being generated correctly so I added the divide "Width / mLayoutValues.Scale" and that seems to make everything work as it should

MY Problem is I know this is NOT right. I am using a Screen function to calculate the width of text being shown on a point device (printers are 1/72)

Now this code about seems to work just fine (see attached PDF - looks great / same when generated by S3, S5 and Tab2)

Any help by anyone?
 

Attachments

  • PlayerVSPlayer.pdf
    134.2 KB · Views: 327

Erel

B4X founder
Staff member
Licensed User
Longtime User
MY Problem is I know this is NOT right.
I think that you are right. MeasureStringWidth returns the number of pixels. The actual value depends on the current device scale. The pdf dimensions are not related to the current device scale so you need to divide the value with the scale.

Canvas.MeasureStringWidth (...) / (GetDeviceLayoutValues.Scale * 160) / 72 = size in inch
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Something is still not right - tried the code below and the results show after it. My scale is 1

Taking the MeasureStringWidth and dividing it by LayoutsValues.Scale seems to work on 3 different devices I have (One has a scale of 1, two others have a scale of 2 & 3).

I just don't understand why - I tried looking by have not been able to locate what MeasureStringWidth returns.

I know the PDFWriter is returning the proper Paper Width 612 / 72 = 8.5 which is what I am using

B4X:
Dim Width As Int = mCanvas.MeasureStringWidth(FmtLabel.Text, FmtLabel.Typeface, FmtLabel.TextSize)
Log("Width1:"  &Width &"  What I would Return:" &(Width / mLayoutValues.Scale) &"  Alternate Return:" &(Width / (mLayoutValues.Scale * 160) / 72)    &"  PaperWidth:" &mPDFPaperSize.LETTER_WIDTH)

Width1:182 What I would Return:182 Alternate Return:0.01579861111111111 PaperWidth:612


I have also tried all of these:
B4X:
Log("Width1:"  &Width &"  What I would Return:" &(Width / mLayoutValues.Scale) &"  Alternate Return:" &(Width / (mLayoutValues.Scale * 160) / 72)    &"  PaperWidth:" &mPDFPaperSize.LETTER_WIDTH)
Log("Width2:"  &Width &"  What I would Return:" &(Width / mLayoutValues.Scale) &"  Returns Inches??:" &(Width / (mLayoutValues.Scale * 160))      &"  PaperWidth:" &mPDFPaperSize.LETTER_WIDTH)
Log("Width3:"  &Width &"  What I would Return:" &(Width / mLayoutValues.Scale) &"  Returns 72's:" &(Width / (mLayoutValues.Scale * 160dip) * 72)  &"  PaperWidth:" &mPDFPaperSize.LETTER_WIDTH)
Width1:453 What I would Return:453 Alternate Return:0.039322916666666666 PaperWidth:612
Width2:453 What I would Return:453 Returns Inches??:2.83125 PaperWidth:612
Width3:453 What I would Return:453 Returns 72's:203.85 PaperWidth:612

Much as I feel that the 72's number is what I want. If causes anything based on the field width to be off by more than half

I really appreciate you looking at this but don't want to eat up your time.

But I feel sure once everyone starts printing this could be a problem more are interested in.

If I get to convert to PdfDocument maybe it's canvas MeasureString (if there is one) will just return the proper width for a printer?

BobVal
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…