forked from ThunderGemios10/The-Super-Duper-Script-Editor-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_printer.py
1305 lines (957 loc) · 42.6 KB
/
text_printer.py
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
################################################################################
### Copyright © 2012-2013 BlackDragonHunt
###
### This file is part of the Super Duper Script Editor.
###
### The Super Duper Script Editor 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 3 of the License,
### or (at your option) any later version.
###
### The Super Duper Script Editor 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 the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################
from PyQt4 import QtCore, QtGui, Qt
from PyQt4.QtGui import QImage, QPainter, QColor, QPixmap, QBitmap, QTransform
from PyQt4.QtCore import QRect, QRectF
from enum import Enum
import ctypes
import os.path
import re
import sys
import time
import common
from clt import CLT_STYLES
import font_parser
from font_parser import FONT_DATA
from object_labels import get_char_name, get_ammo_name, get_present_name
from text_files import load_text
from text_format import TextFormat, TEXT_ALIGN, TEXT_ORIENT, TEXT_FORMATS
from sprite import get_sprite_file, SPRITE_TYPE
################################################################################
### VARIABLES
################################################################################
IMG_W = 480
IMG_H = 272
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# GFX_DIR = os.path.join(BASE_DIR, "data/gfx")
GFX_DIR = common.editor_config.gfx_dir
AMMO_DIR = os.path.join(GFX_DIR, "ammo")
ANAGRAM_DIR = os.path.join(GFX_DIR, "anagram")
BG_DIR = os.path.join(GFX_DIR, "bg")
BGD_DIR = os.path.join(GFX_DIR, "bgd")
CUTIN_DIR = os.path.join(GFX_DIR, "cutin")
FLASH_DIR = os.path.join(GFX_DIR, "flash")
FONT_DIR = os.path.join(GFX_DIR, "font")
GALLERY_DIR = os.path.join(GFX_DIR, "gallery")
MENU_DIR = os.path.join(GFX_DIR, "menu")
MOVIE_DIR = os.path.join(GFX_DIR, "movies")
PRESENT_DIR = os.path.join(GFX_DIR, "presents")
SPRITE_DIR = os.path.join(GFX_DIR, "sprites")
TEXTBOX_DIR = os.path.join(GFX_DIR, "textbox")
TRIAL_DIR = os.path.join(GFX_DIR, "trial")
IMG_FILTERS = Enum("unfiltered", "sepia", "inverted")
FONTS = {}
################################################################################
### FUNCTIONS
################################################################################
##############################################################################
### @fn load_fonts()
### @desc Loads the two font images.
##############################################################################
def load_fonts():
FONTS[1] = QImage(os.path.join(FONT_DIR, "Font01.png"))
FONTS[2] = QImage(os.path.join(FONT_DIR, "Font02.png"))
font_parser.parse_font(1, os.path.join(FONT_DIR, "Font01.font"))
font_parser.parse_font(2, os.path.join(FONT_DIR, "Font02.font"))
##############################################################################
### @fn replace_all_colors(image, color)
### @desc Replaces all colors in the image with the given color.
##############################################################################
def replace_all_colors(image, color):
new_img = image.copy()
if not new_img.format() is QImage.Format_ARGB32_Premultiplied:
new_img = new_img.convertToFormat(QImage.Format_ARGB32_Premultiplied)
color_img = QImage(new_img.width(), new_img.height(), QImage.Format_ARGB32_Premultiplied)
color_img.fill(color.rgba())
painter = QPainter(new_img)
painter.setCompositionMode(QPainter.CompositionMode_SourceAtop)
painter.drawImage(new_img.rect(), color_img, color_img.rect())
painter.end()
return new_img
##############################################################################
### @fn add_v_gradient(image, colors)
### @desc Paints a vertical gradient over the image from colors[0] to colors[i]
##############################################################################
def add_v_gradient(image, colors):
if len(colors) < 2:
return image
new_img = image.copy()
if not new_img.format() is QImage.Format_ARGB32_Premultiplied:
new_img = new_img.convertToFormat(QImage.Format_ARGB32_Premultiplied)
gradient = QtGui.QLinearGradient(0, 0, 0, new_img.height())
gradient.setColorAt(0, colors[0])
for i in range(1, len(colors) - 1):
gradient.setColorAt(i / len(colors), colors[i])
gradient.setColorAt(1, colors[-1])
painter = QPainter(new_img)
painter.setCompositionMode(QPainter.CompositionMode_SourceAtop)
painter.fillRect(new_img.rect(), gradient)
painter.end()
return new_img
##############################################################################
### @fn add_border(image, color, size)
### @desc Adds a colored border to the image "size" pixels large.
##############################################################################
def add_border(image, color, size):
w = image.width()
h = image.height()
out = QImage(w + (size * 2), h + (size * 2), QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
#border = image.scaled(w, h, Qt.Qt.KeepAspectRatioByExpanding, Qt.Qt.FastTransformation)
border = replace_all_colors(image, color)
painter = QPainter(out)
for i in range(0, (size * 2) + 1):
for j in range(0, (size * 2) + 1):
painter.drawImage(QRectF(i, j, w, h), border, QRectF((border.rect())))
painter.drawImage(QRect(size, size, w, h), image, image.rect())
painter.end()
return out
##############################################################################
### @fn filter_image(image)
### @desc Filters the image.
### The DLL crashes on app quit for whatever reason,
### so this presently does nothing at all. <_>
##############################################################################
def filter_image(image, filter):
if filter == IMG_FILTERS.unfiltered:
return image
out = image.copy()
# So we know we have 32 bits
out.convertToFormat(QImage.Format_ARGB32_Premultiplied)
if filter == IMG_FILTERS.sepia:
pass
# Slow as all hell.
#def to_sepia(color):
# gray = QtGui.qGray(color)
# sepia_r = gray * 255 / 255
# sepia_g = gray * 240 / 255
# sepia_b = gray * 192 / 255
# sepia_a = QtGui.qAlpha(color)
# #return QtGui.qRgba(sepia_r, sepia_g, sepia_b, sepia_a)
# return sepia_r, sepia_g, sepia_b, sepia_a
#
#bpp = out.byteCount() / (out.width() * out.height())
#assert bpp == 4 # Why would it be anything else?
#
#for j in range(out.height()):
# scanline = out.scanLine(j)
# scanline.setsize(out.bytesPerLine())
# QRgba* line = (QRgba*)out.scanLine(j)
#
# for i in range(out.width()):
# pixel = scanline[i * bpp : (i + 1) * bpp]
# b, g, r, a = ord(pixel[0]), ord(pixel[1]), ord(pixel[2]), ord(pixel[3])
# sepia_r, sepia_g, sepia_b, sepia_a = to_sepia(QtGui.qRgba(r, g, b, a))
# pixel[0] = chr(sepia_b)
# pixel[1] = chr(sepia_g)
# pixel[2] = chr(sepia_r)
# pixel[3] = chr(sepia_a)
elif filter == IMG_FILTERS.inverted:
out.invertPixels()
#pixels_ptr = ctypes.c_void_p(out.bits().__int__())
#img = filters.image(pixels_ptr, out.width(), out.height(), out.width(), out.height())
#g_val = 32
#filters.flatten(img, filters.rgb(g_val, g_val, g_val), filters.sepia)
#filters.brightness(img, 32)
#filters.contrast(img, 164)
#filters.desaturate(img, 0.15)
return out
##############################################################################
### @fn draw_centering_guides(image, target_x, target_y, target_w, guide_h)
### @desc Draws two vertical red lines surrounding the given area
### to be used to assist in centering text.
##############################################################################
def draw_centering_guides(image, target_x, target_y, target_w, guide_h):
left_x = target_x - (target_w / 2.0)
right_x = left_x + target_w
top_y = target_y
bottom_y = top_y + guide_h
new_img = image.copy()
if not new_img.format() is QImage.Format_ARGB32_Premultiplied:
new_img = new_img.convertToFormat(QImage.Format_ARGB32_Premultiplied)
painter = QPainter(new_img)
pen = painter.pen()
pen.setColor(QColor(255, 0, 0))
painter.setPen(pen)
painter.drawLine(left_x, top_y, left_x, bottom_y)
painter.drawLine(right_x, top_y, right_x, bottom_y)
painter.end()
return new_img
def get_nametag(char_id, x, y, color, vertical = False):
w = IMG_W
h = IMG_H
nametag = get_char_name(char_id, common.editor_config.data01_dir)
if nametag == None:
nametag = "NAMETAG MISSING"
if vertical:
nametag_img = QImage(h, w, QImage.Format_ARGB32_Premultiplied)
new_x = h - y
new_y = x
x = new_x
y = new_y
else:
nametag_img = QImage(w, h, QImage.Format_ARGB32_Premultiplied)
format = TextFormat(x = x, y = y, w = 25, h = 25, align = TEXT_ALIGN.left, orient = TEXT_ORIENT.hor, clt = 0)
nametag_img = print_text(nametag_img, nametag, None, format = format, mangle = False)
if vertical:
matrix = QTransform()
matrix.rotate(-90)
nametag_img = nametag_img.transformed(matrix)
nametag_img = replace_all_colors(nametag_img, color)
return nametag_img
##############################################################################
### @fn get_sprite(sprite_id)
### @desc Returns the sprite for the specified sprite ID.
##############################################################################
def get_sprite(sprite_id):
sprite_file = get_sprite_file(sprite_id)
if sprite_file == None:
return None
sprite_file = os.path.join(SPRITE_DIR, sprite_file)
if not os.path.isfile(sprite_file):
sprite_file = os.path.join(SPRITE_DIR, "bustup_%02d_%02d.png" % (99, 99))
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
sprite = QImage(sprite_file)
# Center the sprite on our image.
sprite_x = (out.width() - sprite.width()) / 2
sprite_y = 0
painter.drawImage(QRect(sprite_x, sprite_y, sprite.width(), sprite.height()), sprite, sprite.rect())
painter.end()
return out
##############################################################################
### @fn get_bg(room_id)
### @desc Returns the background image for the specified room ID.
##############################################################################
def get_bg(room_id):
if room_id == -1:
return None
bg_file = os.path.join(BG_DIR, "bg_%03d.png" % room_id)
if not os.path.isfile(bg_file):
bg_file = os.path.join(BG_DIR, "bg_%03d.png" % 999)
out = QImage(bg_file)
if not out.format == QImage.Format_ARGB32_Premultiplied:
out = out.convertToFormat(QImage.Format_ARGB32_Premultiplied)
return out
##############################################################################
### @fn get_bgd(bgd_id)
### @desc Returns the background image for the specified CG background.
##############################################################################
def get_bgd(bgd_id):
if bgd_id == -1:
return None
bgd_file = os.path.join(BGD_DIR, "bgd_%03d.png" % bgd_id)
if not os.path.isfile(bgd_file):
bgd_file = os.path.join(BG_DIR, "%04d.png" % 9999)
out = QImage(bgd_file)
if not out.format == QImage.Format_ARGB32_Premultiplied:
out = out.convertToFormat(QImage.Format_ARGB32_Premultiplied)
return out
##############################################################################
### @fn get_ammo(ammo_id, x, y)
### @desc Returns the specified ammo.
##############################################################################
def get_ammo(ammo_id, x, y):
if ammo_id == -1:
return None
ammo_file = os.path.join(AMMO_DIR, "kotodama_ico_%03d.png" % ammo_id)
ammo_border_file = os.path.join(AMMO_DIR, "border.png")
if not os.path.isfile(ammo_file):
ammo_file = os.path.join(AMMO_DIR, "kotodama_ico_%03d.png" % 999)
ammo = QImage(ammo_file)
border = QImage(ammo_border_file)
x_pos = x
y_pos = y
border_x = x_pos - ((border.width() - ammo.width()) / 2)
border_y = y_pos - ((border.height() - ammo.height()) / 2)
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
painter.drawImage(QRect(x_pos, y_pos, ammo.width(), ammo.height()), ammo, ammo.rect())
painter.drawImage(QRect(border_x, border_y, border.width(), border.height()), border, border.rect())
painter.end()
return out
##############################################################################
### @fn get_ammo_ingame(ammo_id)
### @desc Returns the specified ammo.
##############################################################################
def get_ammo_ingame(ammo_id):
x_pos = 144
y_pos = 59
return get_ammo(ammo_id, x_pos, y_pos)
##############################################################################
### @fn get_ammo_menu(ammo_id)
### @desc Returns the specified ammunition icon.
##############################################################################
def get_ammo_menu(ammo_id):
x_pos = 35
y_pos = 80
return get_ammo(ammo_id, x_pos, y_pos)
##############################################################################
### @fn get_cutin(cutin_id)
### @desc Returns the specified cutin.
##############################################################################
def get_cutin(cutin_id):
if cutin_id == -1:
return None
cutin_file = os.path.join(CUTIN_DIR, "cutin_ico_%03d.png" % cutin_id)
cutin_border_file = os.path.join(CUTIN_DIR, "border.png")
if not os.path.isfile(cutin_file):
cutin_file = os.path.join(CUTIN_DIR, "cutin_ico_%03d.png" % 999)
cutin = QImage(cutin_file)
border = QImage(cutin_border_file)
x_pos = 307
y_pos = 91
border_x = x_pos - ((border.width() - cutin.width()) / 2)
border_y = y_pos - ((border.height() - cutin.height()) / 2)
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
painter.drawImage(QRect(x_pos, y_pos, cutin.width(), cutin.height()), cutin, cutin.rect())
painter.drawImage(QRect(border_x, border_y, border.width(), border.height()), border, border.rect())
painter.end()
return out
##############################################################################
### @fn get_flash(flash_id)
### @desc Returns the background image for the specified flash event.
##############################################################################
def get_flash(flash_id):
if flash_id == -1:
return None
flash_file = os.path.join(FLASH_DIR, "fla_%03d.png" % flash_id)
if not os.path.isfile(flash_file):
flash_file = os.path.join(FLASH_DIR, "fla_%03d.png" % 999)
out = QImage(flash_file)
if not out.format == QImage.Format_ARGB32_Premultiplied:
out = out.convertToFormat(QImage.Format_ARGB32_Premultiplied)
return out
##############################################################################
### @fn get_movie(movie_id)
### @desc Returns the background image for the specified movie.
##############################################################################
def get_movie(movie_id):
if movie_id == -1:
return None
movie_file = os.path.join(MOVIE_DIR, "movie_%02d.png" % movie_id)
if not os.path.isfile(movie_file):
movie_file = os.path.join(MOVIE_DIR, "movie_%02d.png" % 99)
out = QImage(movie_file)
if not out.format == QImage.Format_ARGB32_Premultiplied:
out = out.convertToFormat(QImage.Format_ARGB32_Premultiplied)
return out
##############################################################################
### @fn get_present(file_id)
### @desc Returns the specified present icon.
##############################################################################
def get_present(file_id, x_pos, y_pos):
icon_file = os.path.join(PRESENT_DIR, "present_ico_%03d.png" % file_id)
if not os.path.isfile(icon_file):
icon_file = os.path.join(PRESENT_DIR, "present_ico_non.png")
icon = QImage(icon_file)
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
painter.drawImage(QRect(x_pos, y_pos, icon.width(), icon.height()), icon, icon.rect())
painter.end()
return out
def get_present_menu(present_id):
x_pos = 35
y_pos = 80
return get_present(present_id, x_pos, y_pos)
def get_present_ingame(present_id):
x_pos = 144
y_pos = 59
return get_present(present_id, x_pos, y_pos)
##############################################################################
### @fn get_event_icon(file_id)
### @desc Returns the specified event icon.
##############################################################################
def get_event_icon(file_id):
icon_file = os.path.join(GALLERY_DIR, "gallery_thumbnail_e_%03d.png" % file_id)
if not os.path.isfile(icon_file):
icon_file = os.path.join(GALLERY_DIR, "gallery_ico_e_none.png")
icon = QImage(icon_file)
x_pos = 278
y_pos = 66
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
painter.drawImage(QRect(x_pos, y_pos, icon.width(), icon.height()), icon, icon.rect())
painter.end()
return out
##############################################################################
### @fn get_movie_icon(file_id)
### @desc Returns the specified movie icon.
##############################################################################
def get_movie_icon(file_id):
icon_file = os.path.join(GALLERY_DIR, "gallery_thumbnail_m_%03d.png" % file_id)
if not os.path.isfile(icon_file):
icon_file = os.path.join(GALLERY_DIR, "gallery_ico_m_none.png")
icon = QImage(icon_file)
x_pos = 310
y_pos = 66
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
painter.drawImage(QRect(x_pos, y_pos, icon.width(), icon.height()), icon, icon.rect())
painter.end()
return out
##############################################################################
### @fn get_artwork_icon(file_id)
### @desc Returns the specified movie icon.
##############################################################################
def get_artwork_icon(file_id):
icon_file = os.path.join(GALLERY_DIR, "gallery_thumbnail_a_%03d.png" % file_id)
if not os.path.isfile(icon_file):
icon_file = os.path.join(GALLERY_DIR, "gallery_ico_a_none.png")
icon = QImage(icon_file)
x_pos = 326
y_pos = 66
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
painter.drawImage(QRect(x_pos, y_pos, icon.width(), icon.height()), icon, icon.rect())
painter.end()
return out
##############################################################################
### @fn get_box(scene_info)
### @desc Returns the text box specified by the given scene info.
##############################################################################
def get_box(scene_info):
mode = scene_info.mode
box_color = scene_info.box_color
box_type = scene_info.box_type
speaking = scene_info.speaking
speaker_id = scene_info.speaker
headshot = scene_info.headshot
chapter = scene_info.chapter
if box_color != common.BOX_COLORS.yellow and box_color != common.BOX_COLORS.green and box_color != common.BOX_COLORS.blue:
box_color = common.BOX_COLORS.yellow
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
painter.setRenderHint(QPainter.Antialiasing, True)
# Some commonality between the boxes.
box = QImage()
button = QImage()
nametag_x = 0
nametag_y = 0
nametag_color = QColor(255, 255, 255, 255)
nametag_vert = False
if box_type == common.BOX_TYPES.flat:
box = QImage(os.path.join(TEXTBOX_DIR, "box_gray.png"))
button = QImage(os.path.join(TEXTBOX_DIR, "button_%s.png" % box_color))
nametag_x = 10
nametag_y = 176
nametag_color = QColor(255, 255, 255, 255)
nametag_vert = False
elif box_type == common.BOX_TYPES.novel:
box = QImage(os.path.join(TEXTBOX_DIR, "box_novel.png"))
elif box_type == common.BOX_TYPES.normal:
if mode == common.SCENE_MODES.normal:
box = QImage(os.path.join(TEXTBOX_DIR, "box.png"))
button = QImage(os.path.join(TEXTBOX_DIR, "button_%s.png" % box_color))
nametag_x = 0
nametag_y = 220
nametag_color = QColor(50, 50, 50, 255)
nametag_vert = True
if not box.format() is QImage.Format_ARGB32_Premultiplied:
box = box.convertToFormat(QImage.Format_ARGB32_Premultiplied)
box_painter = QPainter(box)
box_painter.setRenderHint(QPainter.Antialiasing, True)
if speaker_id == 0: # Main character gets a special text box.
namebox = QImage(os.path.join(TEXTBOX_DIR, "name_%s_mc.png" % box_color))
else:
namebox = QImage(os.path.join(TEXTBOX_DIR, "name_%s.png" % box_color))
box_painter.drawImage(box.rect(), namebox, namebox.rect())
box_painter.end()
elif mode == common.SCENE_MODES.trial:
box_base = QImage(os.path.join(TRIAL_DIR, "trial_box.png"))
banner = QImage(os.path.join(TRIAL_DIR, "trial_banner.png"))
if speaker_id == 0: # Main character gets a special text box.
namebox = QImage(os.path.join(TRIAL_DIR, "trial_name_mc.png"))
else:
namebox = QImage(os.path.join(TRIAL_DIR, "trial_name.png"))
if not headshot == None:
case_num = QImage(os.path.join(TRIAL_DIR, "case_%02d.png" % chapter))
headshot = QImage(os.path.join(TRIAL_DIR, "headshot", "%02d.png" % headshot))
underlay = QImage(os.path.join(TRIAL_DIR, "trial_headshot.png"))
else:
case_num = QImage()
underlay = QImage()
headshot = QImage()
button = QImage(os.path.join(TRIAL_DIR, "button.png"))
nametag_x = 12
nametag_y = 168
nametag_color = QColor(255, 255, 255, 255)
nametag_vert = False
box = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
box.fill(QColor(0, 0, 0, 0).rgba())
box_painter = QPainter(box)
box_painter.setRenderHint(QPainter.Antialiasing, True)
box_painter.drawImage(box.rect(), banner, banner.rect())
box_painter.drawImage(box.rect(), namebox, namebox.rect())
box_painter.drawImage(box.rect(), box_base, box_base.rect())
box_painter.drawImage(box.rect(), underlay, underlay.rect())
box_painter.drawImage(box.rect(), headshot, headshot.rect())
box_painter.drawImage(box.rect(), case_num, case_num.rect())
box_painter.end()
else:
box = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
box.fill(QColor(0, 0, 0, 0).rgba())
painter.drawImage(out.rect(), box, box.rect())
painter.drawImage(out.rect(), button, button.rect())
if not speaker_id == None:
nametag = get_nametag(speaker_id, nametag_x, nametag_y, nametag_color, nametag_vert)
painter.drawImage(out.rect(), nametag, nametag.rect())
painter.end()
return out
##############################################################################
### @fn get_normal(scene_info, show_bg = True, show_sprite = True, show_box = True)
### @desc Returns an image containing the scene in normal mode.
##############################################################################
def get_normal(scene_info, show_bg = True, show_sprite = True, show_box = True):
sprite_id = scene_info.sprite
room_id = scene_info.room
scene_id = scene_info.scene
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(out)
painter.setRenderHint(QPainter.Antialiasing, True)
if show_bg:
if scene_info.movie >= 0:
bg = get_movie(scene_info.movie)
elif scene_info.flash >= 0:
bg = get_flash(scene_info.flash)
elif scene_info.bgd >= 0:
bg = get_bgd(scene_info.bgd)
else:
bg = get_bg(room_id)
if bg:
painter.drawImage(out.rect(), bg, bg.rect())
if show_sprite:
sprite = get_sprite(sprite_id)
if sprite:
painter.drawImage(out.rect(), sprite, sprite.rect())
if not scene_info.img_filter == IMG_FILTERS.unfiltered:
painter.end()
out = filter_image(out, scene_info.img_filter)
painter = QPainter(out)
painter.setRenderHint(QPainter.Antialiasing, True)
if show_box:
box = get_box(scene_info)
painter.drawImage(out.rect(), box, box.rect())
painter.end()
return out
##############################################################################
### @fn get_trial(scene_info, show_bg = True, show_sprite = True, show_box = True)
### @desc Returns an image containing the scene in trial mode.
##############################################################################
def get_trial(scene_info, show_bg = True, show_sprite = True, show_box = True):
case_num = scene_info.chapter
sprite_id = scene_info.sprite
if case_num > 6 or case_num <= 0:
case_num = 1
out = None
if show_bg:
if scene_info.movie >= 0:
out = get_movie(scene_info.movie)
elif scene_info.flash >= 0:
out = get_flash(scene_info.flash)
elif scene_info.bgd >= 0:
out = get_bgd(scene_info.bgd)
else:
# out = QImage(os.path.join(BG_DIR, "bg_%03d.png" % (199 + case_num)))
out = get_bg(199 + case_num)
else:
out = QImage(IMG_W, IMG_H, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())
if not out.format() is QImage.Format_ARGB32_Premultiplied:
out = out.convertToFormat(QImage.Format_ARGB32_Premultiplied)
painter = QPainter(out)
painter.setRenderHint(QPainter.Antialiasing, True)
if show_sprite:
sprite_id.sprite_type = SPRITE_TYPE.stand
sprite = get_sprite(sprite_id)
if sprite:
painter.drawImage(out.rect(), sprite, sprite.rect())
if not scene_info.img_filter == IMG_FILTERS.unfiltered:
painter.end()
out = filter_image(out, scene_info.img_filter)
painter = QPainter(out)
painter.setRenderHint(QPainter.Antialiasing, True)
if show_box:
box = get_box(scene_info)
painter.drawImage(out.rect(), box, box.rect())
return out
##############################################################################
### @fn get_letter(clt, char)
### @desc Returns an image containing the letter rendered with the given CLT.
##############################################################################
def get_letter(clt, char):
if not clt in CLT_STYLES:
clt = 0
font = CLT_STYLES[clt].font
hscale = CLT_STYLES[clt].scale / 100.0
vscale = CLT_STYLES[clt].scale / 100.0
try:
info = FONT_DATA[font][char]
except:
# This is the character the game replaces unknown characters with.
info = FONT_DATA[font][u'\u2261']
expand_l = 0
expand_r = 0
expand_t = 0
expand_b = 0
box = QRect(info['x'] - expand_l, info['y'] - expand_t, info['w'] + expand_l + expand_r, info['h'] + expand_t + expand_b)
#box = QRect(info['x'] - expand_l, info['y'] - expand_t, info['w'], info['h'])
expand_l += CLT_STYLES[clt].border_size
expand_r += CLT_STYLES[clt].border_size
expand_t += CLT_STYLES[clt].border_size
expand_b += CLT_STYLES[clt].border_size
xshift = -expand_l
yshift = +expand_t
if font == 1:
yshift += 0
elif font == 2:
yshift -= 2
final_w = info['w']
final_h = info['h']
if hscale != 1.0:
final_w = (final_w * hscale)
if vscale != 1.0:
old_h = final_h
final_h = (final_h * vscale)
yshift = yshift + ((old_h - final_h) / 2.0)
final_w += expand_l + expand_r
final_h += expand_t + expand_b
letter = FONTS[CLT_STYLES[clt].font].copy(box)
if hscale != 1.0 or vscale != 1.0:
matrix = QTransform()
matrix.scale(hscale, vscale)
letter = letter.transformed(matrix, Qt.Qt.SmoothTransformation)
top_color = CLT_STYLES[clt].top_color
bottom_color = CLT_STYLES[clt].bottom_color
if top_color and not bottom_color:
letter = replace_all_colors(letter, top_color)
elif top_color and bottom_color:
letter = add_v_gradient(letter, [top_color, bottom_color])
border_size = CLT_STYLES[clt].border_size
border_color = CLT_STYLES[clt].border_color
if border_size:
letter = add_border(letter, border_color, border_size)
return letter, (xshift, yshift, final_w, final_h)
##############################################################################
### @fn mangle_line(line, lengths, scene_mode, cur_font)
### @desc Hack the line up a bit to reflect how it'll show up in-game
### based on some in-game quirks.
##############################################################################
def mangle_line(line, scene_mode):
# If it doesn't have a category, assume it's safe.
if scene_mode == common.SCENE_MODES.other:
return line
# The game auto-wraps after 54 characters.
# Not that we should ever run into an issue with that.
# max_len = 54
max_len = 96
# Replace extra characters with something ugly
# so it's easy to know it needs to be fixed.
too_long = u'\u2261'
# The max length is even tighter
if scene_mode == common.SCENE_MODES.ammo or scene_mode == common.SCENE_MODES.present:
max_len = 61
elif scene_mode == common.SCENE_SPECIAL.option:
max_len = 54
elif scene_mode == common.SCENE_MODES.help:
max_len = 87
extra_chars = len(line) - max_len
if extra_chars > 0:
line = line[:max_len] + (too_long * extra_chars)
return line
##############################################################################
### @fn process_text(text, scene_mode, format, mangle)
### @desc Converts the given text into three lists: lines, lengths, and CLT changes.
##############################################################################
def process_text(text, scene_mode, format, mangle = True):
# Replace our unmarked CLTs with whatever default CLT we're given.
# Also start the line off with the default CLT so we're definitely using it.
# Useful for modes like Nonstop Debate, where text is normally CLT 16.
text = "<CLT>" + text
text = re.sub("<CLT>", "<CLT %d>" % format.clt, text)
lines = []
lengths = []
clt_changes = []
last_clt = format.clt
for line in text.split("\n"):
# Start the line off with the last-used CLT, so the parsers know what it is.
line = ("<CLT %d>" % last_clt) + line
line, length, clt = font_parser.get_len(line, format.clt)
# Hack the line up a bit to reflect how it'll show up in-game
# based on some in-game quirks.
if mangle:
line = mangle_line(line, scene_mode)
# If there isn't an initial CLT, start the line off with
# the CLT still in use at the end of the previous line.
if not 0 in clt.keys():
clt[0] = last_clt
last_clt = clt[max(clt.keys())]
# If we're supposed to skip blanks and this line is blank
# after parsing the formatting, then don't add it to the list.
if format.kill_blanks and line.strip() == "":
continue
lines.append(line)
lengths.append(length)
clt_changes.append(clt)
return lines, lengths, clt_changes
##############################################################################
### @fn print_text(image, text, scene_mode = common.SCENE_MODES.normal)
### @desc Prints the given text onto the given image.
##############################################################################
def print_text(image, text, scene_mode = common.SCENE_MODES.normal, format = TextFormat(), mangle = True):
img_w = IMG_W
img_h = IMG_H
if image:
img_w = image.width()
img_h = image.height()
out = QImage(img_w, img_h, QImage.Format_ARGB32_Premultiplied)
out.fill(QColor(0, 0, 0, 0).rgba())