-
Notifications
You must be signed in to change notification settings - Fork 0
/
RagecomicToPDF.cs
470 lines (376 loc) · 21.9 KB
/
RagecomicToPDF.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using MyMedia = System.Windows.Media;
using iTextSharp;
using iTextSharp.awt.geom;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using FluxJpeg.Core.Encoder;
using FluxJpeg.Core.Filtering;
using Draw = System.Drawing;
using FluxJpeg.Core.Decoder;
using Ionic;
using Ionic.Zip;
namespace RagemakerToPDF
{
class RagecomicToPDF
{
static int rowHeight = 239; // row height excluding borders/gridlines
static int rowWidth = 324; // row width excluding borders/gridlines
static int borderThickness = 1; // It's just 1. Won't really change, but whatever. If you change it, things will get fucky.
public static bool MakePDF(Ragecomic comic, string pdffile)
{
using (System.IO.FileStream fs = new FileStream(pdffile, FileMode.Create))
{
int rows = (int)Math.Ceiling((double)comic.panels / 2);
int width = rowWidth * 2 + borderThickness * 3;
int height = rowHeight * rows + borderThickness * rows + borderThickness; // 239 per row, plus 1 pixel border per row, plus 1 pixel extra border
ZipFile drawImageZip = new ZipFile();
bool hasDrawImages = comic.items.getDrawImageCount() > 0;
if (hasDrawImages)
{
drawImageZip.Dispose();
string addition = "";
int nr = 1;
while (File.Exists(pdffile + ".drawimages" + addition + ".zip"))
{
addition = "_" + (nr++).ToString();
}
drawImageZip = new ZipFile(pdffile + ".drawimages" + addition + ".zip");
}
// Create an instance of the document class which represents the PDF document itself.
Rectangle pageSize = new Rectangle(width, height);
Document document = new Document(pageSize, 0, 0, 0, 0);
// Create an instance to the PDF file by creating an instance of the PDF
// Writer class using the document and the filestrem in the constructor.
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.AddAuthor("Derp");
document.AddCreator("RagemakerToPDF");
document.AddKeywords("rage, comic");
document.AddSubject("A rage comic");
document.AddTitle("A rage comic");
// Open the document to enable you to write to the document
document.Open();
PdfContentByte cb = writer.DirectContent;
// Fill background with white
Rectangle rect = new iTextSharp.text.Rectangle(0, 0, width, height);
rect.BackgroundColor = new BaseColor(255, 255, 255);
cb.Rectangle(rect);
// Draw grid on bottom if it isn't set to be on top.
if (!comic.gridAboveAll && comic.showGrid)
{
DrawGrid(cb, width, height, rows, comic);
}
//List<MyMedia.FontFamily> families = MyMedia.Fonts.SystemFontFamilies.ToList();
// This was a neat idea, but it's much too slow
//List<string> files = FontFinder.GetFilesForFont("Courier New").ToList();
//string filename = FontFinder.GetSystemFontFileName(families[0].)
//Draw.Font testFont = new Draw.Font(new Draw.FontFamily("Courier New"),12f,Draw.FontStyle.Regular | Draw.FontStyle.Bold);
string courierPath = FontFinder.GetSystemFontFileName("Courier New", true);
string courierBoldPath = FontFinder.GetSystemFontFileName("Courier New", true, Draw.FontStyle.Bold);
string tahomaPath = FontFinder.GetSystemFontFileName("Tahoma", true, Draw.FontStyle.Bold);
// Define base fonts
Font[] fonts = new Font[3];
fonts[0] = new Font(BaseFont.CreateFont(courierPath, BaseFont.CP1252, BaseFont.EMBEDDED));
fonts[1] = new Font(BaseFont.CreateFont(courierBoldPath, BaseFont.CP1252, BaseFont.EMBEDDED));
fonts[2] = new Font(BaseFont.CreateFont(tahomaPath, BaseFont.CP1252, BaseFont.EMBEDDED));
/*fonts[0] = BaseFont.CreateFont("fonts/TRCourierNew.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
fonts[1] = BaseFont.CreateFont("fonts/TRCourierNewBold.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
fonts[2] = new Font(BaseFont.CreateFont("fonts/Tahoma-Bold.ttf", BaseFont.CP1252, BaseFont.EMBEDDED));*/
int drawimageindex = 0;
int index = 0;
foreach (var item in comic.items)
{
if (item is Face)
{
Face face = (Face)item;
Image pdfImage;
System.Drawing.Image faceImage;
// If mirroring is necessary, we have to read the image via C# and use RotateFlip, as iTextSharp doesn't support mirroring.
if (face.mirrored)
{
// If it's a JPEG, open it with Flux.JPEG.Core, because the internal JPEG decoder (and also LibJpeg.NET) creates weird dithering artifacts with greyscale JPGs, which some of the rage faces are.
if (Path.GetExtension(face.file).ToLower() == ".jpeg" || Path.GetExtension(face.file).ToLower() == ".jpg")
{
FileStream jpegstream = File.OpenRead("images/" + face.file);
FluxJpeg.Core.DecodedJpeg myJpeg = new JpegDecoder(jpegstream).Decode();
// Only use this JPEG decoder if the colorspace is Gray. Otherwise the normal one is just fine.
if (myJpeg.Image.ColorModel.colorspace == FluxJpeg.Core.ColorSpace.Gray)
{
myJpeg.Image.ChangeColorSpace(FluxJpeg.Core.ColorSpace.YCbCr);
faceImage = myJpeg.Image.ToBitmap();
} else
{
faceImage = System.Drawing.Image.FromFile("images/" + face.file);
}
}
else
{
faceImage = System.Drawing.Image.FromFile("images/" + face.file);
}
// Apply mirroring
faceImage.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipX);
pdfImage = Image.GetInstance(faceImage, System.Drawing.Imaging.ImageFormat.Png);
}
else
{
// Just let iTextSharp handle it if no mirroring is required. Will also save space (presumably)
pdfImage = Image.GetInstance("images/" + face.file);
}
pdfImage.ScalePercent(face.scalex * 100, face.scaley * 100);
pdfImage.Rotation = -(float)Math.PI * face.rotation / 180.0f;
pdfImage.SetAbsolutePosition(item.x, (height - item.y) - pdfImage.ScaledHeight);
// Set opacity to proper value
if (face.opacity < 1)
{
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = face.opacity;
cb.SetGState(graphicsState);
}
cb.AddImage(pdfImage);
// Set back to normal
if (face.opacity < 1)
{
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 1f;
cb.SetGState(graphicsState);
}
}
else if (item is DrawImage)
{
DrawImage drawimage = (DrawImage)item;
drawImageZip.AddEntry("drawimage_" + (drawimageindex++).ToString() + ".png", drawimage.imagedata);
System.Drawing.Image pngImage = System.Drawing.Image.FromStream(new MemoryStream(drawimage.imagedata));
Image pdfImage = Image.GetInstance(pngImage, System.Drawing.Imaging.ImageFormat.Png);
// Rotation is NOT to be applied. Ragemaker actually has a bug that causes it to save rotated images in their rotated form, but save the rotation value anyway
// Thus rotating the image by the rotation value will actually rotate them double compared to what they originally looked like
// The irony is that ragemaker *itself* cannot properly load an xml it created with this rotation value, as it will also apply the rotation
// As such, this tool is currently the only way to correctly display that .xml file as it was originally meant to look, not even ragemaker itself can properly load it again.
//pdfImage.Rotation = -(float)Math.PI * item.rotation / 180.0f;
pdfImage.SetAbsolutePosition(item.x, (height - item.y) - pdfImage.ScaledHeight);
// Opacity likewise seems to be baked in, and in fact the opacity value doesn't even exist.
// Implementing it anyway, in case it ever becomes a thing.
if (drawimage.opacity < 1)
{
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = drawimage.opacity;
cb.SetGState(graphicsState);
}
cb.AddImage(pdfImage);
// Set back to normal
if (drawimage.opacity < 1)
{
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 1f;
cb.SetGState(graphicsState);
}
}
else if (item is Text)
{
int padding = 4;
Text text = (Text)item;
// Create template
PdfTemplate xobject = cb.CreateTemplate(text.width, text.height);
// Background color (if set)
if (text.bgOn)
{
Rectangle bgRectangle = new Rectangle(0, 0, text.width, text.height);
System.Drawing.Color bgColor = System.Drawing.ColorTranslator.FromHtml(text.bgColor);
rect.BackgroundColor = new BaseColor(bgColor.R, bgColor.G, bgColor.B, (int)Math.Floor(text.opacity * 255));
xobject.Rectangle(rect);
}
// Create text
Rectangle textangle = new Rectangle(padding, 0, text.width - padding, text.height);
ColumnText ct = new ColumnText(xobject);
ct.SetSimpleColumn(textangle);
Paragraph paragraph = new Paragraph(text.text);
Font myFont = fonts[text.style];
// More specific treatment if it's an AnyFont element which allows the user to select any font and styles, not just the normal 3 presets
// This isn't perfect, as the current FontFinder doesn't indicate whether he actually found an Italic/Bold typeface, hence it's not possible
// to determine whether faux-italic/faux-bold should be applied. Currently it will only work correctly if each used font has a specific typeface
// for the needed styles (bold or italic), otherwise incorrect results.
// TODO Fix, for example let FontFinder return array of strings, one of which is indicating the suffix that was found.
if (text is AnyFontText)
{
AnyFontText anyfont = (AnyFontText)text;
string fontname = anyfont.font;
string fontfile = "";
if (anyfont.bold)
{
fontfile = FontFinder.GetSystemFontFileName(fontname, true, Draw.FontStyle.Bold);
int fontStyle = 0;
if (anyfont.italic)
{
fontStyle |= Font.ITALIC;
}
if (anyfont.underline)
{
fontStyle |= Font.UNDERLINE;
}
myFont = new Font(BaseFont.CreateFont(fontfile, BaseFont.CP1252, BaseFont.EMBEDDED), 100f, fontStyle);
} else if (anyfont.italic)
{
fontfile = FontFinder.GetSystemFontFileName(fontname, true, Draw.FontStyle.Italic);
int fontStyle = 0;
if (anyfont.underline)
{
fontStyle |= Font.UNDERLINE;
}
myFont = new Font(BaseFont.CreateFont(fontfile, BaseFont.CP1252, BaseFont.EMBEDDED), 100f, fontStyle);
} else
{
fontfile = FontFinder.GetSystemFontFileName(fontname, true, Draw.FontStyle.Regular);
int fontStyle = 0;
if (anyfont.underline)
{
fontStyle |= Font.UNDERLINE;
}
myFont = new Font(BaseFont.CreateFont(fontfile, BaseFont.CP1252, BaseFont.EMBEDDED), 100f, fontStyle);
}
}
myFont.Size = text.size;
System.Drawing.Color color = (System.Drawing.Color)(new System.Drawing.ColorConverter()).ConvertFromString(text.color);
myFont.Color = new BaseColor(color.R, color.G, color.B, (int)Math.Floor(text.opacity * 255));
paragraph.Font = myFont;
paragraph.Alignment = text.align == Text.ALIGN.LEFT ? PdfContentByte.ALIGN_LEFT : (text.align == Text.ALIGN.RIGHT ? PdfContentByte.ALIGN_RIGHT : PdfContentByte.ALIGN_CENTER);
paragraph.SetLeading(0, 1.12f);
ct.AddElement(paragraph);
ct.Go();
// Angle to radians
float angle = (float)Math.PI * text.rotation / 180.0f;
// Calculate Bounding Box size for correct placement later
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(new System.Drawing.Rectangle(0, 0, (int)Math.Round(text.width), (int)Math.Round(text.height)));
Matrix translateMatrix = new Matrix();
translateMatrix.RotateAt(text.rotation, new System.Drawing.PointF(text.width / 2, text.height / 2));
gp.Transform(translateMatrix);
var gbp = gp.GetBounds();
float newWidth = gbp.Width, newHeight = gbp.Height;
// Create correct placement
// Background info: I rotate around the center of the text box, thus the center of the text box is what I attempt to place correctly with the initial .Translate()
AffineTransform transform = new AffineTransform();
transform.Translate(item.x + newWidth / 2 - text.width / 2, height - (item.y + newHeight / 2 - text.height / 2) - text.height);
transform.Rotate(-angle, text.width / 2, text.height / 2);
cb.AddTemplate(xobject, transform);
}
index++;
}
if (comic.gridAboveAll && comic.showGrid)
{
DrawGrid(cb, width, height, rows, comic);
}
//document.Add(new Paragraph("Hello World!"));
// Close the document
document.Close();
// Close the writer instance
writer.Close();
// Always close open filehandles explicity
fs.Close();
if (hasDrawImages)
{
drawImageZip.Save();
}
drawImageZip.Dispose();
}
return false;
}
public static void DrawGrid(PdfContentByte cb, int width, int height, int rows, Ragecomic comic)
{
if (!comic.showGrid)
{
return;
}
// Outer box
Rectangle rect = new iTextSharp.text.Rectangle(0, 0, width, height);
//rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER;
rect.Border = iTextSharp.text.Rectangle.BOX;
rect.BorderWidth = 1;
rect.BorderColor = new BaseColor(0, 0, 0);
cb.Rectangle(rect);
int matricesCount = comic.gridLines.GetLength(0);
cb.SetColorStroke(new BaseColor(0, 0, 0));
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0f;
cb.SetGState(graphicsState);
// The first dimension of the array represents "rows" in a sense.
// And the second dimension has 5 values each, representing various gridlines.
// Gridlines row format:
// 0 - bottom left
// 1 - center
// 2 - bottom right
// 3 - left diagonal
// 4 - right diagonal
// diagonals can be 0,1 or 2.
// 0 - disabled
// 1 - bottom left to top right
// 2 - top left to bottom right
for (int i = 0; i < matricesCount; i++)
{
int ipp = i + 1;
// bottom left
if (comic.gridLines[i, 0] > 0) {
cb.MoveTo(0,height-rowHeight*ipp- borderThickness * ipp);
cb.LineTo(borderThickness + rowWidth,height-rowHeight* ipp - borderThickness * ipp);
cb.Stroke();
}
// center vertical
if (comic.gridLines[i, 1] > 0)
{
cb.MoveTo(borderThickness + rowWidth, height - rowHeight * ipp - borderThickness * ipp);
cb.LineTo(borderThickness + rowWidth, height - rowHeight * i - borderThickness * i);
cb.Stroke();
}
// bottom right
if (comic.gridLines[i, 2] > 0)
{
cb.MoveTo(borderThickness + rowWidth, height - rowHeight * ipp - borderThickness * ipp);
cb.LineTo(borderThickness * 2 + rowWidth * 2, height - rowHeight * ipp - borderThickness * ipp);
cb.Stroke();
}
// left: bottom left to top right
if (comic.gridLines[i, 3] == 1)
{
cb.MoveTo(0, height - rowHeight * ipp - borderThickness * ipp);
cb.LineTo(borderThickness + rowWidth, height - rowHeight * i - borderThickness * i);
cb.Stroke();
}
// left: top left to bottom right
else if(comic.gridLines[i, 3] == 2) {
cb.MoveTo(0, height - rowHeight * i - borderThickness * i);
cb.LineTo(borderThickness + rowWidth, height - rowHeight * ipp - borderThickness * ipp);
cb.Stroke();
}
// right: bottom left to top right
if (comic.gridLines[i, 4] == 1)
{
cb.MoveTo(borderThickness+ rowWidth, height - rowHeight * ipp - borderThickness * ipp);
cb.LineTo(borderThickness*2 + rowWidth*2, height - rowHeight * i - borderThickness * i);
cb.Stroke();
}
// right: top left to bottom right
else if (comic.gridLines[i, 4] == 2) {
cb.MoveTo(borderThickness + rowWidth, height - rowHeight * i - borderThickness * i);
cb.LineTo(borderThickness * 2 + rowWidth * 2, height - rowHeight * ipp - borderThickness * ipp);
cb.Stroke();
}
}
// Lastly, the existence of the center gridline in the bottommost row is determined by the panel count
if (comic.panels % 2 == 0) {
int i = matricesCount;
int ipp = i + 1;
// center vertical
cb.MoveTo(borderThickness + rowWidth, height - rowHeight * ipp - borderThickness * ipp);
cb.LineTo(borderThickness + rowWidth, height - rowHeight * i - borderThickness * i);
cb.Stroke();
}
graphicsState.FillOpacity = 1f;
cb.SetGState(graphicsState);
}
}
}