Skip to main content
Known Participant
October 4, 2011
Question

Font.hasGlyphs and line breaks not working

  • October 4, 2011
  • 1 reply
  • 818 views

Hi everybody,

I have a function for checking whether the embedded font has all the glyphs available, it looks like this:

public static function hasEmbeddedAllGlyphs(string:String, fontName:String):Boolean {

            var availableFonts:Array = Font.enumerateFonts();

            for(var i:uint=0; i<availableFonts.length; i++) {

                if(availableFonts.fontName == fontName) {

                    return availableFonts.hasGlyphs(string);

                    break;

                }

            }

            return false;

        }

Now this works flawlessy if I call it with this:

var s:String = "line 1 line 2";

trace(FontUtil.hasEmbeddedAllGlyphs(s, FontUtil.EMBEDDED_FONT_1)); // returns true

However if I add a linebreak into the string, it stops working:

var s:String = "line 1 \n line 2";

trace(FontUtil.hasEmbeddedAllGlyphs(s, FontUtil.EMBEDDED_FONT_1)); // returns false!

What the... why exactly is this so and does anybody know a workaround? I am using this  call in another static method which sets the text depending on the available font and therefore I cannot split the string.

Thank you!

This topic has been closed for replies.

1 reply

FygoAuthor
Known Participant
October 4, 2011

I will answer to myself about the workaround:

public static function hasEmbeddedAllGlyphs(string:String, fontName:String):Boolean {

     

      var testString:String = string.split("\n").join();

        

      var availableFonts:Array = Font.enumerateFonts();

            for(var i:uint=0; i<availableFonts.length; i++) {

                if(availableFonts.fontName == fontName) {

                    return availableFonts.hasGlyphs(testString);

                    break;

                }

            }

            return false;

        }

This makes the trick. However I would still like to know why it is so and whether there is some easier way I am missing.