Session 226 Core Text and Fonts
Session 226 Core Text and Fonts
Session 226
Ned Holbrook
Typographic Engineer
These are confidential sessionsplease refrain from streaming, blogging, or taking pictures
CoreText Overview
Fundamental framework for Unicode text layout and fonts
Available via:
AppKit (OS X)
Core Animation
UIKit (iOS)
WebKit
CoreText Overview
Availability
CoreText Overview
Deprecated OS X Frameworks
New Features
Line Bounds
CTLineGetTypographicBounds()
Line Bounds
CTLineGetTypographicBounds()
Line Bounds
CTLineGetBoundsWithOptions(, 0)
Line Bounds
CTLineGetBoundsWithOptions(, kCTLineBoundsUseOpticalBounds)
Line Bounds
CTLineGetBoundsWithOptions(, kCTLineBoundsUseHangingPunctuation)
Line Bounds
CTLineGetBoundsWithOptions(, kCTLineBoundsUseHangingPunctuation |
kCTLineBoundsUseOpticalBounds)
Line Bounds
CTLineGetBoundsWithOptions(, kCTLineBoundsUseGlyphPathBounds)
Line Bounds
kCTParagraphStyleSpecifierLineBoundsOptions
Baseline Alignment
Better handling of mixed scripts
Hello
Baseline Alignment
Better handling of mixed scripts
Hello
Baseline Alignment
Better handling of mixed scripts
#$ World
Baseline Alignment
Better handling of mixed scripts
#$
World
Baseline Alignment
Typographic effects
Drop Caps
Baseline Alignment
Typographic effects
rop
aps
Baseline Alignment
Basics
Baseline Alignment
Basics, cont.
Baseline Alignment
Font Fallbacks
CTFontRef font;
NSMutableAttributedString *attrString;
NSRange range;
NSDictionary *referenceInfo = @{
(id)kCTBaselineReferenceFont : (id)font
};
[attrString addAttribute:(id)kCTBaselineReferenceInfoAttributeName
value:referenceInfo range:range];
Baseline Alignment
Specifying a Baseline
CTFontRef font, hangingFont;
NSMutableAttributedString *attrString;
NSRange range, wordRange;
NSDictionary *referenceInfo = @{
(id)kCTBaselineReferenceFont : (id)font
};
[attrString addAttribute:(id)kCTBaselineReferenceInfoAttributeName
value:referenceInfo range:range];
NSRange hangingRange = NSMakeRange(wordRange.location, 1);
[attrString addAttribute:(id)kCTBaselineClassAttributeName
value:(id)kCTBaselineClassHanging range:hangingRange];
[attrString addAttribute:(id)kCTFontAttributeName,
value:(id)hangingFont range:hangingRange];
Baseline Alignment
Specifying an Offset
CTFontRef font;
NSMutableAttributedString *attrString;
NSRange range;
NSDictionary *referenceInfo = @{
(id)kCTBaselineReferenceFont : (id)font,
(id)kCTBaselineClassHanging : @5.0
};
[attrString addAttribute:(id)kCTBaselineReferenceInfoAttributeName
value:referenceInfo range:range];
CTFontCollection
Lion improvements
Mutable collections
Easier to access font family members
Toll-free bridged with NSFontCollection
Preferred way to enumerate fonts
CTFontRef font;
CTFontDescriptorRef descriptor = CTFontCopyFontDescriptor(font);
CTFontDescriptorRef pcapDescriptor =
CTFontDescriptorCreateCopyWithFeature(descriptor,
(CFNumberRef)[NSNumber numberWithInt:kLowerCaseType],
(CFNumberRef)[NSNumber numberWithInt:
kLowerCasePetiteCapsSelector]);
CTFontRef pcapFont = CTFontCreateWithFontDescriptor(pcapDescriptor,
0.0, NULL);
Bidirectional Text
kCTWritingDirectionAttributeName
Embedding or override
Advanced Topics
Vertical Text
Vertical Text
kCTVerticalFormsAttributeName = @YES
Vertical Text
kCTVerticalFormsAttributeName = @YES
Vertical Text
Rotate context
Vertical Text
Rotate context
Vertical Text
Use NSTextView or CTFramesetter if possible
Not using one of those?
Layout process is same as with horizontal text
Drawing is much, much different
Font Names
Fonts have many names
Example:
PostScript
TimesNewRomanPS-ItalicMT
Family
Full/Display
Style
Italic
Font Names
Fonts have many names
Example:
PostScript
TimesNewRomanPS-ItalicMT
Family
Full/Display
Style
Italic
Font Names
Fonts have many names
Example:
PostScript
TimesNewRomanPS-ItalicMT
Family
Full/Display
Style
Italic
Font Names
Performance
CTFontCreateWithName() is currently too lenient
Font Names
Specifying a non-PostScript name
NSDictionary *attributes = @{
(id)kCTFontFamilyNameAttribute : @"Times New Roman"
};
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes(
(CFDictionaryRef)attributes);
NSArray *matches =
(NSArray *)CTFontDescriptorCreateMatchingFontDescriptors(
descriptor, NULL);
if ([matches count] != 0) {
// at least one match found, first one would have been returned by
// CTFontDescriptorCreateMatchingFontDescriptor()
}
Fallback Fonts
Layout relies on fallback fonts
Augment system cascade using kCTFontCascadeListAttribute
Terminate fallback processing with font covering all characters
LastResort
Composite (CFR) fonts on Mountain Lion
/System/Library/DTDs/SplicedFont.dtd
Embedding Fonts
Fonts need not live in distinct files to be usable
On Mountain Lion or iOS
CGDataProviderRef provider = CGDataProviderCreateWithCFData(data);
CGFontRef cgFont = CGFontCreateWithDataProvider(provider);
CFErrorRef error = NULL;
if (CTFontManagerRegisterGraphicsFont(cgFont, &error)) {
// access font via font names
CTFontManagerUnregisterGraphicsFont(cgFont, NULL);
}
On Lion or higher
CTFontManagerCreateFontDescriptorFromData()
Emoji
Emoji
Emoji
Unification is a big problem
I Unicode
I Unicode
Emoji
Unicode 6.1 solutionvariation sequences
<U+2665>
???
<U+2665 U+FE0E>
<U+2665 U+FE0F>
Analogous to CGContextShowGlyphsAtPositions()
Use kCTForegroundColorFromContextAttributeName
Synthesizing Styles
CoreText does not synthesize font styles (bold, italic)
Clients may choose to do something for compatibility reasons
Italic: Skewed font matrix
Bold: No best approach
More Information
Jake Behrens
UI Frameworks Evangelist
[email protected]
Documentation
Mac OS X Human Interface Guidelines
http://developer.apple.com/ue
Related Sessions
Introduction to Attributed Strings for iOS
Mission
Wednesday 3:15PM
Mission
Thursday 10:15AM
Labs
Core Text Framework Lab
Essentials Lab B
Thursday 11:30AM