【问题标题】:embedded font displays no text (as3)嵌入字体不显示文本 (as3)
【发布时间】:2018-09-06 17:13:28
【问题描述】:

使用 Flash 开发。所以我从这里下载了“source sans”字体:https://fonts.google.com/specimen/Source+Sans+Pro?selection.family=Source+Sans+Pro

它有一堆 ttf 文件,“粗体、斜体等”我猜我只需要一个,所以我将常规文件复制到我的 src 文件夹,将其重命名为“SourceSansPro”,右键单击我的 src 文件夹并添加新的字体库。我将其命名为“SourceSansPro”。现在是我的代码:

主类:

    package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFormat;


    public class Main extends Sprite 
    {

        private var format:TextFormat = new TextFormat("SourceSansPro");
        private var text:TextField = new TextField;

        public function Main() 
        {
            text.embedFonts = true;
            text.setTextFormat(format);

            text.text = "abcdefg";

            addChild(text);

        }



    }

}

字体库的东西:

/**
Suggested workflow:
- create a fontLibrary subfolder in your project (NOT in /bin or /src)
- for example: /lib/fontLibrary
- copy font files in this location
- create a FontLibrary class in the same location
- one font library can contain several font classes (duplicate embed and registration code)

FlashDevelop QuickBuild options: (just press Ctrl+F8 to compile this library)
@mxmlc -o bin/SourceSansPro.swf -static-link-runtime-shared-libraries=true -noplay
*/
package 
{
    import flash.display.Sprite;
    import flash.text.Font;

    /**
     * Font library
     * @author 111
     */
    public class SourceSansPro extends Sprite 
    {
        /*
        Common unicode ranges:
        Uppercase   : U+0020,U+0041-U+005A
        Lowercase   : U+0020,U+0061-U+007A
        Numerals    : U+0030-U+0039,U+002E
        Punctuation : U+0020-U+002F,U+003A-U+0040,U+005B-U+0060,U+007B-U+007E
        Basic Latin : U+0020-U+002F,U+0030-U+0039,U+003A-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E
        Latin I     : U+0020,U+00A1-U+00FF,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183
        Latin Ext. A: U+0100-U+01FF,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183
        Latin Ext. B: U+0180-U+024F,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183
        Greek       : U+0374-U+03F2,U+1F00-U+1FFE,U+2000-U+206f,U+20A0-U+20CF,U+2100-U+2183
        Cyrillic    : U+0400-U+04CE,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183
        Armenian    : U+0530-U+058F,U+FB13-U+FB17
        Arabic      : U+0600-U+06FF,U+FB50-U+FDFF,U+FE70-U+FEFF
        Hebrew      : U+05B0-U+05FF,U+FB1D-U+FB4F,U+2000-U+206f,U+20A0-U+20CF,U+2100-U+2183

        About 'embedAsCFF' attribute:
        - is Flex 4 only (comment out to target Flex 2-3)
        - is 'true' by default, meaning the font is embedded for the new TextLayout engine only
        - you must set explicitely to 'false' for use in regular TextFields

        More information:
        http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf69084-7f5f.html
        */


        [Embed(source="SourceSansPro.ttf"
        ,fontFamily  ='SourceSansPro'
        ,fontStyle   ='normal' // normal|italic
        ,fontWeight  ='normal' // normal|bold
        ,unicodeRange='U+0020-U+002F,U+0030-U+0039,U+003A-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E'
        ,embedAsCFF='false'
        )]
        public static const fontClass:Class;

        public function SourceSansPro() 
        {
            Font.registerFont(fontClass);

        }

    }

}

所以在 main 中,如果 text.embedFonts 为 false 则显示默认字体,如果为 true 则显示为空白。

有什么帮助吗?

编辑 - 新代码

package
{

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.Font;

    public class Main extends Sprite 
    {

        private var format:TextFormat = new TextFormat("libel");
        private var text:TextField = new TextField;

        public function Main() 
        {



            [Embed(source="libel.ttf"
        ,fontFamily  ='libel'
        ,fontStyle   ='normal' // normal|italic
        ,fontWeight  ='normal' // normal|bold
        ,unicodeRange='U+0020-U+002F,U+0030-U+0039,U+003A-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E'
        ,embedAsCFF='false'
        )]

        Font.registerFont();

            text.embedFonts = true;
            text.setTextFormat(format);

            text.text = "abcdefg";

            addChild(text);

        }



    }

}

【问题讨论】:

  • 您可能需要阅读以下内容:forums.adobe.com/thread/751964 使用 [Embed] 元标记,然后是 Font.registerFont(...),然后是您的样本测试可能会起作用。
  • 对不起,我不明白您所说的“使用 [Embed] 元标记,然后使用 Font.registerFont(...)”是什么意思
  • 这就是我向您提供链接的原因。如果第一个解释性不够,还有另一个:divillysausages.com/2011/02/17/as3-font-embedding-masterclass [Embed(...)] 元标记是一个 AS3 语言条目,它允许将外部文件嵌入到项目中并使其数据可访问。您可以嵌入纯文本或二进制文件,或图片,或者,在您的情况下,整个字体。然后有办法访问嵌入的字体并让应用程序使用它来显示文本。
  • 我的代码中还没有嵌入吗?我读了你的链接,但我不明白你想告诉我什么。我是否错误地使用了 embed 或 Font.registerFont()?
  • 好的,我有点理解你的问题。我发布的脚本至少具有嵌入和使用外部字体的正确语法。这里的问题是,您将脚本视为魔术。我给你提供了两个链接,至少有一个地方你可以清楚地看到如何使用 [Embed(...)] 元标记,AS3 官方文档也很好,所以你不应该在阅读 Fonts.registerFont(...) 方法的作用和方式时遇到了问题。然而,您只是复制粘贴了一些代码,对它没有太多了解。附言请删除重复的线程。

标签: actionscript-3 flashdevelop


【解决方案1】:

这可能是你想要的。

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.Font;

    public class Main extends Sprite 
    {
        // After the [Embed] tag you need
        // a variable definition it is linked to.
        [Embed(source="libel.ttf", fontFamily='libel')]
        private var Libel:Class;

        public function Main() 
        {
            // In order to share the font with the whole application,
            // you need to provide its class to the method.
            Font.registerFont(Libel);

            var aFormat:TextFormat = new TextFormat;

            aFormat.font = "libel";
            // ... other format properties here.

            var aField:TextField = new TextField;

            // This way you will see that
            // TextField even if fonts don't render.
            aField.border = true;

            // Setting the default text format is a good idea here.
            aField.embedFonts = true;
            aField.setTextFormat(aFormat);
            aField.defaultTextFormat = aFormat;

            aField.text = "abcdefg";

            addChild(aField);
        }
    }
}

【讨论】:

  • @mellowm 如果代码示例确实有帮助并按预期工作,您会很高兴。
  • 哦,它成功了,我明白它现在是如何工作的
猜你喜欢
  • 1970-01-01
  • 2012-10-10
  • 2011-12-10
  • 2021-04-07
  • 2013-07-17
  • 2011-08-25
  • 1970-01-01
  • 2011-11-24
  • 2011-11-16
相关资源
最近更新 更多