@@ -131,6 +131,7 @@ public final class DrawingLibrary: NativeLibrary {
131
131
self . define ( Procedure ( " draw-ellipse " , drawEllipse) )
132
132
self . define ( Procedure ( " fill-ellipse " , fillEllipse) )
133
133
self . define ( Procedure ( " draw-text " , drawText) )
134
+ self . define ( Procedure ( " draw-styled-text " , drawStyledText) )
134
135
self . define ( Procedure ( " draw-html " , drawHtml) )
135
136
self . define ( Procedure ( " draw-image " , drawImage) )
136
137
self . define ( Procedure ( " draw-drawing " , drawDrawing) )
@@ -230,6 +231,7 @@ public final class DrawingLibrary: NativeLibrary {
230
231
231
232
// Utilities
232
233
self . define ( Procedure ( " text-size " , textSize) )
234
+ self . define ( Procedure ( " styled-text-size " , styledTextSize) )
233
235
self . define ( Procedure ( " html-size " , htmlSize) )
234
236
235
237
// Define constants
@@ -563,6 +565,17 @@ public final class DrawingLibrary: NativeLibrary {
563
565
return . void
564
566
}
565
567
568
+ private func drawStyledText( text: Expr ,
569
+ location: Expr ,
570
+ drawing: Expr ? ) throws -> Expr {
571
+ guard case . object( let obj) = text, let attribStr = ( obj as? StyledText ) ? . value else {
572
+ throw RuntimeError . type ( text, expected: [ StyledText . type] )
573
+ }
574
+ let loc = try self . asObjectLocation ( location)
575
+ try self . drawing ( from: drawing) . append ( . attributedText( attribStr, at: loc) )
576
+ return . void
577
+ }
578
+
566
579
private func drawHtml( text: Expr ,
567
580
location: Expr ,
568
581
drawing: Expr ? ) throws -> Expr {
@@ -1828,6 +1841,25 @@ public final class DrawingLibrary: NativeLibrary {
1828
1841
return . pair( . flonum( Double ( rect. width) ) , . flonum( Double ( rect. height) ) )
1829
1842
}
1830
1843
1844
+ private func styledTextSize( text: Expr , dimensions: Expr ? ) throws -> Expr {
1845
+ guard case . object( let obj) = text, let str = ( obj as? StyledText ) ? . value else {
1846
+ throw RuntimeError . type ( text, expected: [ StyledText . type] )
1847
+ }
1848
+ let size : CGSize
1849
+ switch dimensions {
1850
+ case . none:
1851
+ size = CGSize ( width: CGFloat . infinity, height: CGFloat . infinity)
1852
+ case . some( . pair( . flonum( let w) , . flonum( let h) ) ) :
1853
+ size = CGSize ( width: w, height: h)
1854
+ case . some( let w) :
1855
+ size = CGSize ( width: CGFloat ( try w. asDouble ( coerce: true ) ) , height: CGFloat . infinity)
1856
+ }
1857
+ let rect = str. boundingRect ( with: size,
1858
+ options: [ . usesLineFragmentOrigin, . usesFontLeading] ,
1859
+ context: nil )
1860
+ return . pair( . flonum( Double ( rect. width) ) , . flonum( Double ( rect. height) ) )
1861
+ }
1862
+
1831
1863
private func htmlSize( text: Expr , dimensions: Expr ? ) throws -> Expr {
1832
1864
let http = Data ( try text. asString ( ) . utf8)
1833
1865
let str =
0 commit comments