简体   繁体   English

具有灰色颜色空间的CoreText

[英]CoreText with gray color space

I am trying to use CoreText on iOS to render OpenGL textures. 我正在尝试在iOS上使用CoreText渲染OpenGL纹理。 CoreText renders in a CoreGraphics Bitmap context, which is then loaded in OpenGL using glTexImage2D . CoreText在CoreGraphics位图上下文中渲染,然后使用glTexImage2D将其加载到OpenGL中。

When I create a bitmap context using an RGB color space evertyhing works fine 当我使用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);

However, I would like to use a grayscale only color space. 但是,我只想使用灰度颜色空间。 When I do the text does not appear. 当我这样做时,文本不会出现。

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
uint8_t *data = (uint8_t *)calloc(height, width);
CGContextRef context = CGBitmapContextCreate(data, width, height, 8, width, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);

The text I am rendering is black. 我渲染的文本是黑色的。 In both cases I can draw in the context using CoreGraphics methods. 在这两种情况下,我都可以使用CoreGraphics方法在上下文中进行绘制。

I draw the text using the fllowing code: 我使用下面的代码绘制文本:

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

Is there a special setting in CoreText to make this work ? CoreText中是否有特殊设置可以使这项工作正常进行?

Thanks 谢谢

OK, i managed to reproduce the problem. 好的,我设法重现了问题。 In your NSAttributeString property dictionary, you shouldn't use a UIColor (NSColor?), but a CGColorRef. 在NSAttributeString属性字典中,不应使用UIColor(NSColor?),而应使用CGColorRef。 This way the CGContext APIs will know how to treat your color depending on the colorspace. 这样, CGContext API将知道如何根据颜色空间来处理颜色。 If you just do the following, you should be ok to go. 如果您只是执行以下操作,则应该可以。

CGColorRef col = [UIColor blackColor].CGColor;
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                          //whatever attributes you need
                                          col, kCTForegroundColorAttributeName,
                                          nil];

NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:yourText
                                                                   attributes:attributesDict];

I hope this helps, let me know if it works! 希望对您有所帮助,让我知道它是否有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM