@@ -36,19 +36,15 @@ struct AttributedStringRenderer {
36
36
case decimal( Int )
37
37
}
38
38
39
- let baseURL : URL ?
40
- let baseWritingDirection : NSWritingDirection
41
- let alignment : NSTextAlignment
42
- let sizeCategory : ContentSizeCategory
43
- let style : MarkdownStyle
39
+ let environment : Environment
44
40
45
41
func renderDocument( _ document: Document ) -> NSAttributedString {
46
42
return renderBlocks (
47
43
document. blocks,
48
44
state: . init(
49
- font: style. font,
50
- foregroundColor: style. foregroundColor,
51
- paragraphSpacing: style. measurements. paragraphSpacing
45
+ font: environment . style. font,
46
+ foregroundColor: environment . style. foregroundColor,
47
+ paragraphSpacing: environment . style. measurements. paragraphSpacing
52
48
)
53
49
)
54
50
}
@@ -101,8 +97,8 @@ extension AttributedStringRenderer {
101
97
102
98
var state = state
103
99
state. font = state. font. italic ( )
104
- state. headIndent += style. measurements. headIndentStep
105
- state. tailIndent += style. measurements. tailIndentStep
100
+ state. headIndent += environment . style. measurements. headIndentStep
101
+ state. tailIndent += environment . style. measurements. tailIndentStep
106
102
state. tabStops. append (
107
103
. init( textAlignment: . natural, location: state. headIndent)
108
104
)
@@ -129,13 +125,14 @@ extension AttributedStringRenderer {
129
125
let result = NSMutableAttributedString ( )
130
126
131
127
var itemState = state
132
- itemState. paragraphSpacing = bulletList. tight ? 0 : style. measurements. paragraphSpacing
133
- itemState. headIndent += style. measurements. headIndentStep
128
+ itemState. paragraphSpacing =
129
+ bulletList. tight ? 0 : environment. style. measurements. paragraphSpacing
130
+ itemState. headIndent += environment. style. measurements. headIndentStep
134
131
itemState. tabStops. append (
135
132
contentsOf: [
136
133
. init(
137
- textAlignment: . trailing( baseWritingDirection) ,
138
- location: itemState. headIndent - style. measurements. listMarkerSpacing
134
+ textAlignment: . trailing( environment . baseWritingDirection) ,
135
+ location: itemState. headIndent - environment . style. measurements. listMarkerSpacing
139
136
) ,
140
137
. init( textAlignment: . natural, location: itemState. headIndent) ,
141
138
]
@@ -172,21 +169,24 @@ extension AttributedStringRenderer {
172
169
// as the head indent step if higher than the style's head indent step.
173
170
let highestNumber = orderedList. start + orderedList. items. count - 1
174
171
let headIndentStep = max (
175
- style. measurements. headIndentStep,
172
+ environment . style. measurements. headIndentStep,
176
173
NSAttributedString (
177
174
string: " \( highestNumber) . " ,
178
- attributes: [ . font: state. font. monospacedDigit ( ) . resolve ( sizeCategory: sizeCategory) ]
179
- ) . em ( ) + style. measurements. listMarkerSpacing
175
+ attributes: [
176
+ . font: state. font. monospacedDigit ( ) . resolve ( sizeCategory: environment. sizeCategory)
177
+ ]
178
+ ) . em ( ) + environment. style. measurements. listMarkerSpacing
180
179
)
181
180
182
181
var itemState = state
183
- itemState. paragraphSpacing = orderedList. tight ? 0 : style. measurements. paragraphSpacing
182
+ itemState. paragraphSpacing =
183
+ orderedList. tight ? 0 : environment. style. measurements. paragraphSpacing
184
184
itemState. headIndent += headIndentStep
185
185
itemState. tabStops. append (
186
186
contentsOf: [
187
187
. init(
188
- textAlignment: . trailing( baseWritingDirection) ,
189
- location: itemState. headIndent - style. measurements. listMarkerSpacing
188
+ textAlignment: . trailing( environment . baseWritingDirection) ,
189
+ location: itemState. headIndent - environment . style. measurements. listMarkerSpacing
190
190
) ,
191
191
. init( textAlignment: . natural, location: itemState. headIndent) ,
192
192
]
@@ -258,8 +258,8 @@ extension AttributedStringRenderer {
258
258
state: State
259
259
) -> NSAttributedString {
260
260
var state = state
261
- state. font = state. font. scale ( style. measurements. codeFontScale) . monospaced ( )
262
- state. headIndent += style. measurements. headIndentStep
261
+ state. font = state. font. scale ( environment . style. measurements. codeFontScale) . monospaced ( )
262
+ state. headIndent += environment . style. measurements. headIndentStep
263
263
state. tabStops. append (
264
264
. init( textAlignment: . natural, location: state. headIndent)
265
265
)
@@ -313,14 +313,14 @@ extension AttributedStringRenderer {
313
313
314
314
var inlineState = state
315
315
inlineState. font = inlineState. font. bold ( ) . scale (
316
- style. measurements. headingScales [ heading. level - 1 ]
316
+ environment . style. measurements. headingScales [ heading. level - 1 ]
317
317
)
318
318
319
319
result. append ( renderInlines ( heading. text, state: inlineState) )
320
320
321
321
// The paragraph spacing is relative to the parent font
322
322
var paragraphState = state
323
- paragraphState. paragraphSpacing = style. measurements. headingSpacing
323
+ paragraphState. paragraphSpacing = environment . style. measurements. headingSpacing
324
324
325
325
result. addAttribute (
326
326
. paragraphStyle,
@@ -342,7 +342,7 @@ extension AttributedStringRenderer {
342
342
. init(
343
343
string: . nbsp,
344
344
attributes: [
345
- . font: state. font. resolve ( sizeCategory: sizeCategory) ,
345
+ . font: state. font. resolve ( sizeCategory: environment . sizeCategory) ,
346
346
. strikethroughStyle: NSUnderlineStyle . single. rawValue,
347
347
. strikethroughColor: PlatformColor . separator,
348
348
]
@@ -425,7 +425,7 @@ extension AttributedStringRenderer {
425
425
NSAttributedString (
426
426
string: text,
427
427
attributes: [
428
- . font: state. font. resolve ( sizeCategory: sizeCategory) ,
428
+ . font: state. font. resolve ( sizeCategory: environment . sizeCategory) ,
429
429
. foregroundColor: PlatformColor ( state. foregroundColor) ,
430
430
]
431
431
)
@@ -441,7 +441,7 @@ extension AttributedStringRenderer {
441
441
442
442
private func renderInlineCode( _ inlineCode: InlineCode , state: State ) -> NSAttributedString {
443
443
var state = state
444
- state. font = state. font. scale ( style. measurements. codeFontScale) . monospaced ( )
444
+ state. font = state. font. scale ( environment . style. measurements. codeFontScale) . monospaced ( )
445
445
return renderText ( inlineCode. code, state: state)
446
446
}
447
447
@@ -466,7 +466,7 @@ extension AttributedStringRenderer {
466
466
let absoluteURL =
467
467
link. url
468
468
. map ( \. relativeString)
469
- . flatMap { URL ( string: $0, relativeTo: baseURL) }
469
+ . flatMap { URL ( string: $0, relativeTo: environment . baseURL) }
470
470
. map ( \. absoluteURL)
471
471
if let url = absoluteURL {
472
472
result. addAttribute ( . link, value: url, range: NSRange ( 0 ..< result. length) )
@@ -483,19 +483,20 @@ extension AttributedStringRenderer {
483
483
private func renderImage( _ image: CommonMark . Image , state: State ) -> NSAttributedString {
484
484
image. url
485
485
. map ( \. relativeString)
486
- . flatMap { URL ( string: $0, relativeTo: baseURL) }
486
+ . flatMap { URL ( string: $0, relativeTo: environment . baseURL) }
487
487
. map ( \. absoluteURL)
488
488
. map {
489
489
NSAttributedString ( markdownImageURL: $0)
490
490
} ?? NSAttributedString ( )
491
491
}
492
492
493
493
private func paragraphStyle( state: State ) -> NSParagraphStyle {
494
- let pointSize = state. font. resolve ( sizeCategory: sizeCategory) . pointSize
494
+ let pointSize = state. font. resolve ( sizeCategory: environment . sizeCategory) . pointSize
495
495
let result = NSMutableParagraphStyle ( )
496
496
result. setParagraphStyle ( . default)
497
- result. baseWritingDirection = baseWritingDirection
498
- result. alignment = alignment
497
+ result. baseWritingDirection = environment. baseWritingDirection
498
+ result. alignment = environment. alignment
499
+ result. lineSpacing = environment. lineSpacing
499
500
result. paragraphSpacing = round ( pointSize * state. paragraphSpacing)
500
501
result. headIndent = round ( pointSize * state. headIndent)
501
502
result. tailIndent = round ( pointSize * state. tailIndent)
0 commit comments