Thursday, July 28, 2011

AS3: Transmute namespace instead of removing it in XML

There are lot many articles and solutions available on web related to the xml namespace. Few guide us over how to handle namespace in xml via as3 while rest have provided solution by removing xmlns tag from the xml.

But I think, there is better way than removing or filtering the namespace. The solution that I use is very clever one. You might have noticed that to put namespace in xml colon character is used. So keeping this property in mind, I have developed a function to replace all the colons from namespaces only with the underscore character.

The code uses regular expression to search the colon character in only namespace. And then replace it with the underscore character.

This way all the namespaces are still available and accessible but in the form of attributes. And this is the advantage of converting it into a attributes.

  * @method transmuter  
  * @author Abhishek Kumar  
 private function transmuter(data:String):String  
  var modified:String = data.replace("xmlns=", "xmlns:default=");  
  var pattern:RegExp = new RegExp(":(?!/)", "gi");  
  var transmuted:String = modified.replace(pattern, "_");  
  return transmuted;  

The function takes xml as a string. So make the xml into string as, xmldata.toString(); then use the function to transmute it. And convert the returned xmldata that is in string format into the xml class format.

Saturday, July 2, 2011

Batch File - Bulk Renamer ... Advanced

This post is the continuation of my last post, i.e., Batch File - Bulk Renamer; related to systematically renaming all the files in a folder.

The problem I noticed with my previous code is that, either it works with the particular type of file (aka same extension) or in absence of file-extensions it renames this batch file also. So I thought to introduce a condition in this so as to skip this batch file while renaming other files.

Here is the code for that,


@echo off&set /a cnt=1
for %%a in (*) do call :PROCESS "%%a"
goto :EOF
IF not %1=="bulk_renamer.bat" (rename %1 "IMG_%cnt%.jpg")
set /a cnt+=1

Note: This code would rename all the files present in the folder except bulk_renamer.bat

Help: Refer to my previous post for the steps to create or modify the script.


Friday, July 1, 2011

AS3: To determine the angle between two given coordinates

Here is a sample code to determine the angle between two given coordinates. In other words, to calculate the angle of an object with respect to the origin.

This could be used to determine current angle of mouse-pointer with some pre-defined origin. In which case the movieclip attached to the mouse-pointer can always point towards it's origin.

Many examples of this logic can be seen on web. One of the popular example is; a female face with moving eyes that follows the user's mouse-pointer movement, so that the eyes always look towards the mouse-pointer.

Sample Code:
  import flash.geom.Point;  
  * @file  
  * @author Abhishek Kumar  
  public class Connecteur
   private var originCoordinate:Point;  
   private var currentCoordinate:Point;  
   private function setOriginCoordinate():void  
     originCoordinate = new Point(Source.x, Source.y);  
   private function setCurrentCoordinate():void  
     currentCoordinate = new Point(Pointer.x, Pointer.y);  
   private function getCurrentAngle():Number  
     var perpendicular:Number = (currentCoordinate.y - originCoordinate.y);  
     var base:Number = (currentCoordinate.x - originCoordinate.x);  
     var theta:Number = Math.atan2(perpendicular, base); // in radians  
     return (theta * 180 / Math.PI); // in degrees  

TTFEdit - TrueTypes font editor

TTFEdit is a GPL editor for TrueType fonts. TrueType is a standard used by modern operating systems like Mac OS X, Windows XP and Linux. There are two different TrueType specifications: from Apple (original author of this technology) and from Microsoft. Mac OS X and Windows XP use own font rendering engines, there is also FreeType library used by Linux. On these three systems the same font will generally look different. TTFEdit tries to produce TTF files compatible with all mentioned systems.

Download link:

Hosted at SourceForge: