[Sugar-devel] [PATCH] Users cand change font for the cards, also 0001-Made-english_rp-default-for-en_AU.patch applied
Ariel Calzada
ariel.calzada at gmail.com
Wed Apr 11 09:21:51 EDT 2012
From: Ariel Calzada <aricalso at 000PaRaDoX000.(none)>
THIS PATCH Let users change the font for the cards. Users just need to edit the game and
select each font ( top and bottom cards ). Also this patch has included
0001-Made-english_rp-default-for-en_AU.patch
---
createcardpanel.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++---
speak/voice.py | 48 ++++++++++++++++++++++++----
svgcard.py | 33 ++++++++++++++++---
3 files changed, 152 insertions(+), 17 deletions(-)
diff --git a/createcardpanel.py b/createcardpanel.py
index e654b1c..3aec20b 100644
--- a/createcardpanel.py
+++ b/createcardpanel.py
@@ -19,6 +19,8 @@
import gtk
from os.path import join, basename
+import os
+from sugar.activity.activity import get_bundle_path
import shutil
from gettext import gettext as _
@@ -92,8 +94,8 @@ class CreateCardPanel(gtk.EventBox):
# Set card editors
- self.cardeditor1 = CardEditor()
- self.cardeditor2 = CardEditor()
+ self.cardeditor1 = CardEditor(1)
+ self.cardeditor2 = CardEditor(2)
self.clean(None)
self.cardeditor1.connect('has-text', self.receive_text_signals)
self.cardeditor2.connect('has-text', self.receive_text_signals)
@@ -248,11 +250,11 @@ class CardEditor(gtk.EventBox):
'has-sound': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]),
}
- def __init__(self):
+ def __init__(self,editor_index):
gtk.EventBox.__init__(self)
self.snd = None
-
+ self.editor_index = editor_index
self.temp_folder = None
box = gtk.VBox()
@@ -307,10 +309,86 @@ class CardEditor(gtk.EventBox):
else:
self.usespeak = None
- box.pack_start(toolbar, False)
+ box.pack_start(self.create_font_selector(), True, True, 0)
self.add(box)
+ def get_saved_font(self, editor_index):
+ fd = get_bundle_path() + "/data"
+ fn = fd + "/config.conf." + str(editor_index)
+
+ if not os.path.exists(fd):
+ os.mkdir(fd)
+
+ if not os.path.isfile(fn):
+ return "Monospace"
+
+ f = open(fn,"r")
+ contents = f.read().strip()
+ f.close()
+
+ if contents == "":
+ return "Monospace"
+
+ return contents
+
+ def set_saved_font(self, editor_index, font):
+ fd = get_bundle_path() + "/data"
+ fn = fd + "/config.conf." + str(editor_index)
+
+ if not os.path.exists(fd):
+ os.mkdir(fd)
+
+ f = open(fn,"w")
+ f.write(font)
+ f.close()
+
+
+ def create_font_selector(self):
+ """ Create font combobox
+ """
+ font_selector = gtk.ComboBox()
+ font_renderer = gtk.CellRendererText()
+ font_selector.pack_start(font_renderer)
+ font_selector.add_attribute(font_renderer, 'text', 0)
+ font_selector.add_attribute(font_renderer, 'font', 0)
+ font_model = gtk.ListStore(str)
+
+ context = self.get_pango_context() # Deja Vu Sans bold
+ font_index = 0
+ count = 0
+ faces = {}
+ selected_font = self.get_saved_font(self.editor_index)
+
+ for i in context.list_families():
+ name = i.get_name()
+
+ if name == selected_font:
+ font_index = count
+
+ count = count + 1
+ font_model.append([name])
+ font_faces = gtk.ListStore(str, str)
+
+ for face in i.list_faces():
+ face_name = face.get_face_name()
+ font_faces.append([face_name, "%s %s" % (name, face_name)])
+
+ faces[name] = font_faces
+
+ font_selector.set_model(font_model)
+ font_selector.set_active(font_index)
+ font_selector.connect("changed", self.font_changed)
+ font_selector.show()
+
+ return font_selector
+
+ def font_changed(self,widget):
+ iter = widget.get_active_iter()
+ font = widget.get_model().get_value(iter, 0)
+ if font:
+ self.set_saved_font(self.editor_index,font)
+
def update_text(self, entry):
self.card.change_text(entry.get_text())
if len(entry.get_text()) == 0:
diff --git a/speak/voice.py b/speak/voice.py
index 5fc732e..c45d108 100644
--- a/speak/voice.py
+++ b/speak/voice.py
@@ -7,12 +7,12 @@
#
# Parts of Speak.activity are based on code from Measure.activity
# Copyright (C) 2007 Arjun Sarwal - arjun at laptop.org
-#
+#
# Speak.activity 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.
-#
+#
# Speak.activity 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
@@ -81,8 +81,36 @@ class Voice:
friendlyname = friendlyname.replace('_test','')
friendlyname = friendlyname.replace('en-','')
friendlyname = friendlyname.replace('english-wisper','whisper')
- friendlyname = friendlyname.capitalize()
- self.friendlyname = _(friendlyname)
+
+ friendlyname = friendlyname.replace('english-us', 'us')
+
+ friendlynameRP = name # friendlyname for RP
+ friendlynameRP = friendlynameRP.replace('english_rp', 'rp')
+ friendlynameRP = friendlynameRP.replace('english_wmids', 'wmids')
+
+ parts = re.split('[ _-]', friendlyname)
+ partsRP = re.split('[ _]', friendlynameRP) #RE for english_RP
+ self.short_name = _(parts[0].capitalize())
+ self.friendlyname = ' '.join([self.short_name] + parts[1:])
+
+ friendlynameRP1 = None
+ if friendlynameRP == 'rp':
+ friendlynameRP1 = 'English (Received Pronunciation)'
+ self.friendlyname = 'English (Received Pronunciation)'
+
+ friendlynameUS = None
+ if friendlyname == 'us':
+ friendlynameUS = 'English (USA)'
+ self.friendlyname = 'English (USA)'
+
+ friendlynameWMIDS = None
+ if friendlynameRP == 'wmids':
+ friendlynameWMIDS = 'English (West Midlands)'
+ self.friendlyname = 'English (West Midlands)'
+
+ def __cmp__(self, other):
+ return cmp(self.friendlyname, other.friendlyname if other else '')
+
def allVoices():
if _allVoices:
@@ -110,9 +138,9 @@ def defaultVoice():
def fit(a,b):
"Compare two language ids to see if they are similar."
- as_ = re.split(r'[^a-z]+', a.lower())
- bs = re.split(r'[^a-z]+', b.lower())
- for count in range(0, min(len(as_),len(bs))):
+ as_ = re.split(r'[^a-z]+', a.lower())
+ bs = re.split(r'[^a-z]+', b.lower())
+ for count in range(0, min(len(as_),len(bs))):
if as_[count] != bs[count]:
count -= 1
break
@@ -126,6 +154,12 @@ def defaultVoice():
for voice in voices.values():
voiceMetric = fit(voice.language, lang)
bestMetric = fit(best.language, lang)
+
+ if lang.startswith('en_AU'):
+ if voice.friendlyname=='English (Received Pronunciation)':
+ best = voice
+ break
+
if voiceMetric > bestMetric:
best = voice
diff --git a/svgcard.py b/svgcard.py
index 7500f3f..5a87ac1 100644
--- a/svgcard.py
+++ b/svgcard.py
@@ -23,8 +23,9 @@ import re
import gtk
import pango
import logging
-
from sugar.util import LRU
+from sugar.activity.activity import get_bundle_path
+import os
import theme
import face
@@ -102,6 +103,26 @@ class SvgCard(gtk.EventBox):
#gc.collect()
+ def get_saved_font(self, editor_index):
+ fd = get_bundle_path() + "/data"
+ fn = fd + "/config.conf." + str(editor_index)
+
+ if not os.path.exists(fd):
+ os.mkdir(fd)
+
+ if not os.path.isfile(fn):
+ return "Monospace"
+
+ f = open(fn,"r")
+ contents = f.read().strip()
+ f.close()
+
+ if contents == "":
+ return "Monospace"
+
+ return contents
+
+
def _realize_cb(self, widget):
self.gc = widget.window.new_gc()
@@ -288,9 +309,6 @@ class SvgCard(gtk.EventBox):
def create_text_layout(self, text):
key = (self.size, text)
- if key in _text_layout_cache:
- return _text_layout_cache[key]
-
max_lines_count = len([i for i in text.split(' ') if i])
for size in range(80, 66, -8) + range(66, 44, -6) + \
@@ -300,7 +318,12 @@ class SvgCard(gtk.EventBox):
layout = self.create_pango_layout(text)
layout.set_width(PIXELS_PANGO(card_size))
layout.set_wrap(pango.WRAP_WORD)
- desc = pango.FontDescription('Deja Vu Sans bold ' + str(size))
+
+ font = "Monospace"
+ if self.id != -1:
+ font = self.get_saved_font(self.pprops['back_text']['card_text'])
+
+ desc = pango.FontDescription(font + " " + str(size))
layout.set_font_description(desc)
if layout.get_line_count() <= max_lines_count and \
--
1.7.5.4
More information about the Sugar-devel
mailing list