[Drawkit] Fix for aliasing of drawing edges

Brad Larson larson at sonoplot.com
Mon May 19 16:03:51 PDT 2008


I noticed that the borders of the drawing, along with the edges of  
objects, were being aliased (one-pixel-wide black lines were drawn as  
two-pixel-wide grey lines).  The Quartz drawing model is a little odd  
in that integer coordinates are considered to be between pixels, so  
you need to shift your drawing space by a half pixel in X and Y to get  
things to line up correctly.

By changing

if([NSGraphicsContext currentContextDrawingToScreen])
{
	[[self paperColour] set];
	NSRectFill( rect );
}

to be

if([NSGraphicsContext currentContextDrawingToScreen])
{
	NSAffineTransform* pixelShiftToAdjustForQuartzCoordinates =  
[NSAffineTransform transform];
	[pixelShiftToAdjustForQuartzCoordinates translateXBy:0.5 yBy:0.5];
	[pixelShiftToAdjustForQuartzCoordinates concat];
		
	[[self paperColour] set];
	NSRectFill( rect );
}

in - (void) drawRect:inView: within DKDrawing.m, you can remove this  
aliasing.  There is still some aliasing with the background grids and  
objects drawn on them, due to their drawing coordinates not mapping to  
integer values.  In DrawDemo, you'll need to turn off grid snapping to  
see how much sharper rectangles and the like are with this setting,  
because otherwise they'll snap to the non-integer grid locations.

A go-between function, template, or method might be needed to floor()  
or ceil() the x and y values of grids and drawable objects for display  
to the screen.  Apple has some tips on this at http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/chapter_4_section_6.html 
  .


______________________
Brad Larson
SonoPlot, Inc.
3030 Laura Lane, Suite 120
Middleton, WI 53562





More information about the Drawkit mailing list