Brent Robinson wrote: > Hi There, > > I am having some issues with generating a script to align characters > to the right on the framework pdf component. This is what I did.
First, I used helvetica and helvetica bold, because they have easily
known widths. I set up a hash to look up the widths and calculate total
string width using a quick custom function. Then, I just subtract the
width from my right hand aligned position. Cheap and dirty, but works
like a champ. It's all below.
--
Drew
/*
* this function returns the typographic width of a string.
*/
public function width_of($str, $font_key, $pt_size)
{
/*
* get the width of a numeric - used for right justification.
*/
$wx = 0;
for ( $i = 0; $i < strlen($str); $i++ )
{
$ch = ord(substr($str, $i, 1));
$wx += self::$char_wx[$font_key][$ch];
}
$width = ( $wx / 1000 ) * $pt_size;
return($width);
}
/*
* This is an array of character widths for a few standard fonts.
*
* Included is "h" (helvetica) and "hb" (helvetica-bold).
*
* Each element is identified by the ascii decimal value of a
* character as the key, and the width (wx) of the character as the
* value.
*
* The widths are in thousandths of a point, so you can calculate a
* characters width as follows:
*
* pt_size_of_font * wx_value / 1000
*/
public static $char_wx = array("h" => array("32" => "278",
"33" => "278",
"34" => "355",
"35" => "556",
"36" => "556",
"37" => "889",
"38" => "667",
"39" => "222",
"40" => "333",
"41" => "333",
"42" => "389",
"43" => "584",
"44" => "278",
"45" => "333",
"46" => "278",
"47" => "278",
"48" => "556",
"49" => "556",
"50" => "556",
"51" => "556",
"52" => "556",
"53" => "556",
"54" => "556",
"55" => "556",
"56" => "556",
"57" => "556",
"58" => "278",
"59" => "278",
"60" => "584",
"61" => "584",
"62" => "584",
"63" => "556",
"64" => "1015",
"65" => "667",
"66" => "667",
"67" => "722",
"68" => "722",
"69" => "667",
"70" => "611",
"71" => "778",
"72" => "722",
"73" => "278",
"74" => "500",
"75" => "667",
"76" => "556",
"77" => "833",
"78" => "722",
"79" => "778",
"80" => "667",
"81" => "778",
"82" => "722",
"83" => "667",
"84" => "611",
"85" => "722",
"86" => "667",
"87" => "944",
"88" => "667",
"89" => "667",
"90" => "611",
"91" => "278",
"92" => "278",
"93" => "278",
"94" => "469",
"95" => "556",
"96" => "222",
"97" => "556",
"98" => "556",
"99" => "500",
"100" => "556",
"101" => "556",
"102" => "278",
"103" => "556",
"104" => "556",
"105" => "222",
"106" => "222",
"107" => "500",
"108" => "222",
"109" => "833",
"110" => "556",
"111" => "556",
"112" => "556",
"113" => "556",
"114" => "333",
"115" => "500",
"116" => "278",
"117" => "556",
"118" => "500",
"119" => "722",
"120" => "500",
"121" => "500",
"122" => "500",
"123" => "334",
"124" => "260",
"125" => "334",
"126" => "584",
"161" => "333",
"162" => "556",
"163" => "556",
"164" => "167",
"165" => "556",
"166" => "556",
"167" => "556",
"168" => "556",
"169" => "191",
"170" => "333",
"171" => "556",
"172" => "333",
"173" => "333",
"174" => "500",
"175" => "500",
"177" => "556",
"178" => "556",
"179" => "556",
"180" => "278",
"182" => "537",
"183" => "350",
"184" => "222",
"185" => "333",
"186" => "333",
"187" => "556",
"188" => "1000",
"189" => "1000",
"191" => "611",
"193" => "333",
"194" => "333",
"195" => "333",
"196" => "333",
"197" => "333",
"198" => "333",
"199" => "333",
"200" => "333",
"202" => "333",
"203" => "333",
"205" => "333",
"206" => "333",
"207" => "333",
"208" => "1000",
"225" => "1000",
"227" => "370",
"232" => "556",
"233" => "778",
"234" => "1000",
"235" => "365",
"241" => "889",
"245" => "278",
"248" => "222",
"249" => "611",
"250" => "944",
"251" => "611"),
"hb" => array("32" => "278",
"33" => "333",
"34" => "474",
"35" => "556",
"36" => "556",
"37" => "889",
"38" => "722",
"39" => "278",
"40" => "333",
"41" => "333",
"42" => "389",
"43" => "584",
"44" => "278",
"45" => "333",
"46" => "278",
"47" => "278",
"48" => "556",
"49" => "556",
"50" => "556",
"51" => "556",
"52" => "556",
"53" => "556",
"54" => "556",
"55" => "556",
"56" => "556",
"57" => "556",
"58" => "333",
"59" => "333",
"60" => "584",
"61" => "584",
"62" => "584",
"63" => "611",
"64" => "975",
"65" => "722",
"66" => "722",
"67" => "722",
"68" => "722",
"69" => "667",
"70" => "611",
"71" => "778",
"72" => "722",
"73" => "278",
"74" => "556",
"75" => "722",
"76" => "611",
"77" => "833",
"78" => "722",
"79" => "778",
"80" => "667",
"81" => "778",
"82" => "722",
"83" => "667",
"84" => "611",
"85" => "722",
"86" => "667",
"87" => "944",
"88" => "667",
"89" => "667",
"90" => "611",
"91" => "333",
"92" => "278",
"93" => "333",
"94" => "584",
"95" => "556",
"96" => "278",
"97" => "556",
"98" => "611",
"99" => "556",
"100" => "611",
"101" => "556",
"102" => "333",
"103" => "611",
"104" => "611",
"105" => "278",
"106" => "278",
"107" => "556",
"108" => "278",
"109" => "889",
"110" => "611",
"111" => "611",
"112" => "611",
"113" => "611",
"114" => "389",
"115" => "556",
"116" => "333",
"117" => "611",
"118" => "556",
"119" => "778",
"120" => "556",
"121" => "556",
"122" => "500",
"123" => "389",
"124" => "280",
"125" => "389",
"126" => "584",
"161" => "333",
"162" => "556",
"163" => "556",
"164" => "167",
"165" => "556",
"166" => "556",
"167" => "556",
"168" => "556",
"169" => "238",
"170" => "500",
"171" => "556",
"172" => "333",
"173" => "333",
"174" => "611",
"175" => "611",
"177" => "556",
"178" => "556",
"179" => "556",
"180" => "278",
"182" => "556",
"183" => "350",
"184" => "278",
"185" => "500",
"186" => "500",
"187" => "556",
"188" => "1000",
"189" => "1000",
"191" => "611",
"193" => "333",
"194" => "333",
"195" => "333",
"196" => "333",
"197" => "333",
"198" => "333",
"199" => "333",
"200" => "333",
"202" => "333",
"203" => "333",
"205" => "333",
"206" => "333",
"207" => "333",
"208" => "1000",
"225" => "1000",
"227" => "370",
"232" => "611",
"233" => "778",
"234" => "1000",
"235" => "365",
"241" => "889",
"245" => "278",
"248" => "278",
"249" => "611",
"250" => "944",
"251" => "611"));
