Skip to content

Commit

Permalink
macos: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Nov 6, 2024
1 parent ab8e894 commit f496169
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions gral_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down

0 comments on commit f496169

Please sign in to comment.