diff options
author | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-09-04 16:02:30 (GMT) |
---|---|---|
committer | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-09-04 16:02:30 (GMT) |
commit | 05f2a6684a42209e30fa2fcd0d7cb014c4663a39 (patch) | |
tree | 6b3cfd307e94f6b640ff985d141daa82b8fef7c1 /ep_layer_letterpress.py | |
parent | 7ee9a0b36814d90954929a4fead2bf9ff4314c19 (diff) |
Additional samplers added. Using set_sensitive to disable GUI elements.
Diffstat (limited to 'ep_layer_letterpress.py')
-rw-r--r-- | ep_layer_letterpress.py | 165 |
1 files changed, 103 insertions, 62 deletions
diff --git a/ep_layer_letterpress.py b/ep_layer_letterpress.py index 7ce19d9..b607020 100644 --- a/ep_layer_letterpress.py +++ b/ep_layer_letterpress.py @@ -15,40 +15,53 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -import math import random import pango import pangocairo import model_layer import ka_random +import ka_factory import model_constraintpool import exon_color import exon_position -import exon_direction import exon_buzzword +FONTFAMILY_CONSTRAINT = 'fontfamily_constraint' +FONTSTYLE_CONSTRAINT = 'fontstyle_constraint' +FONTSIZE_CONSTRAINT = 'fontsize_constraint' +FONTWEIGHT_CONSTRAINT = 'fontweight_constraint' +SAMPLERTYPE_CONSTRAINT = 'samplertype_constraint' + class LetterPress(model_layer.Layer): """LetterPress - inv: len(self.direction_steps) > 0 """ - cdef = [{'bind' : 'family', + cdef = [{'bind' : FONTFAMILY_CONSTRAINT, 'name' : 'Font family', 'domain': model_constraintpool.STRING_1_OF_N, - 'enum' : ['Sans', - 'Nimbus Sans L', + 'enum' : ['Normal', + 'Sans', + 'Serif', 'Monospace',]}, - {'bind' : 'style', + {'bind' : FONTSTYLE_CONSTRAINT, 'name' : 'Font style', 'domain': model_constraintpool.INT_1_OF_N, 'enum' : [('Normal', pango.STYLE_NORMAL), ('Oblique', pango.STYLE_OBLIQUE), ('Italic', pango.STYLE_ITALIC),]}, - {'bind' : 'size', - 'name' : 'Font size', + {'bind' : FONTSIZE_CONSTRAINT, + 'name' : 'Font size in percent of the drawing area.', + 'domain': model_constraintpool.INT_RANGE, + 'min' : 1, 'max': 100}, + {'bind' : FONTWEIGHT_CONSTRAINT, + 'name' : 'Specifies how bold or light the font should be.', 'domain': model_constraintpool.INT_RANGE, - 'min' : 4, 'max': 64}, - ] + 'min' : 100, 'max': 900}, + {'bind' : SAMPLERTYPE_CONSTRAINT, + 'name' : 'Permitted sampler types', + 'domain': model_constraintpool.STRING_1_OF_N, + 'enum' : ka_factory.get_factory('sampler').keys()}, + ] font_style = {pango.STYLE_NORMAL: 'Normal', pango.STYLE_OBLIQUE: 'Oblique', @@ -60,11 +73,15 @@ class LetterPress(model_layer.Layer): super(LetterPress, self).__init__(trunk) cpool = model_constraintpool.ConstraintPool.get_pool() self.textcolor = exon_color.Color(self.path, 0, 0, 0, 1) - self.family = cpool.get(self, 'family')[0] - self.style = cpool.get(self, 'style')[0] - self.size = cpool.get(self, 'size')[0] + self.family = cpool.get(self, FONTFAMILY_CONSTRAINT)[0] + self.style = cpool.get(self, FONTSTYLE_CONSTRAINT)[0] + self.size = cpool.get(self, FONTSIZE_CONSTRAINT)[0] + self.weight = cpool.get(self, FONTWEIGHT_CONSTRAINT)[0] self.center = exon_position.Position(self.path, 0.0, 0.0) - self.direction_steps = [exon_direction.Direction(self.path, 0.0, 0.0)] +# self.direction_steps = [exon_direction.Direction(self.path, 0.0, 0.0)] + sampler_factory = ka_factory.get_factory('sampler') + sampler_key = sampler_factory.keys()[0] + self.sampler = sampler_factory.create(sampler_key, self.path) self.buzzwords = exon_buzzword.Buzzword(self.path, ['']) def __eq__(self, other): @@ -75,59 +92,75 @@ class LetterPress(model_layer.Layer): and self.family == other.family \ and self.style == other.style \ and self.size == other.size \ + and self.weight == other.weight \ and self.center == other.center \ - and len(self.direction_steps) == len(other.direction_steps) \ + and self.sampler == other.sampler \ and self.buzzwords == other.buzzwords - if equal: - for index, direction in enumerate(self.direction_steps): - equal = equal and direction == other.direction_steps[index] +# and len(self.direction_steps) == len(other.direction_steps) \ +# if equal: +# for index, direction in enumerate(self.direction_steps): +# equal = equal and direction == other.direction_steps[index] return equal def randomize(self): """Randomize the layers components.""" super(LetterPress, self).randomize() cpool = model_constraintpool.ConstraintPool.get_pool() - family_constraint = cpool.get(self, 'family') - style_constraint = cpool.get(self, 'style') - size_constraint = cpool.get(self, 'size') self.textcolor.randomize() + family_constraint = cpool.get(self, FONTFAMILY_CONSTRAINT) self.family = random.choice(family_constraint) + style_constraint = cpool.get(self, FONTSTYLE_CONSTRAINT) self.style = random.choice(style_constraint) - self.size = random.randint(size_constraint[0], size_constraint[1]) + size_constraint = cpool.get(self, FONTSIZE_CONSTRAINT) + self.size = ka_random.randint_constrained(size_constraint) + weight_constraint = cpool.get(self, FONTWEIGHT_CONSTRAINT) + self.weight = ka_random.randint_constrained(weight_constraint) self.center.randomize() + + sampler_factory = ka_factory.get_factory('sampler') + samplertype_constraint = cpool.get(self, SAMPLERTYPE_CONSTRAINT) + self.sampler = sampler_factory.create_random(samplertype_constraint, + self.path) + self.sampler.randomize() + self.buzzwords.randomize() - self.direction_steps = [] - for i in range(random.randint(1, 6)): - direction = exon_direction.Direction(self.path, 0.0, 0.0) - direction.randomize() - self.direction_steps.append(direction) +# self.direction_steps = [] +# for i in range(random.randint(1, 6)): +# direction = exon_direction.Direction(self.path, 0.0, 0.0) +# direction.randomize() +# self.direction_steps.append(direction) def mutate(self): """Make small random changes to the layers components.""" super(LetterPress, self).mutate() cpool = model_constraintpool.ConstraintPool.get_pool() - family_constraint = cpool.get(self, 'family') - style_constraint = cpool.get(self, 'style') - size_constraint = cpool.get(self, 'size') self.textcolor.mutate() if ka_random.is_mutating(): + family_constraint = cpool.get(self, FONTFAMILY_CONSTRAINT) self.family = random.choice(family_constraint) if ka_random.is_mutating(): + style_constraint = cpool.get(self, FONTSTYLE_CONSTRAINT) self.style = random.choice(style_constraint) if ka_random.is_mutating(): - self.size += random.randint(-12, 12) - self.size = ka_random.limitate_range(self.size, \ - size_constraint[0], \ - size_constraint[1]) + size_constraint = cpool.get(self, FONTSIZE_CONSTRAINT) + self.size = ka_random.jitter_discret_constrained( + self.size, size_constraint) + if ka_random.is_mutating(): + weight_constraint = cpool.get(self, FONTWEIGHT_CONSTRAINT) + self.weight = ka_random.jitter_discret_constrained( + self.weight, weight_constraint) self.center.mutate() + self.sampler.mutate() self.buzzwords.mutate() - for direction in self.direction_steps: - direction.mutate() +# for direction in self.direction_steps: +# direction.mutate() def shuffle(self): """Shuffle similar componets.""" super(LetterPress, self).shuffle() self.buzzwords.shuffle() + if ka_random.is_shuffling(): + self.sampler.shuffle() # self.direction_steps.shuffle() def crossingover(self, other): @@ -139,18 +172,20 @@ class LetterPress(model_layer.Layer): post: __return__ is not other """ new_one = LetterPress(self.get_trunk()) - crossing = self.crossingover_base(new_one, other, 5) + crossing = self.crossingover_base(new_one, other, 7) new_one.textcolor = self.textcolor.crossingover(other.textcolor) new_one.center = self.center if crossing[0] else other.center - new_one.buzzwords = self.buzzwords if crossing[1] else other.buzzwords - new_one.family = self.family if crossing[2] else other.family - new_one.style = self.style if crossing[3] else other.style - new_one.size = self.size if crossing[4] else other.size - new_one.direction_steps = ka_random.crossingover(self.direction_steps, \ - other.direction_steps) + new_one.sampler = self.sampler if crossing[1] else other.sampler + new_one.buzzwords = self.buzzwords if crossing[2] else other.buzzwords + new_one.family = self.family if crossing[3] else other.family + new_one.style = self.style if crossing[4] else other.style + new_one.size = self.size if crossing[5] else other.size + new_one.weight = self.weight if crossing[6] else other.weight +# new_one.direction_steps = ka_random.crossingover(self.direction_steps, \ +# other.direction_steps) return new_one - def draw(self, ctx, width, height): + def render(self, ctx, width, height): """ pre: ctx is not None pre: width > 0 @@ -158,34 +193,32 @@ class LetterPress(model_layer.Layer): pre: width == height """ # self.operator = cairo.OPERATOR_OVER - self.begin_draw(ctx, width, height) + self.begin_render(ctx, width, height) ctx.scale(1.0/width, 1.0/height) pango_ctx = pangocairo.CairoContext(ctx) - px, py = self.center.x_pos, self.center.y_pos + points = self.sampler.get_sample_points() fi = di = -1 for word in self.buzzwords.wordlist: - di = (di+1) % len(self.direction_steps) - step = self.direction_steps[di] - px += step.offset * math.cos(step.radian) - py += step.offset * math.sin(step.radian) + di = (di+1) % len(points) + px = self.center.x_pos + points[di][0] + py = self.center.y_pos + points[di][1] layout = pango_ctx.create_layout() fi = (fi+1) % len(self.family) desc = pango.FontDescription(self.family[fi]) - desc.set_size(self.size * pango.SCALE) + desc.set_size(int(self.size * width * 0.01 * pango.SCALE)) desc.set_style(self.style) - # desc.set_weight(self.font["weight"]) + desc.set_weight(self.weight) layout.set_text(word.encode('utf-8')) layout.set_font_description(desc) layout.set_alignment(pango.ALIGN_CENTER) rgba = self.textcolor.rgba pango_ctx.set_source_rgba(rgba[0], rgba[1], rgba[2], rgba[3]) pango_ctx.update_layout(layout) - pixel_extents = layout.get_pixel_extents() -# extents = layout.get_extents() - dx = pixel_extents[1][2] / 2 - dy = pixel_extents[1][3] / 2 + + pixel_size = layout.get_pixel_size() + dx, dy = 0.5 * pixel_size[0], 0.9 * pixel_size[1] pango_ctx.move_to((width * px) - dx, (height * py) - dy) pango_ctx.show_layout(layout) @@ -193,11 +226,17 @@ class LetterPress(model_layer.Layer): # super(LetterPress, self).explain(formater) formater.text_list('buzzwords: ', self.buzzwords.wordlist) formater.color_item(self.textcolor, 'text color:') - formater.text_item(self.family \ - + ', ' + LetterPress.font_style[self.style] \ - + ', ' + str(self.size)) + formater.text_item(self.family + + ', ' + LetterPress.font_style[self.style] + + ', ' + str(self.size) + + ', ' + str(self.weight)) formater.position_item(self.center, 'center:') - formater.direction_array(self.direction_steps, 'direction:') + text, surface, descr = self.sampler.explain() + if surface is not None: + formater.surface_item(surface, 'sampling points:' + text, descr) + else: + formater.text_item(text) +# formater.direction_array(self.direction_steps, 'direction:') def copy(self): """The LetterPress diagram layers copy constructor. @@ -210,7 +249,9 @@ class LetterPress(model_layer.Layer): new_one.family = self.family new_one.style = self.style new_one.size = self.size + new_one.weight = self.weight new_one.center = self.center.copy() + new_one.sampler = self.sampler.copy() new_one.buzzwords = self.buzzwords.copy() - new_one.direction_steps = ka_random.copy_list(self.direction_steps) +# new_one.direction_steps = ka_random.copy_list(self.direction_steps) return new_one |