-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotter.cs
441 lines (371 loc) · 12.7 KB
/
Plotter.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
using SkiaSharp;
using SkiaSharpOpenGLBenchmark.css;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace SkiaSharpOpenGLBenchmark
{
// Type of plot operation
public enum PlotOperationType
{
PLOT_OP_TYPE_NONE = 0, // No operation
PLOT_OP_TYPE_SOLID, // Solid colour
PLOT_OP_TYPE_DOT, // Dotted plot
PLOT_OP_TYPE_DASH, // Dashed plot
}
// plot_style.h:76
// Plot style for stroke/fill plotters
public struct PlotStyle
{
public PlotOperationType StrokeType; // Stroke plot type
public int StrokeWidth; // Width of stroke, in pixels
public uint StrokeColour; // Colour of stroke
public PlotOperationType FillType; // Fill plot type
public uint FillColour; // Colour of fill
}
// plot_style.h:88
// Generic font family type
public enum PlotFontGenericFamily
{
PLOT_FONT_FAMILY_SANS_SERIF = 0,
PLOT_FONT_FAMILY_SERIF,
PLOT_FONT_FAMILY_MONOSPACE,
PLOT_FONT_FAMILY_CURSIVE,
PLOT_FONT_FAMILY_FANTASY,
PLOT_FONT_FAMILY_COUNT // Number of generic families
}
// Font plot flags
public enum PlotFontFlags
{
FONTF_NONE = 0,
FONTF_ITALIC = 1,
FONTF_OBLIQUE = 2,
FONTF_SMALLCAPS = 4,
}
// plot_style.h:111
public struct PlotFontStyle
{
public string[] Families;
public PlotFontGenericFamily Family; // Generic family to plot with
public int Size; // Font size, in pt
public int Weight; // Font weight: value in range [100,900] as per CSS
public PlotFontFlags Flags; // Font flags
public uint Background; // Background colour to blend to, if appropriate
public uint Foreground; // Colour of text
}
// frontends/windows/plot.c
public class Plotter
{
public const int PlotStyleRadix = 10; // 22:10 fixed point
public const int PlotStyleScale = 1 << PlotStyleRadix; // Scaling factor for plot styles
public SKSurface Surface;
public Plotter()
{
// Create surface used for plotting
SKImageInfo info = new SKImageInfo(1000, 1000, SKColorType.Rgba8888);
Surface = SKSurface.Create(info);
// Test output
Surface.Canvas.Clear(SKColor.Parse("#c0c0c0"));
var paint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = new SKColor(0xffccddee)
};
var rect = new SKRect(100,100,110,110);
Surface.Canvas.DrawRect(rect, paint);
/*var paint2 = new SKPaint()
{
TextSize = 16,
Color = SKColors.Yellow,
IsAntialias = true,
Typeface = SKTypeface.FromFamilyName(
"Arial",
SKFontStyleWeight.Normal,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright)
};
Surface.Canvas.DrawText("Privet!", 100, 100, paint2);*/
}
public void Rectangle(PlotStyle pstyle, Rect r)
{
//Log.Unimplemented($"({r.x0},{r.y0})-({r.x1},{r.y1})");
var paint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = new SKColor((byte)(pstyle.StrokeColour & 0xff),
(byte)((pstyle.StrokeColour & 0xff00) >> 8),
(byte)((pstyle.StrokeColour & 0xff0000) >> 16))
};
var rect = new SKRect(r.x0, r.y0, r.x1, r.y1);
Surface.Canvas.DrawRect(rect, paint);
}
public void Line()
{
Log.Unimplemented();
}
public void Polygon()
{
Log.Unimplemented();
}
public void Clip(Rect r)
{
Log.Unimplemented($"({r.x0},{r.y0})-({r.x1},{r.y1})");
SKRect rect = new SKRect(r.x0, r.y0, r.x1, r.y1);
//Surface.Canvas.ClipRect(rect);
}
// text()
// plot.c:984
public void Text(PlotFontStyle fstyle, int x, int y, string text)
{
Log.Unimplemented($"Draw text '{text}' at ({x},{y})");
//(style->flags & FONTF_ITALIC) ? TRUE : FALSE
//int nHeight = -MulDiv(style->size, GetDeviceCaps(hdc, LOGPIXELSY), 72 * PLOT_STYLE_SCALE);
int dpi = 144;
float nHeight = fstyle.Size / PlotStyleScale * dpi / 72;
var paint2 = new SKPaint()
{
TextSize = nHeight,
// Color = new SKColor((byte)(fstyle.Foreground & 0xff),
// (byte)((fstyle.Foreground & 0xff00) >> 8),
// (byte)((fstyle.Foreground & 0xff0000) >> 16)),
Color = new SKColor((byte)((fstyle.Foreground & 0xff0000) >> 16),
(byte)((fstyle.Foreground & 0xff00) >> 8),
(byte)(fstyle.Foreground & 0xff)),
IsAntialias = true,
Typeface = SKTypeface.FromFamilyName(
"Arial",
SKFontStyleWeight.Normal,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright)
};
Surface.Canvas.DrawText(text, x, y, paint2);
}
public void Disc()
{
Log.Unimplemented();
}
public void Arc()
{
Log.Unimplemented();
}
public void Bitmap()
{
Log.Unimplemented();
}
public void Path()
{
Log.Unimplemented();
}
// win32_font_width()
// frontends/windows/font.c:186
public void GetFontWidth(PlotFontStyle fstyle, string textSource, int length, ref int width)
{
int dpi = 144;
float nHeight = fstyle.Size / PlotStyleScale * dpi / 72;
string text;
// Caller may want to measure only part of the string
if (textSource.Length != length)
text = textSource.Substring(0, length);
else
text = textSource;
// Measure text
var paint3 = new SKPaint()
{
TextSize = nHeight,
IsAntialias = true,
Typeface = SKTypeface.FromFamilyName(
"Arial",
SKFontStyleWeight.Normal,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright)
};
SKFontMetrics metrics;
paint3.GetFontMetrics(out metrics);
SKRect bounds = new SKRect();
float textWidth = paint3.MeasureText(text, ref bounds);
width = (int)Math.Round(textWidth);
// HACK
/*
switch (text)
{
case "some text ":
width = 103;
break;
case " ":
width = 7;
break;
case "and link":
width = 85;
break;
case "and another p":
width = 151;
break;
default:
break;
}*/
Log.Print(LogChannel.Layout, $"Font width of '{text}' size {fstyle.Size} is {width}");
// Size = 0x3000
// "some tex", w = 103
// " ", w = 7
// "and link", w = 85
// "and another p", w = 151
}
// font.c:244 win32_font_position()
void GetFontPosition(PlotFontStyle fstyle, string text, int maxWidth, ref int char_offset, ref int actual_x)
{
int dpi = 144;
float nHeight = fstyle.Size / PlotStyleScale * dpi / 72;
//var length = text.Length;
var paint = new SKPaint()
{
TextSize = nHeight,
IsAntialias = true,
Typeface = SKTypeface.FromFamilyName(
"Arial",
SKFontStyleWeight.Normal,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright)
};
// 1.
// offset = max characters fitting into "x" width
// s = dimensions of string (s.cx is used)
int charCount = (int)paint.BreakText(text, maxWidth);
// 2. Compute width and height of "text" string of length "offset"
// actual_x = computed width
// char_offset = offset
float textWidth = paint.MeasureText(text.Substring(0, charCount));
actual_x = (int)Math.Round(textWidth);
char_offset = (int)charCount;
}
// font.c:302 win32_font_split()
public void SplitText(PlotFontStyle fstyle, string text, int x, ref int offset, ref int actual_x)
{
int c_off;
// get the offset into teh string on the proposed position
GetFontPosition(fstyle, text, x, ref offset, ref actual_x);
// return the whole string fits in the proposed length
if (offset == text.Length)
return;
c_off = offset;
// walk backwards through string looking for space to break on
while ((text[offset] != ' ') &&
offset > 0)
{
offset--;
}
// walk forwards through string looking for space if back failed
if (offset == 0)
{
offset = c_off;
while ((offset < text.Length) &&
(text[offset] != ' '))
{
offset++;
}
}
// find the actual string width of the break
GetFontWidth(fstyle, text, offset, ref actual_x);
//Log.Print(LogChannel.Text, DEEPDEBUG,
//"ret %d Split %u chars at %ipx: Split at char %i (%ipx) - %.*s",
//res, length, x, *offset, *actual_x, *offset, string);
}
// Helpers
// font.c:39
/**
* Map a generic CSS font family to a generic plot font family
*
* \param css Generic CSS font family
* \return Plot font family
*/
public static PlotFontGenericFamily FontGenericFamily(CssFontFamilyEnum css)
{
PlotFontGenericFamily plot;
switch (css) {
case CssFontFamilyEnum.CSS_FONT_FAMILY_SERIF:
plot = PlotFontGenericFamily.PLOT_FONT_FAMILY_SERIF;
break;
case CssFontFamilyEnum.CSS_FONT_FAMILY_MONOSPACE:
plot = PlotFontGenericFamily.PLOT_FONT_FAMILY_MONOSPACE;
break;
case CssFontFamilyEnum.CSS_FONT_FAMILY_CURSIVE:
plot = PlotFontGenericFamily.PLOT_FONT_FAMILY_CURSIVE;
break;
case CssFontFamilyEnum.CSS_FONT_FAMILY_FANTASY:
plot = PlotFontGenericFamily.PLOT_FONT_FAMILY_FANTASY;
break;
case CssFontFamilyEnum.CSS_FONT_FAMILY_SANS_SERIF:
default:
plot = PlotFontGenericFamily.PLOT_FONT_FAMILY_SANS_SERIF;
break;
}
return plot;
}
// font.c:71
/**
* Map a CSS font weight to a plot weight value
*
* \param css CSS font weight
* \return Plot weight
*/
public static int FontWeight(CssFontWeightEnum css)
{
int weight;
switch (css) {
case CssFontWeightEnum.CSS_FONT_WEIGHT_100:
weight = 100;
break;
case CssFontWeightEnum.CSS_FONT_WEIGHT_200:
weight = 200;
break;
case CssFontWeightEnum.CSS_FONT_WEIGHT_300:
weight = 300;
break;
case CssFontWeightEnum.CSS_FONT_WEIGHT_400:
case CssFontWeightEnum.CSS_FONT_WEIGHT_NORMAL:
default:
weight = 400;
break;
case CssFontWeightEnum.CSS_FONT_WEIGHT_500:
weight = 500;
break;
case CssFontWeightEnum.CSS_FONT_WEIGHT_600:
weight = 600;
break;
case CssFontWeightEnum.CSS_FONT_WEIGHT_700:
case CssFontWeightEnum.CSS_FONT_WEIGHT_BOLD:
weight = 700;
break;
case CssFontWeightEnum.CSS_FONT_WEIGHT_800:
weight = 800;
break;
case CssFontWeightEnum.CSS_FONT_WEIGHT_900:
weight = 900;
break;
}
return weight;
}
// font.c:119
/**
* Map a CSS font style and font variant to plot font flags
*
* \param style CSS font style
* \param variant CSS font variant
* \return Computed plot flags
*/
public static PlotFontFlags FontFlags(CssFontStyleEnum style, CssFontVariantEnum variant)
{
PlotFontFlags flags = PlotFontFlags.FONTF_NONE;
if (style == CssFontStyleEnum.CSS_FONT_STYLE_ITALIC)
flags |= PlotFontFlags.FONTF_ITALIC;
else if (style == CssFontStyleEnum.CSS_FONT_STYLE_OBLIQUE)
flags |= PlotFontFlags.FONTF_OBLIQUE;
if (variant == CssFontVariantEnum.CSS_FONT_VARIANT_SMALL_CAPS)
flags |= PlotFontFlags.FONTF_SMALLCAPS;
return flags;
}
}
}