forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
827 lines (710 loc) · 25.6 KB
/
main.cpp
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//--------------------------------------------------------------------------------------
// main.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "GGO_Glyph.h"
#include "FrontPanel\RasterFont.h"
#include "CommandLineHelpers.h"
#include <conio.h>
using namespace ATG;
namespace
{
enum OPTIONS : uint32_t
{
OPT_TYPEFACE = 1,
OPT_HEIGHT,
OPT_WEIGHT,
OPT_ITALIC,
OPT_UNDERLINE,
OPT_STRIKEOUT,
OPT_CHARSET,
OPT_QUALITY,
OPT_PITCH,
OPT_FAMILY,
OPT_GGO_DEPTH,
OPT_CHARACTER_REGION,
OPT_OUTPUT_FILENAME,
OPT_DEFAULT_GLYPH,
OPT_OVERWRITE,
OPT_MAX
};
static_assert(OPT_MAX <= 64, "Options must fit into a uint64_t bitfield");
const SValue g_Options[] =
{
{ L"tf", OPT_TYPEFACE },
{ L"h", OPT_HEIGHT },
{ L"w", OPT_WEIGHT },
{ L"it", OPT_ITALIC },
{ L"ul", OPT_UNDERLINE },
{ L"so", OPT_STRIKEOUT },
{ L"cs", OPT_CHARSET },
{ L"q", OPT_QUALITY },
{ L"p", OPT_PITCH },
{ L"fa", OPT_FAMILY },
{ L"d", OPT_GGO_DEPTH },
{ L"cr", OPT_CHARACTER_REGION },
{ L"of", OPT_OUTPUT_FILENAME },
{ L"ow", OPT_OVERWRITE },
{ L"dg", OPT_DEFAULT_GLYPH },
};
#define DEF_WEIGHT(wght) { L#wght, FW_ ## wght }
const SValue g_Weights[] =
{
DEF_WEIGHT(DONTCARE),
DEF_WEIGHT(THIN),
DEF_WEIGHT(EXTRALIGHT),
DEF_WEIGHT(LIGHT),
DEF_WEIGHT(NORMAL),
DEF_WEIGHT(MEDIUM),
DEF_WEIGHT(SEMIBOLD),
DEF_WEIGHT(BOLD),
DEF_WEIGHT(EXTRABOLD),
DEF_WEIGHT(HEAVY),
DEF_WEIGHT(ULTRALIGHT),
DEF_WEIGHT(REGULAR),
DEF_WEIGHT(DEMIBOLD),
DEF_WEIGHT(ULTRABOLD),
DEF_WEIGHT(BLACK)
};
#undef DEF_WEIGHT
#define DEF_CHARSET(chrst) { L#chrst, chrst ## _CHARSET }
const SValue g_Charsets[] =
{
DEF_CHARSET(ANSI),
DEF_CHARSET(DEFAULT),
DEF_CHARSET(SYMBOL),
DEF_CHARSET(SHIFTJIS),
DEF_CHARSET(HANGEUL),
DEF_CHARSET(HANGUL),
DEF_CHARSET(GB2312),
DEF_CHARSET(CHINESEBIG5),
DEF_CHARSET(OEM),
DEF_CHARSET(JOHAB),
DEF_CHARSET(HEBREW),
DEF_CHARSET(ARABIC),
DEF_CHARSET(GREEK),
DEF_CHARSET(TURKISH),
DEF_CHARSET(VIETNAMESE),
DEF_CHARSET(THAI),
DEF_CHARSET(EASTEUROPE),
DEF_CHARSET(RUSSIAN),
DEF_CHARSET(MAC),
DEF_CHARSET(BALTIC)
};
#undef DEF_CHARSET
#define DEF_QUALITY(qlty) { L#qlty, qlty ## _QUALITY }
const SValue g_Qualities[] =
{
DEF_QUALITY(ANTIALIASED),
DEF_QUALITY(CLEARTYPE),
DEF_QUALITY(DEFAULT),
DEF_QUALITY(DRAFT),
DEF_QUALITY(NONANTIALIASED),
DEF_QUALITY(PROOF)
};
#undef DEF_QUALITY
#define DEF_PITCH(ptch) { L#ptch, ptch ## _PITCH}
const SValue g_Pitches[] =
{
DEF_PITCH(DEFAULT),
DEF_PITCH(FIXED),
DEF_PITCH(VARIABLE)
};
#undef DEF_PITCH
#define DEF_FAMILY(fam) { L#fam, FF_ ## fam }
const SValue g_Families[] =
{
DEF_FAMILY(DECORATIVE),
DEF_FAMILY(DONTCARE),
DEF_FAMILY(MODERN),
DEF_FAMILY(ROMAN),
DEF_FAMILY(SCRIPT),
DEF_FAMILY(SWISS)
};
#undef DEF_FAMILY
#define DEF_GGO_DEPTH(ggo) { L#ggo, GGO_ ## ggo ## _BITMAP }
const SValue g_GGO_Depths[] =
{
{ L"1BPP", GGO_BITMAP },
DEF_GGO_DEPTH(GRAY2),
DEF_GGO_DEPTH(GRAY4),
DEF_GGO_DEPTH(GRAY8)
};
#undef DEF_GGO_DEPTH
bool ParseDefaultGlyph(const wchar_t *charCode, wchar_t &defaultGlyph, std::vector<WCRANGE> ®ions)
{
const wchar_t *hexCode = charCode;
// Match and consume the hex prefix '0x'
if (_wcsnicmp(L"0x", hexCode, 2) != 0)
{
wprintf(L"Expected a hexidecimal character code specification\n");
return false;
}
hexCode += 2;
unsigned code = 0;
// Scan the hex value
if (swscanf_s(hexCode, L"%lx", &code) != 1)
{
wprintf(L"Expected a hexidecimal value following the prefix ('0x')\n");
return false;
}
hexCode = ConsumeDigits(hexCode);
if (code > 0xFFFF)
{
wprintf(L"The hexidecimal value of the character code specification is too large (0x%X)\n", code);
return false;
}
if (*hexCode)
{
wprintf(L"Unexpected characters following the character code specification (%ls).\n", hexCode);
return false;
}
defaultGlyph = code;
WCRANGE range = { wchar_t(code), 1 };
regions.push_back(range);
return true;
}
void PrintLogo()
{
wprintf(L"Microsoft (R) Raster Font Generator for for Xbox One\n");
wprintf(L"Copyright (C) Microsoft Corp. All rights reserved.\n");
#ifdef _DEBUG
wprintf(L"*** Debug build ***\n");
#endif
wprintf(L"\n");
}
void PrintUsage()
{
PrintLogo();
wprintf(L"Usage: rasterfontgen <options> \n\n");
wprintf(L" -tf <string> Name of the font typeface\n");
wprintf(L" -h <number> [REQUIRED] Height of the font's character cell in logical units\n");
wprintf(L" -w <weight> Font weight\n");
wprintf(L" -it Specifies an italic font\n");
wprintf(L" -ul Specifies an underlined font\n");
wprintf(L" -so Specifies a strikeout font\n");
wprintf(L" -cs <charset> The character set\n");
wprintf(L" -q <quality> The output quality\n");
wprintf(L" -p <pitch> The pitch of the font\n");
wprintf(L" -fa <family> The family of the font\n");
wprintf(L" -d <depth> The GetGlyphOutline (GGO) pixel depth\n");
wprintf(L" -cr:<range> Character region. Specifies a range of unicode code points to include in the font.\n");
wprintf(L" Examples: -cr:a-z -cr:0x1200-0x1250 -cr:0x1234\n");
wprintf(L" -of <filename> [REQUIRED] Name of the output file\n");
wprintf(L" -ow Overwrite the output file if it already exists\n");
wprintf(L" -dg <caracter code> Specifies the default glyph to use when the font does not support a particular character code\n");
wprintf(L" Examples: -dg:0x1234\n");
const wchar_t wgts[] = L"\n <weight>: ";
wprintf(wgts);
PrintTable(_countof(wgts) - 2, g_Weights);
const wchar_t chsts[] = L"\n <charset>: ";
wprintf(chsts);
PrintTable(_countof(chsts) - 2, g_Charsets);
const wchar_t qlties[] = L"\n <quality>: ";
wprintf(qlties);
PrintTable(_countof(qlties) - 2, g_Qualities);
const wchar_t ptchs[] = L"\n <pitch>: ";
wprintf(ptchs);
PrintTable(_countof(ptchs) - 2, g_Pitches);
const wchar_t fams[] = L"\n <family>: ";
wprintf(fams);
PrintTable(_countof(fams) - 2, g_Families);
const wchar_t dpths[] = L"\n <depth>: ";
wprintf(dpths);
PrintTable(_countof(dpths) - 2, g_GGO_Depths);
}
void GetGlyphRanges(HDC hDC, std::vector<WCRANGE> &ranges)
{
std::unique_ptr<uint8_t[]> glyphSetBytes;
GLYPHSET *glyphSet = nullptr;
{
auto size = GetFontUnicodeRanges(hDC, nullptr);
glyphSetBytes = std::make_unique<uint8_t[]>(size);
glyphSet = reinterpret_cast<GLYPHSET*>(glyphSetBytes.get());
if (!GetFontUnicodeRanges(hDC, glyphSet))
{
throw std::exception("Unable to get unicode ranges for font.");
}
for (unsigned r = 0; r < glyphSet->cRanges; ++r)
{
ranges.push_back(glyphSet->ranges[r]);
}
}
}
bool IsCodePointInRange(wchar_t codePoint, const WCRANGE &range)
{
wchar_t firstCharacter = range.wcLow;
wchar_t lastCharacter = firstCharacter + range.cGlyphs - 1;
return (codePoint >= firstCharacter && codePoint <= lastCharacter);
}
bool FilterKerningPair(const KERNINGPAIR &pair, const std::vector<WCRANGE> &ranges)
{
bool firstInRange = false;
bool secondInRange = false;
for (unsigned r = 0; r < ranges.size(); ++r)
{
firstInRange = firstInRange || IsCodePointInRange(pair.wFirst, ranges[r]);
secondInRange = secondInRange || IsCodePointInRange(pair.wSecond, ranges[r]);
if (firstInRange && secondInRange)
{
return true;
}
}
return false;
}
bool FilterCodePoint(wchar_t codePoint, const std::vector<WCRANGE> &ranges)
{
for(unsigned r = 0; r < ranges.size(); ++r)
{
if (IsCodePointInRange(codePoint, ranges[r]))
{
return true;
}
}
return false;
}
void Create_GGO_Glyphs(
LOGFONT *logfont,
unsigned ggoDepth,
const std::vector<WCRANGE> &requestedRanges,
std::vector<KERNINGPAIR> &kerningPairs,
std::vector<GGO_Glyph> &ggoGlyphs,
wchar_t requestedDefaultGlyph)
{
// Set up a memory device context for laying out the glyph sheet
HDC hDC = GetDC(nullptr);
HDC memDC = CreateCompatibleDC(hDC);
HFONT font = CreateFontIndirect(logfont);
if (!font)
{
throw std::exception("Could not create font.");
}
SelectObject(memDC, font);
wchar_t defaultGlyph = requestedDefaultGlyph;
// Check to see if it is a TrueType font and early out if not
{
TEXTMETRIC metrics = {};
if (!GetTextMetrics(memDC, &metrics))
{
throw std::exception("Could not get text metrics.");
}
bool isTrueType = (metrics.tmPitchAndFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE;
if (!isTrueType)
{
throw std::exception("Requested font is not a TrueType font");
}
if (requestedDefaultGlyph == '\0')
{
requestedDefaultGlyph = metrics.tmDefaultChar;
}
}
// Get the supported Unicode ranges for the font
std::vector<WCRANGE> supportedRanges;
GetGlyphRanges(memDC, supportedRanges);
// Get the kerning pairs for the font
{
unsigned numPairs = GetKerningPairs(memDC, 0, nullptr);
std::unique_ptr<KERNINGPAIR[]> kerns = std::make_unique<KERNINGPAIR[]>(numPairs);
auto test = GetKerningPairs(memDC, numPairs, kerns.get());
if (numPairs != test)
{
throw std::exception("Unable to get kerning pairs for the font.");
}
for (unsigned i = 0; i < numPairs; ++i)
{
KERNINGPAIR &pair = kerns[i];
if (requestedRanges.size() == 0)
{
if (FilterKerningPair(pair, supportedRanges))
{
kerningPairs.push_back(pair);
}
}
else
{
if (FilterKerningPair(pair, requestedRanges) && FilterKerningPair(pair, supportedRanges))
{
kerningPairs.push_back(pair);
}
}
}
}
// Create the GGO_Glyphs for the font
{
std::set<wchar_t> includedCodePoints;
includedCodePoints.insert(defaultGlyph);
if (requestedRanges.size() == 0)
{
for (int r = 0; r < supportedRanges.size(); ++r)
{
const WCRANGE &range = supportedRanges[r];
wchar_t firstCharacter = range.wcLow;
wchar_t lastCharacter = range.wcLow + range.cGlyphs - 1;
for (wchar_t wc = firstCharacter; wc <= lastCharacter; ++wc)
{
includedCodePoints.insert(wc);
}
}
}
else
{
for (int rr = 0; rr < requestedRanges.size(); ++rr)
{
const WCRANGE &range = requestedRanges[rr];
wchar_t firstCharacter = range.wcLow;
wchar_t lastCharacter = range.wcLow + range.cGlyphs - 1;
for (wchar_t wc = firstCharacter; wc <= lastCharacter; ++wc)
{
if (FilterCodePoint(wc, supportedRanges))
{
includedCodePoints.insert(wc);
}
else
{
wprintf(L"The code point, 0x%04x, is not supported by the font and will not be included.\n", wc);
}
}
}
}
for (auto itr = includedCodePoints.begin(); itr != includedCodePoints.end(); ++itr)
{
ggoGlyphs.emplace_back(memDC, *itr, ggoDepth);
}
}
DeleteObject(font);
DeleteObject(memDC);
}
size_t CreatePixelBuffer(const std::vector<GGO_Glyph> &ggoGlyphs, uint8_t *pixels, size_t pixelBufferSize)
{
size_t pixelByteCount = 0;
// Allocate the buffer for the glyph pixels
{
for (int i = 0; i < ggoGlyphs.size(); ++i)
{
auto metrics = ggoGlyphs[i].GetMetrics();
pixelByteCount += GetStorageSize<uint8_t>(metrics.gmBlackBoxX * metrics.gmBlackBoxY);
}
if (pixels == nullptr || pixelByteCount > pixelBufferSize)
{
return pixelByteCount;
}
memset(pixels, 0, pixelBufferSize);
}
// Copy the glyphs into the pixel buffer
{
uint8_t *pxls = pixels;
for (int i = 0; i < ggoGlyphs.size(); ++i)
{
unsigned pixelIdx = 0;
ggoGlyphs[i].ForEachPixel([&](unsigned col, unsigned row, uint8_t clr) {
unsigned shift = 7 - (pixelIdx % 8);
pxls[pixelIdx / 8] |= (!!clr) << shift;
pixelIdx++;
});
auto metrics = ggoGlyphs[i].GetMetrics();
pxls += GetStorageSize<uint8_t>(metrics.gmBlackBoxX * metrics.gmBlackBoxY);
}
}
return pixelByteCount;
}
void ComputeAscentDescent(
const std::vector<GGO_Glyph> &ggoGlyphs,
unsigned &effectiveAscent,
unsigned &effectiveDescent)
{
effectiveAscent = 0;
effectiveDescent = 0;
// Compute the effective ascent and effective descent based on the actual glyphs
{
for (int i = 0; i < ggoGlyphs.size(); ++i)
{
auto metrics = ggoGlyphs[i].GetMetrics();
int ascent = metrics.gmptGlyphOrigin.y;
if (ascent > 0)
{
// Positive vertical coordinate places the upper left corner of the glyph's
// "black box" above the glyph base-line.
effectiveAscent = std::max(effectiveAscent, unsigned(ascent));
}
// Compute the descent for the character as a signed vertical distance from the
// lower-left corner of the glyph's "black box" to the glyph base-line.
// A negative value indicates that nothing is drawn below the base-line.
int descent = metrics.gmBlackBoxY - metrics.gmptGlyphOrigin.y;
if (descent > 0)
{
effectiveDescent = std::max(effectiveDescent, unsigned(descent));
}
}
++effectiveAscent;
}
}
} // ANONYMOUS namespace
int wmain(int argc, wchar_t *argv[])
{
// Parameters and defaults
const wchar_t *typeFace = nullptr;
unsigned fontHeight = 0;
unsigned fontWeight = FW_DONTCARE;
bool useItalic = false;
bool useUnderline = false;
bool useStrikeOut = false;
unsigned charset = DEFAULT_CHARSET;
unsigned quality = DEFAULT_QUALITY;
unsigned pitch = DEFAULT_PITCH;
unsigned family = FF_DONTCARE;
unsigned ggoDepth = GGO_BITMAP;
const wchar_t *outputFilename = nullptr;
bool overwriteOutputFile = false;
wchar_t defaultGlyph = '\0';
std::vector<WCRANGE> regions;
unsigned individualOptions = 0;
// Parse the command line
for (int argIdx = 0; argIdx < argc; ++argIdx)
{
wchar_t *arg = argv[argIdx];
if (('-' == arg[0]) || ('/' == arg[0]))
{
arg++;
wchar_t *value = nullptr;
for (value = arg; *value && (':' != *value); ++value);
if (*value)
*value++ = 0;
unsigned option = 0;
if( !LookupByName(arg, g_Options, option) || (individualOptions & (1 << option)))
{
PrintUsage();
return 1;
}
if (option != OPT_CHARACTER_REGION)
{
individualOptions |= (1 << option);
}
// Handle options with an additional value parameter
switch (option)
{
case OPT_TYPEFACE:
case OPT_HEIGHT:
case OPT_WEIGHT:
case OPT_CHARSET:
case OPT_QUALITY:
case OPT_PITCH:
case OPT_FAMILY:
case OPT_GGO_DEPTH:
case OPT_CHARACTER_REGION:
case OPT_OUTPUT_FILENAME:
case OPT_DEFAULT_GLYPH:
if (!*value)
{
if (argIdx + 1 >= argc)
{
PrintUsage();
return 1;
}
++argIdx;
value = argv[argIdx];
}
break;
}
switch (option)
{
case OPT_TYPEFACE:
typeFace = value;
break;
case OPT_HEIGHT:
if (swscanf_s(value, L"%lu", &fontHeight) != 1)
{
wprintf(L"Invalid value specified with -h (%ls)\n", value);
wprintf(L"\n");
PrintUsage();
return 1;
}
break;
case OPT_WEIGHT:
if (!ParseTableValue(option, value, g_Weights, fontWeight))
{
PrintUsage();
return 1;
}
break;
case OPT_ITALIC:
useItalic = true;
break;
case OPT_UNDERLINE:
useUnderline = true;
break;
case OPT_STRIKEOUT:
useStrikeOut = true;
break;
case OPT_CHARSET:
if (!ParseTableValue(option, value, g_Charsets, charset))
{
PrintUsage();
return 1;
}
break;
case OPT_QUALITY:
if (!ParseTableValue(option, value, g_Qualities, quality))
{
PrintUsage();
return 1;
}
break;
case OPT_PITCH:
if (!ParseTableValue(option, value, g_Pitches, pitch))
{
PrintUsage();
return 1;
}
break;
case OPT_FAMILY:
if (!ParseTableValue(option, value, g_Families, family))
{
PrintUsage();
return 1;
}
break;
case OPT_GGO_DEPTH:
if (!ParseTableValue(option, value, g_GGO_Depths, ggoDepth))
{
PrintUsage();
return 1;
}
break;
case OPT_CHARACTER_REGION:
if (!ParseCharacterRegion(value, regions))
{
wprintf(L"Invalid character region value specified with -%ls: (%ls)\n", LookupByValue(OPT_CHARACTER_REGION, g_Options), value);
wprintf(L"\n");
PrintUsage();
return 1;
}
break;
case OPT_OUTPUT_FILENAME:
outputFilename = value;
break;
case OPT_OVERWRITE:
overwriteOutputFile = true;
break;
case OPT_DEFAULT_GLYPH:
if (!ParseDefaultGlyph(value, defaultGlyph, regions))
{
wprintf(L"Invalid character code specified with -%ls: (%ls)\n", LookupByValue(OPT_DEFAULT_GLYPH, g_Options), value);
wprintf(L"\n");
PrintUsage();
return 1;
}
}
}
}
if (fontHeight == 0)
{
wprintf(L"No value for required option -%ls\n", LookupByValue(OPT_HEIGHT, g_Options));
PrintUsage();
return 1;
}
if (outputFilename == nullptr)
{
wprintf(L"No value for required options -%ls\n", LookupByValue(OPT_OUTPUT_FILENAME, g_Options));
PrintUsage();
return 1;
}
if (!overwriteOutputFile)
{
if (GetFileAttributesW(outputFilename) != INVALID_FILE_ATTRIBUTES)
{
wprintf(L"\nERROR: Output file already exists, use -%ls to overwrite.\n", LookupByValue(OPT_OVERWRITE, g_Options));
PrintUsage();
return 1;
}
}
try
{
LOGFONT logfont = {};
logfont.lfHeight = fontHeight;
logfont.lfWidth = 0;
logfont.lfEscapement = 0;
logfont.lfOrientation = 0;
logfont.lfWeight = fontWeight;
logfont.lfItalic = useItalic;
logfont.lfUnderline = useUnderline;
logfont.lfStrikeOut = useStrikeOut;
logfont.lfCharSet = charset;
logfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
logfont.lfQuality = quality;
logfont.lfPitchAndFamily = uint8_t(pitch) | uint8_t(family);
logfont.lfFaceName[LF_FACESIZE];
if(typeFace)
wcscpy_s(logfont.lfFaceName, typeFace);
// Create the glyphs
std::vector<GGO_Glyph> ggoGlyphs;
std::vector<KERNINGPAIR> kerns;
Create_GGO_Glyphs(
&logfont,
ggoDepth,
regions,
kerns,
ggoGlyphs,
defaultGlyph);
// Create the pixel buffer
size_t pixelBufferSize = 0;
std::unique_ptr<uint8_t[]> pixels;
{
pixelBufferSize = CreatePixelBuffer(ggoGlyphs, nullptr, 0);
pixels = std::make_unique<uint8_t[]>(pixelBufferSize);
CreatePixelBuffer(ggoGlyphs, pixels.get(), pixelBufferSize);
}
// Compute the ascent and descent for the font
unsigned effectiveAscent = 0;
unsigned effectiveDescent = 0;
ComputeAscentDescent(ggoGlyphs, effectiveAscent, effectiveDescent);
// Create the raster glyphs
std::vector<RasterGlyphSheet::RasterGlyph> rasterGlyphs;
rasterGlyphs.resize(ggoGlyphs.size());
uint32_t pixelIndex = 0;
for (int i = 0; i < ggoGlyphs.size(); ++i)
{
rasterGlyphs[i].character = ggoGlyphs[i].GetCharacterCode();
auto& metrics = ggoGlyphs[i].GetMetrics();
rasterGlyphs[i].blackBoxOriginX = int16_t(metrics.gmptGlyphOrigin.x);
rasterGlyphs[i].blackBoxOriginY = int16_t(metrics.gmptGlyphOrigin.y);
rasterGlyphs[i].blackBoxWidth = metrics.gmBlackBoxX;
rasterGlyphs[i].blackBoxHeight = metrics.gmBlackBoxY;
rasterGlyphs[i].cellIncX = metrics.gmCellIncX;
rasterGlyphs[i].cellIncY = metrics.gmCellIncY;
rasterGlyphs[i].pixelIndex = pixelIndex;
pixelIndex += GetStorageSize<uint8_t>(metrics.gmBlackBoxX * metrics.gmBlackBoxY);
}
// Create the kerning pairs
std::vector<RasterGlyphSheet::KerningPair> rasterKerns;
rasterKerns.resize(kerns.size());
for (int i = 0; i < kerns.size(); ++i)
{
rasterKerns[i].first = kerns[i].wFirst;
rasterKerns[i].second = kerns[i].wSecond;
rasterKerns[i].amount = kerns[i].iKernAmount;
}
// Write the RasterFont to the file
{
// Create the RasterFont
RasterFont rf(std::make_unique<RasterGlyphSheet>(
effectiveAscent,
effectiveDescent,
rasterGlyphs,
rasterKerns,
uint32_t(pixelBufferSize),
pixels.get(),
'\0'));
rf.WriteToFile(outputFilename);
}
}
catch (std::exception &exn)
{
printf("Encountered an exception when trying to generate the font: \n%s", exn.what());
return 1;
}
return 0;
}