A challenge for you!

Post

Posted
Rating:
#1 (In Topic #1075)
Avatar
Guru
cogier is in the usergroup ‘Guru’
I wanted to create 3 lines of text for a label (2" x 1" or 50mm x 25mm).
The text should contain only complete words on each line and no longer than 20 characters.
The string supplied could be anything from 3 to hundreds of characters. Excessive characters can be ignored.
The code I came up with is in the 'Create' routine below, it can be run in a Graphical Application.
I wondered if someone can come up with simpler code to do the same thing.

Code (gambas)

  1. Label1 As Label
  2. Label2 As Label
  3. Label3 As Label
  4.  
  5. Public Sub Form_Open()
  6.  
  7.   BuildForm
  8.  
  9.   Create("Raspberry Pi 3 Model B+ Consists of : Black Pi3 Case 2B/3B Black Modular Rpi Plus Case Sd Card Cover Black Modular Rpi Plus Case Usb Hdmi Cover Raspberry Pi 3 Model B + Raspberry Pi Universal Psu 5 1V-2 Transcend 16Gb Microsd High Capacity Class 10")
  10.   'Create("DOUBLE FACEPLATE With stuff")
  11.   'Create("Box")
  12.  
  13.  
  14.  
  15.   Dim iSplit As Integer
  16.   Dim sWork, s1, s2, s3 As String
  17.  
  18.   If Len(sDesc) < 21 Then
  19.     s1 = sDesc
  20.     sDesc = Replace(sDesc, s1, "")
  21.   Else
  22.     sWork = Mid(sDesc, 1, 21)
  23.     iSplit = RInStr(sWork, Chr(32)) - 1
  24.     s1 = Mid(sWork, 1, iSplit)
  25.     sDesc = Replace(sDesc, s1, "")
  26.   End If
  27.  
  28.   If Len(sDesc) < 21 Then
  29.     s2 = sDesc
  30.     sDesc = Replace(sDesc, s2, "")
  31.   Else
  32.     sWork = Mid(sDesc, 1, 21)
  33.     iSplit = RInStr(sWork, Chr(32)) - 1
  34.     s2 = Mid(sWork, 1, iSplit)
  35.     sDesc = Replace(sDesc, s2, "")
  36.  
  37.   If Len(sDesc) < 21 Then
  38.     s3 = sDesc
  39.   Else
  40.     sWork = Mid(sDesc, 1, 21)
  41.     iSplit = RInStr(sWork, Chr(32)) - 1
  42.     s3 = Mid(sWork, 1, iSplit)
  43.  
  44.   Label1.Text = Trim(s1)
  45.   Label2.Text = Trim(s2)
  46.   Label3.Text = Trim(s3)
  47.  
  48.  
  49. Public Sub BuildForm()
  50.  
  51.   With Me
  52.     .Height = 88
  53.     .Width = 192
  54.     .Arrangement = Arrange.Vertical
  55.     .Padding = 5
  56.  
  57.   With Label1 = New Label(Me) As "Label1"
  58.     .H = 28
  59.     .W = 100
  60.     .Alignment = Align.Center
  61.  
  62.   With Label2 = New Label(Me) As "Label2"
  63.     .H = 28
  64.     .W = 100
  65.     .Alignment = Align.Center
  66.  
  67.   With Label3 = New Label(Me) As "Label3"
  68.     .H = 28
  69.     .W = 100
  70.     .Alignment = Align.Center
  71.  
  72.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
can Paint.TrimText() help?
/comp/gb.qt4/paint/trimtext - Gambas Documentation

That's how i would do it, as simply as possible ;)
make 1 label sized to 3 chars high then use Paint.TrimText or Paint.TrimRichtext to get an auto trimmed string.

possibly not good enough for your specific needs though.
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
my brain just did this…

Code (gambas)

  1.  
  2.  
  3.   Dim aLines As String[] = ["", "", ""], iLine As Integer
  4.   For Each sWord As String In Split(sDesc, " ", Null, True)
  5.     If Len(aLines[iLine] & " " & sWord) > 20 Then
  6.       Inc iLine
  7.       If iLine = 3 Then Break
  8.       aLines[iLine] = sWord
  9.       If Len(sWord) > 20 Then aLines[iLine] = Mid(sWord, 1, 20)
  10.     Else
  11.       aLines[iLine] &= If(aLines[iLine] = "", "", " ") & sWord
  12.     Endif
  13.   Next
  14.  
  15.   Label1.Text = aLines[0]
  16.   Label2.Text = aLines[1]
  17.   Label3.Text = aLines[2]
  18.  
  19.  
  20.  
  21.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
this is a bit better.

