Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Created December 9, 2017 02:50
Show Gist options
  • Save raphlinus/894044353bfab0b4806416d3aa92228f to your computer and use it in GitHub Desktop.
Save raphlinus/894044353bfab0b4806416d3aa92228f to your computer and use it in GitHub Desktop.
@IBAction func shapingToy(_: Any) {
print("shaping toy start")
//let text = "hello 😀 العالمية"
let text = "hello this is a pretty long string that's designed to see what the effect on shaping is. Probably it's not something we need to worry about"
let font = NSFont(name: "Inconsolata", size: 14)!
let attributes: [NSAttributedStringKey: AnyObject] = [
NSAttributedStringKey.font: font,
]
let attrString = NSMutableAttributedString(string: text, attributes: attributes)
// benchmark time!
let start = Date()
for _ in 0..<1000 {
let _ = CTLineCreateWithAttributedString(attrString)
}
let elapsed = Date().timeIntervalSince(start)
print("time for 1000 shaping calls: \(elapsed)")
let ctline = CTLineCreateWithAttributedString(attrString)
let runs = CTLineGetGlyphRuns(ctline) as [AnyObject] as! [CTRun]
var histo: [NSFont: Int] = [:]
for run in runs {
let count = CTRunGetGlyphCount(run)
let attributes: NSDictionary = CTRunGetAttributes(run)
let font = attributes[kCTFontAttributeName] as! CTFont
histo[font] = 1 + (histo[font] ?? 0)
let fontHash = font.hashValue
let status = CTRunGetStatus(run)
print("run \(count) glyphs font \(font) #\(fontHash) \(status)")
print("\(run)")
for i in 0..<count {
var glyph = CGGlyph()
var pos = CGPoint()
let range = CFRange(location: i, length: 1)
CTRunGetGlyphs(run, range, &glyph)
CTRunGetPositions(run, range, &pos)
var rect = CGRect()
CTFontGetBoundingRectsForGlyphs(font, .horizontal, &glyph, &rect, 1)
var adv = CGSize()
CTRunGetAdvances(run, range, &adv)
var idx = CFIndex()
CTRunGetStringIndices(run, range, &idx)
print(" glyph \(glyph) @ \(pos) \(rect) +\(adv) [\(idx)]")
}
}
print("font histogram: \(histo)")
}
/*
Copyright 2017 Google LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment