Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds DefaultStringAttributes #359

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Sources/Runestone/Library/DefaultStringAttributes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Foundation
import UIKit

struct DefaultStringAttributes {
private let textColor: UIColor
private let font: UIFont
private let kern: CGFloat
private let tabWidth: CGFloat
private let lineHeightMultiplier: CGFloat

init(
textColor: UIColor,
font: UIFont,
kern: CGFloat,
tabWidth: CGFloat,
lineHeightMultiplier: CGFloat = 1
) {
self.textColor = textColor
self.font = font
self.kern = kern
self.tabWidth = tabWidth
self.lineHeightMultiplier = lineHeightMultiplier
}

func apply(to attributedString: NSMutableAttributedString) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = []
paragraphStyle.defaultTabInterval = tabWidth
paragraphStyle.lineHeightMultiple = lineHeightMultiplier
let range = NSRange(location: 0, length: attributedString.length)
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: textColor,
.font: font,
.kern: kern as NSNumber,
.paragraphStyle: paragraphStyle
]
attributedString.beginEditing()
attributedString.setAttributes(attributes, range: range)
attributedString.endEditing()
}
}
4 changes: 1 addition & 3 deletions Sources/Runestone/TextView/Core/TextInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1623,9 +1623,7 @@ extension TextInputView: LineControllerStorageDelegate {
// MARK: - LineControllerDelegate
extension TextInputView: LineControllerDelegate {
func lineSyntaxHighlighter(for lineController: LineController) -> LineSyntaxHighlighter? {
let syntaxHighlighter = languageMode.createLineSyntaxHighlighter()
syntaxHighlighter.kern = kern
return syntaxHighlighter
languageMode.createLineSyntaxHighlighter()
}

func lineControllerDidInvalidateLineWidthDuringAsyncSyntaxHighlight(_ lineController: LineController) {
Expand Down
21 changes: 8 additions & 13 deletions Sources/Runestone/TextView/LineController/LineController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final class LineController {
var kern: CGFloat = 0 {
didSet {
if kern != oldValue {
syntaxHighlighter?.kern = kern
isDefaultAttributesInvalid = true
}
}
}
Expand Down Expand Up @@ -240,25 +240,20 @@ private extension LineController {
private func updateDefaultAttributesIfNecessary() {
if isDefaultAttributesInvalid {
if let input = createLineSyntaxHighlightInput() {
syntaxHighlighter?.setDefaultAttributes(on: input.attributedString)
let defaultStringAttributes = DefaultStringAttributes(
textColor: theme.textColor,
font: theme.font,
kern: kern,
tabWidth: tabWidth
)
defaultStringAttributes.apply(to: input.attributedString)
}
updateParagraphStyle()
isDefaultAttributesInvalid = false
isSyntaxHighlightingInvalid = true
isTypesetterInvalid = true
}
}

private func updateParagraphStyle() {
if let attributedString = attributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = []
paragraphStyle.defaultTabInterval = tabWidth
let range = NSRange(location: 0, length: attributedString.length)
attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
}
}

private func updateTypesetterIfNecessary() {
if isTypesetterInvalid {
lineFragmentTree.reset(rootValue: 0, rootData: LineFragmentNodeData(lineFragment: nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,8 @@ final class LineSyntaxHighlighterInput {
protocol LineSyntaxHighlighter: AnyObject {
typealias AsyncCallback = (Result<Void, Error>) -> Void
var theme: Theme { get set }
var kern: CGFloat { get set }
var canHighlight: Bool { get }
func setDefaultAttributes(on attributedString: NSMutableAttributedString)
func syntaxHighlight(_ input: LineSyntaxHighlighterInput)
func syntaxHighlight(_ input: LineSyntaxHighlighterInput, completion: @escaping AsyncCallback)
func cancel()
}

extension LineSyntaxHighlighter {
func setDefaultAttributes(on attributedString: NSMutableAttributedString) {
let entireRange = NSRange(location: 0, length: attributedString.length)
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: theme.textColor,
.font: theme.font,
.kern: kern as NSNumber
]
attributedString.beginEditing()
attributedString.setAttributes(attributes, range: entireRange)
attributedString.endEditing()
}
}
Loading