Code (gambas)

  1.  
  2.   Dim aLines As String[] = ["", "", ""], iLine As Integer
  3.  
  4.   For Each sWord As String In Split(sDesc, " ", Null, True)
  5.  
  6.     If [":", ",", "."].Exist(sword) Then Continue  ' Omit these single chars
  7.  
  8.     If Len(aLines[iLine] & If(aLines[iLine], " ", "") & sWord) > 20 Then
  9.  
  10.       If Not aLines[iLine] Then  ' is first word on the line and it does not fit so trim it on same line.
  11.         aLines[iLine] = Mid(sWord, 1, 20)
  12.  
  13.       Else  ' not the first word so move to new line and add word if 3 lines or less
  14.         Inc iLine
  15.         If iLine = 3 Then Break
  16.         aLines[iLine] = Mid(sWord, 1, 20)
  17.       Endif
  18.  
  19.     Else
  20.       ' it fits on the line so just add it..
  21.       aLines[iLine] &= If(aLines[iLine] = "", "", " ") & sWord
  22.     Endif
  23.   Next
  24.  
  25.   Label1.Text = aLines[0]
  26.   Label2.Text = aLines[1]
  27.   Label3.Text = aLines[2]
  28.  
  29.  

I get this…

Raspberry Pi 3 Model
B+ Consists of Black
Pi3 Case 2B/3B Black
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
 Thank you, Bruce. I am looking at your code. It certainly contains one or two nifty bits of code I need to get my head around.

I was hoping this type of question might persuade a few more to contribute, so if you have another solution, let us see it!
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’

cogier said

Thank you, Bruce. I am looking at your code. It certainly contains one or two nifty bits of code I need to get my head around.

I was hoping this type of question might persuade a few more to contribute, so if you have another solution, let us see it!

hehe, yeah alternative methods are always cool to see :)

how about the Picture method i first spoke of…
Instead of a label i use a PictureBox and make an image for it simply using Pant.DrawRichtext() and Paint.TrimRichtext()
But i think it's not going to trim as you want?
So i set the tooltip to be the complete text ;)

Code (gambas)

  1.  
  2.  
  3.  
  4. Public Sub Form_Open()
  5.  
  6.   Dim sText As String = "Raspberry Pi 3 Model B+ Consists of : Black Pi3 Case 2B/3B Black Modular Rpi Plus Case Sd Card Cover Black Modular Rpi Plus Case Usb Hdmi Cover Raspberry Pi 3 Model B + Raspberry Pi Universal Psu 5 1V-2 Transcend 16Gb Microsd High Capacity Class 10"
  7.  
  8.   PictureBox1.Picture = MakeInfoImage(sText, PictureBox1.W, PictureBox1.H)
  9.   PictureBox1.Tooltip = sText
  10.  
  11.  
  12.  
  13. Public Sub MakeInfoImage(sDesc As String, W As Integer, H As Integer) As Picture
  14.  
  15.  Dim p As Picture = New Picture(w, h, True)
  16.  
  17.  Paint.Begin(p)
  18.    Paint.DrawRichText(Paint.TrimRichText(sDesc, W, H), 0, 0, W, H, Align.Left)
  19.  Paint.End
  20.  
  21.  Return p
  22.  
  23.  

shows like this…
Image

(Click to enlarge)


Or I can use the following after Paint.Begin() to make smaller font and fit more text …

Paint.Font.Size = 8

then I get this..
Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
how about the Picture method i first spoke of…

Thanks, but I have managed to get Gambas to work with a Zebra Thermal Printer using gb.cairo to create a pdf document of 2" x 1" which I put in the /tmp folder. I can then print it by shelling the lp command.

Code (gambas)

  1. For iLoop = 1 To SliderBoxCopies.Value
  2.     Shell "lp /tmp/jtprint.pdf" Wait

It works well so I do not want to have to rewrite that part of the program to cater for an alternative method. If it ain't broke, don't fix it!

<IMG src="https://www.cogier.com/gambas/jtprint.png"> </IMG>
(Your mods not yet included)
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
Here I made the function take a Font size parameter and used it on 3 different picture box's…

Image

(Click to enlarge)


 8-)
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’

cogier said

how about the Picture method i first spoke of…

Thanks, but I have managed to get Gambas to work with a Zebra Thermal Printer using gb.cairo to create a pdf document of 2" x 1" which I put in the /tmp folder. I can then print it by shelling the lp command.

Code (gambas)

  1. For iLoop = 1 To SliderBoxCopies.Value
  2.     Shell "lp /tmp/jtprint.pdf" Wait

It works well so I do not want to have to rewrite that part of the program to cater for an alternative method. If it ain't broke, don't fix it!

<IMG src="https://www.cogier.com/gambas/jtprint.png"> </IMG>
(Your mods not yet included)

Aah cool :)

well it was a fun challenge anyway :)
Online now: No Back to the top
1 guest and 0 members have just viewed this.