Search code examples
iphonecocoapdfcurrencyeuro

How to output a Euro symbol in a PDF


I am trying to output a Euro symbol to a PDF using Core Graphics. I have the following code, which uses NSMacOSRomanStringEncoding (I had to use this to get £ and $ symbols to appear correctly), but the Euro symbol comes out as ¤

CGRect pageRect = CGRectMake(0, 0, 800, 1150);
CFMutableDataRef pdfData = (CFMutableDataRef) [NSMutableData dataWithCapacity:0];                     
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(pdfData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, nil);

CGContextSelectFont(pdfContext, "Helvetica", 15, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
const char *ctext = [@"€" cStringUsingEncoding:NSMacOSRomanStringEncoding];
CGContextShowTextAtPoint(pdfContext, 10, 10, ctext, strlen(ctext));

Solution

  • You should be able to use CGContextShowGlyphsAtPoint to draw the Euro symbol. The catch is that you need to pass that function a CGGlyph as input, rather than a Unicode character. Furthermore, the mapping from Unicode characters to CGGlyphs is font-dependent and often nontrivial. (Sometimes it's a simple offset that you can guess based on trial and error.)

    It looks like Core Text has a function CTFontGetGlyphsForCharacters which might perform the transformation; I've never used it in practice, though:

    http://developer.apple.com/library/mac/#documentation/Carbon/Reference/CTFontRef/Reference/reference.html

    Also: if you use CGContextShowGlyphsAtPoint you will need to replace the call to CGContextSelectFont with CGContextSetFont and CGContextSetFontSize instead.