forked from 3c1u/TextRender
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextrender.cc
More file actions
1082 lines (908 loc) · 32 KB
/
textrender.cc
File metadata and controls
1082 lines (908 loc) · 32 KB
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
/**
* @file textrender.cc
* @author Hikaru Terazono (3c1u) <3c1u@vulpesgames.tokyo>
* @brief Drop-in replacement for TextRender.dll.
* @date 2020-08-16
*
* @copyright Copyright (c) 2020 Hikaru Terazono. All rights reserved.
*
*/
/* from TextRender.tjs:
* ・フェースは1回の描画中での変更は無しとしてあります。変更すると結果が保証されません
* ・ルビは指定領域から上にはみ出た位置に配置されます。最大サイズの本文テキスト上端が0位置です
*
* 入力用特殊テキスト書式
* \n 改行
* \t タブ文字
* \i インデント開始(次行から)
* \r インデント解除(次行から)
* \w 空白相当分表示位置を進める
* \k キー入力待ち情報を生成
* \x nul文字相当
* \文字 エスケープ指定。特殊機能が無効
* [xxxx] ルビ指定。次の文字にかかる
* [xxxx,文字数] ルビ指定。次の指定した個数の複数の文字にかかる
*
* フォント指定
* %f名前; フォントフェイス指定
* %bX ボールド指定 0:off 1:on その他:デフォルト
* %iX イタリック指定 0:off 1:on その他:デフォルト
* %sX 影指定 0:off 1:on その他:デフォルト
* %eX エッジ指定 0:off 1:on その他:デフォルト
* %数値; フォントサイズ指定(デフォルトに対するパーセント)
* %B 大サイズフォント
* %S 小サイズフォント
* #xxxxxx; 色指定(xxxは色を16進指定)
* %r フォントリセット
*
* スタイル指定
* %C センタリング (align=0)
* %R 右よせ (align=1)
* %L 左寄せ (align=-1)
* %p数値; ピッチ
*
* 特殊指定
* %d数値; 文字あたり表示時間指定(標準に対するパーセント指定 100で標準速度)
* %w数値; 時間待ち(1文字表示の時間に対するパーセント指定100で1文字分)
* %D数値; 時間同期指定 指定した時間で表示同期指定 単位:ms
* %D$xxx; 時間同期指定 同期指定・ラベル指定(xxxはラベル名)
*
* $xxx; 埋め込み指定(xxxは変数名) ※onEval で処理されます
* &xxx; グラフィック文字指定 (xxxは画像名)
*/
#include "ncbind/ncbind.hpp"
#include "DebugIntf.h"
#include <optional>
#if 0
#define dbg_print TVPAddLog
#else
#define dbg_print(...)
#endif
// use Kirikiri-Z rasterizer for layouting
#include "FontRasterizer.h"
FontRasterizer *GetCurrentRasterizer();
using RgbColor = uint32_t;
#define setprop_t(d, p, ty) \
{ \
tTJSVariant v(ty(p)); \
d->PropSet(TJS_MEMBERENSURE, TJS_W(#p), nullptr, &v, d); \
}
#define setprop_opt_t(d, p, ty) \
if (p != std::nullopt) { \
tTJSVariant v(ty(*p)); \
d->PropSet(TJS_MEMBERENSURE, TJS_W(#p), nullptr, &v, d); \
} else { \
tTJSVariant v; \
d->PropSet(TJS_MEMBERENSURE, TJS_W(#p), nullptr, &v, d); \
}
#define setprop(d, p) setprop_t(d, p, )
#define setprop_opt(d, p) setprop_opt_t(d, p, )
#define getprop_t(d, p, ty) \
{ \
tTJSVariant v; \
if (TJS_SUCCEEDED(d->PropGet(0, TJS_W(#p), nullptr, &v, d)) && \
v.Type() != tvtVoid) { \
p = ty(v); \
} \
}
#define getprop_opt_t(d, p, ty) \
{ \
tTJSVariant v; \
if (TJS_SUCCEEDED(d->PropGet(0, TJS_W(#p), nullptr, &v, d)) && \
v.Type() != tvtVoid) { \
p = ty(v); \
} else { \
p = std::nullopt; \
} \
}
#define getprop_ensure_opt_deref(d, p, e) \
{ \
tTJSVariant v; \
if (TJS_SUCCEEDED(d->PropGet(0, TJS_W(#p), nullptr, &v, d)) && \
v.Type() != tvtVoid) { \
auto ens = v.e; \
if (ens) \
p = *ens; \
else \
p = std::nullopt; \
} else { \
p = std::nullopt; \
} \
}
#define getprop(d, p) getprop_t(d, p, )
#define getprop_ensure_deref(d, p, e) \
{ \
tTJSVariant v; \
if (TJS_SUCCEEDED(d->PropGet(0, TJS_W(#p), nullptr, &v, d)) && \
v.Type() != tvtVoid) { \
auto ens = v.e; \
if (ens) \
p = *ens; \
} \
}
struct TextRenderState {
bool bold = false; // 太字
bool italic = false; // 斜体
tjs_string face = TJS_W("user"); // フォントフェイス
int fontSize = 24; // フォントサイズ
RgbColor chColor = 0xffffff; // 文字色
int rubySize = 10; // ルビの大きさ
int rubyOffset = -2; // ルビのオフセット
bool shadow = true; // 影
RgbColor shadowColor = 0x000000; // 影の色
bool edge = false; // 縁取り
RgbColor edgeColor = 0x0080ff; // 縁の色
int lineSpacing = 6; // 行間
int pitch = 0; // 字間
int lineSize = 0; // ラインの高さ
// -------------------------------------------------------------- //
tTJSVariant serialize() const {
auto dict = TJSCreateDictionaryObject();
setprop(dict, bold);
setprop(dict, italic);
setprop(dict, fontSize);
setprop(dict, face);
setprop_t(dict, chColor, static_cast<tjs_int>);
setprop(dict, rubySize);
setprop(dict, rubyOffset);
setprop(dict, shadow);
setprop_t(dict, shadowColor, static_cast<tjs_int>);
setprop(dict, edge);
setprop_t(dict, edgeColor, static_cast<tjs_int>);
setprop(dict, lineSpacing);
setprop(dict, pitch);
setprop(dict, lineSize);
auto res = tTJSVariant(dict, dict);
dict->Release();
return res;
}
void deserialize(tTJSVariant t) {
auto dict = t.AsObjectNoAddRef();
if (!dict) {
return;
}
getprop(dict, bold);
getprop(dict, italic);
getprop(dict, fontSize);
getprop_ensure_deref(dict, face, AsStringNoAddRef());
getprop_t(dict, chColor, static_cast<tjs_int>);
getprop(dict, rubySize);
getprop(dict, rubyOffset);
getprop(dict, shadow);
getprop_t(dict, shadowColor, static_cast<tjs_int>);
getprop(dict, edge);
getprop_t(dict, edgeColor, static_cast<tjs_int>);
getprop(dict, lineSpacing);
getprop(dict, pitch);
getprop(dict, lineSize);
}
static TextRenderState from(tTJSVariant t) {
TextRenderState state{};
state.deserialize(t);
return state;
}
};
struct TextRenderOptions {
tjs_string following = TJS_W(
"%),:;]}。」゙゚。,、.:;゛゜ヽヾゝゞ々’”)〕]}〉》」』】°′″℃¢%‰ !.?"
"、・ァィゥェォャュョッー・?!ーぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ");
tjs_string leading = TJS_W("\\$([{「‘“(〔[{〈《「『【¥$£");
tjs_string begin = TJS_W("「『(‘“〔[{〈《");
tjs_string end = TJS_W("」』)’”〕]}〉》");
// -------------------------------------------------------------- //
tTJSVariant serialize() const {
auto dict = TJSCreateDictionaryObject();
setprop(dict, following);
setprop(dict, leading);
setprop(dict, begin);
setprop(dict, end);
auto res = tTJSVariant(dict, dict);
dict->Release();
return res;
}
void deserialize(tTJSVariant t) {
auto dict = t.AsObjectNoAddRef();
if (!dict) {
return;
}
getprop_ensure_deref(dict, following, AsStringNoAddRef());
getprop_ensure_deref(dict, leading, AsStringNoAddRef());
getprop_ensure_deref(dict, begin, AsStringNoAddRef());
getprop_ensure_deref(dict, end, AsStringNoAddRef());
}
static TextRenderState from(tTJSVariant t) {
TextRenderState state{};
state.deserialize(t);
return state;
}
};
struct CharacterInfo {
bool bold = false; // 太字
bool italic = false; // 斜体
bool graph = false; // グラフィック文字
bool vertical = false; // 縦書き
tjs_string face = TJS_W("user"); // フォントフェイス名?
int x = 0; // X座標
int y = 0; // Y座標
int cw = 0; // 文字幅
int size = 0; // フォントサイズ?
RgbColor color = 0xffffff; // 文字色
std::optional<RgbColor> edge = std::nullopt; // 縁の色
std::optional<RgbColor> shadow = std::nullopt; // 影の色
tjs_string text = TJS_W(""); // 文字
// -------------------------------------------------------------- //
tTJSVariant serialize() const {
auto dict = TJSCreateDictionaryObject();
setprop(dict, bold);
setprop(dict, italic);
setprop(dict, graph);
setprop(dict, vertical);
setprop(dict, x);
setprop(dict, y);
setprop(dict, cw);
setprop(dict, size);
setprop(dict, face);
setprop_t(dict, color, static_cast<tjs_int>);
setprop_opt_t(dict, edge, static_cast<tjs_int>);
setprop_opt_t(dict, shadow, static_cast<tjs_int>);
setprop(dict, text);
auto res = tTJSVariant(dict, dict);
dict->Release();
return res;
}
void deserialize(tTJSVariant t) {
auto dict = t.AsObjectNoAddRef();
if (!dict) {
return;
}
getprop(dict, bold);
getprop(dict, italic);
getprop(dict, graph);
getprop(dict, vertical);
getprop(dict, x);
getprop(dict, y);
getprop(dict, cw);
getprop(dict, size);
getprop_ensure_deref(dict, face, AsStringNoAddRef());
getprop_t(dict, color, static_cast<tjs_int>);
getprop_opt_t(dict, edge, static_cast<tjs_int>);
getprop_opt_t(dict, shadow, static_cast<tjs_int>);
getprop_ensure_deref(dict, text, AsStringNoAddRef());
}
static TextRenderState from(tTJSVariant t) {
TextRenderState state{};
state.deserialize(t);
return state;
}
};
#define property_accessor(name, type, storage) \
type get_##name() const { return storage; } \
void set_##name(type v) { storage = v; }
#define property_accessor_cast(name, type, cast, storage) \
cast get_##name() const { return cast(storage); } \
void set_##name(cast v) { storage = type(v); }
#define property_accessor_string(name, storage) \
tTJSVariant get_##name() const { return tTJSVariant(storage); } \
void set_##name(tTJSVariant v) { \
auto s = v.AsStringNoAddRef(); \
storage = s->LongString ? s->LongString : s->ShortString; \
}
#define property_delegate(name) NCB_PROPERTY(name, get_##name, set_##name);
/**
* @brief The base of the TextRender class. This only performs the text
* layouting and the line breaking (禁則処理).
*/
class TextRenderBase {
public:
TextRenderBase();
virtual ~TextRenderBase();
bool render(tTJSString text, int autoIndent, int diff, int all,
bool _reserved);
void setRenderSize(int width, int height);
void setDefault(tTJSVariant defaultSettings);
void setOption(tTJSVariant options);
tTJSVariant getCharacters(int start, int end);
void clear();
void done();
// property accessor
property_accessor(vertical, bool, m_vertical);
property_accessor(bold, bool, m_state.bold);
property_accessor(italic, bool, m_state.italic);
property_accessor_string(face, m_state.face);
property_accessor(fontSize, int, m_state.fontSize);
property_accessor_cast(chColor, RgbColor, tjs_int, m_state.chColor);
property_accessor(rubySize, int, m_state.rubySize);
property_accessor(rubyOffset, int, m_state.rubyOffset);
property_accessor(shadow, bool, m_state.shadow);
property_accessor_cast(shadowColor, RgbColor, tjs_int, m_state.shadowColor);
property_accessor(edge, bool, m_state.edge);
property_accessor(lineSpacing, int, m_state.lineSpacing);
property_accessor(pitch, int, m_state.pitch);
property_accessor(lineSize, int, m_state.lineSize);
property_accessor(defaultBold, bool, m_default.bold);
property_accessor(defaultItalic, bool, m_default.italic);
property_accessor_string(defaultFace, m_default.face);
property_accessor(defaultFontSize, int, m_default.fontSize);
property_accessor_cast(defaultChColor, RgbColor, tjs_int, m_default.chColor);
property_accessor(defaultRubySize, int, m_default.rubySize);
property_accessor(defaultRubyOffset, int, m_default.rubyOffset);
property_accessor(defaultShadow, bool, m_default.shadow);
property_accessor_cast(defaultShadowColor, RgbColor, tjs_int,
m_default.shadowColor);
property_accessor(defaultEdge, bool, m_default.edge);
property_accessor(defaultLineSpacing, int, m_default.lineSpacing);
property_accessor(defaultPitch, int, m_default.pitch);
property_accessor(defaultLineSize, int, m_default.lineSize);
private:
int m_boxWidth = 0;
int m_boxHeight = 0;
int m_x = 0;
int m_y = 0;
int m_indent = 0;
int m_autoIndent = 0;
bool m_overflow = false;
bool m_isBeginningOfLine = true;
bool m_vertical = false;
TextRenderOptions m_options{};
TextRenderState m_default{};
TextRenderState m_state{};
std::vector<CharacterInfo> m_characters{};
std::vector<CharacterInfo> m_buffer{};
uint32_t m_mode = 0;
void pushCharacter(tjs_char ch);
void pushGraphicalCharacter(tjs_string const& graph);
void performLinebreak();
void flush(bool force = false);
void updateFont();
};
enum TextRenderAlignment {
kTextRenderAlignmentLeft = -1,
kTextRenderAlignmentCenter = 0,
kTextRenderAlignmentRight = 1,
};
// [LEADING] [NORMAL] [FOLLOWING] の形になるように文字をセグメンテーションする.
enum TextRenderMode {
kTextRenderModeLeading = 0,
kTextRenderModeNormal,
kTextRenderModeFollowing,
};
// -------------------------------------------------------------------
TextRenderBase::TextRenderBase() {}
TextRenderBase::~TextRenderBase() {}
static bool readchar(tTJSString const &str, size_t &i, tjs_char &c) {
auto const len = str.GetLen();
if (++i >= len) {
return false;
}
c = str[i];
return true;
}
static void read_integer(tTJSString const &str, size_t &i, int &value) {
tjs_char ch;
bool is_negative = false;
while (true) {
if (!readchar(str, i, ch)) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected either integer or ';', found EOF"));
}
if ('0' <= ch && ch <= '9') {
value = value * 10 + (ch - '0');
continue;
} else if (ch == '-') {
is_negative = !is_negative;
continue;
} else if (ch == ';') {
if (is_negative) {
value = -value;
}
return;
}
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected either integer or ';', found '%1'"),
ch);
}
}
bool TextRenderBase::render(tTJSString text, int autoIndent, int diff, int all,
bool same) {
// 入力のパース
auto const len = text.GetLen();
for (size_t i = 0; i < len; ++i) {
auto ch = text[i];
switch (ch) {
// 制御文字のパース
case '%':
if (!readchar(text, i, ch)) {
TVPThrowExceptionMessage(TJS_W("TextRenderBase::render() failed to "
"parse: expected character, found EOF"));
}
switch (ch) {
case 'f': // フォントフェイス
{
tjs_string faceName{};
while (true) {
if (!readchar(text, i, ch)) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected character, found EOF"));
}
if (ch == ';')
break;
faceName += ch;
}
dbg_print(
TVPFormatMessage(TJS_W("change font face name: %1"), faceName));
m_state.face = faceName;
break;
}
case 'b': // フォントの装飾
{
if (!readchar(text, i, ch) && (ch == '0' || ch == '1')) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse %%b: expected either '0' or '1', found EOF"));
}
bool flag = ch == '1';
if (flag)
dbg_print(TJS_W("set bold"));
else
dbg_print(TJS_W("unset bold"));
m_state.bold = flag;
break;
}
case 'i': {
if (!readchar(text, i, ch) && (ch == '0' || ch == '1')) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse %%i: expected either '0' or '1', found EOF"));
}
bool flag = ch == '1';
if (flag)
dbg_print(TJS_W("set italic (oblique)"));
else
dbg_print(TJS_W("unset italic (oblique)"));
m_state.italic = flag;
break;
}
case 's': {
if (!readchar(text, i, ch) && (ch == '0' || ch == '1')) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse %%s: expected either '0' or '1', found EOF"));
}
bool flag = ch == '1';
if (flag)
dbg_print(TJS_W("set shadow"));
else
dbg_print(TJS_W("unset shadow"));
m_state.shadow = flag;
break;
}
case 'e': {
if (!readchar(text, i, ch) && (ch == '0' || ch == '1')) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse %%e: expected either '0' or '1', found '%1'"),
ch);
}
bool flag = ch == '1';
if (flag)
dbg_print(TJS_W("set edge"));
else
dbg_print(TJS_W("unset edge"));
m_state.edge = flag;
break;
}
case 'B': // TODO: Big
dbg_print(TJS_W("big font"));
break;
case 'S': // TODO: Small
dbg_print(TJS_W("small font"));
break;
case 'r': // Reset
dbg_print(TJS_W("reset"));
m_state = m_default;
break;
case 'C': // TODO: Centre
dbg_print(TJS_W("centre"));
break;
case 'R': // TODO: Right
dbg_print(TJS_W("right"));
break;
case 'L': // TODO: Left
dbg_print(TJS_W("left"));
break;
case 'p': // ピッチ,%p[0-9]+;
{
int value = 0;
read_integer(text, i, value);
m_state.pitch = value;
break;
}
case 'd': // 文字あたり表示時間指定,%d[0-9]+;
{
int value = 0;
read_integer(text, i, value);
// TODO: text speed
break;
}
case 'w': // 時間待ち,%w[0-9]+;
{
int value = 0;
read_integer(text, i, value);
// TODO: wait
break;
}
case 'D': // %D[0-9]+; || %D$.+;
{
if (ch == '$') {
tjs_string labelName{};
while (true) {
if (!readchar(text, i, ch)) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected character, found EOF"));
}
if (ch == ';')
break;
labelName += ch;
}
// TODO: labelName
} else {
int value = 0;
read_integer(text, i, value);
// TODO: label?
}
//
break;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
int value = static_cast<int>(ch - '0');
read_integer(text, i, value);
// font size
m_state.fontSize = m_default.fontSize * value / 100;
dbg_print(
TVPFormatMessage(TJS_W("new font size: %1 px"), m_state.fontSize));
updateFont();
break;
}
default:
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected any of 'fbiseBSrCRLpdwD0123456789', found "
"'%1'"),
ch);
break;
}
break;
case '\\':
if (!readchar(text, i, ch)) {
TVPThrowExceptionMessage(TJS_W("TextRenderBase::render() failed to "
"parse: expected character, found EOF"));
}
switch (ch) {
case 'n':
// 改行
flush();
performLinebreak();
break;
case 't':
// タブ
pushCharacter('\t');
break;
case 'i':
m_indent = m_x;
break;
case 'r':
m_indent = 0;
break;
case 'w':
pushCharacter(' ');
break;
case 'k':
// TODO: キー待ち
//
break;
case 'x':
// TODO: nul文字
// unknown behaviour: possible UB
break;
default:
goto __draw_normal; // chをふつうの文字列として描画する
break;
}
break;
case '[': {
// [ .* ]
// [ .*, [0-9]+ ]
tjs_string ruby{};
while (true) {
if (!readchar(text, i, ch)) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected character, found EOF"));
}
if (ch == ']')
break;
ruby += ch;
}
// TODO: implement ruby
break;
}
case '#': {
// [ .* ]
// [ .*, [0-9]+ ]
RgbColor colour = 0x00;
while (true) {
if (!readchar(text, i, ch)) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected character, found EOF"));
}
if (ch == ';')
break;
RgbColor c = 0;
if ('0' <= ch && ch <= '9') {
c = static_cast<RgbColor>(ch - '0');
} else if ('A' <= ch && ch <= 'F') {
c = 0x0a + static_cast<RgbColor>(ch - 'A');
} else if ('a' <= ch && ch <= 'f') {
c = 0x0a + static_cast<RgbColor>(ch - 'a');
} else {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected hexadecimal number, found '%1'"),
ch);
}
colour = (colour << 4) || c;
}
m_state.chColor = colour;
break;
}
case '&': {
// '&' .+ ';'
tjs_string graph{};
while (true) {
if (!readchar(text, i, ch)) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected character, found EOF"));
}
if (ch == ';')
break;
graph += ch;
}
pushGraphicalCharacter(graph);
break;
}
case '$': {
// '&' .+ ';'
tjs_string varName{};
while (true) {
if (!readchar(text, i, ch)) {
TVPThrowExceptionMessage(
TJS_W("TextRenderBase::render() failed to "
"parse: expected character, found EOF"));
}
if (ch == ';')
break;
varName += ch;
}
// TODO: implement eval
break;
}
default:
__draw_normal:
// タダの文字として処理する
// TODO: character should include format options;
// as the font is lazy-evaluated/drawn
// (restrictions for line-breaking algorithm)
pushCharacter(ch);
break;
}
}
return !m_overflow;
}
void TextRenderBase::performLinebreak() {
auto rasterizer = GetCurrentRasterizer();
m_x = m_indent;
m_isBeginningOfLine = true;
m_y += rasterizer->GetAscentHeight() + m_state.lineSpacing;
}
void TextRenderBase::pushGraphicalCharacter(tjs_string const& graph) {
// TODO: implement graphical characters
}
void TextRenderBase::pushCharacter(tjs_char ch) {
if ((0xD800 <= ch && ch <= 0xDBFF) /* upper surrogate-pair */
|| (0xDC00 <= ch && ch <= 0xDFFF) /* lower surrogate-pair */) {
TVPThrowExceptionMessage(TJS_W("unexpected character: surrogate pair"));
}
auto isLeadingChar = m_options.leading.find_first_of(ch) != std::string::npos;
auto isFollowingChar =
m_options.following.find_first_of(ch) != std::string::npos;
auto isIndent = m_options.begin.find_first_of(ch) != std::string::npos;
auto isIndentDecr = m_options.end.find_first_of(ch) != std::string::npos;
uint32_t current;
if (isLeadingChar) {
current = kTextRenderModeLeading;
} else if (isFollowingChar) {
current = kTextRenderModeFollowing;
} else {
current = kTextRenderModeNormal;
}
if (m_mode == kTextRenderModeFollowing || m_mode != kTextRenderModeLeading) {
flush();
}
auto rasterizer = GetCurrentRasterizer();
auto text_height = rasterizer->GetAscentHeight();
int advance_width = 0, advance_height = 0;
rasterizer->GetTextExtent(ch, advance_width, advance_height);
CharacterInfo info{
.bold = m_state.bold,
.italic = m_state.italic,
.graph = false,
.vertical = false,
.face = m_state.face,
.x = 0,
.y = 0,
.cw = advance_width,
.size = text_height,
.color = m_state.chColor,
.edge =
m_state.edge ? std::make_optional(m_state.edgeColor) : (std::nullopt),
.shadow = m_state.shadow ? std::make_optional(m_state.shadowColor)
: (std::nullopt),
.text = (tjs_string() + ch),
};
m_buffer.push_back(std::move(info));
if (m_autoIndent) {
// pre-indent
if (m_isBeginningOfLine && m_autoIndent < 0) {
m_x -= advance_width;
}
if (isIndent) {
m_indent = m_x + advance_width;
// TODO: register pair
}
if (isIndentDecr && m_indent > 0) {
flush(); // FIXME: not safe?
m_indent = 0;
}
}
m_mode = current;
m_isBeginningOfLine = false;
}
void TextRenderBase::flush(bool force) {
if (m_buffer.empty()) {
return;
}
// try place all characters in the same line
auto x = m_x;
for (auto &ch : m_buffer) {
auto advance_width = ch.cw;
auto new_x = advance_width + x + m_state.pitch;
if (m_boxWidth < new_x) {
if (force) {
performLinebreak();
x = m_x;
new_x = advance_width + x + m_state.pitch;
} else {
performLinebreak();
flush(true);
return;
}
}
ch.x = x;
ch.y = m_y;
x = new_x;
}
m_x = x;
m_characters.insert(m_characters.end(), m_buffer.begin(), m_buffer.end());
m_buffer.clear();
}
void TextRenderBase::setRenderSize(int width, int height) {
m_boxWidth = width;
m_boxHeight = height;
dbg_print(
TVPFormatMessage(TJS_W("set render size: (%1, %2)"), width, height));
clear();
}
void TextRenderBase::setDefault(tTJSVariant defaultSettings) {
dbg_print(TJS_W("set default format"));
m_default.deserialize(defaultSettings);
}
void TextRenderBase::setOption(tTJSVariant options) {
dbg_print(TJS_W("set option"));
m_options.deserialize(options);
}
tTJSVariant TextRenderBase::getCharacters(int start, int end) {
// TODO: only (0, 0) is observed
auto array = TJSCreateArrayObject();
dbg_print(TVPFormatMessage(TJS_W("get characters: [%1, %2]"), start, end));
if ((end < start) || (start == 0 && end == 0)) {
for (size_t i = 0, cnt = m_characters.size(); i < cnt; ++i) {
auto ch = m_characters[i].serialize();
array->PropSetByNum(TJS_MEMBERENSURE, i, &ch, array);
}
} else {
// TODO: unknown behaviour
}