Skip to content

Commit

Permalink
Fix ViewBox Scale
Browse files Browse the repository at this point in the history
  • Loading branch information
yiszza committed Feb 17, 2024
1 parent 40fb5f6 commit fc0fa66
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/IconPacksGenerator/IconPacksGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="CliWrap" Version="3.6.0" />
<PackageReference Include="Microsoft.Maui.Graphics" Version="8.0.7" />
<PackageReference Include="Svg" Version="3.4.4" />
<PackageReference Include="System.Text.Json" Version="7.0.1" />
</ItemGroup>
Expand Down
60 changes: 56 additions & 4 deletions src/IconPacksGenerator/Util.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Svg;
using System.Text;
using System.Xml.Linq;
using Microsoft.Maui.Graphics;
using Svg;

namespace IconPacksGenerator;

Expand Down Expand Up @@ -31,10 +32,44 @@ internal static string GetCamelId(this string id)
if (File.Exists(path))
{
var xd = XDocument.Load(path);

var vector = xd.Descendants("vector");

var viewportWidthName = XName.Get(
"viewportWidth",
"http://schemas.android.com/apk/res/android"
);
var viewportWidth = vector
.Select(x => x.Attribute(viewportWidthName))
.First()
?.Value;

var viewportHeightName = XName.Get(
"viewportHeight",
"http://schemas.android.com/apk/res/android"
);
var viewportHeight = vector
.Select(x => x.Attribute(viewportWidthName))
.First()
?.Value;

var pe = xd.Descendants("path");
var dataName = XName.Get("pathData", "http://schemas.android.com/apk/res/android");
var data = pe.Select(x => x.Attribute(dataName)).First()?.Value;
return data;

if (!string.IsNullOrEmpty(data))
{
if (
int.TryParse(viewportWidth, out var width)
&& int.TryParse(viewportHeight, out var height)
)
return GetScaledPathData(
data,
new SvgViewBox(width, height, width, height)
);
else
return GetScaledPathData(data, new SvgViewBox(24f, 24f, 24f, 24f));
}
}
return default;
}
Expand Down Expand Up @@ -68,9 +103,26 @@ internal static string GetCamelId(this string id)
else if (element is SvgPath)
result.Add(element.ParsePath());
}
result.Reverse();
return GetScaledPathData(
result.Count > 0 ? string.Join(' ', result) : default,
svgDoc.ViewBox
);
}
result.Reverse();
return result.Count > 0 ? string.Join(' ', result) : default;
return default;
}

internal static string? GetScaledPathData(string? pathData, SvgViewBox viewBox)
{
if (string.IsNullOrEmpty(pathData))
return default;

var scaleX = 24f / viewBox.Width;
var scaleY = 24f / viewBox.Height;

var path = PathBuilder.Build(pathData);
var scaledPath = path.AsScaledPath(scaleX, scaleY);
return scaledPath.ToDefinitionString();
}

internal static void OutputIconKindFile(Dictionary<string, string> iconKinds, string type)
Expand Down

0 comments on commit fc0fa66

Please sign in to comment.