-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpltext.cpp
1331 lines (1102 loc) · 44.4 KB
/
pltext.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
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
/*
BSD 3-Clause License
Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/wex/blob/develop/LICENSE
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <wx/tokenzr.h>
#include <wx/choicdlg.h>
#include <wx/dir.h>
#include <wex/plot/pltext.h>
struct escape_sequence {
wxString code;
wxUniChar value;
};
static escape_sequence escape_codes[] = {
{"Alpha", L'\x0391'},
{"Beta", L'\x0392'},
{"Gamma", L'\x0393'},
{"Delta", L'\x0394'},
{"Epsilon", L'\x0395'},
{"Zeta", L'\x0396'},
{"Eta", L'\x0397'},
{"Theta", L'\x0398'},
{"Iota", L'\x0399'},
{"Kappa", L'\x039a'},
{"Lambda", L'\x039b'},
{"Mu", L'\x039c'},
{"Nu", L'\x039d'},
{"Xi", L'\x039e'},
{"Omicron", L'\x039f'},
{"Pi", L'\x03a0'},
{"Rho", L'\x03a1'},
{"Sigma", L'\x03a3'},
{"Tau", L'\x03a4'},
{"Upsilon", L'\x03a5'},
{"Phi", L'\x03a6'},
{"Chi", L'\x03a7'},
{"Psi", L'\x03a8'},
{"Omega", L'\x03a9'},
{"alpha", L'\x03b1'},
{"beta", L'\x03b2'},
{"gamma", L'\x03b3'},
{"delta", L'\x03b4'},
{"epsilon", L'\x03b5'},
{"zeta", L'\x03b6'},
{"eta", L'\x03b7'},
{"theta", L'\x03b8'},
{"iota", L'\x03b9'},
{"kappa", L'\x03ba'},
{"lambda", L'\x03bb'},
{"mu", L'\x03bc'},
{"nu", L'\x03bd'},
{"xi", L'\x03be'},
{"omicron", L'\x03bf'},
{"pi", L'\x03c0'},
{"rho", L'\x03c1'},
{"fsigma", L'\x03c3'},
{"sigma", L'\x03c3'},
{"tau", L'\x03c4'},
{"upsilon", L'\x03c5'},
{"phi", L'\x03c6'},
{"chi", L'\x03c7'},
{"psi", L'\x03c8'},
{"omega", L'\x03c9'},
{"emph", L'\x00a1'},
{"qmark", L'\x00bf'},
{"cent", L'\x00a2'},
{"pound", L'\x00a3'},
{"euro", L'\x20ac'},
{"section", L'\x00a7'},
{"dot", L'\x00b7'},
{"mult", L'\x00d7'},
{"copy", L'\x00a9'},
{"reg", L'\x00ae'},
{"deg", L'\x00b0'},
{"pm", L'\x00b1'},
{"ne", L'\x2260'},
{"approx", L'\x2248'},
{wxEmptyString, 0},
};
static const int FontPointAdjust = 2;
wxPLTextLayout::wxPLTextLayout(wxPLOutputDevice &dc, const wxString &text, TextAlignment ta)
: m_bounds(0, 0) {
if (text.IsEmpty()) return;
// get current font state for subsequent relative adjustments
double fontPoints = dc.TextPoints();
// split text into lines, and parse each one into text pieces after resolving escape sequences
wxArrayString lines = wxStringTokenize(text, "\r\n");
for (size_t i = 0; i < lines.Count(); i++)
m_lines.push_back(Parse(Escape(lines[i])));
if (m_lines.size() == 0) return;
// compute extents of each text piece with the right font
for (size_t i = 0; i < m_lines.size(); i++) {
for (size_t j = 0; j < m_lines[i].size(); j++) {
text_piece &tp = m_lines[i][j];
double width, height;
dc.TextPoints(tp.state == text_piece::NORMAL ? fontPoints : fontPoints - FontPointAdjust);
dc.Measure(tp.text, &width, &height);
tp.size.x = width;
tp.size.y = height;
}
}
// obtain the approximate heights for normal and small text
dc.TextPoints(fontPoints - FontPointAdjust);
double height_small = fontPoints - FontPointAdjust;
dc.Measure("0", NULL, &height_small);
dc.TextPoints(fontPoints);
double height_normal = fontPoints;
dc.Measure("0", NULL, &height_normal);
// sequentially calculate the origins of each text piece
const double offset = 0.25 * height_small; // amount to raise/lower super/sub-scripts
double y = 0;
for (size_t i = 0; i < m_lines.size(); i++) {
double x = 0;
bool has_sup = false;
// layout this line's X positions, keep track of whether it has super/subs
for (size_t j = 0; j < m_lines[i].size(); j++) {
text_piece &tp = m_lines[i][j];
if (tp.state == text_piece::SUPERSCRIPT)
has_sup = true;
// save original relative alignment from x
// allows future realignment of text pieces if needed
tp.aligned_x = x;
tp.origin.x = x;
x += tp.size.x;
}
// save the line width as the maximum bounds
if (x > m_bounds.x)
m_bounds.x = x;
// layout this line's Y positions
if (has_sup) y += offset;
for (size_t j = 0; j < m_lines[i].size(); j++) {
text_piece &tp = m_lines[i][j];
if (tp.state == text_piece::NORMAL)
tp.origin.y = y;
else if (tp.state == text_piece::SUPERSCRIPT)
tp.origin.y = y - offset;
else if (tp.state == text_piece::SUBSCRIPT)
tp.origin.y = y + height_normal - 3 * offset;
}
y += height_normal + offset / 3;
}
if (ta != LEFT)
Align(ta);
// save the final y position as the maximum height
m_bounds.y = y;
}
void wxPLTextLayout::Align(TextAlignment ta) {
// realign x offsets for differently aligned text
// now that we know the total bounds width
// note: does not require recalculating text sizes since they are cached
for (size_t i = 0; i < m_lines.size(); i++) {
double line_width = 0;
for (size_t j = 0; j < m_lines[i].size(); j++) {
// restore original aligned positions for each text piece
m_lines[i][j].origin.x = m_lines[i][j].aligned_x;
line_width += m_lines[i][j].size.x;
}
double offset = 0;
if (ta == CENTER) offset = 0.5 * (m_bounds.x - line_width);
else if (ta == RIGHT) offset = m_bounds.x - line_width;
if (offset != 0) // only do this for center/right alignments
for (size_t j = 0; j < m_lines[i].size(); j++)
m_lines[i][j].origin.x += offset;
}
}
void wxPLTextLayout::Render(wxPLOutputDevice &dc, double x, double y, double rotationDegrees, bool drawBounds) {
if (m_lines.size() == 0) return;
bool aa = dc.GetAntiAliasing();
// wx 3.1.5
// if (drawBounds) dc.SetAntiAliasing(false);
dc.SetAntiAliasing(true);
double fontPoints = dc.TextPoints();
if (drawBounds) {
dc.Pen(*wxLIGHT_GREY, 0.5);
dc.NoBrush();
}
// layout has already been calculated, assuming the same font.
// render the text directly given the starting coordinates
if (rotationDegrees == 0.0) {
for (size_t i = 0; i < m_lines.size(); i++) {
for (size_t j = 0; j < m_lines[i].size(); j++) {
text_piece &tp = m_lines[i][j];
dc.TextPoints(tp.state == text_piece::NORMAL ? fontPoints : fontPoints - FontPointAdjust);
dc.Text(tp.text, x + tp.origin.x, y + tp.origin.y);
if (drawBounds)
dc.Rect(x + tp.origin.x, y + tp.origin.y, tp.size.x, tp.size.y);
}
}
} else {
double theta = -M_PI / 180 * rotationDegrees;
double sintheta = sin(theta);
double costheta = cos(theta);
for (size_t i = 0; i < m_lines.size(); i++) {
for (size_t j = 0; j < m_lines[i].size(); j++) {
text_piece &tp = m_lines[i][j];
dc.TextPoints(tp.state == text_piece::NORMAL ? fontPoints : fontPoints - FontPointAdjust);
double rotx = tp.origin.x * costheta - tp.origin.y * sintheta;
double roty = tp.origin.x * sintheta + tp.origin.y * costheta;
dc.Text(tp.text, x + rotx, y + roty, rotationDegrees);
}
}
}
// restore font state
dc.TextPoints(fontPoints);
if (drawBounds) dc.SetAntiAliasing(aa);
}
wxString wxPLTextLayout::Escape(const wxString &text) {
wxString result;
result.Alloc(text.Length());
bool last_char_slash = false;
wxString::const_iterator it = text.begin();
while (it != text.end()) {
if (*it == '\\' && !last_char_slash) {
++it; // skip the slash
wxString code;
while (it != text.end()
&& (*it) != '\\'
&& wxIsalpha((*it))) {
code += *it;
++it;
}
last_char_slash = (it != text.end()
&& code.Len() == 0
&& *it == '\\');
if (it != text.end()
&& (*it == ' ' || *it == '\t'))
it++; // skip next space if it exists. assume is for delineating codes
wxUniChar value(0);
size_t idx = 0;
while (::escape_codes[idx].value != 0) {
if (::escape_codes[idx].code == code) {
value = ::escape_codes[idx].value;
break;
} else idx++;
}
if (value != 0) result += value;
else if (!last_char_slash) result << L'\x275a';
} else {
last_char_slash = false;
result += *it;
++it;
}
}
return result;
}
std::vector<wxPLTextLayout::text_piece> wxPLTextLayout::Parse(const wxString &text) {
std::vector<text_piece> list;
text_piece tp;
wxString::const_iterator it = text.begin();
while (it != text.end()) {
if (*it == '^' || *it == '_') {
wxUniChar modifier = *it;
it++; // skip modifier to get the next character
if (it == text.end() || *it == modifier) {
// if we have a double modifier,
// simply add it as normal text
tp.text += *it;
} else {
if (!tp.text.IsEmpty()) {
tp.state = text_piece::NORMAL;
list.push_back(tp);
tp.text.clear();
}
tp.state = (modifier == '^') ? text_piece::SUPERSCRIPT : text_piece::SUBSCRIPT;
if (it != text.end() && *it == '{') {
it++; // skip the {
while (it != text.end() && *it != '}') {
tp.text += *it;
it++;
}
// go until } encountered
if (it != text.end() && *it == '}')
it++;
} else {
while (it != text.end()
&& *it != ' '
&& *it != '/'
&& *it != '\t'
&& *it != '^'
&& *it != '_'
&& *it != '('
&& *it != '{'
&& *it != '['
&& *it != '='
&& *it != ','
&& *it != ';') {
tp.text += *it;
it++;
}
if (it != text.end() && *it != ' ')
--it; // return the character that ended the current mode
}
if (tp.text.Len() > 0) {
list.push_back(tp);
tp.text.clear();
}
}
} else {
tp.text += *it;
}
if (it != text.end())
++it;
}
// push back final text piece if needed
if (!tp.text.IsEmpty()) {
tp.state = text_piece::NORMAL;
list.push_back(tp);
}
return list;
}
#include <wx/dc.h>
#include <wx/dcgraph.h>
#include <wx/dcbuffer.h>
BEGIN_EVENT_TABLE(wxPLTextLayoutDemo, wxWindow)
EVT_PAINT(wxPLTextLayoutDemo::OnPaint)
EVT_SIZE(wxPLTextLayoutDemo::OnSize)
END_EVENT_TABLE()
wxPLTextLayoutDemo::wxPLTextLayoutDemo(wxWindow *parent)
: wxWindow(parent, wxID_ANY) {
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
}
std::vector<double> wxPLTextLayoutDemo::Draw(wxPLOutputDevice &dc, const wxPLRealRect &geom) {
std::vector<double> vlines;
// test 1
{
dc.TextPoints(0);
wxPLTextLayout t1(dc, "basic text, nothing special");
t1.Render(dc, geom.x + 20, geom.y + 20, 0.0, true);
vlines.push_back(geom.x + 20);
vlines.push_back(geom.x + 20 + t1.Width());
}
// test 2
{
dc.TextPoints(2);
//gc->SetFont( wxFont(18, wxFONTFAMILY_ROMAN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, "Century Schoolbook"), *wxBLACK );
wxPLTextLayout t2(dc,
"escape^sup_sub \\\\ \\ \\euro \\badcode \\Omega~\\Phi\\ne\\delta, but\n\\Kappa\\approx\\zeta \\emph\\qmark, and this is the end of the text!. Cost was 10 \\pound, or 13.2\\cent");
t2.Render(dc, geom.x + 20, geom.y + 120, 0.0, true);
}
// test 3
{
dc.TextPoints(-1);
dc.TextColour(*wxRED);
//gc->SetFont( wxFont(10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, "Consolas"), *wxBLACK );
wxPLTextLayout t3(dc,
"super^2 ,not_\\rho\\gamma f hing_{special,great,best}\n\\alpha^^\\beta c^\\delta efjhijkl__mnO^25 pq_0 r\\Sigma tuvwxyz\nABCDEFGHIJKL^^MNOPQRSTUVWXZY");
t3.Render(dc, geom.x + 20, geom.y + 420, 90, true);
t3.Render(dc, geom.x + 200, geom.y + 350, 45.0, true);
t3.Render(dc, geom.x + 400, geom.y + 300, 0.0, true);
vlines.push_back(geom.x + 400);
vlines.push_back(geom.x + 400 + t3.Width());
}
// test 4
{
dc.TextPoints(-2);
dc.TextColour(*wxBLUE);
//gc->SetFont( wxFont(16, wxFONTFAMILY_ROMAN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL), *wxBLACK );
wxPLTextLayout t4(dc, "x_1^2_3 abc=y_2^4");
t4.Render(dc, geom.x + 200, geom.y + 70, 0, false);
}
// test 5
{
dc.TextPoints(+3);
//gc->SetFont( wxFont(7, wxFONTFAMILY_ROMAN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL), *wxBLACK );
wxPLTextLayout t5(dc, "small (7): x_1^2_3 abc=y_2^4");
t5.Render(dc, geom.x + 500, geom.y + 50, 0, true);
}
// test 6
{
dc.TextPoints(-3);
//gc->SetFont( wxFont(8, wxFONTFAMILY_ROMAN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL), *wxBLACK );
wxPLTextLayout t6(dc, "small (8): x_1^2_3 abc=y_2^4");
t6.Render(dc, geom.x + 500, geom.y + 80, 0, false);
}
// test 7
{
dc.TextPoints(-2);
//gc->SetFont( wxFont(9, wxFONTFAMILY_ROMAN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL), *wxBLACK );
wxPLTextLayout t7(dc, "small (9): x_1^2_3 abc=y_2^4");
t7.Render(dc, geom.x + 500, geom.y + 100, 0, false);
}
// test 8
{
dc.TextPoints(-1);
dc.TextColour(*wxGREEN);
//gc->SetFont( wxFont(10, wxFONTFAMILY_ROMAN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL), *wxBLACK );
//wxPLGraphicsOutputDevice dc( gc );
wxPLTextLayout t8(dc, "small (10): x_1^2_3 abc=y_2^4");
t8.Render(dc, geom.x + 500, geom.y + 120, 0, false);
}
vlines.push_back(geom.x + 500);
return vlines;
}
void wxPLTextLayoutDemo::OnPaint(wxPaintEvent &) {
wxAutoBufferedPaintDC pdc(this);
int width, height;
GetClientSize(&width, &height);
pdc.SetBackground(*wxWHITE_BRUSH);
pdc.Clear();
wxGraphicsContext *gc = wxGraphicsContext::Create(pdc);
wxPLGraphicsOutputDevice dc(gc, 1.0, 12.0);
Draw(dc, wxPLRealRect(0, 0, width, height / 2));
delete gc;
}
void wxPLTextLayoutDemo::OnSize(wxSizeEvent &) {
Refresh();
}
#include <wx/buffer.h>
#include <wx/zstream.h>
#include <wx/msgdlg.h>
#include <wex/utils.h>
#include <wex/pdf/pdffont.h>
#include <wex/pdf/pdffontmanager.h>
#include "fontdata.h"
static std::vector<std::vector<unsigned char> *> gs_inflatedFontData;
static bool inflate_builtin_fonts() {
size_t nfonts = 0;
while (builtinfonts[nfonts].data)
nfonts++;
if (gs_inflatedFontData.size() == nfonts)
return true;
for (size_t i = 0; i < gs_inflatedFontData.size(); i++)
if (gs_inflatedFontData[i] != 0)
delete gs_inflatedFontData[i];
gs_inflatedFontData.clear();
wxString errors;
for (size_t i = 0; i < nfonts; i++) {
wxMemoryInputStream in((const void *) builtinfonts[i].data, (size_t) builtinfonts[i].len);
wxZlibInputStream zin(in);
std::vector<unsigned char> *buf = new std::vector<unsigned char>;
buf->reserve(builtinfonts[i].len * 3);
while (zin.CanRead())
buf->push_back((unsigned char) zin.GetC());
gs_inflatedFontData.push_back(buf);
/*
wxPdfFontManager *fmgr = wxPdfFontManager::GetFontManager();
int pdfstyle = wxPDF_FONTSTYLE_REGULAR;
if ( builtinfonts[i].bold ) pdfstyle |= wxPDF_FONTSTYLE_BOLD;
if ( builtinfonts[i].italic ) pdfstyle |= wxPDF_FONTSTYLE_ITALIC;
if ( !fmgr->GetFont( builtinfonts[i].face, pdfstyle ).IsValid() )
{
wxPdfFont font = fmgr->RegisterFont( buf, builtinfonts[i].face, 0 );
if ( !font.IsValid() )
errors += "failed to register builtin freetype/pdf font: " + wxString(builtinfonts[i].face) + "\n";
}*/
}
if (errors.size() > 0)
wxShowTextMessageDialog(errors);
return gs_inflatedFontData.size() == nfonts;
}
#include <ft2build.h>
#include FT_FREETYPE_H
#define SUBPIXEL_RENDERING 1
//#define GAMMA_CORRECTION 1
#ifdef SUBPIXEL_RENDERING
#include FT_LCD_FILTER_H
#endif
static FT_Library ft_library = 0;
struct ft_face_info {
FT_Face face;
wxString font;
wxString file;
font_data *builtin;
wxMemoryBuffer data;
};
static std::vector<ft_face_info> ft_faces;
static bool check_freetype_init() {
if (ft_library == 0) {
FT_Error err = FT_Init_FreeType(&ft_library);
if (inflate_builtin_fonts()) {
int i = 0;
while (builtinfonts[i].data) {
FT_Face face;
FT_Error err_tmp = FT_New_Memory_Face(ft_library,
&(*gs_inflatedFontData[i])[0], gs_inflatedFontData[i]->size(),
0, &face);
if (0 == err_tmp) {
ft_face_info fi;
fi.face = face;
fi.font = builtinfonts[i].face;
fi.file = wxEmptyString;
fi.builtin = &builtinfonts[i];
ft_faces.push_back(fi);
}
i++;
}
}
#ifdef SUBPIXEL_RENDERING
if (err == 0) FT_Library_SetLcdFilter(ft_library, FT_LCD_FILTER_DEFAULT);
#endif
return err == 0;
} else
return true;
}
int wxFreeTypeLoadAllFonts(const wxString &path) {
wxString dir;
if (path.IsEmpty()) {
wxGetEnv("WEXDIR", &dir);
dir += "/pdffonts";
} else
dir = path;
int n = 0;
wxArrayString files;
wxDir::GetAllFiles(dir, &files, wxEmptyString, wxDIR_FILES);
for (size_t k = 0; k < files.size(); k++) {
wxFileName file(files[k]);
wxString ext(file.GetExt().Lower());
wxString name(file.GetName());
if (ext == "ttf" || ext == "otf") {
int face = wxFreeTypeLoadFont(files[k]);
if (face >= 0)
n++;
}
}
return n;
}
int wxFreeTypeLoadFont(const wxString &font_file) {
if (!check_freetype_init()) return -1;
for (size_t i = 0; i < ft_faces.size(); i++)
if (wxFileName(ft_faces[i].file).SameAs(wxFileName(font_file)))
return i;
FT_Face face;
FT_Error err = FT_New_Face(ft_library, font_file.c_str(), 0, &face);
if (0 == err) {
ft_face_info fi;
fi.face = face;
fi.font = wxFileName(font_file).GetName();
fi.file = font_file;
fi.builtin = NULL;
ft_faces.push_back(fi);
return ft_faces.size() - 1;
} else
return -1;
}
wxArrayString wxFreeTypeListFonts() {
check_freetype_init();
wxArrayString list;
for (size_t i = 0; i < ft_faces.size(); i++)
list.Add(ft_faces[i].font);
return list;
}
wxString wxFreeTypeFontFile(int ifnt) {
if (ifnt >= 0 && ifnt < (int) ft_faces.size())
return ft_faces[ifnt].file;
else
return wxEmptyString;
}
wxString wxFreeTypeFontName(int fnt) {
if (fnt >= 0 && fnt < (int) ft_faces.size())
return ft_faces[fnt].font;
else
return wxEmptyString;
}
bool wxFreeTypeFontStyle(int ifnt, bool *bold, bool *italic) {
if (ifnt >= 0 && ifnt < (int) ft_faces.size()) {
if (bold) *bold = (ft_faces[ifnt].face->style_flags & FT_STYLE_FLAG_BOLD) > 0;
if (italic) *italic = (ft_faces[ifnt].face->style_flags & FT_STYLE_FLAG_ITALIC) > 0;
return true;
} else
return false;
}
unsigned char *wxFreeTypeFontData(int ifnt, size_t *len) {
if (ifnt >= 0 && ifnt < (int) ft_faces.size()) {
if (ft_faces[ifnt].builtin && ifnt < (int) gs_inflatedFontData.size()) {
if (len) *len = gs_inflatedFontData[ifnt]->size();
return &(*gs_inflatedFontData[ifnt])[0];
}
}
return 0;
}
int wxFreeTypeFindFont(const wxString &font) {
wxString ll(font.Lower());
for (size_t i = 0; i < ft_faces.size(); i++)
if (ft_faces[i].font.Lower() == ll)
return (int) i;
return -1;
}
// see: https://bel.fi/alankila/lcd/
// and: https://bel.fi/alankila/lcd/alpcor.html
// also: https://www.freetype.org/freetype2/docs/text-rendering-general.html
unsigned char scale(int alpha, int color) {
return (unsigned char) (alpha * color / 255);
}
unsigned char blend(int alpha, int color1, int color2) {
return (unsigned char) ((color1 * alpha + (255 - alpha) * color2) / 255);
}
// blending uncorrected formula (same as GAMMA=1.0):
// double output = input1 * alpha + input2 * (1.0 - alpha);
//#define GAMMA 1.2
/* input1 = source color 1, 0 .. 1
* input2 = source color 2, 0 .. 1
* alpha = alpha blend factor, 0 .. 1
*/
// uncorrected formula (same as GAMMA=1.0):
// double output = input1 * alpha + input2 * (1.0 - alpha);
// corrected formula:
// double tmp = pow(input1, GAMMA) * alpha + pow(input2, GAMMA) * (1.0 - alpha);
// double output = pow(tmp, 1.0/GAMMA);
static void wxFreeTypeGlyph(unsigned char *rgb, unsigned char *alpha,
size_t width, size_t height,
unsigned char R, unsigned char G, unsigned char B,
FT_Bitmap *bitmap, int x, int y) {
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width
#ifdef SUBPIXEL_RENDERING
/ 3
#endif
;
FT_Int y_max = y + bitmap->rows;
for (i = x, p = 0; i < x_max; i++, p++) {
for (j = y, q = 0; j < y_max; j++, q++) {
if (i < 0 || j < 0 ||
i >= (int) width || j >= (int) height)
continue;
int index = j * width + i;
/*
//double input = bitmap->buffer[q * bitmap->width + p];
//alpha[ j*width + i ] = (unsigned char)(255.0/pow(255.0,1.0/GAMMA)*pow(input,1.0/GAMMA));
unsigned char A = bitmap->buffer[q * bitmap->width + p];
//rgb[ index + 0 ] = R;// alpha_correct( A, R );
//rgb[ index + 1 ] = G;// alpha_correct( A, G );
//rgb[ index + 2 ] = B;//alpha_correct( A, B );
alpha[ index ] = A;// alpha_correct( A, A );
*/
#ifdef SUBPIXEL_RENDERING
int pixpos = (q * bitmap->pitch + 3 * p);
unsigned char f_r = bitmap->buffer[pixpos];
unsigned char f_g = bitmap->buffer[pixpos + 1];
unsigned char f_b = bitmap->buffer[pixpos + 2];
// use green as alpha for overall intensity: http://alienryderflex.com/sub_pixel/
#ifdef GAMMA_CORRECTION
rgb[ 3*index ] = gamma_correct( 255-f_r, R );
rgb[ 3*index+1 ] = gamma_correct( 255-f_g, G );
rgb[ 3*index+2 ] = gamma_correct( 255-f_b, B );
alpha[index] = 255;
#else
if (f_g > 0 || f_g > 0 || f_b > 0) {
#if SUBPIXEL_BLACK_TEXT_ONLY
// produces good looking black text on horizontally oriented LCDs.
rgb[3 * index] = 255 - f_r;
rgb[3 * index + 1] = 255 - f_g;
rgb[3 * index + 2] = 255 - f_b;
#else
unsigned char A = f_g;
// colored text looks messed up
rgb[3 * index] = blend(255 - f_r, 255 - f_r, R);
rgb[3 * index + 1] = blend(255 - f_g, 255 - f_g, G);
rgb[3 * index + 2] = blend(255 - f_b, 255 - f_b, B);
#endif
alpha[index] = A;
}
#endif
#else
unsigned char A = bitmap->buffer[q * bitmap->pitch + p];
if (A > 0)
{
rgb[3 * index] = R;
rgb[3 * index + 1] = G;
rgb[3 * index + 2] = B;
alpha[index] = A;//blend( A, A, alpha[index] );
}
#endif
}
}
}
// see also : http://jcgt.org/published/0002/01/04/paper.pdf
static wxRealPoint rotate2d(
const wxRealPoint &P,
double angle) {
double rad = angle * M_PI / 180.0;
return wxRealPoint(
cos(rad) * P.x - sin(rad) * P.y,
sin(rad) * P.x + cos(rad) * P.y);
}
void wxFreeTypeDraw(wxDC &dc, const wxPoint &pos, int ifnt, double points, unsigned int dpi,
const wxString &text, const wxColour &c, double angle) {
wxRealPoint offset(0, 0);
wxImage img(wxFreeTypeDraw(&offset, ifnt, points, dpi, text, c, angle));
if (img.IsOk()) {
wxPoint offpt(pos.x - (int) offset.x, pos.y - (int) offset.y);
dc.DrawBitmap(wxBitmap(img), offpt);
/*
dc.SetPen( *wxLIGHT_GREY_PEN );
dc.SetBrush( *wxTRANSPARENT_BRUSH );
dc.DrawRectangle( offpt.x, offpt.y, img.GetWidth(), img.GetHeight() );
*/
}
}
#include <wex/utils.h>
void wxFreeTypeDraw(wxGraphicsContext &gc, const wxPoint &pos, int ifnt, double points, unsigned int dpi,
const wxString &text, const wxColour &c, double angle) {
wxRealPoint offset(0, 0);
wxImage img(wxFreeTypeDraw(&offset, ifnt, points, dpi, text, c, angle));
if (img.IsOk()) {
wxRealPoint offpt(pos.x - offset.x, pos.y - offset.y);
gc.DrawBitmap(wxBitmap(img), offpt.x, offpt.y, img.GetWidth(), img.GetHeight());
/*
gc.SetPen( *wxLIGHT_GREY_PEN );
gc.SetBrush( *wxTRANSPARENT_BRUSH );
gc.DrawRectangle( offpt.x, offpt.y, img.GetWidth(), img.GetHeight() );
*/
}
}
wxImage
wxFreeTypeDraw(wxRealPoint *offset, int ifnt, double points, unsigned int dpi, const wxString &text, const wxColour &c,
double angle) {
if (!check_freetype_init() || ft_faces.size() == 0 || text.IsEmpty() || ifnt < 0)
return wxNullImage;
if (ifnt >= (int) ft_faces.size())
ifnt = 0; // use default if invalid font
// first measure the text
wxSize size(wxFreeTypeMeasure(ifnt, points, dpi, text));
if (size.x == 0 || size.y == 0)
return wxNullImage;
wxRealPoint pp[3] = {wxRealPoint(0, -size.y), wxRealPoint(size.x, -size.y), wxRealPoint(size.x, 0)};
if (angle != 0.0)
for (size_t i = 0; i < 3; i++)
pp[i] = rotate2d(pp[i], angle);
// find bounding box of rotated text
wxRealPoint min(0, 0), max(0, 0);
for (size_t i = 0; i < 3; i++) {
if (pp[i].x < min.x) min.x = pp[i].x;
if (pp[i].x > max.x) max.x = pp[i].x;
if (pp[i].y < min.y) min.y = pp[i].y;
if (pp[i].y > max.y) max.y = pp[i].y;
}
// create image surface of appropriate size
wxSize bounds(std::abs(max.x - min.x), std::fabs(max.y - min.y));
wxImage img(bounds, false);
// find offset coordinate for top-left placement
*offset = wxPoint(-min.x, max.y);
// render the text
wxFreeTypeDraw(&img, true, *offset, ifnt, points, dpi, text, c, angle);
return img;
}
void wxFreeTypeDraw(wxImage *img, bool init_img, const wxPoint &pos,
int ifnt, double points, unsigned int dpi,
const wxString &text, const wxColour &c, double angle) {
if (!check_freetype_init() || ft_faces.size() == 0 || text.IsEmpty() || ifnt < 0 || !img)
return;
if (ifnt >= (int) ft_faces.size())
ifnt = 0;
wxSize size(img->GetWidth(), img->GetHeight());
if (!img->HasAlpha())
img->InitAlpha();
FT_Face face = ft_faces[ifnt].face;
FT_Error err = FT_Set_Char_Size(face, (int) (points * 64.0), 0, dpi, dpi);
unsigned char R = c.Red();
unsigned char G = c.Green();
unsigned char B = c.Blue();
unsigned char *rgb = img->GetData();
unsigned char *alpha = img->GetAlpha();
if (init_img) {
// initialize rgb and alpha
for (int i = 0; i < size.x * size.y; i++) {
rgb[3 * i] = R;
rgb[3 * i + 1] = G;
rgb[3 * i + 2] = B;
alpha[i] = 0;
}
}
angle *= M_PI / 180;
double pix_ascent = ((double) face->ascender) / ((double) face->units_per_EM) * points * dpi / 72.0;
wxRealPoint origin(pos.x, pos.y);
origin.x += pix_ascent * sin(angle);
origin.y += pix_ascent * cos(angle);
FT_Matrix matrix;
matrix.xx = (FT_Fixed) (cos(angle) * 0x10000L);
matrix.xy = (FT_Fixed) (-sin(angle) * 0x10000L);
matrix.yx = (FT_Fixed) (sin(angle) * 0x10000L);
matrix.yy = (FT_Fixed) (cos(angle) * 0x10000L);
bool use_kerning = (FT_HAS_KERNING(face) > 0);
FT_UInt previous = 0, glyph_index;
FT_Vector pen;
pen.x = (int) (origin.x * 64);
pen.y = (int) ((size.y - origin.y) * 64.0);
for (wxString::const_iterator it = text.begin(); it != text.end(); ++it) {
FT_Set_Transform(face, &matrix, &pen);
glyph_index = FT_Get_Char_Index(face, *it);
/* retrieve kerning distance and move pen position */
if (use_kerning && previous && glyph_index) {
FT_Vector delta;
FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
FT_Vector_Transform(&delta, &matrix);
pen.x += delta.x >> 6;
pen.y += delta.y >> 6;
}
unsigned int mode = FT_LOAD_DEFAULT;
if (angle != 0.0) // see: http://chanae.walon.org/pub/ttf/ttf_glyphs.htm
mode |= FT_LOAD_NO_HINTING;
#ifdef SUBPIXEL_RENDERING
mode |= FT_LOAD_TARGET_LCD;
#endif
err = FT_Load_Glyph(face, glyph_index, mode);
if (err) continue;
err = FT_Render_Glyph(face->glyph,
#ifdef SUBPIXEL_RENDERING
FT_RENDER_MODE_LCD
#else
FT_RENDER_MODE_NORMAL
#endif
);
if (err) continue;
wxFreeTypeGlyph(rgb, alpha, size.x, size.y, R, G, B,
&face->glyph->bitmap,
face->glyph->bitmap_left,
size.y - face->glyph->bitmap_top);
// increment pen pos