/ src / common / FilePreviewCommon / GcodeHelper.cs
GcodeHelper.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Collections.Generic;
 7  using System.IO;
 8  using System.Linq;
 9  using System.Text;
10  
11  namespace Microsoft.PowerToys.FilePreviewCommon
12  {
13      /// <summary>
14      /// Gcode file helper class.
15      /// </summary>
16      public static class GcodeHelper
17      {
18          /// <summary>
19          /// Gets any thumbnails found in a gcode file.
20          /// </summary>
21          /// <param name="reader">The <see cref="TextReader"/> instance to the gcode file.</param>
22          /// <returns>The thumbnails found in a gcode file.</returns>
23          public static IEnumerable<GcodeThumbnail> GetThumbnails(TextReader reader)
24          {
25              string? line;
26              var format = GcodeThumbnailFormat.Unknown;
27              StringBuilder? capturedText = null;
28  
29              while ((line = reader.ReadLine()) != null)
30              {
31                  if (line.StartsWith("; thumbnail", StringComparison.InvariantCulture))
32                  {
33                      var parts = line[11..].Split(" ");
34  
35                      switch (parts[1])
36                      {
37                          case "begin":
38                              format = parts[0].ToUpperInvariant() switch
39                              {
40                                  "" => GcodeThumbnailFormat.PNG,
41                                  "_JPG" => GcodeThumbnailFormat.JPG,
42                                  "_QOI" => GcodeThumbnailFormat.QOI,
43                                  _ => GcodeThumbnailFormat.Unknown,
44                              };
45                              capturedText = new StringBuilder();
46  
47                              break;
48  
49                          case "end":
50                              if (capturedText != null)
51                              {
52                                  yield return new GcodeThumbnail(format, capturedText.ToString());
53  
54                                  capturedText = null;
55                              }
56  
57                              break;
58                      }
59                  }
60                  else
61                  {
62                      capturedText?.Append(line[2..]);
63                  }
64              }
65          }
66  
67          /// <summary>
68          /// Gets the best thumbnail available in a gcode file.
69          /// </summary>
70          /// <param name="reader">The <see cref="TextReader"/> instance to the gcode file.</param>
71          /// <returns>The best thumbnail available in the gcode file.</returns>
72          public static GcodeThumbnail? GetBestThumbnail(TextReader reader)
73          {
74              return GetThumbnails(reader)
75                  .Where(x => x.Format != GcodeThumbnailFormat.Unknown)
76                  .OrderByDescending(x => (int)x.Format)
77                  .ThenByDescending(x => x.Data.Length)
78                  .FirstOrDefault();
79          }
80      }
81  }