Example #1
0
 def add_dialog(self,
                layer=0,
                start=0,
                end=5000,
                style=None,
                actor="",
                effect="",
                text="",
                tag="",
                comment=False):
     if not isinstance(start, Time):
         start = Time(start)
     if not isinstance(end, Time):
         end = Time(end)
     if not style:
         style = self.get_style("Default").as_dict()
     else:
         style = style.as_dict()
     dialog_item = {
         "layer": layer,
         "start": start.strtime,
         "end": end.strtime,
         "style": style,
         "actor": actor,
         "effect": effect,
         "text": text,
         "comment": comment
     }
     d = Dialog(dialog_item, self.resolution)
     d.tag = tag
     self.add(d)
Example #2
0
 def __init__(self, dialog, resolution):
     self.resolution = resolution
     self.layer = dialog["layer"]
     self.start = Time.from_strtime(dialog["start"])
     self.end = Time.from_strtime(dialog["end"])
     self.style = Style(dialog["style"]["name"], dialog["style"])
     self.actor = dialog["actor"]
     self.effect = dialog["effect"]
     self.text = dialog["text"]
     self.comment = dialog["comment"]
     self.tag = ""
Example #3
0
 def __init__(self, dialog, resolution):
     self.resolution = resolution
     self.layer = dialog["layer"]
     self.start = Time.from_strtime(dialog["start"])
     self.end = Time.from_strtime(dialog["end"])
     self.style = Style(dialog["style"]["name"], dialog["style"])
     self.actor = dialog["actor"]
     self.effect = dialog["end"]
     self.text = dialog["text"]
     self.comment = dialog["comment"]
     self.tag = ""
Example #4
0
    def _syls(self):
        """Syllables(karaoke) of the current line"""
        syllabes = []
        line_start = self.start
        line_end = self.end
        sleft = self.left

        re_syls = re.findall(RE_KARA, self._rawtext)
        syl_n = len(re_syls)

        # fix prespace
        #    input: {\k30}ka{\k16} shi
        #    output: {\k30}ka {\k16}shi
        re_syls = [list(syl) for syl in re_syls]
        for i, syl in enumerate(re_syls):
            match = re.match("(\s*)(\w+)(\s*)", list(syl)[-1])
            if match:
                prespace, _, postspace = match.groups()
                if prespace:
                    if i > 0:
                        re_syls[i - 1][-1] = re_syls[i - 1][-1] + prespace
                        re_syls[i][-1] = _ + postspace

        for i, match in enumerate(re_syls):
            duration, style, actor, effect, text = match

            # The time in duration is in centisecond
            duration = Time.from_cs(int(duration))
            style = self.get_style(style) if style else self.style
            actor = actor if actor else self.actor
            effect = effect if effect else self.effect

            width = Text(style, text).width + self.style._fix_width

            # Absolute times
            start = line_start
            line_start += duration
            if i == syl_n - 1:
                # Ensure that the end time and the width of the last syl
                # is the same that the end time and width of the line
                end = line_end
            else:
                end = line_start

            syl_item = {
                "layer": self.layer,
                "start": start.strtime, "end": end.strtime,
                "style": style.as_dict(), "actor": actor,
                "effect": effect, "text": text.strip(), "comment": False}

            syllabes.append(Syl(syl_item, self.resolution, sleft))
            sleft += width

        return syllabes
Example #5
0
    def _syls(self):
        """Syllables(karaoke) of the current line"""
        syllabes = []
        line_start = self.start
        line_end = self.end
        sleft = self.left

        re_syls = re.findall(RE_KARA, re.sub(RE_TAGS2, "", self._rawtext))
        syl_n = len(re_syls)

        spacewidth = Text(self.style, " ").width

        for i, match in enumerate(re_syls):
            duration, inline, text = match

            # The time in duration is in centisecond
            duration = Time.from_cs(int(duration))
            style = self.style
            actor = self.actor
            effect = self.effect

            prespace = len(text) - len(text.lstrip())
            postspace = len(text) - len(text.rstrip())

            sleft += prespace * spacewidth
            width = Text(style, text.strip()).width + style._fix_width

            # Absolute times
            start = line_start
            line_start += duration
            if i == syl_n - 1:
                # Ensure that the end time and the width of the last syl
                # is the same that the end time and width of the line
                end = line_end
            else:
                end = line_start

            syl_item = {
                "layer": self.layer,
                "start": start.strtime,
                "end": end.strtime,
                "style": style.as_dict(),
                "actor": actor,
                "inline": inline,
                "effect": effect,
                "text": text.strip(),
                "comment": False
            }

            syllabes.append(Syl(syl_item, self.resolution, sleft))
            sleft += width + postspace * spacewidth

        return syllabes
Example #6
0
    def _chars(self):
        charas = []
        for i, s in enumerate(self.syls):

            cleft = s.left
            line_start = s.start
            line_end = s.end

            char_n = len(s.text)

            # For syls of one char
            if char_n == 1 or char_n == 0:
                duration = s.dur
            else:
                duration = Time(int(round(s.dur.ms / char_n, 0)))

            for ci, char in enumerate(s.text):

                start = line_start
                line_start += duration

                width = Text(s.style, char).width + self.style._fix_width

                if ci == char_n - 1:
                    # Ensure that the end time and the width of the last char
                    # is the same that the end time and width of the syl
                    end = line_end
                else:
                    end = line_start

                char_item = {
                    "layer": s.layer,
                    "start": start.strtime,
                    "end": end.strtime,
                    "style": s.style.as_dict(),
                    "actor": s.actor,
                    "inline": s.inline,
                    "effect": s.effect,
                    "text": char.strip(),
                    "comment": False,
                    "sylstart": s.start,
                    "sylend": s.end,
                }

                charas.append(Char(char_item, self.resolution, cleft))
                cleft += width

        return charas