Memoryleak due to CTFontRef - ios

I am generating a PDF using the below code but it leads to memory leak can anybody help ?
the code is given below.
- (void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect {
NSMutableAttributedString *string = [[[NSMutableAttributedString alloc]
initWithString:textToDraw] autorelease];
// make a few words bold
CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 8.0, NULL);
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(0, [string length])];
// add some color.
if (_flag == 1) {
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor whiteColor].CGColor
range:NSMakeRange(0, [string length])];
} else {
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor blackColor].CGColor
range:NSMakeRange(0, [string length])];
}
// layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextSetRGBFillColor(currentContext, 0, 0, 0, 1.0);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
//CFRelease(stringRef);
CFRelease(framesetter);
}
I'm calling this function several time while generation the
PDF and every time this leads to memory leak.

CTFontCreateWithName follows the create-name-rule, which is if you create it, you own it and you have to release it when you're finished:
CFRelease(helveticaBold);

Related

iOS 8 Pdf output-minimum font scaling to fit frame

I'm using the code below to write text to a pdf. This is pretty standard stuff, using attributes to style the text.
Use of attributes works but I need to be able to shrink the text to fit the available frame, similar to a UILabel. There doesn't appear to be any kind of minimum font size setting, which probably makes sense as attributed text can be variable in font and sizes.
So, is there a way to measure the proposed size of the text when outputting to pdf to see if it will fit the frame, consequently allowing me to reduce my font size attributes manually?
-(void)drawText:(NSString*)text inFrame:(CGRect)frameRect withAttributes:(NSDictionary*)attributes
{
CFStringRef stringRef = (__bridge CFStringRef)text;
CFDictionaryRef attributeRef = (__bridge CFDictionaryRef)attributes;
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, attributeRef);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGFloat offset = (frameRect.origin.y*2)+frameRect.size.height;
CGContextTranslateCTM(currentContext, 0, offset);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, -offset);
CFRelease(frameRef);
CFRelease(framesetter);
CFRelease(currentText);
}

Creating multi page PDF from text content

