1212#MAX_CHARS_PER_LINE=20
1313MAX_CHARS_PER_LINE = 35
1414
15+ SHORT_DIALOG_WARNING_LEN = 10
16+
17+
1518import sys
1619import re
1720
@@ -22,8 +25,25 @@ def remove_tags(text):
2225 text = re .sub (r'<[^>]+' , '' , text )
2326 return text
2427
28+
29+ def length_after_last_tag (s ):
30+ tag_end = s .rfind ('>' )
31+ if tag_end == - 1 :
32+ return 0 # No closing tag found, so return 0
33+ return len (s ) - tag_end - 1
2534
26-
35+
36+ def last_line_length (s ):
37+ last_lineend_pos = s .rfind ('<CC03EA>' )
38+ if last_lineend_pos > 0 :
39+ pause_pos = s .find ('<pause' , last_lineend_pos )
40+ if pause_pos > 0 :
41+ return (pause_pos - last_lineend_pos - len ('<pause>' ))
42+ # else
43+ return 1000
44+
45+
46+
2747def insert_lineend_every_x_chars (s , init_lines_counter = 0 ):
2848 position = 0
2949 result = ""
@@ -33,6 +53,11 @@ def insert_lineend_every_x_chars(s, init_lines_counter=0):
3353 for token in s .split (" " ):
3454 #print(token)
3555 #print(remove_tags(token))
56+
57+ #if token.endswith("<lineend"):
58+ # #token = token.rstrip("<lineend")
59+ # curr_lines_counter += 1
60+
3661 if (curr_char_counter + len (remove_tags (token )) + 1 ) < MAX_CHARS_PER_LINE :
3762 # just append the token
3863 if curr_char_counter == 0 :
@@ -52,23 +77,18 @@ def insert_lineend_every_x_chars(s, init_lines_counter=0):
5277 result += "<pause><lineend><CC03EA>" + token
5378 curr_lines_counter = 0 # reset
5479 curr_char_counter = len (remove_tags (token ))
55-
56- return result
57-
58-
59-
60-
61- def length_after_last_tag (s ):
62- tag_end = s .rfind ('>' )
63- if tag_end == - 1 :
64- return 0 # No closing tag found, so return 0
65- return len (s ) - tag_end - 1
66-
80+ # endif
81+ # end for
6782
83+ if last_line_length (result ) < SHORT_DIALOG_WARNING_LEN :
84+ print ("SHORT_DIALOG_WARNING: " + result )
6885
86+ return result
6987
7088
7189
90+
91+
7292
7393f = open (sys .argv [1 ], 'r' , encoding = 'shift_jis' , errors = 'ignore' )
7494out = open (sys .argv [2 ], 'w' , encoding = 'shift_jis' )
@@ -83,7 +103,10 @@ def length_after_last_tag(s):
83103 tags = re .split (r'(?<!color=\w{4})>' , line )
84104 #print(tags)
85105 # ['<CC03EA', '<color=0006><string=0014', '<lineend', '<color=0001>Do you need some directions?<pause', '<lineend', '<CC03EA', "The village of <color=0002>Nutsbill<color=0001> is straight to the west from this town's left exit.<pause", '<lineend', '|\n']
106+ # ['<CC03EA', "<color=0001>I can hear Jestonai's voice coming from somewhere...<pause", '<lineend', '<CC03EA', '<color=0004>Jestonai<lineend', '<color=0001>You who yearns for Knighthood. Defeat this monster, and prove your strength!<pause', '<lineend']
86107 for t in tags :
108+ #print(t)
109+ #print(init_lines_counter)
87110 if t .startswith ("<lineend" ):
88111 init_lines_counter += 1
89112 continue
@@ -95,14 +118,24 @@ def length_after_last_tag(s):
95118 if t .startswith ("<pause" ):
96119 init_lines_counter = 0 # new dialog, reset to 0
97120 continue
98-
121+
99122 if (len (remove_tags (t )) < MAX_CHARS_PER_LINE ):
100123 # nothing to change
124+ if t .endswith ("<lineend" ):
125+ init_lines_counter += 1
101126 continue
102127 else :
103128 # add "<lineend>"/<pause> tags where needed
129+ #print(t)
130+ #print(init_lines_counter)
104131 new_line_text = insert_lineend_every_x_chars (t , init_lines_counter )
105132 line = line .replace (t , new_line_text )
133+
134+ #if t.endswith("<lineend"):
135+ # init_lines_counter += 1
136+ # if init_lines_counter >= 3:
137+ # # replace last "<lineend" -> pause tag
138+
106139 # end for tags
107140 # end if
108141 out .write (line )
0 commit comments