# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007-2008 by Infiscape Corporation
#
# This program 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 2 of the License, or (at your option)
# any later version.
#
# This program 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 GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import re

import opendocument
import opendocument.core as core
import opendocument.drawing as draw
od = opendocument
AI = od.AttributeInfo
style_base = opendocument.bases.style

class Style(style_base.Style):
   __metaclass__ = od.OpenDocumentMeta
   childTypes = ['vpresent:page3d-properties',]
   overrides = {'style:family':
      AI(values=[u'text', u'paragraph', u'section', u'ruby', u'table',
                 u'table-column', u'table-row', u'table-cell', u'graphic',
                 u'presentation', u'drawing-page', u'chart', u'page3d'])}

MANGLE_NAME_EX   = re.compile(r'(\W)')
DEMANGLE_NAME_EX = re.compile(r'_(\d\d)_')

def mangleStyleName(name):
   def convert(matchObj):
      return "_%02x_" % ord(matchObj.group(1))

   return MANGLE_NAME_EX.sub(convert, name)

def demangleStyleName(name):
   def convert(matchObj):
      return matchObj.group(1).decode('hex')

   return DEMANGLE_NAME_EX.sub(convert, name)



class ParagraphProperties(style_base.ParagraphProperties):
   __metaclass__ = od.OpenDocumentMeta
   childTypes=['style:tab-stops',]

class GraphicProperties(style_base.GraphicProperties):
   __metaclass__ = od.OpenDocumentMeta
   childTypes=['text:list-style',]

bad = \
"""
class FontFace(opendocument.DocumentNode):
   def __init__(self, attrs):
      opendocument.DocumentNode.__init__(self, attrs)

      # Attributes are:
      #    style:name                - Font name (required)
      #    svg:font-family           - Font family (optional)
      #    style:font-family-generic - Generic font class (required?)
      #    style:font-pitch          - Either "fixed" or "variable" (optional)
      #    style:font-charset        - Encoding (optional)
      #    svg:font-stretch          -
      #    svg:unitsPerEm            -
      #    svg:ascent                -

      self.name       = attrs['style:name'].strip()
      self.family     = attrs.get('svg:font-family', None)
      self.pitch      = attrs.get('style:font-pitch', None)
      self.charset    = attrs.get('style:font-charset', None)
      self.stretch    = attrs.get('svg:font-stretch', None)
      self.unitsPerEm = attrs.get('svg:units-per-em', None)
      self.ascent     = attrs.get('svg:ascent', None)

      self.setFontFamilyGeneric(
         attrs['style:font-family-generic'].strip().lower()
      )

   def getName(self):
      return self.name

   def getFamily(self):
      return self.family

   def getFamilyGeneric(self):
      return self.familyGeneric

   def setFamilyGeneric(self, family):
      valid_families = ['roman', 'swiss', 'modern', 'decorative', 'script',
                        'system']

      family = family.strip().lower()
      if family not in valid_families:
         family = 'swiss'

      self.familyGeneric = family
      self.setAttribute('style:font-family-generic', family)

   def getPitch(self):
      return self.pitch

   def setPitch(self, pitch):
      valid_pitches = ['fixed', 'variable']

      pitch = pitch.strip().lower()
      if pitch not in valid_pitches:
         pitch = 'variable'

      self.pitch = pitch
      self.setAttribute('style:font-pitch', pitch)

   def getCharset(self):
      return self.charset

   def getStrecth(self):
      return self.stretch

   def getUnitsPerEm(self):
      return self.unitsPerEm

   def getAscent(self):
      return self.ascent

class HandoutMaster(opendocument.DocumentNode):
   #opendocument.DocumentNode.__init__(self, attrs, [draw.Shape])
   def __init__(self, attrs):
      opendocument.DocumentNode.__init__(self, attrs, [])

      self.presentationPageLayoutName = \
         attrs.get('presentation:presentation-page-layout-name', None)
      self.pageLayoutName = attrs['style:page-layout-name']
      self.styleName      = attrs.get('draw:style-name', None)
      self.headerName     = attrs.get('presentation:use-header-name', None)
      self.footerName     = attrs.get('presentation:use-footer-name', None)
      self.dateTimeName   = attrs.get('presentation:use-date-time-name', None)

   def getPresentationPageLayoutName(self):
      return self.presentationPageLayoutName

   def getPageLayoutName(self):
      return self.pageLayoutName

   def getStyleName(self):
      return self.styleName

   def getHeaderName(self):
      return self.headerName

   def getFooterName(self):
      return self.footerName

   def getDateTimeName(self):
      return self.dateTimeName

class MasterPage(opendocument.DocumentNode):
   #[draw.Shape, Style, core.Forms])
   def __init__(self, attrs):
      opendocument.DocumentNode.__init__(self, attrs, [])
      print "[TODO] Handle all style.MasterPage attributes"

class DefaultStyle(opendocument.DocumentNode):
   def __init__(self, attrs):
      opendocument.DocumentNode.__init__(self, attrs)

      # Attributes are:
      #    style:name   - Style name (required)
      #    style:family - Style family (required)

      self.name = attrs['style:name']
      self.setFamily(attrs['style:family'])

   def getName(self):
      return demangleStyleName(self.name)

   def setName(self, name):
      name = mangleStyleName(name)
      self.name = name
      self.setAttribute('style:name', name)

   def getFamily(self):
      return self.family

   def setFamily(self, family):
      valid_families = ['text', 'paragraph', 'section', 'table',
                        'table-column', 'table-row', 'table-cell',
                        'table-page', 'chart', 'graphics', 'default',
                        'drawing-page', 'presentation', 'control', 'ruby']

      family = family.strip().lower()
      if family not in valid_families:
         family = 'default'

      self.family = family
      self.setAttribute('style:family', family)

class Style(opendocument.DocumentNode):
   def __init__(self, attrs):
      opendocument.DocumentNode.__init__(self, attrs)
      print "[TODO] Handle all style.Style attributes"
"""