I have a long text content and wanted to convert it in to a multipage PDF file.
I have followed the instructions provided in this tutorial http://www.raywenderlich.com/6581/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-1.
But I am struggling to make the pdf multi page.
The code I'm using can create single page PDF.
+(void)drawText
{
NSString* textToDraw = #"The sample text";
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
CGRect frameRect = CGRectMake(50, 50, 512, 1000);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);
}
+(void)drawPDF:(NSString*)fileName
{
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
[self drawText];
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
you can draw pdf using following code:
// Use Core Text to draw the text in a frame on the page.
- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange
andFramesetter:(CTFramesetterRef)framesetter
{
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Create a path object to enclose the text. Use 72 point
// margins all around the text.
CGRect frameRect = CGRectMake(72, 72, 468, 648);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
// The currentRange variable specifies only the starting point. The framesetter
// lays out as much text as will fit into the frame.
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, kDefaultPageHeight);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
// Update the current range based on what was drawn.
currentRange = CTFrameGetVisibleStringRange(frameRef);
currentRange.location += currentRange.length;
currentRange.length = 0;
CFRelease(frameRef);
return currentRange;
}
- (void)drawPageNumber:(NSInteger)pageNum
{
NSString* pageString = [NSString stringWithFormat:#"Page %ld", (long)pageNum];
UIFont* theFont = [UIFont systemFontOfSize:12];
// CGSize maxSize = CGSizeMake(kDefaultPageWidth, 72);
CGSize pageStringSize = [pageString sizeWithAttributes:
#{NSFontAttributeName:
theFont}];
// CGSize pageStringSize = [pageString sizeWithFont:theFont
// constrainedToSize:maxSize
// lineBreakMode:NSLineBreakByClipping];
CGRect stringRect = CGRectMake(((kDefaultPageWidth - pageStringSize.width) / 2.0),
720.0 + ((72.0 - pageStringSize.height) / 2.0) ,
pageStringSize.width,
pageStringSize.height);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = #{NSFontAttributeName: [UIFont systemFontOfSize:15], NSParagraphStyleAttributeName: paragraphStyle};
[pageString drawInRect:stringRect withAttributes:attributes];
// [pageString drawInRect:stringRect withFont:theFont];
}
For save pdf:
- (IBAction)savePDFFile:(id)sender
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"sampleData" ofType:#"plist"];
// get a temprorary filename for this PDF
path = NSTemporaryDirectory();
self.pdfFilePath = [path stringByAppendingPathComponent:
[NSString stringWithFormat:#"%d.pdf",
[[NSDate date]
timeIntervalSince1970] ]];
// Prepare the text using a Core Text Framesetter
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL,
(CFStringRef)textView.text, NULL);
if (currentText) {
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
if (framesetter) {
NSString* pdfFileName = self.pdfFilePath; //[NSString stringWithString:#"test.pdf"];
// Create the PDF context using the default page: currently constants at the size
// of 612 x 792.
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
CFRange currentRange = CFRangeMake(0, 0);
NSInteger currentPage = 0;
BOOL done = NO;
do {
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth,
kDefaultPageHeight), nil);
// Draw a page number at the bottom of each page
currentPage++;
[self drawPageNumber:currentPage];
// Render the current page and update the current range to
// point to the beginning of the next page.
currentRange = [self renderPage:currentPage withTextRange:
currentRange andFramesetter:framesetter];
// If we're at the end of the text, exit the loop.
if (currentRange.location == CFAttributedStringGetLength
((CFAttributedStringRef)currentText))
done = YES;
} while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
// Release the framewetter.
CFRelease(framesetter);
} else {
NSLog(#"Could not create the framesetter needed to lay out the atrributed string.");
}
// Release the attributed string.
CFRelease(currentText);
} else {
NSLog(#"Could not create the attributed string for the framesetter");
}
// Ask the user if they'd like to see the file or email it.
UIActionSheet* actionSheet = [[[UIActionSheet alloc] initWithTitle:#"Would you like to preview or email this PDF?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Preview", #"Email", nil] autorelease];
[actionSheet showInView:self.view];
}
here it is full demo please check: http://ge.tt/1eW8iJt1/v/0

Draw Angled/Rotated MultiLine Text - Core Text + Core Graphics

After going through several blogs & forums I didn't find an appropriate solution for drawing inclined/angled text using core Text on a views context.
So here is how it goes.
I have a view whose - (void)drawRect:(CGRect)rect is invoked to draw a string (multi or single line text) on screen.
CODE:
- (void)drawRect:(CGRect)rect
{
NSString *text = #"This is some text being drawn by CoreText!\nAnd some more text on another line!";
//Core Text (Create Attributed String)
UIColor *textColor = [UIColor blackColor];
CGColorRef color = textColor.CGColor;
CTFontRef font = CTFontCreateWithName((CFStringRef) #"HelveticaNeue", 20.0, NULL);
CTTextAlignment theAlignment = kCTTextAlignmentLeft;
CFIndex theNumberOfSettings = 1;
CTParagraphStyleSetting theSettings[1] =
{
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),
&theAlignment }
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
CFBridgingRelease(font), (NSString *)kCTFontAttributeName,
color, (NSString *)kCTForegroundColorAttributeName,
paragraphStyle, (NSString *) kCTParagraphStyleAttributeName,
nil];
NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:text attributes:attributesDict];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)stringToDraw);
//Create Frame
CGMutablePathRef path = CGPathCreateMutable();
CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
//First translate your image View according to transform
transform = CGAffineTransformTranslate(transform, 0, - self.bounds.size.height);
// Then whenever you want any point according to UIKit related coordinates apply this transformation on the point or rect.
CGRect frameText = CGRectMake(60, 100, 200, 200);
CGRect newRectForUIKit = CGRectApplyAffineTransform(frameText, transform);
CGPathAddRect(path, NULL, newRectForUIKit);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
CGContextTranslateCTM(ctx, 0, ([self bounds]).size.height );
CGContextScaleCTM(ctx, 1.0, -1.0);
//Draw Frame
CTFrameDraw(frame, ctx);
//Release all retained objects
CFRelease(path);
}
Output:
Apart from drawing text I want to add an angle to the entire drawn text. Something like this(Required output)
So how do I add an rotation angle to the drawn text in core text?
Note: 1)A single context can have multiple drawn text objects with their respective angles as shown below
I hope my question is clear.
Apply the rotation to the context before drawing the CTFrameRef into it.
Edit :
If you want multiple angles, you need to save/restore the graphics states, each time.
Something like :
- (void)drawRect:(CGRect)rect
{
NSString *text = #"This is some text being drawn by CoreText!\nAnd some more text on another line!";
//Core Text (Create Attributed String)
UIColor *textColor = [UIColor blackColor];
CGColorRef color = textColor.CGColor;
CTFontRef font = CTFontCreateWithName((CFStringRef) #"HelveticaNeue", 20.0, NULL);
CTTextAlignment theAlignment = kCTTextAlignmentLeft;
CFIndex theNumberOfSettings = 1;
CTParagraphStyleSetting theSettings[1] =
{
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),
&theAlignment }
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
CFBridgingRelease(font), (NSString *)kCTFontAttributeName,
color, (NSString *)kCTForegroundColorAttributeName,
paragraphStyle, (NSString *) kCTParagraphStyleAttributeName,
nil];
NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:text attributes:attributesDict];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)stringToDraw);
//Create Frame
CGMutablePathRef path = CGPathCreateMutable();
CGRect frameText = CGRectMake(60, 100, 200, 200);
CGPathAddRect(path, NULL, frameText);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx); /* save Graphic State for context rotation */
// transform (rotate) context
CGAffineTransform transform = CGAffineTransformMakeTranslation(self.bounds.size.width / 2.f, self.bounds.size.height / 2.f);
CGFloat rotation = -M_PI / 2.f;
transform = CGAffineTransformRotate(transform,rotation);
transform = CGAffineTransformTranslate(transform,-self.bounds.size.width / 2.f, -self.bounds.size.height / 2.f);
CGContextConcatCTM(ctx, transform);
CGContextSaveGState(ctx);
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
CGContextTranslateCTM(ctx, 0, ([self bounds]).size.height );
CGContextScaleCTM(ctx, 1.0, -1.0);
//Draw Frame
CTFrameDraw(frame, ctx);
//Release all retained objects
CFRelease(path);
CGContextRestoreGState(ctx);
CGContextRestoreGState(ctx); /* restore Graphic State for context rotation */
CGContextSaveGState(ctx); /* save Graphic States for another drawing */
/* lets draw another string with different angle */
attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
CFBridgingRelease(CTFontCreateWithName((CFStringRef) #"HelveticaNeue", 14.0, NULL)), (NSString *)kCTFontAttributeName,
[UIColor yellowColor].CGColor, (NSString *)kCTForegroundColorAttributeName,
nil];
stringToDraw = [[NSAttributedString alloc] initWithString:#"another piece of text to drawn on same context with no angle" attributes:attributesDict];
framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)stringToDraw);
path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(60, -100, 200, 200));
frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(path);
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
CGContextTranslateCTM(ctx, 0, ([self bounds]).size.height );
CGContextScaleCTM(ctx, 1.0, -1.0);
CTFrameDraw(frame, ctx);
/**
* #note don't forget to restore a previously saved GState, or this will be source of problems
*/
CGContextRestoreGState(ctx);
CFRelease(frame);
CFRelease(framesetter);
}

