-
Notifications
You must be signed in to change notification settings - Fork 181
/
wintext.c
4328 lines (3868 loc) · 127 KB
/
wintext.c
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// wintext.c (part of mintty)
// Copyright 2008-13 Andy Koppe, 2015-2018 Thomas Wolff
// Adapted from code from PuTTY-0.60 by Simon Tatham and team.
// Licensed under the terms of the GNU General Public License v3 or later.
#include "winpriv.h"
#include "winsearch.h"
#include "charset.h" // wcscpy, wcsncat, combiningdouble
#include "config.h"
#include "winimg.h" // winimg_paint
#include <winnls.h>
#include <usp10.h> // Uniscribe
#define dont_debug_bold 1
enum {
FONT_NORMAL = 0x00,
FONT_BOLD = 0x01,
FONT_ITALIC = 0x02,
FONT_BOLDITAL = FONT_BOLD | FONT_ITALIC,
FONT_UNDERLINE = 0x04,
FONT_BOLDUND = FONT_BOLD | FONT_UNDERLINE,
FONT_STRIKEOUT = 0x08,
FONT_HIGH = 0x10,
FONT_ZOOMFULL = 0x20,
FONT_ZOOMSMALL = 0x40,
FONT_WIDE = 0x80,
//FONT_NARROW = 0x100,
FONT_NARROW = 0, // disabled narrowing via font
FONT_MAXNO = FONT_WIDE + FONT_NARROW
};
enum {LDRAW_CHAR_NUM = 31, LDRAW_CHAR_TRIES = 4};
// Possible linedraw character mappings, in order of decreasing suitability.
// The first choice is the same as used by xterm in most cases,
// except the diamond for which the narrower form is more authentic
// (see http://vt100.net/docs/vt220-rm/table2-4.html).
// The last resort for each is an ASCII character, which we assume will be
// available in any font.
static const wchar linedraw_chars[LDRAW_CHAR_NUM][LDRAW_CHAR_TRIES] = {
{0x2666, 0x25C6, '*'}, // 0x60 '`' Diamond ♦ ◆
{0x2592, '#'}, // 0x61 'a' Checkerboard (error)
{0x2409, 0x2192, 0x01AD, 't'}, // 0x62 'b' Horizontal tab
{0x240C, 0x21A1, 0x0192, 'f'}, // 0x63 'c' Form feed
{0x240D, 0x21B5, 0x027C, 'r'}, // 0x64 'd' Carriage return
{0x240A, 0x21B4, 0x019E, 'n'}, // 0x65 'e' Linefeed
{0x00B0, 'o'}, // 0x66 'f' Degree symbol
{0x00B1, '~'}, // 0x67 'g' Plus/minus
{0x2424, 0x21B4, 0x019E, 'n'}, // 0x68 'h' Newline
{0x240B, 0x2193, 0x028B, 'v'}, // 0x69 'i' Vertical tab
{0x2518, '+'}, // 0x6A 'j' Lower-right corner
{0x2510, '+'}, // 0x6B 'k' Upper-right corner
{0x250C, '+'}, // 0x6C 'l' Upper-left corner
{0x2514, '+'}, // 0x6D 'm' Lower-left corner
{0x253C, '+'}, // 0x6E 'n' Crossing lines
{0x23BA, 0x203E, ' '}, // 0x6F 'o' High horizontal line
{0x23BB, 0x207B, ' '}, // 0x70 'p' Medium-high horizontal line
{0x2500, 0x2014, '-'}, // 0x71 'q' Middle horizontal line
{0x23BC, 0x208B, ' '}, // 0x72 'r' Medium-low horizontal line
{0x23BD, '_'}, // 0x73 's' Low horizontal line
{0x251C, '+'}, // 0x74 't' Left "T"
{0x2524, '+'}, // 0x75 'u' Right "T"
{0x2534, '+'}, // 0x76 'v' Bottom "T"
{0x252C, '+'}, // 0x77 'w' Top "T"
{0x2502, '|'}, // 0x78 'x' Vertical bar
{0x2264, '#'}, // 0x79 'y' Less than or equal to
{0x2265, '#'}, // 0x7A 'z' Greater than or equal to
{0x03C0, '#'}, // 0x7B '{' Pi
{0x2260, '#'}, // 0x7C '|' Not equal to
{0x00A3, 'L'}, // 0x7D '}' UK pound sign
{0x00B7, '.'}, // 0x7E '~' Centered dot
};
// colour values; this should perhaps be part of struct term
COLORREF colours[COLOUR_NUM];
// diagnostic information flag
bool show_charinfo = false;
// master font family properties
LOGFONT lfont;
// logical font size, as configured (< 0: pixel size)
int font_size;
// scaled font size; pure font height, without spacing
static int font_height;
// character cell size, including spacing:
int cell_width, cell_height;
// border padding:
int PADDING = 1;
// width mode
bool font_ambig_wide;
typedef enum {BOLD_SHADOW, BOLD_FONT} BOLD_MODE;
typedef enum {UND_LINE, UND_FONT} UND_MODE;
struct charpropcache {
uint width: 2;
xchar ch: 21;
} __attribute__((packed));
// font family properties
struct fontfam {
wstring name;
wstring name_reported;
int weight;
bool isbold;
HFONT fonts[FONT_MAXNO];
bool fontflag[FONT_MAXNO];
bool font_dualwidth;
struct charpropcache * cpcache[FONT_BOLDITAL + 1];
uint cpcachelen[FONT_BOLDITAL + 1];
int fw_norm;
int fw_bold;
BOLD_MODE bold_mode;
UND_MODE und_mode;
int row_spacing, col_spacing;
int descent;
// VT100 linedraw character mappings for current font:
wchar win_linedraw_chars[LDRAW_CHAR_NUM];
} fontfamilies[11];
int line_scale;
wchar
win_linedraw_char(int i)
{
int findex = (term.curs.attr.attr & FONTFAM_MASK) >> ATTR_FONTFAM_SHIFT;
if (findex > 10)
findex = 0;
struct fontfam * ff = &fontfamilies[findex];
return ff->win_linedraw_chars[i];
}
char *
fontpropinfo()
{
//__ Options - Text: font properties information: "Leading": total line padding (see option RowSpacing), Bold/Underline modes (font or manual, see options BoldAsFont/UnderlineManual/UnderlineColour)
char * fontinfopat = _("Leading: %d, Bold: %s, Underline: %s");
//__ Options - Text: font properties: value taken from font
char * fontinfo_font = _("font");
//__ Options - Text: font properties: value affected by option
char * fontinfo_manual = _("manual");
int taglen = max(strlen(fontinfo_font), strlen(fontinfo_manual));
char * fontinfo = newn(char, strlen(fontinfopat) + 23 + 2 * taglen);
sprintf(fontinfo, fontinfopat, fontfamilies->row_spacing,
fontfamilies->bold_mode ? fontinfo_font : fontinfo_manual,
fontfamilies->und_mode ? fontinfo_font : fontinfo_manual);
return fontinfo;
}
uint
colour_dist(colour a, colour b)
{
return
2 * sqr(red(a) - red(b)) +
4 * sqr(green(a) - green(b)) +
1 * sqr(blue(a) - blue(b));
}
#define dont_debug_brighten
colour
brighten(colour c, colour against, bool monotone)
{
uint r = red(c), g = green(c), b = blue(c);
// "brighten" away from the background:
// if we are closer to black than the contrast reference, rather darken
bool darken = colour_dist(c, 0) < colour_dist(against, 0);
#ifdef debug_brighten
printf("%s %06X against %06X\n", darken ? "darkening" : "brighting", c, against);
#endif
uint _brighter() {
uint s = min(85, 255 - max(max(r, g), b));
return make_colour(r + s, g + s, b + s);
}
uint _darker() {
int sub = 70;
return make_colour(max(0, (int)r - sub), max(0, (int)g - sub), max(0, (int)b - sub));
}
colour bright;
uint thrsh = 22222; // contrast threshhold;
// if we're closer to either fg or bg,
// turn "brightening" into the other direction
if (darken) {
bright = _darker();
#ifdef debug_brighten
printf("darker %06X -> %06X dist %d\n", c, bright, colour_dist(c, bright));
#endif
if (colour_dist(bright, c) < thrsh || colour_dist(bright, against) < thrsh) {
if (monotone) {
uint r = red(bright), g = green(bright), b = blue(bright);
return make_colour(r - (r >> 2), g - (g >> 2), b - (b >> 2));
}
bright = _brighter();
#ifdef debug_brighten
printf(" fix %06X -> %06X dist %d/%d\n", c, bright, colour_dist(bright, c), colour_dist(bright, against));
#endif
}
}
else {
bright = _brighter();
#ifdef debug_brighten
printf("lightr %06X -> %06X dist %d\n", c, bright, colour_dist(c, bright));
#endif
if (colour_dist(bright, c) < thrsh || colour_dist(bright, against) < thrsh) {
if (monotone) {
uint r = red(bright), g = green(bright), b = blue(bright);
return make_colour(r + ((256 - r) >> 2), g + ((256 - g) >> 2), b + ((256 - b) >> 2));
}
bright = _darker();
#ifdef debug_brighten
printf(" fix %06X -> %06X dist %d/%d\n", c, bright, colour_dist(bright, c), colour_dist(bright, against));
#endif
}
}
return bright;
}
static uint
get_font_quality(void)
{
return
(uchar[]){
[FS_DEFAULT] = DEFAULT_QUALITY,
[FS_NONE] = NONANTIALIASED_QUALITY,
[FS_PARTIAL] = ANTIALIASED_QUALITY,
[FS_FULL] = CLEARTYPE_QUALITY
}[(int)cfg.font_smoothing];
}
#define dont_debug_create_font
#define dont_debug_fonts 1
#define dont_debug_win_char_width
#if defined(debug_fonts) && debug_fonts > 0
#define trace_font(params) printf params
#else
#define trace_font(params)
#endif
static HFONT
create_font(wstring name, int weight, bool underline)
{
#ifdef debug_create_font
printf("font [??]: %d (size %d) 0 w%4d i0 u%d s0\n", font_height, font_size, weight, underline);
#endif
return
CreateFontW(
font_height, 0, 0, 0, weight, false, underline, false,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
get_font_quality(), FIXED_PITCH | FF_DONTCARE,
name
);
}
static int
row_padding(int i, int e)
{
// may look nicer; used to break box characters; for background discussion,
// see https://github.com/mintty/mintty/issues/631#issuecomment-279690468
static bool allow_add_font_padding = true;
if (i == 0 && e == 0)
if (allow_add_font_padding)
return 2;
else
return 0;
else {
int exc = 0;
if (i > 3)
exc = i - 3;
int adj = e - exc;
if (allow_add_font_padding || adj <= 0)
return adj;
else
return 0;
}
}
static void
show_font_warning(struct fontfam * ff, char * msg)
{
// suppress multiple font error messages
if (ff->name_reported && wcscmp(ff->name_reported, ff->name) == 0) {
return;
}
else {
if (ff->name_reported)
delete(ff->name_reported);
ff->name_reported = wcsdup(ff->name);
}
char * fn = cs__wcstoutf(ff->name);
char * fullmsg;
int len = asprintf(&fullmsg, "%s:\n%s", msg, fn);
free(fn);
if (len > 0) {
show_message(fullmsg, MB_ICONWARNING);
free(fullmsg);
}
else
show_message(msg, MB_ICONWARNING);
}
#ifndef TCI_SRCLOCALE
//old MinGW
#define TCI_SRCLOCALE 0x1000
#endif
#ifdef check_font_ranges
#warning this does not tell us whether a glyph is shown
static GLYPHSET *
win_font_ranges(HDC dc, struct fontfam * ff, int fontno)
{
if (!ff->fonts[fontno] || fontno >= FONT_BOLDITAL)
return 0;
SelectObject(dc, ff->fonts[fontno]);
int ursize = GetFontUnicodeRanges(dc, 0);
GLYPHSET * gs = malloc(ursize);
gs->cbThis = ursize;
gs->flAccel = 0;
if (GetFontUnicodeRanges(dc, gs)) {
#ifdef debug_font_ranges
printf("%d %ls\n", fontno, ff->name);
for (uint i = 0; i < gs->cRanges; i++) {
printf("%04X: %d\n", gs->ranges[i].wcLow, gs->ranges[i].cGlyphs);
}
#endif
}
return gs;
}
static bool
glyph_in(WCHAR c, GLYPHSET * gs)
{
int min = 0;
int max = gs->cRanges - 1;
int mid;
while (max >= min) {
mid = (min + max) / 2;
if (c < gs->ranges[mid].wcLow) {
max = mid - 1;
} else if (c < gs->ranges[mid].wcLow + gs->ranges[mid].cGlyphs) {
return true;
} else {
min = mid + 1;
}
}
return false;
}
#endif
static UINT
get_default_charset(void)
{
CHARSETINFO csi;
long int acp = GetACP();
int ok = TranslateCharsetInfo((DWORD *)acp, &csi, TCI_SRCCODEPAGE);
if (ok)
return csi.ciCharset;
else
return DEFAULT_CHARSET;
}
struct data_adjust_font_weights {
struct fontfam *ff;
int fw_norm_0, fw_bold_0, fw_norm_1, fw_bold_1, default_charset;
bool font_found, ansi_found, cs_found;
};
static int CALLBACK
enum_fonts_adjust_font_weights(const LOGFONTW * lfp, const TEXTMETRICW * tmp, DWORD fontType, LPARAM lParam)
{
struct data_adjust_font_weights *data = (struct data_adjust_font_weights *)lParam;
(void)tmp;
(void)fontType;
#if defined(debug_fonts) && debug_fonts > 1
trace_font(("%ls %dx%d %d it %d cs %d %s\n", lfp->lfFaceName, (int)lfp->lfWidth, (int)lfp->lfHeight, (int)lfp->lfWeight, lfp->lfItalic, lfp->lfCharSet, (lfp->lfPitchAndFamily & 3) == FIXED_PITCH ? "fixed" : ""));
#endif
data->font_found = true;
if (lfp->lfCharSet == ANSI_CHARSET)
data->ansi_found = true;
if (lfp->lfCharSet == data->default_charset || lfp->lfCharSet == DEFAULT_CHARSET)
data->cs_found = true;
if (lfp->lfWeight > data->fw_norm_0 && lfp->lfWeight <= data->ff->fw_norm)
data->fw_norm_0 = lfp->lfWeight;
if (lfp->lfWeight > data->fw_bold_0 && lfp->lfWeight <= data->ff->fw_bold)
data->fw_bold_0 = lfp->lfWeight;
if (lfp->lfWeight < data->fw_norm_1 && lfp->lfWeight >= data->ff->fw_norm)
data->fw_norm_1 = lfp->lfWeight;
if (lfp->lfWeight < data->fw_bold_1 && lfp->lfWeight >= data->ff->fw_bold)
data->fw_bold_1 = lfp->lfWeight;
return 1; // continue
}
static void
adjust_font_weights(struct fontfam * ff, int findex)
{
LOGFONTW lf;
wcscpy(lf.lfFaceName, W(""));
wcsncat(lf.lfFaceName, ff->name, lengthof(lf.lfFaceName) - 1);
lf.lfPitchAndFamily = 0;
//lf.lfCharSet = ANSI_CHARSET; // report only ANSI character range
// use this to avoid double error popup (e.g. Font=David):
lf.lfCharSet = DEFAULT_CHARSET; // report all supported char ranges
// find the closest available widths such that
// fw_norm_0 <= ff->fw_norm <= fw_norm_1
// fw_bold_0 <= ff->fw_bold <= fw_bold_1
int default_charset = get_default_charset();
struct data_adjust_font_weights data = {
.ff = ff,
.fw_norm_0 = 0,
.fw_bold_0 = 0,
.fw_norm_1 = 1000,
.fw_bold_1 = 1001,
.default_charset = default_charset,
.font_found = false,
.ansi_found = false,
.cs_found = default_charset == DEFAULT_CHARSET
};
HDC dc = GetDC(0);
EnumFontFamiliesExW(dc, &lf, enum_fonts_adjust_font_weights, (LPARAM)&data, 0);
trace_font(("font width (%d)%d(%d)/(%d)%d(%d)", data.fw_norm_0, ff->fw_norm, data.fw_norm_1, data.fw_bold_0, ff->fw_bold, data.fw_bold_1));
ReleaseDC(0, dc);
// check if no font found
if (!data.font_found) {
show_font_warning(ff, _("Font not found, using system substitute"));
ff->fw_norm = 400;
ff->fw_bold = 700;
trace_font(("//\n"));
return;
}
if (!data.ansi_found && !data.cs_found) {
string l;
if (!strcmp(cfg.charset, "CP437") || ((l = getlocenvcat("LC_CTYPE")) && strstr(l, "CP437"))) {
// accept limited range
}
else if (findex) {
// don't report for alternative / secondary fonts
}
else
show_font_warning(ff, _("Font has limited support for character ranges"));
}
// find available widths closest to selected widths
if (abs(ff->fw_norm - data.fw_norm_0) <= abs(ff->fw_norm - data.fw_norm_1) && data.fw_norm_0 > 0)
ff->fw_norm = data.fw_norm_0;
else if (data.fw_norm_1 < 1000)
ff->fw_norm = data.fw_norm_1;
if (abs(ff->fw_bold - data.fw_bold_0) < abs(ff->fw_bold - data.fw_bold_1) || data.fw_bold_1 > 1000)
ff->fw_bold = data.fw_bold_0;
else if (data.fw_bold_1 < 1001)
ff->fw_bold = data.fw_bold_1;
// ensure bold is bolder than normal
if (ff->fw_bold <= ff->fw_norm) {
trace_font((" -> %d/%d", ff->fw_norm, ff->fw_bold));
if (data.fw_norm_0 < ff->fw_norm && data.fw_norm_0 > 0)
ff->fw_norm = data.fw_norm_0;
if (ff->fw_bold - ff->fw_norm < 300) {
if (data.fw_bold_1 > ff->fw_bold && data.fw_bold_1 < 1001)
ff->fw_bold = data.fw_bold_1;
else
ff->fw_bold = min(ff->fw_norm + 300, 1000);
}
}
// enforce preselected boldness
int selweight = ff->weight;
if (selweight < 700 && ff->isbold)
selweight = 700;
if (selweight - ff->fw_norm >= 300) {
trace_font((" -> %d(%d)/%d", ff->fw_norm, selweight, ff->fw_bold));
ff->fw_norm = selweight;
ff->fw_bold = min(ff->fw_norm + 300, 1000);
}
trace_font((" -> %d/%d\n", ff->fw_norm, ff->fw_bold));
}
/*
* Initialise all the fonts of a font family we will need initially:
Normal (the ordinary font), and optionally bold and underline;
Other font variations are done if/when they are needed (another_font).
We also:
- check the font width and height, correcting our guesses if necessary.
- verify that the bold font is the same width as the ordinary one,
and engage shadow bolding if not.
- verify that the underlined font is the same width as the ordinary one,
and engage manual underlining if not.
*/
static void
win_init_fontfamily(HDC dc, int findex)
{
struct fontfam * ff = &fontfamilies[findex];
trace_resize(("--- init_fontfamily\n"));
TEXTMETRIC tm;
for (uint i = 0; i < FONT_BOLDITAL; i++) {
if (ff->fonts[i])
delete(ff->cpcache[i]);
ff->cpcache[i] = 0;
ff->cpcachelen[i] = 0;
}
for (uint i = 0; i < FONT_MAXNO; i++) {
if (ff->fonts[i]) {
DeleteObject(ff->fonts[i]);
ff->fonts[i] = 0;
}
ff->fontflag[i] = false;
}
// if initialized as BOLD_SHADOW then real bold is never attempted
ff->bold_mode = BOLD_FONT;
ff->und_mode = UND_FONT;
if (cfg.underl_manual || cfg.underl_colour != (colour)-1)
ff->und_mode = UND_LINE;
if (ff->weight) {
ff->fw_norm = ff->weight;
ff->fw_bold = min(ff->fw_norm + 300, 1000);
// adjust selected font weights to available font weights
trace_font(("-> Weight %d/%d\n", ff->fw_norm, ff->fw_bold));
adjust_font_weights(ff, findex);
trace_font(("-> -> %d/%d\n", ff->fw_norm, ff->fw_bold));
}
else if (ff->isbold) {
ff->fw_norm = FW_BOLD;
ff->fw_bold = FW_HEAVY;
trace_font(("-> IsBold %d/%d\n", ff->fw_norm, ff->fw_bold));
}
else {
ff->fw_norm = FW_DONTCARE;
ff->fw_bold = FW_BOLD;
trace_font(("-> normal %d/%d\n", ff->fw_norm, ff->fw_bold));
}
ff->fonts[FONT_NORMAL] = create_font(ff->name, ff->fw_norm, false);
LOGFONT logfont;
GetObject(ff->fonts[FONT_NORMAL], sizeof(LOGFONT), &logfont);
trace_font(("created font %s %d it %d cs %d\n", logfont.lfFaceName, (int)logfont.lfWeight, logfont.lfItalic, logfont.lfCharSet));
SelectObject(dc, ff->fonts[FONT_NORMAL]);
GetTextMetrics(dc, &tm);
if (!tm.tmHeight) {
// corrupt font installation (e.g. deleted font file)
show_font_warning(ff, _("Font installation corrupt, using system substitute"));
wstrset(&ff->name, W(""));
ff->fonts[FONT_NORMAL] = create_font(ff->name, ff->fw_norm, false);
GetObject(ff->fonts[FONT_NORMAL], sizeof(LOGFONT), &logfont);
SelectObject(dc, ff->fonts[FONT_NORMAL]);
GetTextMetrics(dc, &tm);
}
if (!findex)
lfont = logfont;
#ifdef check_charset_only_for_returned_font
int default_charset = get_default_charset();
if (tm.tmCharSet != default_charset && default_charset != DEFAULT_CHARSET) {
show_font_warning(ff, _("Font does not support system locale"));
}
#endif
float latin_char_width, greek_char_width, line_char_width, cjk_char_width;
GetCharWidthFloatW(dc, 0x0041, 0x0041, &latin_char_width);
GetCharWidthFloatW(dc, 0x03B1, 0x03B1, &greek_char_width);
GetCharWidthFloatW(dc, 0x2500, 0x2500, &line_char_width);
GetCharWidthFloatW(dc, 0x4E00, 0x4E00, &cjk_char_width);
if (!findex) {
ff->row_spacing = row_padding(tm.tmInternalLeading, tm.tmExternalLeading);
trace_font(("00 height %d avwidth %d asc %d dsc %d intlead %d extlead %d %ls\n",
(int)tm.tmHeight, (int)tm.tmAveCharWidth, (int)tm.tmAscent, (int)tm.tmDescent,
(int)tm.tmInternalLeading, (int)tm.tmExternalLeading,
ff->name));
ff->row_spacing += cfg.row_spacing;
if (ff->row_spacing < -tm.tmDescent)
ff->row_spacing = -tm.tmDescent;
trace_font(("row spacing int %d ext %d -> %+d; add %+d -> %+d; desc %d -> %+d %ls\n",
(int)tm.tmInternalLeading, (int)tm.tmExternalLeading, row_padding(tm.tmInternalLeading, tm.tmExternalLeading),
cfg.row_spacing, row_padding(tm.tmInternalLeading, tm.tmExternalLeading) + cfg.row_spacing,
(int)tm.tmDescent, ff->row_spacing, ff->name));
ff->col_spacing = cfg.col_spacing;
cell_height = tm.tmHeight + ff->row_spacing;
//cell_width = tm.tmAveCharWidth + ff->col_spacing;
cell_width = (int)(latin_char_width * 16) + ff->col_spacing;
line_scale = cell_height * 100 / abs(font_height);
PADDING = tm.tmAveCharWidth;
if (cfg.padding >= 0 && cfg.padding < PADDING)
PADDING = cfg.padding;
}
else {
ff->row_spacing = cell_height - tm.tmHeight;
//ff->col_spacing = cell_width - tm.tmAveCharWidth;
ff->col_spacing = cell_width - (int)(latin_char_width * 16);
}
#ifdef debug_create_font
printf("size %d -> height %d -> height %d\n", font_size, font_height, cell_height);
#endif
//ff->font_dualwidth = (tm.tmMaxCharWidth >= tm.tmAveCharWidth * 3 / 2);
ff->font_dualwidth = (cjk_char_width >= latin_char_width * 3 / 2);
// Determine whether ambiguous-width characters are wide in this font */
if (!findex)
font_ambig_wide =
greek_char_width >= latin_char_width * 1.5 ||
line_char_width >= latin_char_width * 1.5;
#ifdef debug_win_char_width
int w_latin = win_char_width(0x0041, 0);
int w_greek = win_char_width(0x03B1, 0);
int w_lines = win_char_width(0x2500, 0);
printf("%04X %5.2f %d\n", 0x0041, latin_char_width, w_latin);
printf("%04X %5.2f %d\n", 0x03B1, greek_char_width, w_greek);
printf("%04X %5.2f %d\n", 0x2500, line_char_width, w_lines);
bool faw = w_greek > w_latin || w_lines > w_latin;
printf("font faw %d (dual %d [ambig %d])\n", faw, ff->font_dualwidth, font_ambig_wide);
#endif
// Initialise VT100 linedraw character mappings.
// See what glyphs are available.
ushort glyphs[LDRAW_CHAR_NUM][LDRAW_CHAR_TRIES];
GetGlyphIndicesW(dc, *linedraw_chars, LDRAW_CHAR_NUM * LDRAW_CHAR_TRIES,
*glyphs, true);
// For each character, try the list of possible mappings until either we
// find one that has a glyph in the font or we hit the ASCII fallback.
for (uint i = 0; i < LDRAW_CHAR_NUM; i++) {
uint j = 0;
while (linedraw_chars[i][j] >= 0x80 &&
(glyphs[i][j] == 0xFFFF || glyphs[i][j] == 0x1F))
j++;
ff->win_linedraw_chars[i] = linedraw_chars[i][j];
#define draw_vt100_line_drawing_chars
#ifdef draw_vt100_line_drawing_chars
if ('j' - '`' <= i && i <= 'x' - '`')
ff->win_linedraw_chars[i] = linedraw_chars[i][0];
#endif
}
ff->fonts[FONT_UNDERLINE] = create_font(ff->name, ff->fw_norm, true);
/*
* Some fonts, e.g. 9-pt Courier, draw their underlines
* outside their character cell. We successfully prevent
* screen corruption by clipping the text output, but then
* we lose the underline completely. Here we try to work
* out whether this is such a font, and if it is, we set a
* flag that causes underlines to be drawn by hand.
*
* Having tried other more sophisticated approaches (such
* as examining the TEXTMETRIC structure or requesting the
* height of a string), I think we'll do this the brute
* force way: we create a small bitmap, draw an underlined
* space on it, and test to see whether any pixels are
* foreground-coloured. (Since we expect the underline to
* go all the way across the character cell, we only search
* down a single column of the bitmap, half way across.)
*/
if (ff->und_mode == UND_FONT) {
HDC und_dc = CreateCompatibleDC(dc);
HBITMAP und_bm = CreateCompatibleBitmap(dc, cell_width, cell_height);
HBITMAP und_oldbm = SelectObject(und_dc, und_bm);
SelectObject(und_dc, ff->fonts[FONT_UNDERLINE]);
SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
SetTextColor(und_dc, RGB(255, 255, 255));
SetBkColor(und_dc, RGB(0, 0, 0));
SetBkMode(und_dc, OPAQUE);
ExtTextOutA(und_dc, 0, 0, ETO_OPAQUE, null, " ", 1, null);
bool gotit = false;
// look for font-generated underline in character cell
//int i = 0;
// look for font-generated underline in descender section only
int i = tm.tmAscent;
//int i = tm.tmAscent + 1;
for (; i < cell_height; i++) {
COLORREF c = GetPixel(und_dc, cell_width / 2, i);
if (c != RGB(0, 0, 0))
gotit = true;
}
SelectObject(und_dc, und_oldbm);
DeleteObject(und_bm);
DeleteDC(und_dc);
if (!gotit) {
trace_font(("ul outbox %ls\n", ff->name));
ff->und_mode = UND_LINE;
DeleteObject(ff->fonts[FONT_UNDERLINE]);
ff->fonts[FONT_UNDERLINE] = 0;
}
}
if (ff->bold_mode == BOLD_FONT)
ff->fonts[FONT_BOLD] = create_font(ff->name, ff->fw_bold, false);
ff->descent = tm.tmAscent + 1;
if (ff->descent >= cell_height)
ff->descent = cell_height - 1;
int fontsize[FONT_UNDERLINE + 1];
#ifdef handle_baseline_leap
int base_ascent = tm.tmAscent;
#endif
for (uint i = 0; i < lengthof(fontsize); i++) { // could skip FONT_ITALIC here
if (ff->fonts[i]) {
if (SelectObject(dc, ff->fonts[i]) && GetTextMetrics(dc, &tm)) {
fontsize[i] = tm.tmAveCharWidth + 256 * tm.tmHeight;
trace_font(("%02X height %d avwidth %d asc %d dsc %d intlead %d extlead %d %ls\n",
i, (int)tm.tmHeight, (int)tm.tmAveCharWidth,
(int)tm.tmAscent, (int)tm.tmDescent,
(int)tm.tmInternalLeading, (int)tm.tmExternalLeading,
ff->name));
#ifdef handle_baseline_leap
if (i == FONT_BOLD && tm.tmAscent < base_ascent) {
// for Courier New, this correlates with a significant visual leap
// of the bold font from the baseline of the normal font,
// but not for other fonts; so let's do nothing
}
#endif
}
else
fontsize[i] = -i;
}
else
fontsize[i] = -i;
}
if (fontsize[FONT_UNDERLINE] != fontsize[FONT_NORMAL]) {
trace_font(("ul size!= %ls\n", ff->name));
ff->und_mode = UND_LINE;
DeleteObject(ff->fonts[FONT_UNDERLINE]);
ff->fonts[FONT_UNDERLINE] = 0;
}
if (ff->bold_mode == BOLD_FONT) {
int diffsize = abs(fontsize[FONT_BOLD] - fontsize[FONT_NORMAL]);
#if defined(debug_create_font) || defined(debug_bold)
if (*ff->name)
printf("bold_mode %d font_size %d size %d bold %d diff %d %s %ls\n",
ff->bold_mode, font_size,
fontsize[FONT_NORMAL], fontsize[FONT_BOLD], diffsize,
fontsize[FONT_BOLD] != fontsize[FONT_NORMAL] ? "///" : "===",
ff->name);
#endif
if (diffsize * 16 > fontsize[FONT_NORMAL]) {
trace_font(("bold_mode %d\n", ff->bold_mode));
ff->bold_mode = BOLD_SHADOW;
DeleteObject(ff->fonts[FONT_BOLD]);
ff->fonts[FONT_BOLD] = 0;
}
}
trace_font(("bold_mode %d\n", ff->bold_mode));
ff->fontflag[FONT_NORMAL] = true;
ff->fontflag[FONT_BOLD] = true;
ff->fontflag[FONT_UNDERLINE] = true;
}
static wstring
wcscasestr(wstring in, wstring find)
{
#if CYGWIN_VERSION_API_MINOR < 206
#define wcsncasecmp wcsncmp
#endif
int l = wcslen(find);
wstring look = in;
for (int i = 0; i <= (int)wcslen(in) - l; i++, look++) { // uint fails!
if (0 == wcsncasecmp(look, find, l)) {
return look;
}
}
return 0;
}
static void
findFraktur(wstring * fnp)
{
LOGFONTW lf;
wcscpy(lf.lfFaceName, W(""));
lf.lfPitchAndFamily = 0;
lf.lfCharSet = ANSI_CHARSET; // report only ANSI character range
int CALLBACK enum_fonts(const LOGFONTW * lfp, const TEXTMETRICW * tmp, DWORD fontType, LPARAM lParam)
{
(void)tmp;
(void)fontType;
wstring * fnp = (wstring *)lParam;
#if defined(debug_fonts) && debug_fonts > 1
trace_font(("%ls %dx%d %d it %d cs %d %s\n", lfp->lfFaceName, (int)lfp->lfWidth, (int)lfp->lfHeight, (int)lfp->lfWeight, lfp->lfItalic, lfp->lfCharSet, (lfp->lfPitchAndFamily & 3) == FIXED_PITCH ? "fixed" : ""));
#endif
if ((lfp->lfPitchAndFamily & 3) == FIXED_PITCH
&& !lfp->lfCharSet
&& lfp->lfFaceName[0] != '@'
)
{
if (wcscasestr(lfp->lfFaceName, W("Fraktur"))) {
*fnp = wcsdup(lfp->lfFaceName);
return 0; // done
}
else if (wcscasestr(lfp->lfFaceName, W("Blackletter"))) {
*fnp = wcsdup(lfp->lfFaceName);
// continue to look for "Fraktur"
}
}
return 1; // continue
}
HDC dc = GetDC(0);
EnumFontFamiliesExW(dc, 0, enum_fonts, (LPARAM)fnp, 0);
ReleaseDC(0, dc);
}
/*
* Initialize fonts for all configured font families.
*/
void
win_init_fonts(int size)
{
trace_resize(("--- init_fonts %d\n", size));
HDC dc = GetDC(wnd);
font_size = size;
#ifdef debug_dpi
printf("dpi %d dev %d\n", dpi, GetDeviceCaps(dc, LOGPIXELSY));
#endif
if (cfg.handle_dpichanged && per_monitor_dpi_aware)
font_height =
font_size > 0 ? -MulDiv(font_size, dpi, 72) : -font_size;
// dpi is determined initially and via WM_WINDOWPOSCHANGED;
// if WM_DPICHANGED were used, this would need to be modified
else
font_height =
font_size > 0 ? -MulDiv(font_size, GetDeviceCaps(dc, LOGPIXELSY), 72) : -font_size;
static bool initinit = true;
for (uint fi = 0; fi < lengthof(fontfamilies); fi++) {
if (!fi) {
fontfamilies[fi].name = cfg.font.name;
fontfamilies[fi].weight = cfg.font.weight;
fontfamilies[fi].isbold = cfg.font.isbold;
}
else {
fontfamilies[fi].name = cfg.fontfams[fi].name;
fontfamilies[fi].weight = cfg.fontfams[fi].weight;
fontfamilies[fi].isbold = false;
}
if (fi == 20 - 10 && !*(fontfamilies[fi].name))
findFraktur(&fontfamilies[fi].name);
if (initinit)
fontfamilies[fi].name_reported = null;
win_init_fontfamily(dc, fi);
}
initinit = false;
ReleaseDC(wnd, dc);
}
wstring
win_get_font(uint fi)
{
if (fi < lengthof(fontfamilies))
return fontfamilies[fi].name;
else
return null;
}
void
win_change_font(uint fi, wstring fn)
{
if (fi < lengthof(fontfamilies)) {
fontfamilies[fi].name = fn;
fontfamilies[fi].name_reported = null;
HDC dc = GetDC(wnd);
win_init_fontfamily(dc, fi);
ReleaseDC(wnd, dc);
win_adapt_term_size(true, false);
win_font_cs_reconfig(true);
}
}
uint
win_get_font_size(void)
{
return abs(font_size);
}
void
win_set_font_size(int size, bool sync_size_with_font)
{
trace_resize(("--- win_set_font_size %d %d×%d\n", size, term.rows, term.cols));
size = size ? sgn(font_size) * min(size, 72) : cfg.font.size;
if (size != font_size) {
win_init_fonts(size);
trace_resize((" (win_set_font_size -> win_adapt_term_size)\n"));
win_adapt_term_size(sync_size_with_font, false);
}
}
void
win_zoom_font(int zoom, bool sync_size_with_font)
{
trace_resize(("--- win_zoom_font %d\n", zoom));
win_set_font_size(zoom ? max(1, abs(font_size) + zoom) : 0, sync_size_with_font);
}
static HDC dc;
static enum { UPDATE_IDLE, UPDATE_BLOCKED, UPDATE_PENDING } update_state;
static bool ime_open;
static int update_skipped = 0;
int lines_scrolled = 0;
#define dont_debug_cursor 1
static struct charnameentry {
xchar uc;
string un;
} * charnametable = null;
static int charnametable_len = 0;
static int charnametable_alloced = 0;
static bool charnametable_init = false;
static void
init_charnametable()
{
if (charnametable_init)
return;
charnametable_init = true;
void add_charname(uint cc, char * cn) {
if (charnametable_len >= charnametable_alloced) {
charnametable_alloced += 999;
if (!charnametable)
charnametable = newn(struct charnameentry, charnametable_alloced);
else
charnametable = renewn(charnametable, charnametable_alloced);
}
charnametable[charnametable_len].uc = cc;
charnametable[charnametable_len].un = strdup(cn);
charnametable_len++;
}
char * cnfn = get_resource_file(W("info"), W("charnames.txt"), false);
FILE * cnf = fopen(cnfn, "r");
if (cnf) {
uint cc;
char cn[100];
while (fscanf(cnf, "%X %[- A-Z0-9]", &cc, cn) == 2) {
add_charname(cc, cn);
}
fclose(cnf);
}
else {
cnf = fopen("/usr/share/unicode/ucd/UnicodeData.txt", "r");
if (!cnf)
return;
FILE * crf = fopen("/usr/share/unicode/ucd/NameAliases.txt", "r");
uint ccorr = 0;
char buf[100];
char nbuf[100];
while (fgets(buf, sizeof(buf), cnf)) {
uint cc;
char cn[99];
if (sscanf(buf, "%X;%[- A-Z0-9];", &cc, cn) == 2) {
//0020;SPACE;Zs;0;WS;;;;;N;;;;;
if (crf) {