forked from yeo32cjf/hmltotex_open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhwpEqToTex.cpp
2459 lines (2109 loc) · 53.1 KB
/
hwpEqToTex.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
#include "hwpEqToTex.h"
#define OPENPARTMP 1
#define CLOSEPARTMP 2
int spcsize;
int funcsize;
int spcnextsize;
int spcunicodesize;
int remainsize;
int hwpParOutSize;
int oneparsize;
int twoparsize;
vector<wstring> texFuncSorted;
vector<wstring> hwpParameterOutKeyword;
vector<funcList> funcSorted;
vector<funcList> funcRemainSorted;
vector<wstring> funcSorted_t;
vector<wstring> funcRemainSorted_t;
vector<funcList> func;
wstring oneParameter[] = { L"\\sqrt",L"\\mrm",L"\\vec",L"\\vrm",L"\\ovr",L"\\ovl",L"\\begintabu"
,L"\\sqrt",L"_",L"^",L"\\begin",L"\\end",L"\\framebox"
,L"\\overleftrightarrow",L"\\acute",L"\\grave",L"\\ddot",L"\\dot"
,L"\\underline",L"\\hat",L"\\check",L"\\arc",L"\\widetilde" };
wstring twoParameter[] = { L"\\dfrac" };
//괄호로 인수가 묶여있는지 확인해야 하는 경우
wstring needToWrap[] = { L"\\ovr",L"\\vec",L"\\framebox"
,L"\\overleftrightarrow",L"\\acute",L"\\grave",L"\\ddot",L"\\dot"
,L"\\underline",L"\\hat",L"\\check",L"\\arc",L"\\widetilde" };
wchar_t openpartmp[] = { OPENPARTMP,0 }; //여는 중괄호 대체 문자열
wchar_t endpartmp[] = { CLOSEPARTMP,0 }; //닫는 중괄호 대체 문자열
wstring spchwp[] = { L"<",L">",L"&" };
wstring spcTex[] = { L"<",L">",L"&" };
wstring funchwp[] = { L"sinh",L"cosh",L"tanh",L"sech",L"csch",L"cosech",L"coth"
,L"arcsin",L"arccos",L"arctan"
,L"csc",L"cosec",L"sec",L"cot",L"sin",L"cos",L"tan",L"log",L"ln",L"lg"
,L"max",L"min",L"arc",L"arg",L"Exp",L"exp",L"det",L"gcd",L"mod"
,L"if",L"for",L"and",L"hom",L"ker",L"deg",L"dim",L"DEG"
,L"APPROX",L"SIMEQ",L"CONG",L"ASYMP",L"ISO",L"DIAMOND",L"DSUM",L"FORALL"
,L"Partial",L"LNOT",L"PROPTO",L"XOR",L"NABLA",L"DDAGGER",L"DAGGER"
,L"CDOTS",L"LDOTS",L"VDOTS",L"DDOTS",L"TRIANGLE",L"MSANGLE",L"SANGLE",L"RTANGLE"
,L"VDASH",L"DASHV",L"TOP",L"MODELS",L"LAPLACE",L"CENTIGRADE",L"FAHRENHEIT"
,L"LSLANT",L"RSLANT",L"ATT",L"HUND",L"THOU",L"WELL",L"BASE",L"BENZENE"
,L"SIM",L"INF",L"INFTY",L"rightarrow"
,L"BULLET",L"AST",L"STAR",L"BIGCIRC",L"THEREFORE",L"BECAUSE",L"IDENTICAL",L"EXIST",L"DOTEQ",L"image",L"REIMAGE"
,L"NOTOWNS",L"notOWNS",L"NOTIN",L"OWNS",L"SQSUBSETEQ",L"SQSUBSET",L"UPLUS"
,L"SMALLSUM",L"SMALLPROD",L"SMCOPROD",L"SQCAP",L"SQCUP"
,L"OPLUS",L"OMINUS",L"OTIMES",L"ODIV",L"ODOT",L"LOR",L"LAND"
,L"SUBSETEQ",L"SUPSETEQ",L"SUBSET",L"SUPERSET"
,L"larrow",L"rarrow",L"uparrow",L"downarrow",L"LARROW",L"LRARROW",L"DOWNARROW",L"UPARROW",L"udarrow"
,L"UDARROW",L"RARROW",L"NWARROW",L"SEARROW",L"NEARROW",L"SWARROW"
,L"HOOKLEFT",L"HOOKRIGHT",L"MAPSTO",L"PVER"
,L"CUP",L"CAP",L"SMALLINTER",L"SMALLUNION",L"EMPTYSET",L"cdot",L"CDOT"
,L"IN",L"SUCC",L"PREC"
,L"ALEPH",L"hbar",L"imath",L"jmath",L"Ω",L"LITER",L"WP",L"ANGSTROM"
,L"vartheta",L"varpi",L"varsigma",L"varupsilon",L"varphi",L"varepsilon"
,L"LEFT",L"RIGHT"
,L"bar",L"vec",L"box"
,L"dyad",L"acute",L"grave",L"ddot",L"dot",L"under",L"hat",L"check",L"arch",L"tilde"
,L"ANGLE",L"rm",L"it",L"BOT"
,L"ALPHA",L"BETA",L"GAMMA",L"DELTA",L"EPSILON",L"ZETA",L"ETA",L"THETA"
,L"IOTA",L"KAPPA",L"LAMBDA",L"MU",L"NU",L"XI",L"PI",L"RHO"
,L"SIGMA",L"TAU",L"UPSILON",L"PHI",L"CHI",L"PSI",L"OMEGA"
,L"alpha",L"beta",L"gamma",L"delta",L"epsilon",L"zeta",L"eta",L"theta"
,L"iota",L"kappa",L"lambda",L"mu",L"nu",L"xi",L"pi",L"rho"
,L"sigma",L"tau",L"upsilon",L"phi",L"chi",L"psi",L"omega"
,L"sum",L"prod",L"coprod",L"inter",L"union",L"BIGSQCAP",L"BIGSQCUP"
,L"BIGOPLUS",L"BIGOMINUS",L"BIGOTIMES",L"BIGODIV",L"BIGODOT",L"BIGUPLUS"
,L"int",L"dint",L"tint",L"oint",L"odint",L"otint"
,L"leq",L"geq",L"le",L"ge"
,L"sqrt",L"root",L"lim",L"Lim",L"over"
,L"times",L"prime",L"circ"
,L"pile"
};
wstring funcTex[] = { L"\\sinh",L"\\cosh",L"\\tanh"
,L"\\operatorname{sech}",L"\\operatorname{sech}",L"\\operatorname{cosech}",L"\\operatorname{coth}"
,L"\\arcsin",L"\\arccos",L"\\arctan"
,L"\\csc",L"\\operatorname{cosec}",L"\\sec",L"\\cot",L"\\sin",L"\\cos",L"\\tan",L"\\log",L"\\ln",L"\\lg"
,L"\\max",L"\\min",L"\\mrm{arc}",L"\\arg",L"\\operatorname{Exp}",L"\\exp",L"\\det",L"\\gcd",L"\\bmod"
,L"\\mrm{if}",L"\\mrm{for}",L"\\mrm{and}",L"\\hom",L"\\ker",L"\\deg",L"\\dim",L"^{\\circ}"
,L"\\approx",L"\\simeq",L"\\cong",L"\\asymp",L"\\Bumpeq",L"\\diamond",L"\\dotplus",L"\\forall"
,L"\\partial",L"\\lnot",L"\\propto",L"\\veebar",L"\\nabla",L"\\ddagger",L"\\dagger"
,L"\\cdots",L"\\ldots",L"\\vdots",L"\\ddots",L"\\triangle",L"\\measuredangle",L"\\sphericalangle",L"rtangle"
,L"\\vdash",L"\\dashv",L"\\top",L"\\models",L"\\laplace",L"\\centigrade",L"\\fahrenhe"
,L"\\diagup",L"\\diagdown",L"att",L"hund",L"thou",L"well",L"base",L"benzene"
,L"\\sim",L"\\infty",L"\\infty",L"\\to"
,L"\\bullet",L"\\ast",L"\\star",L"\\bigcirc",L"\\therefore",L"\\because",L"identical",L"\\exists",L"\\doteq",L"\\fallingdotseq",L"\\risingdotseq"
,L"\\not\\ni",L"\\not\\ni",L"\\notin",L"\\ni",L"\\sqsubseteq",L"\\sqsubset",L"\\uplus"
,L"\\Sigma",L"\\Pi",L"\\amalg",L"\\sqcap",L"\\sqcup"
,L"\\oplus",L"\\ominus",L"\\otimes",L"\\oslash",L"\\odot",L"\\lor",L"\\land"
,L"\\subseteq",L"\\supseteq",L"\\subset",L"\\supset"
,L"\\leftarrow",L"\\rightarrow",L"\\uparrow",L"\\downarrow",L"\\Leftarrow",L"\\Leftrightarrow",L"\\Downarrow",L"\\Uparrow",L"\\updownarrow"
,L"\\Updownarrow",L"\\Rightarrow",L"\\nwarrow",L"\\searrow",L"\\nearrow",L"\\swarrow"
,L"\\hookleftarrow",L"\\hookrightarrow",L"\\mapsto",L"\\Vert"
,L"\\cup",L"\\cap",L"\\cap",L"\\cup",L"\\varnothing",L"\\bcd",L"\\bcd"
,L"\\in",L">",L"<"
,L"\\aleph",L"\\hbar",L"\\imath",L"\\jmath",L"\\Omega",L"\\ell",L"\\wp",L"ANGSTROM"
,L"\\vartheta",L"\\varpi",L"\\varsigma",L"\\varupsilon",L"\\varphi",L"\\varepsilon"
,L"\\left",L"\\right"
,L"\\ovl",L"\\vec",L"" //L"\\framebox"
,L"\\overleftrightarrow",L"\\acute",L"\\grave",L"\\ddot",L"\\dot",L"\\underline",L"\\hat",L"\\check",L"\\arc",L"\\widetilde"
,L"\\angle",L"\\xrm",L"\\it",L"\\bot"
,L"A",L"B",L"\\Gamma",L"\\Delta",L"E",L"Z",L"H",L"\\Theta"
,L"I",L"K",L"\\Lambda",L"M",L"N",L"\\Xi",L"\\Pi",L"P"
,L"\\Sigma",L"T",L"Y",L"\\Phi",L"X",L"\\Psi",L"\\Omega"
,L"\\alpha",L"\\beta",L"\\gamma",L"\\delta",L"\\epsilon",L"\\zeta",L"\\eta",L"\\theta"
,L"\\iota",L"\\kappa",L"\\lambda",L"\\mu",L"\\nu",L"\\xi",L"\\pi",L"\\rho"
,L"\\sigma",L"\\tau",L"\\upsilon",L"\\phi",L"\\chi",L"\\psi",L"\\omega"
,L"\\sum",L"\\prod",L"\\coprod",L"\\bigcap",L"\\bigcup",L"\\bigsqcap",L"\\bigsqcup"
,L"\\bigoplus",L"BIGOMINUS",L"\\bigotimes",L"BIGODIV",L"\\bigodot",L"\\biguplus"
,L"\\int",L"\\iint",L"\\iiint",L"\\oint",L"oiint",L"oiiint"
,L"\\le",L"\\ge",L"\\le",L"\\ge"
,L"\\sqrt",L"\\sqrt",L"\\lim",L"\\operatorname{Lim}",L"\\kfrac"
,L"\\times",L"'",L"\\circ"
,L"\\pile"
};
wstring spchwpNext[] = { L",",L"%",L"-+",L"!=",L"<->",L"->"
,L"<<<",L">>>",L"<<",L">>",L"==",L"<=",L">=",L"+-",L"-+"
};
wstring spcTexNext[] = { L",\\: ",L"\\% ",L"\\mp ",L"\\ne ",L"\\leftrightarrow ",L"\\to "
,L"\\lll ",L"\\ggg ",L"\\ll ",L"\\gg ",L"\\equiv ",L"\\le ",L"\\ge ",L"\\pm ",L"\\mp "
};
//± ÷ ⊒ ⊐ °
wchar_t spcUnicode1[] = { 0xC2,0xB1,0x00 };
wchar_t spcUnicode2[] = { 0xC3,0xB7,0x00 };
wchar_t spcUnicode3[] = { 0xE2,0x8A,0x92,0x00 };
wchar_t spcUnicode4[] = { 0xE2,0x8A,0x90,0x00 };
wchar_t spcUnicode5[] = { 0xEE,0x81,0x8D,0x00 };
wchar_t spcUnicode6[] = { 0xC2,0xB0,0x00 };
wchar_t spcUnicode7[] = { 0xE2,0x88,0xA0,0x00 };
wchar_t* hwpUnicodeSpc[] = { spcUnicode1,spcUnicode2,spcUnicode3,spcUnicode4,spcUnicode5,spcUnicode6,spcUnicode7 };
wchar_t* texUnicodeSpc[] = { L"\\pm ",L"\\div ",L"\\sqsupseteq ",L"\\sqsupset ",L"\\vert ",L"^{\\circ}",L"\\angle " };
int hwpUnicodeSpcLen[] = { 2,2,3,3,3,2,3 };
int texUnicodeSpcLen[] = { 4,5,12,10,6,8,7 };
wstring funcRemain[] = { L"\\xrm",L"\\beginttabuu",L"\\laplace",L"\\centigrade",L"\\fahrenhe"
,L"$ ",L" $",L"{ ",L" {",L" }",L"} ",L"( ",L" (",L" )",L") ",L"[ ",L" [",L" ]",L"] "
,L" \\",L" '",L"' ",L" ^",L"^ ",L" _",L"_ "
,L"\\mrm{}",L"#",L"\\it",L"~",L"\n ",L"\n\n" };
wstring funcRemainNext[] = { L"\\mrm",L"\\begin{tabu}",L"\\mathcal{L}",L"^{\\circ}\\mrm{C}",L"{}^{\\circ}\\mrm{F}"
,L"$",L"$",L"{",L"{",L"}",L"}",L"(",L"(",L")",L")",L"[",L"[",L"]",L"]"
,L"\\",L"'",L"'",L"^",L"^",L"_",L"_"
,L"",L"\\\\",L"",L"",L"\n",L"\n" };
wstring parameterWrap[] = { L"sinh",L"cosh",L"tanh",L"sech",L"csch",L"cosech",L"coth"
,L"arcsin",L"arccos",L"arctan"
,L"csc",L"cosec",L"sec",L"cot",L"sin",L"cos",L"tan",L"log",L"ln",L"lg"
,L"max",L"min",L"arg",L"Exp",L"exp",L"gcd",L"mod"
,L"if",L"for",L"and",L"hom",L"ker",L"deg",L"dim"
,L"NOTOWNS",L"notOWNS"
,L"LEFT",L"RIGHT"
,L"bar",L"vec",L"box"
,L"dyad",L"acute",L"grave",L"ddot",L"dot",L"under",L"hat",L"check",L"arch",L"tilde"
,L"rm",L"it"
,L"sum",L"prod",L"coprod",L"inter",L"union",L"BIGSQCAP",L"BIGSQCUP"
,L"BIGOPLUS",L"BIGOMINUS",L"BIGOTIMES",L"BIGODIV",L"BIGODOT",L"BIGUPLUS"
,L"int",L"dint",L"tint",L"oint",L"odint",L"otint"
,L"sqrt",L"root",L"lim",L"Lim",L"over"
,L"pile"
};
wstring doubleSubSupList[] = { L"\\sum",L"\\prod",L"\\int",L"\\iint",L"\\iiint",L"\\oint"
,L"\\bigcup",L"\\bigcap", L"\\bigsqcup", L"\\biguplus"
,L"\\bigvee \\bigwedge \\bigoplus",L" \\bigotimes",L" \\bigodot"
};
const bool operator<(const funcList& a, const funcList& b)
{
int n = _wcsicmp(a.hwp->data(), b.hwp->data());
if (n)
return n < 0;
return wcscmp(a.hwp->data(), b.hwp->data()) < 0;
}
void initializeEqValue(int argc, char** argv)
{
spcsize = sizeof(spchwp) / sizeof(wstring); //특수기호 개수
funcsize = sizeof(funchwp) / sizeof(wstring); //
spcnextsize = sizeof(spchwpNext) / sizeof(wstring);
spcunicodesize = sizeof(hwpUnicodeSpc) / sizeof(wchar_t*);
remainsize = sizeof(funcRemain) / sizeof(wstring);
hwpParOutSize = sizeof(parameterWrap) / sizeof(wstring);
oneparsize = sizeof(oneParameter) / sizeof(wstring);
twoparsize = sizeof(twoParameter) / sizeof(wstring);
//한글 수식에서 의미가 있는 문자열 읽기
for (int i = 0; i < funcsize; i++)
{
funcSorted.push_back(funcList());
funcSorted[i].hwp = &funchwp[i];
funcTex[i] += CLOSEPARTMP;
funcSorted[i].Tex = &funcTex[i];
}
for (int i = 0; i < funcsize; i++)
{
if (isAlphabet(funcTex[i][0]) == false)
{
texFuncSorted.push_back(funcTex[i]);
int n = texFuncSorted.back().find(' ');
if (n != wstring::npos)
texFuncSorted.back().erase(n, 1);
}
}
texFuncSorted.push_back(L"\\circ");
sort(texFuncSorted.begin(), texFuncSorted.end(), compareStrIgnore());
for (int i = 0; i < hwpParOutSize; i++)
{
hwpParameterOutKeyword.push_back(parameterWrap[i]);
}
sort(hwpParameterOutKeyword.begin(), hwpParameterOutKeyword.end(), compareStrIgnore());
//항상 바뀌어야 하는 문자열 읽기
for (int i = 0; i < remainsize; i++)
{
funcRemainSorted.push_back(funcList());
funcRemainSorted[i].hwp = &funcRemain[i];
funcRemainSorted[i].Tex = &funcRemainNext[i];
}
//이분 검색을 위해 정렬
sort(funcSorted.begin(), funcSorted.end());
sort(funcRemainSorted.begin(), funcRemainSorted.end());
}
bool bSearchTexFunc(wstring& str)
{
int upper, lower, mid;
lower = 0;
upper = texFuncSorted.size() - 1;
while (1)
{
mid = (upper + lower) / 2;
int res = wcsicmp(texFuncSorted[mid].data(), str.data());
if (res == 0)
return true;
if (res > 0)
upper = mid - 1;
else
lower = mid + 1;
if (upper < lower)
return false;
}
}
inline int findParMatch(wstring& str, int start) //str[start]가 {라고 가정
{
int parLev = 1;
for (int i = start + 1; str[i]; i++)
{
if (str[i] == '{')
++parLev;
else if (str[i] == '}')
--parLev;
if (parLev == 0)
return i + 1;
}
}
bool getParameterRange(wstring& str, int start, int* from, int* to)
{
int n = start;
int res = 0;
*from = n;
vector<int> pos;
pos.push_back(n);
while (pos.empty() == false)
{
n = pos.back();
pos.pop_back();
findNotSpace(str, n);
//최초의 공백 아닌 문자가 여는 괄호일 때
if (str[n] == '{')
{
int parLev = 1;
while (1)
{
++n;
if (str[n] == '{')
++parLev;
else if (str[n] == '}')
--parLev;
else if (str[n] == '$')
{
if (parLev)
{
for (int i = 0; i < parLev; i++)
{
str.insert(n, L"}");
parLev = 0;
break;
}
}
}
if (parLev == 0)
break;
}
++n;
}
else
{
if (str[n] == OPENPARTMP)
str[n++] = ' ';
while (str[n] != '$')
{
if (str[n] == '{' || str[n] == '}' || str[n] == ' ' || str[n] == '^' || str[n] == '_' || str[n] == '`')
break;
++n;
}
++res;
}
int tmp = n;
findNotSpace(str, tmp);
if (str[tmp] == '^' || str[tmp] == '_')
{
pos.push_back(tmp + 1);
++res;
}
}
*to = n;
return (res == 0);
}
bool getParameterRangeBack(wstring& str, int start, int* from, int* to)
{
int n = start;
int res = 0;
*to = n;
--n;
vector<int> pos;
pos.push_back(n);
while (pos.empty() == false)
{
n = pos.back();
pos.pop_back();
findNotSpaceBack(str, n);
//최초의 공백 아닌 문자가 닫는 괄호일 때
if (str[n] == '}')
{
int parLev = -1;
--n;
while (n != 1)
{
if (str[n] == '{')
++parLev;
else if (str[n] == '}')
--parLev;
if (parLev == 0)
break;
--n;
}
}
else
{
while (n != 0)
{
if (str[n] == '{' || str[n] == '}' || str[n] == ' ' || str[n] == '^' || str[n] == '_' || str[n] == '`')
break;
--n;
}
++n;
++res;
}
int tmp = n - 1;
findNotSpaceBack(str, tmp);
if (str[tmp] == '^' || str[tmp] == '_')
{
pos.push_back(tmp - 1);
++res;
}
}
*from = n;
return (res == 0);
}
int smartInsert(wstring& str, int pos, wchar_t* p, int* insertedPos)
{
int min = pos - 1, max = pos;
int len = wcslen(p);
for (; str[min] == ' '; min--) {}
for (; str[max] == ' '; max++) {}
++min;
//[min,max)는 공백의 범위
if (max - min >= len)
{
for (int i = 0; i < len; i++)
{
str[max - len + i] = p[i];
}
if (insertedPos)
*insertedPos = max - len;
return 0;
}
else
{
for (int i = 0; i < max - min; i++)
{
str[min + i] = p[i];
}
str.insert(max, p + max - min);
if (insertedPos)
*insertedPos = min;
return len - max + min;
}
}
//wstring형 변수 source에서 start의 위치부터 함수명 검색 (함수명 배열:ar,크기:nunm)
//대소문자를 구분하지 않고 한글 함수명 검색, ar는 오름차순으로 정렬되어 있어야 함
int bSearchAlphabet(wstring& source, int start, vector<funcList>& ar, int num)
{
int upper, lower, mid;
lower = 0;
upper = num - 1;
const wchar_t* src = source.data() + start;
//wcout << source.data()+start << endl;
while (1)
{
mid = (upper + lower) / 2;
wstring* midval = ar[mid].hwp;
int overlapNum;
int findRes = compareSubStr(src, midval->data(), midval->size(), &overlapNum);
int direction = 0; //대소문자를 고려해야 할 때, 검색 방향
//wcout << *midval << " : " << overlapNum << " : " << findRes << endl;
if (findRes == 0 || findRes != 0 && overlapNum >= 2) //mid의 위치에서 앞으로도 찾고 뒤로도 찾음
{
int m = mid;
int prevOverlapNum = overlapNum;
int nextOverlapNum = overlapNum;
for (int i = m + 1; i < num; i++)
{
wstring* nextval = ar[i].hwp;
int oNum;
int findNext = compareSubStr(src, nextval->data(), nextval->size(), &oNum);
//wcout << "Next : " << *nextval << " : " << oNum << " : " << findNext << endl;
if (oNum >= overlapNum)
{
if (findNext == 0)
{
findRes = 0;
direction = 1;
mid = i;
midval = nextval;
}
nextOverlapNum = oNum;
}
else if (oNum < overlapNum)
break;
}
if (findRes != 0)
{
for (int i = m - 1; i >= 0; i--)
{
wstring* prevval = ar[i].hwp;
int oNum;
int findPrev = compareSubStr(src, prevval->data(), prevval->size(), &oNum);
//wcout << "Prev : " << *prevval << " : " << oNum << " : " << findPrev << endl;
if (findPrev == 0)
{
findRes = 0;
direction = -1;
mid = i;
midval = prevval;
}
else if (oNum < 2)
break;
}
}
}
if (findRes > 0)
lower = mid + 1;
else if (findRes < 0)
upper = mid - 1;
else
{
if (direction != 0)
{
wchar_t firstLattermid = (*midval)[0];
wchar_t firstLattersrc = source[start];
//source의 시작 문자, 검색된 문자열의 첫 문자가 모두 대문자이거나 모두 소문자일 때
if (firstLattermid >= 'A' && firstLattermid <= 'Z' && firstLattersrc >= 'A' && firstLattersrc <= 'Z'
|| firstLattermid >= 'a' && firstLattermid <= 'z' && firstLattersrc >= 'a' && firstLattersrc <= 'z')
return mid;
wstring* compval = ar[mid - direction].hwp;
//wcout << "comp : " << direction << " : " << *midval << " , " << *compval << endl;
//mid의 대문자가 prev인 경우
if (_wcsicmp(midval->data(), compval->data()) == 0)
{
//prev는 대문자, mid는 소문자를 가리킴
//대소문자를 무시하면 prev와 mid는 같음
//source의 시작 문자가 대문자인 경우
if (direction < 0 && firstLattersrc >= 'a' && firstLattersrc <= 'z'
|| direction > 0 && firstLattersrc >= 'A' && firstLattersrc <= 'Z')
return mid - direction;
else
return mid;
}
}
return mid;
}
if (upper < lower)
return wstring::npos;
}
}
int bSearchOutParameter(wstring& source, int start, vector<wstring>& ar)
{
int upper, lower, mid;
int num = ar.size();
lower = 0;
upper = num - 1;
const wchar_t* src = source.data() + start;
//wcout << source.data()+start << endl;
while (1)
{
mid = (upper + lower) / 2;
wstring* midval = &ar[mid];
int overlapNum;
int findRes = compareSubStr(src, midval->data(), midval->size(), &overlapNum);
int direction = 0; //대소문자를 고려해야 할 때, 검색 방향
//wcout << *midval << " : " << overlapNum << " : " << findRes << endl;
if (findRes == 0 || findRes != 0 && overlapNum >= 2) //mid의 위치에서 앞으로도 찾고 뒤로도 찾음
{
int m = mid;
int prevOverlapNum = overlapNum;
int nextOverlapNum = overlapNum;
for (int i = m + 1; i < num; i++)
{
wstring* nextval = &ar[i];
int oNum;
int findNext = compareSubStr(src, nextval->data(), nextval->size(), &oNum);
//wcout << "Next : " << *nextval << " : " << oNum << " : " << findNext << endl;
if (oNum >= overlapNum)
{
if (findNext == 0)
{
findRes = 0;
direction = 1;
mid = i;
midval = nextval;
}
nextOverlapNum = oNum;
}
else if (oNum < overlapNum)
break;
}
if (findRes != 0)
{
for (int i = m - 1; i >= 0; i--)
{
wstring* prevval = &ar[i];
int oNum;
int findPrev = compareSubStr(src, prevval->data(), prevval->size(), &oNum);
//wcout << "Prev : " << *prevval << " : " << oNum << " : " << findPrev << endl;
if (findPrev == 0)
{
findRes = 0;
direction = -1;
mid = i;
midval = prevval;
}
else if (oNum < 2)
break;
}
}
}
if (findRes > 0)
lower = mid + 1;
else if (findRes < 0)
upper = mid - 1;
else
{
if (direction != 0)
{
wchar_t firstLattermid = (*midval)[0];
wchar_t firstLattersrc = source[start];
//source의 시작 문자, 검색된 문자열의 첫 문자가 모두 대문자이거나 모두 소문자일 때
if (firstLattermid >= 'A' && firstLattermid <= 'Z' && firstLattersrc >= 'A' && firstLattersrc <= 'Z'
|| firstLattermid >= 'a' && firstLattermid <= 'z' && firstLattersrc >= 'a' && firstLattersrc <= 'z')
return mid;
wstring* compval = &ar[mid - direction];
//wcout << "comp : " << direction << " : " << *midval << " , " << *compval << endl;
//mid의 대문자가 prev인 경우
if (_wcsicmp(midval->data(), compval->data()) == 0)
{
//prev는 대문자, mid는 소문자를 가리킴
//대소문자를 무시하면 prev와 mid는 같음
//source의 시작 문자가 대문자인 경우
if (direction < 0 && firstLattersrc >= 'a' && firstLattersrc <= 'z'
|| direction > 0 && firstLattersrc >= 'A' && firstLattersrc <= 'Z')
return mid - direction;
else
return mid;
}
}
return mid;
}
if (upper < lower)
return wstring::npos;
}
}
inline bool isAlphabet(wchar_t p)
{
return p >= 'a' && p <= 'z' || p >= 'A' && p <= 'Z';
}
inline int compareSubStr(const wchar_t* str1, const wchar_t* str2, int num, int* overlapNum)
{
for (int i = 0; i < num; i++)
{
int c1 = tolower(str1[i]);
int c2 = tolower(str2[i]);
if (c1 != c2)
{
*overlapNum = i;
return (c1 > c2) ? 1 : -1;
}
}
*overlapNum = num;
return 0;
}
inline void findNotSpace(wstring& str, int& val)
{
for (; str[val] == ' ' || str[val] == CLOSEPARTMP; val++);
}
inline void findNotSpaceBack(wstring& str, int& val)
{
for (; str[val] == ' ' || str[val] == CLOSEPARTMP; val--);
}
inline void findRangeStart(wstring& str, int start, int& val)
{
int parLev = 0;
int i;
for (i = start; str[i] != '$'; i--)
{
if (str[i] == '{' && str[i - 1] != '\\')
++parLev;
else if (str[i] == '}' && str[i - 1] != '\\')
--parLev;
if (parLev > 0)
break;
}
val = i + 1;
}
inline void findRangeEnd(wstring& str, int start, int& val)
{
int parLev = 0;
int i;
for (i = start; str[i] != '$'; i++)
{
if (str[i] == '{' && str[i - 1] != '\\')
++parLev;
else if (str[i] == '}' && str[i - 1] != '\\')
--parLev;
if (parLev < 0)
break;
}
val = i;
}
int deleqalign(wstring& str) //eqalign을 처리함
{
int start = str.find(L"eqalign");
if (start == wstring::npos)
return 1;
int end = 0;
vector<int> eqstart; //eqalign의 위치
vector<int> eqmid; //eqalign의 여는 괄호의 위치
vector<int> eqend; //eqalign의 닫는 괄호의 위치
vector<int> searchBack; //뒤로 검색 여부
vector<int> searchNext; //앞으로 검색 여부
vector<int> isMatrix; //true이면 행렬로, false이면 \begin{aligned} \end{aligned}로 수식을 표현
vector<int> sharpNum; //하나의 eqalign에서 #의 개수
eqstart.push_back(0);
eqmid.push_back(0);
eqend.push_back(0);
searchBack.push_back(false);
searchNext.push_back(false);
isMatrix.push_back(false);
sharpNum.push_back(0);
int i = 1;
while (1) //수식 내에 존재하는 모든 eqalign을 찾음
{
end = start + 7;
for (; str[end] != '{'; end++) {} //처음으로 등장하는 여는 괄호를 찾음
eqstart.push_back(start);
eqmid.push_back(end);
sharpNum.push_back(0);
int parLevel = 0;
while (1) //닫는 괄호를 찾음
{
++end;
switch (str[end])
{
case '{':
++parLevel;
break;
case '}':
--parLevel;
break;
case '#':
if (parLevel == 0)
++sharpNum[i];
break;
}
if (parLevel == -1)
break;
}
eqend.push_back(end);
start = end + 1;
searchBack.push_back(true);
searchNext.push_back(true);
isMatrix.push_back(true);
start = str.find(L"eqalign", start);
if (start == wstring::npos)
break;
++i;
}
eqstart.push_back(str.size() - 1);
eqmid.push_back(str.size() - 1);
eqend.push_back(str.size() - 1);
searchBack.push_back(true);
searchNext.push_back(false);
isMatrix.push_back(false);
sharpNum.push_back(0);
//i번째의 eqend와 i+1번째의 eqstart 사이에 &가 있으면 eqalign은 행렬처럼 사용된 것
start = 1; //start번째 eqalign과 end번째의 eqalign이 모두 행렬로 사용된 경우
end = 1;
for (i = 1; i < eqstart.size() - 1; i++)
{
int parLevel = 0;
int beginLevel = 0;
bool hasLinebreak = false;
int startSearch = 0;
int j = 0;
int k = 0;
int andnum = 0; //인접한 eqalign 사이에 &가 1개인 경우, 행렬로 처리
if (searchBack[i] == true)
{
parLevel = 0;
beginLevel = 0;
for (k = eqstart[i] - 1; k >= eqend[i - 1] + 1; k--)
{
switch (str[k])
{
case '{':
++parLevel;
if (parLevel == 0)
{
if (str.find(L"cases}", k + 1, 6) == k + 1
|| str.find(L"matrix}", k + 2, 7) == k + 2
|| str.find(L"tabu}", k + 1, 5) == k + 1)
{
if (str.find(L"\\begin", k - 6, 6) == k - 6)
{
k -= 6;
++beginLevel;
}
else if (str.find(L"\\end", k - 4, 4) == k - 4)
{
k -= 4;
--beginLevel;
isMatrix[i] = false;
searchNext[i] = false;
}
}
else if (str[k + 1] == 'c')
{
int a;
for (a = k + 2; str[a] != 'c'; a++) {};
if (str[a] == '}' && k >= 12) //\begin{tabu}{c...}
{
if (str.find(L"\\begin{tabu}", k - 12, 12) != k - 12)
{
isMatrix[i] = false;
searchNext[i] = false;
}
}
else
{
isMatrix[i] = false;
searchNext[i] = false;
}
}
else
{
isMatrix[i] = false;
searchNext[i] = false;
}
}
else if (parLevel > 0)
{
isMatrix[i] = false;
searchNext[i] = false;
}
break;
case '}':
--parLevel;
break;
case '&':
if (beginLevel == 0)
{
isMatrix[i] = false;
searchNext[i] = false;
}
break;
case '#':
if (beginLevel == 0)
hasLinebreak = true;
break;
case ' ':
case '\n':
case '\r':
break;
default:
if (parLevel == 0)
{
isMatrix[i] = false;
searchNext[i] = false;
}
break;
}
if (parLevel == 1 || beginLevel == 1 || hasLinebreak == true || searchNext[i] == false)
break;
}
}
hasLinebreak = false;
if (searchNext[i] == true && isMatrix[i] == true)
{
parLevel = 0;
beginLevel = 0;
for (j = eqend[i] + 1; j < eqstart[i + 1]; j++)
{
switch (str[j])
{
case '{':