summaryrefslogtreecommitdiffstats
path: root/3rdparty/simplepie/demo/for_the_demo/source_files/sIFR-r245/SifrStyleSheet.as
blob: 6a98ca552261f2ed14117bce7388b252bbedfcb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*=:project
    scalable Inman Flash Replacement (sIFR) version 3.

  =:file
    Copyright: 2006 Mark Wubben.
    Author: Mark Wubben, <http://novemberborn.net/>

  =:history
    * IFR: Shaun Inman
    * sIFR 1: Mike Davidson, Shaun Inman and Tomas Jogin
    * sIFR 2: Mike Davidson, Shaun Inman, Tomas Jogin and Mark Wubben

  =:license
    This software is licensed and provided under the CC-GNU LGPL.
    See <http://creativecommons.org/licenses/LGPL/2.1/>    
*/

import TextField.StyleSheet;

class SifrStyleSheet extends TextField.StyleSheet {
  public var fontSize;
  public var latestLeading = 0;
  
  public function parseCSS(cssText:String) {
    var native = new TextField.StyleSheet();
    var parsed = native.parseCSS(cssText);
    
    if(!parsed) return false;
    
    var selectors = native.getStyleNames();
    for(var i = selectors.length - 1; i >= 0; i--) {
      var selector = selectors[i];
      var nativeStyle = native.getStyle(selector);
      var style = this.getStyle(selector) || nativeStyle;
      if(style != nativeStyle) {
        for(var property in nativeStyle) style[property] = nativeStyle[property];
      }
      this.setStyle(selector, style);
    }
    
    return true;
  }
  
  // Apply leading to the textFormat. Much thanks to <http://www.blog.lessrain.com/?p=98>.
  private function applyLeading(format, leading) {
    this.latestLeading = leading;
    
    if(leading >= 0) {
        format.leading = leading;
        return format;
    }

    // Workaround for negative leading, which is ignored otherwise.
    var newFormat = new TextFormat(null, null, null, null, null, null, null, null, null, null, null, null, leading);
    for(var property in format) if(property != 'leading') newFormat[property] = format[property];

    return newFormat;
  }
  
  public function transform(style) {
    var format = super.transform(style);
    if(style.leading) format = applyLeading(format, style.leading);
    if(style.letterSpacing) format.letterSpacing = style.letterSpacing;
    // Support font sizes relative to the size of .sIFR-root.
    if(this.fontSize && style.fontSize && style.fontSize.indexOf('%')) {
      format.size = this.fontSize * parseInt(style.fontSize) / 100;
    }
    format.kerning = _root.kerning == 'true' || !(_root.kerning == 'false') || sIFR.defaultKerning;
    return format;
  }
}