-
Notifications
You must be signed in to change notification settings - Fork 0
/
MontaggioDorsiEngine.cs
213 lines (191 loc) · 6.49 KB
/
MontaggioDorsiEngine.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// copyright (c) 2020-2022 Roberto Ceccarelli - Casasoft
// http://strawberryfield.altervista.org
//
// This file is part of Casasoft Contemporary Carte de Visite Tools
// https://github.com/strawberryfield/Contemporary_CDV
//
// Casasoft CCDV Tools is free software:
// you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Casasoft CCDV Tools 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 AGPL v.3
// along with Casasoft CCDV Tools.
// If not, see <http://www.gnu.org/licenses/>.
using Casasoft.CCDV.JSON;
using Casasoft.CCDV.Scripting;
using ImageMagick;
using System;
using System.Text.Json;
namespace Casasoft.CCDV.Engines;
/// <summary>
/// Montaggio dorsi engine
/// </summary>
public class MontaggioDorsiEngine : BaseEngine
{
/// <summary>
/// Output paper size
/// </summary>
public PaperFormats PaperFormat { get; set; }
#region constructors
/// <summary>
/// Constructor
/// </summary>
public MontaggioDorsiEngine() : base()
{
parameters = new MontaggioDorsiParameters();
ScriptingClass = new MontaggioDorsiScripting();
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="par"></param>
public MontaggioDorsiEngine(CommandLine par) : base(par)
{
parameters = new MontaggioDorsiParameters();
MontaggioDorsiCommandLine p = (MontaggioDorsiCommandLine)par;
PaperFormat = p.PaperFormat;
ScriptingClass = new MontaggioDorsiScripting();
Script = p.Script;
}
#endregion
#region json
/// <summary>
/// Returns the parameters in json format
/// </summary>
/// <returns></returns>
public override string GetJsonParams()
{
GetBaseJsonParams();
MontaggioDorsiParameters p = (MontaggioDorsiParameters)parameters;
p.PaperFormat = PaperFormat;
return JsonSerializer.Serialize(p);
}
/// <summary>
/// Sets the parameters from json formatted string
/// </summary>
/// <param name="json"></param>
public override void SetJsonParams(string json)
{
MontaggioDorsiParameters p = JsonSerializer.Deserialize<MontaggioDorsiParameters>(json);
parameters = p;
SetBaseJsonParams();
PaperFormat = p.PaperFormat;
}
#endregion
#region build
/// <summary>
/// Does the dirty work
/// </summary>
/// <param name="quiet"></param>
/// <returns></returns>
public override MagickImage GetResult(bool quiet)
{
_ = base.GetResult(quiet);
MagickImage final = PaperFormat == PaperFormats.Medium ? img.InCartha15x20_o() : img.InCartha20x27_o();
if (ScriptInstance != null)
{
var f = Compiler.Run(ScriptInstance, "OutputImage", null);
if(f != null)
{
final = (MagickImage)f;
}
}
MagickImageCollection imagesV = new();
MagickImageCollection imagesO = new();
// if no file specified use a blank image
if (FilesList.Count == 0)
{
MagickImage dorsoOrig = img.CDV_Full_v();
if (PaperFormat == PaperFormats.Medium)
{
for (int i = 0; i < 3; i++) imagesV.Add(dorsoOrig.Clone());
}
else
{
for (int i = 0; i < 4; i++) imagesV.Add(dorsoOrig.Clone());
dorsoOrig.Rotate(90);
for (int i = 0; i < 2; i++) imagesO.Add(dorsoOrig.Clone());
}
}
else
{
int nImg = 0;
if (PaperFormat == PaperFormats.Medium)
{
_ = LoadImages(3, nImg, imagesV, quiet, fmt.CDV_Full_v);
}
else
{
nImg = LoadImages(4, nImg, imagesV, quiet, fmt.CDV_Full_v);
_ = LoadImages(2, nImg, imagesO, quiet, fmt.CDV_Full_o);
}
}
// Margini di taglio
Drawables draw = new();
draw.StrokeColor(BorderColor).StrokeWidth(1);
if (PaperFormat == PaperFormats.Medium)
{
int top = (final.Height - fmt.CDV_Full_v.Height) / 2;
int left = (final.Width - fmt.CDV_Full_v.Width * 3) / 2;
Utils.HLine(draw, top, final.Width);
Utils.HLine(draw, final.Height - top, final.Width);
Utils.VLine(draw, left, final.Height);
Utils.VLine(draw, final.Width - left, final.Height);
}
else
{
int h = fmt.ToPixels(10);
Utils.HLine(draw, h, final.Width);
h += fmt.CDV_Full_v.Height;
Utils.HLine(draw, h, final.Width);
h += fmt.CDV_Full_v.Width;
Utils.HLine(draw, h, final.Width);
}
draw.Draw(final);
if (PaperFormat == PaperFormats.Medium)
{
final.Composite(imagesV.AppendHorizontally(), Gravity.Center, new PointD(0, 0));
}
else
{
final.Composite(imagesV.AppendHorizontally(), Gravity.North, new PointD(0, fmt.ToPixels(10)));
final.Composite(imagesO.AppendHorizontally(), Gravity.North,
new PointD(0, fmt.ToPixels(10) + fmt.CDV_Full_v.Height - 1));
}
return final;
}
#region functions
private int LoadImages(int n, int counter, MagickImageCollection dest, bool quiet, MagickGeometry orientation)
{
int nImg = counter;
for (int i = 0; i < n; i++)
{
if (!quiet) Console.WriteLine($"Processing: {FilesList[nImg]}");
MagickImage image = new(FilesList[nImg]);
if (ScriptInstance != null)
{
var im = Compiler.Run(ScriptInstance, "ProcessOnLoad", new object[] { image });
if (im != null)
{
image = (MagickImage)im;
}
}
MagickImage dorso = Utils.RotateResizeAndFill(image, orientation, FillColor);
dorso.BorderColor = BorderColor;
dorso.Border(1);
dest.Add(dorso);
nImg++;
if (nImg >= FilesList.Count) nImg = 0;
}
return nImg;
}
#endregion
#endregion
}