FONT_DATA = { 1: {}, 2: {} } def get_char_name(char_id, data_dir = common.editor_config.data01_dir):
  dir = os.path.join(data_dir, "jp", "script", "32_CharaName.pak")
  return get_label(char_id, dir) def get_ammo_name(ammo_id, data_dir = common.editor_config.data01_dir):
  dir = os.path.join(data_dir, "jp", "script", "06_KotodamaName.pak")
  return get_label(ammo_id, dir) def get_present_name(present_id, data_dir = common.editor_config.data01_dir):
  dir = os.path.join(data_dir, "jp", "script", "04_ItemName.pak")
  return get_label(present_id, dir) def load_text(filename):
  if not os.path.isfile(filename):
    raise ValueError("%s is not a file." % filename)
  
  file = codecs.open(filename, mode = 'r', encoding = 'utf-16')
  text = file.read()
  file.close()
  return text class TextFormat:
  def __init__(self, x = 0, y = 0, w = 480, h = 24, align = TEXT_ALIGN.left,
               orient = TEXT_ORIENT.hor, clt = 0, kill_blanks = False):
    
    self.x            = x
    self.y            = y
    self.w            = w
    self.h            = h
    self.align        = align
    self.orient       = orient
    self.clt          = clt
    self.kill_blanks  = kill_blanks TEXT_ALIGN   = Enum("left", "right", "center", "offcenter") TEXT_ORIENT  = Enum("hor", "ver") TEXT_FORMATS = {
  None:                           TextFormat(),
  common.SCENE_MODES.normal:      TextFormat(x =  46, y = 216, w = 444, h = 24, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt =  0, kill_blanks = True),
  common.SCENE_MODES.normal_flat: TextFormat(x =  18, y = 208, w = 444, h = 27, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt =  0, kill_blanks = True),
  common.SCENE_MODES.trial:       TextFormat(x =  18, y = 207, w = 444, h = 27, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt =  0, kill_blanks = True),
  common.SCENE_MODES.rules:       TextFormat(x =  32, y = 165, w = 416, h = 18, align = TEXT_ALIGN.center,  orient = TEXT_ORIENT.hor, clt =  0, kill_blanks = True),
  common.SCENE_MODES.ammo:        TextFormat(x =  50, y =  88, w = 185, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = False),
  common.SCENE_MODES.ammoname:    TextFormat(x =  36, y =  50, w = 200, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = True),
  common.SCENE_MODES.ammosummary: TextFormat(x =  41, y = 198, w = 200, h = 12, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = False),
  common.SCENE_MODES.present:     TextFormat(x =  50, y =  88, w = 185, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = False),
  common.SCENE_MODES.presentname: TextFormat(x =  66, y =  50, w = 200, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = True),
  common.SCENE_MODES.debate:      TextFormat(x =   0, y = 160, w = 480, h = 24, align = TEXT_ALIGN.center,  orient = TEXT_ORIENT.hor, clt = 16, kill_blanks = True),
  common.SCENE_MODES.mtb:         TextFormat(x =  18, y = 160, w = 444, h = 24, align = TEXT_ALIGN.center,  orient = TEXT_ORIENT.hor, clt =  2, kill_blanks = True),
  common.SCENE_MODES.climax:      TextFormat(x =  18, y = 208, w = 444, h = 27, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt =  0, kill_blanks = True),
  common.SCENE_MODES.anagram:     TextFormat(x =  18, y = 202, w = 420, h = 24, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 16, kill_blanks = True),
  common.SCENE_MODES.menu:        TextFormat(x =  55, y = 204, w = 370, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = True),
  common.SCENE_MODES.map:         TextFormat(x =  38, y =  65, w = 200, h = 14, align = TEXT_ALIGN.center,  orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = True),
  common.SCENE_MODES.report:      TextFormat(x = 170, y =  90, w = 292, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = True),
  common.SCENE_MODES.report2:     TextFormat(x = 182, y = 187, w = 292, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = True),
  common.SCENE_MODES.skill:       TextFormat(x =  18, y = 106, w = 292, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = False),
  common.SCENE_MODES.skill2:      TextFormat(x = 248, y = 154, w = 292, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = True),
  common.SCENE_MODES.music:       TextFormat(x =  95, y = 112, w = 180, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = True),
  common.SCENE_MODES.eventname:   TextFormat(x =  29, y =  59, w = 196, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 89, kill_blanks = True),
  common.SCENE_MODES.artworkname: TextFormat(x =  29, y =  59, w = 196, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 89, kill_blanks = True),
  common.SCENE_MODES.moviename:   TextFormat(x =  29, y =  59, w = 196, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 89, kill_blanks = True),
  common.SCENE_MODES.theatre:     TextFormat(x =  18, y = 208, w = 444, h = 27, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt =  0, kill_blanks = True),
  common.SCENE_MODES.help:        TextFormat(x =  34, y =  24, w = 420, h = 20, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = False),
  common.SCENE_MODES.other:       TextFormat(x =  18, y =  24, w = 420, h = 14, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt = 90, kill_blanks = False),
  common.SCENE_MODES.novel:       TextFormat(x =  28, y =  25, w = 420, h = 20, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt =  0, kill_blanks = False),
  common.SCENE_SPECIAL.option:    TextFormat(x = 200, y = 143, w = 254, h = 25, align = TEXT_ALIGN.left,    orient = TEXT_ORIENT.hor, clt =  0, kill_blanks = True),
} def get_sprite_file(sprite_id):
  
  if sprite_id.char_id == -1 or sprite_id.sprite_id == -1:
    return None
  
  filename = "%s_%02d_%02d.png" % (str(sprite_id.sprite_type), sprite_id.char_id, sprite_id.sprite_id)
  return filename SPRITE_TYPE = Enum("bustup", "stand")
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
from clt import CLT_STYLES
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
import ctypes
import os.path
import re
import sys
import time
import common
import font_parser
  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)