@@ -36,19 +36,15 @@ struct AttributedStringRenderer {
3636 case decimal( Int )
3737 }
3838
39- let baseURL : URL ?
40- let baseWritingDirection : NSWritingDirection
41- let alignment : NSTextAlignment
42- let sizeCategory : ContentSizeCategory
43- let style : MarkdownStyle
39+ let environment : Environment
4440
4541 func renderDocument( _ document: Document ) -> NSAttributedString {
4642 return renderBlocks (
4743 document. blocks,
4844 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
5248 )
5349 )
5450 }
@@ -101,8 +97,8 @@ extension AttributedStringRenderer {
10197
10298 var state = state
10399 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
106102 state. tabStops. append (
107103 . init( textAlignment: . natural, location: state. headIndent)
108104 )
@@ -129,13 +125,14 @@ extension AttributedStringRenderer {
129125 let result = NSMutableAttributedString ( )
130126
131127 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
134131 itemState. tabStops. append (
135132 contentsOf: [
136133 . 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
139136 ) ,
140137 . init( textAlignment: . natural, location: itemState. headIndent) ,
141138 ]
@@ -172,21 +169,24 @@ extension AttributedStringRenderer {
172169 // as the head indent step if higher than the style's head indent step.
173170 let highestNumber = orderedList. start + orderedList. items. count - 1
174171 let headIndentStep = max (
175- style. measurements. headIndentStep,
172+ environment . style. measurements. headIndentStep,
176173 NSAttributedString (
177174 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
180179 )
181180
182181 var itemState = state
183- itemState. paragraphSpacing = orderedList. tight ? 0 : style. measurements. paragraphSpacing
182+ itemState. paragraphSpacing =
183+ orderedList. tight ? 0 : environment. style. measurements. paragraphSpacing
184184 itemState. headIndent += headIndentStep
185185 itemState. tabStops. append (
186186 contentsOf: [
187187 . 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
190190 ) ,
191191 . init( textAlignment: . natural, location: itemState. headIndent) ,
192192 ]
@@ -258,8 +258,8 @@ extension AttributedStringRenderer {
258258 state: State
259259 ) -> NSAttributedString {
260260 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
263263 state. tabStops. append (
264264 . init( textAlignment: . natural, location: state. headIndent)
265265 )
@@ -313,14 +313,14 @@ extension AttributedStringRenderer {
313313
314314 var inlineState = state
315315 inlineState. font = inlineState. font. bold ( ) . scale (
316- style. measurements. headingScales [ heading. level - 1 ]
316+ environment . style. measurements. headingScales [ heading. level - 1 ]
317317 )
318318
319319 result. append ( renderInlines ( heading. text, state: inlineState) )
320320
321321 // The paragraph spacing is relative to the parent font
322322 var paragraphState = state
323- paragraphState. paragraphSpacing = style. measurements. headingSpacing
323+ paragraphState. paragraphSpacing = environment . style. measurements. headingSpacing
324324
325325 result. addAttribute (
326326 . paragraphStyle,
@@ -342,7 +342,7 @@ extension AttributedStringRenderer {
342342 . init(
343343 string: . nbsp,
344344 attributes: [
345- . font: state. font. resolve ( sizeCategory: sizeCategory) ,
345+ . font: state. font. resolve ( sizeCategory: environment . sizeCategory) ,
346346 . strikethroughStyle: NSUnderlineStyle . single. rawValue,
347347 . strikethroughColor: PlatformColor . separator,
348348 ]
@@ -425,7 +425,7 @@ extension AttributedStringRenderer {
425425 NSAttributedString (
426426 string: text,
427427 attributes: [
428- . font: state. font. resolve ( sizeCategory: sizeCategory) ,
428+ . font: state. font. resolve ( sizeCategory: environment . sizeCategory) ,
429429 . foregroundColor: PlatformColor ( state. foregroundColor) ,
430430 ]
431431 )
@@ -441,7 +441,7 @@ extension AttributedStringRenderer {
441441
442442 private func renderInlineCode( _ inlineCode: InlineCode , state: State ) -> NSAttributedString {
443443 var state = state
444- state. font = state. font. scale ( style. measurements. codeFontScale) . monospaced ( )
444+ state. font = state. font. scale ( environment . style. measurements. codeFontScale) . monospaced ( )
445445 return renderText ( inlineCode. code, state: state)
446446 }
447447
@@ -466,7 +466,7 @@ extension AttributedStringRenderer {
466466 let absoluteURL =
467467 link. url
468468 . map ( \. relativeString)
469- . flatMap { URL ( string: $0, relativeTo: baseURL) }
469+ . flatMap { URL ( string: $0, relativeTo: environment . baseURL) }
470470 . map ( \. absoluteURL)
471471 if let url = absoluteURL {
472472 result. addAttribute ( . link, value: url, range: NSRange ( 0 ..< result. length) )
@@ -483,19 +483,20 @@ extension AttributedStringRenderer {
483483 private func renderImage( _ image: CommonMark . Image , state: State ) -> NSAttributedString {
484484 image. url
485485 . map ( \. relativeString)
486- . flatMap { URL ( string: $0, relativeTo: baseURL) }
486+ . flatMap { URL ( string: $0, relativeTo: environment . baseURL) }
487487 . map ( \. absoluteURL)
488488 . map {
489489 NSAttributedString ( markdownImageURL: $0)
490490 } ?? NSAttributedString ( )
491491 }
492492
493493 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
495495 let result = NSMutableParagraphStyle ( )
496496 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
499500 result. paragraphSpacing = round ( pointSize * state. paragraphSpacing)
500501 result. headIndent = round ( pointSize * state. headIndent)
501502 result. tailIndent = round ( pointSize * state. tailIndent)
0 commit comments