File: smilies.py

package info (click to toggle)
sugar-chat-activity 85-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,560 kB
  • sloc: python: 1,488; makefile: 10; sh: 6
file content (238 lines) | stat: -rw-r--r-- 10,125 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# -*- coding: utf-8 -*-
# Copyright 2010, Mukesh Gupta
# Copyright 2010, Aleksey Lim
# Copyright 2014, Walter Bender
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
from gettext import gettext as _
from gi.repository import GdkPixbuf
from sugar3.graphics import style
from sugar3.activity.activity import get_bundle_path

THEME = \
    [
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-) and :)
        ('smile', _('Smile'), [':-)', ':)']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are ;-) and ;)
        ('wink', _('Winking'), [';-)', ';)']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-/ and :/
        ('confused', _('Confused'), [':-/', ':/']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-( and :(
        ('sad', _('Sad'), [':-(', ':(']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-D and :D
        ('grin', _('Grin'), [':-D', ':D']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-| and :|
        ('neutral', _('Neutral'), (':-|', ':|')),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-O, :O, =-O and =O
        ('shock', _('Shock'), [':-O', ':O', '=-O', '=O']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are B-), B), 8-) and 8)
        ('cool', _('Cool'), ['B-)', 'B)', '8-)', '8)']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-P and :P
        ('tongue', _('Tongue'), [':-P', ':P']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :">
        ('blush', _('Blushing'), [':">']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :'-( and :'(
        ('weep', _('Weeping'), [":'-(", ":'("]),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are O-), O), O:-) and O:)
        ('angel', _('Angel'), ['O-)', 'O)', 'O:-)', 'O:)']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-$ and :-$
        ('shutup', _("Don't tell anyone"), (':-$', ':-$')),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are x-(, x(, X-( and x-(
        ('angry', _('Angry'), ('x-(', 'x(', 'X-(', 'x-(')),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are >:> and >:)
        ('devil', _('Devil'), ('>:>', '>:)')),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-B, :B
        ('nerd', _('Nerd'), (':-B', ':B')),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-*, :*
        ('kiss', _('Kiss'), (':-*', ':*')),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :))
        ('laugh', _('Laughing'), [':))']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are I-)
        ('sleep', _('Sleepy'), ['I-)']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are :-&
        ('sick', _('Sick'), [':-&']),
        # TRANS: A smiley (http://en.wikipedia.org/wiki/Smiley) explanation
        # TRANS: ASCII-art equivalents are /:)
        ('eyebrow', _('Raised eyebrows'), ['/:)']),
        ('unicode-1', _('Heart'), ['♥']),
        ('unicode-2', _('Airplane'), ['✈']),
        ('unicode-3', _('Music'), ['♬']),
        ('unicode-4', _('Check box'), ['☑']),
        ('unicode-5', _('Spade'), ['♠']),
        ('unicode-6', _('Telephone'), ['☎']),
        ('unicode-7', _('X box'), ['☒']),
        ('unicode-8', _('Cadusis'), ['☤']),
        ('unicode-9', _('Female'), ['♀']),
        ('unicode-10', _('Star'), ['✩']),
        ('unicode-11', _('Letter'), ['✉']),
        ('unicode-12', _('Poison'), ['☠']),
        ('unicode-13', _('Check mark'), ['✔']),
        ('unicode-14', _('Male'), ['♂']),
        ('unicode-15', _('Star'), ['★']),
        ('unicode-16', _('Shamrock'), ['☘']),
        ('unicode-17', _('Recycle'), ['♺']),
        ('unicode-18', _('X'), ['✖']),
        ('unicode-19', _('Fire'), ['♨']),
        ('unicode-20', _('Leaf'), ['❦']),
        ('unicode-21', _('Cloud'), ['☁']),
        ('unicode-22', _('V'), ['✌']),
        ('unicode-23', _('Crown'), ['♛']),
        ('unicode-24', _('Floret'), ['❁']),
        ('unicode-25', _('Star and crescent'), ['☪']),
        ('unicode-26', _('Umbrella'), ['☂']),
        ('unicode-27', _('Pencil'), ['✏']),
        ('unicode-28', _('Floret'), ['❀']),
        ('unicode-29', _('Snowman'), ['☃']),
        ('unicode-30', _('Point right'), ['☛']),
        ('unicode-31', _('Horse'), ['♞']),
        ('unicode-32', _('Floret'), ['✿']),
        ('unicode-33', _('Peace sign'), ['☮']),
        ('unicode-34', _('Sun'), ['☼']),
        ('unicode-35', _('Point left'), ['☚']),
        ('unicode-36', _('Floret'), ['✾']),
        ('unicode-37', _('Yin yang'), ['☯']),
        ('unicode-38', _('Moon'), ['☾']),
        ('unicode-39', _('Point up'), ['☝']),
        ('unicode-40', _('Floret'), ['✽']),
        ('unicode-41', _('Meteor'), ['☄']),
        ('unicode-42', _('Point down'), ['☟']),
        ('unicode-43', _('Floret'), ['✺']),
        ('unicode-44', _('Scissors'), ['✂']),
        ('unicode-45', _('Pen'), ['✍']),
        ('unicode-46', _('Floret'), ['✵']),
        ('unicode-47', _('Stars'), ['⁂']),
        ('unicode-48', _('Place'), ['⌘']),
        ('unicode-49', _('High voltage'), ['⚡']),
    ]

SMILIES_SIZE = int(style.STANDARD_ICON_SIZE * 0.75)
_catalog = {}


def _smiley_to_theme_name(smiley):
    for theme in THEME:
        if smiley in theme[2]:
            return theme[0]
    return None


def parse(text):
    '''Parse text and find smiles.
    :param text:
    string to parse for smilies
    :returns:
    array of string parts and pixbufs
    '''
    if not _catalog:
        init()

    result = [text]

    for smiley in sorted(_catalog.keys(), lambda x, y: cmp(len(y), len(x))):
        new_result = []
        for word in result:
            if isinstance(word, GdkPixbuf.Pixbuf):
                new_result.append(word)
            else:
                parts = word.split(smiley)
                for i in parts[:-1]:
                    name = _smiley_to_theme_name(smiley)
                    new_result.append(i)
                    if name is not None and 'unicode' in name:
                        new_result.append(smiley)
                    else:
                        new_result.append(_catalog[smiley])
                new_result.append(parts[-1])
        result = new_result

    return result


def init():
    if _catalog:
        return

    svg_dir = os.path.join(get_bundle_path(), 'icons', 'smilies')

    for index, (name, hint, codes) in enumerate(THEME):
        archivo = os.path.join(svg_dir, '%s.svg' % (name))
        if name[0:7] == 'unicode':
            # Create the icon from unicode character on the fly
            pl = GdkPixbuf.PixbufLoader.new_with_type('svg')
            pl.write(_generate_svg(codes[0]))
            pl.close()
            pixbuf = pl.get_pixbuf()
            if not os.path.exists(archivo):
                try:
                    fd = open(archivo, 'w')
                    fd.write(_generate_svg(codes[0]))
                    fd.close()
                except IOError:
                    pass
        else:
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
                archivo, SMILIES_SIZE, SMILIES_SIZE)
        for i in codes:
            _catalog[i] = pixbuf
            THEME[index] = (archivo, hint, codes)


def _generate_svg(letter):
    # TODO: Adjust font size and character positioning
    return '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' + \
        '<svg\n' + \
        ' xmlns:dc="http://purl.org/dc/elements/1.1/"\n' + \
        ' xmlns:cc="http://creativecommons.org/ns#"\n' + \
        ' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n' + \
        ' xmlns:svg="http://www.w3.org/2000/svg"\n' + \
        ' xmlns="http://www.w3.org/2000/svg"\n' + \
        ' version="1.0"\n' + \
        ' width="55"\n' + \
        ' height="55"\n' + \
        '>\n' + \
        '<text\n' + \
        ' x="0"\n' + \
        ' y="42"\n' + \
        'style="font-size:40px;font-style:normal;font-weight:normal;\n' + \
        'fill:#ffffff;fill-opacity:1;stroke:none;' + \
        'font-family:Bitstream Vera Sans">\n' + \
        '<tspan\n' + \
        ' x="0"\n' + \
        ' y="42">\n' + \
        letter + \
        '</tspan></text>\n' + \
        '</svg>\n'