DPI independent Print

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

DPI independent Print

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Num3.

Well now you can put your text and gfx
with a mm accuracy on paper!!!

Don't forget this code is for A4, alter
the procedures for other paper sizes...

Here's the snippet 4 all :)

Code: Select all

;****************************************
; Super Printer - DPI indepent print
; by Rui Carvalho - 2003
;
; Part of GIA - Lift Management Software
; Freeware
;****************************************

; mailto: rui-carvalho@bigfoot.com

; Original printer code by Fred

Declare ycm(x.f)
Declare xcm(x.f)

LoadFont(0, "Arial", ycm(0.4))   ;4mm font size
LoadFont(1, "Arial", ycm(1))   ;1 cm font size

If PrintRequester()
  If StartPrinting("PureBasic Test")
    If StartDrawing(PrinterOutput())
        DrawingFont(FontID(0))
        DrawText(xcm(15), xcm(5.5), "PureBasic Printer Test")
        DrawingFont(FontID(1))   ;1 cm font size
        DrawText(xcm(10), ycm(20), "PureBasic Printer Test 2")
        If LoadImage(0, "Data\PureBasiclogo.bmp")   ;The logo bitmap is bigger
          ResizeImage(0, xcm(6), ycm(2))   ;resize image for 6cm by 2cm
          DrawImage(ImageID(0), xcm(1), ycm(1))   ;draw image at 1cm by 1cm
        Else
          MessageRequester("", "2", 0)
        EndIf
        FrontColor(RGB(100, 100, 100))
        Box(xcm(2), ycm(6), xcm(6), ycm(8))   ;draw a 4cm by 4cm square
      StopDrawing()
    EndIf
    StopPrinting()
  EndIf
EndIf

; **** the trick !!!! ****

Procedure xcm(x.f)
  result = x * (PrinterPageWidth() / 21)   ;21cm A4
  ProcedureReturn result
EndProcedure

Procedure ycm(x.f)
  result = x * (PrinterPageWidth() / 29.7)   ;29,7cm A4
  ProcedureReturn result
EndProcedure

; Tested with fineprintpdf in multiple DPI's
; and on Xerox DC440 laser printer
--
Kind Regards
Rui Carvalho

Old programmers never die... They branch into a subroutine...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by searhin.

Num3
i think that's a nice one. i was about to add a printing routine to my app anyway so i tried your structures. found it's very comfortable to use and easily gives the expected results!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Num3.
Originally posted by searhin

Num3
i think that's a nice one. i was about to add a printing routine to my app anyway so i tried your structures. found it's very comfortable to use and easily gives the expected results!
Thanks :)

I'll be releasing a full printpreview and print code soon (print being the same from this snippet) , so keep looking at the forums...




--
Kind Regards
Rui Carvalho

Old programmers never die... They branch into a subroutine...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Num3.

Here's the link to the other topic:

viewtopic.php?t=5388

enjoy

--
Kind Regards
Rui Carvalho

Old programmers never die... They branch into a subroutine...
Passant
New User
New User
Posts: 9
Joined: Sun May 11, 2003 12:33 am

Post by Passant »

I've got another question to that topic. I've you use this code there is a little problem to integrate the printerrequester in your application because the user can change the format to A5 and so on and than the printed result is in another size than you print the same on A4.
But what can I do to print on all formats exactly?
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Hi Passant,

That's not a really easy task...

1. You need to know which size was selected by the user
2. You need to make a table of conversion

Code: Select all

If A4 : width=21 :height = 29.7 : endif
If A5 : width=21 :height = 14.85 : endif

etc...
etc...
3. Change my code to use variable page size instead of fixed size

Code: Select all

Procedure xcm(x.f)

result = x * (PrinterPageWidth()/width) 
ProcedureReturn result

EndProcedure


Procedure ycm(x.f)

result = x * (PrinterPageheight()/height) 
ProcedureReturn result

EndProcedure

4. Correct the bug i've found today :oops:


Procedure ycm(x.f)

result = x * (PrinterPageWidth()/29.7)
should be: result = x * (PrinterPageheight()/29.7)
ProcedureReturn result

EndProcedure
Passant
New User
New User
Posts: 9
Joined: Sun May 11, 2003 12:33 am

Post by Passant »

Thank you for the answer first of all. It really seems to be a problem with very unconfortable solutions. Maybe it is much more easyer to solve my problem on a completely other way.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: DPI independent Print

Post by collectordave »

Another old thread.

The new vector drawing routines in PB 5.4 have made printing a fairly eay task.

Check out prinr and print preview topic.

This is DPI independent etc and prints in landscape when required and will also accept paper sizes a printer can support. Still being developed but almost there. Also shows a preview of what you want to print.

Hope this helps anyone else looking for this.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply