[Gambas-user] printer problem

Doriano Blengino doriano.blengino at ...1909...
Thu Feb 4 09:48:59 CET 2010


Charlie Reinl ha scritto:
> Salut,
>
> I still haven't solved the Draw.Text tab problem, but I'v a new one (a
> problem).
>
> If you open the printer-setup the first time, the 'Print to printer' is
> chosen, and the target-file behind 'Print to file' is empty.
>
> If you check 'Print to file' and fill the target-file and then you
> switch back to 'Print to printer', the target-file field is disabled.
> (Setup Printer.png)
>
> Everything seems to be alright. 
>
> But, and there is my new problem, in my code I can't see what should be
> done. (printing 5 times to file, makes no sense)
>
> Where is the magic pointer, which shows me the chosen device?
> (Printer Properties.png)
>   
For what I know about printing in gambas, things are as following.

You can set by code printer.file to print to a file (a postscript is 
generated), or set printer.name to direct the printing to an actual 
printer. If printer.file is non-empty, the printing will go to a file. 
The same is true if those properties are set by the interactive setup 
panel. After that, you don't have to worry about the destination, the 
code is the same: you draw() on the printer, and the underlying software 
will do what is appropriate.

Other properties let you to define other printing aspects, like page 
format, b/w or color, and so on.

The properties Copies, FromPage and ToPage are different, because they 
don't do anything - you should read them and do things accordingly. To 
be more precise, FromPage and ToPage are exactly so (if you read that 
the user has set FromPage=2 and ToPage=2, then your printing routine 
should only print the page number 2, and then terminate). "Copies" 
sometimes works, sometimes not, so I decided to read the number of 
copies, reset it to 1, and print several copies myself. I mean: "Copies" 
was sometimes working, sometimes was ignored, and sometimes messed up 
the printing. If you choose to do multiple copies by code, don't forget 
to reset Copies to 1.

About printing several copies to a file, I see nothing strange... a 
postscript file should be generated, containing two or more identical 
pages. If you feed that file to a printer, multiple identical pages get 
out. But in the first attempt, I did it differently, and I crashed into 
rewriting the same file several times, as you are arguing. Can't 
remember if it was my fault or gambas or qt one. I solved all together 
by invoking multiple times the subroutine which prints a single page, 
inside a "draw.begin()" and "draw.end()".

About tabulators, I would take a single string to print, break it in 
several sub-strings taking tabulators as separators, taking care that 
two tabulators in a row *do* define an empty string, print the single 
sub-strings, calculate the width of every string and advancing the 
position to the next tab-stop. The general idea is something like that:

    1. Declare an array for tabulator stops and fill it with suitable 
positions. Those positions can be freely set or be equally-spaced, 
depends on the application; but anyway they depend on the resolution of 
the printer. You can say "I want a tabstop every inch on paper", so your 
positions will have values 1*Printer.Resolution, 2*Printer.Resolution 
and so on. Or you could say "I want a tabulator stop to be wide as 8 
spaces", and in that case you should take the width of a space (using 
textwidth()), multiply it by 8 and then by Printer.Resolution. Or you 
could say "I want a tabulator in the middle of the page and one at 3/4 
of the page". So the first value will be Printer.Width/2 and the second 
value will be Printer.Width*3/4.

    2. Declare a subroutine which prints a string at X,Y position, 
taking tabs in account. It could look like this:

sub outtabstr(text as string, xpos as integer, ypos as integer)
  dim toprint as string
  dim tpos as integer

  while text<>"" do
    ' search a tabulator
    tpos = instr(text, "\9")
    if tpos=0 then
      ' no tabulators, print and exit
      draw.text(text, xpos, ypos)
      return
    endif

       ' there is a tab. print the left part
       toprint = left(text, tpos-1)          ' take the part to print
       text = mid(text, tpos+1)             ' and delete it from the rest
       draw.text(toprint, xpos, ypos)   ' printed
       ' now advance the X position to a suitable position
       xpos += draw.textwidth(toprint)   ' our virtual cursor now is here
       tpos = 0
       ' MAX_TABSTOPS refers to  an array 0-based
       while tpos<MAX_TABSTOPS and xpos>=tabstops[tpos]
          inc tpos
       wend
       if tpos>=MAX_TABSTOPS
          ' no more stops defined. What to do?
          xpos += 300
       else
          ' ok, move to that tab position
          xpos = tabstops[tpos]
       endif
    wend
end

The above code is not tested and has several problems. It does not work 
with UTF8, it does not check for margins, does not go to the next line, 
it does not return the position of the virtual cursor after having 
printed a string. But the general idea should be correct.

Regards,
Doriano





More information about the User mailing list