-
Notifications
You must be signed in to change notification settings - Fork 3
/
FontPath.cs
591 lines (545 loc) · 24.9 KB
/
FontPath.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Text;
namespace KMZRebuilder
{
public static class FontPath
{
public static PointD[][] SymbolToPath(FontFamily font, char symbol, float size, PointF startPoint, PathOffset offset, bool flipvertical, bool fliphorizontal, float angle)
{
System.Drawing.Drawing2D.GraphicsPath path = new GraphicsPath();
if(font.IsStyleAvailable(FontStyle.Regular))
path.AddString(new string(new char[] { symbol }), font, (int)FontStyle.Regular, size, new PointF(0, 0), StringFormat.GenericDefault);
else if (font.IsStyleAvailable(FontStyle.Bold))
path.AddString(new string(new char[] { symbol }), font, (int)FontStyle.Bold, size, new PointF(0, 0), StringFormat.GenericDefault);
else
path.AddString(new string(new char[] { symbol }), font, (int)FontStyle.Italic, size, new PointF(0, 0), StringFormat.GenericDefault);
if (flipvertical)// if flip
{
Matrix m = new Matrix(1f, 0f, 0f, -1f, 0f, 0f); // flip Y
//m = new Matrix(-1, 0, 0, 1, 0, 0); // flip X
path.Transform(m);
////////////////////////
m = new Matrix();
m.Translate(0, size);
path.Transform(m);
};
if (fliphorizontal)// if flip
{
Matrix m = new Matrix(1f, 0f, 0f, -1f, 0f, 0f); // flip Y
m = new Matrix(-1, 0, 0, 1, 0, 0); // flip X
path.Transform(m);
////////////////////////
m = new Matrix();
m.Translate(size, 0);
path.Transform(m);
};
if (angle != 0)
{
Matrix m = new Matrix();
m.RotateAt(angle, new PointF(startPoint.X + size / 2.0f, startPoint.Y + size / 2.0f));
path.Transform(m);
};
if (true) // move
{
Matrix m = new Matrix();
RectangleF rect = GetBounds(path.PathPoints);
switch (offset)
{
case PathOffset.Center: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.TopMiddle: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top); break;
case PathOffset.TopRight: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top); break;
case PathOffset.RightMiddle: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.BottomRight: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.BottomMiddle: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.BottomLeft: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.LeftMiddle: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.TopLeft: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top); break;
case PathOffset.None: m.Translate(startPoint.X, startPoint.Y); break;
};
path.Transform(m);
};
List<PointD[]> pathXX = new List<PointD[]>();
List<PointD> pathX = new List<PointD>();
for (int i = 0; i < path.PathPoints.Length; i++)
{
if ((path.PathTypes[i] == 0) && (pathX.Count > 0))
{
pathXX.Add(pathX.ToArray());
pathX.Clear();
};
pathX.Add(new PointD(path.PathPoints[i], path.PathTypes[i]));
};
if (pathX.Count > 0) pathXX.Add(pathX.ToArray());
return pathXX.ToArray();
}
public static PointD[][] StringToPath(FontFamily font, string text, float size, PointF startPoint, PathOffset offset, bool flipvertical, bool fliphorizontal, float angle)
{
System.Drawing.Drawing2D.GraphicsPath path = new GraphicsPath();
if(font.IsStyleAvailable(FontStyle.Regular))
path.AddString(text, font, (int)FontStyle.Regular, size, new PointF(0, 0), StringFormat.GenericDefault);
else if (font.IsStyleAvailable(FontStyle.Bold))
path.AddString(text, font, (int)FontStyle.Bold, size, new PointF(0, 0), StringFormat.GenericDefault);
else
path.AddString(text, font, (int)FontStyle.Italic, size, new PointF(0, 0), StringFormat.GenericDefault);
if (flipvertical)// if flip
{
Matrix m = new Matrix(1f, 0f, 0f, -1f, 0f, 0f); // flip Y
//m = new Matrix(-1, 0, 0, 1, 0, 0); // flip X
path.Transform(m);
////////////////////////
m = new Matrix();
m.Translate(0, size);
path.Transform(m);
};
if (fliphorizontal)// if flip
{
Matrix m = new Matrix(1f, 0f, 0f, -1f, 0f, 0f); // flip Y
m = new Matrix(-1, 0, 0, 1, 0, 0); // flip X
path.Transform(m);
////////////////////////
m = new Matrix();
m.Translate(size, 0);
path.Transform(m);
};
if (angle != 0)
{
Matrix m = new Matrix();
m.RotateAt(angle, new PointF(startPoint.X + size / 2.0f, startPoint.Y + size / 2.0f));
path.Transform(m);
};
if (true) // move
{
Matrix m = new Matrix();
RectangleF rect = GetBounds(path.PathPoints);
switch (offset)
{
case PathOffset.Center: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.TopMiddle: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top); break;
case PathOffset.TopRight: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top); break;
case PathOffset.RightMiddle: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.BottomRight: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.BottomMiddle: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.BottomLeft: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.LeftMiddle: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.TopLeft: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top); break;
case PathOffset.None: m.Translate(startPoint.X, startPoint.Y); break;
};
path.Transform(m);
};
List<PointD[]> pathXX = new List<PointD[]>();
List<PointD> pathX = new List<PointD>();
for (int i = 0; i < path.PathPoints.Length; i++)
{
if ((path.PathTypes[i] == 0) && (pathX.Count > 0))
{
pathXX.Add(pathX.ToArray());
pathX.Clear();
};
pathX.Add(new PointD(path.PathPoints[i], path.PathTypes[i]));
};
if (pathX.Count > 0) pathXX.Add(pathX.ToArray());
return pathXX.ToArray();
}
public static PointD[][] StringToPath(FontFamily font, string text, float size, float spacing, PointF startPoint, PathOffset offset, bool flipvertical, bool fliphorizontal, float angle)
{
bool flipvert = flipvertical;
if (fliphorizontal)
flipvert = !flipvert;
PointF rotatePoint = new PointF(startPoint.X + size / 2, startPoint.Y + size / 2);
text = text.Replace("\r", " ").Replace("\n", " ").Replace("\t", " ");
char[] chars = text.ToCharArray();
RectangleF bounds = new RectangleF(0, 0, 0, 0);
System.Drawing.Drawing2D.GraphicsPath PPS = new GraphicsPath();
PointF start = new PointF(0, 0);
foreach (char c in chars)
{
if (bounds.Width > 0)
start = new PointF(start.X + bounds.Width + spacing, start.Y);
if (c == ' ')
continue;
if ((c == '.') || (c == ',') || (c == ':') || (c == ';') || (c == '!') || (c == '?'))
{
start = new PointF(start.X - spacing * 0.7f, start.Y);
};
PointD[][] path = SymbolToPath(font, c, size, start, FontPath.PathOffset.None, flipvert, false, 0);
bounds = GetBounds(path);
foreach (PointD[] pol in path)
PPS.AddPolygon(PointD.ToPointF(pol));
};
if (fliphorizontal)
{
Matrix m = new Matrix();
m.RotateAt(180, rotatePoint);
PPS.Transform(m);
m = new Matrix();
RectangleF rect = GetBounds(PPS.PathPoints);
m.Translate(rect.Width - size / 2, 0);
PPS.Transform(m);
};
if (angle != 0)
{
Matrix m = new Matrix();
m.RotateAt(angle, rotatePoint);
PPS.Transform(m);
};
if (true)// move
{
Matrix m = new Matrix();
RectangleF rect = GetBounds(PPS.PathPoints);
switch (offset)
{
case PathOffset.Center: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.TopMiddle: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top); break;
case PathOffset.TopRight: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top); break;
case PathOffset.RightMiddle: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.BottomRight: m.Translate(startPoint.X - rect.Left - rect.Width, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.BottomMiddle: m.Translate(startPoint.X - rect.Left - rect.Width / 2, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.BottomLeft: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top - rect.Height); break;
case PathOffset.LeftMiddle: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top - rect.Height / 2); break;
case PathOffset.TopLeft: m.Translate(startPoint.X - rect.Left, startPoint.Y - rect.Top); break;
};
PPS.Transform(m);
};
List<PointD[]> pathXX = new List<PointD[]>();
List<PointD> pathX = new List<PointD>();
for (int i = 0; i < PPS.PathPoints.Length; i++)
{
if ((PPS.PathTypes[i] == 0) && (pathX.Count > 0))
{
pathXX.Add(pathX.ToArray());
pathX.Clear();
};
pathX.Add(new PointD(PPS.PathPoints[i], PPS.PathTypes[i]));
};
if (pathX.Count > 0) pathXX.Add(pathX.ToArray());
return pathXX.ToArray();
}
public static RectangleF GetBounds(PointF[][] poitns)
{
float xmin = float.MaxValue;
float xmax = float.MinValue;
float ymin = float.MaxValue;
float ymax = float.MinValue;
foreach (PointF[] pp in poitns)
foreach (PointF p in pp)
{
if (p.X < xmin) xmin = p.X;
if (p.X > xmax) xmax = p.X;
if (p.Y < ymin) ymin = p.Y;
if (p.Y > ymax) ymax = p.Y;
};
return new RectangleF(xmin, ymin, Math.Abs(xmax - xmin), Math.Abs(ymax - ymin));
}
public static RectangleF GetBounds(PointD[][] poitns)
{
float xmin = float.MaxValue;
float xmax = float.MinValue;
float ymin = float.MaxValue;
float ymax = float.MinValue;
foreach (PointD[] pp in poitns)
foreach (PointD p in pp)
{
if (p.X < xmin) xmin = (float)p.X;
if (p.X > xmax) xmax = (float)p.X;
if (p.Y < ymin) ymin = (float)p.Y;
if (p.Y > ymax) ymax = (float)p.Y;
};
return new RectangleF(xmin, ymin, Math.Abs(xmax - xmin), Math.Abs(ymax - ymin));
}
public static RectangleF GetBounds(PointF[] poitns)
{
float xmin = float.MaxValue;
float xmax = float.MinValue;
float ymin = float.MaxValue;
float ymax = float.MinValue;
foreach (PointF p in poitns)
{
if (p.X < xmin) xmin = p.X;
if (p.X > xmax) xmax = p.X;
if (p.Y < ymin) ymin = p.Y;
if (p.Y > ymax) ymax = p.Y;
};
return new RectangleF(xmin, ymin, Math.Abs(xmax - xmin), Math.Abs(ymax - ymin));
}
public static RectangleF GetBounds(PointD[] poitns)
{
float xmin = float.MaxValue;
float xmax = float.MinValue;
float ymin = float.MaxValue;
float ymax = float.MinValue;
foreach (PointD p in poitns)
{
if (p.X < xmin) xmin = (float)p.X;
if (p.X > xmax) xmax = (float)p.X;
if (p.Y < ymin) ymin = (float)p.Y;
if (p.Y > ymax) ymax = (float)p.Y;
};
return new RectangleF(xmin, ymin, Math.Abs(xmax - xmin), Math.Abs(ymax - ymin));
}
public static PointF[][] FlipVetrical(PointF[][] points)
{
RectangleF rect = GetBounds(points);
for (int f = 0; f < points.Length; f++)
for (int s = 0; s < points[f].Length; s++)
points[f][s].Y = rect.Top - points[f][s].Y + rect.Bottom;
return points;
}
public static PointD[][] FlipVetrical(PointD[][] points)
{
RectangleF rect = GetBounds(points);
for (int f = 0; f < points.Length; f++)
for (int s = 0; s < points[f].Length; s++)
points[f][s].Y = rect.Top - points[f][s].Y + rect.Bottom;
return points;
}
public static PointF[] FlipVetrical(PointF[] points)
{
RectangleF rect = GetBounds(points);
for (int f = 0; f < points.Length; f++)
points[f].Y = rect.Top - points[f].Y + rect.Bottom;
return points;
}
public static PointD[] FlipVetrical(PointD[] points)
{
RectangleF rect = GetBounds(points);
for (int f = 0; f < points.Length; f++)
points[f].Y = rect.Top - points[f].Y + rect.Bottom;
return points;
}
public static PointF[][] FlipHorizontal(PointF[][] points)
{
RectangleF rect = GetBounds(points);
for (int f = 0; f < points.Length; f++)
for (int s = 0; s < points[f].Length; s++)
points[f][s].X = rect.Right - points[f][s].X + rect.Left;
return points;
}
public static PointD[][] FlipHorizontal(PointD[][] points)
{
RectangleF rect = GetBounds(points);
for (int f = 0; f < points.Length; f++)
for (int s = 0; s < points[f].Length; s++)
points[f][s].X = rect.Right - points[f][s].X + rect.Left;
return points;
}
public static PointF[] FlipHorizontal(PointF[] points)
{
RectangleF rect = GetBounds(points);
for (int f = 0; f < points.Length; f++)
points[f].X = rect.Right - points[f].X + rect.Left;
return points;
}
public static PointD[] FlipHorizontal(PointD[] points)
{
RectangleF rect = GetBounds(points);
for (int f = 0; f < points.Length; f++)
points[f].X = rect.Right - points[f].X + rect.Left;
return points;
}
public static string[] PathToLineString(PointF[][] points)
{
List<string> result = new List<string>();
foreach (PointF[] path in points)
result.Add(PathToLineString(path));
return result.ToArray();
}
public static string[] PathToLineString(PointD[][] points)
{
List<string> result = new List<string>();
foreach (PointD[] path in points)
result.Add(PathToLineString(path));
return result.ToArray();
}
public static string PathToLineString(PointF[] points)
{
string result = "";
foreach (PointF p in points)
{
if (result.Length > 0) result += " ";
result += String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0},{1},{2}", p.X, p.Y, 0);
};
result += String.Format(System.Globalization.CultureInfo.InvariantCulture, " {0},{1},{2}", points[0].X, points[0].Y, 0);
return result;
}
public static string PathToLineString(PointD[] points)
{
string result = "";
foreach (PointD p in points)
{
if (result.Length > 0) result += " ";
result += String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0},{1},{2}", p.X, p.Y, 0);
};
result += String.Format(System.Globalization.CultureInfo.InvariantCulture, " {0},{1},{2}", points[0].X, points[0].Y, 0);
return result;
}
public static string PathToPolygonString(PointF[] points)
{
string result = "";
foreach (PointF p in points)
{
if (result.Length > 0) result += " ";
result += String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0},{1},{2}", p.X, p.Y, 0);
};
return result;
}
public static string PathToPolygonString(PointD[] points)
{
string result = "";
foreach (PointD p in points)
{
if (result.Length > 0) result += " ";
result += String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0},{1},{2}", p.X, p.Y, 0);
};
return result;
}
public static string[] PathToPolygonString(PointF[][] points)
{
List<string> result = new List<string>();
foreach (PointF[] path in points)
result.Add(PathToPolygonString(path));
return result.ToArray();
}
public static string[] PathToPolygonString(PointD[][] points)
{
List<string> result = new List<string>();
foreach (PointD[] path in points)
result.Add(PathToPolygonString(path));
return result.ToArray();
}
public class PointD
{
public double X;
public double Y;
public byte Type;
public PointD() { }
public PointD(int X, int Y)
{
this.X = X;
this.Y = Y;
}
public PointD(int X, int Y, byte Type)
{
this.X = X;
this.Y = Y;
this.Type = Type;
}
public PointD(float X, float Y)
{
this.X = X;
this.Y = Y;
}
public PointD(float X, float Y, byte Type)
{
this.X = X;
this.Y = Y;
this.Type = Type;
}
public PointD(double X, double Y)
{
this.X = X;
this.Y = Y;
}
public PointD(double X, double Y, byte Type)
{
this.X = X;
this.Y = Y;
this.Type = Type;
}
public PointD(Point point)
{
this.X = point.X;
this.Y = point.Y;
}
public PointD(Point point, byte Type)
{
this.X = point.X;
this.Y = point.Y;
this.Type = Type;
}
public PointD(PointF point)
{
this.X = point.X;
this.Y = point.Y;
}
public PointD(PointF point, byte Type)
{
this.X = point.X;
this.Y = point.Y;
this.Type = Type;
}
public PointF PointF
{
get
{
return new PointF((float)X, (float)Y);
}
set
{
this.X = value.X;
this.Y = value.Y;
}
}
public static PointF ToPointF(PointD point)
{
return point.PointF;
}
public static PointF[] ToPointF(PointD[] points)
{
PointF[] result = new PointF[points.Length];
for (int i = 0; i < result.Length; i++)
result[i] = points[i].PointF;
return result;
}
}
public enum PathOffset : byte
{
Center = 0,
TopMiddle = 1,
TopRight = 2,
RightMiddle = 3,
BottomRight = 4,
BottomMiddle = 5,
BottomLeft = 6,
LeftMiddle = 7,
TopLeft = 8,
None = 255
}
}
public static class FontPathSample
{
public static Image Sample()
{
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(@"Fonts\PTZ55F.ttf");
PointF startPoint = new PointF(100f, 100f);
List<FontPath.PointD[]> pathes = new List<FontPath.PointD[]>();
pathes.AddRange((FontPath.StringToPath(pfc.Families[0], "Áäûùü!", 75f, startPoint, FontPath.PathOffset.TopLeft, false, false, 0f)));
//pathes.AddRange((FontPath.StringToPath(pfc.Families[0], "Aõòóíã!!!", 0.01f, 0.01f * 0.12f, startPoint, FontPath.PathOffset.BottomLeft, false, false, 0f)));
//pathes.AddRange((FontPath.SymbolToPath(pfc.Families[0], 'G', 150, startPoint, FontPath.PathOffset.None, false, false, -0f)));
string[] LineStrings = FontPath.PathToLineString(pathes.ToArray());
Image im = new Bitmap(600, 600);
using (Graphics g = Graphics.FromImage(im))
{
Pen pen = new Pen(Brushes.Black, 2);
Pen pen2 = new Pen(Brushes.Red, 1);
g.DrawLine(pen2, new PointF(0, startPoint.Y), new PointF(im.Width, startPoint.Y));
g.DrawLine(pen2, new PointF(startPoint.X, 0), new PointF(startPoint.X, im.Height));
foreach (FontPath.PointD[] path in pathes)
{
PointF[] p = FontPath.PointD.ToPointF(path);
//g.FillPolygon(Brushes.Red, p);
g.DrawPolygon(pen, p);
//g.DrawLines(pen, p);
//g.DrawLine(pen, p[p.Length-1], p[0]);
};
};
return im;
}
}
}