Logo Search packages:      
Sourcecode: calibre version File versions  Download package

cssfontfacerule.py

00001 """CSSFontFaceRule implements DOM Level 2 CSS CSSFontFaceRule.

From cssutils 0.9.6 additions from CSS Fonts Module Level 3 are
added http://www.w3.org/TR/css3-fonts/.
"""
__all__ = ['CSSFontFaceRule']
__docformat__ = 'restructuredtext'
__version__ = '$Id: cssfontfacerule.py 1868 2009-10-17 19:36:54Z cthedot $'

from cssstyledeclaration import CSSStyleDeclaration
import cssrule
import cssutils
import xml.dom

00015 class CSSFontFaceRule(cssrule.CSSRule):
    """
    The CSSFontFaceRule interface represents a @font-face rule in a CSS
    style sheet. The @font-face rule is used to hold a set of font 
    descriptions.

    Format::

        font_face
          : FONT_FACE_SYM S*
            '{' S* declaration [ ';' S* declaration ]* '}' S*
          ;
          
    cssutils uses a :class:`~cssutils.css.CSSStyleDeclaration`  to
    represent the font descriptions. For validation a specific profile
    is used though were some properties have other valid values than
    when used in e.g. a :class:`~cssutils.css.CSSStyleRule`.
    """
00033     def __init__(self, style=None, parentRule=None, 
                 parentStyleSheet=None, readonly=False):
        """
        If readonly allows setting of properties in constructor only.

        :param style:
            CSSStyleDeclaration used to hold any font descriptions 
            for this CSSFontFaceRule
        """
        super(CSSFontFaceRule, self).__init__(parentRule=parentRule, 
                                              parentStyleSheet=parentStyleSheet)
        self._atkeyword = u'@font-face'
        self._style = CSSStyleDeclaration(parentRule=self)
        if style:
            self.style = style
        
        self._readonly = readonly
        
    def __repr__(self):
        return "cssutils.css.%s(style=%r)" % (
                self.__class__.__name__, self.style.cssText)

    def __str__(self):
        return "<cssutils.css.%s object style=%r valid=%r at 0x%x>" % (
                self.__class__.__name__, self.style.cssText, self.valid, 
                id(self))

00060     def _getCssText(self):
        """Return serialized property cssText."""
        return cssutils.ser.do_CSSFontFaceRule(self)

00064     def _setCssText(self, cssText):
        """
        :exceptions:
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified CSS string value has a syntax error and
              is unparsable.
            - :exc:`~xml.dom.InvalidModificationErr`:
              Raised if the specified CSS string value represents a different
              type of rule than the current one.
            - :exc:`~xml.dom.HierarchyRequestErr`:
              Raised if the rule cannot be inserted at this point in the
              style sheet.
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if the rule is readonly.
        """
        super(CSSFontFaceRule, self)._setCssText(cssText)
        
        tokenizer = self._tokenize2(cssText)
        attoken = self._nexttoken(tokenizer, None)
        if self._type(attoken) != self._prods.FONT_FACE_SYM:
            self._log.error(u'CSSFontFaceRule: No CSSFontFaceRule found: %s' %
                self._valuestr(cssText),
                error=xml.dom.InvalidModificationErr)
        else:
            # save if parse goes wrong
            oldstyle = CSSStyleDeclaration()
            oldstyle._absorb(self.style)
            
            ok = True
            beforetokens, brace = self._tokensupto2(tokenizer, 
                                                    blockstartonly=True,
                                                    separateEnd=True)            
            if self._tokenvalue(brace) != u'{':
                ok = False
                self._log.error(
                    u'CSSFontFaceRule: No start { of style declaration found: %r' %
                    self._valuestr(cssText), brace)
            
            # parse stuff before { which should be comments and S only
            new = {'wellformed': True}
            newseq = self._tempSeq()#[]
            
            beforewellformed, expected = self._parse(expected=':',
                seq=newseq, tokenizer=self._tokenize2(beforetokens),
                productions={})
            ok = ok and beforewellformed and new['wellformed']
    
            styletokens, braceorEOFtoken = self._tokensupto2(tokenizer, 
                                                             blockendonly=True,
                                                             separateEnd=True)

            val, typ = self._tokenvalue(braceorEOFtoken), self._type(braceorEOFtoken)
            if val != u'}' and typ != 'EOF':
                ok = False
                self._log.error(
                    u'CSSFontFaceRule: No "}" after style declaration found: %r' %
                    self._valuestr(cssText))
                
            nonetoken = self._nexttoken(tokenizer)
            if nonetoken:
                ok = False
                self._log.error(u'CSSFontFaceRule: Trailing content found.',
                                token=nonetoken)

            if 'EOF' == typ:
                # add again as style needs it
                styletokens.append(braceorEOFtoken)

            # SET, may raise:
            self.style.cssText = styletokens

            if ok:
                # contains probably comments only (upto ``{``)
                self._setSeq(newseq)                
            else:
                # RESET
                self.style._absorb(oldstyle)
                

    cssText = property(_getCssText, _setCssText,
        doc="(DOM) The parsable textual representation of this rule.")

00146     def _setStyle(self, style):
        """
        :param style:
            a CSSStyleDeclaration or string
        """
        self._checkReadonly()
        if isinstance(style, basestring):
            self._style.cssText = style
        else:
            self._style = style
            self._style.parentRule = self

    style = property(lambda self: self._style, _setStyle,
                     doc="(DOM) The declaration-block of this rule set, "
                         "a :class:`~cssutils.css.CSSStyleDeclaration`.")

    type = property(lambda self: self.FONT_FACE_RULE, 
                    doc="The type of this rule, as defined by a CSSRule "
                        "type constant.")

    def _getValid(self):
        needed = ['font-family', 'src']
        for p in self.style.getProperties(all=True):
            if not p.valid:
                return False
            try:
                needed.remove(p.name)
            except ValueError:
                pass
        return not bool(needed)

    valid = property(_getValid, doc='CSSFontFace is valid if properties '
                     '`font-family` and `src` are set and all properties are '
                     'valid.')
    
    # constant but needed:
    wellformed = property(lambda self: True)

Generated by  Doxygen 1.6.0   Back to index