Is it correct to implement scaled printing in Cocoa?

I am trying to implement printing in a new Cocoa application. Everything is working fine, except for scaling, that is, printing at 75%, 125%, etc.

As far as I can tell from Apple's docs, the program should adjust the rectangle returned from the rectForPage method: depending on the scale factor. I found an Apple code sample that seemed to work that way, and an old cocoabuilder post.

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Printing/osxp_pagination/osxp_pagination.html

http://www.cocoabuilder.com/archive/cocoa/211683-scaled-printing.html

My rectForPage: the code looked like this:

NSPrintInfo *pi = [[NSPrintOperation currentOperation] printInfo];
NSSize paperSize = [pi paperSize]; // Calculate the page dimensions in points
// Convert dimensions to the scaled view
CGFloat pageScale = [[[pi dictionary] objectForKey:NSPrintScalingFactor] floatValue];
CGFloat topMargin = [pi topMargin];
CGFloat leftMargin = [pi leftMargin];
CGFloat bottomMargin = [pi bottomMargin];
CGFloat rightMargin = [pi rightMargin];
CGFloat pageHeight = (paperSize.height - topMargin - bottomMargin) / pageScale;
CGFloat pageWidth = (paperSize.width - leftMargin - rightMargin) / pageScale;
NSRect bounds = [self bounds];
NSRect actualPageRect = NSMakeRect(
                                   NSMinX(bounds),
                                   NSMinY(bounds),
                                   pageWidth * pageScale,
                                   pageHeight * pageScale);

return actualPageRect;

. , , 100%. , actualPageRect , , . , Apple , , .

. directForPage: , drawRect: :

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    NSAffineTransform* xform = [NSAffineTransform transform];
    [xform scaleXBy:pageScale yBy:pageScale];
    [xform concat];
    [self layoutReport];
    [xform invert];
    [xform concat];
}

knowPageRange: .

- . , , . , . , - , , , , rectForPage:, . , , , .

+4
2

rectForPage:, :

page.

, , pageNumber. NSZeroRect, pageNumber .

, . , , , . ( drawRect: , .) (.. , self.bounds), ( - 1/72 ).

, scalingFactor ?

, 10.12.6 AppKit - scalingFactor . ( - .)

drawRect: - . ; . scalingFactor 2, , (1/2 ) .

, ( setBoundsSize: scaleUnitSquareToSize:) . , . , , ; , . ​​

, ( 10.12.6), , .

, , - TextEdit, ( , , , topMargin bottomMargin). , , , , .

+1

OSX/Swift 5: ( PageRange, rectForPage.)

override func knowsPageRange(_ range: NSRangePointer) -> Bool {
        return true
    }


override func rectForPage(_ page: Int) -> NSRect {

    guard let pi = NSPrintOperation.current?.printInfo else{return CGRect.zero}

    let paperSize = pi.paperSize // Calculate the page dimensions in points
    // Convert dimensions to the scaled view

    let dict = pi.dictionary()
    let pageScale = dict[NSPrintInfo.AttributeKey.scalingFactor] as! CGFloat

    let topMargin = pi.topMargin
    let  leftMargin = pi.leftMargin
    let bottomMargin = pi.bottomMargin
    let rightMargin = pi.rightMargin
    let pageHeight = (paperSize.height - topMargin - bottomMargin) / pageScale
    let pageWidth = (paperSize.width - leftMargin - rightMargin) / pageScale
    let bounds = self.bounds
    let actualPageRect = NSRect(x: NSMinX(bounds), y:  NSMinY(bounds), width: pageWidth * pageScale, height: pageHeight * pageScale)

    return actualPageRect
}

, Rect, : .

: ..

if page>1{return CGRect.zero}
0

Source: https://habr.com/ru/post/1685120/


All Articles