@@ -52,9 +52,11 @@ private static void OutputStars()
5252 DrawFatL ( ) ;
5353
5454 DrawText ( "Hello World" ) ;
55+
5556 DrawText (
5657 "Hello World Hello World Hello World Hello World Hello World Hello World Hello World" ,
57- new EllipsePolygon ( PointF . Empty , 100 ) ) ;
58+ new EllipsePolygon ( PointF . Empty , 100 ) ) ;
59+ // new RectangularPolygon(PointF.Empty, new SizeF(100, 100)));
5860 }
5961
6062 private static void DrawText ( string text )
@@ -69,21 +71,21 @@ private static void DrawText(string text)
6971
7072 private static void DrawText ( string text , IPath path )
7173 {
72- FontFamily fam = SystemFonts . Get ( "Arial" ) ;
73- var font = new Font ( fam , 30 ) ;
74+ FontFamily family = SystemFonts . Get ( "Arial" ) ;
75+ Font font = new ( family , 30 ) ;
7476 TextOptions textOptions = new ( font )
7577 {
7678 WrappingLength = path . ComputeLength ( ) ,
7779 VerticalAlignment = VerticalAlignment . Top ,
7880 HorizontalAlignment = HorizontalAlignment . Left ,
7981
8082 // Enable this to test vertical layout mode.
81- LayoutMode = LayoutMode . VerticalLeftRight
83+ // LayoutMode = LayoutMode.VerticalLeftRight
8284 } ;
8385
8486 IPathCollection glyphs = TextBuilder . GenerateGlyphs ( text , path , textOptions ) ;
8587
86- glyphs . SaveImage ( "Text-Path" , text + ".png" ) ;
88+ glyphs . SaveImageWithPath ( path , "Text-Path" , text + ".png" ) ;
8789 }
8890
8991 private static void DrawFatL ( )
@@ -223,61 +225,67 @@ private static void OutputClippedRectangle()
223225
224226 public static void SaveImage ( this IPath shape , params string [ ] path ) => new PathCollection ( shape ) . SaveImage ( path ) ;
225227
226- public static void SaveImage ( this IPathCollection shape , params string [ ] path )
228+ public static void SaveImage ( this IPathCollection collection , params string [ ] path )
227229 {
228- shape = shape . Translate ( - shape . Bounds . Location ) // touch top left
229- . Translate ( new Vector2 ( 10 ) ) ; // move in from top left
230+ // Offset the path collection to ensure our resultant image is
231+ // large enough to contain the rendered output.
232+ collection = collection . Translate ( - collection . Bounds . Location ) ;
233+
234+ int width = ( int ) ( collection . Bounds . Left + collection . Bounds . Right ) ;
235+ int height = ( int ) ( collection . Bounds . Top + collection . Bounds . Bottom ) ;
236+ using var img = new Image < Rgba32 > ( width , height ) ;
237+
238+ // Fill the canvas background and draw our shape
239+ img . Mutate ( i => i . Fill ( Color . DarkBlue ) ) ;
230240
241+ // Draw our path collection.
242+ img . Mutate ( i => i . Fill ( Color . HotPink , collection ) ) ;
243+
244+ // Ensure directory exists
231245 string fullPath = IOPath . GetFullPath ( IOPath . Combine ( "Output" , IOPath . Combine ( path ) ) ) ;
246+ IODirectory . CreateDirectory ( IOPath . GetDirectoryName ( fullPath ) ) ;
247+ img . Save ( fullPath ) ;
248+ }
232249
233- // pad even amount around shape
234- int width = ( int ) ( shape . Bounds . Left + shape . Bounds . Right ) ;
235- int height = ( int ) ( shape . Bounds . Top + shape . Bounds . Bottom ) ;
250+ public static void SaveImageWithPath ( this IPathCollection collection , IPath shape , params string [ ] path )
251+ {
252+ // Offset the shape and path collection to ensure our resultant image is
253+ // large enough to contain the rendered output.
254+ shape = shape . Translate ( - collection . Bounds . Location ) ;
255+ collection = collection . Translate ( - collection . Bounds . Location ) ;
236256
237- using ( var img = new Image < Rgba32 > ( width , height ) )
238- {
239- img . Mutate ( i => i . Fill ( Color . DarkBlue ) . Draw ( Color . HotPink , 3 , new EllipsePolygon ( width / 2F , height / 2F , 93 ) ) ) ;
257+ var bounds = RectangleF . Union ( shape . Bounds , collection . Bounds ) ;
258+ int width = ( int ) ( bounds . Left + bounds . Right ) ;
259+ int height = ( int ) ( bounds . Top + bounds . Bottom ) ;
240260
241- foreach ( IPath s in shape )
242- {
243- // In ImageSharp.Drawing.Paths there is an extension method that takes in an IShape directly.
244- img . Mutate ( i => i . Fill ( Color . HotPink , s ) ) ;
245- }
261+ using var img = new Image < Rgba32 > ( width , height ) ;
246262
247- // img.Draw(Color.LawnGreen, 1, new ShapePath(shape));
263+ // Fill the canvas background and draw our shape
264+ img . Mutate ( i => i . Fill ( Color . DarkBlue ) . Fill ( Color . White . WithAlpha ( .25F ) , shape ) ) ;
248265
249- // Ensure directory exists
250- IODirectory . CreateDirectory ( IOPath . GetDirectoryName ( fullPath ) ) ;
266+ // Draw our path collection.
267+ img . Mutate ( i => i . Fill ( Color . HotPink , collection ) ) ;
251268
252- img . Save ( fullPath ) ;
253- }
269+ // Ensure directory exists
270+ string fullPath = IOPath . GetFullPath ( IOPath . Combine ( "Output" , IOPath . Combine ( path ) ) ) ;
271+ IODirectory . CreateDirectory ( IOPath . GetDirectoryName ( fullPath ) ) ;
272+ img . Save ( fullPath ) ;
254273 }
255274
256275 public static void SaveImage ( this IPath shape , int width , int height , params string [ ] path )
257276 => new PathCollection ( shape ) . SaveImage ( width , height , path ) ;
258277
259278 public static void SaveImage ( this IPathCollection shape , int width , int height , params string [ ] path )
260279 {
261- string fullPath = IOPath . GetFullPath ( IOPath . Combine ( "Output" , IOPath . Combine ( path ) ) ) ;
262-
263- using ( var img = new Image < Rgba32 > ( width , height ) )
264- {
265- img . Mutate ( i => i . Fill ( Color . DarkBlue ) ) ;
280+ using var img = new Image < Rgba32 > ( width , height ) ;
281+ img . Mutate ( i => i . Fill ( Color . DarkBlue ) ) ;
282+ img . Mutate ( i => i . Fill ( Color . HotPink , shape ) ) ;
266283
267- // In ImageSharp.Drawing.Paths there is an extension method that takes in an IShape directly.
268- foreach ( IPath s in shape )
269- {
270- // In ImageSharp.Drawing.Paths there is an extension method that takes in an IShape directly.
271- img . Mutate ( i => i . Fill ( Color . HotPink , s ) ) ;
272- }
273-
274- // img.Draw(Color.LawnGreen, 1, new ShapePath(shape));
275-
276- // Ensure directory exists
277- IODirectory . CreateDirectory ( IOPath . GetDirectoryName ( fullPath ) ) ;
284+ // Ensure directory exists
285+ string fullPath = IOPath . GetFullPath ( IOPath . Combine ( "Output" , IOPath . Combine ( path ) ) ) ;
286+ IODirectory . CreateDirectory ( IOPath . GetDirectoryName ( fullPath ) ) ;
278287
279- img . Save ( fullPath ) ;
280- }
288+ img . Save ( fullPath ) ;
281289 }
282290 }
283291}
0 commit comments