Skip to content

Instantly share code, notes, and snippets.

@nst
Last active August 29, 2015 14:05
Show Gist options
  • Save nst/417d937d45524af11cf7 to your computer and use it in GitHub Desktop.
Save nst/417d937d45524af11cf7 to your computer and use it in GitHub Desktop.
//
// main.m
// Unicode
//
// Created by Nicolas Seriot on 26/07/14.
// Copyright (c) 2014 Nicolas Seriot. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
void drawLastResortGlyphsWithPDF(BOOL usePDF) {
CGContextRef pdfContext = nil;
NSImage *image = nil;
NSString *path = nil;
// create file or context
CGFloat width = 2000;
CGFloat height = 2300;
if(usePDF) {
path = @"/tmp/lastresort.pdf";
NSURL *fileURL = [NSURL fileURLWithPath:path];
CGRect mediaBox = CGRectMake(0, 0, width, height);
pdfContext = CGPDFContextCreateWithURL((CFURLRef)fileURL, &mediaBox, NULL);
CGPDFContextBeginPage(pdfContext, NULL);
NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:pdfContext flipped:NO];
[NSGraphicsContext setCurrentContext:nsContext];
} else {
path = @"/tmp/lastresort.png";
NSSize imageSize = NSMakeSize(width, height);
image = [[NSImage alloc] initWithSize:imageSize];
[image setBackgroundColor:[NSColor whiteColor]];
[image lockFocus];
}
/**/
NSFont *font = [NSFont fontWithName:@"LastResort" size:96.0];
NSFont *nameFont = [NSFont fontWithName:@"Monaco" size:12.0];
for(unsigned int i = 0; i < 0x100; i++) {
NSLog(@"-- 0x%02x", i);
CGFloat x = 50 + 120 * (i % 16);
CGFloat y = height - 130 - (i / 16) * 140;
NSPoint p = NSMakePoint(x, y);
NSGlyph glyph = (NSGlyph)i;
NSBezierPath *bp = [NSBezierPath bezierPath];
[bp moveToPoint:p];
[bp appendBezierPathWithGlyph:glyph inFont:font];
[bp fill];
NSString *s = [NSString stringWithFormat:@"0x%02X", i];
[s drawAtPoint:NSMakePoint(x + 40, y - 40) withAttributes:@{NSFontAttributeName:nameFont}];
};
// close context and save file
if(usePDF) {
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
} else {
[image unlockFocus];
CGImageRef imageRef = [image CGImageForProposedRect:NULL
context:nil
hints:nil];
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:imageRef];
[imageRep setSize:[image size]];
NSData *pngData = [imageRep representationUsingType:NSPNGFileType properties:nil];
[pngData writeToFile:path atomically:YES];
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
drawLastResortGlyphsWithPDF(YES);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment