Gnumed.timelinelib.wxgui.components.font

Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Module font |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.components.font

      1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  import wx
     20
     21
     22  FONT_FACE_ENCODING = "utf-8"
     23
     24
     25 -class Font(wx.Font):
     26
     27 -    def __init__(self, point_size=12, family=wx.FONTFAMILY_DEFAULT, style=wx.FONTSTYLE_NORMAL,
     28                   weight=wx.FONTWEIGHT_NORMAL, underlined=False, face_name="", encoding=wx.FONTENCODING_DEFAULT,
     29                   wxcolor=wx.BLACK):
     30          self.wxcolor = wxcolor
     31          wx.Font.__init__(self, point_size, family, style, weight, underlined, face_name, encoding)
     32
     33 -    def _get_wxcolor(self):
     34          return self.wxcolor
     35
     36 -    def _set_wxcolor(self, wxcolor):
     37          self.wxcolor = wxcolor
     38
     39      WxColor = property(_get_wxcolor, _set_wxcolor)
     40
     41 -    def _get_wxfont(self):
     42          return self
     43
     44 -    def _set_wxfont(self, wxfont):
     45          self.PointSize = wxfont.PointSize
     46          self.Family = wxfont.Family
     47          self.Style = wxfont.Style
     48          self.Weight = wxfont.Weight
     49          self.SetUnderlined(wxfont.GetUnderlined())
     50          self.FaceName = wxfont.FaceName
     51          self.Encoding = wxfont.Encoding
     52
     53      WxFont = property(_get_wxfont, _set_wxfont)
     54
     55 -    def serialize(self):
     56          return "%s:%s:%s:%s:%s:%s:%s:%s" % (
     57              self.PointSize,
     58              self.Family,
     59              self.Style,
     60              self.Weight,
     61              self.GetUnderlined(),
     62              self.FaceName.encode(FONT_FACE_ENCODING),
     63              self.Encoding,
     64              self.WxColor,
     65          )
     66
     67 -    def increment(self, step=2):
     68          self.PointSize += step
     69
     70 -    def decrement(self, step=2):
     71          self.PointSize -= step
     72
     73
     74  # Profiling of timelinelib\wxgui\components\font.py:63(deserialize_font)
     75  #
     76  # Open Timeline and drag-scroll 10 times back and forth with the mouse.
     77  #
     78  # Before caching Font info:
     79  #          ncalls  tottime  percall  cumtime  percall
     80  # Try 1:     4265    0.154    0.000    0.610    0.000
     81  # Try 2:     4395    0.154    0.000    0.613    0.000
     82  # Try 3:     3801    0.133    0.000    0.528    0.000
     83  # Try 4:     4430    0.152    0.000    0.607    0.000
     84  # Try 5:     3926    0.139    0.000    0.553    0.000
     85  #
     86  # After caching Font info:
     87  # Try 1:     3894    0.004    0.000    0.004    0.000
     88  # Try 2:     5246    0.005    0.000    0.005    0.000
     89  # Try 3:     4972    0.004    0.000    0.005    0.000
     90  # Try 4:     4611    0.004    0.000    0.005    0.000
     91  # Try 5:     3306    0.003    0.000    0.003    0.000
     92
     93  font_cache = {}
     94
     95
     96 -def deserialize_font(serialized_font):
     97      if serialized_font not in font_cache:
     98          bool_map = {"True": True, "False": False}
     99          (
    100              point_size,
    101              family,
    102              style,
    103              weight,
    104              underlined,
    105              facename,
    106              encoding,
    107              color,
    108          ) = serialized_font.split(":")
    109          color_args = color[1:-1].split(",")
    110          wxcolor = wx.Colour(
    111              int(color_args[0]),
    112              int(color_args[1]),
    113              int(color_args[2]),
    114              int(color_args[3])
    115          )
    116          font = Font(
    117              int(point_size),
    118              int(family),
    119              int(style),
    120              int(weight),
    121              bool_map[underlined],
    122              facename.decode(FONT_FACE_ENCODING),
    123              int(encoding),
    124              wxcolor
    125          )
    126          font_cache[serialized_font] = font
    127      return font_cache[serialized_font]
    128
    129
    130 -def set_minor_strip_text_font(font, dc, force_bold=False, force_normal=False, force_italic=False, force_upright=False):
    131      set_text_font(font, dc, force_bold, force_normal, force_italic, force_upright)
    132
    133
    134 -def set_major_strip_text_font(font, dc, force_bold=False, force_normal=False, force_italic=False, force_upright=False):
    135      set_text_font(font, dc, force_bold, force_normal, force_italic, force_upright)
    136
    137
    138 -def set_balloon_text_font(font, dc, force_bold=False, force_normal=False, force_italic=False, force_upright=False):
    139      set_text_font(font, dc, force_bold, force_normal, force_italic, force_upright)
    140
    141
    142 -def set_legend_text_font(font, dc):
    143      set_text_font(font, dc)
    144
    145
    146 -def set_text_font(selectable_font, dc, force_bold=False, force_normal=False, force_italic=False, force_upright=False):
    147      font = deserialize_font(selectable_font)
    148      old_weight = font.Weight
    149      old_style = font.Style
    150      if force_bold:
    151          font.Weight = wx.FONTWEIGHT_BOLD
    152      elif force_normal:
    153          font.Weight = wx.FONTWEIGHT_NORMAL
    154      if force_italic:
    155          font.Style = wx.FONTSTYLE_ITALIC
    156      elif force_upright:
    157          font.Style = wx.FONTSTYLE_NORMAL
    158      dc.SetFont(font)
    159      dc.SetTextForeground(font.WxColor)
    160      font.Style = old_style
    161      font.Weight = old_weight
    162
    163
    164 -def edit_font_data(parent_window, font):
    165      data = wx.FontData()
    166      data.SetInitialFont(font)
    167      data.SetColour(font.WxColor)
    168      dialog = wx.FontDialog(parent_window, data)
    169      try:
    170          if dialog.ShowModal() == wx.ID_OK:
    171              font_data = dialog.GetFontData()
    172              font.WxFont = font_data.GetChosenFont()
    173              font.WxColor = font_data.GetColour()
    174              return True
    175          else:
    176              return False
    177      finally:
    178          dialog.Destroy()
    179