Ticket #1690: 0001-Retain-font-family-and-font-size.patch

File 0001-Retain-font-family-and-font-size.patch, 5.9 KB (added by lionaneesh, 11 years ago)

Patch, Note: This is a GCI task (http://www.google-melange.com/gci/task/view/google/gci2012/7950210)

  • AbiWordActivity.py

    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  
    1919from gettext import gettext as _
    2020import logging
    2121import os
     22import ConfigParser
    2223
    2324# Abiword needs this to happen as soon as possible
    2425import gobject
    gobject.threads_init() 
    2728import gtk
    2829import telepathy
    2930import telepathy.client
     31import os
     32
    3033
    3134from abiword import Canvas
    3235
    import speech 
    5356from speechtoolbar import SpeechToolbar
    5457
    5558logger = logging.getLogger('write-activity')
     59DEFAULT_CONFIG_PATH = get_bundle_path() + '/font.cfg'
    5660
    5761
    5862class AbiWordActivity(activity.Activity):
    class AbiWordActivity(activity.Activity): 
    6064    def __init__(self, handle):
    6165        activity.Activity.__init__(self, handle)
    6266
     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
    6374        # abiword uses the current directory for all its file dialogs
    6475        os.chdir(os.path.expanduser('~'))
    6576
    class AbiWordActivity(activity.Activity): 
    160171
    161172        self.abiword_canvas.connect('size-allocate', self.size_allocate_cb)
    162173
     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
    163187    def size_allocate_cb(self, abi, alloc):
    164188        gobject.idle_add(abi.queue_draw)
    165189
    class AbiWordActivity(activity.Activity): 
    205229                keybindings_file, 0, 0)
    206230        # set default font
    207231        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)
    212237        self.abiword_canvas.moveto_bod()
    213238
    214239    def get_preview(self):
  • new file font.cfg

    diff --git a/font.cfg b/font.cfg
    new file mode 100644
    index 0000000..f9e1c76
    - +  
     1[Config]
     2font = Sans
     3fontsize = 12
     4
  • widgets.py

    diff --git a/widgets.py b/widgets.py
    index f12544a..4d05a31 100644
    a b import dbus 
    1717import time
    1818from gettext import gettext as _
    1919import logging
     20import ConfigParser
    2021
    2122from sugar.graphics.radiotoolbutton import RadioToolButton
    2223from sugar.graphics.combobox import ComboBox
    from sugar.graphics.palette import Palette 
    2425from sugar.graphics.toolbutton import ToolButton
    2526from sugar.graphics.menuitem import MenuItem
    2627from sugar.datastore import datastore
     28from sugar.activity.activity import get_bundle_path
    2729
    2830from sugar.activity.activity import SCOPE_PRIVATE
    2931
    3032logger = logging.getLogger('write-activity')
     33DEFAULT_CONFIG_PATH = get_bundle_path() + '/font.cfg'
    3134
    3235"""
    3336# The FontCombo is not used anymore, keep the code here for reference
    class FontCombo(ComboBox): 
    97100class FontSizeCombo(ComboBox):
    98101
    99102    def __init__(self, abi):
    100         ComboBox.__init__(self)
     103        ComboBox.__init__(self)
     104
     105        self.fontsize = '12'
    101106
    102         self._abi_handler = abi.connect('font-size', self._font_size_cb)
     107        self._abi_handler = abi.connect('font-size', self._font_size_cb)
    103108
    104109        self._font_sizes = ['8', '9', '10', '11', '12', '14', '16', '20', \
    105110                            '22', '24', '26', '28', '36', '48', '72']
    106111        self._changed_id = self.connect('changed', self._font_size_changed_cb,
    107112                abi)
    108113
     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
    109119        for i, s in enumerate(self._font_sizes):
    110120            self.append_item(i, s, None)
    111             if s == '12':
     121            if s == self.fontsize:
    112122                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)
    113140
    114141    def _font_size_changed_cb(self, combobox, abi):
    115142        if self.get_active() != -1: