generated from unoplatform/template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ResizeImageInfo.cs
147 lines (110 loc) · 4.29 KB
/
ResizeImageInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Build.Framework;
using SkiaSharp;
namespace Uno.Resizetizer
{
[DebuggerDisplay("OutputName: {OutputName}, OutputPath: {OutputPath}, IsAppIcon: {IsAppIcon}")]
internal class ResizeImageInfo
{
public string? Alias { get; set; }
public string? Filename { get; set; }
public string OutputPath =>
string.IsNullOrWhiteSpace(Alias)
? string.IsNullOrWhiteSpace(Filename)
? Path.GetDirectoryName(ForegroundFilename)
: Path.GetDirectoryName(Filename)
: Path.GetDirectoryName(Alias);
public string OutputName =>
string.IsNullOrWhiteSpace(Alias)
? string.IsNullOrWhiteSpace(Filename)
? Path.GetFileNameWithoutExtension(ForegroundFilename)
: Path.GetFileNameWithoutExtension(Filename)
: Path.GetFileNameWithoutExtension(Alias);
public string OutputExtension =>
string.IsNullOrWhiteSpace(Alias) || !Path.HasExtension(Alias)
? string.IsNullOrWhiteSpace(Filename) || !Path.HasExtension(Filename)
? Path.GetExtension(ForegroundFilename)
: Path.GetExtension(Filename)
: Path.GetExtension(Alias);
public SKSize? BaseSize { get; set; }
public bool Resize { get; set; } = true;
public SKColor? TintColor { get; set; }
public SKColor? Color { get; set; }
public bool IsVector => IsVectorFilename(Filename);
public bool IsAppIcon { get; set; }
public string? ForegroundFilename { get; set; }
public bool ForegroundIsVector => IsVectorFilename(ForegroundFilename);
public double ForegroundScale { get; set; } = 1.0;
private static bool IsVectorFilename(string? filename)
=> Path.GetExtension(filename)?.Equals(".svg", StringComparison.OrdinalIgnoreCase) ?? false;
public static ResizeImageInfo Parse(ITaskItem image)
=> Parse(new[] { image })[0];
public static List<ResizeImageInfo> Parse(IEnumerable<ITaskItem> images)
{
var r = new List<ResizeImageInfo>();
if (images == null)
return r;
foreach (var image in images)
{
var info = new ResizeImageInfo();
var fileInfo = new FileInfo(image.GetMetadata("FullPath"));
if (!fileInfo.Exists)
throw new FileNotFoundException("Unable to find background file: " + fileInfo.FullName, fileInfo.FullName);
info.Filename = fileInfo.FullName;
info.Alias = image.GetMetadata("Link");
if (string.IsNullOrWhiteSpace(info.Alias))
{
var projDirectory = image.GetMetadata("DefiningProjectDirectory");
if (!string.IsNullOrWhiteSpace(projDirectory))
{
info.Alias = fileInfo.FullName.Replace(projDirectory, string.Empty);
}
}
info.BaseSize = Utils.ParseSizeString(image.GetMetadata("BaseSize"));
if (bool.TryParse(image.GetMetadata("Resize"), out var rz))
{
info.Resize = rz;
}
else if (info.BaseSize == null && !info.IsVector)
{
// By default do not resize non-vector images
info.Resize = false;
}
var tintColor = image.GetMetadata("TintColor");
info.TintColor = Utils.ParseColorString(tintColor);
if (info.TintColor is null && !string.IsNullOrEmpty(tintColor))
throw new InvalidDataException($"Unable to parse color value '{tintColor}' for '{info.Filename}'.");
var color = image.GetMetadata("Color");
info.Color = Utils.ParseColorString(color);
if (info.Color is null && !string.IsNullOrEmpty(color))
throw new InvalidDataException($"Unable to parse color value '{color}' for '{info.Filename}'.");
if (bool.TryParse(image.GetMetadata("IsAppIcon"), out var iai))
info.IsAppIcon = iai;
if (float.TryParse(image.GetMetadata("ForegroundScale"), out var fsc))
info.ForegroundScale = fsc;
var fgFile = image.GetMetadata("ForegroundFile");
if (!string.IsNullOrEmpty(fgFile))
{
var fgFileInfo = new FileInfo(fgFile);
if (!fgFileInfo.Exists)
throw new FileNotFoundException("Unable to find foreground file: " + fgFileInfo.FullName, fgFileInfo.FullName);
info.ForegroundFilename = fgFileInfo.FullName;
}
// make sure the image is a foreground if this is an icon
if (info.IsAppIcon && string.IsNullOrEmpty(info.ForegroundFilename))
{
info.ForegroundFilename = info.Filename;
info.Filename = null;
}
// TODO:
// - Parse out custom DPI's
r.Add(info);
}
return r;
}
}
}