﻿# -*- coding: utf-8 -*-

########################################## Declaration ######################################################

# Declaration:
# ASS style wrapper class
# for easy access to multiple styles inherited from the k-timed ASS file.
# usage:
#       styles = AssStyles(GetVal(val_AssHeader))
#       style = styles['style_name']
#
# Copyright (c) 2011 milkyjing (milkyjing@gmail.com). All rights reserved.
# Please visit tcax.rhacg.com to get the latest information
# Version: 0.1

######################################### Modules ###########################################################


# receives the header returned by GetVal(val_AssHeader) function
# returns a list of ASS style strings
def GetAssStyleStrings(header):
    pos = header.find('Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding')
    h = header[pos:]
    lines = h.splitlines()
    ss_li = []
    for l in lines:
        if l.startswith('Style:'):
            ss_li.append(l)
    return ss_li

# receives an ASS style string in the list returned by GetAssStyleStrings function
# returns a list of ASS style value strings
def ExtractAssStyleString(s):
    pos = s.find(':') + 1
    s = s[pos:]
    li = s.split(',')
    return li

# the constructor receives an ASS style string in the list returned by GetAssStyleStrings function
class AssStyle:
    def __init__(self, s):
        li = ExtractAssStyleString(s)
        ac1 = li[3].lstrip('&H')
        ac2 = li[4].lstrip('&H')
        ac3 = li[5].lstrip('&H')
        ac4 = li[6].lstrip('&H')
        self.name = li[0].strip()
        self.fontName = li[1].strip()
        self.fs = int(li[2].strip())
        self.a1 = int(ac1[0:2], 16)
        self.c1 = ac1[2:8]
        self.a2 = int(ac2[0:2], 16)
        self.c2 = ac2[2:8]
        self.a3 = int(ac3[0:2], 16)
        self.c3 = ac3[2:8]
        self.a4 = int(ac4[0:2], 16)
        self.c4 = ac4[2:8]
        self.scaleX = float(li[11].strip())
        self.scaleY = float(li[12].strip())
        self.spacing = int(li[13].strip())
        self.angle = float(li[14].strip())
        self.bord = float(li[16].strip())
        self.shad = float(li[17].strip())
        self.alignment = int(li[18].strip())
        self.marginL = int(li[19].strip())
        self.marginR = int(li[20].strip())
        self.marginV = int(li[21].strip())

# the constructor receives the header returned by GetVal(val_AssHeader) function
class AssStyles:
    def __init__(self, header):
        li = GetAssStyleStrings(header)
        self.styles = dict()
        for s in li:
            style = AssStyle(s)
            self.styles[style.name] = style
    def __getitem__(self, index):
        return self.styles[index]



