Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / cssutils / css / cssfontfacerule.py @ 475

History | View | Annotate | Download (7.02 KB)

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

3
From cssutils 0.9.6 additions from CSS Fonts Module Level 3 are
4
added http://www.w3.org/TR/css3-fonts/.
5
"""
6
__all__ = ['CSSFontFaceRule']
7
__docformat__ = 'restructuredtext'
8
__version__ = '$Id$'
9

    
10
from cssstyledeclaration import CSSStyleDeclaration
11
import cssrule
12
import cssutils
13
import xml.dom
14

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

21
    Format::
22

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

38
        :param style:
39
            CSSStyleDeclaration used to hold any font descriptions 
40
            for this CSSFontFaceRule
41
        """
42
        super(CSSFontFaceRule, self).__init__(parentRule=parentRule, 
43
                                              parentStyleSheet=parentStyleSheet)
44
        self._atkeyword = u'@font-face'
45
            
46
        if style:
47
            self.style = style
48
        else:
49
            self.style = CSSStyleDeclaration()
50
        
51
        self._readonly = readonly
52
        
53
    def __repr__(self):
54
        return u"cssutils.css.%s(style=%r)" % (
55
                self.__class__.__name__, 
56
                self.style.cssText)
57

    
58
    def __str__(self):
59
        return u"<cssutils.css.%s object style=%r valid=%r at 0x%x>" % (
60
                self.__class__.__name__, 
61
                self.style.cssText, 
62
                self.valid, 
63
                id(self))
64

    
65
    def _getCssText(self):
66
        """Return serialized property cssText."""
67
        return cssutils.ser.do_CSSFontFaceRule(self)
68

    
69
    def _setCssText(self, cssText):
70
        """
71
        :exceptions:
72
            - :exc:`~xml.dom.SyntaxErr`:
73
              Raised if the specified CSS string value has a syntax error and
74
              is unparsable.
75
            - :exc:`~xml.dom.InvalidModificationErr`:
76
              Raised if the specified CSS string value represents a different
77
              type of rule than the current one.
78
            - :exc:`~xml.dom.HierarchyRequestErr`:
79
              Raised if the rule cannot be inserted at this point in the
80
              style sheet.
81
            - :exc:`~xml.dom.NoModificationAllowedErr`:
82
              Raised if the rule is readonly.
83
        """
84
        super(CSSFontFaceRule, self)._setCssText(cssText)
85
        
86
        tokenizer = self._tokenize2(cssText)
87
        attoken = self._nexttoken(tokenizer, None)
88
        if self._type(attoken) != self._prods.FONT_FACE_SYM:
89
            self._log.error(u'CSSFontFaceRule: No CSSFontFaceRule found: %s' %
90
                self._valuestr(cssText),
91
                error=xml.dom.InvalidModificationErr)
92
        else:
93
            newStyle = CSSStyleDeclaration(parentRule=self)
94
            ok = True
95
            
96
            beforetokens, brace = self._tokensupto2(tokenizer, 
97
                                                    blockstartonly=True,
98
                                                    separateEnd=True)            
99
            if self._tokenvalue(brace) != u'{':
100
                ok = False
101
                self._log.error(u'CSSFontFaceRule: No start { of style '
102
                                u'declaration found: %r' 
103
                                % self._valuestr(cssText), brace)
104
            
105
            # parse stuff before { which should be comments and S only
106
            new = {'wellformed': True}
107
            newseq = self._tempSeq()
108
            
109
            beforewellformed, expected = self._parse(expected=':',
110
                seq=newseq, tokenizer=self._tokenize2(beforetokens),
111
                productions={})
112
            ok = ok and beforewellformed and new['wellformed']
113
    
114
            styletokens, braceorEOFtoken = self._tokensupto2(tokenizer, 
115
                                                             blockendonly=True,
116
                                                             separateEnd=True)
117

    
118
            val, type_ = self._tokenvalue(braceorEOFtoken),\
119
                         self._type(braceorEOFtoken)
120
            if val != u'}' and type_ != 'EOF':
121
                ok = False
122
                self._log.error(u'CSSFontFaceRule: No "}" after style '
123
                                u'declaration found: %r'
124
                                % self._valuestr(cssText))
125
                
126
            nonetoken = self._nexttoken(tokenizer)
127
            if nonetoken:
128
                ok = False
129
                self._log.error(u'CSSFontFaceRule: Trailing content found.',
130
                                token=nonetoken)
131

    
132
            if 'EOF' == type_:
133
                # add again as style needs it
134
                styletokens.append(braceorEOFtoken)
135

    
136
            # SET, may raise:
137
            newStyle.cssText = styletokens
138

    
139
            if ok:
140
                # contains probably comments only (upto ``{``)
141
                self._setSeq(newseq)
142
                self.style = newStyle                 
143
                
144
    cssText = property(_getCssText, _setCssText,
145
                       doc=u"(DOM) The parsable textual representation of this "
146
                           u"rule.")
147

    
148
    def _setStyle(self, style):
149
        """
150
        :param style:
151
            a CSSStyleDeclaration or string
152
        """
153
        self._checkReadonly()
154
        if isinstance(style, basestring):
155
            self._style = CSSStyleDeclaration(cssText=style, parentRule=self)
156
        else:
157
            style._parentRule = self
158
            self._style = style
159

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

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

    
168
    def _getValid(self):
169
        needed = ['font-family', 'src']
170
        for p in self.style.getProperties(all=True):
171
            if not p.valid:
172
                return False
173
            try:
174
                needed.remove(p.name)
175
            except ValueError:
176
                pass
177
        return not bool(needed)
178

    
179
    valid = property(_getValid, 
180
                     doc=u"CSSFontFace is valid if properties `font-family` "
181
                         u"and `src` are set and all properties are valid.")
182
    
183
    # constant but needed:
184
    wellformed = property(lambda self: True)