# Phatch - Photo Batch Processor
# Copyright (C) 2007-2008 www.stani.be
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/
#
# Phatch recommends SPE (http://pythonide.stani.be) for editing python.

# Embedded icon is taken from www.openclipart.org (public domain)

# Follows PEP8

from core import models
from lib.reverse_translation import _t


def init():
    global Image, ImageDraw, ImageFont
    import Image
    import ImageDraw
    import ImageFont
    global calculate_location, convert_safe_mode
    from lib.imtools import calculate_location, convert_safe_mode


def draw_text(image, text, horizontal_offset, vertical_offset,
              horizontal_justification, vertical_justification, size,
              color='#FFFFFF', orientation=None, font=None):
    """Draws text on an image."""
    image = convert_safe_mode(image)
    if orientation:
        orientation = getattr(Image, orientation)

    draw = ImageDraw.Draw(image)
    if font.strip():
        font = ImageFont.truetype(font, size)
    else:
        font = ImageFont.load_default()
        text = text.encode('ascii', 'replace')

    if orientation:
        font = ImageFont.TransposedFont(font, orientation)

    location = calculate_location(
        horizontal_offset, vertical_offset,
        horizontal_justification, vertical_justification,
        image.size, draw.textsize(text, font=font))

    # draw
    draw.text(location, text, font=font, fill=color)

    # composite the watermark with the layer
    return image


class Action(models.OffsetMixin, models.Action):
    """Draws text on an image."""

    label = _t('Text')
    author = 'Stani'
    email = 'spe.stani.be@gmail.com'
    init = staticmethod(init)
    pil = staticmethod(draw_text)
    version = '0.1'
    tags = [_t('default'), _t('filter')]
    __doc__ = _t('Write text at a given position')

    def interface(self, fields):
        fields[_t('Text')] = self.CharField(
            choices=self.STAMPS)
        fields[_t('Font')] = self.FontFileField('Free Sans')
        fields[_t('Size')] = self.PixelField('5%',
                                            choices=self.SMALL_PIXELS)
        fields[_t('Color')] = self.ColorField('#000000')

        super(Action, self).interface(fields)

    def get_relevant_field_labels(self):
        return ['Text', 'Font', 'Size', 'Color'] + \
            super(Action, self).get_relevant_field_labels()

    def values(self, info, pixel_fields={}):
        x, y = info['size']
        pixel_fields.update({'Size': (x + y) / 2})
        return super(Action, self).values(info,
            pixel_fields=pixel_fields)

    icon = \
'x\xda\x01\xcc\x043\xfb\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\
\x00\x000\x08\x06\x00\x00\x00W\x02\xf9\x87\x00\x00\x00\x04sBIT\x08\x08\x08\
\x08|\x08d\x88\x00\x00\x04\x83IDATh\x81\xed\x97]h\x1cU\x14\xc7\x7f\xe7n>6\
\xa8\xa9X)\x08m\xc14Tm\xd463ij\x1b\xdb\x8e\xf5\xc9"\xf8b\x90J)\xd8\xfa\xd5\
\x87>(-\n\x15\xa1"E\xacTQ\x0c(%\xda\xfa\xf1\xa2\xf8\x81H^\x14Vl\x1b\xeb\xee,\
!\xb0\xd5j\x97\xd2*V\xec\x07a\xa3\xe9f\xe3\xcc\xf1!\xbbq2\x99]6\xb0\n\x85\
\xf9=\xcd=s\xce\xff\xfe/3\xf7\x9e\x19\x88\x89\x89\x89\x89\x89\x89\x89\x89\
\xb9j\x91F\t\xd9\xb6\xbd\x0c8\x0ct\xa8\xea`6\x9b}\xaeQ\xda\xb50\x8d\x12R\xd5\
\x83@\x1fp\x93\x88\xec\xb5,k}\xa3\xb4k\xd1\xb0\x05\x88\xc8\x1d\xb3\x84\x8dY\
\xd1(\xedZ4l\x01@"8P\xd5FjW\xe5\x7f\x99\xe4\xbf\xe4\xaa_@ST\xb0\xb3\xb3\xb3u\
\xc1\x82\x05\x8f\x00\x1b\x80\xa5"rYU\x87\x80C\x1d\x1d\x1d~>\x9f\xdf""\x8b\
\x8c1\x1f\xa6\xd3\xe9\xdf\xab\x89\x977\xf2c"r\xb3\xaa\x8e\x8bHND\x0ee2\x99S\
\xb5L\xd9\xb6}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