2020-05-28 14:26:03 +01:00
|
|
|
/******************************************************************************
|
2020-02-10 01:52:56 +00:00
|
|
|
// MARECHAI: Master repository of computing history artifacts information
|
2018-08-07 20:06:44 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : SvgRender.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Render SVG country flags.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-02-10 03:05:39 +00:00
|
|
|
// Copyright © 2003-2020 Natalia Portillo
|
2018-08-07 20:06:44 +01:00
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-04-19 07:51:53 +01:00
|
|
|
using System.IO;
|
2020-02-10 02:10:18 +00:00
|
|
|
using Marechai.Database.Models;
|
2018-04-19 07:51:53 +01:00
|
|
|
using SkiaSharp;
|
2020-05-28 14:26:03 +01:00
|
|
|
using Svg.Skia;
|
2018-04-19 07:51:53 +01:00
|
|
|
|
2020-02-10 02:20:48 +00:00
|
|
|
namespace Marechai.Helpers
|
2018-04-19 07:51:53 +01:00
|
|
|
{
|
|
|
|
|
public static class SvgRender
|
|
|
|
|
{
|
|
|
|
|
public static void RenderCountries()
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!Directory.Exists("wwwroot/assets/flags/countries"))
|
|
|
|
|
return;
|
2018-04-19 07:51:53 +01:00
|
|
|
|
|
|
|
|
foreach(string file in Directory.GetFiles("wwwroot/assets/flags/countries/", "*.svg",
|
|
|
|
|
SearchOption.TopDirectoryOnly))
|
|
|
|
|
{
|
|
|
|
|
SKSvg svg = null;
|
|
|
|
|
|
|
|
|
|
string flagname = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
foreach(string format in new[]
|
|
|
|
|
{
|
|
|
|
|
"png", "webp"
|
|
|
|
|
})
|
2018-04-19 07:51:53 +01:00
|
|
|
{
|
2020-02-10 03:09:42 +00:00
|
|
|
if(!Directory.Exists(Path.Combine("wwwroot/assets/flags/countries", format)))
|
2020-02-10 22:44:18 +00:00
|
|
|
Directory.CreateDirectory(Path.Combine("wwwroot/assets/flags/countries", format));
|
2018-04-19 07:51:53 +01:00
|
|
|
|
|
|
|
|
SKEncodedImageFormat skFormat;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2018-04-19 07:51:53 +01:00
|
|
|
switch(format)
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
case"webp":
|
2018-04-19 07:51:53 +01:00
|
|
|
skFormat = SKEncodedImageFormat.Webp;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2018-04-19 07:51:53 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
skFormat = SKEncodedImageFormat.Png;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2018-04-19 07:51:53 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
foreach(int multiplier in new[]
|
|
|
|
|
{
|
|
|
|
|
1, 2, 3
|
|
|
|
|
})
|
2018-04-19 07:51:53 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!Directory.Exists(Path.Combine("wwwroot/assets/flags/countries", format, $"{multiplier}x")))
|
|
|
|
|
Directory.CreateDirectory(Path.Combine("wwwroot/assets/flags/countries", format,
|
|
|
|
|
$"{multiplier}x"));
|
2018-04-19 07:51:53 +01:00
|
|
|
|
|
|
|
|
string rendered = Path.Combine("wwwroot/assets/flags/countries", format, $"{multiplier}x",
|
|
|
|
|
flagname + $".{format}");
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
if(File.Exists(rendered))
|
|
|
|
|
continue;
|
2018-04-19 07:51:53 +01:00
|
|
|
|
|
|
|
|
Console.WriteLine("Rendering {0}", rendered);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2018-04-19 07:51:53 +01:00
|
|
|
if(svg == null)
|
|
|
|
|
{
|
|
|
|
|
svg = new SKSvg();
|
|
|
|
|
svg.Load(file);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
SKRect svgSize = svg.Picture.CullRect;
|
|
|
|
|
float svgMax = Math.Max(svgSize.Width, svgSize.Height);
|
|
|
|
|
float canvasMin = 32 * multiplier;
|
|
|
|
|
float scale = canvasMin / svgMax;
|
|
|
|
|
var matrix = SKMatrix.MakeScale(scale, scale);
|
|
|
|
|
var bitmap = new SKBitmap((int)(svgSize.Width * scale), (int)(svgSize.Height * scale));
|
|
|
|
|
var canvas = new SKCanvas(bitmap);
|
2018-04-19 07:51:53 +01:00
|
|
|
canvas.DrawPicture(svg.Picture, ref matrix);
|
|
|
|
|
canvas.Flush();
|
2020-02-10 22:44:18 +00:00
|
|
|
var image = SKImage.FromBitmap(bitmap);
|
|
|
|
|
SKData data = image.Encode(skFormat, 100);
|
|
|
|
|
var outfs = new FileStream(rendered, FileMode.CreateNew);
|
2018-04-19 07:51:53 +01:00
|
|
|
data.SaveTo(outfs);
|
|
|
|
|
outfs.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-19 15:38:45 +01:00
|
|
|
|
2020-02-10 02:47:39 +00:00
|
|
|
public static void ImportCompanyLogos(MarechaiContext context)
|
2018-04-19 15:38:45 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!Directory.Exists("wwwroot/assets/incoming"))
|
|
|
|
|
return;
|
2018-04-19 15:38:45 +01:00
|
|
|
|
|
|
|
|
foreach(string file in Directory.GetFiles("wwwroot/assets/incoming", "company_*.svg",
|
|
|
|
|
SearchOption.TopDirectoryOnly))
|
|
|
|
|
{
|
|
|
|
|
string filename = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!filename.StartsWith("company_"))
|
|
|
|
|
continue;
|
2018-04-19 15:38:45 +01:00
|
|
|
|
|
|
|
|
string[] pieces = filename.Split('_');
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
if(pieces.Length != 3)
|
|
|
|
|
continue;
|
2018-04-19 15:38:45 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
var guid = Guid.NewGuid();
|
2018-04-19 15:38:45 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!int.TryParse(pieces[1], out int companyId))
|
|
|
|
|
continue;
|
2018-04-19 15:38:45 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!int.TryParse(pieces[2], out int year))
|
|
|
|
|
continue;
|
2018-04-19 15:38:45 +01:00
|
|
|
|
2018-08-06 23:46:21 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
context.CompanyLogos.Add(new CompanyLogo
|
|
|
|
|
{
|
|
|
|
|
CompanyId = companyId, Year = year, Guid = guid
|
|
|
|
|
});
|
|
|
|
|
|
2018-08-06 23:46:21 +01:00
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2020-02-10 22:44:18 +00:00
|
|
|
catch(Exception)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-04-19 15:38:45 +01:00
|
|
|
|
|
|
|
|
File.Move(file, $"wwwroot/assets/logos/{guid}.svg");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-19 07:51:53 +01:00
|
|
|
}
|
|
|
|
|
}
|