<commit_before># Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utility methods."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import imghdr
import math
import os
import shutil

import requests

import log

logger = log.getLogger()


def convert_configs_to_format(configs, products_data, storage, cloud_storage):
    image_overlays = []
    text_overlays = []

    for config in configs:

        product_data = products_data[config['type']].get(str(config['key']))

        # Skip to next config if it doesnt exist
        if (not product_data) or (not product_data.get(config['field'])):
            continue

        field_value = product_data.get(config['field'])

        # Handle image to be added to video (http* or gs://*)
        if field_value.lower().startswith('http') or field_value.lower().startswith('gs://'):
            image_overlays.append(convert_image_overlay(config, field_value, storage, cloud_storage))
        # Then it must be a text column
        else:
            text_overlays.extend(convert_text_overlay(config, field_value, storage))

    return image_overlays, text_overlays


def convert_text_overlay(config, field_value, storage):

    width = int(config.get('width') or 0)
    font_size = float(config['size'])

    # Wrap long texts to many smaller texts
    words = _wrap_text(field_value, width)

    logger.debug(config)

    # Check if need to scale font to fit the space
    if config.get('keep_ratio', False):

        actual_lines = len(words)
        desired_lines = int(config.get('height', 1))

        if actual_lines > desired_lines:

            # Shrink to less lines
            font_size, width = _fit_font_size_to_width_height(font_size, width, desired_lines, len(field_value))

            # Wrap content again according to new width
            words = _wrap_text(field_value, width)

    # Create overlays to all lines broken down
    texts = []

    for i, word in enumerate(words):
        texts.append({
            'start_time': float(config['start_time']),
            'end_time': float(config['end_time']),
            'align': config.get('align', 'center'),
            'x': float(config['x']),
            'y': float(config['y']) + (1.2 * i * font_size),
            'angle': int(config.get('angle', '0')),
            'text': word,
            'font': storage.get_absolute_path(config['font']),
            'font_color': config['color'],
            'font_size': font_size
        })

    return texts


def _fit_font_size_to_width_height(font_size, width, desired_lines, text_length):

    actual_size = float(text_length)
    desired_size = float(width * desired_lines)
<commit_msg>Removing old feature to resize text<commit_after># Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utility methods."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import imghdr
import math
import os
import shutil

import requests

import log

logger = log.getLogger()


def convert_configs_to_format(configs, products_data, storage, cloud_storage):
    image_overlays = []
    text_overlays = []

    for config in configs:

        product_data = products_data[config['type']].get(str(config['key']))

        # Skip to next config if it doesnt exist
        if (not product_data) or (not product_data.get(config['field'])):
            continue

        field_value = product_data.get(config['field'])

        # Handle image to be added to video (http* or gs://*)
        if field_value.lower().startswith('http') or field_value.lower().startswith('gs://'):
            image_overlays.append(convert_image_overlay(config, field_value, storage, cloud_storage))
        # Then it must be a text column
        else:
            text_overlays.extend(convert_text_overlay(config, field_value, storage))

    return image_overlays, text_overlays


def convert_text_overlay(config, field_value, storage):

    width = int(config.get('width') or 0)
    font_size = float(config['size'])

    # Wrap long texts to many smaller texts
    words = _wrap_text(field_value, width)

    # Create overlays to all lines broken down
    texts = []

    for i, word in enumerate(words):
        texts.append({
            'start_time': float(config['start_time']),
            'end_time': float(config['end_time']),
            'align': config.get('align', 'center'),
            'x': float(config['x']),
            'y': float(config['y']) + (1.2 * i * font_size),
            'angle': int(config.get('angle', '0')),
            'text': word,
            'font': storage.get_absolute_path(config['font']),
            'font_color': config['color'],
            'font_size': font_size
        })

    return texts


def _fit_font_size_to_width_height(font_size, width, desired_lines, text_length):

    actual_size = float(text_length)
    desired_size = float(width * desired_lines)

{'fraction_non_alphanumeric': 0.1052105876477443, 'fraction_numerical': 0.00416181122024305, 'mean_word_length': 3.264017033356991, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5545197', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1706, 'n_words': 535}