Created
March 10, 2015 06:36
-
-
Save abdul/b3790bf516aecea8a285 to your computer and use it in GitHub Desktop.
Create <font>.swf using Flash IDE and JSFL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
_________________________________________________________________________________________________________________ | |
Font Lib creator JSFL | |
@author Abdul Qabiz ([email protected]) | |
@version 1.00 (7/13/2006) | |
@availability 7.0+ | |
@usage a) Double-click to run or Run from Flash IDE's command menu. | |
@dependency - Requires fontlist.txt, that contains newline separated font-names. | |
__________________________________________________________________________________________________________________ | |
*/ | |
var DEFAULT_EXPORT_DIRECTORY_PATH = "file:///Users/abdulqabiz/Desktop/font"; | |
var DEFAULT_FONT_LIST_PATH = "file:///Users/abdulqabiz/Desktop/fontlist.txt"; | |
var fontList; | |
//font embedRanges, this could be 1, 2, 3, 4, 5. You can use pipe (OR) | to use together. | |
var embedRanges = '1|2|3|4'; // this would embed all Alpha-Numberic-Puncuations (114 glyphs) | |
var saveFla = false; | |
var exportDirectoryPath = DEFAULT_EXPORT_DIRECTORY_PATH; | |
var promptForExportDirectory = false; | |
var fontListPath = DEFAULT_FONT_LIST_PATH; | |
var promptForFontList = true; | |
/* Function Definations */ | |
function main () | |
{ | |
if (fl.documents.length > 0) | |
{ | |
alert ("Please close all opened documents before running this script. Once you have done that, please run this script again."); | |
return; | |
} | |
if (promptForFontList) | |
{ | |
fontListPath = fl.browseForFileURL("select", "Select FontList text file"); | |
if (fontListPath == null) | |
{ | |
alert ("Default font-list would be used because you didn't select one."); | |
fontListPath = DEFAULT_FONT_LIST_PATH; | |
} | |
} | |
fontList = FLfile.read(fontListPath); | |
//both platforms have different end of line formats... | |
var platformDelim = fl.version.indexOf ("WIN") == 0 ? "\r\n" : "\n"; | |
//assumes font-names are newline separated in text-file. | |
fontList = fontList.split (platformDelim); | |
if (promptForExportDirectory) | |
{ | |
exportDirectoryPath = fl.browseForFolderURL("Destination Directory for Font Libraries (*.swf)"); | |
if (exportDirectoryPath == null) | |
{ | |
alert ("Default export directory would be used because you didn't select one."); | |
exportDirectoryPath = DEFAULT_EXPORT_DIRECTORY_PATH; | |
} | |
} | |
else | |
{ | |
if (!FLfile.exists (getExportDirectoryPath ())) | |
{ | |
FLfile.createFolder (getExportDirectoryPath ()); | |
} | |
} | |
var n = fontList.length; | |
for (var i=0;i<n;i++) | |
{ | |
var fontName = fontList[i]; | |
createAndExportFontSWF (fontName); | |
createAndExportFontRSL (fontName); | |
createFontImage (fontName); | |
} | |
alert ("Font SWF and libraries exported." + (saveFla ? " Respective .fla files also saved" : "")); | |
} | |
function getExportDirectoryPath () | |
{ | |
return exportDirectoryPath + "/"; | |
} | |
function createAndExportFontSWF (fontName) | |
{ | |
document = fl.getDocumentDOM (); | |
if (document == null) | |
{ | |
fl.createDocument(); | |
document = fl.getDocumentDOM(); | |
} | |
textFieldWorkaround (); | |
document.addNewText ({left:10, top:10, right:200, bottom:50}); | |
document.setElementProperty ('autoExpand', false); | |
document.setElementProperty ('textType', 'dynamic'); | |
document.setTextString('Normal'); | |
document.setElementTextAttr('face', fontName); | |
document.setElementTextAttr('bold', false); | |
document.setElementTextAttr('italic', false); | |
document.setElementProperty('embedRanges', embedRanges); | |
document.selectNone (); | |
document.addNewText ({left:10, top:60, right:200, bottom:110}); | |
document.setElementProperty ('autoExpand', false); | |
document.setElementProperty ('textType', 'dynamic'); | |
document.setTextString('Bold'); | |
document.setElementTextAttr('face', fontName); | |
document.setElementTextAttr('bold', true); | |
document.setElementTextAttr('italic', false); | |
document.setElementProperty('embedRanges', embedRanges); | |
document.selectNone (); | |
document.addNewText ({left:10, top:120, right:200, bottom:170}); | |
document.setElementProperty ('autoExpand', false); | |
document.setElementProperty ('textType', 'dynamic'); | |
document.setTextString('Italic'); | |
document.setElementTextAttr('face', fontName); | |
document.setElementTextAttr('bold', false); | |
document.setElementTextAttr('italic', true); | |
document.setElementProperty('embedRanges', embedRanges); | |
document.selectNone (); | |
document.addNewText ({left:10, top:230, right:200, bottom:280}); | |
document.setElementProperty ('autoExpand', false); | |
document.setElementProperty ('textType', 'dynamic'); | |
document.setTextString('Bold Italic'); | |
document.setElementTextAttr('face', fontName); | |
document.setElementTextAttr('bold', true); | |
document.setElementTextAttr('italic', true); | |
document.setElementProperty('embedRanges', embedRanges); | |
//movieclip creation | |
document.selectAll (); | |
var symbol = document.convertToSymbol("movie clip", "fontExporter", "top left"); | |
var symbolTimeline = symbol.timeline; | |
symbolTimeline.setLayerProperty ("name", "font"); | |
symbolTimeline.addNewLayer ("script", "normal", true); | |
symbolTimeline.layers[symbolTimeline.findLayerIndex("script")].frames[0].actionScript = '#initclip\ntrace ("{font name} Loaded");\n#endinitclip'; | |
symbol.linkageExportForAS = true; | |
symbol.linkageExportInFirstFrame = true; | |
symbol.linkageIdentifier = "fontExporter"; | |
document.selectNone (); | |
document.exportSWF (getExportDirectoryPath () + fontName + ".swf", true); | |
if (saveFla) | |
{ | |
fl.saveDocument(document, getExportDirectoryPath () + fontName + ".fla"); | |
} | |
fl.closeDocument(document, false); | |
} | |
function createAndExportFontRSL (fontName) | |
{ | |
//create a new document. | |
fl.createDocument(); | |
//store the reference of main-document. Note this is different document from the global document, as we | |
var document = fl.getDocumentDOM(); | |
//create a new movieclip symbol in library | |
document.library.addNewItem("movie clip"); | |
//place the it on stage on maintimeline | |
document.library.addItemToDocument({x:10,y:10}); | |
//select the same symbol | |
document.selectAll (); | |
//assuming there is only one object on stage, skipping advanced looping through layers/frames to avoid complexity. | |
//If above code is changed, you need to change code below :) | |
var symbol = document.selection[0].libraryItem; | |
//change the symbol name | |
symbol.name = "fontExporter"; | |
//mark it as import for Runtime Shared Library | |
symbol.linkageImportForRS = true; | |
//set linkage id | |
symbol.linkageIdentifier = "fontExporter"; | |
//set the linkage URL, from where SWF would be loaded into this symbol. | |
symbol.linkageURL = "font/" + fontName + ".swf"; | |
//deselect everything, so the selection goes back to main timeline. | |
document.selectNone (); | |
//Get the reference of main timeline | |
var mainTimeline = document.getTimeline (); | |
//set the current layer name to fontImporter | |
mainTimeline.setLayerProperty ("name", "fontImporter"); | |
//add new layer to add script, move it above fontImporter layer | |
mainTimeline.addNewLayer ("script", "normal", true); | |
//add actionscript code to script layer | |
mainTimeline.layers[mainTimeline.findLayerIndex("script")].frames[0].actionScript = 'trace ("' + fontName + '_library Loaded");'; | |
//add another guide (visible in .fla only) for description. Add a static text with name of font it would import. | |
mainTimeline.addNewLayer ("description","guide"); | |
document.addNewText ({left:10, top:30, right:100, bottom:50}); | |
document.setElementProperty ('autoExpand', true); | |
document.setElementProperty ('textType', 'static'); | |
document.setTextString('Shared Font Library: ' + fontName); | |
//export this SWF as <export_dir>/<font_name>_library.swf | |
document.exportSWF (getExportDirectoryPath () + fontName + "_library.swf", true); | |
//Save .fla if saveFla is set true. | |
if (saveFla) | |
{ | |
fl.saveDocument(document, getExportDirectoryPath () + fontName + "_library.fla"); | |
} | |
fl.closeDocument(document, false); | |
} | |
function createFontImage (fontName) | |
{ | |
var imageText = fontName;//'The quick brown fox jumps over a lazy dog'; | |
fl.createDocument(); | |
var _doc = fl.getDocumentDOM(); | |
_doc.height = 20; | |
_doc.width = 150; | |
_doc.addNewText ({left:0, top:0, right:300, bottom:20}); | |
_doc.setElementProperty('autoExpand', true); | |
_doc.setElementProperty ('textType', 'static'); | |
_doc.setElementProperty('lineType', 'single line'); | |
_doc.setTextString(imageText); | |
_doc.setElementTextAttr('face', fontName); | |
_doc.setElementTextAttr('size', 14); | |
_doc.setElementTextAttr('bold', false); | |
_doc.setElementTextAttr('italic', false); | |
_doc.exportPNG (getExportDirectoryPath () + fontName + ".png", true, true); | |
fl.closeDocument(_doc,false); | |
} | |
function textFieldWorkaround () | |
{ | |
var _doc = fl.getDocumentDOM(); | |
_doc.addNewText ({left:0, top:0, right:300, bottom:20}); | |
_doc.setElementProperty('autoExpand', false); | |
_doc.setElementProperty ('textType', 'dynamic'); | |
_doc.setTextString("d"); | |
_doc.deleteSelection(); | |
} | |
main (); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment