Skip to content

Commit 1e46343

Browse files
committed
Initial commit
1 parent 70e4ffb commit 1e46343

12 files changed

+474
-0
lines changed

AssetGenerator.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetGenerator", "AssetGenerator\AssetGenerator.csproj", "{5C191E9A-6B9F-4E07-B7FF-45AFD928D02D}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{5C191E9A-6B9F-4E07-B7FF-45AFD928D02D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{5C191E9A-6B9F-4E07-B7FF-45AFD928D02D}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{5C191E9A-6B9F-4E07-B7FF-45AFD928D02D}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{5C191E9A-6B9F-4E07-B7FF-45AFD928D02D}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using SkiaSharp.Extended.Svg;
6+
7+
namespace AssetGenerator
8+
{
9+
public class AndroidAssetGenerator : IAssetGenerator
10+
{
11+
public async Task CreateAsset(string filepath, string filename, string destinationDirectory, int quality)
12+
{
13+
var resourceTypes = new Dictionary<AndroidResourceType, float>
14+
{
15+
// Scale factors
16+
{AndroidResourceType.LDPI, 0.75f},
17+
{AndroidResourceType.MDPI, 1},
18+
{AndroidResourceType.HDPI, 1.5f},
19+
{AndroidResourceType.XHDPI, 2f},
20+
{AndroidResourceType.XXHDPI, 3f},
21+
{AndroidResourceType.XXXHDPI, 4f}
22+
};
23+
24+
foreach (var resourceType in resourceTypes)
25+
try
26+
{
27+
var name = Enum.GetName(typeof(AndroidResourceType), resourceType.Key);
28+
var resourceDirectoryName = $"drawable-{name.ToLowerInvariant()}";
29+
var svg = new SKSvg();
30+
try
31+
{
32+
svg.Load(filepath);
33+
}
34+
catch (Exception e)
35+
{
36+
Console.WriteLine($"Unexpected error when parsing asset: {filepath}");
37+
Console.WriteLine("Error: " + e.Message);
38+
Console.WriteLine("Exiting with error 1");
39+
Environment.Exit(1);
40+
}
41+
42+
var width = (int) (svg.CanvasSize.Width * resourceType.Value);
43+
var height = (int) (svg.CanvasSize.Height * resourceType.Value);
44+
var filenameWithExtension = $"{filename}.png";
45+
var resourceDir = Path.Combine(destinationDirectory, resourceDirectoryName);
46+
if (!Directory.Exists(resourceDir))
47+
{
48+
Directory.CreateDirectory(resourceDir);
49+
}
50+
var finalPath = Path.Combine(resourceDir, filenameWithExtension);
51+
await PngHelper.GeneratePng(width, height, filepath, finalPath, quality);
52+
Console.WriteLine($"Successfully created asset: {finalPath}");
53+
}
54+
catch (Exception e)
55+
{
56+
Console.WriteLine($"Failed to generate asset: {filename}");
57+
Console.WriteLine("Error: " + e.Message);
58+
Console.WriteLine("Exiting with error 1");
59+
Environment.Exit(1);
60+
}
61+
}
62+
}
63+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace AssetGenerator
2+
{
3+
public enum AndroidResourceType
4+
{
5+
LDPI,
6+
MDPI,
7+
HDPI,
8+
XHDPI,
9+
XXHDPI,
10+
XXXHDPI
11+
}
12+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{5C191E9A-6B9F-4E07-B7FF-45AFD928D02D}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>AssetGenerator</RootNamespace>
11+
<AssemblyName>AssetGenerator</AssemblyName>
12+
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<LangVersion>7.2</LangVersion>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32">
37+
<HintPath>..\packages\CommandLineParser.2.2.1\lib\net45\CommandLine.dll</HintPath>
38+
</Reference>
39+
<Reference Include="SkiaSharp, Version=1.60.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756">
40+
<HintPath>..\packages\SkiaSharp.1.60.1\lib\net45\SkiaSharp.dll</HintPath>
41+
</Reference>
42+
<Reference Include="SkiaSharp.Extended.Svg, Version=1.60.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756">
43+
<HintPath>..\packages\SkiaSharp.Svg.1.60.0\lib\netstandard2.0\SkiaSharp.Extended.Svg.dll</HintPath>
44+
</Reference>
45+
<Reference Include="System" />
46+
<Reference Include="System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
47+
<HintPath>..\packages\System.Console.4.0.0-rc2-24027\lib\net46\System.Console.dll</HintPath>
48+
</Reference>
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Data" />
51+
<Reference Include="System.IO, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
52+
<HintPath>..\packages\System.IO.4.1.0-rc2-24027\lib\net462\System.IO.dll</HintPath>
53+
</Reference>
54+
<Reference Include="System.Linq, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
55+
<HintPath>..\packages\System.Linq.4.1.0-rc2-24027\lib\net462\System.Linq.dll</HintPath>
56+
</Reference>
57+
<Reference Include="System.Reflection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
58+
<HintPath>..\packages\System.Reflection.4.1.0-rc2-24027\lib\net462\System.Reflection.dll</HintPath>
59+
</Reference>
60+
<Reference Include="System.Reflection.TypeExtensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
61+
<HintPath>..\packages\System.Reflection.TypeExtensions.4.1.0-rc2-24027\lib\net462\System.Reflection.TypeExtensions.dll</HintPath>
62+
</Reference>
63+
<Reference Include="System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
64+
<HintPath>..\packages\System.Runtime.4.1.0-rc2-24027\lib\net462\System.Runtime.dll</HintPath>
65+
</Reference>
66+
<Reference Include="System.Runtime.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
67+
<HintPath>..\packages\System.Runtime.Extensions.4.1.0-rc2-24027\lib\net462\System.Runtime.Extensions.dll</HintPath>
68+
</Reference>
69+
<Reference Include="System.Xml" />
70+
</ItemGroup>
71+
<ItemGroup>
72+
<Compile Include="AndroidAssetGenerator.cs" />
73+
<Compile Include="AndroidResourceType.cs" />
74+
<Compile Include="DeviceType.cs" />
75+
<Compile Include="IAssetGenerator.cs" />
76+
<Compile Include="IOSAssetGenerator.cs" />
77+
<Compile Include="Options.cs" />
78+
<Compile Include="PngHelper.cs" />
79+
<Compile Include="Program.cs" />
80+
<Compile Include="Properties\AssemblyInfo.cs" />
81+
</ItemGroup>
82+
<ItemGroup>
83+
<None Include="packages.config" />
84+
</ItemGroup>
85+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
86+
<Import Project="..\packages\SkiaSharp.1.60.0\build\net45\SkiaSharp.targets" Condition="Exists('..\packages\SkiaSharp.1.60.0\build\net45\SkiaSharp.targets')" />
87+
<Import Project="..\packages\SkiaSharp.1.60.1\build\net45\SkiaSharp.targets" Condition="Exists('..\packages\SkiaSharp.1.60.1\build\net45\SkiaSharp.targets')" />
88+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
89+
Other similar extension points exist, see Microsoft.Common.targets.
90+
<Target Name="BeforeBuild">
91+
</Target>
92+
<Target Name="AfterBuild">
93+
</Target>
94+
-->
95+
</Project>

AssetGenerator/DeviceType.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AssetGenerator
2+
{
3+
public enum DeviceType
4+
{
5+
Android,
6+
iOS
7+
}
8+
}

AssetGenerator/IAssetGenerator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
3+
namespace AssetGenerator
4+
{
5+
public interface IAssetGenerator
6+
{
7+
Task CreateAsset(string filepath, string filename, string destinationDirectory, int quality);
8+
}
9+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using SkiaSharp.Extended.Svg;
5+
6+
namespace AssetGenerator
7+
{
8+
public class IOSAssetGenerator : IAssetGenerator
9+
{
10+
public async Task CreateAsset(string filepath, string filename, string destinationDirectory, int quality)
11+
{
12+
for (var i = 1; i < 4; i++)
13+
{
14+
try
15+
{
16+
var svg = new SKSvg();
17+
try
18+
{
19+
svg.Load(filepath);
20+
}
21+
catch (Exception e)
22+
{
23+
Console.WriteLine($"Unexpected error when parsing asset: {filepath}");
24+
Console.WriteLine("Error: " + e.Message);
25+
Console.WriteLine("Exiting with error 1");
26+
Environment.Exit(1);
27+
}
28+
29+
string newFilename;
30+
if (i == 1)
31+
newFilename = filename + ".png";
32+
else
33+
newFilename = filename + $"@{i}x.png";
34+
35+
var width = (int) (svg.CanvasSize.Width * i);
36+
var height = (int) (svg.CanvasSize.Height * i);
37+
await PngHelper.GeneratePng(width, height, filepath, Path.Combine(destinationDirectory, newFilename), quality);
38+
Console.WriteLine($"Successfully created asset: {Path.GetFileName(newFilename)}");
39+
}
40+
catch (Exception e)
41+
{
42+
Console.WriteLine($"Failed to generate asset: {filename}");
43+
Console.WriteLine("Error: " + e.Message);
44+
Console.WriteLine("Exiting with error 1");
45+
Environment.Exit(1);
46+
}
47+
}
48+
}
49+
}
50+
}

AssetGenerator/Options.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using CommandLine;
3+
using CommandLine.Text;
4+
5+
namespace AssetGenerator
6+
{
7+
public class Options
8+
{
9+
[Option('m', "mode", HelpText = "Specify which assets to build. ios or android", Required = true)]
10+
public string Mode { get; set; }
11+
12+
[Option('s', "source", Separator = '-', HelpText = "Specify source folder", Required = false)]
13+
public string SourceFolderPath { get; set; }
14+
15+
[Option('d', "destination", Separator = '-', HelpText = "Specify destination folder", Required = false)]
16+
public string DestinationFolderPath { get; set; }
17+
18+
[Option('q', "quality", Separator = '-', HelpText = "Specify quality of rendered png files (100 is max)", Required = false, Default = 80)]
19+
public int Quality { get; set; }
20+
21+
[Usage(ApplicationAlias = "AssetGenerator")]
22+
public static IEnumerable<Example> Examples
23+
{
24+
get
25+
{
26+
yield return new Example("Common usage",
27+
new Options
28+
{
29+
Mode = "iOS",
30+
SourceFolderPath = "sourcefolder",
31+
DestinationFolderPath = "destinationfolder",
32+
Quality = 80
33+
});
34+
}
35+
}
36+
}
37+
}

AssetGenerator/PngHelper.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using SkiaSharp;
4+
using SKSvg = SkiaSharp.Extended.Svg.SKSvg;
5+
6+
namespace AssetGenerator
7+
{
8+
public class PngHelper
9+
{
10+
public static async Task GeneratePng(int width, int height, string filepath, string filename, int quality)
11+
{
12+
var svg2 = new SKSvg(new SKSize(width, height));
13+
svg2.Load(filepath);
14+
15+
using (var image = SKImage.FromPicture(svg2.Picture, new SKSizeI(width, height)))
16+
{
17+
using (var data = image.Encode(SKEncodedImageFormat.Png, quality))
18+
{
19+
// save the data to a stream
20+
using (var stream = File.OpenWrite(filename))
21+
{
22+
data.SaveTo(stream);
23+
await stream.FlushAsync();
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)