Board index » delphi » Delphi Canvases (take 2)

Delphi Canvases (take 2)

I had posted the following msg about 2 days ago but didn't get
an answer to my question -- still clueless about the problem
I'm dealing with so I'm posting it again.  Thanx.

---------------------------------------------------

Hi All,

        I'm trying to use Canvases in Delphi to print some graphics
and text out.  When I use the canvas associated with my Form everything
works just fine, but when I try and execute the same code using the
canvas associated with my Printer object things go sour -- It seems
as if the Printer.Canvas is using a different coordinate system
than Form.Canvas.  Apparently, Printer.Canvas has a much denser resolution.
So, the statement:

        TextOut(480, 20, 'Hello World');

'Hello World' appears at the right side of the Form (from my perpective)
when using Form.Canvas, but it PRINTS at the left hand side
on the PAPER (using Printer.Canvas), moving ever so slightly to
the right as if the x-coordinate of 480 is reached somewhere close
to the left margin -- In order to push the text out to the right using
Printer.Canvas the x-coordinate has to be set to very high values
e.g. 1000, 2000.

At first, I thought that somehow Printer.Canvas was using a different
unit of resolution e.g. twips as opposed to pixels, but after going
thru the manuals and online help exhaustively, I found no mention of
twips.  Also, I've been unable to find any info on how I can change
the coordinate system of Printer.Canvas to a less dense scale.  

Comments, suggestions?

Thanx much,
Ali

-- Here's the entire code snippet --

{ Print on Form.Canvas -- WORKS FINE}
Form1.Show;
Form1.Canvas.Font.Name := 'Times New Roman';
Form1.Canvas.Font.Style := [fsBOLD];
Form1.Canvas.Font.Size := 20;
Form1.Canvas.TextOut(480, 20, 'Hello World');

