Cleanup Platform* typealiases

This commit is contained in:
Rob Napier
2020-01-05 11:38:47 -05:00
parent b54de600de
commit 47ec12e4c8
7 changed files with 103 additions and 94 deletions

View File

@@ -9,7 +9,7 @@
import AppKit
import SwiftUI
typealias PlatformFont = NSFont
//typealias PlatformFont = NSFont
typealias PlatformColor = NSColor
@available(OSX, introduced: 10.15)

View File

@@ -162,81 +162,3 @@ struct GlyphRun {
}
}
}
struct PathTextLayoutManager {
public var text: NSAttributedString = NSAttributedString() {
didSet {
invalidateGlyphs()
}
}
public var path: CGPath = CGMutablePath() {
didSet {
invalidateLayout()
}
}
private var needsGlyphGeneration = false
public mutating func invalidateGlyphs() { needsGlyphGeneration = true }
private var needsLayout = false
public mutating func invalidateLayout() { needsLayout = true }
private var glyphRuns: [GlyphRun] = []
private mutating func updateGlyphRuns() {
let line = CTLineCreateWithAttributedString(text)
let runs = CTLineGetGlyphRuns(line) as! [CTRun]
glyphRuns = runs.map { run in
let glyphCount = CTRunGetGlyphCount(run)
let positions: [CGPoint] = Array(unsafeUninitializedCapacity: glyphCount) { (buffer, initialized) in
CTRunGetPositions(run, CFRange(), buffer.baseAddress!)
initialized = glyphCount
}
let widths: [CGFloat] = (0..<glyphCount).map {
CGFloat(CTRunGetTypographicBounds(run, CFRange(location: $0, length: 1), nil, nil, nil))
}
let glyphs = Array<CGGlyph>(unsafeUninitializedCapacity: glyphCount) { (buffer, initialized) in
CTRunGetGlyphs(run, CFRange(), buffer.baseAddress!)
initialized = glyphCount
}
let locations = zip(glyphs, zip(positions, widths))
.map { GlyphLocation(glyph: $0, position: $1.0, width: $1.1) }
.sorted { $0.anchor < $1.anchor }
return GlyphRun(run: run, locations: locations)
}
needsGlyphGeneration = false
}
private mutating func updateGlyphPositions() {
if needsGlyphGeneration { updateGlyphRuns() }
var tangents = TangentGenerator(path: path)
glyphRuns = glyphRuns.map {
var glyphRun = $0
glyphRun.updatePositions(withTangents: &tangents)
return glyphRun
}
needsLayout = false
}
public mutating func draw(in context: CGContext) {
if needsLayout {
updateGlyphPositions()
}
// FIXME: Check if flip is needed (macos)
context.textMatrix = CGAffineTransform(scaleX: 1, y: -1)
for run in glyphRuns {
run.draw(in: context)
}
}
}

View File

@@ -0,0 +1,88 @@
//
// LayoutManager.swift
//
//
// Created by Rob Napier on 1/5/20.
//
import Foundation
import CoreGraphics
import CoreText
struct PathTextLayoutManager {
public var text: NSAttributedString = NSAttributedString() {
didSet {
invalidateGlyphs()
}
}
public var path: CGPath = CGMutablePath() {
didSet {
invalidateLayout()
}
}
private var needsGlyphGeneration = false
public mutating func invalidateGlyphs() { needsGlyphGeneration = true }
private var needsLayout = false
public mutating func invalidateLayout() { needsLayout = true }
private var glyphRuns: [GlyphRun] = []
private mutating func updateGlyphRuns() {
let line = CTLineCreateWithAttributedString(text)
let runs = CTLineGetGlyphRuns(line) as! [CTRun]
glyphRuns = runs.map { run in
let glyphCount = CTRunGetGlyphCount(run)
let positions: [CGPoint] = Array(unsafeUninitializedCapacity: glyphCount) { (buffer, initialized) in
CTRunGetPositions(run, CFRange(), buffer.baseAddress!)
initialized = glyphCount
}
let widths: [CGFloat] = (0..<glyphCount).map {
CGFloat(CTRunGetTypographicBounds(run, CFRange(location: $0, length: 1), nil, nil, nil))
}
let glyphs = Array<CGGlyph>(unsafeUninitializedCapacity: glyphCount) { (buffer, initialized) in
CTRunGetGlyphs(run, CFRange(), buffer.baseAddress!)
initialized = glyphCount
}
let locations = zip(glyphs, zip(positions, widths))
.map { GlyphLocation(glyph: $0, position: $1.0, width: $1.1) }
.sorted { $0.anchor < $1.anchor }
return GlyphRun(run: run, locations: locations)
}
needsGlyphGeneration = false
}
private mutating func updateGlyphPositions() {
if needsGlyphGeneration { updateGlyphRuns() }
var tangents = TangentGenerator(path: path)
glyphRuns = glyphRuns.map {
var glyphRun = $0
glyphRun.updatePositions(withTangents: &tangents)
return glyphRun
}
needsLayout = false
}
public mutating func draw(in context: CGContext) {
if needsLayout {
updateGlyphPositions()
}
// FIXME: Check if flip is needed (macos)
context.textMatrix = CGAffineTransform(scaleX: 1, y: -1)
for run in glyphRuns {
run.draw(in: context)
}
}
}

View File

@@ -1,10 +0,0 @@
//
// PathText.swift
// CurvyText
//
// Created by Rob Napier on 12/6/19.
// Copyright © 2019 Rob Napier. All rights reserved.
//
import SwiftUI

View File

@@ -7,6 +7,15 @@
import SwiftUI
#if canImport(UIKit)
import UIKit
private typealias PlatformFont = UIFont
#elseif canImport(AppKit)
private typealias PlatformFont = NSFont
#else
#error("Unsupported platform")
#endif
@available(iOS, introduced: 13)
@available(OSX, introduced: 10.15)
struct PathText_Previews: PreviewProvider {

View File

@@ -9,7 +9,7 @@
import UIKit
import SwiftUI
typealias PlatformFont = UIFont
//typealias PlatformFont = UIFont
typealias PlatformColor = UIColor
@available(iOS 13, *)

View File

@@ -3,11 +3,11 @@ import XCTest
#if canImport(UIKit)
import UIKit
typealias PlatformFont = UIFont
typealias PlatformColor = UIColor
private typealias PlatformFont = UIFont
private typealias PlatformColor = UIColor
#elseif canImport(AppKit)
typealias PlatformFont = NSFont
typealias PlatformColor = NSColor
private typealias PlatformFont = NSFont
private typealias PlatformColor = NSColor
#else
#error("Unsupported platform")
#endif