2013-04-13

Flashでローカルフォントを使ってテキスト表示

Flashで、ローカルのOS(Windows等)にインストールされたフォントを使い、テキストを表示するサンプル。

https://sites.google.com/site/itouhiro/2013/20130413localfont.swf

環境

  • Windows7 64bit
  • FlashDevelop 4
  • FlexSDK 3.6 (FlashPlayer9.0 or higher)
  • GoogleChrome25

コンパイル方法

以下のMain.asを用意する。

Main.as

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.Font;
    import com.bit101.components.ComboBox;
    import flash.text.TextField;
    import flash.text.TextFormat;
    
    /**
     * ...
     * @author itouhiro
     */
    [SWF(width="640",height="480",backgroundColor="0xfcfcfc",frameRate="30")]
    public class Main extends Sprite 
    {
        private var sampleText:TextField;
        private var sampleString:String = "フォントを選択してください。\nSelect your local font.";
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            
            //フォント一覧を取得
            var fonts:Array = Font.enumerateFonts(true); //'true' returns device&embed font list
            fonts.sortOn("fontName", Array.CASEINSENSITIVE);
            
            //MinimalCompsのコンボボックスを生成
            var cbox:ComboBox = new ComboBox(this, 0, 0, "Select Font");
            cbox.width = 200; cbox.height = 24;
            for (var i:int = 0; i < fonts.length; i++) {
                cbox.addItem(fonts[i].fontName);
            }
            cbox.addEventListener(Event.SELECT, selectHandler);
            
            //テキストの外枠
            graphics.beginFill(0x222222);
            graphics.drawRoundRect(10, stage.stageHeight - 64 * 2 - 15, stage.stageWidth - 15 * 2, 64*2, 10, 10 );
            graphics.endFill();
            
            //テキスト
            sampleText = new TextField();
            sampleText.x = 20; sampleText.y = stage.stageHeight - 64*2 - 5;
            sampleText.width = stage.stageWidth - (sampleText.x * 2);
            sampleText.height = 64*2;
            var tfmt:TextFormat = new TextFormat(null, 32, 0xEEEEEE)
            sampleText.defaultTextFormat = tfmt;
            sampleText.text = sampleString;
            addChild(sampleText);
        }
        
        private function selectHandler(e:Event):void 
        {
            var cbox:ComboBox = e.currentTarget as ComboBox;
            trace('index=' + cbox.selectedIndex);
            trace('item=' + cbox.selectedItem + '(' + typeof(cbox.selectedItem)+')');
            var tfmt:TextFormat = new TextFormat(cbox.selectedItem.toString(), 32, 0xEEEEEE)
            sampleText.defaultTextFormat = tfmt;
            sampleText.text = sampleString; //←これ書かないと更新されない
        }
    }
}

フォントを選択する一行セレクトボックス(コンボボックスというらしい)を使うために、MinimalCompsというライブラリを導入する。 このComboBox、使い勝手はとくによくはない(選択にカーソルキー使えない、Shift+クリックでいきなりクリック先に飛ばない)けど、いろんなコンポーネントを軽量に実装してあるそうなので使ってみる。

Main.asと同じディレクトリに、 http://code.google.com/p/minimalcomps/ で取得した MinimalComps_0_9_10.zip の'com'ディレクトリをcopyする。'assets'ディレクトリはcopyしなくていい。 ( https://github.com/minimalcomps/minimalcomps のが新しいかも)

以下を書き換えて日本語表示に対応する(埋め込みフォントを使わないようにする。メイリオ 14ptを使うように変更)。

diff -ur minimalcomps/com/bit101/components/Component.as 20130413localfont/src/com/bit101/components/Component.as
--- minimalcomps/com/bit101/components/Component.as 2011-04-10 03:35:53.000000000 +0900
+++ 20130413localfont/src/com/bit101/components/Component.as    2013-04-13 14:43:39.376216100 +0900
@@ -50,10 +50,10 @@
        // So if you are using the Flex 3.x sdk compiler, switch the embed statment below to expose the correct version.
        
        // Flex 4.x sdk:
-       [Embed(source="/assets/pf_ronda_seven.ttf", embedAsCFF="false", fontName="PF Ronda Seven", mimeType="application/x-font")]
+//     [Embed(source="/assets/pf_ronda_seven.ttf", embedAsCFF="false", fontName="PF Ronda Seven", mimeType="application/x-font")]
        // Flex 3.x sdk:
//      [Embed(source="/assets/pf_ronda_seven.ttf", fontName="PF Ronda Seven", mimeType="application/x-font")]
-       protected var Ronda:Class;
+//     protected var Ronda:Class;
        
        protected var _width:Number = 0;
        protected var _height:Number = 0;
diff -ur minimalcomps/com/bit101/components/Style.as 20130413localfont/src/com/bit101/components/Style.as
--- minimalcomps/com/bit101/components/Style.as 2011-04-10 03:35:53.000000000 +0900
+++ 20130413localfont/src/com/bit101/components/Style.as    2013-04-13 14:49:45.144216100 +0900
@@ -46,9 +46,9 @@
        public static var LIST_SELECTED:uint = 0xCCCCCC;
        public static var LIST_ROLLOVER:uint = 0XDDDDDD;
        
-       public static var embedFonts:Boolean = true;
-       public static var fontName:String = "PF Ronda Seven";
-       public static var fontSize:Number = 8;
+       public static var embedFonts:Boolean = false;
+       public static var fontName:String = "Meiryo";
+       public static var fontSize:Number = 14;
        
        public static const DARK:String = "dark";
        public static const LIGHT:String = "light";

そしてbuild。

MinimalCompsのLicense ( https://github.com/minimalcomps/minimalcomps )

Copyright (c) 2011 Keith Peters

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

参考リンク

0 件のコメント:

コメントを投稿

人気記事