Created
April 19, 2009 16:00
-
-
Save takuma104/98113 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)drawRect:(CGRect)rect { | |
CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
// CGFontRef font = CGFontCreateWithFontName((CFStringRef)[[self font] fontName]); | |
CGFontRef font = CGFontCreateWithFontName((CFStringRef)@"HiraKakuProN-W3"); | |
// CGFontRef font = CGFontCreateWithFontName((CFStringRef)@"Helvetica"); | |
CGContextSetFont(ctx, font); | |
CGContextSetFontSize(ctx, 14); | |
// Transform text characters to unicode glyphs. | |
CFAbsoluteTime t1 = CFAbsoluteTimeGetCurrent(); | |
NSInteger length = [[self text] length]; | |
unichar chars[100]; | |
CGGlyph glyphs[100]; | |
[[self text] getCharacters:chars range:NSMakeRange(0, length)]; | |
CGFontGetGlyphsForUnichars(font, chars, glyphs, length); | |
NSLog(@"%d,%d,%d", glyphs[0], glyphs[1], glyphs[2]); | |
CGContextSetTextDrawingMode(ctx, kCGTextInvisible); | |
// Measure text dimensions. | |
CFAbsoluteTime t2 = CFAbsoluteTimeGetCurrent(); | |
CGPoint textEnd; | |
for (int i = 0; i < 100; i++) { | |
CGContextSetTextPosition(ctx, 0, 0); | |
CGContextShowGlyphs(ctx, glyphs, length); | |
textEnd = CGContextGetTextPosition(ctx); | |
} | |
CFAbsoluteTime t3 = CFAbsoluteTimeGetCurrent(); | |
for (int i = 0; i < 100; i++) { | |
CGPoint pos = CGContextGetTextPosition(ctx); | |
for (int t = 0; t < length; t++) { | |
CGContextShowGlyphsAtPoint(ctx, pos.x, 0, &glyphs[t], 1); | |
pos = CGContextGetTextPosition(ctx); | |
} | |
} | |
CFAbsoluteTime t4 = CFAbsoluteTimeGetCurrent(); | |
CGContextSetTextMatrix(ctx, CGAffineTransformMakeScale(1, -1)); | |
// CGContextSaveGState(ctx); | |
CGContextSetTextDrawingMode(ctx, kCGTextFill); | |
CGContextSetFillColorWithColor(ctx, [[self shadowColor] CGColor]); | |
// CGContextSetShadowWithColor(ctx, [self shadowOffset], 0, [[self shadowColor] CGColor]); | |
CGContextShowGlyphsAtPoint(ctx, 0, 100, glyphs, length); | |
// CGContextRestoreGState(ctx); | |
CGPoint pos = {0,0}; | |
for (int i = 0; i < length; i++) { | |
CGContextShowGlyphsAtPoint(ctx, pos.x, 120, &glyphs[i], 1); | |
pos = CGContextGetTextPosition(ctx); | |
} | |
CFAbsoluteTime t5 = CFAbsoluteTimeGetCurrent(); | |
NSLog(@"toglyph:%3.4fms measure1:%3.4fms measure2:%3.4fms draw:%3.4fms", (t2-t1)*1000.0, (t3-t2)*1000.0, (t4-t3)*1000.0, (t5-t4)*1000.0); | |
[self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:1.2]; | |
CFRelease(font); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment