diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2012-04-20 10:59:59 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2012-04-20 13:38:22 (GMT) |
commit | 5d99691e1b9f330d540a2a523fbbcf23199e058a (patch) | |
tree | 0992334adf6241d16ec6c33d6bb2529f4934b46f | |
parent | 0c7e0ef169064159bfb608d4c54df901636143cf (diff) |
Use FontCombo
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
-rw-r--r-- | fontcombobox.py | 65 | ||||
-rw-r--r-- | globos.py | 33 | ||||
-rw-r--r-- | historietaactivity.py | 19 | ||||
-rw-r--r-- | toolbar.py | 82 |
4 files changed, 121 insertions, 78 deletions
diff --git a/fontcombobox.py b/fontcombobox.py new file mode 100644 index 0000000..58f9140 --- /dev/null +++ b/fontcombobox.py @@ -0,0 +1,65 @@ +# Copyright (C) 2012 Gonzalo Odiard <gonzalo@laptop.org> +# Based in code form Flavio Danesse <fdanesse@activitycentral.com> +# and Ariel Calzada <ariel.calzada@gmail.com> +# +# 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 gtk + +FONT_BLACKLIST = ['cmex10', 'cmmi10', 'cmr10', 'cmsy10', 'esint10', 'eufm10', + 'msam10', 'msbm10', 'rsfs10', 'wasy10'] + + +class FontComboBox(gtk.ComboBox): + + def __init__(self): + gtk.ComboBox.__init__(self) + font_renderer = gtk.CellRendererText() + self.pack_start(font_renderer) + self.add_attribute(font_renderer, 'text', 0) + self.add_attribute(font_renderer, 'font', 0) + font_model = gtk.ListStore(str) + + context = self.get_pango_context() + font_index = 0 + self.faces = {} + + for family in context.list_families(): + name = family.get_name() + if name not in FONT_BLACKLIST: + font_model.append([name]) + font_faces = [] + for face in family.list_faces(): + face_name = face.get_face_name() + font_faces.append(face_name) + self.faces[name] = font_faces + + sorter = gtk.TreeModelSort(font_model) + sorter.set_sort_column_id(0, gtk.SORT_ASCENDING) + self.set_model(sorter) + self.show() + + def set_font_name(self, font_name): + count = 0 + tree_iter = self.get_model().get_iter_first() + while tree_iter is not None: + value = self.get_model().get_value(tree_iter, 0) + if value == font_name: + self.set_active(count) + count = count + 1 + tree_iter = self.get_model().iter_next(tree_iter) + + def get_font_name(self): + tree_iter = self.get_active_iter() + return self.get_model().get_value(tree_iter, 0) @@ -19,11 +19,13 @@ DIR_ARRIBA = "arriba" DIR_IZQ = "izq" DIR_DER = "der" +DEFAULT_FONT = 'Sans' + class Globo: def __init__(self, x, y, ancho=50, alto=30, modo="normal", - direccion=DIR_ABAJO): + direccion=DIR_ABAJO, font_name=DEFAULT_FONT): self.globe_type = "GLOBE" #determina tamanio minimo @@ -43,7 +45,8 @@ class Globo: ancho_text, alto_text = self.calc_area_texto() #es el contenedor del texto - self.texto = CuadroTexto(self.x, self.y, ancho_text, alto_text) + self.texto = CuadroTexto(self.x, self.y, ancho_text, alto_text, + font_name) def imprimir(self, context): #dibujo al globo de dialogo @@ -394,7 +397,7 @@ class Globo: class Rectangulo(Globo): - def __init__(self, x, y, ancho=50, alto=15): + def __init__(self, x, y, ancho=50, alto=15, font_name=DEFAULT_FONT): self.globe_type = "RECTANGLE" #determina tamanio minimo @@ -411,7 +414,8 @@ class Rectangulo(Globo): self.y = y ancho_text, alto_text = self.calc_area_texto() - self.texto = CuadroTexto(self.x, self.y, ancho_text, alto_text) + self.texto = CuadroTexto(self.x, self.y, ancho_text, alto_text, + font_name) def imprimir(self, context): #imprimimos el rectangulo @@ -468,7 +472,8 @@ class Rectangulo(Globo): class Nube(Globo): - def __init__(self, x, y, ancho=50, alto=30, direccion=DIR_ABAJO): + def __init__(self, x, y, ancho=50, alto=30, direccion=DIR_ABAJO, + font_name=DEFAULT_FONT): self.globe_type = "CLOUD" self.radio = 30 @@ -489,7 +494,8 @@ class Nube(Globo): appdir = os.path.join(activity.get_bundle_path()) ancho_text, alto_text = self.calc_area_texto() - self.texto = CuadroTexto(self.x, self.y, ancho_text, alto_text) + self.texto = CuadroTexto(self.x, self.y, ancho_text, alto_text, + font_name) def imprimir(self, context): @@ -598,7 +604,8 @@ class Nube(Globo): class Grito(Globo): - def __init__(self, x, y, ancho=50, alto=30, direccion=DIR_ABAJO): + def __init__(self, x, y, ancho=50, alto=30, direccion=DIR_ABAJO, + font_name=DEFAULT_FONT): self.globe_type = "EXCLAMATION" self.radio = 30 @@ -615,7 +622,8 @@ class Grito(Globo): self.y = y ancho_text, alto_text = self.calc_area_texto() - self.texto = CuadroTexto(self.x, self.y, ancho_text, alto_text) + self.texto = CuadroTexto(self.x, self.y, ancho_text, alto_text, + font_name) def imprimir(self, context): context.save() @@ -687,7 +695,8 @@ class Grito(Globo): class Imagen(Globo): - def __init__(self, imagen, x, y, ancho=50, alto=30, direccion=DIR_ABAJO): + def __init__(self, imagen, x, y, ancho=50, alto=30, direccion=DIR_ABAJO, + font_name=DEFAULT_FONT): self.globe_type = "IMAGE" self.radio = 30 @@ -706,7 +715,7 @@ class Imagen(Globo): self.icon_buffer = _IconBuffer() self.icon_buffer.file_name = os.path.join(appdir, imagen) self.icon_buffer.stroke_color = '#000000' - self.texto = CuadroTexto(self.x, self.y, 20, 20) + self.texto = CuadroTexto(self.x, self.y, 20, 20, font_name) def imprimir(self, context): @@ -779,7 +788,7 @@ class CuadroTexto: "Es un cuadro de texto con alineacion centralizada" - def __init__(self, x, y, ancho=50, alto=30): + def __init__(self, x, y, ancho=50, alto=30, font_name=DEFAULT_FONT): #Ancho del cuadro = 2*self.ancho self.ancho = ancho @@ -796,7 +805,7 @@ class CuadroTexto: #Caracteristicas de la tipografia self.font_size = 12 - self.font_type = "Georgia" + self.font_type = font_name self.color_r, self.color_g, self.color_b = 0, 0, 0 self.italic = False self.bold = False diff --git a/historietaactivity.py b/historietaactivity.py index 2737a5b..44c480b 100644 --- a/historietaactivity.py +++ b/historietaactivity.py @@ -265,6 +265,7 @@ class Page(gtk.VBox): self.boxs = [] self._active_box = None + self.selected_font_name = globos.DEFAULT_FONT logging.error('SCREEN WIDTH %d DEF_SPACING %d' % (SCREEN_WIDTH, DEF_SPACING)) @@ -377,21 +378,23 @@ class ComicBox(gtk.DrawingArea): return self._globo_activo def add_globo(self, xpos, ypos, gmodo="normal", \ - gdireccion=globos.DIR_ABAJO): + gdireccion=globos.DIR_ABAJO, font_name=globos.DEFAULT_FONT): #agrega un globo al cuadro - globo = globos.Globo(x=xpos, y=ypos, modo=gmodo, direccion=gdireccion) + globo = globos.Globo(x=xpos, y=ypos, modo=gmodo, direccion=gdireccion, + font_name=font_name) self.globos.append(globo) self._globo_activo = globo self.queue_draw() - def add_rectangulo(self, xpos, ypos): + def add_rectangulo(self, xpos, ypos, font_name=globos.DEFAULT_FONT): #agrega un cuadro de texto al cuadro - self.globos.append(globos.Rectangulo(x=xpos, y=ypos)) + self.globos.append(globos.Rectangulo(x=xpos, y=ypos, + font_name=font_name)) self.queue_draw() - def add_nube(self, xpos, ypos): + def add_nube(self, xpos, ypos, font_name=globos.DEFAULT_FONT): #agrega un globo de pensamiento al cuadro - globo = globos.Nube(x=xpos, y=ypos) + globo = globos.Nube(x=xpos, y=ypos, font_name=font_name) self.globos.append(globo) self._globo_activo = globo self.queue_draw() @@ -403,9 +406,9 @@ class ComicBox(gtk.DrawingArea): self._globo_activo = globo self.queue_draw() - def add_grito(self, xpos, ypos): + def add_grito(self, xpos, ypos, font_name=globos.DEFAULT_FONT): #agrega un globo de grito al cuadro - globo = globos.Grito(x=xpos, y=ypos) + globo = globos.Grito(x=xpos, y=ypos, font_name=font_name) self.globos.append(globo) self._globo_activo = globo self.queue_draw() @@ -21,19 +21,18 @@ import logging import os import time import gtk -import pango -import pangocairo -import globos from sugar.graphics.icon import Icon from sugar.graphics.toolbutton import ToolButton from sugar.graphics.toggletoolbutton import ToggleToolButton from sugar.graphics.combobox import ComboBox from sugar.graphics.toolcombobox import ToolComboBox -from sugar.graphics import iconentry from sugar.graphics.objectchooser import ObjectChooser from sugar.activity.widgets import RadioMenuButton from sugar.graphics.menuitem import MenuItem +from fontcombobox import FontComboBox +import globos + WITH_COLOR_BUTTON = True try: from sugar.graphics.colorbutton import ColorToolButton @@ -161,20 +160,26 @@ class GlobesManager(): def __btn_clicked(self, boton): logging.error('boton clicked %s', boton.props.icon_name) + selected_font_name = self._activity.page.selected_font_name if boton == self.add_globe: - self._page.get_active_box().add_globo(60, 60) + self._page.get_active_box().add_globo(60, 60, + font_name=selected_font_name) if boton == self.add_cloud: - self._page.get_active_box().add_nube(60, 60) + self._page.get_active_box().add_nube(60, 60, + font_name=selected_font_name) if boton == self.add_whisp: - self._page.get_active_box().add_globo(60, 60, gmodo="despacio") + self._page.get_active_box().add_globo(60, 60, gmodo="despacio", + font_name=selected_font_name) if boton == self.add_scream: - self._page.get_active_box().add_grito(60, 60) + self._page.get_active_box().add_grito(60, 60, + font_name=selected_font_name) if boton == self.add_box: - self._page.get_active_box().add_rectangulo(60, 60) + self._page.get_active_box().add_rectangulo(60, 60, + font_name=selected_font_name) if boton == self.add_photo: self.add_image() @@ -317,21 +322,10 @@ class TextToolbar(gtk.Toolbar): self.insert(tool_item, -1) # font - self._has_custom_fonts = False - - self._fonts = [] - pango_context = gtk.Widget.create_pango_context(tool_item) - for family in pango_context.list_families(): - self._fonts.append(family.get_name()) - self._fonts.sort() - - self._font_combo = ComboBox() + self._font_combo = FontComboBox() + self._font_combo.set_font_name(globos.DEFAULT_FONT) self._fonts_changed_id = self._font_combo.connect('changed', self._font_changed_cb) - for i, f in enumerate(self._fonts): - self._font_combo.append_item(i, f, None) - if f == 'Times New Roman': - self._font_combo.set_active(i) tool_item = ToolComboBox(self._font_combo) self.insert(tool_item, -1) @@ -383,12 +377,12 @@ class TextToolbar(gtk.Toolbar): def _font_changed_cb(self, combobox): if self._font_combo.get_active() != -1: - logger.debug('Setting font name: %s', - self._fonts[self._font_combo.get_active()]) + font_name = self._font_combo.get_font_name() + logger.debug('Setting font name: %s', font_name) globo_activo = self._page.get_globo_activo() if (globo_activo != None): - globo_activo.texto.font_type = \ - self._fonts[self._font_combo.get_active()] + globo_activo.texto.font_type = font_name + self._page.selected_font_name = font_name self._page.get_active_box().queue_draw() """ @@ -422,35 +416,7 @@ class TextToolbar(gtk.Toolbar): break # font seleccionada - font_family = globeText.font_type - font_index = -1 - - # search for the font name in our font list - for i, f in enumerate(self._fonts): - if f == font_family: - font_index = i - break - - # if we don't know this font yet, then add it (temporary) to the list - if font_index == -1: - logger.debug('Font not found in font list: %s', font_family) - if not self._has_custom_fonts: - # add a separator to seperate the non-available fonts from - # the available ones - self._fonts.append('') # ugly - self._font_combo.append_separator() - self._has_custom_fonts = True - # add the new font - self._fonts.append(font_family) - self._font_combo.append_item(0, font_family, None) - # see how many fonts we have now, so we can select the last one - model = self._font_combo.get_model() - num_children = model.iter_n_children(None) - logger.debug('Number of fonts in the list: %d', num_children) - font_index = num_children - 1 - - # activate the found font - if (font_index > -1): - self._font_combo.handler_block(self._fonts_changed_id) - self._font_combo.set_active(font_index) - self._font_combo.handler_unblock(self._fonts_changed_id) + self._font_combo.handler_block(self._fonts_changed_id) + self._font_combo.set_font_name(globeText.font_type) + self._page.selected_font_name = globeText.font_type + self._font_combo.handler_unblock(self._fonts_changed_id) |