-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathONScripterLabel_text.cpp
2463 lines (2244 loc) · 87.9 KB
/
ONScripterLabel_text.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
/* -*- C++ -*-
*
* ONScripterLabel_text.cpp - Text parser of ONScripter-EN
*
* Copyright (c) 2001-2011 Ogapee. All rights reserved.
* (original ONScripter, of which this is a fork).
*
* ogapee@aqua.dti2.ne.jp
*
* Copyright (c) 2007-2011 "Uncle" Mion Sonozaki
*
* UncleMion@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Modified by Mion , March 2008, to update from
// Ogapee's 20080121 release source code.
// Modified April 2008 to redo the text linebreak processing.
// Modified by Mion, November 2009, to update from
// Ogapee's 20091115 release source code.
#include "ONScripterLabel.h"
#include "Encoding.h"
#include "ScriptHandler.h"
#include <cstring>
extern unsigned short convUTF8ToUTF16(const char **src);
/*
* Welcome to Galladite's handy-dandy guide to how the heck this all
* works! This is mostly written for myself before I convert it to
* UTF-8, but if anyone works on this in the future it may be of use.
*
* Quick terminology:
* - Character - a single letter or symbol
* - Glyph - a representation of a character (so this includes
* fonts and styling)
*
*
*
* Useful variables (from Fontinfo):
* int font_size_xy[2] Width and height of font
* int top_xy[2] Top left origin
* int num_xy[2] Row and column of the text windows
* int xy[2] Current position
* int pitch_xy[2] Width and height of a character
*
* Oh, and tateyoko_mode means whether text is being written
* horizontally or vertically. On is "tate" and one is "yoko".
*
*
*
* Command overview:
* isRotationRequired - used once, returns true on a one-byte char or
* on one of several two-byte characters
* isTranslationRequired - used once, returns true on a full-width
* comma or full stop
* isNonPrinting - used once, returns true on a control character or
* full-width space
*
* renderGlyph - takes a font and a glyph, draws the glyph on a
* surface, and returns the surface
* drawGlyph - takes an SDL surface, a Fontinfo, and other info,
* converts to UTF-16, renders the glyph, and applies any
* transformations
* drawChar - takes a char * (for multibyte), a Fontinfo, a surface,
* and other info. Ensures the font is open, advances the line
* counter, interprets some minor control characters, draws the
* glyph, and advances the text buffer
* drawString - takes lots of information, which is mostly passed
* along. Handles most control characters, ruby text, tabs and
* form feeds etc., handles kinsoku process, newlines, and in the
* end calls drawChar on each character. Also does a little clean-
* up work after the whole string is drawn.
*
* restoreTextBuffer - pretty much drawString but for use in lookback
* mode
* enterTextDisplayMode - what it says on the tin
* leaveTextDisplayMode - what it says on the tin
* doClickEnd - used after clickwaits
* clickWait - initiate a clickwait (calls doClickEnd)
* clickNewPage - initiate a clickwait with new page (calls
* doClickEnd)
* startRuby - what it says on the tin
* endRuby - what it says on the tin
* textCommand - calles saveSaveFile(-1), gets the string to print,
* gosubs to the pretext label (if required), enters text display
* mode, calls processText() (linewrap algo??)
* processEOT - does something I guess
* processText - now this is a big ol' function. Does some text
* processing ig (wait, really?), calles drawChar, returns true
* for a lot of things (why?), reads !s !d !w, also seems to
* handle ruby text (?), does text buttons, calls drawchar...
* doLineBreak - what it says on the tin
* isTextCommand - what it says on the tin - checks if a line in the
* script is a text command or not
* processRuby - calls startRuby and sets some variables
* processBreaks - reads through a line until it finds a break, does
* kinsoku and spacebreak stuff, might call some ruby commands
* findNextBreak - returns the offset of the first break in a string,
* taking into account a pre-existing offset
* terminateTextButton - used to terminate improperly-terminated text
* buttons
* textbtnColorChange - swaps linkcolor[0] and sentence_font.color
* u8strlen - gets the length of a UTF-8 string
*
*
*
* - Do I need to check AnimationInfo? (I hope not lol)
* - Change newlines to measure font hight?
*
* - startRuby needs fixing to use px
* - I must make sure I properly check if the character after an end-
* kinsoku character will fit on the line wihtout relying on columns
* of text
* - Expected pixel length in lookback mode is far too big (91?)
* Does this still apply? 2023-6-23
* - Need to set sane default soft text width limit int UTF-8 mode
*
*/
extern unsigned short convSJIS2UTF16( unsigned short in );
static inline bool isRotationRequired(const char *text, Encoding enc)
{
// Probably works -Galladite 2023-6-21
if (enc.getBytes(text[0]) == 1)
return true;
//if ( !IS_TWO_BYTE(text[0]) ) // ascii, halfwidth kana
// return true;
//fullwidth commas or full-stops
return ((text[0] == (char)0x81) &&
( ((text[1] >= 0x5b) && (text[1] <= 0x5d)) || //hyphens
((text[1] >= 0x60) && (text[1] <= 0x64)) || //tilde,ellipses
((text[1] >= 0x69) && (text[1] <= 0x7a)) || //parens,brackets
(text[1] == 0x50) || (text[1] == 0x51) || //macron,lowline
(text[1] == (char)0x80) )); //division sign
}
static inline bool isTranslationRequired(const char *text)
{
//fullwidth commas or full-stops
return ((text[0] == (char)0x81) &&
(text[1] >= 0x41) && (text[1] <= 0x44));
}
static inline bool isNonPrinting(const char *text)
{
//control chars or fullwidth space
return ( ((unsigned char) text[0] <= 0x20) ||
((text[0] == (char)0x81) && (text[1] == (char)0x40)) );
}
SDL_Surface *ONScripterLabel::renderGlyph(TTF_Font *font, Uint16 text)
{
GlyphCache *gc = root_glyph_cache;
GlyphCache *pre_gc = gc;
while(1){
if (gc->text == text &&
gc->font == font){
if (gc != pre_gc){
pre_gc->next = gc->next;
gc->next = root_glyph_cache;
root_glyph_cache = gc;
}
return gc->surface;
}
if (gc->next == NULL) break;
pre_gc = gc;
gc = gc->next;
}
pre_gc->next = NULL;
gc->next = root_glyph_cache;
root_glyph_cache = gc;
gc->text = text;
gc->font = font;
if (gc->surface != NULL) SDL_FreeSurface(gc->surface);
/* Initializing SDL_Color.unused here to silence warnings about unused
variables. 32 bit operations should be faster than 24 bit ones anyway.
Users will be delighted by this 0.0000001 microsecond increase in speed.
(contribution by Andrius, March 2010) */
static SDL_Color fcol={0xff, 0xff, 0xff, 0xff}, bcol={0, 0, 0, 0};
gc->surface = TTF_RenderGlyph_Shaded( font, text, fcol, bcol );
return gc->surface;
}
void ONScripterLabel::drawGlyph( SDL_Surface *dst_surface, Fontinfo *info, SDL_Color &color, char* text, int xy[2], bool shadow_flag, AnimationInfo *cache_info, SDL_Rect *clip, SDL_Rect &dst_rect )
{
//in case of font size 0
if ((info->font_size_xy[0] == 0) || (info->font_size_xy[1] == 0))
return;
unsigned short unicode;
if (script_h.enc.getEncoding() == Encoding::CODE_CP932) {
if (IS_TWO_BYTE(text[0])){
unsigned index = ((unsigned char*)text)[0];
index = index << 8 | ((unsigned char*)text)[1];
unicode = convSJIS2UTF16( index );
}
else{
unicode = convSJIS2UTF16( ((unsigned char*)text)[0] );
}
} else {
unicode = convUTF8ToUTF16((const char**)&text);
}
int minx, maxx, miny, maxy, advanced;
#if 0
if (TTF_GetFontStyle( (TTF_Font*)info->ttf_font ) !=
(info->is_bold?TTF_STYLE_BOLD:TTF_STYLE_NORMAL) )
TTF_SetFontStyle( (TTF_Font*)info->ttf_font, (info->is_bold?TTF_STYLE_BOLD:TTF_STYLE_NORMAL));
#endif
TTF_GlyphMetrics( (TTF_Font*)info->ttf_font, unicode,
&minx, &maxx, &miny, &maxy, &advanced );
//printf("min %d %d %d %d %d %d\n", minx, maxx, miny, maxy, advanced,TTF_FontAscent((TTF_Font*)info->ttf_font) );
SDL_Surface *tmp_surface = renderGlyph( (TTF_Font*)info->ttf_font, unicode );
if (tmp_surface == NULL) return;
bool rotate_flag = false;
if ( (info->getTateyokoMode() == Fontinfo::TATE_MODE) &&
isRotationRequired(text, script_h.enc) )
rotate_flag = true;
//Mion: to display vertical text more cleanly
if ( (info->getTateyokoMode() == Fontinfo::TATE_MODE) &&
isTranslationRequired(text) ){
dst_rect.x = xy[0] + ExpandPos(info->font_size_xy[0]) - maxx;
dst_rect.y = xy[1] + minx;
}
else if ( rotate_flag ) {
dst_rect.x = xy[0] + ExpandPos(info->font_size_xy[0]) -
TTF_FontAscent((TTF_Font*)info->ttf_font) + miny;
dst_rect.y = xy[1] + minx;
}
else {
dst_rect.x = xy[0] + minx;
dst_rect.y = xy[1] + TTF_FontAscent((TTF_Font*)info->ttf_font) - maxy;
}
if ( shadow_flag ){
dst_rect.x += shade_distance[0];
dst_rect.y += shade_distance[1];
}
if (rotate_flag){
dst_rect.w = tmp_surface->h;
dst_rect.h = tmp_surface->w;
}
else{
dst_rect.w = tmp_surface->w;
dst_rect.h = tmp_surface->h;
}
if (cache_info)
cache_info->blendText( tmp_surface, dst_rect.x, dst_rect.y, color, clip, rotate_flag );
if (dst_surface)
alphaBlendText( dst_surface, dst_rect, tmp_surface, color, clip, rotate_flag );
}
void ONScripterLabel::drawChar( char* text, Fontinfo *info, bool flush_flag,
bool lookback_flag, SDL_Surface *surface,
AnimationInfo *cache_info, int abs_offset, SDL_Rect *clip )
{
//printf("draw %x-%x-%x-%x[%s] %d, %d\n", text[0], text[1], text[2], text[3], text, info->xy[0], info->xy[1] );
if ( info->ttf_font == NULL ){
if ( info->openFont( font_file, screen_ratio1, screen_ratio2 ) == NULL ){
snprintf(script_h.errbuf, MAX_ERRBUF_LEN,
"can't open font file: %s\n", font_file );
errorAndExit(script_h.errbuf);
}
}
if ( info->isEndOfLine() ){
info->newLine();
for (int i=0 ; i<indent_offset ; i++)
if (script_h.enc.getEncoding() == Encoding::CODE_CP932) {
sentence_font.advanceCharInHankaku(2);
} else {
// Don't worry about it (untested)
sentence_font.advanceCharInHankaku(info->pitch_xy[0]);
}
if ( lookback_flag ){
for (int i=0 ; i<indent_offset ; i++){
current_page->add(0x81);
current_page->add(0x40);
}
}
}
char out_text[5]= {'\0', '\0', '\0', '\0', '\0'};
if (text[0] == ScriptHandler::LEFT_PAREN) {
out_text[0] = '(';
} else if (text[0] == ScriptHandler::RIGHT_PAREN) {
out_text[0] = ')';
} else if (text[0] == '\t') {
out_text[0] = ' '; //draw tabs as spaces, for now
} else if ((unsigned char)text[0] < 0x20) {
snprintf(script_h.errbuf, MAX_ERRBUF_LEN,
"drawChar: got unrecognized control character 0x%02x", text[0]);
errorAndCont(script_h.errbuf);
out_text[0] = ' ';
} else {
out_text[0] = text[0];
out_text[1] = text[1];
out_text[2] = text[2];
out_text[3] = text[3];
}
int xy[2];
xy[0] = abs_offset + ExpandPos(info->x(script_h.enc.getEncoding()));
xy[1] = ExpandPos(info->y());
if ( !isNonPrinting(&out_text[0]) || script_h.enc.getEncoding() == Encoding::CODE_UTF8 ){
//don't bother drawing non-printing glyphs - unless it's UTF-8
//mode - spaces may have variable width -Galladite 2023-6-22
SDL_Color color;
SDL_Rect dst_rect;
if ( info->is_shadow ){
color.r = color.g = color.b = 0;
drawGlyph(surface, info, color, out_text, xy, true, cache_info, clip, dst_rect);
}
color.r = info->color[0];
color.g = info->color[1];
color.b = info->color[2];
drawGlyph( surface, info, color, out_text, xy, false, cache_info, clip, dst_rect );
//printf("Char: [%s]\tExpected pixel length: %f\n\n", out_text, strpxlen(out_text, info));
if ( surface == accumulation_surface &&
!flush_flag &&
(!clip || AnimationInfo::doClipping( &dst_rect, clip ) == 0) ){
info->addShadeArea(dst_rect, shade_distance);
dirty_rect.add( dst_rect );
}
else if ( flush_flag ){
info->addShadeArea(dst_rect, shade_distance);
flushDirect( dst_rect, REFRESH_NONE_MODE );
}
}
/* ---------------------------------------- */
/* Update text buffer */
if (script_h.enc.getEncoding() == Encoding::CODE_CP932) {
if (IS_TWO_BYTE(text[0]))
info->advanceCharInHankaku(2);
else
info->advanceCharInHankaku(1);
} else {
// OYABB :D
info->advanceCharInHankaku(strpxlen(out_text, info));
}
if ( lookback_flag ){
/*
current_page->add( text[0] );
if (IS_TWO_BYTE(text[0]))
current_page->add( text[1] );
*/
int n = script_h.enc.getBytes(text[0]);
for (int i=0; i<n; i++)
current_page->add(text[i]);
}
}
void ONScripterLabel::drawString( const char *str, uchar3 color, Fontinfo *info,
bool flush_flag, SDL_Surface *surface,
int abs_offset, SDL_Rect *rect,
AnimationInfo *cache_info, bool skip_whitespace_flag )
{
int i;
int start_xy[2];
start_xy[0] = info->xy[0];
start_xy[1] = info->xy[1];
/* ---------------------------------------- */
/* Draw selected characters */
uchar3 org_color;
setColor(org_color, info->color);
setColor(info->color, color);
bool tateyoko = (info->getTateyokoMode() == Fontinfo::TATE_MODE);
char text[5] = { '\0', '\0', '\0', '\0', '\0' };
while( *str ){
while (*str == ' ' && skip_whitespace_flag) str++;
if (!*str) break;
if ( *str == '`' ){
str++;
skip_whitespace_flag = false;
continue;
}
if (cache_info && !cache_info->is_tight_region){
if (*str == '('){
startRuby((char *)str+1, *info);
info->addLineOffset(ruby_struct.margin);
str++;
continue;
}
else if (*str == '/' && ruby_struct.stage == RubyStruct::BODY ){
info->addLineOffset(ruby_struct.margin);
str = ruby_struct.ruby_end;
if (*ruby_struct.ruby_end == ')'){
endRuby(false, false, NULL, cache_info);
str++;
}
continue;
}
else if (*str == ')' && ruby_struct.stage == RubyStruct::BODY ){
ruby_struct.stage = RubyStruct::NONE;
str++;
continue;
}
//Mion: handling special text locate chars that could be found in
//a "getlog" string
else if (*str == ScriptHandler::TEXT_FF) {
//form feed - reset to the top of the page (locate)
if (tateyoko)
info->xy[0] = (info->num_xy[0]-1) * 2;
else
info->xy[1] = 0;
str++;
continue;
}
else if (*str == ScriptHandler::TEXT_VTAB) {
//vertical tab - move one line down the page (locate)
if (tateyoko)
info->xy[0] -= 2;
else
info->xy[1] += 2;
str++;
continue;
}
else if (*str == ScriptHandler::TEXT_CR) {
//carriage return - reset to the start of the line (locate)
if (tateyoko)
info->xy[1] = 0;
else
info->xy[0] = 0;
str++;
continue;
}
else if (*str == ScriptHandler::TEXT_UP) {
info->setXY(-1, info->xy[1]/2 - 1);
str++;
continue;
}
else if (*str == ScriptHandler::TEXT_DOWN) {
info->setXY(-1, info->xy[1]/2 + 1);
str++;
continue;
}
else if (*str == ScriptHandler::TEXT_LEFT) {
info->setXY(info->xy[0]/2 - 1);
str++;
continue;
}
else if (*str == ScriptHandler::TEXT_RIGHT) {
info->setXY(info->xy[0]/2 + 1);
str++;
continue;
}
}
int n = script_h.enc.getBytes(*str);
//if ( IS_TWO_BYTE(*str) ){
if (n > 1) {
/* Kinsoku process */
if (isEndKinsoku(str+n)) {
int i = 0;
// While we keep running into endkinsoku characters...
while (!info->isEndOfLine(i) &&
isEndKinsoku( str+n+i )){
// ...keep moving along by the number of bytes in
// the previous character we checked
i += script_h.enc.getBytes(*(str+n+i));
}
if (info->isEndOfLine(i)){
info->newLine();
for (int i=0 ; i<indent_offset ; i++){
if (script_h.enc.getEncoding() == Encoding::CODE_CP932)
sentence_font.advanceCharInHankaku(2);
else
sentence_font.advanceCharInHankaku(info->pitch_xy[0]);
}
}
}
for (int i=0; i<n; i++) {
text[i] = *str++;
}
drawChar( text, info, false, false, surface, cache_info, abs_offset );
}
else if ((*str == 0x0a) ||
((*str == '\\') && info->is_newline_accepted)){
info->newLine();
str++;
}
else{
text[0] = *str++;
text[1] = '\0';
drawChar( text, info, false, false, surface, cache_info, abs_offset );
//Mion: fix for allowing mixed 1 & 2 byte chars in English mode
/*
if (script_h.preferred_script == ScriptHandler::JAPANESE_SCRIPT) {
if (*str && *str != 0x0a){
text[0] = *str++;
drawChar( text, info, false, false, surface, cache_info, abs_offset );
}
}
*/
if (script_h.preferred_script == ScriptHandler::JAPANESE_SCRIPT) {
// This fix may be unnecessary now, but just in case...
// Yippee nesting
for (int i=0; i<3; i++) {
if (*str && *str != 0x0a){
text[0] = *str++;
drawChar( text, info, false, false, surface, cache_info, abs_offset );
} else {
break;
}
}
}
}
}
for ( i=0 ; i<3 ; i++ ) info->color[i] = org_color[i];
/* ---------------------------------------- */
/* Calculate the area of selection */
SDL_Rect clipped_rect = info->calcUpdatedArea(start_xy, screen_ratio1, screen_ratio2, script_h.enc.getEncoding());
clipped_rect.x += abs_offset;
info->addShadeArea(clipped_rect, shade_distance);
if ( flush_flag )
flush( refresh_window_text_mode, &clipped_rect );
if ( rect ) *rect = clipped_rect;
}
void ONScripterLabel::restoreTextBuffer(SDL_Surface *surface)
{
text_info.fill( 0, 0, 0, 0 );
char out_text[5] = { '\0', '\0', '\0', '\0', '\0' };
Fontinfo f_info = sentence_font;
f_info.clear();
bool tateyoko = (f_info.getTateyokoMode() == Fontinfo::TATE_MODE);
int n;
for ( int i=0 ; i<current_page->text_count ; i+=n ){
n = script_h.enc.getBytes(current_page->text[i]);
if ( current_page->text[i] == 0x0a ){
f_info.newLine();
}
else{
// Copy the whole character into out_text from
// current_page, taking into account the offset
for (int j=0; j<n; j++) {
out_text[j] = current_page->text[i+j];
out_text[j+1] = '\0'; // To prevent overflowing into
// adjacent characters
}
if (out_text[0] == '('){
startRuby(current_page->text + i + 1, f_info);
f_info.addLineOffset(ruby_struct.margin);
continue;
}
else if (out_text[0] == '/' && ruby_struct.stage == RubyStruct::BODY ){
f_info.addLineOffset(ruby_struct.margin);
i = ruby_struct.ruby_end - current_page->text - 1;
if (*ruby_struct.ruby_end == ')'){
endRuby(false, false, NULL, &text_info);
i++;
}
continue;
}
else if (out_text[0] == ')' && ruby_struct.stage == RubyStruct::BODY ){
ruby_struct.stage = RubyStruct::NONE;
continue;
}
else if (out_text[0] == ScriptHandler::TEXT_FF) {
//form feed - reset to the top of the page (locate)
if (tateyoko)
f_info.xy[0] = (f_info.num_xy[0]-1) * 2;
else
f_info.xy[1] = 0;
continue;
}
else if (out_text[0] == ScriptHandler::TEXT_VTAB) {
//vertical tab - move one line down the page (locate)
if (tateyoko)
f_info.xy[0] -= 2;
else
f_info.xy[1] += 2;
continue;
}
else if (out_text[0] == ScriptHandler::TEXT_CR) {
//carriage return - reset to the start of the line (locate)
if (tateyoko)
f_info.xy[1] = 0;
else
f_info.xy[0] = 0;
continue;
}
else if (out_text[0] == ScriptHandler::TEXT_UP) {
f_info.setXY(-1, f_info.xy[1]/2 - 1);
continue;
}
else if (out_text[0] == ScriptHandler::TEXT_DOWN) {
f_info.setXY(-1, f_info.xy[1]/2 + 1);
continue;
}
else if (out_text[0] == ScriptHandler::TEXT_LEFT) {
f_info.setXY(f_info.xy[0]/2 - 1);
continue;
}
else if (out_text[0] == ScriptHandler::TEXT_RIGHT) {
f_info.setXY(f_info.xy[0]/2 + 1);
continue;
}
drawChar( out_text, &f_info, false, false, surface, &text_info );
}
}
}
void ONScripterLabel::enterTextDisplayMode(bool text_flag)
{
if (line_enter_status <= 1 && saveon_flag && internal_saveon_flag && text_flag){
saveSaveFile( -1 );
internal_saveon_flag = false;
}
did_leavetext = false;
if ( !(display_mode & DISPLAY_MODE_TEXT) ){
refreshSurface( effect_dst_surface, NULL, refresh_window_text_mode );
dirty_rect.clear();
dirty_rect.add( sentence_font_info.pos );
if (setEffect(&window_effect, false, true)) return;
while(doEffect(&window_effect, false));
display_mode = DISPLAY_MODE_TEXT;
text_on_flag = true;
}
}
void ONScripterLabel::leaveTextDisplayMode(bool force_leave_flag)
{
//ons-en feature: when in certain skip modes, don't actually leave
//text display mode unless forced to (but say you did)
if (!force_leave_flag && ( skip_mode & (SKIP_NORMAL | SKIP_TO_EOP) || ctrl_pressed_status )) {
did_leavetext = true;
return;
}
if (force_leave_flag) did_leavetext = false;
if ( !did_leavetext && (display_mode & DISPLAY_MODE_TEXT) &&
(force_leave_flag || (erase_text_window_mode != 0)) ){
SDL_BlitSurface(backup_surface, NULL, effect_dst_surface, NULL);
dirty_rect.add(sentence_font_info.pos);
if (setEffect(&window_effect, false, false)) return;
while(doEffect(&window_effect, false));
display_mode = DISPLAY_MODE_NORMAL;
}
display_mode |= DISPLAY_MODE_UPDATED;
}
bool ONScripterLabel::doClickEnd()
{
bool ret = false;
draw_cursor_flag = true;
if (!((skip_mode & SKIP_TO_EOL) && clickskippage_flag))
skip_mode &= ~(SKIP_TO_WAIT | SKIP_TO_EOL);
if ( automode_flag ){
event_mode = WAIT_TEXT_MODE | WAIT_INPUT_MODE |
WAIT_VOICE_MODE | WAIT_TIMER_MODE;
if ( automode_time < 0 )
ret = waitEvent( -automode_time * num_chars_in_sentence );
else
ret = waitEvent( automode_time );
}
else if ( autoclick_time > 0 ){
event_mode = WAIT_SLEEP_MODE | WAIT_TIMER_MODE;
ret = waitEvent( autoclick_time );
}
else{
event_mode = WAIT_TEXT_MODE | WAIT_INPUT_MODE | WAIT_TIMER_MODE;
ret = waitEvent(-1);
}
num_chars_in_sentence = 0;
draw_cursor_flag = false;
return ret;
}
bool ONScripterLabel::clickWait()
{
int tmp_skip = skip_mode;
skip_mode &= ~(SKIP_TO_WAIT | SKIP_TO_EOL);
flush( REFRESH_NONE_MODE );
//Mion: apparently NScr doesn't call textgosub on clickwaits
// while in skip mode (but does call it on pagewaits)
if ( (skip_mode & (SKIP_NORMAL | SKIP_TO_EOP)) ||
((tmp_skip & SKIP_TO_EOL) && clickskippage_flag) ||
ctrl_pressed_status ){
skip_mode = tmp_skip;
clickstr_state = CLICK_NONE;
num_chars_in_sentence = 0;
if ( textgosub_label && (script_h.getNext()[0] != 0x0a))
new_line_skip_flag = true;
event_mode = IDLE_EVENT_MODE;
if ( waitEvent(0) ) return false;
}
else{
key_pressed_flag = false;
if ( textgosub_label ){
if ((tmp_skip & SKIP_TO_EOL) && clickskippage_flag)
skip_mode = tmp_skip;
saveoffCommand();
clickstr_state = CLICK_NONE;
char *next = script_h.getNext();
if (*next == 0x0a) {
textgosub_clickstr_state = CLICK_WAITEOL;
} else {
new_line_skip_flag = true;
textgosub_clickstr_state = CLICK_WAIT;
}
gosubReal( textgosub_label, next, true );
return false;
}
clickstr_state = CLICK_WAIT;
if (doClickEnd()) return false;
clickstr_state = CLICK_NONE;
key_pressed_flag = false;
}
script_h.cur_rgosub_wait++;
return true;
}
bool ONScripterLabel::clickNewPage()
{
skip_mode &= ~(SKIP_TO_WAIT | SKIP_TO_EOL);
flush( REFRESH_NONE_MODE );
clickstr_state = CLICK_NEWPAGE;
if ( (skip_mode & SKIP_NORMAL || ctrl_pressed_status) && !textgosub_label ){
clickstr_state = CLICK_NONE;
num_chars_in_sentence = 0;
event_mode = IDLE_EVENT_MODE;
if (waitEvent(0)) return false;
}
else{
key_pressed_flag = false;
if ( textgosub_label ){
saveoffCommand();
clickstr_state = CLICK_NONE;
char *next = script_h.getNext();
textgosub_clickstr_state = CLICK_NEWPAGE;
gosubReal( textgosub_label, next, true );
return false;
}
if (doClickEnd()) return false;
}
newPage( true );
clickstr_state = CLICK_NONE;
key_pressed_flag = false;
script_h.cur_rgosub_wait++;
return true;
}
void ONScripterLabel::startRuby(char *buf, Fontinfo &info)
{
ruby_struct.stage = RubyStruct::BODY;
ruby_font = info;
if (ruby_font.ttf_font == NULL &&
script_h.enc.getEncoding() == Encoding::CODE_UTF8) {
fprintf(stderr, "Error: incorrect font information for ruby text!\nThis is a known bug; for now, please do not use ruby text on the first line.\n");
exit(1);
//ruby_font.openFont(font_file, screen_ratio1, screen_ratio2);
}
// Don't do this in UTF-8 mode
if (script_h.enc.getEncoding() == Encoding::CODE_CP932)
ruby_font.ttf_font = NULL;
if ( ruby_struct.font_size_xy[0] != -1 )
ruby_font.font_size_xy[0] = ruby_struct.font_size_xy[0];
else
ruby_font.font_size_xy[0] = info.font_size_xy[0]/2;
if ( ruby_struct.font_size_xy[1] != -1 )
ruby_font.font_size_xy[1] = ruby_struct.font_size_xy[1];
else
ruby_font.font_size_xy[1] = info.font_size_xy[1]/2;
ruby_struct.body_count = 0;
ruby_struct.ruby_count = 0;
int n = 0;
char check_text[5] = {'\0', '\0', '\0', '\0', '\0'};
while(1){
if ( *buf == '/' ){
ruby_struct.stage = RubyStruct::RUBY;
ruby_struct.ruby_start = buf+1;
}
else if ( *buf == ')' || *buf == '\0' ){
break;
}
else {
if (script_h.enc.getEncoding() == Encoding::CODE_CP932) {
n=1;
if ( ruby_struct.stage == RubyStruct::BODY )
ruby_struct.body_count++;
else if ( ruby_struct.stage == RubyStruct::RUBY )
ruby_struct.ruby_count++;
} else {
n = script_h.enc.getBytes(*buf);
for (int i=0; i<n; i++) {
check_text[i] = buf[i];
}
if ( ruby_struct.stage == RubyStruct::BODY )
ruby_struct.body_count+=strpxlen(check_text, &ruby_font);
else if ( ruby_struct.stage == RubyStruct::RUBY )
ruby_struct.ruby_count+=strpxlen(check_text, &ruby_font);
}
}
buf += n;
}
// In case it was skipped earlier due to UTF-8 mode
ruby_font.ttf_font = NULL;
ruby_struct.ruby_end = buf;
ruby_struct.stage = RubyStruct::BODY;
//TODO: calculate body count and ruby count in px
ruby_struct.margin = ruby_font.initRuby(info, ruby_struct.body_count/2, ruby_struct.ruby_count/2, script_h.enc.getEncoding());
}
void ONScripterLabel::endRuby(bool flush_flag, bool lookback_flag, SDL_Surface *surface, AnimationInfo *cache_info)
{
char out_text[5]= {'\0', '\0', '\0', '\0', '\0'};
if ( rubyon_flag ){
ruby_font.clear();
char *buf = ruby_struct.ruby_start;
while( buf < ruby_struct.ruby_end ){
// I should probably leave this here until my fix has been
// tested -Galladite 2023-6-21
/*
out_text[0] = *buf;
if ( IS_TWO_BYTE(*buf) ){
out_text[1] = *(buf+1);
drawChar( out_text, &ruby_font, flush_flag, lookback_flag, surface, cache_info );
buf++;
}
else{
out_text[1] = '\0';
drawChar( out_text, &ruby_font, flush_flag, lookback_flag, surface, cache_info );
}
buf++;
*/
int n = script_h.enc.getBytes(*buf);
for (int i=0; i<n; i++) {
out_text[i] = *buf;
buf++;
}
drawChar( out_text, &ruby_font, flush_flag, lookback_flag, surface, cache_info );
}
}
ruby_struct.stage = RubyStruct::NONE;
}
int ONScripterLabel::textCommand()
{
if (line_enter_status <= 1 && saveon_flag && internal_saveon_flag){
saveSaveFile( -1 );
internal_saveon_flag = false;
}
char *buf = script_h.getStringBuffer();
if (pretextgosub_label &&
( (script_h.current_cmd_type == ScriptHandler::CMD_PRETEXT) ||
((!pagetag_flag || (page_enter_status == 0)) &&
(line_enter_status == 0)) )){
bool tag_flag = (script_h.current_cmd_type == ScriptHandler::CMD_PRETEXT);
if (current_page->tag) delete[] current_page->tag;
if (tag_flag && (buf[0] != '\0')){
int len = strlen(buf);
string_buffer_offset = len;
current_page->tag = new char[len+1];
memcpy(current_page->tag, buf, strlen(buf));
current_page->tag[len] = 0;
}
else{
current_page->tag = NULL;
}
gosubReal( pretextgosub_label, script_h.getNext(), true );
line_enter_status = 1;
return RET_CONTINUE;
}
refresh_window_text_mode = REFRESH_NORMAL_MODE | REFRESH_WINDOW_MODE | REFRESH_TEXT_MODE;
enterTextDisplayMode();
// Use an enum, for crying out loud!! What does this mean?? -Galladite 2023-5-23
line_enter_status = 2;
if (pagetag_flag) page_enter_status = 1;
if (debug_level > 1)
printf("textCommand %s %d %d %d\n", script_h.getStringBuffer() + string_buffer_offset, string_buffer_offset, event_mode, line_enter_status);
while(processText());
return RET_CONTINUE;
}
void ONScripterLabel::processEOT()
{
skip_mode &= ~SKIP_TO_WAIT;
if ((skip_mode & SKIP_TO_EOL) || (sentence_font.wait_time == 0)){
flush(refreshMode());
if (!clickskippage_flag && !skip_past_newline)
skip_mode &= ~SKIP_TO_EOL;
}
indent_offset = 0;
if (!sentence_font.isLineEmpty() && !new_line_skip_flag){
doLineBreak(true);
}
line_enter_status = 0;
}
bool ONScripterLabel::processText()
//Mion: extensively modified the text processing
{
//printf("processText %s %d %d %d\n", script_h.getStringBuffer() + string_buffer_offset, string_buffer_offset, event_mode, line_enter_status);
char out_text[5]= {'\0', '\0', '\0', '\0', '\0'};
bool old_new_line_skip_flag = new_line_skip_flag; //Mion: for temp Umineko8 fix
// process linebreaking when at the start of (non-pretext) buffer
if (line_enter_status == 2) {
if (!new_line_skip_flag)
line_has_nonspace = true;