Text formating/justification

jflaplante

Member
Licensed User
Longtime User
Probably another simple question...

I'm in the final stretch of my POS system and I'm printing invoices using this excellent share by timwil:

http://www.b4x.com/forum/basic4andr...torials/17692-bluetooth-printing-via-spp.html

I'm having great success printing to my bluetooth printer but I would more control about the text formatting.

For example, lets say that I have 48 characters per line available. On one line I would like to print a quantity, a price and a total. timwil's way uses the 'textwriter.writeline' mthod to write directly to the bluetooth serial port. I'm probably too tired to figure it out but I would like to be able to simply format my output text like so:

Currently, products quantity and prices don't have the same length:

B4X:
12 @ 143.50$ = 1722.00$
6 @ 15.00$ = 90.00$

But I would prefer something like that:

B4X:
12 @ 143.50$ = 1722.00$
 6 @  15.00$ =   90.00$

I'm able to use number format to add 0s with the minimum integer function but after that I would need to remove those 0s.

There must be a library somewhere that does that?

Something similar to those python functions:

B4X:
string.ljust(s, width[, fillchar])
string.rjust(s, width[, fillchar])
string.center(s, width[, fillchar])
These functions respectively left-justify, right-justify and center a string in a
field of given width. They return a string that is at least width characters wide,
created by padding the string s with the character fillchar (default is a space) 
until the given width on the right, left or both sides. The string is never truncated.

Thanks in advance.

JF.
 
Last edited:

jflaplante

Member
Licensed User
Longtime User
I'm not familiar with this library. Are you able to use monospace font with it?

I want to use it to print on a portable bluetooth printer which uses, by default, monospaced font. So let's say I have 48 characters per line, I can fill those characters by concatenation with small chunks (quantity, price, total, etc) that are themselves justified in their own given space.

I could also use the final string that is let say 40 characters and justify it inside the 48 spaces available. The final result would be a 48 characters string composed of various parts. It's kind of like a table without a table.

The examples I've given are part of a Python language Library, the same functions exist in core ColdFusion or Railo (ljustify,cjustify and rjustify) and probably in PHP too. I only asked because i didn't want to re-invent the wheel if it already existed but if even you are not aware of such a function, I will code it and offer it to the StringFunction lib author.
 
Upvote 0

jflaplante

Member
Licensed User
Longtime User
I came up with this... It is unnecessary long for clarity and can certainly be optimized but it works!

B4X:
Sub justify(text As String, len As Int, kind As String, fill As String) As String

'text = your text that you want justified
'len = over how many characters is text to be justified
'kind = l,r or c for left, right or center
'fill = wick character to fill the rest of the len (often " ")
'text and fill will be truncated if too long

   Dim result As String
   Dim freechar As Int
   Dim before As Int
   Dim after As Int
   
   'truncate the text if too long
   If text.Length > len Then
      text = text.SubString2(0,len-1)
   End If
   
   'truncate the fill if too long, take only the first character
   If fill.Length > 1 Then
      fill = text.SubString2(0,1)
   End If
   
   ' Characters to be filled
   freechar = len - text.Length
   
   Select kind
   
      ' center
      Case "c"
         before = Round(freechar/2)
         after = freechar - before
         
      ' left   
      Case "l"
         before = 0
         after = freechar
      
      ' right   
      Case "r"
         before = freechar
         after = 0
      
      Case Else
         Log("Invalid kind: " & kind & " (must be l,r or c)")
         ' this will return the original string
         before = 0
         after = 0
   End Select
      
   ' before
   For i = 1 To before
      result = result & fill
   Next
   
   ' the text in question
   result = result & text
   
   ' after
   For i = 1 To after
      result = result & fill
   Next
      
      
   Return result
      
End Sub

I've benefited alot from the knowledge of others and Erel in particular in the last few months, if it turn out that this function indeed didn't exist yet, I would fell great to have contributed something (as small as it is) at last!
 
Last edited:
Upvote 0
Top