4 Replies Latest reply on Feb 25, 2010 2:07 AM by frszilinski

    Calculate Font/Text Metrics

    frszilinski

      Hi all,

      I need to calculate the width and height of a certain font with a specific text.

       

      Short Example (values are fictitiouse):

      Font="Arial" FontSize="10"

      Text="acs" => width=30 & height=10

      Text="acd" => width=30 & height=14

      Text="acW" => width=38 & height=15

      Text="gcW" => width=38 & height=22

       

      Do somebody of you know a way how to do that? Fonts can't be embedded because list of fonts need to be device fonts...

       

      Hope somebody can help me!

      Thanks

        • 2. Re: Calculate Font/Text Metrics
          frszilinski Level 1

          I already found this, thanks.

           

          But how can I get the exact height of the text with a specific font?

          "w"  =>  height=20px

          "W"  =>  height=28px

          "qW"  => height=34px

          ???

           

          See, I get the height and descent, but there is no way to get the high from the second red line to the first (so the additional high of the W compared to x)

          text-metrics.jpg

           

          Does somebody know how to calculate or get the exact measurement? Said I like to draw a rectangle around the text with no spaces...

           

          Thank you!!!

          • 3. Re: Calculate Font/Text Metrics
            frszilinski Level 1

            Bumb and here is a sample code so you can try what I did so far

             

            private function testToMeassureText(text:String):void {
                 var font:String = "Times New Roman";
                 var size:Number = 10;
            
                 var myTextField:TextField = new TextField();
                 myTextField.x = 0;
                 myTextField.y = 0;
                 myTextField.background = false;
                 myTextField.wordWrap = false;
                 myTextField.autoSize = TextFieldAutoSize.LEFT;
                 myTextField.text = text;
                 var newFormat:TextFormat = new TextFormat(); 
                 newFormat.size = size;
                 newFormat.font = font;
                 newFormat.italic = false;
                 newFormat.bold = false;
                 myTextField.setTextFormat(newFormat);
                 var metrics1:TextLineMetrics = myTextField.getLineMetrics(0);
                 trace("------------------------------------------------------");
                 trace("Metrics ascent is: " + metrics1.ascent.toString());
                 trace("Metrics descent is: " + metrics1.descent.toString());
                 trace("Metrics height is: " + metrics1.height.toString()); 
                 trace("Metrics width is: " + metrics1.width.toString());
            }
            
            

             

            Also tried:

             

             

            var bound:Rectangle = myTextField.getCharBoundaries(0);
            trace("Char width is: " + bound.width);
            trace("Char height is: " + bound.height);
            
            

             

             

             

            Same Problem, the height always the same, doesn't matter if it is iE. an "a" "A" or "g"

             

             

            Now I know, I am searching for the x-height of a font but I could not find a solution!

            Any Ideas?

             

            Message was edited by: frankhoff

            • 4. Re: Calculate Font/Text Metrics
              frszilinski Level 1

              Everybody who is interested in the solution, check out my blog (german but code should work in other languages too:)

              http://www.esentri.com/blog/2010/02/flex-fontmetrics-und-exakte-berechnung-der-schrift-bre ite-und-hohe/