[comp.lang.postscript] printing thin lines on 300dpi printer

dsmith@applga.aa.cad.slb.com (J. Daniel Smith) (10/02/90)

I'm trying to print 1 pixel wide lines on a 300dpi PS printer.
Following the techniques on page 62-63 of _Real World PostScript_, 
I have the code shown below.

The problem is that the top line of the box is thicker than the other
sides.  I tried printing this file on two different Apple PS printers,
and the result was the same on both of them.  I also tried different
values for "setlinewidth" (0.24, 0.12 and 0.003).  And I tried putting
the SnapToPixel before the "rmoveto".  None of this worked!  There
must be a way to do this properly (i.e. *not* doing everything in
1/300 inch units).

Thanks for any and all help.
   Dan

P.S. Internet mail isn't working properly right now; use either
"uunet!sharkey!applga!dsmith" or "smithda@buster.cps.msu.edu" or just
post a followup article.
=========================================================================
J. Daniel Smith                     Internet: dsmith@applga.aa.cad.slb.com
Schlumberger CAD/CAM                  BITNET: smithdan@msuegr
Ann Arbor, Michigan                   Usenet: uunet!sharkey!applga!dsmith
                                       SINet: AAACA1::SMITH

The two most common things in the universe are hydrogen and stupidity,
but not necessarily in that order. 
=========================================================================
---------
%!PS-Adobe-2.0
%%EndComments

/bdf {bind def} bind def
/inch {72 mul} bdf

%
% Logo sizes
/boxy {.25 inch} bdf      % box start this far up from division name
/boxlength {7.5 inch} bdf % length of entire logo box
/logoheight {.1875 inch} bdf

/SnapToPixel   % snap X,Y coord to device pixels
  { transform
    round .25 add
    exch
    round .25 add
    exch
    itransform
  } bdf

/drawboxes
 {gsave
  .12 setlinewidth % 1/600 inch hairlines 
  newpath				% long, narrow box
    0 boxy SnapToPixel moveto
    boxlength 0 rlineto
    0 logoheight rlineto
    boxlength neg 0 rlineto
  closepath
  stroke

  grestore
  } bdf
%%EndProlog

%%BeginSetup
0.5 inch 1 inch SnapToPixel translate  % 0.5 inch margins
%%EndSetup

drawboxes

showpage
%%Trailer

lambert@spectrum.cs.unsw.oz.au (Tim Lambert) (10/03/90)

>>>>> On 1 Oct 90 22:21:16 GMT, dsmith@applga.aa.cad.slb.com (J. Daniel Smith) said:

> I'm trying to print 1 pixel wide lines on a 300dpi PS printer.

Why not say "0 setlinewidth"?

Tim

woody@chinacat.Unicom.COM (Woody Baker @ Eagle Signal) (10/04/90)

In article <LAMBERT.90Oct3110022@nankeen.spectrum.cs.unsw.oz.au>, lambert@spectrum.cs.unsw.oz.au (Tim Lambert) writes:
> >>>>> On 1 Oct 90 22:21:16 GMT, dsmith@applga.aa.cad.slb.com (J. Daniel Smith) said:
> 
> > I'm trying to print 1 pixel wide lines on a 300dpi PS printer.
> 
> Why not say "0 setlinewidth"?
Because as has been pointed out before, 0 setlinewidth is not guaranteed to
produce a 1 pixel wide line.  setting it to .12 is supposed to explicitly
do that.  A lot of times the problems with wide lines can be traced to
rounding problems.  As I understand it, you can round up or down
to .25, 0 .5.  Since any dot touched by a line gets filled in, in certain
roungings, you will have problems.  This seems to occur when you have
rounded such that you are straddling  the boundry between 2 ajacent
rows of pixels.  Some time back, the rules were posted to the net.
I'm not sure just where they are.


Cheers
Woody

lambert@spectrum.cs.unsw.oz.au (Tim Lambert) (10/05/90)

>>>>> On 4 Oct 90 00:32:29 GMT, woody@chinacat.Unicom.COM (Woody Baker @ Eagle Signal) said:

> In article <LAMBERT.90Oct3110022@nankeen.spectrum.cs.unsw.oz.au>, I wrote:
>> >>>>> On 1 Oct 90 22:21:16 GMT, dsmith@applga.aa.cad.slb.com (J. Daniel Smith) said:

>> > I'm trying to print 1 pixel wide lines on a 300dpi PS printer.

>> Why not say "0 setlinewidth"?
> Because as has been pointed out before, 0 setlinewidth is not guaranteed to
> produce a 1 pixel wide line.  setting it to .12 is supposed to explicitly
> do that.

Huh?  The Red Book says that a line width of 0 produces a line one
device pixel wide.  If it doesn't then there is something wrong with
the implementation of PostScript on your printer.  I haven't tried it
that much, but it has worked whenever I've tried it.  You can draw a one
pixel wide line easily and very quickly using Bresenham's algorithm so
there aren't any excuses for an implementation that doesn't get it
right.

> A lot of times the problems with wide lines can be traced to
> rounding problems.  As I understand it, you can round up or down
> to .25, 0 .5.  Since any dot touched by a line gets filled in, in certain
> roungings, you will have problems.  This seems to occur when you have
> rounded such that you are straddling  the boundry between 2 ajacent
> rows of pixels. 


Yes, I can see how this can happen.  But if a line width of 0 gives
you a line two pixels wide, I don't see how a line width of 0.12 could
make it narrower.

Tim

fryd@g.gp.cs.cmu.edu (Michael Fryd) (10/06/90)

> Newsgroups: comp.lang.postscript
> Date: 1 Oct 90 22:21:16 GMT
> Sender: dsmith@applga.aa.cad.slb.com (J. Daniel Smith)
> Reply-To: smithda@buster.cps.msu.edu
> 
> I'm trying to print 1 pixel wide lines on a 300dpi PS printer.
> Following the techniques on page 62-63 of _Real World PostScript_, 
> I have the code shown below.
> 
> The problem is that the top line of the box is thicker than the other
> sides.  I tried printing this file on two different Apple PS printers,
> and the result was the same on both of them.  I also tried different
> values for "setlinewidth" (0.24, 0.12 and 0.003).  And I tried putting
> the SnapToPixel before the "rmoveto".  None of this worked!  There
> must be a way to do this properly (i.e. *not* doing everything in
> 1/300 inch units).


The problem is that the endpoints for each line segment must be properly
alligned with the device pixels.  The Solution is to use SnapToPixel
on each corner of the box.  This requires a slight rewrite to the code because
SnapToPixel only works with absolute coordinates, not relative moves.



the solution is to change:
    0 boxy SnapToPixel moveto
    boxlength 0 rlineto
    0 logoheight rlineto
    boxlength neg 0 rlineto
  closepath

to:
    0 boxy SnapToPixel moveto
    boxlength boxy SnapToPixel lineto
    boxlength logoheight boxy add  SnapToPixel lineto
    0 logoheight boxy add SnapToPixel lineto
  closepath



Michael Fryd
President                           Voice: (412) 751-5557
MEFCO, Inc.                         Fax:   (412) 751-8403
2401 Coulter Road                   Email: Michael.Fryd@CS.CMU.EDU
McKeesport, PA  15131-4251