A challenge for you!

Post your Gambas programming questions here.
Post Reply
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

A challenge for you!

Post by cogier »

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.

Label1 As Label
Label2 As Label
Label3 As Label

Public Sub Form_Open()

  BuildForm

  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")
  'Create("DOUBLE FACEPLATE With stuff")
  'Create("Box")

End

Public Sub Create(sDesc As String)

  Dim iSplit As Integer
  Dim sWork, s1, s2, s3 As String

  If Len(sDesc) < 21 Then
    s1 = sDesc
    sDesc = Replace(sDesc, s1, "")
  Else
    sWork = Mid(sDesc, 1, 21)
    iSplit = RInStr(sWork, Chr(32)) - 1
    s1 = Mid(sWork, 1, iSplit)
    sDesc = Replace(sDesc, s1, "")
  End If

  If Len(sDesc) < 21 Then
    s2 = sDesc
    sDesc = Replace(sDesc, s2, "")
  Else
    sWork = Mid(sDesc, 1, 21)
    iSplit = RInStr(sWork, Chr(32)) - 1
    s2 = Mid(sWork, 1, iSplit)
    sDesc = Replace(sDesc, s2, "")
  Endif

  If Len(sDesc) < 21 Then
    s3 = sDesc
  Else
    sWork = Mid(sDesc, 1, 21)
    iSplit = RInStr(sWork, Chr(32)) - 1
    s3 = Mid(sWork, 1, iSplit)
  Endif

  Label1.Text = Trim(s1)
  Label2.Text = Trim(s2)
  Label3.Text = Trim(s3)

End

Public Sub BuildForm()

  With Me
    .Height = 88
    .Width = 192
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With Label1 = New Label(Me) As "Label1"
    .H = 28
    .W = 100
    .Alignment = Align.Center
  End With

  With Label2 = New Label(Me) As "Label2"
    .H = 28
    .W = 100
    .Alignment = Align.Center
  End With

  With Label3 = New Label(Me) As "Label3"
    .H = 28
    .W = 100
    .Alignment = Align.Center
  End With

End
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: A challenge for you!

Post by BruceSteers »

can Paint.TrimText() help?
http://gambaswiki.org/wiki/comp/gb.qt4/paint/trimtext

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.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: A challenge for you!

Post by BruceSteers »

my brain just did this...


Public Sub Create(sDesc As String)
  
  Dim aLines As String[] = ["", "", ""], iLine As Integer
  For Each sWord As String In Split(sDesc, " ", Null, True)
    If Len(aLines[iLine] & " " & sWord) > 20 Then 
      Inc iLine
      If iLine = 3 Then Break
      aLines[iLine] = sWord
      If Len(sWord) > 20 Then aLines[iLine] = Mid(sWord, 1, 20)
    Else
      aLines[iLine] &= If(aLines[iLine] = "", "", " ") & sWord
    Endif
  Next
  
  Label1.Text = aLines[0]
  Label2.Text = aLines[1]
  Label3.Text = aLines[2]

End


If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: A challenge for you!

Post by BruceSteers »

this is a bit better.

Public Sub Create(sDesc As String)

  Dim aLines As String[] = ["", "", ""], iLine As Integer

  For Each sWord As String In Split(sDesc, " ", Null, True)

    If [":", ",", "."].Exist(sword) Then Continue  ' Omit these single chars

    If Len(aLines[iLine] & If(aLines[iLine], " ", "") & sWord) > 20 Then

      If Not aLines[iLine] Then  ' is first word on the line and it does not fit so trim it on same line.
        aLines[iLine] = Mid(sWord, 1, 20)

      Else  ' not the first word so move to new line and add word if 3 lines or less
        Inc iLine
        If iLine = 3 Then Break
        aLines[iLine] = Mid(sWord, 1, 20)
      Endif

    Else
      ' it fits on the line so just add it..
      aLines[iLine] &= If(aLines[iLine] = "", "", " ") & sWord
    Endif
  Next

  Label1.Text = aLines[0]
  Label2.Text = aLines[1]
  Label3.Text = aLines[2]

End


I get this...

Raspberry Pi 3 Model
B+ Consists of Black
Pi3 Case 2B/3B Black
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: A challenge for you!

Post by cogier »

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!
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: A challenge for you!

Post by BruceSteers »

cogier wrote: Friday 4th August 2023 2:30pm 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 ;)




Public Sub Form_Open()

  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"

  PictureBox1.Picture = MakeInfoImage(sText, PictureBox1.W, PictureBox1.H)
  PictureBox1.Tooltip = sText

End


Public Sub MakeInfoImage(sDesc As String, W As Integer, H As Integer) As Picture
 
 Dim p As Picture = New Picture(w, h, True)

 Paint.Begin(p)
   Paint.DrawRichText(Paint.TrimRichText(sDesc, W, H), 0, 0, W, H, Align.Left)
 Paint.End

 Return p
 
End


shows like this...
UntitledN.png
UntitledN.png (3.31 KiB) Viewed 1789 times
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..
Untitled.png
Untitled.png (3.02 KiB) Viewed 1789 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: A challenge for you!

Post by cogier »

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.
For iLoop = 1 To SliderBoxCopies.Value
    Shell "lp /tmp/jtprint.pdf" Wait
Next


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!

Image
(Your mods not yet included)
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: A challenge for you!

Post by BruceSteers »

Here I made the function take a Font size parameter and used it on 3 different picture box's...
Screenshot at 2023-08-04 17-45-26.jpg
Screenshot at 2023-08-04 17-45-26.jpg (134.25 KiB) Viewed 1786 times
8-)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: A challenge for you!

Post by BruceSteers »

cogier wrote: Friday 4th August 2023 4:37pm
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.
For iLoop = 1 To SliderBoxCopies.Value
    Shell "lp /tmp/jtprint.pdf" Wait
Next


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!

Image
(Your mods not yet included)
Aah cool :)

well it was a fun challenge anyway :)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply