<commit_before>import typing as T
from copy import copy

from bubblesub.api import Api
from bubblesub.api.log import LogLevel
from bubblesub.ass.event import AssEvent, AssEventList
from bubblesub.ass.meta import AssMeta
from bubblesub.ass.util import ass_to_plaintext
from bubblesub.ass_renderer import AssRenderer

WIDTH_MULTIPLIERS = {1: 0.7, 2: 0.9}


class BaseResult:
    def __init__(
        self, event: T.Union[AssEvent, T.List[AssEvent]], text: str
    ) -> None:
        if isinstance(event, list):
            self.event = event[0]
            self.additional_events = event[1:]
        else:
            self.event = event
            self.additional_events = []
        self.text = text

    @property
    def events(self) -> T.Iterable[AssEvent]:
        yield self.event
        yield from self.additional_events

    def __repr__(self) -> str:
        ids = "+".join([f'#{event.number or "?"}' for event in self.events])
        return f"{ids}: {self.text}"


class DebugInformation(BaseResult):
    log_level = LogLevel.Debug


class Information(BaseResult):
    log_level = LogLevel.Info


class Violation(BaseResult):
    log_level = LogLevel.Warning


def measure_frame_size(
    api: Api, renderer: AssRenderer, event: AssEvent
) -> T.Tuple[int, int]:
    fake_event_list = AssEventList()
    fake_event_list.append(copy(event))

    renderer.set_source(
        style_list=renderer.style_list,
        event_list=fake_event_list,
        meta=renderer.meta,
        video_resolution=renderer.video_resolution,
    )

    layers = list(renderer.render_raw(time=event.start))
    if not layers:
        return (0, 0)
    min_x = min(layer.dst_x for layer in layers)
    min_y = min(layer.dst_y for layer in layers)
    max_x = max(layer.dst_x + layer.w for layer in layers)
    max_y = max(layer.dst_y + layer.h for layer in layers)
    return (int((max_x - min_x) * api.video.aspect_ratio), max_y - min_y)


def get_optimal_line_heights(
    api: Api, renderer: AssRenderer
) -> T.Dict[str, float]:
    TEST_LINE_COUNT = 20
    VIDEO_RES_X = 100
    VIDEO_RES_Y = TEST_LINE_COUNT * 300

    fake_meta = AssMeta()
    renderer.set_source(<commit_msg>cfg/bubblesub/qc: don't measure blur and border layers<commit_after>import typing as T
from copy import copy

from bubblesub.api import Api
from bubblesub.api.log import LogLevel
from bubblesub.ass.event import AssEvent, AssEventList
from bubblesub.ass.meta import AssMeta
from bubblesub.ass.util import ass_to_plaintext
from bubblesub.ass_renderer import AssRenderer

WIDTH_MULTIPLIERS = {1: 0.7, 2: 0.9}


class BaseResult:
    def __init__(
        self, event: T.Union[AssEvent, T.List[AssEvent]], text: str
    ) -> None:
        if isinstance(event, list):
            self.event = event[0]
            self.additional_events = event[1:]
        else:
            self.event = event
            self.additional_events = []
        self.text = text

    @property
    def events(self) -> T.Iterable[AssEvent]:
        yield self.event
        yield from self.additional_events

    def __repr__(self) -> str:
        ids = "+".join([f'#{event.number or "?"}' for event in self.events])
        return f"{ids}: {self.text}"


class DebugInformation(BaseResult):
    log_level = LogLevel.Debug


class Information(BaseResult):
    log_level = LogLevel.Info


class Violation(BaseResult):
    log_level = LogLevel.Warning


def measure_frame_size(
    api: Api, renderer: AssRenderer, event: AssEvent
) -> T.Tuple[int, int]:
    fake_event_list = AssEventList()
    fake_event_list.append(copy(event))

    renderer.set_source(
        style_list=renderer.style_list,
        event_list=fake_event_list,
        meta=renderer.meta,
        video_resolution=renderer.video_resolution,
    )

    layers = [
        layer
        for layer in renderer.render_raw(time=event.start)
        if layer.type == 0
    ]
    if not layers:
        return (0, 0)
    min_x = min(layer.dst_x for layer in layers)
    min_y = min(layer.dst_y for layer in layers)
    max_x = max(layer.dst_x + layer.w for layer in layers)
    max_y = max(layer.dst_y + layer.h for layer in layers)
    return (int((max_x - min_x) * api.video.aspect_ratio), max_y - min_y)


def get_optimal_line_heights(
    api: Api, renderer: AssRenderer
) -> T.Dict[str, float]:
    TEST_LINE_COUNT = 20
    VIDEO_RES_X = 100
    VIDEO_RES_Y = TEST_LINE_COUNT * 300

    fake_meta = AssMeta()
    renderer.set_source(
{'fraction_non_alphanumeric': 0.10051720260850011, 'fraction_numerical': 0.008320215875871374, 'mean_word_length': 2.964349376114082, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3739294', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1450, 'n_words': 356}