예제 #1
0
        void ProcessAppIcon(SharedImageInfo img, ConcurrentBag <ResizedImageInfo> resizedImages)
        {
            var appIconName = img.OutputName;

            // Generate the actual bitmap app icons themselves
            var appIconDpis = DpiPath.GetAppIconDpis(PlatformType, appIconName);

            Log.LogMessage(MessageImportance.Low, $"App Icon");

            // Apple and Android have special additional files to generate for app icons
            if (PlatformType == "android")
            {
                Log.LogMessage(MessageImportance.Low, $"Android Adaptive Icon Generator");

                appIconName = appIconName.ToLowerInvariant();

                var adaptiveIconGen = new AndroidAdaptiveIconGenerator(img, appIconName, IntermediateOutputPath, this);
                var iconsGenerated  = adaptiveIconGen.Generate();

                foreach (var iconGenerated in iconsGenerated)
                {
                    resizedImages.Add(iconGenerated);
                }
            }
            else if (PlatformType == "ios")
            {
                Log.LogMessage(MessageImportance.Low, $"iOS Icon Assets Generator");

                var appleAssetGen = new AppleIconAssetsGenerator(img, appIconName, IntermediateOutputPath, appIconDpis, this);

                var assetsGenerated = appleAssetGen.Generate();

                foreach (var assetGenerated in assetsGenerated)
                {
                    resizedImages.Add(assetGenerated);
                }
            }

            Log.LogMessage(MessageImportance.Low, $"Generating App Icon Bitmaps for DPIs");

            var appTool = new SkiaSharpAppIconTools(img, this);

            Log.LogMessage(MessageImportance.Low, $"App Icon: Intermediate Path " + IntermediateOutputPath);

            foreach (var dpi in appIconDpis)
            {
                Log.LogMessage(MessageImportance.Low, $"App Icon: " + dpi);

                var destination = Resizer.GetFileDestination(img, dpi, IntermediateOutputPath)
                                  .Replace("{name}", appIconName);

                Log.LogMessage(MessageImportance.Low, $"App Icon Destination: " + destination);

                appTool.Resize(dpi, Path.ChangeExtension(destination, ".png"));
            }
        }
        System.Threading.Tasks.Task DoExecute()
        {
            Svg.SvgDocument.SkipGdiPlusCapabilityCheck = true;

            var images = ParseImageTaskItems(SharedImages);

            var dpis = DpiPath.GetDpis(PlatformType);

            if (dpis == null || dpis.Length <= 0)
            {
                return(System.Threading.Tasks.Task.CompletedTask);
            }

            var originalScaleDpi = DpiPath.GetOriginal(PlatformType);

            var resizedImages = new ConcurrentBag <ResizedImageInfo>();

            System.Threading.Tasks.Parallel.ForEach(images, img =>
            {
                if (img.IsAppIcon)
                {
                    var appIconName = "appicon";                     // Path.GetFileNameWithoutExtension(img.Filename);

                    // Generate the actual bitmap app icons themselves
                    var appIconDpis = DpiPath.GetAppIconDpis(PlatformType, appIconName);

                    Log.LogMessage(MessageImportance.Low, $"App Icon");

                    // Apple and Android have special additional files to generate for app icons
                    if (PlatformType == "android")
                    {
                        Log.LogMessage(MessageImportance.Low, $"Android Adaptive Icon Generator");

                        appIconName = appIconName.ToLowerInvariant();

                        var adaptiveIconGen = new AndroidAdaptiveIconGenerator(img, appIconName, IntermediateOutputPath, this);
                        var iconsGenerated  = adaptiveIconGen.Generate();

                        foreach (var iconGenerated in iconsGenerated)
                        {
                            resizedImages.Add(iconGenerated);
                        }
                    }
                    else if (PlatformType == "ios")
                    {
                        Log.LogMessage(MessageImportance.Low, $"iOS Icon Assets Generator");

                        var appleAssetGen = new AppleIconAssetsGenerator(img, appIconName, IntermediateOutputPath, appIconDpis, this);

                        var assetsGenerated = appleAssetGen.Generate();

                        foreach (var assetGenerated in assetsGenerated)
                        {
                            resizedImages.Add(assetGenerated);
                        }
                    }

                    Log.LogMessage(MessageImportance.Low, $"Generating App Icon Bitmaps for DPIs");

                    var appTool = new SkiaSharpAppIconTools(img, this);

                    Log.LogMessage(MessageImportance.Low, $"App Icon: Intermediate Path " + IntermediateOutputPath);

                    foreach (var dpi in appIconDpis)
                    {
                        Log.LogMessage(MessageImportance.Low, $"App Icon: " + dpi);

                        var destination = Resizer.GetFileDestination(img, dpi, IntermediateOutputPath)
                                          .Replace("{name}", appIconName);
                        Log.LogMessage(MessageImportance.Low, $"App Icon Destination: " + destination);
                        appTool.Resize(dpi, Path.ChangeExtension(destination, ".png"));
                    }
                }
                else
                {
                    var opStopwatch = new Stopwatch();
                    opStopwatch.Start();

                    var op = "Resize";

                    // By default we resize, but let's make sure
                    if (img.Resize)
                    {
                        var resizer = new Resizer(img, IntermediateOutputPath, this);

                        foreach (var dpi in dpis)
                        {
                            Log.LogMessage(MessageImportance.Low, $"Resizing {img.Filename}");

                            var r = resizer.Resize(dpi, InputsFile);
                            resizedImages.Add(r);

                            Log.LogMessage(MessageImportance.Low, $"Resized {img.Filename}");
                        }
                    }
                    else
                    {
                        op = "Copy";

                        Log.LogMessage(MessageImportance.Low, $"Copying {img.Filename}");
                        // Otherwise just copy the thing over to the 1.0 scale
                        var r = Resizer.CopyFile(img, originalScaleDpi, IntermediateOutputPath, InputsFile, this, PlatformType.ToLower().Equals("android"));
                        resizedImages.Add(r);
                        Log.LogMessage(MessageImportance.Low, $"Copied {img.Filename}");
                    }

                    opStopwatch.Stop();

                    Log.LogMessage(MessageImportance.Low, $"{op} took {opStopwatch.ElapsedMilliseconds}ms");
                }
            });

            var copiedResources = new List <TaskItem>();

            foreach (var img in resizedImages)
            {
                var    attr     = new Dictionary <string, string>();
                string itemSpec = Path.GetFullPath(img.Filename);

                // Fix the item spec to be relative for mac
                if (bool.TryParse(IsMacEnabled, out bool isMac) && isMac)
                {
                    itemSpec = img.Filename;
                }

                // Add DPI info to the itemspec so we can use it in the targets
                attr.Add("_ResizetizerDpiPath", img.Dpi.Path);
                attr.Add("_ResizetizerDpiScale", img.Dpi.Scale.ToString());

                copiedResources.Add(new TaskItem(itemSpec, attr));
            }

            CopiedResources = copiedResources.ToArray();

            return(System.Threading.Tasks.Task.CompletedTask);
        }