总结
我正在为 iPhone 开发一个计算器应用程序,并希望将数学表达式绘制到 UIView,与 LaTeX 外观相同。
因此,我使用 cmr10.ttf(LaTeX 默认值)作为绘图的字体,但未显示某些字符。
测试代码和详细信息
这是我的测试代码:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
const int len = 4;
Byte cstr[len];
cstr[0] = 0x30; // 0
cstr[1] = 0x00; // Capital Gamma
cstr[2] = 0x41; // A
cstr[3] = 0x61; // a
NSString *str = [[NSString alloc] initWithBytes:(const void*)cstr length:len encoding:NSASCIIStringEncoding];
[str drawAtPoint:CGPointMake(10, 0) withFont:[UIFont fontWithName:@"cmr10" size:40]];
}
我原本希望在 UIView 中显示“0 Γ Aa”,但实际上显示了“0Aa”而没有大写的 gamma。
根据 CMR10 代码表(见下文),0x00 表示大写的 gamma。但是在 ASCII 表中,0x00 表示 NUL 控制字符。这可能就是为什么没有显示大写的 gamma 字符。
这里是 CMR10 的代码表。字母等一般字符与 ASCII 表的代码相同,但其他字符不同。
(来自http://www.tug.org/texlive//devsrc/Master/texmf-dist/doc/latex/base/encguide.pdf的第 18 页)
问题
所以我想知道的是如何绘制一个字符,其中字符代码与 ASCII 中的控制字符相同。
附加信息
我在 BaKoMa 字体包中使用 cmr10.ttf。
我开发这个计算器应用程序在 Xcode 4.6.1 和 iOS5 或更高的设备。
我找到了一种通过使用CGContextShowGlyphsAtPoint
和CGFontGetGlyphWithGlyphName
绘制具有控制字符代码的字符的方法。
例子:
CGContextRef context = UIGraphicsGetCurrentContext();
if (context) {
CGFontRef font = CGFontCreateWithFontName(CFSTR("cmr10"));
CGContextSetFont(context, font);
CGContextSetFontSize(context, 40);
CGAffineTransform transform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, transform);
const int len = 4;
CGGlyph glyphs[len];
glyphs[0] = CGFontGetGlyphWithGlyphName(font, CFSTR("Gamma"));
glyphs[1] = CGFontGetGlyphWithGlyphName(font, CFSTR("Upsilon"));
glyphs[2] = CGFontGetGlyphWithGlyphName(font, CFSTR("Theta"));
glyphs[3] = CGFontGetGlyphWithGlyphName(font, CFSTR("fl"));
CGContextShowGlyphsAtPoint(context, 0, 50, glyphs, len);
CGFontRelease(font);
}
CGFontGetGlyphWithGlyphName
的第二个参数中的“Gamma”是名为“post”的字形名称(请参阅http://scripts.sil.org/cms/scripts/page.php?item_id=IWS-Chapter08#05931f9d)。它在 cmr10.ttf 文件中定义。
我使用 TTFEdit 找到字形名称。
启动 TTFEdit。
文件-& gt;打开。然后选择一个 TTF 文件并单击打开。
选择glyf选项卡。
找到一个字符,然后将鼠标悬停在它并保持几秒钟。
字形名称将显示为提示。
drawAtPoint:
应该透明地处理所有编码的东西,所以我希望以下工作:
NSString *str = @"0ΓAa";
[str drawAtPoint:CGPointMake(10, 0) withFont:[UIFont fontWithName:@"cmr10" size:40]];
更新:我现在已经下载了字体并测试了代码,它确实可以正常工作。
更新 2:它不起作用,但是我已经使用“TTFdump”工具(从Microsoft Typography tools页面)检查了“cmr10.ttf”字体,发现以下内容:
该字体包含一个“cmap”表,其平台 ID = 3,编码 ID = 1。根据http://www.microsoft.com/typography/otspec/cmap.htm,这应该是从 Unicode 到字形 id 的映射。但事实并非如此。例如,Unicode U + 00A1 映射到字形 id 19,即“Gamma”字形。但是“Gamma”的真正 Unicode 是 U + 0393。
所以这个
// 00A1 = Gamma, 00A8 = Upsilon, 00A3 = Theta, 00B0 = fl.
NSString *str = @"\u00A1\u00A8\u00A3\u00B0";
[str drawAtPoint:CGPointMake(10, 10) withFont:[UIFont fontWithName:@"cmr10" size:40]];
实际上显示 cmr10 字体中的字符!
但是我没有发现这个奇怪的编码来自哪里。所以这更多的是理论上的兴趣,并且使用CGFontGetGlyphWithGlyphName
作为 Daiki 的答案是更好的解决方案。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(28条)