From a9d0de14af3bda7d724e467403bfc4af36204bf4 Mon Sep 17 00:00:00 2001 From: Elias Aebi Date: Wed, 6 Nov 2024 15:19:02 +0100 Subject: [PATCH] macos: clean up --- gral_macos.m | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/gral_macos.m b/gral_macos.m index ac4b0a4..8673ce4 100644 --- a/gral_macos.m +++ b/gral_macos.m @@ -247,11 +247,10 @@ void gral_draw_context_draw_image(struct gral_draw_context *draw_context, struct void gral_draw_context_draw_text(struct gral_draw_context *draw_context, struct gral_text *text, float x, float y, float red, float green, float blue, float alpha) { CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)text); - CGContextTranslateCTM((CGContextRef)draw_context, x, y); CGContextSetTextMatrix((CGContextRef)draw_context, CGAffineTransformMakeScale(1.0f, -1.0f)); - CFArrayRef glyphRuns = CTLineGetGlyphRuns(line); - for (int i = 0; i < CFArrayGetCount(glyphRuns); i++) { - CTRunRef run = CFArrayGetValueAtIndex(glyphRuns, i); + CFArrayRef glyph_runs = CTLineGetGlyphRuns(line); + for (int i = 0; i < CFArrayGetCount(glyph_runs); i++) { + CTRunRef run = CFArrayGetValueAtIndex(glyph_runs, i); CGPoint const *positions = CTRunGetPositionsPtr(run); CGGlyph const *glyphs = CTRunGetGlyphsPtr(run); int count = CTRunGetGlyphCount(run); @@ -264,33 +263,31 @@ void gral_draw_context_draw_text(struct gral_draw_context *draw_context, struct else { CGContextSetRGBFillColor((CGContextRef)draw_context, red, green, blue, alpha); } + CGContextSetTextPosition((CGContextRef)draw_context, x, y); CTFontDrawGlyphs(font, glyphs, positions, count, (CGContextRef)draw_context); } - CGContextTranslateCTM((CGContextRef)draw_context, -x, -y); CFRelease(line); } void gral_draw_context_add_text(struct gral_draw_context *draw_context, struct gral_text *text, float x, float y) { CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)text); - CGContextTranslateCTM((CGContextRef)draw_context, x, y); CGContextSetTextMatrix((CGContextRef)draw_context, CGAffineTransformMakeScale(1.0f, -1.0f)); - CFArrayRef glyphRuns = CTLineGetGlyphRuns(line); - for (int i = 0; i < CFArrayGetCount(glyphRuns); i++) { - CTRunRef run = CFArrayGetValueAtIndex(glyphRuns, i); + CFArrayRef glyph_runs = CTLineGetGlyphRuns(line); + for (int i = 0; i < CFArrayGetCount(glyph_runs); i++) { + CTRunRef run = CFArrayGetValueAtIndex(glyph_runs, i); CGPoint const *positions = CTRunGetPositionsPtr(run); CGGlyph const *glyphs = CTRunGetGlyphsPtr(run); int count = CTRunGetGlyphCount(run); CFDictionaryRef attributes = CTRunGetAttributes(run); CTFontRef font = CFDictionaryGetValue(attributes, kCTFontAttributeName); for (int j = 0; j < count; j++) { - CGContextSetTextPosition((CGContextRef)draw_context, positions[j].x, positions[j].y); + CGContextSetTextPosition((CGContextRef)draw_context, x + positions[j].x, y + positions[j].y); CGAffineTransform matrix = CGContextGetTextMatrix((CGContextRef)draw_context); CGPathRef path = CTFontCreatePathForGlyph(font, glyphs[j], &matrix); CGContextAddPath((CGContextRef)draw_context, path); CGPathRelease(path); } } - CGContextTranslateCTM((CGContextRef)draw_context, -x, -y); CFRelease(line); }