Wednesday, March 4, 2015

JSFL to update text type(input/static/dynamic) and font face of Textfield


// This code modified by Nitin, copyright 2015.
// Modified code found at: https://delfeld.wordpress.com/2009/12/01/jsfl-textfield-fix/
// Original code found at: http://deleteaso.com/fix-textfields-jsfl/

fl.outputPanel.clear();

var doChangeStaticToDynamic = true;
var updateFontName = "Arial"
var outStr = "Fixing textfields for proper output.\nNOTE: This will only affect objects held in the library (or instances of them).\n\n";
var testDoc;

// cycle through all open documents:
for (doc in fl.documents)
{
    outStr += "Document: " + fl.documents[doc].name + "\n";
    
    // set TextField defaults in this doc:
    scanLibrary(fl.documents[doc].library);
}

fl.trace( outStr );

// -------------------------------------------------------

// auxillary function:
function getIsWholeInt(n)
{
    var s = String(n);
    if(s.indexOf(".") != -1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

// auxillary function:
function round(n)
{
    var s = String(n);
    var a = s.split(".");
    var num = parseInt(a[0]);
    var dec = parseInt(a[1].substr(0, 1));
    if(dec >= 5)
    {
        num++;
    }
    return num;
}

/**
 * Scans the supplied flash library for linked classes and makes sure the textfields are set up properly
 * @param   library     A flash library to scan.
 */
function scanLibrary(library)
{
    // outStr += "Scanning textfields";
    
    // loop variables:
    var item, timeline, layerCnt, frameCnt, elems, elemCnt, matX, tx, matY, ty;
    
    var items = library.items;
    var replaceCount = 0;
    
    // for each item in the library:
    for( var i = 0; i < items.length; i++ )
    {
        item = items[i];
        
        //outStr += "item: " + item.itemType + "\n"
        
        // dig into any MCs:
        if (item.itemType == "movie clip" || item.itemType == "graphic" )
        {
            // store current timeline:
            timeline = item.timeline;
            
            // store total number of layers:
            layerCnt = timeline.layerCount;
            
            // set up editing mode:
            library.selectItem(item);
            library.editItem();
            
            // cycle through all layers within this timeline:
            while(layerCnt--)
            {
                // store total frames
                frameCnt = timeline.layers[layerCnt].frameCount;
                
                // cycle through all frames within this layer:
                while(frameCnt--)
                {
                    // store elements array:
                    elems = timeline.layers[layerCnt].frames[frameCnt].elements;
                    
                    // store total number of elements in this frame:
                    elemCnt = elems.length;
                    
                    // cycle through all elements within this frame:
                    while(elemCnt--)
                    {
                        // process any text type element:
                        if( elems[elemCnt].elementType == "text" )
                        {
                            outStr += "\t Found a textfield: "
                            + ((elems[elemCnt].name == "") ? "{no name}" : elems[elemCnt].name);
                            
       elems[elemCnt].setTextAttr("face", updateFontName);
       
                            //Change the static TextFields to dynamic
                            if( elems[elemCnt].textType == "static" )
                            {
                                if( !doChangeStaticToDynamic)
                                {
                                    outStr += ".  Ignoring this static TextField.\n";
                                    //skip this iteration (element)
                                    continue;
                                }
                                else
                                {
                                    outStr += ". (Switching this static TextField type to dynamic.)\n";
                                    // change text type to dynamic:
                                    elems[elemCnt].textType = "dynamic";
                                }
                            }
                            
                            outStr += "\t Text type: " + elems[elemCnt].textType + "\n";
                            
                            if(elems[elemCnt].textType == "dynamic")
                            {
                                //Remove the ability to be selectable
                                elems[elemCnt].selectable = false;
                            }
                            
                            // This section applies to all dynamic or input TextFields
                            
                            // Remove the auto kern attribute
                            elems[elemCnt].setTextAttr('autoKern', false);
                            
                            // Embed fonts UpperCase, LowerCase, Numerals, Punctuation
                            // This can be fine tuned to get a smaller font set.
                            elems[elemCnt].embedRanges = "1|2|3|4";
                            
                            // Pop onto whole pixel
                            // not sure that two assignments are needed:
                            matX = elems[elemCnt].matrix;
                            tx = matX.tx;
                            if(!getIsWholeInt(tx))
                            {
                                matX.tx = round(tx);
                                elems[elemCnt].matrix = matX;
                            }
                            // Can this be combined with the 'x' section?
                            matY = elems[elemCnt].matrix;
                            ty = matY.ty;
                            if(!getIsWholeInt(ty))
                            {
                                matY.ty = round(ty);
                                elems[elemCnt].matrix = matY;
                            }
                        }
                    }
                }
            }
        }
    }
}

4 comments:

Unknown said...

Very nice post here and thanks for latest smartphone applications it .I always like and such a super colors of phone for these post.Excellent and very cool idea and great models and different kinds of the more information's.

Web designing training in chennai

Jayalakshmi said...

Very much useful information.Thank you for sharing this blog about Nitin Sinha.
angular js training in chennai

angular js training in tambaram

full stack training in chennai

full stack training in tambaram

php training in chennai

php training in tambaram

photoshop training in chennai

photoshop training in tambaram

shiny said...

Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.



oracle training in chennai

oracle training in annanagar

oracle dba training in chennai

oracle dba training in annanagar

ccna training in chennai

ccna training in annanagar

seo training in chennai

seo training in annanagar

deiva said...

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
hadoop training in chennai

hadoop training in omr

salesforce training in chennai

salesforce training in omr

c and c plus plus course in chennai

c and c plus plus course in omr

machine learning training in chennai

machine learning training in omr

Using FormBuilder, Validators & FormGroup in Angular 2 to post data to Node server (node.js)

This blog explains usage of  FormBuilder , Validators , FormGroup classes in Angular 2 projects. In this post we will create HTML form a...