I've completely run out of ideas on this. It follows, and is part of my previous question:
embedding a font in a swf using as3
I just don't seem to be able to get the flash.text.engine to use my embedded font. NB the font has to be loaded into the application (as embedded in swf) after the user has chosen the two languages (for translation to and from). There seems to be a little info implying that it is now necessary to use the fontswf application which is in the sdk. I have tried this and produced loadable swf files but I can't find any info on how these are then loaded (i.e. the getDefinition and registerFont bits below don't work as there are no classes in these swf) and applied to text.engine objects. The source for the embedding is in my answer to my question above. This is a test as3 which demonstrates how it doesn't work!
package
{
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.TextBlock;
import flash.text.engine.TextLine;
import flash.text.engine.TextElement;
import flash.net.URLRequest;
import flash.text.Font;
public class Main extends Sprite
{
private var loader:Loader;
private var tl:TextLine;
public function Main():void
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,fontLoaded);
loader.load(new URLRequest("EnglishF.swf"));
}
private function fontLoaded(evt:Event):void {
var FontClass:Class
FontClass = evt.target.applicationDomain.getDefinition("EnglishF") as Class;
try {
Font.registerFont(FontClass.myFont);
trace("successfully loaded " + FontClass);
// gives 'successfully loaded EnglishF'
} catch (err:Error) {}
var fontList:Array = Font.enumerateFonts();
for (var i:int = 0; i < fontList.length; i++) {
trace(fontList[i].fontName, fontList[i].fontType);
// gives 'EnglishF embeddedCFF'
}
var block:TextBlock = new TextBlock();
var font:FontDescription = new FontDescription("EnglishF");
var formt:ElementFormat = new ElementFormat(font, 30);
trace(FontDescription.isFontCompatible("EnglishF","normal","normal"), formt.fontDescription.fontName);
// gives 'true EnglishF'
formt.color = 0x882233;
var span:TextElement = new TextElement("Hello World. This is certainly NOT in the Font provided!", formt);
block.content = span;
tl = block.createTextLine();
tl.x = 10;
tl.y = tl.ascent + 10;
addChild(tl);
}
}
}
Am I doing anything wrong, or is this impossible?
Hopefully this will help. Under this line:
var font:FontDescription = new FontDescription("EnglishF");
add the following line:
font.fontLookup = FontLookup.EMBEDDED_CFF;
This will let the text framework know that you're using a CFF font (used in the new text framework), instead of a regular embedded font (used in TextFields).
Hope this helps.
Jordan
Related
I am getting Error:#2044, so I assumed that my code is wrong in calling the Sound functions but I can't seem to find where I am making the error.
package {
import flash.display.MovieClip;
import flash.media.SoundChannel;
import flash.media.Sound;
import flash.net.URLRequest;
public class Tile extends MovieClip{
public function GetAndSwitchKey():String {
//Some Code//
//Create sounds
//------------> Start here
var bSound:Sound = new Sound();
var bReq:URLRequest = new URLRequest("B.mp3");
var oSound:Sound = new Sound();
var oReq:URLRequest = new URLRequest("O.mp3");
var mSound:Sound = new Sound();
var mReq:URLRequest = new URLRequest("M.mp3");
//Load sounds
bSound.load(bReq);
oSound.load(oReq);
mSound.load(mReq);
//<-------- End here
//Some code//
switch (temp) {
case "B": bSound.play(); break;
case "O": oSound.play(); break;
case "M": mSound.play(); break;
}
//Some code//
}
}
}
The way I added the files is by placing them inside the same file as the Action script 3 file. I also then changed them in the propperties to be exportable for action script. But as far as I am aware I don't have to specify the directory since a copy is made in the SWF.
Replacing the code indicated between the "start here" and "end here". This will resolve the error. And will call the sound file properly.
var mySoundHolder = new mySound();
I made a function that would add a child from my library using a variable made from an xml.
var ChosenGraphic:String = units.unit.(#titel==k1).graphic;
var mc:MovieClip = new ChosenGraphic;
addChild(mc);
I know I can't use :String for this, but I don't have a clue what to use. I'm trying to get it to work for 2 hours now, and it's getting really frustrating.
Everything else works, I've tested that.
You want to use the getDefinitionByName() top level function.
Below is an example of using it, lifted straight from the documentation that I linked to. Note that the class name string must be the fully qualified class name (ie: it includes the package name as well as the class).
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
public class GetDefinitionByNameExample extends Sprite {
private var bgColor:uint = 0xFFCC00;
private var size:uint = 80;
public function GetDefinitionByNameExample() {
var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;
var instance:Object = new ClassReference();
instance.graphics.beginFill(bgColor);
instance.graphics.drawRect(0, 0, size, size);
instance.graphics.endFill();
addChild(DisplayObject(instance));
}
}
}
I have developed a Facebook application using Flash as3. In this application when ever the user finished the test, a certificate URL is posted on user wall. the url is encode with Turkish character user first name and user last name.In certificate swf file it reads the url and display the user name on the jpg. This work great with English users but the problem arrives when the user name is in Turkish. I am attaching the jpg of the main problem, you can clearly see the url is looking good, but when i copy past the url i get the same result as display on the picture.
My Certificate Swf code is
package {
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.external.ExternalInterface;
import com.adobe.images.JPGEncoder;
public class main extends MovieClip {
public function main() {
valuePairs = new Array();
nextValuePare = new Array();
var search:String = ExternalInterface.call("window.location.href.toString");
//var vars:URLVariables = new URLVariables(search);
valuePairs = search.substring(search.indexOf("?")+1).split("&");
trace(valuePairs);
var map:Object = new Object();
for (var i:int = 0; i<valuePairs.length; i++)
{
nextValuePare = valuePairs[i].split("=");
trace(nextValuePare);
map[nextValuePare[0]] = nextValuePare[1];
}
picture.fbookName.text = String(map["fName"]) +" "+ String(map["lName"]);
}
private var _urlLoader:URLLoader;
private var _urlVariable:URLVariables;
private var valuePairs:Array;
private var nextValuePare:Array
}
}
You should URL encode your URL with the top level function encodeURI(). Then in the swf that extracts the names from the URL use decodeURI().
I am using Flash Text Engine's TextBlock & TextLine to display text. However, I found that some font make TextLine calculate height incorrectly. Here is my sample code:
package users
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.FontLookup;
import flash.text.engine.FontPosture;
import flash.text.engine.FontWeight;
import flash.text.engine.RenderingMode;
import flash.text.engine.TextBaseline;
import flash.text.engine.TextBlock;
import flash.text.engine.TextElement;
import flash.text.engine.TextLine;
public class TestTextSize extends Sprite
{
private var textBlock:TextBlock = null;
private var textElement:TextElement = null;
private var _textField:TextField;
private var _textFormat:TextFormat;
private var elementFormat:ElementFormat;
private var _textLine:TextLine;
[Embed(source="assets/fonts/SWANSE__.TTF", fontFamily="Swansea", mimeType="application/x-font", embedAsCFF="true")]
private const Swansea:Class;
public function TestTextSize()
{
super();
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
stage.scaleMode = StageScaleMode.NO_SCALE;
var fontDescription:FontDescription = new FontDescription("Swansea"
, FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF);
elementFormat = new ElementFormat();
elementFormat.fontDescription = fontDescription;
elementFormat.fontSize = 114.5;
elementFormat.color = 0x990000;
textElement = new TextElement("This is some text", elementFormat);
textBlock = new TextBlock(textElement);
textBlock.baselineZero = TextBaseline.ASCENT;
_textLine = textBlock.createTextLine(null, 1000,0,true);
_textLine.y = 0;
addChild(_textLine);
trace(_textLine.height);
trace(_textLine.textHeight);
trace(_textLine.totalHeight);
trace(_textLine.totalAscent);
trace(_textLine.totalDescent);
var rect:Rectangle = _textLine.getBounds(this);
trace(rect.y, rect.height);
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0x004400);
sprite.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
sprite.graphics.endFill();
sprite.alpha = 0.3;
sprite.y = 0;
addChild(sprite);
}
}
}
The font I'm using is Swansea (http://www.fontspace.com/roger-white/swansea) standard normal font.
From the code, I extract TextLine's height by using getBounds, and use that height to draw rectangle. The rectangle appears to has height shorter than the actual text, means that getBounds can't get correct actual text height.
The result from running the code looks like this:
The console give the following result:
57.6
57.59326171875
57.59326171875
0
57.59326171875
0 57.6
which means TextLine.textHeight, height, totalHeight, and result from getBounds give the same value, roughly 57.6 pixel.
So, my question is, how can I get the actual text height?
Please don't tell me not to use Swansea. So far, I have tried 5-6 different fonts and only Swansea give me this problem. But, how do I know that this problem will not happen again? At least, if there is a way to distinguish font that give this problem and font that won't, that would be highly appreciated.
I have found that if I comment out the line
textBlock.baselineZero = TextBaseline.ASCENT;
then the totalHeight and totalAscent property will return 83.81396484375.
Maybe you could also try to re-package the font with a font editor.
I need to convert bitmapData grabbed from movie clip to jpeg or png. Is there some lib for that?
see this example in take portion that convert byte to image, i have given whole for your reference
package {
import encoding.JPGEncoder;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.events.Event
public class Converter extends Sprite {
[Embed(source = "image.jpg")]
private var image:Class
private const QUALITY:uint = 80;
public function Converter():void {
var bitmap:Bitmap = new image();
var bitmapData:BitmapData = bitmap.bitmapData;
//encode BitmapData to JPG
var encoder:JPGEncoder = new JPGEncoder(QUALITY);
var rawBytes:ByteArray = encoder.encode(bitmap.bitmapData);
//decode JPG ByteArray back to BitmapData
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData)
loader.loadBytes(rawBytes);
}
private function getBitmapData(e:Event):void {
var decodedBitmapData:BitmapData = Bitmap(e.target.content).bitmapData
trace(decodedBitmapData);
}
}
}
You can now use the native jpeg encoding built into Flash Player 11.3 and AIR 3.3.
var quality:int = 50;
var bounds:Rectangle = bitmapData.rect;
var result:ByteArray = bitmapData.encode(bounds, new JPEGEncoderOptions(50));
This requires -swf-version=16 be passed to the compiler.
This is not the simplest way, but depending on the dimensions of your image, may be worth a look. Jens Krause has compiled jpeglib with Alchemy, and it encodes much faster than as3corelib's version, or even Thibault Imbert's improved AS3 version:
http://www.websector.de/blog/2009/06/21/speed-up-jpeg-encoding-using-alchemy/