iphone - 如何在iOS中绘制LaTeX字体(cmr10.ttf)的特殊字符?

标签 iphone ios nsstring latex special-characters

总结

我正在为 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表编码相同,其他不同。

enter image description here

(来自 http://www.tug.org/texlive//devsrc/Master/texmf-dist/doc/latex/base/encguide.pdf 的第 18 页)

问题

所以我想知道如何在ASCII中绘制一个字符编码与控制字符相同的字符。

附加信息

  • 我使用了 BaKoMa 字体包中的 cmr10.ttf。
  • 我正在 Xcode 4.6.1 和 iOS5 或更高版本的设备上开发这个计算器应用。

最佳答案

我找到了一种使用 CGContextShowGlyphsAtPointCGFontGetGlyphWithGlyphName 来绘制具有控制字符代码的字符的方法。

例子:

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 查找字形名称。

  1. 启动 TTFEdit。
  2. 文件 -> 打开。然后选择一个 TTF 文件并单击“打开”。
  3. 选择glyf 标签。
  4. 找到一个角色,然后将鼠标悬停在上面并按住几秒钟。
  5. 字形名称将显示为提示。

关于iphone - 如何在iOS中绘制LaTeX字体(cmr10.ttf)的特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15854417/

相关文章:

ios - 通过 NSPredicate 过滤数组时出现 NSUnknownKeyException

ios - 在团队中将应用程序提交到 iTunes connect

iphone - 下载并首次启动 iPhone 应用程序后立即预填充用户数据

iphone - UITableViewCell 上标和下标

python - 在 Objective-C NSMutableArray 中等效于 Python 的 `dict.get(' key', None)`?

ios - 在 Swift 中使用自定义函数调用的 snackbar

ios - Xcode 因尝试在首选项下开设帐户而死亡,开发证书问题?

iphone - 在 Settings.app 中隐藏设置包

ios - 来自 NSString 的子字符串

iphone - 将 NSString 中的 ""字符替换为 "\"(以创建 Unix 路径)