• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Custom printing problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Custom printing problems


  • Subject: Re: Custom printing problems
  • From: Jonathan Jackel <email@hidden>
  • Date: Thu, 11 Dec 2003 21:13:58 -0500

Replying to my own post, I managed to solve this following Buddy's
suggestion. For the sake of the archives, the code is below. It takes
a text view and sets it up for printing two columns to a page.

The thing that was hanging me up conceptually was thinking that a text
view owned a layout manager, a text container, and a text storage. In
fact, the text storage owns a layout manager, which owns text
containers, which own their text views. The diagram here really made
the lightbulb go on:

http://developer.apple.com/documentation/Cocoa/Conceptual/
TextArchitecture/index.html#//apple_ref/doc/uid/10000086i

Jonathan

On Dec 10, 2003, at 11:24 AM, Buddy Kurz wrote:

> Were you aware of the possibility of adding multiple text containers
> (and attached views) to a single layout manager? This is beyond my
> experience but it seems like just what you need.
>
> On Dec 9, 2003, at 6:12 PM, Jonathan Jackel wrote:
>
>> I am using a custom view to set up a special layout for my document
>> when it prints. I want two columns on each page. Right now I'm
>> working on the one on the right, and I can't get it, well, right.
>>
>> Basically I set up a text view to hold the text that appears on each
>> printed page, with a frame that covers the area for the righthand
>> column. I create the view and fill it with an attributed string. I
>> figure out how much of the string is visible, save the invisible part
>> in an attributed string variable, and then delete the invisible part
>> from the text view. Then I go to the next page, laying out additional
>> text views as necessary. The code appears below.
>>
>> This works pretty well, but I occasionally get very strange results.
>> For instance, sometimes what should be the last line of the third page
>> is printed above the first line on the second page. There has to be a
>> reasonable explanation for this, but I don't know what it could be.
>> Any ideas? BTW, if anyone has a better approach to multi-column
>> printing, I'd love to hear it.


@interface MyPrintView : NSView {
NSTextView *sourceView;
NSLayoutManager *manager;
float pageHeight;
float pageWidth;
float pages;
}

- (id)initWithTextView:(NSTextView *)textView;

//Typical accessors here for sourceView and manager

- (BOOL)knowsPageRange:(NSRange *)rptr;
- (NSRect)rectForPage:(int)pageNum;


@end

@implementation MyPrintView

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}

- (id)initWithTextView:(NSTextView *)textView
{
self = [super initWithFrame:[textView frame]];
if (self) {
// Initialization code here.
int guessPages;
NSSize paperSize;
BOOL isFirstColumn = NO;
NSRange glyphRange;
unsigned glyphCount;
NSTextView *anotherView;
NSTextStorage *storage;
NSTextContainer *container;
NSPrintInfo *info;
NSRect tempFrame;

[self setSourceView:textView];
info = [NSPrintInfo sharedPrintInfo];
paperSize = [info paperSize];
pageWidth = paperSize.width - ([info leftMargin] + [info
rightMargin]);
pageHeight = paperSize.height - ([info topMargin] + [info
bottomMargin]);

pages = 1;
storage = [[self sourceView] textStorage];
guessPages = 1 + [storage length]/750;
//[self setFrameSize:NSMakeSize(pageWidth, pageHeight *
guessPages)];

[self setManager:[[[NSLayoutManager alloc] init] autorelease]];
tempFrame = NSMakeRect(0, 0, pageWidth/2, pageHeight);
container = [[[NSTextContainer alloc]
initWithContainerSize:tempFrame.size] autorelease];
[storage addLayoutManager:manager];
[manager addTextContainer:container];
anotherView = [[[NSTextView alloc] initWithFrame:tempFrame
textContainer:container] autorelease];
[self addSubview:anotherView];
glyphCount = [manager numberOfGlyphs];
glyphRange = [manager glyphRangeForTextContainer:container];

while (NSMaxRange(glyphRange) < glyphCount)
{
if (isFirstColumn) pages++;
tempFrame = NSMakeRect(pageWidth/2 * (isFirstColumn ? 0:
1), pageHeight * (pages - 1), pageWidth/2, pageHeight - 36);
container = [[[NSTextContainer alloc]
initWithContainerSize:tempFrame.size] autorelease];
[storage addLayoutManager:manager];
[manager addTextContainer:container];
anotherView = [[[NSTextView alloc] initWithFrame:tempFrame
textContainer:container] autorelease];
[self addSubview:anotherView];
glyphRange = [manager glyphRangeForTextContainer:container];
isFirstColumn = (!isFirstColumn);
}

[self setFrameSize:NSMakeSize(pageWidth, pageHeight * pages)];
}
return self;
}

- (void)dealloc
{
[[[self sourceView] textStorage] removeLayoutManager:manager];
[sourceView release];
[manager release];
[super dealloc];
}


//Typical accessors here for sourceView and manager

- (BOOL)knowsPageRange:(NSRange *)rptr
{
rptr->location = 1;
rptr->length = pages;
return YES;
}

- (NSRect)rectForPage:(int)pageNum
{
return NSMakeRect (0, (pageNum - 1) * pageHeight, pageWidth,
pageHeight);
}


@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Custom printing problems (From: Jonathan Jackel <email@hidden>)
 >Re: Custom printing problems (From: Buddy Kurz <email@hidden>)

  • Prev by Date: Re: Editing the text in an NSCell
  • Next by Date: Re: unsharp mask filter?
  • Previous by thread: Re: Custom printing problems
  • Next by thread: Problem with com.apple.systempreferences.plist
  • Index(es):
    • Date
    • Thread