Printing in PB, Dante's 7 circles of Hell!!!

Just starting out? Need help? Post your questions and find answers here.
User avatar
CDXbow
User
User
Posts: 97
Joined: Mon Aug 12, 2019 5:32 am
Location: Oz

Printing in PB, Dante's 7 circles of Hell!!!

Post by CDXbow »

Printing
I have been unable to print properly the text content of the Editor Gadget. I wanted a simple solution that was cross platform and didn't involve third party libraries. The only previous printing I have done used the klutzy shellexecute method, I wanted something better. I tried many examples without successes and I even had the AI's try. Eventually they suggested I ask in the forums. The best I have done was with this code from srods post here, https://www.purebasic.fr/english/viewto ... 96#p548696 where he was responding to Colombo, who was suffering Printer Hell himself. This almost works, it prints 4 copies of the text, gradually shifting down and across the page then some printer job comments. I have fiddled with it endlessly and I'm going backwards. Here's the code.

Code: Select all

Procedure PrintTextFile()
  Protected text.s 
  Protected line.s
  Protected printer.i
  Protected output.i
  Protected lineNum.i
  Protected lineCount.i
  Protected vPos.i
  Protected textHeight.i
  Protected pageHeight.i  
       
  lineCount = CountGadgetItems(0)
 lineNum = 0
 text.s = ""
 text.s = GetGadgetText(0)
 
 If PrintRequester()       

  If StartPrinting(text)
 
    LoadFont(0, "Arial", 65)
    LoadFont(1, "Arial", 65)   
 
    If StartDrawing(PrinterOutput())
      
      BackColor(RGB(255, 255, 255)) ; Uses white as back color, usuful when printing on a white sheet
      FrontColor(RGB(0, 0, 0)) ; Use black for standard text color
     
      DrawingFont(FontID(0))
      textHeight = TextHeight("A")
      pageHeight = OutputHeight()
      vPos = 0
      For lineNum = 0 To lineCount-1
   
          If vPos + textHeight >= pageHeight
            vPos = 0
            NewPrinterPage()
          EndIf
        line.s = GetGadgetItemText(0, lineNum)
        DrawText(50, vPos, line)
        vPos + TextHeight
      Next
      StopDrawing()
    EndIf
   
    StopPrinting()
  EndIf
If there is anyone who can fix it or has a working procedure that prints the contents of the editor gadget I'd love to see it.
User avatar
CDXbow
User
User
Posts: 97
Joined: Mon Aug 12, 2019 5:32 am
Location: Oz

Re: Printing in PB, Dante's 7 circles of Hell!!!

Post by CDXbow »

No help?
No one has code that can print text from the editor gadget without using a print library?
Andesdaf
User
User
Posts: 83
Joined: Sun Mar 22, 2009 2:53 pm
Location: GER, Saxony

Re: Printing in PB, Dante's 7 circles of Hell!!!

Post by Andesdaf »

quick&dirty

Code: Select all

Procedure eventPrintHandler()
  If Not PrintRequester()
    ProcedureReturn 
  EndIf
  
  If StartPrinting("Test") 
    iFont = LoadFont(#PB_Any, "Arial", 12)
    If IsFont(iFont)
      If StartVectorDrawing(PrinterVectorOutput(#PB_Unit_Point))
  
        VectorFont(FontID(iFont), 12)
  
        DrawVectorParagraph(GetGadgetText(0), VectorOutputWidth(), VectorOutputHeight())
  
        StopVectorDrawing()
      Endif
    Endif
    StopPrinting()
  Endif
EndProcedure
 
If OpenWindow(0, 0, 0, 322, 180, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 306, 133)
  ButtonGadget(1, 8, 146, 40, 25, "Print")
  BindGadgetEvent(1, @eventPrintHandler())
  For a = 0 To 5
    AddGadgetItem(0, a, "Line "+Str(a))
  Next
  AddGadgetItem(0, a, "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. " +
                      "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, " + 
                      "consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.")
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Last edited by Andesdaf on Sat Mar 01, 2025 10:38 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18150
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Printing in PB, Dante's 7 circles of Hell!!!

Post by Fred »

Just to point out that this kind of code with procedure return at every unsuccessful call is very risky, here you will miss a StopDarwing() if you exit the procedure after StartDrawing()

Also what's wrong with printing in PB ?
Andesdaf
User
User
Posts: 83
Joined: Sun Mar 22, 2009 2:53 pm
Location: GER, Saxony

Re: Printing in PB, Dante's 7 circles of Hell!!!

Post by Andesdaf »

True, I updated my above code.
User avatar
CDXbow
User
User
Posts: 97
Joined: Mon Aug 12, 2019 5:32 am
Location: Oz

Re: Printing in PB, Dante's 7 circles of Hell!!!

Post by CDXbow »

Thanks Andesdaf, I don't mind it quick and dirty, I will try this afternoon.

Thanks for getting back to me Fred. PB makes many things easy, printing is not one of them. You're a gun programmer so it's easy for you but for mugs like me it's not. There have been many print libraries over the years, unfortunately many are incompatible now. There are many posts in the forums with folks struggling with printing. Not many example PB programs print and if they do it's often the inelegant shellexucute . After 20+ years the IDE still doesn't print, all of this suggests to me it's not that easy. With javascript/html it's 5 lines to print. It works. Easy. One more line and I can even add spellchecking. Tough competition.

While I was going around in ever smaller circles trying to get it to print properly, it came to me, the Print Gadget. It would fit conceptually with PB nicely.

1. Print text from a text containing gadget
2. Print a text file - really just a special case of 1, should be easy.
3. Print an Image - harder
4. Print the contents of a canvas - I expect it would be difficult
5. Print a window/form.

I don't know how doable this would be, but if they were possible I would be prepared to fund development, if the cost were not too high. Is it possible? Would you do it?
User avatar
deeproot
Enthusiast
Enthusiast
Posts: 284
Joined: Thu Dec 17, 2009 12:00 pm
Location: Llangadog, Wales, UK
Contact:

Re: Printing in PB, Dante's 7 circles of Hell!!!

Post by deeproot »

Fred wrote: Sat Mar 01, 2025 8:20 pm Also what's wrong with printing in PB ?
Speaking entirely from a personal point of view - nothing, it works just fine.

I guess different people have different needs, but any changes to PB printer commands that are not backward compatible would be an issue. Routines in my application were written quite a few years ago and involve a fair bit of code.
Post Reply