Permalink
Browse files

Updating documentation to be correct and bug fixes for styles.

  • Loading branch information...
1 parent c68e965 commit 1465c8a1d24b58566f102df18a592b82e501fde1 Paul Taylor committed May 21, 2011
@@ -10,7 +10,7 @@ package org.tinytlf.util.fte
/**
* Returns the index of the atom at a particular point. If the point
* is outside the boundaries of the line, this determines which side the
- * point is on, and returns 0 or atomCount - 1.
+ * point is on, and returns 0 or line.atomCount.
*/
public static function getAtomIndexAtPoint(line:TextLine, stageCoords:Point):int
{
@@ -7,7 +7,6 @@
package org.tinytlf.styles
{
import com.flashartofwar.fcss.styles.IStyle;
- import com.flashartofwar.fcss.utils.TypeHelperUtil;
import flash.system.Capabilities;
import flash.utils.flash_proxy;
@@ -19,16 +18,29 @@ package org.tinytlf.styles
public function FCSSStyleProxy(styleObject:Object = null)
{
super(styleObject);
+
+ if(styleObject is IStyle)
+ {
+ styleName = IStyle(styleObject).styleName;
+ }
}
- //Properties which can have their values set in em, pt, px, or % sizes.
- protected const specialSizeProperties:Object = {fontSize: true, lineHeight: true, wordSpacing: true, letterSpacing: true};
-
override protected function mergeProperty(property:String, source:Object):void
{
- if(property in specialSizeProperties)
+ var value:* = source[property];
+
+ if(value is String)
{
- this[property] = deriveFontSizeValue(source[property], this[property]);
+ if(valueIsEm(value))
+ this[property] = deriveValueFromBaseValue(value, this[property]);
+ else if(valueIsPoint(value))
+ this[property] = deriveValueFromPoint(value);
+ else if(valueIsPercent(value))
+ this[property] = deriveValueFromBaseValue(value, this[property]);
+ else if(valueIsPixel(value))
+ this[property] = deriveValueFromPixel(value);
+ else
+ super.mergeProperty(property, source);
}
else
{
@@ -56,68 +68,73 @@ package org.tinytlf.styles
return new FCSSStyleProxy(this);
}
- protected function deriveFontSizeValue(sizeValue:Object, baseFontSize:Number = NaN):Object
+ protected function valueIsPoint(value:String):Boolean
{
- var number:Number = baseFontSize;
-
- if(sizeValue is Number)
- {
- number = Number(sizeValue);
- }
- else if(sizeValue is String)
- {
- var s:String = String(sizeValue);
- if(/pt/i.test(s))
- {
- s = s.substring(0, s.indexOf('pt'));
- //a point is 1/72nd of an inch.
- number = (Number(s) / 72) * screenDPI;
- }
- else if(/em/i.test(s))
- {
- //If no font size was passed in, just store the sizeValue for later.
- if(baseFontSize != baseFontSize)
- return sizeValue;
-
- s = s.substring(0, s.indexOf('em'));
- number = baseFontSize * Number(s);
- }
- else if(/%/i.test(s))
- {
- //If no font size was passed in, just store the sizeValue for later.
- if(baseFontSize != baseFontSize)
- return sizeValue;
-
- s = s.substring(0, s.indexOf('%'));
- number = baseFontSize * Number(s) * .01;
- }
- else
- {
- if(/px/i.test(s))
- s = s.substring(0, s.indexOf('px'));
-
- number = Number(s);
- }
- }
+ return /pt/i.test(value);
+ }
+
+ protected function valueIsEm(value:String):Boolean
+ {
+ return /em/i.test(value);
+ }
+
+ protected function valueIsPercent(value:String):Boolean
+ {
+ return /%/i.test(value);
+ }
+
+ protected function valueIsPixel(value:String):Boolean
+ {
+ return /px/i.test(value);
+ }
+
+ protected function deriveValueFromPoint(value:String):Number
+ {
+ //a point is 1/72nd of an inch.
+ return (Number(value.substring(0, value.indexOf('pt'))) / 72) * screenDPI;
+ }
+
+ protected function deriveValueFromBaseValue(value:String, baseValue:Number = NaN):*
+ {
+ //If no base value was passed in, we can't derive an M-height.
+ if(baseValue != baseValue)
+ return value;
- return number;
+ return baseValue * Number(value.substring(0, value.indexOf('em')));
}
- //Put this here so we're not querying Capabilities every time.
+ protected function deriveValueFromPixel(value:String):Number
+ {
+ return Number(value.substring(0, value.indexOf('px')));
+ }
+
+ //Cache the screen DPI.
protected static const screenDPI:Number = Capabilities.screenDPI;
override flash_proxy function setProperty(name:*, value:*):void
{
//Convert any #FFFFFF values to 0xFFFFFF
if(value is String && String(value).indexOf("#") != -1)
- value = TypeHelperUtil.stringToUint(value);
+ {
+ value = uint('0x' + String(value).substring(1));
+ }
super.setProperty(name, value);
}
public function merge(obj:Object):void
{
mergeWith(obj);
+
+ if(obj is IStyle)
+ {
+ styleName = IStyle(obj).styleName;
+ }
+ }
+
+ override public function toString():String
+ {
+ return styleName + ' ' + super.toString();
}
}
}
@@ -6,52 +6,37 @@
*/
package org.tinytlf.styles
{
- import com.flashartofwar.fcss.styles.IStyle;
- import com.flashartofwar.fcss.stylesheets.FStyleSheet;
+ import com.flashartofwar.fcss.styles.*;
+ import com.flashartofwar.fcss.stylesheets.*;
import flash.text.engine.*;
public class FCSSTextStyler extends TextStyler
{
[Embed(source="default.css", mimeType="application/octet-stream")]
- private const defaultCSS:Class;
+ private var defaultCSS:Class;
public function FCSSTextStyler()
{
style = new defaultCSS().toString();
// A list of non-inheriting styles. If a style isn't in this list,
// it's assumed to be an inheriting style.
- setStyle('nonInheritingStyles',
+ setStyle('nonInheritingStyles',
{
- margin:null, marginLeft: null, marginRight:null, marginTop:null,
- padding: null, paddingLeft: null, paddingRight:null, paddingTop:null,
+ margin: null, marginLeft: null, marginRight: null, marginTop: null,
+ padding: null, paddingLeft: null, paddingRight: null, paddingTop: null,
width: null, height: null, 'class': null, id: null, style: null
});
}
- private var sheet:FStyleSheet;
- private var stylesheet:String;
+ private const styleSheet:TinytlfStyleSheet = new TinytlfStyleSheet();
override public function set style(value:Object):void
{
if(value is String)
{
- if(!sheet)
- sheet = new TinytlfStyleSheet();
-
- stylesheet = String(value);
- sheet.parseCSS(stylesheet);
-
- //Add the global styles onto this ITextStyler dude.
- if(super.style is FCSSStyleProxy)
- {
- mergeWith(new FCSSStyleProxy(sheet.getStyle("*")));
- value = super.style;
- }
- else
- {
- value = new FCSSStyleProxy(sheet.getStyle("*"));
- }
+ styleSheet.parseCSS(String(value));
+ value = styleSheet.getStyle("*");
}
super.style = value;
@@ -77,29 +62,26 @@ package org.tinytlf.styles
if(element is String)
element = String(element).split(' ');
if(element is Array)
- return sheet.getStyle.apply(null, element as Array);
+ return styleSheet.getStyle.apply(null, element as Array);
return super.describeElement(element);
}
override public function toString():String
{
- if(stylesheet)
- return stylesheet;
-
- return super.toString();
+ return styleSheet.toString();
}
}
}
-import com.flashartofwar.fcss.applicators.AbstractApplicator;
-import com.flashartofwar.fcss.styles.IStyle;
-import com.flashartofwar.fcss.stylesheets.FStyleSheet;
-import com.flashartofwar.fcss.utils.TypeHelperUtil;
+import com.flashartofwar.fcss.applicators.*;
+import com.flashartofwar.fcss.styles.*;
+import com.flashartofwar.fcss.stylesheets.*;
+import com.flashartofwar.fcss.utils.*;
import flash.text.engine.*;
-import org.tinytlf.styles.FCSSStyleProxy;
+import org.tinytlf.styles.*;
internal class EFApplicator extends AbstractApplicator
{
@@ -173,6 +155,36 @@ internal class FDApplicator extends AbstractApplicator
internal class TinytlfStyleSheet extends FStyleSheet
{
+ override public function parseCSS(cssText:String, useCSSTidy:Boolean = true):IStyleSheet
+ {
+ cachedstyles.length = 0;
+
+ indexCSS(useCSSTidy ? tidy(cssText) : cssText);
+
+ // Force @variables to cache
+ getStyle("@variables");
+
+ this.cssText = '';
+
+ return this;
+ }
+
+ override public function toString():String
+ {
+ if(cssText != '')
+ return cssText;
+
+ var s:String = '';
+ var o:Object;
+
+ for each(var styleName:String in styleNames)
+ {
+ s += styleLookup(styleName, false).toString();
+ }
+
+ return cssText = s;
+ }
+
override protected function createEmptyStyle():IStyle
{
return new FCSSStyleProxy();

0 comments on commit 1465c8a

Please sign in to comment.