-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathProgram.cs
130 lines (118 loc) · 4.27 KB
/
Program.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
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using NRasterizer.Rasterizer;
namespace NRasterizer.CLI
{
public class NRasterizerProgram
{
private static Rectangle Entire(Bitmap b)
{
return new Rectangle(0, 0, b.Width, b.Height);
}
private static void BlitTo(Raster raster, Bitmap bitmap)
{
var b = bitmap;
BitmapData bitmapData = null;
try
{
bitmapData = b.LockBits(Entire(b), ImageLockMode.WriteOnly, b.PixelFormat);
int si = 0;
IntPtr di = bitmapData.Scan0;
for (int y = 0; y < b.Height; y++)
{
Marshal.Copy(raster.Pixels, si, di, b.Width);
si += raster.Stride;
di = new IntPtr(di.ToInt64() + bitmapData.Stride);
}
}
finally
{
if (bitmapData != null)
{
b.UnlockBits(bitmapData);
}
}
}
private static void Grayscale(Image image)
{
var palette = image.Palette;
for (int i = 0; i < palette.Entries.Length; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
image.Palette = palette;
}
private void DrawSoft(FileInfo fontPath, FileInfo target, string text)
{
const int width = 200;
const int height = 80;
const int Size = 64;
const int Resolution = 72;
var raster = new Raster(width, height, width, 72);
using (var input = fontPath.OpenRead())
{
var typeface = new OpenTypeReader().Read(input);
var rasterizer = new Rasterizer.Rasterizer(raster);
var renderer = new Renderer(typeface, rasterizer);
renderer.Render(0, 0, text, Size, Resolution);
}
using (Bitmap b = new Bitmap(width, height, PixelFormat.Format8bppIndexed))
{
b.SetResolution(Resolution, Resolution);
Grayscale(b);
BlitTo(raster, b);
b.Save(target.FullName, ImageFormat.Png);
}
}
private void DrawGDI(FileInfo fontPath, FileInfo target, string text)
{
const int width = 200;
const int height = 80;
const int Size = 64;
const int Resolution = 72;
using (var input = fontPath.OpenRead())
{
var typeface = new OpenTypeReader().Read(input);
using (Bitmap b = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
b.SetResolution(Resolution, Resolution);
using (var g = Graphics.FromImage(b))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
var rasterizer = new GDIGlyphRasterizer(g, Brushes.Black);
var renderer = new Renderer(typeface, rasterizer);
renderer.Render(0, 0, text, Size, Resolution);
}
b.Save(target.FullName, ImageFormat.Png);
}
}
}
public void Draw(string rasterizerName, FileInfo fontPath, FileInfo target, string text)
{
target.Directory.Create();
if (rasterizerName == "gdi+")
{
DrawGDI(fontPath, target, text);
return;
}
if (rasterizerName == "nrasterizer")
{
DrawSoft(fontPath, target, text);
return;
}
throw new ApplicationException("Unknown rasterizer: " + rasterizerName);
}
public static void Main(string[] args)
{
var rasterizerName = args[0];
var fontPath = new FileInfo(args[1]);
var target = new FileInfo(args[2]);
var text = args[3];
var program = new NRasterizerProgram();
program.Draw(rasterizerName, fontPath, target, text);
}
}
}