From 4cea13c44ec26de51a629d1581cb5908bf88e482 Mon Sep 17 00:00:00 2001
From: Aneesh Dogra <lionaneesh@gmail.com>
Date: Wed, 28 Nov 2012 04:21:49 +0530
Subject: [PATCH] Retain font-family and font-size. Fix #1690.
Note: self.abiword_canvas.get_selection('text/plain')[1] gives random values
if the file is empty. Therefore, I used len(self.abiword_canvas.get_content('text/plain')) instead to check for empty files.
---
AbiWordActivity.py | 33 +++++++++++++++++++++++++++++----
font.cfg | 4 ++++
widgets.py | 33 ++++++++++++++++++++++++++++++---
3 files changed, 63 insertions(+), 7 deletions(-)
create mode 100644 font.cfg
diff --git a/AbiWordActivity.py b/AbiWordActivity.py
index b272f04..0b5c842 100644
a
|
b
|
|
19 | 19 | from gettext import gettext as _ |
20 | 20 | import logging |
21 | 21 | import os |
| 22 | import ConfigParser |
22 | 23 | |
23 | 24 | # Abiword needs this to happen as soon as possible |
24 | 25 | import gobject |
… |
… |
gobject.threads_init() |
27 | 28 | import gtk |
28 | 29 | import telepathy |
29 | 30 | import telepathy.client |
| 31 | import os |
| 32 | |
30 | 33 | |
31 | 34 | from abiword import Canvas |
32 | 35 | |
… |
… |
import speech |
53 | 56 | from speechtoolbar import SpeechToolbar |
54 | 57 | |
55 | 58 | logger = logging.getLogger('write-activity') |
| 59 | DEFAULT_CONFIG_PATH = get_bundle_path() + '/font.cfg' |
56 | 60 | |
57 | 61 | |
58 | 62 | class AbiWordActivity(activity.Activity): |
… |
… |
class AbiWordActivity(activity.Activity): |
60 | 64 | def __init__(self, handle): |
61 | 65 | activity.Activity.__init__(self, handle) |
62 | 66 | |
| 67 | self.font = 'Sans' # Default, only used when no configuration is present |
| 68 | |
| 69 | if os.path.exists('/etc/font.cfg'): |
| 70 | self._read_configuration('/etc/font.cfg') |
| 71 | elif os.path.exists(DEFAULT_CONFIG_PATH): |
| 72 | self._read_configuration(DEFAULT_CONFIG_PATH) |
| 73 | |
63 | 74 | # abiword uses the current directory for all its file dialogs |
64 | 75 | os.chdir(os.path.expanduser('~')) |
65 | 76 | |
… |
… |
class AbiWordActivity(activity.Activity): |
160 | 171 | |
161 | 172 | self.abiword_canvas.connect('size-allocate', self.size_allocate_cb) |
162 | 173 | |
| 174 | def _read_configuration(self, file_name): |
| 175 | logging.debug('Reading configuration from file %s' % file_name) |
| 176 | fp = open(file_name) |
| 177 | config = ConfigParser.ConfigParser() |
| 178 | config.readfp(fp) |
| 179 | fp.close() |
| 180 | |
| 181 | if config.has_section('Config') and config.has_option('Config', 'font'): |
| 182 | self.font = config.get('Config', 'font') |
| 183 | logging.debug("Setting font to %s" % self.font) |
| 184 | |
| 185 | logging.debug('font %s', self.font) |
| 186 | |
163 | 187 | def size_allocate_cb(self, abi, alloc): |
164 | 188 | gobject.idle_add(abi.queue_draw) |
165 | 189 | |
… |
… |
class AbiWordActivity(activity.Activity): |
205 | 229 | keybindings_file, 0, 0) |
206 | 230 | # set default font |
207 | 231 | self.abiword_canvas.select_all() |
208 | | # get_selection return content and length |
209 | | if self.abiword_canvas.get_selection('text/plain')[1] == 0: |
210 | | logging.error('Setting default font to Sans in new documents') |
211 | | self.abiword_canvas.set_font_name('Sans') |
| 232 | # get_selection gives out random values of length for empty documents |
| 233 | # get_content returns content |
| 234 | if len(self.abiword_canvas.get_content('text/plain')) == 0: |
| 235 | logging.debug('Setting default font to %s in new documents' % self.font) |
| 236 | self.abiword_canvas.set_font_name(self.font) |
212 | 237 | self.abiword_canvas.moveto_bod() |
213 | 238 | |
214 | 239 | def get_preview(self): |
diff --git a/font.cfg b/font.cfg
new file mode 100644
index 0000000..f9e1c76
-
|
+
|
|
| 1 | [Config] |
| 2 | font = Sans |
| 3 | fontsize = 12 |
| 4 | |
diff --git a/widgets.py b/widgets.py
index f12544a..4d05a31 100644
a
|
b
|
import dbus |
17 | 17 | import time |
18 | 18 | from gettext import gettext as _ |
19 | 19 | import logging |
| 20 | import ConfigParser |
20 | 21 | |
21 | 22 | from sugar.graphics.radiotoolbutton import RadioToolButton |
22 | 23 | from sugar.graphics.combobox import ComboBox |
… |
… |
from sugar.graphics.palette import Palette |
24 | 25 | from sugar.graphics.toolbutton import ToolButton |
25 | 26 | from sugar.graphics.menuitem import MenuItem |
26 | 27 | from sugar.datastore import datastore |
| 28 | from sugar.activity.activity import get_bundle_path |
27 | 29 | |
28 | 30 | from sugar.activity.activity import SCOPE_PRIVATE |
29 | 31 | |
30 | 32 | logger = logging.getLogger('write-activity') |
| 33 | DEFAULT_CONFIG_PATH = get_bundle_path() + '/font.cfg' |
31 | 34 | |
32 | 35 | """ |
33 | 36 | # The FontCombo is not used anymore, keep the code here for reference |
… |
… |
class FontCombo(ComboBox): |
97 | 100 | class FontSizeCombo(ComboBox): |
98 | 101 | |
99 | 102 | def __init__(self, abi): |
100 | | ComboBox.__init__(self) |
| 103 | ComboBox.__init__(self) |
| 104 | |
| 105 | self.fontsize = '12' |
101 | 106 | |
102 | | self._abi_handler = abi.connect('font-size', self._font_size_cb) |
| 107 | self._abi_handler = abi.connect('font-size', self._font_size_cb) |
103 | 108 | |
104 | 109 | self._font_sizes = ['8', '9', '10', '11', '12', '14', '16', '20', \ |
105 | 110 | '22', '24', '26', '28', '36', '48', '72'] |
106 | 111 | self._changed_id = self.connect('changed', self._font_size_changed_cb, |
107 | 112 | abi) |
108 | 113 | |
| 114 | if os.path.exists('/etc/font.cfg'): |
| 115 | self._read_configuration('/etc/font.cfg') |
| 116 | elif os.path.exists(DEFAULT_CONFIG_PATH): |
| 117 | self._read_configuration(DEFAULT_CONFIG_PATH) |
| 118 | |
109 | 119 | for i, s in enumerate(self._font_sizes): |
110 | 120 | self.append_item(i, s, None) |
111 | | if s == '12': |
| 121 | if s == self.fontsize: |
112 | 122 | self.set_active(i) |
| 123 | |
| 124 | |
| 125 | def _read_configuration(self, file_name): |
| 126 | logging.debug('Reading configuration from file %s (for fontsize)' % file_name) |
| 127 | fp = open(file_name) |
| 128 | config = ConfigParser.ConfigParser() |
| 129 | config.readfp(fp) |
| 130 | fp.close() |
| 131 | |
| 132 | if config.has_section('Config') and config.has_option('Config', 'fontsize'): |
| 133 | self.fontsize = config.get('Config', 'fontsize') |
| 134 | if self.fontsize in self._font_sizes: |
| 135 | logging.debug("Setting fontsize to %s" % self.fontsize) |
| 136 | else: |
| 137 | self.fontsize = '12' |
| 138 | |
| 139 | logging.debug('font size %s', self.fontsize) |
113 | 140 | |
114 | 141 | def _font_size_changed_cb(self, combobox, abi): |
115 | 142 | if self.get_active() != -1: |