{ Print on Printer.Canvas -- DOESN'T WORK LIKE IT SHOULD}
Printer.BeginDoc;
Printer.Canvas.Font.Name := 'Times New Roman';
Printer.Canvas.Font.Style := [fsBOLD];
Printer.Canvas.Font.Size := 20;
Printer.Canvas.TextOut(480, 20, 'Hello World');
Printer.EndDoc;

 

Re:Delphi Canvases (take 2)


Quote
StarCrusher <abukh...@vpcc.sunysb.edu> wrote:
>    TextOut(480, 20, 'Hello World');
>'Hello World' appears at the right side of the Form (from my perpective)
>when using Form.Canvas, but it PRINTS at the left hand side
>on the PAPER (using Printer.Canvas), moving ever so slightly to
>the right as if the x-coordinate of 480 is reached somewhere close
>to the left margin -- In order to push the text out to the right using
>Printer.Canvas the x-coordinate has to be set to very high values
>e.g. 1000, 2000.
>At first, I thought that somehow Printer.Canvas was using a different
>unit of resolution e.g. twips as opposed to pixels, but after going
>thru the manuals and online help exhaustively, I found no mention of
>twips.  Also, I've been unable to find any info on how I can change
>the coordinate system of Printer.Canvas to a less dense scale.  
>Comments, suggestions?

I believe the difference is due to the resolution of the 2 different
devices. On a typical monitor setting the number of pixels per inch is
between 72 to 96, this depends on resolution. You can find this out by
the property PixelsPerInch of you Form. On a typical printer, the
resolution is 300 pixel per inch , this again depends on your printer
and I believe you can get runtime information of this by checking up
devicecapability or something like this (read the printer object help
or Tcanvas help to see if you can find something like this).

So to set a text at 1 inch from the left,
on a screen with 96 pix/inch, Left := 96
on a printer with 300 pix/inch, Left := 300;

You will need to do some calculation, translation before it will print
correctly.

All the best,
Edison
::::::::::::::::::::::::::::::::::::::::::::::
::                                          ::
::  Edison Too the Stone Man                ::
::                                          ::
::::::::::::::::::::::::::::::::::::::::::::::

Re:Delphi Canvases (take 2)


Quote
StarCrusher <abukh...@vpcc.sunysb.edu> wrote:
>    I'm trying to use Canvases in Delphi to print some graphics
>and text out.  When I use the canvas associated with my Form everything
>works just fine, but when I try and execute the same code using the
>canvas associated with my Printer object things go sour -- It seems
>as if the Printer.Canvas is using a different coordinate system
>than Form.Canvas.  Apparently, Printer.Canvas has a much denser resolution.

Hello Ali,
Of course ithas much denser resolution - screen 96dpi and printers
from 140dpi to more than 600dpi. All you have to do is to write
simple conversion rountines.

for example (should be optimizedfor speed)

PrinterX := Round( FormX*(Printer.PageWidth/Form.Width));

the same holds for height.

Hope it helps,
Pawel

--
\||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||/
-         Pawel Ksiezyk     ksie...@bull.mimuw.edu.pl        -
-    Institute of Informatics   Warsaw University   Poland   -
/||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\

Re:Delphi Canvases (take 2)


Quote
StarCrusher wrote:
>         I'm trying to use Canvases in Delphi to print some graphics
> and text out.  When I use the canvas associated with my Form everything
> works just fine, but when I try and execute the same code using the
> canvas associated with my Printer object things go sour -- It seems
> as if the Printer.Canvas is using a different coordinate system
> than Form.Canvas.  Apparently, Printer.Canvas has a much denser resolution.
> So, the statement:

>         TextOut(480, 20, 'Hello World');

> 'Hello World' appears at the right side of the Form (from my perpective)
> when using Form.Canvas, but it PRINTS at the left hand side
> on the PAPER (using Printer.Canvas), moving ever so slightly to
> the right as if the x-coordinate of 480 is reached somewhere close
> to the left margin -- In order to push the text out to the right using
> Printer.Canvas the x-coordinate has to be set to very high values
> e.g. 1000, 2000.

Well, yes. Your printer is putting out 300 or 1200 dots per inch, and your
monitor is only doing maybe 100. So, you have to scale your printing.

You have two basic choices: Use screen units, and convert to printer units at
print time, or use some abstract unit and convert at both display and print
time. For example, if you use a unit that's equal to one ten thousandth of
the display area (0..9999):

const
  AbstractPels = 10000;

with Printer.Canvas do
  TextOut( LongInt(AbstractX) * ClipRect.Right div AbstractPels,
           LongInt(AbstractY) * ClipRect.Bottom div AbstractPels,
           'Hello World' );

alternatively, to just scale your form to the printer:

with Printer.Canvas do
  TextOut( LongInt(AbstractX) * ClipRect.Right div Form.Right,
           LongInt(AbstractY) * ClipRect.Bottom div Form.Bottom,
           'Hello World' );

--

http://www.midnightbeach.com/jon   Personal Pages
http://www.midnightbeach.com/jon/pubs Programming Publications
http://www.midnightbeach.com/hs             Home School Resource List

Re:Delphi Canvases (take 2)


Quote
StarCrusher <abukh...@vpcc.sunysb.edu> wrote:
>Hi All,
>    I'm trying to use Canvases in Delphi to print some graphics
>and text out.  When I use the canvas associated with my Form everything
>works just fine, but when I try and execute the same code using the
>canvas associated with my Printer object things go sour -- It seems
>as if the Printer.Canvas is using a different coordinate system
>than Form.Canvas.  Apparently, Printer.Canvas has a much denser resolution.
>So, the statement:
>    TextOut(480, 20, 'Hello World');
>'Hello World' appears at the right side of the Form (from my perpective)
>when using Form.Canvas, but it PRINTS at the left hand side
>on the PAPER (using Printer.Canvas), moving ever so slightly to
>the right as if the x-coordinate of 480 is reached somewhere close
>to the left margin -- In order to push the text out to the right using
>Printer.Canvas the x-coordinate has to be set to very high values
>e.g. 1000, 2000.
>At first, I thought that somehow Printer.Canvas was using a different
>unit of resolution e.g. twips as opposed to pixels, but after going
>thru the manuals and online help exhaustively, I found no mention of
>twips.  Also, I've been unable to find any info on how I can change
>the coordinate system of Printer.Canvas to a less dense scale.  
>Comments, suggestions?
>Thanx much,
>Ali
>-- Here's the entire code snippet --
>{ Print on Form.Canvas -- WORKS FINE}
>Form1.Show;
>Form1.Canvas.Font.Name := 'Times New Roman';
>Form1.Canvas.Font.Style := [fsBOLD];
>Form1.Canvas.Font.Size := 20;
>Form1.Canvas.TextOut(480, 20, 'Hello World');
>{ Print on Printer.Canvas -- DOESN'T WORK LIKE IT SHOULD}
>Printer.BeginDoc;
>Printer.Canvas.Font.Name := 'Times New Roman';
>Printer.Canvas.Font.Style := [fsBOLD];
>Printer.Canvas.Font.Size := 20;
>Printer.Canvas.TextOut(480, 20, 'Hello World');
>Printer.EndDoc;

A printer that prints at 300dpi on a form 8 inches wide has about 2400
pixels from side to side as compared to 480 (or 800 or 1024) on your
screen. Therefore proportions are different and must be taken into
account, including x and y axis positions and font size. The printer
canvas is therefore larger than the Screen canvas.

Gerry

Re:Delphi Canvases (take 2)


In article: <31D2B464.7...@vpcc.sunysb.edu>  StarCrusher <abukh...@vpcc.sunysb.edu> writes:

Quote
> Path:

qmap.demon.co.uk!news.demon.co.uk!dispatch.news.demon.net!demon!usenet2.news.uk.psi.net!uknet!us
enet1.news.uk.psi.net!uknet!tank.news.pipex.net!pipex!oleane!jussieu.fr!math.ohio-state.edu!uwm.
edu!newsfeed.internetmci.com!news.dbtech.net!news.sprintlink.net!news-fw-12.sprintlink.net!news.
cc.sunysb.edu!usenet

Quote
> From: StarCrusher <abukh...@vpcc.sunysb.edu>
> Newsgroups: comp.lang.pascal.delphi.misc
> Subject: Delphi Canvases (take 2)
> Date: Thu, 27 Jun 1996 12:18:44 -0400
> Organization: State University of New York at Stony Brook
> Lines: 55
> Message-ID: <31D2B464.7...@vpcc.sunysb.edu>
> NNTP-Posting-Host: tearose.vpcc.sunysb.edu
> Mime-Version: 1.0
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
> X-Mailer: Mozilla 2.02Gold (Win95; I)

> I had posted the following msg about 2 days ago but didn't get
> an answer to my question -- still clueless about the problem
> I'm dealing with so I'm posting it again.  Thanx.

> ---------------------------------------------------

> Hi All,

>    I'm trying to use Canvases in Delphi to print some graphics
> and text out.  When I use the canvas associated with my Form everything
> works just fine, but when I try and execute the same code using the
> canvas associated with my Printer object things go sour -- It seems
> as if the Printer.Canvas is using a different coordinate system
> than Form.Canvas.  Apparently, Printer.Canvas has a much denser resolution.
> So, the statement:

>    TextOut(480, 20, 'Hello World');

> 'Hello World' appears at the right side of the Form (from my perpective)
> when using Form.Canvas, but it PRINTS at the left hand side
> on the PAPER (using Printer.Canvas), moving ever so slightly to
> the right as if the x-coordinate of 480 is reached somewhere close
> to the left margin -- In order to push the text out to the right using
> Printer.Canvas the x-coordinate has to be set to very high values
> e.g. 1000, 2000.

> At first, I thought that somehow Printer.Canvas was using a different
> unit of resolution e.g. twips as opposed to pixels, but after going
> thru the manuals and online help exhaustively, I found no mention of
> twips.  Also, I've been unable to find any info on how I can change
> the coordinate system of Printer.Canvas to a less dense scale.  

> Comments, suggestions?

> Thanx much,
> Ali

Ali,

Had the same problem, this is the solution:

Before printing do this:

  printer.font.pixelsPerInch := getDeviceCaps(form.canvas.handle, LOGPIXELSY);

The pixelsPerInch property of the font is there so that a font of n points on the screen
is n points on the printer, or something like that, and that get device caps thingy, if you
tell it LOGPIXELSY and the handle of a device, it tells you how many dots there are in an
inch on that device.

Lots of love
yer uncle DOB

Other Threads