Skip to content

Commit

Permalink
avm2: Check the text.engine.FontDescription properties before setting
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpie authored and torokati44 committed Dec 26, 2023
1 parent 061b7f9 commit 28a3c71
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
36 changes: 34 additions & 2 deletions core/src/avm2/globals/flash/text/engine/FontDescription.as
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ package flash.text.engine {

private var _cffHinting:String;



public function FontDescription(fontName:String = "_serif", fontWeight:String = "normal", fontPosture:String = "normal",
fontLookup:String = "device", renderingMode:String = "cff", cffHinting:String = "horizontalStem") {
this.fontName = fontName;
Expand All @@ -31,6 +29,8 @@ package flash.text.engine {
}

public function set fontName(value:String):void {
if (value == null) throwNonNull("fontName");

this._fontName = value;
}

Expand All @@ -39,6 +39,11 @@ package flash.text.engine {
}

public function set fontWeight(value:String):void {
if (value == null) throwNonNull("fontWeight");
if (value != FontWeight.NORMAL && value != FontWeight.BOLD) {
throwNotAccepted("fontWeight");
}

this._fontWeight = value;
}

Expand All @@ -47,6 +52,11 @@ package flash.text.engine {
}

public function set fontPosture(value:String):void {
if (value == null) throwNonNull("fontPosture");
if (value != FontPosture.NORMAL && value != FontPosture.ITALIC) {
throwNotAccepted("fontPosture");
}

this._fontPosture = value;
}

Expand All @@ -55,6 +65,11 @@ package flash.text.engine {
}

public function set fontLookup(value:String):void {
if (value == null) throwNonNull("fontLookup");
if (value != FontLookup.DEVICE && value != FontLookup.EMBEDDED_CFF) {
throwNotAccepted("fontLookup");
}

this._fontLookup = value;
}

Expand All @@ -63,6 +78,11 @@ package flash.text.engine {
}

public function set renderingMode(value:String):void {
if (value == null) throwNonNull("renderingMode");
if (value != RenderingMode.NORMAL && value != RenderingMode.CFF) {
throwNotAccepted("renderingMode");
}

this._renderingMode = value;
}

Expand All @@ -71,6 +91,11 @@ package flash.text.engine {
}

public function set cffHinting(value:String):void {
if (value == null) throwNonNull("cffHinting");
if (value != CFFHinting.NONE && value != CFFHinting.HORIZONTAL_STEM) {
throwNotAccepted("cffHinting");
}

this._cffHinting = value;
}

Expand All @@ -79,5 +104,12 @@ package flash.text.engine {
return false;
}

private static function throwNonNull(name: String) {
throw new TypeError("Error #2007: Parameter " + name + " must be non-null.", 2007);
}

private static function throwNotAccepted(name: String) {
throw new ArgumentError("Error #2008: Parameter " + name + " must be one of the accepted values.", 2008);
}
}
}
52 changes: 52 additions & 0 deletions tests/tests/swfs/avm2/text_engine_fontdescription/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package {
import flash.display.Sprite;
import flash.text.engine.*;

public class Test extends Sprite {
public function Test():void {
var font = new FontDescription();

var properties = ["cffHinting", "fontLookup", "fontName", "fontPosture", "fontWeight", "renderingMode"];
for each (var prop in properties) {
trace("// FontDescription::" + prop)

try {
font[prop] = null;
} catch (e) {
trace(e);
}

try {
font[prop] = "<invalid>";
} catch (e) {
trace(e);
}
}

font.cffHinting = CFFHinting.NONE;
trace("// cffHinting: " + font.cffHinting);
font.cffHinting = CFFHinting.HORIZONTAL_STEM;
trace("// cffHinting: " + font.cffHinting);

font.fontLookup = FontLookup.DEVICE;
trace("// fontLookup: " + font.fontLookup);
font.fontLookup = FontLookup.EMBEDDED_CFF;
trace("// fontLookup: " + font.fontLookup);

font.fontPosture = FontPosture.NORMAL;
trace("// fontPosture: " + font.fontPosture);
font.fontPosture = FontPosture.ITALIC;
trace("// fontPosture: " + font.fontPosture);

font.fontWeight = FontWeight.NORMAL;
trace("// fontWeight: " + font.fontWeight);
font.fontWeight = FontWeight.BOLD;
trace("// fontWeight: " + font.fontWeight);

font.renderingMode = RenderingMode.NORMAL;
trace("// renderingMode: " + font.renderingMode);
font.renderingMode = RenderingMode.CFF;
trace("// renderingMode: " + font.renderingMode);
}
}
}
27 changes: 27 additions & 0 deletions tests/tests/swfs/avm2/text_engine_fontdescription/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// FontDescription::cffHinting
TypeError: Error #2007: Parameter cffHinting must be non-null.
ArgumentError: Error #2008: Parameter cffHinting must be one of the accepted values.
// FontDescription::fontLookup
TypeError: Error #2007: Parameter fontLookup must be non-null.
ArgumentError: Error #2008: Parameter fontLookup must be one of the accepted values.
// FontDescription::fontName
TypeError: Error #2007: Parameter fontName must be non-null.
// FontDescription::fontPosture
TypeError: Error #2007: Parameter fontPosture must be non-null.
ArgumentError: Error #2008: Parameter fontPosture must be one of the accepted values.
// FontDescription::fontWeight
TypeError: Error #2007: Parameter fontWeight must be non-null.
ArgumentError: Error #2008: Parameter fontWeight must be one of the accepted values.
// FontDescription::renderingMode
TypeError: Error #2007: Parameter renderingMode must be non-null.
ArgumentError: Error #2008: Parameter renderingMode must be one of the accepted values.
// cffHinting: none
// cffHinting: horizontalStem
// fontLookup: device
// fontLookup: embeddedCFF
// fontPosture: normal
// fontPosture: italic
// fontWeight: normal
// fontWeight: bold
// renderingMode: normal
// renderingMode: cff
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_frames = 1

0 comments on commit 28a3c71

Please sign in to comment.