AS3 Loading and Accessing fonts in external SWF at Runtime


/ Published in: ActionScript 3
Save to your folder(s)

These two classes demonstrate how to create an external font SWF that contains embedded fonts, and how to then load that external font SWF and access and use the fonts inside it.


Copy this code and paste it in your HTML
  1. ///////////////////////////////////////////////////////////////////////////////////////////
  2. // FontSwfDocumentClass.as (Publish this to create FontSWF.swf, which we will then load in to our main application below)
  3. //
  4. package {
  5.  
  6. import flash.display.Sprite;
  7.  
  8. public class FontSwfDocumentClass extends Sprite {
  9.  
  10. // This font was downloaded from here ...
  11. // http://www.dafont.com/sketchetik.font
  12. [Embed(source = "C:/Users/Adrian/Desktop/Sketchetik-Light.otf",
  13. fontName = "Sketchetik-Light",
  14. fontStyle = "normal",
  15. fontWeight = "normal",
  16. unicodeRange = "U+0020-003C,U+003E-007E,U+00A0,U+00A3,U+00A9,U+00AC,U+00AE,U+00BA,U+2013,U+2018-2019,U+201C-201D,U+20AC",
  17. mimeType = "application/x-font",
  18. advancedAntiAliasing = true
  19. ,embedAsCFF=false)]
  20. public static var SketchetikLight:Class;
  21.  
  22. }
  23. }
  24.  
  25. ///////////////////////////////////////////////////////////////////////////////////////////
  26. // This is the Document Class that actually loads in the FontSWF and uses the embedded font
  27. //
  28. package
  29. {
  30. import flash.display.Loader;
  31. import flash.display.Sprite;
  32. import flash.events.Event;
  33. import flash.net.URLRequest;
  34. import flash.text.AntiAliasType;
  35. import flash.text.Font;
  36. import flash.text.TextField;
  37. import flash.text.TextFieldAutoSize;
  38. import flash.text.TextFormat;
  39.  
  40. public class Main extends Sprite
  41. {
  42.  
  43. public function Main():void
  44. {
  45. if (stage) init();
  46. else addEventListener(Event.ADDED_TO_STAGE, init);
  47. }
  48.  
  49. private function init(e:Event = null):void
  50. {
  51. removeEventListener(Event.ADDED_TO_STAGE, init);
  52. loadFontSwf("FontSWF.swf");
  53. }
  54.  
  55. private function loadFontSwf(url:String):void {
  56. var loader:Loader = new Loader();
  57. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontSwfLoaded);
  58. loader.load(new URLRequest(url));
  59. }
  60.  
  61. private function onFontSwfLoaded(e:Event):void {
  62. var FontLibrary:Class = e.target.applicationDomain.getDefinition("FontSwfDocumentClass") as Class;
  63. Font.registerFont(FontLibrary.SketchetikLight);
  64. drawText();
  65. }
  66.  
  67. public function drawText():void {
  68. var tfmt:TextFormat = new TextFormat();
  69. tfmt.color = 0x000000;
  70. tfmt.size = 150;
  71. tfmt.font = "Sketchetik-Light";
  72.  
  73. var tf:TextField = new TextField();
  74. tf.defaultTextFormat = tfmt;
  75. tf.antiAliasType = AntiAliasType.ADVANCED;
  76. tf.autoSize = TextFieldAutoSize.LEFT;
  77. tf.embedFonts = true;
  78. tf.width = 800;
  79. tf.height = 600;
  80. tf.x = 100;
  81. tf.y = 100;
  82. tf.border = true;
  83. tf.rotation = 15;
  84. tf.text = "Hello World";
  85. addChild(tf);
  86. }
  87.  
  88. }
  89.  
  90. }
  91.  
  92. // Handy URLs ...
  93. // http://blog.madebypi.co.uk/2011/02/28/fontswffer-1-1/
  94. // http://nochump.com/blog/archives/20
  95. // http://www.tillschneidereit.de/unicode_range_tool.html
  96. // http://yourpalmark.com/2009/04/05/embedding-fonts-using-external-swf-files/
  97. // http://blog.flexexamples.com/2007/10/25/embedding-fonts-from-a-flash-swf-file-into-a-flex-application/

URL: http://nochump.com/blog/archives/20

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.