Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_stamp_glyph.py
blob: c2b151a5d00176ce9f084a7db7f912369562353e (plain)
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
# coding: UTF8
# Copyright 2009 Thomas Jourdan
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import random
import cairo
import model_random
import model_locus
import model_allele
import model_constraintpool
from gettext import gettext as _

FONTSIZE_CONSTRAINT   = 'fontsizeconstraint'

class GlyphStamp(model_allele.Allele):
    """GlyphStamp:.
    inv: self.size >= 0.0
    inv: len(unichrtab) == len(self.mapping)
    inv: forall(self.mapping, lambda x: x >= 0 and x < len(unichrtab))
    """

    cdef = [{'bind'  : FONTSIZE_CONSTRAINT,
             'name'  : 'Font size in percent of the drawing area.',
             'domain': model_constraintpool.INT_RANGE,
             'min'   : 0.01, 'max': 1.0},
           ]

    def __init__(self, trunk):
        """Constructor for a flip merger."""
        super(GlyphStamp, self).__init__(trunk)
        cpool = model_constraintpool.ConstraintPool.get_pool()
        self.size = cpool.get(self, FONTSIZE_CONSTRAINT)[0]
        self.mapping = [ix for ix in range(len(unichrtab))]

    def __eq__(self, other):
        """Equality based on radius."""
        equal = isinstance(other, GlyphStamp) \
                and self.size == other.size
        if equal:
            for cix, value in enumerate(self.mapping):
                equal = equal and value == other.mapping[cix]
        return equal

    def randomize(self):
        """Randomize the layers components."""
        cpool = model_constraintpool.ConstraintPool.get_pool()
        random.shuffle(self.mapping)
        size_constraint = cpool.get(self, FONTSIZE_CONSTRAINT)
        self.size = model_random.uniform_constrained(size_constraint)

    def mutate(self):
        """Make small random changes to the layers components."""
        cpool = model_constraintpool.ConstraintPool.get_pool()
        size_constraint = cpool.get(self, FONTSIZE_CONSTRAINT)
        self.size = model_random.jitter_constrained(self.size, size_constraint)

    def swap_places(self):
        """Shuffle mapping table."""
        model_random.swap_places(self.mapping)
        for dummy in range(len(unichrtab) / 20):
            model_random.swap_places(self.mapping)

    def crossingover(self, other):
        """
        pre: isinstance(other, GlyphStamp)
        pre: isinstance(self, GlyphStamp)
        # check for distinct references, needs to copy content, not references
        post: __return__ is not self
        post: __return__ is not other
        post: model_locus.unique_check(__return__, self, other) == ''
        """
        new_one = GlyphStamp(self.get_trunk())
        cross_sequence = model_random.crossing_sequence(1)
        new_one.size = self.size if cross_sequence[0] else other.size
        new_one.mapping = model_random.crossingover_nativeelement_list(
                                                        self.mapping,
                                                        other.mapping)
        return new_one
    
    def set_extent(self, width, height):
        """Set extent of stamp. Some stamps will ignore this."""
        pass

    def render(self, ctx, point, rad=0.1, state=0):
        """
        pre: ctx is not None
        pre: len(point) == 2
        """
        uc = unichr(unichrtab[self.mapping[state % len(unichrtab)]])
        ctx.select_font_face('Sans',
                             cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
        ctx.set_font_size(self.size)
        x_bearing, y_bearing, width, height = ctx.text_extents(uc)[:4]
        ctx.move_to(point[0] - width / 2 - x_bearing,
                    point[1] - height / 2 - y_bearing)
        ctx.show_text(uc)

    def explain(self):
        """
        post: len(__return__) == 3
        """
        return u'A collection of characters: ', \
               None, \
               None

    def copy(self):
        """A copy constructor.
        post: isinstance(__return__, GlyphStamp)
        # check for distinct references, needs to copy content, not references
        post: len(__return__.mapping) == len(self.mapping)
        post: __return__ is not self
        """
        new_one = GlyphStamp(self.get_trunk())
        new_one.mapping = [self.mapping[ix] for ix in range(len(self.mapping))]
        new_one.size = self.size
        return new_one

unichrtab = (
    0x00a4,
    0x00ac,
    0x00b1,
    0x00b5,
    0x00b6,
    0x00bc,
    0x00bd,
    0x00be,
    0x00c6,
    0x00d8,
    0x00e6,
    0x00f8,
    0x013d,
    0x013f,
    0x0152,
    0x0153,
    0x0192,
    0x0402,
    0x0409,
    0x040a,
    0x0428,
    0x0429,
    0x042d,
    0x042e,
    0x0431,
    0x0444,
    0x044d,
    0x044e,
    0x0459,
    0x0498,
    0x04b4,
    0x04be,
    0x04c1,
    0x04d4,
    0x04d5,
    0x04da,
    0x04dc,
    0x04de,
    0x04f4,
    0x2021,
    0x2202,
    0x2206,
    0x2211,
    0x221a,
    0x221e,
    0x2260,
)