mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-09 23:41:09 -04:00
iOS: Get CoreText text rendering backend closer to matching FreeType output
This commit is contained in:
parent
224df71aac
commit
4bf0cda6e2
@ -766,7 +766,7 @@ void interop_SysFontFree(void* handle) {
|
|||||||
CFBridgingRelease(handle);
|
CFBridgingRelease(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
int interop_SysTextWidth(struct DrawTextArgs* args) {
|
static NSMutableAttributedString* GetAttributedString(struct DrawTextArgs* args, cc_bool shadow) {
|
||||||
UIFont* font = (__bridge UIFont*)args->font->handle;
|
UIFont* font = (__bridge UIFont*)args->font->handle;
|
||||||
cc_string left = args->text, part;
|
cc_string left = args->text, part;
|
||||||
char colorCode = 'f';
|
char colorCode = 'f';
|
||||||
@ -774,53 +774,54 @@ int interop_SysTextWidth(struct DrawTextArgs* args) {
|
|||||||
|
|
||||||
while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode))
|
while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode))
|
||||||
{
|
{
|
||||||
BitmapCol color = Drawer2D_GetColor(colorCode);
|
BitmapCol color = Drawer2D_GetColor(colorCode);
|
||||||
NSString* bit = ToNSString(&part);
|
if (shadow) color = GetShadowColor(color);
|
||||||
NSDictionary* attrs =
|
|
||||||
@{
|
NSString* bit = ToNSString(&part);
|
||||||
NSFontAttributeName : font,
|
NSRange range = NSMakeRange(str.length, bit.length);
|
||||||
NSForegroundColorAttributeName : ToUIColor(color, 1.0f)
|
[str.mutableString appendString:bit];
|
||||||
};
|
|
||||||
NSAttributedString* attr_bit = [[NSAttributedString alloc] initWithString:bit attributes:attrs];
|
[str addAttribute:NSFontAttributeName value:font range:range];
|
||||||
[str appendAttributedString:attr_bit];
|
[str addAttribute:NSForegroundColorAttributeName value:ToUIColor(color, 1.0f) range:range];
|
||||||
|
|
||||||
|
if (args->font->flags & FONT_FLAGS_UNDERLINE) {
|
||||||
|
NSNumber* style = [NSNumber numberWithInt:kCTUnderlineStyleSingle];
|
||||||
|
[str addAttribute:NSUnderlineStyleAttributeName value:style range:range];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
int interop_SysTextWidth(struct DrawTextArgs* args) {
|
||||||
|
NSMutableAttributedString* str = GetAttributedString(args, false);
|
||||||
|
|
||||||
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)str);
|
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)str);
|
||||||
CGRect bounds = CTLineGetImageBounds(line, NULL);
|
CGRect bounds = CTLineGetImageBounds(line, NULL);
|
||||||
|
|
||||||
CGFloat ascent, descent, leading;
|
CGFloat ascent, descent, leading;
|
||||||
double width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
|
double width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
|
||||||
|
|
||||||
|
CFRelease(line);
|
||||||
return Math_Ceil(width);
|
return Math_Ceil(width);
|
||||||
}
|
}
|
||||||
|
|
||||||
void interop_SysTextDraw(struct DrawTextArgs* args, struct Context2D* ctx, int x, int y, cc_bool shadow) {
|
void interop_SysTextDraw(struct DrawTextArgs* args, struct Context2D* ctx, int x, int y, cc_bool shadow) {
|
||||||
UIFont* font = (__bridge UIFont*)args->font->handle;
|
UIFont* font = (__bridge UIFont*)args->font->handle;
|
||||||
cc_string left = args->text, part;
|
NSMutableAttributedString* str = GetAttributedString(args, shadow);
|
||||||
char colorCode = 'f';
|
|
||||||
NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init];
|
|
||||||
|
|
||||||
while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode))
|
float X = x, Y = y;
|
||||||
{
|
if (shadow) { X += 1.3f; Y -= 1.3f; }
|
||||||
BitmapCol color = Drawer2D_GetColor(colorCode);
|
|
||||||
NSString* bit = ToNSString(&part);
|
|
||||||
NSDictionary* attrs =
|
|
||||||
@{
|
|
||||||
NSFontAttributeName : font,
|
|
||||||
NSForegroundColorAttributeName : ToUIColor(color, 1.0f)
|
|
||||||
};
|
|
||||||
NSAttributedString* attr_bit = [[NSAttributedString alloc] initWithString:bit attributes:attrs];
|
|
||||||
[str appendAttributedString:attr_bit];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (shadow) return;
|
|
||||||
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)str);
|
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)str);
|
||||||
struct Bitmap* bmp = &ctx->bmp;
|
struct Bitmap* bmp = &ctx->bmp;
|
||||||
|
|
||||||
CGContextRef cg_ctx = CGBitmapContextCreate(bmp->scan0, bmp->width, ctx->height, 8, bmp->width * 4,
|
CGContextRef cg_ctx = CGBitmapContextCreate(bmp->scan0, bmp->width, ctx->height, 8, bmp->width * 4,
|
||||||
CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrder32Host | kCGImageAlphaNoneSkipFirst);
|
CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrder32Host | kCGImageAlphaNoneSkipFirst);
|
||||||
CGContextSetTextPosition(cg_ctx, x, y);
|
CGContextSetTextPosition(cg_ctx, X, Y - font.descender);
|
||||||
CTLineDraw(line, cg_ctx);
|
CTLineDraw(line, cg_ctx);
|
||||||
CGContextRelease(cg_ctx);
|
CGContextRelease(cg_ctx);
|
||||||
|
|
||||||
|
CFRelease(line);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user