Re: Drawing same text and graphics in different CGContextRef
site_archiver@lists.apple.com Delivered-To: cocoa-dev@lists.apple.com
On Sep 22, 2023, at 12:44 PM, JPH <turbo3d@free.fr> wrote:
Hi David,
Trying to understand the mechanism you suggest ( I am not familiar with the concept of context pushing - poping :(I )
NSGraphicsContext <apple-reference-documentation://hcmXlh9fv1> * newcontext= [NSGraphicsContex <apple-reference-documentation://hcmXlh9fv1> graphicsContextWithCGContext:theprintContext flipped:YES] // theprintContext obtained from PMSessionGetCGGraphicsContext(printSession, &theprintContext) [newcontext saveGraphicsState]; //Saves the current graphics state and creates a new graphics state on the top of the stack. myDrawMethod( newcontext.CGContext) [NSGraphicsContext <apple-reference-documentation://hcmXlh9fv1> restoreGraphicsState;]
Is this correct ?
You want NSGraphicsContext.currentContext (I had expected API similar to UIKit without looking it up, but it looks like you just have to save the current context, set your current context, and restore it afterwards).
All the best JP
a graphic context created by myself ( hence not the supplied on by the printing framework )
Le 22 sept. 2023 à 17:59, David Duncan <david.duncan@apple.com <mailto:david.duncan@apple.com>> a écrit :
On Sep 22, 2023, at 11:12 AM, JPH via Cocoa-dev <cocoa-dev@lists.apple.com <mailto:cocoa-dev@lists.apple.com>> wrote:
I am facing the following problems in drawing text and graphics in CGContextRef of an NSview and Printing framework:
1) drawing text using: [finalString drawInRect:textBox withAttributes:attributes]; Works nicely in my sub-classed NSViews in a CGContextRef obtained in the drawRect : method with CGContextRef viewCtx= [[NSGraphicsContext currentContext] CGContext]; But draws nothing in a print context obtained PMSessionGetCGGraphicsContext(printSession, &ctx); //( geometry is perfectly previewed,Pdf’ed and printed except texts .. )
2) filling a bezier path with a pattern Color
NSImage* nsPatImage= [[NSImage alloc] initWithCGImage:cgPatImage size:NSZeroSize]; NSColor* patternColor= [NSColor colorWithPatternImage:nsPatImage]; [patternColor setFill]; [bezierPath fill];
Works like a charm in the graphic context of NSView and print nothing in a print context.
I suspect this has something to do with the notion of « Current context » , as the context in NSView is explicitly the « currentContext » But not sure if the printing context is the « current one » in the usual print sequence ;
Correct. The current context is an AppKit concept, and you are working below AppKit in this code sequence – so you need to ensure the context is made current yourself. So you need to create an NSGraphicsContext, push it, and pop it, around your drawing code that relies on the current context. Since you pass the CGContext to myDrawMethod:, you could do this inside that method.
err= PMSessionBeginPageNoDialog(printSession, format ) ctx = PMSessionGetCGGraphicsContext(printSession, &ctx) [myDrawMethode: ctx] // [finalString drawInRect:textBox withAttributes:attributes]; draws nothing in the printing context PMSessionEndPageNoDialog(printSession);
I would appreciate any advice or suggestion regarding these points King regards JP
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev@lists.apple.com <mailto:Cocoa-dev@lists.apple.com>)
Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com <http://lists.apple.com/>
Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/david.duncan%40apple.com
This email sent to david.duncan@apple.com
_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com
participants (1)
-
David Duncan via Cocoa-dev