iOS Dynamic PDF generation, with desired Text font type and font color

I am creating dynamic PDF from my application. In some cases i want my text to be write in PDF with desired color. how can i get that?
I am using CoreText.
Here is my code to draw text in my PDF,
+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect
{
frameRect.origin.y = frameRect.origin.y + frameRect.size.height; // New line
CFStringRef stringRef = ( CFStringRef)textToDraw;
CGColorSpaceCreateWithName(stringRef);
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
//CFRelease(stringRef);
CFRelease(framesetter);
}
Any help or suggestion will be appreciated.,
Thanks in advance.
If you are using coretext, try
CTFontRef font = CTFontCreateWithName((CFStringRef)#"Helvetica", 16.0f, nil);
CFAttributedStringSetAttribute(textString,CFRangeMake(0, strLength-1), kCTFontAttributeName, font);
You can also try CGContextSetFont
NSString *fontName = #"Helvetica";
CGFontRef fontRef = CGFontCreateWithFontName((__bridge CFStringRef)fontName);
CGContextSetFont(context, fontRef);
CGContextSetFontSize(context, 30);
See my question, you will get an idea.

Generate hi resolution pdf on ios

Is there any way to generate high resolution PDFs on ios that support retina display? I made a basic PDF generator but on my iPad 3 it still looks pixelated. Thank you for all kind of advices!
Update:
In my PDF file text are smooth and beautiful. But when I use code to draw this pdf i got pixelated on retina display (drawing code is in a bottom).
PDF generation:
NSString *fileName = [self.bookManager cacheBookFileForPage:i];
UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextTranslateCTM(currentContext, 0, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// TODO: Temporary numbers
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 768 * 2, 1024 * 2), nil);
NSString *textToDraw = #"text";
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
CTFontRef ctFont= CTFontCreateWithName(CFSTR("Arial"), 20 * 2, &CGAffineTransformIdentity);
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)ctFont, (NSString *)kCTFontAttributeName,
nil];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:textToDraw
attributes:attributes];
CFRelease(ctFont);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string);
// TODO: temporary
CGRect frameRect = CGRectMake(100, 100, 800 * 2, 50 * 2);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
PDF Drawing:
// This will return pageRef from PDF
CGPDFPageRef page = [self.bookManager contentForPage:index + 1];
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGContextGetClipBoundingBox(context));
CGContextTranslateCTM(context, 0.0f, [self.view.layer bounds].size.height);
CGRect transformRect = CGRectMake(0, 0, [self.view.layer bounds].size.width, [self.view.layer bounds].size.height);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, transformRect, 0, true);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextDrawPDFPage(context, page);
I think one of the problems you have is that you are scaling what you are drawing; don't use that transform! Just create your pdf context and draw natively in that. Your fonts will look crisp as they are now drawn in full resolution, instead of half rez and then the bitmap of that font is scaled up, which of course introduces jaggies.
tl;dr: draw directly to the pdf, don't use the scaling transform on text.

Resources