RE: Size of text in Flash?
"William L. Thomson Jr." <[email protected]> 11 Dec 2002 17:34:38 -0800
| Newsgroups | gmane.comp.java.jgenerator.devel |
|---|---|
| Message-ID | <[email protected]> |
--=-RMW9kYTHOf/dE+4oI0wt
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Andrew,
Thanks for you suggestion but I still am not able to achieve the end
goal I seek. When you determined the size of the text, I assume you used
those boundaries for the text field.
But how did you confirm the size of the box/bounds?
In my case what I am doing is creating a TextField, and trying to
calculate the size of the box based on the size of the chars. Once the
TextField's bounds have been set I am passing that TextField to a
function that turns it into a button. That function creates a button to
the exact bounds the TextField was set to.
In all experiments/tests so far the box is either to big or to small.
When I do seem to have things working I change the text, and problems
again.
For clarity I am including a test case I made so I could figure out the
ratio, or what I need to do. I am attaching it for others to see comment
on. You will see your suggestions commented out, not entirely, but
partially. Since I started with your suggestions and when it did not
work I elaborated on them.
On Wed, 2002-12-11 at 15:05, Andrew Watson wrote:
> I've conducted some brief tests on Flash with Arial font, and these calculations seem to hold true....
>
> ratio = 1024 / point_size
> size_in_pixels = size_in_twips / ratio
> space_between_baselines = (ascent_in_twips + descent_in_twips) / ratio + line_space_in_points
>
> For example, with Arial font, ascent is 927, descent is 217, and the height of a letter A is 733 (according to flash)
> so for size 16 Arial font, with a line_spacing of 2:
>
> ratio = 1024 / 16 = 64
> height in pixels of a letter A = 733 / 64 = 11.45
> distance from the bottom of one A to the next = (927 + 217) / 64 + 2 = 19.875
>
> I scaled this up 8 times, and it still held true, so even if it's not right, its pretty damn close.
>
> Hope this helps
>
> Andrew Watson
>
--
Sincerely,
William L. Thomson Jr.
Support Group
Obsidian-Studios Inc.
439 Amber Way
Petaluma, Ca. 94952
Phone 707.766.9509
Fax 707.766.8989
http://www.obsidian-studios.com
--=-RMW9kYTHOf/dE+4oI0wt
Content-Disposition: attachment; filename=Study_4.java
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-java; name=Study_4.java; charset=ISO-8859-1
/** Study 1
* @author William L. Thomson Jr.
* @company Obsidian-Studios Inc.
* @version 1.0
* @date April 7, 2002
* @point To create a new swf place a textfield on the stage that shows the=
current frame number
*/
package com.obsidian.studios.jgenerator.studies;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.*;
import java.util.*;
import com.iv.flash.api.*;
import com.iv.flash.api.action.*;
import com.iv.flash.api.button.*;
import com.iv.flash.api.shape.*;
import com.iv.flash.api.text.*;
import com.iv.flash.util.*;
public class Study_4 {
private final int FONT_HEIGHT =3D 12;
public static void main(String[] arg) {
new Study_4();
}
public Study_4() {
Util.init("/opt/jgenerator");
/* Flash File */
FlashFile flashFile =3D FlashFile.newFlashFile(); =
// create new flash file
flashFile.setFrameRate(12); =
// set the flash frame rate
Rectangle2D rectangle2d =3D flashFile.getFrameSize(); =
// create a new rectangle2d
rectangle2d.setFrame(0,0,Study.STAGE_WIDTH,Study.STAGE_HEIGHT); =
// set the rectangle2d frame size
flashFile.setFrameSize(rectangle2d); =
// set the flash frame size
flashFile.setVersion(Study.VERSION); =
// set the version of Flash
/* Script */
Script script =3D new Script(1); =
// create script with 1 frame
script.setMain(); =
// make this the main script
flashFile.setMainScript(script); =
// set main script for the file
/* Frame */
Frame frame =3D script.getFrameAt(0);
=20
/* Font*/
Font font =3D FontDef.load("/opt/jgenerator/fonts/Arial.fft"); =
// load flash font file into object
=20
/* TextField*/
String text =3D "Next >>";
double font_height =3D 0;
double font_width =3D 0;
double font_x =3D 0;
double font_y =3D 0;
double ratio =3D 1024/FONT_HEIGHT;
char[] chars =3D text.toCharArray();
for(int i=3D0;i<chars.length;i++) {
int index =3D font.getIndex(chars[i]);
Rectangle2D[] rect2d =3D font.getGlyphBounds();
System.out.println("Char =3D"+chars[i]);
System.out.println("Ascent =3D"+font.ascent);
System.out.println("Descent =3D"+font.descent);
System.out.println("Height =3D"+rect2d[index].getHeight());
System.out.println("Width =3D"+rect2d[index].getWidth());
System.out.println("Advance =3D"+font.getAdvanceValue(index));
=20
font_height =3D ((font.ascent+font.descent)/ratio)*20;
//double height =3D (rect2d[index].getHeight()/ratio)*20;
//if(height>font_height) font_height =3D height;
=20
font_width +=3D (rect2d[index].getWidth()/ratio+2)*20;
//font_width +=3D (font.getAdvanceValue(index)/ratio+2)*20;
//font_width +=3D ((font.getAdvanceValue(index)+rect2d[index].g=
etWidth())/ratio+2)*20;
//font_width +=3D (font.ascent+font.descent)/ratio+2;
}
TextField textField =3D new TextField("<u>"+text+"</u>","p1",font,F=
ONT_HEIGHT*20,AlphaColor.blue);
System.out.println("TextField Height =3D"+font_height);
System.out.println("TextField Width =3D"+font_width);
textField.setBounds(GeomHelper.newRectangle(0,0,font_width,font_hei=
ght));
//alignment (0-left,1-right,2-center,3-justify) rest in twixels
//textField.setLayout(0,0,0,0,0);
//textField.addFlags(TextField.BORDER);
//textField.addFlags(TextField.HASFONT);
//textField.addFlags(TextField.HASLAYOUT);
//textField.addFlags(TextField.HASMAXLENGTH);
//textField.addFlags(TextField.HASTEXT);
//textField.addFlags(TextField.HASTEXTCOLOR);
textField.addFlags(TextField.HTML);
textField.addFlags(TextField.NOSELECT);
textField.addFlags(TextField.READONLY);
//textField.addFlags(TextField.USEOUTLINES);
textField.setMaxLength(text.length());
=20
//font.printContent(System.out,"Indent?",textField.getID());
=20
/* TextField Instance */
Button2 button2 =3D createButton(textField);
//button2.addActionCondition(onRelease());
Instance textFieldInstance =3D frame.addInstance(button2,2,AffineTr=
ansform.getTranslateInstance(0,0),null);
=20
/* Write SWF*/
Study.writeSWF(flashFile,"/wlt/Temp/Study_4.swf"); // wr=
ite the
}
protected Button2 createButton(TextField text) {
Button2 button2 =3D new Button2();
int states =3D ButtonRecord.Up|ButtonRecord.Over|ButtonRecord.Down;=
// add the symbol to define the rest of the stat=
es
button2.addButtonRecord(new ButtonRecord(states,text,1,AffineTransf=
orm.getTranslateInstance(0,0),CXForm.newIdentity(true)));
Shape shape =3D new Shape();
shape.setBounds(text.getBounds());
shape.setFillStyle0(FillStyle.newSolid(new Color(0,0,0)));
shape.drawRectangle(text.getBounds());
button2.addButtonRecord(new ButtonRecord(ButtonRecord.HitTest,shape=
,2,AffineTransform.getTranslateInstance(0,0),CXForm.newIdentity(true)));
button2.setTrackAs(Button2.TrackAsButton);
return(button2);
}
protected ActionCondition onRelease() {
Program program =3D new Program();
program.push(Program.PROP_URL);
program.getProperty();
program.push("?start=3D10&stop=3D20&");
program.addString();
program.push("_blank");
program.getURL(0);
return(ActionCondition.onRelease(program));
}
}
--=-RMW9kYTHOf/dE+4oI0wt--
-------------------------------------------------------
This sf.net email is sponsored by:
With Great Power, Comes Great Responsibility
Learn to use your power at OSDN's High Performance Computing Channel
http://hpc.devchannel.org/