| 1 |
/* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 2 |
/* |
| 3 |
* TADS.as - Flash add-in for the TADS Web UI. This provides access to |
| 4 |
* some extended functionality that's not available in the standard |
| 5 |
* Javascript API in Web browsers, but which Flash provides. |
| 6 |
* |
| 7 |
* Note that this Flash object is purely for programmatic use from |
| 8 |
* Javascript, and is designed to be hidden. This object has no visual |
| 9 |
* presence or UI interaction. |
| 10 |
* |
| 11 |
* Flash is available almost everywhere there's a browser, and it has |
| 12 |
* extremely high uptake where it's available - it's almost as ubiquitous |
| 13 |
* as the browser itself, which is why we use it for these extensions. |
| 14 |
* However, it's important to keep in mind that Flash isn't available |
| 15 |
* *everywhere*. It's notably missing on Apple handheld platforms (iPhone, |
| 16 |
* iPod, iPad), and on platforms even where it's available, a small |
| 17 |
* percentage of users choose not to install it. We therefore have to |
| 18 |
* degrade gracefully when Flash isn't present. |
| 19 |
* |
| 20 |
* Our graceful degradation strategy has three prongs. First, the object |
| 21 |
* is designed to be hidden, and has no UI; this means there's be nothing |
| 22 |
* visually missing when Flash isn't there. Second, the functionality we |
| 23 |
* implement through Flash has to be non-essential, so that the rest of the |
| 24 |
* application will continue to work when this object is inactive. |
| 25 |
* Javascript callers that invoke our API have to take care to handle |
| 26 |
* errors when the object isn't active, and to provide reasonable defaults |
| 27 |
* or alternative behavior. Third, in cases where proprietary browser |
| 28 |
* extensions or other means exist to access the same functionality in the |
| 29 |
* absense of Flash, we'll consider implementing those alternatives. |
| 30 |
*/ |
| 31 |
|
| 32 |
package |
| 33 |
{ |
| 34 |
import flash.display.Sprite; |
| 35 |
import flash.external.ExternalInterface; |
| 36 |
import flash.text.Font; |
| 37 |
import flash.text.FontStyle; |
| 38 |
import flash.system.Security; |
| 39 |
|
| 40 |
/* |
| 41 |
* The main TADS class. We're based on Sprite because we're designed |
| 42 |
* to be embedded in an HTML page (via <OBJECT> or <EMBED>), but note |
| 43 |
* that our embedded object is meant to be hidden, since it exists only |
| 44 |
* for its programmatic interface. |
| 45 |
*/ |
| 46 |
public class TADS extends Sprite |
| 47 |
{ |
| 48 |
/* constructor */ |
| 49 |
public function TADS() |
| 50 |
{ |
| 51 |
/* |
| 52 |
* Set up our Javascript API. Javascript can only call |
| 53 |
* entrypoints that we explicitly export via addCallback(). |
| 54 |
*/ |
| 55 |
var app:TADS = this; |
| 56 |
ExternalInterface.addCallback( |
| 57 |
"getFonts", function():Array { return app.getFonts() }); |
| 58 |
|
| 59 |
/* |
| 60 |
* Notify the host page that we've finished loading. To do |
| 61 |
* this, look for a parameter called "onload" - this contains |
| 62 |
* the name of a host page function to call when loading is |
| 63 |
* complete. If the parameter is there, treat the string as a |
| 64 |
* Javascript function name in the host page, and invoke it via |
| 65 |
* the external host interface. |
| 66 |
*/ |
| 67 |
var onload:String = this.loaderInfo.parameters["onload"]; |
| 68 |
if (onload != null) |
| 69 |
ExternalInterface.call(onload); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* Javascript callback: get a list of fonts installed on the |
| 74 |
* system. The browser javascript API has no way of getting this |
| 75 |
* information, but Flash can. |
| 76 |
*/ |
| 77 |
public function getFonts():Array |
| 78 |
{ |
| 79 |
/* |
| 80 |
* enumerate all fonts (embedded fonts and device fonts - |
| 81 |
* device fonts are the system fonts that can be shown on the |
| 82 |
* display, so they're really the ones we're after here) |
| 83 |
*/ |
| 84 |
return Font.enumerateFonts(true); |
| 85 |
} |
| 86 |
} |
| 87 |
} |