如何在 PDF 中输出欧元符号
我正在尝试使用 Core Graphics 将欧元符号输出到 PDF。我有以下代码,它使用 NSMacOSRomanStringEncoding (我必须使用它来使 £ 和 $ 符号正确显示),但欧元符号显示为 ¤
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));
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));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是因为
MacRomanEncoding
默认情况下不包含欧元符号,请参阅“PDF 参考 1.7”(第 D.1 节拉丁字符集和编码)中的引用:Thats because
MacRomanEncoding
doesn't contain euro symbol by default, see this quote from "PDF reference 1.7" (Section D.1 Latin Character Set and Encodings):您应该能够使用 CGContextShowGlyphsAtPoint 来绘制欧元符号。问题是您需要向该函数传递一个
CGGlyph
作为输入,而不是一个 Unicode 字符。此外,从 Unicode 字符到CGGlyphs
的映射依赖于字体,而且通常很重要。 (有时它是一个简单的偏移量,您可以根据试验和错误来猜测。)看起来 Core Text 有一个函数
CTFontGetGlyphsForCharacters
可以执行转换;不过,我从未在实践中使用过它:http://developer.apple.com/library/mac/#documentation/Carbon/Reference/CTFontRef/Reference/reference.html
另外:如果您使用
CGContextShowGlyphsAtPoint
您需要将对CGContextSelectFont
的调用替换为CGContextSetFont
和CGContextSetFontSize
。You should be able to use
CGContextShowGlyphsAtPoint
to draw the Euro symbol. The catch is that you need to pass that function aCGGlyph
as input, rather than a Unicode character. Furthermore, the mapping from Unicode characters toCGGlyphs
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 toCGContextSelectFont
withCGContextSetFont
andCGContextSetFontSize
instead.