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 -class Font(wx.Font):
     23
     24 -    def __init__(self, point_size=12, family=wx.FONTFAMILY_DEFAULT, style=wx.FONTSTYLE_NORMAL,
     25                   weight=wx.FONTWEIGHT_NORMAL, underlined=False, face_name="", encoding=wx.FONTENCODING_DEFAULT,
     26                   wxcolor=wx.BLACK):
     27          self.wxcolor = wxcolor
     28          wx.Font.__init__(self, point_size, family, style, weight, underlined, face_name, encoding)
     29
     30 -    def _get_wxcolor(self):
     31          return self.wxcolor
     32
     33 -    def _set_wxcolor(self, wxcolor):
     34          self.wxcolor = wxcolor
     35
     36      WxColor = property(_get_wxcolor, _set_wxcolor)
     37
     38 -    def _get_wxfont(self):
     39          return self
     40
     41 -    def _set_wxfont(self, wxfont):
     42          self.PointSize = wxfont.PointSize
     43          self.Family = wxfont.Family
     44          self.Style = wxfont.Style
     45          self.Weight = wxfont.Weight
     46          self.SetUnderlined(wxfont.GetUnderlined())
     47          self.FaceName = wxfont.FaceName
     48          self.Encoding = wxfont.Encoding
     49
     50      WxFont = property(_get_wxfont, _set_wxfont)
     51
     52 -    def serialize(self):
     53          return "%s:%s:%s:%s:%s:%s:%s:%s" % (
     54              self.PointSize,
     55              self.Family,
     56              self.Style,
     57              self.Weight,
     58              self.GetUnderlined(),
     59              self.FaceName,
     60              self.Encoding,
     61              self.WxColor,
     62          )
     63
     64 -    def increment(self, step=2):
     65          self.PointSize += step
     66
     67 -    def decrement(self, step=2):
     68          self.PointSize -= step
     69
     70
     71  # Profiling of timelinelib\wxgui\components\font.py:63(deserialize_font)
     72  #
     73  # Open Timeline and drag-scroll 10 times back and forth with the mouse.
     74  #
     75  # Before caching Font info:
     76  #          ncalls  tottime  percall  cumtime  percall
     77  # Try 1:     4265    0.154    0.000    0.610    0.000
     78  # Try 2:     4395    0.154    0.000    0.613    0.000
     79  # Try 3:     3801    0.133    0.000    0.528    0.000
     80  # Try 4:     4430    0.152    0.000    0.607    0.000
     81  # Try 5:     3926    0.139    0.000    0.553    0.000
     82  #
     83  # After caching Font info:
     84  # Try 1:     3894    0.004    0.000    0.004    0.000
     85  # Try 2:     5246    0.005    0.000    0.005    0.000
     86  # Try 3:     4972    0.004    0.000    0.005    0.000
     87  # Try 4:     4611    0.004    0.000    0.005    0.000
     88  # Try 5:     3306    0.003    0.000    0.003    0.000
     89
     90  font_cache = {}
     91
     92
     93 -def deserialize_font(serialized_font):
     94      if serialized_font not in font_cache:
     95          bool_map = {"True": True, "False": False}
     96          (
     97              point_size,
     98              family,
     99              style,
    100              weight,
    101              underlined,
    102              facename,
    103              encoding,
    104              color,
    105          ) = serialized_font.split(":")
    106          color_args = color[1:-1].split(",")
    107          wxcolor = wx.Colour(
    108              int(color_args[0]),
    109              int(color_args[1]),
    110              int(color_args[2]),
    111              int(color_args[3])
    112          )
    113          font = Font(
    114              int(point_size),
    115              int(family),
    116              int(style),
    117              int(weight),
    118              bool_map[underlined],
    119              facename,
    120              int(encoding),
    121              wxcolor
    122          )
    123          font_cache[serialized_font] = font
    124      return font_cache[serialized_font]
    125
    126
    127 -def set_minor_strip_text_font(font, dc, force_bold=False, force_normal=False, force_italic=False, force_upright=False):
    128      set_text_font(font, dc, force_bold, force_normal, force_italic, force_upright)
    129
    130
    131 -def set_major_strip_text_font(font, dc, force_bold=False, force_normal=False, force_italic=False, force_upright=False):
    132      set_text_font(font, dc, force_bold, force_normal, force_italic, force_upright)
    133
    134
    135 -def set_balloon_text_font(font, dc, force_bold=False, force_normal=False, force_italic=False, force_upright=False):
    136      set_text_font(font, dc, force_bold, force_normal, force_italic, force_upright)
    137
    138
    139 -def set_legend_text_font(font, dc):
    140      set_text_font(font, dc)
    141
    142
    143 -def set_text_font(selectable_font, dc, force_bold=False, force_normal=False, force_italic=False, force_upright=False):
    144      font = deserialize_font(selectable_font)
    145      old_weight = font.Weight
    146      old_style = font.Style
    147      if force_bold:
    148          font.Weight = wx.FONTWEIGHT_BOLD
    149      elif force_normal:
    150          font.Weight = wx.FONTWEIGHT_NORMAL
    151      if force_italic:
    152          font.Style = wx.FONTSTYLE_ITALIC
    153      elif force_upright:
    154          font.Style = wx.FONTSTYLE_NORMAL
    155      dc.SetFont(font)
    156      dc.SetTextForeground(font.WxColor)
    157      font.Style = old_style
    158      font.Weight = old_weight
    159
    160
    161 -def edit_font_data(parent_window, font):
    162      data = wx.FontData()
    163      data.SetInitialFont(font)
    164      data.SetColour(font.WxColor)
    165      dialog = wx.FontDialog(parent_window, data)
    166      try:
    167          if dialog.ShowModal() == wx.ID_OK:
    168              font_data = dialog.GetFontData()
    169              font.WxFont = font_data.GetChosenFont()
    170              font.WxColor = font_data.GetColour()
    171              return True
    172          else:
    173              return False
    174      finally:
    175          dialog.Destroy()
    176