如何在 PDF 中输出欧元符号

发布于 2024-12-06 02:50:51 字数 766 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆消瘦 2024-12-13 02:50:51

那是因为 MacRomanEncoding 默认情况下不包含欧元符号,请参阅“PDF 参考 1.7”(第 D.1 节拉丁字符集和编码)中的引用:

  1. 在 PDF 1.3 中,欧元字符已添加到 Adob​​e 标准拉丁字符集中。它在 WinAnsiEncoding 中编码为 200,在 PDFDocEncoding 中编码为 240,分配以前未使用的代码。 Apple 将代码 333 的 Mac OS 拉丁文本编码从货币字符更改为欧元字符。然而,这种不兼容的变化并没有反映在PDF的MacRomanEncoding中,它继续将代码333映射到货币。如果需要欧元字符,可以使用编码字典来指定与 MacRomanEncoding 的单一差异。

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):

  1. In PDF 1.3, the euro character was added to the Adobe standard Latin character set. It is encoded as 200 in WinAnsiEncoding and 240 in PDFDocEncoding, assigning codes that were previously unused. Apple changed the Mac OS Latin-text encoding for code 333 from the currency character to the euro character. However, this incompatible change has not been reflected in PDF’s MacRomanEncoding, which continues to map code 333 to currency. If the euro character is desired, an encoding dictionary can be used to specify this single difference from MacRomanEncoding.
离鸿 2024-12-13 02:50:51

您应该能够使用 CGContextShowGlyphsAtPoint 来绘制欧元符号。问题是您需要向该函数传递一个 CGGlyph 作为输入,而不是一个 Unicode 字符。此外,从 Unicode 字符到 CGGlyphs 的映射依赖于字体,而且通常很重要。 (有时它是一个简单的偏移量,您可以根据试验和错误来猜测。)

看起来 Core Text 有一个函数 CTFontGetGlyphsForCharacters 可以执行转换;不过,我从未在实践中使用过它:

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

另外:如果您使用CGContextShowGlyphsAtPoint 您需要将对 CGContextSelectFont 的调用替换为 CGContextSetFontCGContextSetFontSize

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文