* What are the units used for measuring printing views? Points (1/72
inch),
or some other unit?
I'm writing a little photobooth application that takes 4 pictures with
an iSight, assembles them together, and prints them on 4x6 photo
paper. Printing is done by a subclass of NSView; the drawRect method
is below. I've set the instance's size to 6*72.0, 4*72.0, assuming
that points are the right units, and then compute an affine
transformation that should map
my assembled image to the 4x6 page. But when I actually run the print
operation, the resulting output is much too small, occupying perhaps a
quarter of the 4x6 page, and it's also strangely offset from the
corner.
Does anyone have suggestions for debugging this problems, or pointers
to helpful documentation or articles?
--amk
- (void)drawRect:(NSRect)rect {
NSImage *strip;
NSPrintInfo *print_info;
NSRect bounds;
print_info = [NSPrintInfo sharedPrintInfo];
bounds = [print_info imageablePageBounds];
strip = [model getCurrentStrip];
printf("passed-in rect %f %f %f %f\n",
rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
printf("bounds of drawing %f %f %f %f\n",
bounds.origin.x, bounds.origin.y, bounds.size.width,
bounds.size.height);
float avail_width = (bounds.size.width);
float avail_height= (bounds.size.height);
printf("available paper = %f %f\n", avail_width, avail_height);
[self lockFocus];
NSAffineTransform *transform = [NSAffineTransform transform];
[transform scaleXBy:(avail_width/(2*ISIGHT_WIDTH))
yBy:(avail_height/(2*ISIGHT_HEIGHT))];
[transform translateXBy:bounds.origin.x yBy:bounds.origin.y];
[transform concat];
[strip drawInRect: bounds
fromRect: NSZeroRect
operation: NSCompositeSourceOver
fraction: 1.0];
[self unlockFocus];
}
> I'm trying to understand the coordinate systems and units used for
> printing with Cocoa; does anyone know of a good resource? Apple's
> printing architecture guide doesn't seem to explain it very well. In
> particular:
>
> * What are the units used for measuring printing views? Points (1/72
> inch),
> or some other unit?
1/72 of an inch. I wrote the following test program to prove it:
// AppDelegate.h
// Created by David Phillip Oster on 5/25/08. Apache License
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject {
}
- (IBAction)print:(id)sender;
@end
// AppDelegate.m
//
// Created by David Phillip Oster on 5/25/08. Apache License
//
#import "AppDelegate.h"
#import "PrintView.h"
@implementation AppDelegate
- (IBAction)print:(id)sender {
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
PrintView *printView = [[[PrintView alloc]
initWithPrintInfo:printInfo] autorelease];
NSPrintOperation *printOp =
[NSPrintOperation printOperationWithView:printView
printInfo:printInfo];
[printOp setShowPanels:YES];
[printOp runOperation];
}
@end
// PrintView.h
//
// Created by David Phillip Oster on 5/25/08. Apache License
//
#import <Cocoa/Cocoa.h>
// Draws 1 inch squares every other inch.
// Prints two pages with second page intentionally blank.
@interface PrintView : NSView {
// for printing.
NSSize paperSize_;
float paperMarginLeft_;
float paperMarginTop_;
}
- (id)initWithPrintInfo:(NSPrintInfo *)printInfo;
- (BOOL)isFlipped;
- (BOOL)knowsPageRange:(NSRangePointer)rangep;
@end
// PrintView.m
//
// Created by David Phillip Oster on 5/25/08. Apache License
//
#import "PrintView.h"
enum {
RES = 72
};
@implementation PrintView
- (id)initWithPrintInfo:(NSPrintInfo *)printInfo {
paperSize_ = [printInfo paperSize];
paperMarginLeft_ = [printInfo leftMargin];
paperMarginTop_ = [printInfo topMargin];
NSRect frame = NSUnionRect([self rectForPage:1], [self rectForPage:2]);
self = [super initWithFrame:frame];
return self;
}
// origin at top, y increases down.
- (BOOL)isFlipped {
return YES;
}
// 1's based page num
- (NSRect)rectForPage:(int)pageNum {
return NSMakeRect(0, (pageNum-1) * paperSize_.height,
paperSize_.width, paperSize_.height);
}
- (void)drawRect:(NSRect)visRect {
int x, y;
for (y = 0; y < 11;y += 2) {
for (x = 0; x < 9; x+= 2) {
NSRect r = NSMakeRect(x*RES, y*RES, RES, RES);
if (NSIntersectsRect(visRect, r)) {
NSRect intersectionRect = NSIntersectionRect(visRect, r);
if ( 2 < intersectionRect.size.height) { // Note 1, below.
[NSBezierPath strokeRect:r];
}
}
}
}
}
// Note 1: without this if statement, this would draw the bottom
// edge of the squares at the top of the second page.
- (BOOL)knowsPageRange:(NSRangePointer)rangep {
rangep->location = 1; // 1's based page num
rangep->length = 2;
return YES;
}
@end