Re: (PHP) Using browser fonts with SWFText and SWFShape

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Here is an example of animated text which worked in 0.3Beta. The functions used in the script are extracted from a larger library I am (was) building. I apologize for the lack of code comments, but I removed them for this email. The method I developed for this script is an approximation of how I viewed it being done in Adobe Flash: create a text string, break it up into individual shapes, then animate those shape by easing their positions.

======================

<?php

function spread_objects($count, $width, $height, $mode=0) {
 for ($i=0; $i<$count; $i++) {
       switch ($i) {
           case 1 :
           case 3 :
           case 5 : $xsign = 1; break;
           case 7 :
           case 9 : $xsign = -1; break;
           case 0 :
         case 2 :
         case 4 : $ysign = 1; break;
         case 6 :
         case 8 : $ysign = -1; break;
     }
   switch ($mode) {
     case 0 : // Place objects at the bottom
       $xy[$i]['x'] = rand(1, $width);
       $xy[$i]['y'] = rand($height, $height+10);
               break;
     case 1 :  // Place objects at right
             $xy[$i]['x'] = rand($width, $width+10);
             $xy[$i]['y'] = rand(1, $height);
             break;
         case 2 :  // Place objects at top
             $xy[$i]['x'] = rand(1, $width);
             $xy[$i]['y'] = -rand(1, $height);
             break;
         case 3 :  // Place objects at left
             $xy[$i]['x'] = -rand(1, $width);
             $xy[$i]['y'] = rand(1, $height);
               break;
         case 4 :  // Distribute around the frame
             $xy[$i]['x'] = rand(1, $width) * $xsign;
             $xy[$i]['y'] = rand(1, $height) * $ysign;
             break;
  }
 }
 return $xy;
}

function breakApart($string, $font, $color, $scale, $border, $bordercolor=0) {
 for ($i=0; $i<strlen($string); $i++) {
   $glyph = new SWFShape();
   list($r, $g, $b) = hex2rgb($color);
   $glyph->setRightFill($r, $g, $b);
   if ($border > 0) {
     list($r, $g, $b) = hex2rgb($bordercolor);
     $glyph->setLine($border, $r, $g, $b);
   }
   else {
     $border = 0;
   }
   $glyph->drawGlyph($font, $string[$i]);

   $w = $font->getWidth($string[$i]) * $scale;
   $shapes[$i]['clip'] = new SWFSprite();
   $shapes[$i]['glyph'] = $shapes[$i]['clip']->add($glyph);
   $shapes[$i]['glyph']->scaleTo($scale);
   $shapes[$i]['width'] = $w + $border;
 }
 return $shapes;
}

function hex2rgb($c) {
 if (!$c) return false;

 $c = trim($c);
 $out = false;
 if (eregi("^[0-9ABCDEFabcdef\#]+$", $c)) {
  $c = str_replace('#','', $c);
  $l = strlen($c);
  if ($l == 3) {
   unset($out);
   $out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0, 1));
   $out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1, 1));
   $out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2, 1));
   $out[3] = $out['a'] = $out['alpha'] = 0;
  }
  elseif ($l == 6) {
   unset($out);
   $out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0, 2));
   $out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 2, 2));
   $out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 4, 2));
   $out[3] = $out['a'] = $out['alpha'] = 0;
  }
  elseif ($l == 8) {
   unset($out);
   $out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0, 2));
   $out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 2, 2));
   $out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 4, 2));
   $out[3] = $out['a'] = $out['alpha'] = hexdec(substr($c, 6, 2));
  }
  else
   $out = false;
 }
 return $out;
}

function moveObject($clip, $object, $start_x, $start_y, $stop_x, $stop_y,
                                        $duration, $use_time=false) {
 global $swfRate;

 if ($use_time) {
   $duration *= $swfRate;
 }

 $inc_x = ($stop_x - $start_x);
 $inc_y = ($stop_y - $start_y);
 for ($i=1; $i<=$duration; $i++) {
   $x = easeLinear($i, $start_x, $inc_x, $duration);
   $y = easeLinear($i, $start_y, $inc_y, $duration);
   $object->moveTo($x, $y);
   $clip->nextFrame();
 }
}

function easeLinear($t, $b, $c, $d) {
 return $c * $t / $d + $b;
}

function skip_frames($n, $parent=0) {
   global $stage;

   if ($parent == 0) $parent = $stage;
   for ($i=0; $i<$n; $i++) {
       $parent->nextFrame();
   }
}

$swfWidth = 550;
$swfHeight = 400;
$stage = new SWFMovie(7);
$stage->setDimension($swfWidth, $swfHeight);
$stage->setBackground(0xCC, 0xCC, 0xCC);

$myFont = new SWFFont("fdb/Impact.fdb");
$myString = "Ming Lives!";
$scale = 0.5;

$myShapes = breakApart($myString, $myFont, "0000ff", $scale, 0, "ff0000");

$totalWidth = 0;
for ($i=0; $i<count($myShapes); $i++) {
 $totalWidth += $myShapes[$i]['width'];
}

for ($mode=0; $mode<5; $mode++) {
   $xy = spread_objects(count($myShapes), $swfWidth, $swfHeight, $mode);
   $stop_x = $swfWidth/2 - $totalWidth/2;

   for ($i=0; $i<count($myShapes); $i++) {
moveObject($myShapes[$i]['clip'], $myShapes[$i]['glyph'], $xy[$i]['x'],
                $xy[$i]['y'], $stop_x, 200, 24);
     $stop_x += $myShapes[$i]['width'];
       skip_frames(15, $myShapes[$i]['clip']);
     $stage->add($myShapes[$i]['clip']);
   }
}

$stage->save($outswf="test.swf",9);
$ftmp=fopen($outswf,"r");
$stmp=fread($ftmp,filesize($outswf));
$ftmp=fopen($outswf,"w");
fwrite($ftmp,substr_replace($stmp,chr(8),3,1));
header('Content-type: application/x-shockwave-flash');
$stage->output();

?>

======================

I would like to get this working in 0.4.2. I do not relish the thought of moving to text fields, but it looks like it cannot be avoided.


Larry


strk wrote:
On Fri, Oct 09, 2009 at 11:46:45AM -0400, Larry Dunbar wrote:

animated. With ming 0.3 this is was easily accomplished using a combination of SWFFont, SWFText, and SWFShape methods. This seems nearly impossible now that SWFText and SWFShape no longer support SWFBrowserFont fonts.

Could you show the ming-0.3 code producing what you are looking for ?
Do you have funding to help fixing the regression ?

--strk;

 Free GIS & Flash consultant/developer      ()  ASCII Ribbon Campaign
http://foo.keybit.net/~strk/services.html /\ Keep it simple!

begin:vcard
fn:Larry Dunbar
n:Dunbar;Larry
org:LowTechGeezer.com
adr:;;4595 Rosebud St.;Cocoa;FL;32927;US
email;internet:ldunbar@xxxxxxxxxxxxxxxx
tel;work:(321) 482-5753
tel;home:(321) 482-5753
x-mozilla-html:TRUE
version:2.1
end:vcard

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Ming-users mailing list
Ming-users@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/ming-users

[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux