iOS で CoreText を使用して OpenGL テクスチャをレンダリングしようとしています。CoreText は CoreGraphics Bitmap コンテキストでレンダリングされ、次に を使用して OpenGL にロードされますglTexImage2D
。
RGB カラー スペースを使用してビットマップ コンテキストを作成すると、すべて正常に動作します。
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
uint8_t *data = (uint8_t *)calloc(height, 4 * width);
CGContextRef context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaNoneSkipLast);
CGColorSpaceRelease(colorSpace);
ただし、グレースケールのみの色空間を使用したいと思います。私がするとき、テキストは表示されません。
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
uint8_t *data = (uint8_t *)calloc(height, width);
CGContextRef context = CGBitmapContextCreate(data, width, height, 8, width, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
私がレンダリングしているテキストは黒です。どちらの場合も、CoreGraphics メソッドを使用してコンテキスト内で描画できます。
次のコードを使用してテキストを描画します。
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)text);
CGSize dimensions = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [text length]), NULL, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX), NULL);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, dimensions.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGRect box = CGRectMake(0, 0, dimensions.width, dimensions.height);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, box );
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [text length]), path, NULL);
CTFrameDraw(frame, context);
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);
これを機能させるための CoreText の特別な設定はありますか?
ありがとう