Annotation of nono/util/mkcgrom/bdfcut.php, revision 1.1.1.2

1.1       root        1: <?php
                      2: //
                      3: // BDF ファイルから必要な範囲だけ抜き出す
                      4: // Copyright (C) 2012 [email protected]
                      5: //
                      6: 
1.1.1.2 ! root        7: // 書式
        !             8: //     bdfcut.php [command] < in.bdf > out.bdf
        !             9: //
        !            10: // 解説
        !            11: //     BDF ファイルから必要な範囲だけを抜き出します。
        !            12: //     標準入力から読み込み、標準出力に書き出します。
        !            13: //
        !            14: //     範囲は command で指定します。今のところ以下が使用できます。
        !            15: //
        !            16: //       h : Unicode の BDF からひらがなのみを抜き出します。
        !            17: //       r : Unicode の BDF からひらがな、カタカナのみを抜き出します。
        !            18: //       k : JISX0208 の BDF から非漢字のみを抜き出します。
1.1       root       19: 
                     20:        $opt = $argv[1];
                     21: 
1.1.1.2 ! root       22:        $range = array();
1.1       root       23: 
1.1.1.2 ! root       24:        // オプションによって出力範囲を指定
1.1       root       25:        if ($opt == "h") {
1.1.1.2 ! root       26:                // Unicode のひらがなのみ取り出す
        !            27:                $range[] = array(0x3041, 0x309f);
        !            28:        } else if ($opt == "r") {
        !            29:                // Unicode のひらがな、カタカナを取り出す
        !            30:                $range[] = array(0x3041, 0x30fc);
        !            31:                // 、。
        !            32:                $range[] = array(0x3001, 0x3002);
        !            33:                // 「」
        !            34:                $range[] = array(0x300c, 0x300d);
        !            35:        } else if ($opt == "k") {
        !            36:                // JIS X 0208 の非漢字のみ取り出す
        !            37:                $range[] = array(0x2120, 0x287f);
1.1       root       38:        }
                     39: 
1.1.1.2 ! root       40:        // どのオプションもなければ終了
        !            41:        if (count($range) < 1) {
1.1       root       42:                fprintf(STDERR, "usage: {$argv[0]} [command] < in.bdf > out.bdf\n");
                     43:                fprintf(STDERR, "   h: hiragana only in unicode\n");
1.1.1.2 ! root       44:                fprintf(STDERR, "   r: hira and katakana in unicode\n");
        !            45:                fprintf(STDERR, "   k: non-kanji only in JIS X 0208\n");
1.1       root       46:                exit(1);
                     47:        }
                     48: 
1.1.1.2 ! root       49:        // プロパティ部分。
        !            50:        // o TTF から縦横比を変えて変換した BDF には QUAD_WIDTH が含まれてない
        !            51:        //   ので、ついでにここで追加してやる。
1.1       root       52:        $pixel_size = -1;
                     53:        $quad_width = -1;
                     54:        $res_x = -1;
                     55:        $res_y = -1;
                     56:        while (($buf = fgets(STDIN))) {
                     57:                if (preg_match("/^PIXEL_SIZE (\d+)/", $buf, $m)) {
                     58:                        $pixel_size = $m[1] + 0;
                     59: 
                     60:                } else if (preg_match("/^QUAD_WIDTH (\d+)/", $buf, $m)) {
                     61:                        $quad_width = $m[1] + 0;
                     62: 
                     63:                } else if (preg_match("/^RESOLUTION_X (\d+)/", $buf, $m)) {
                     64:                        $res_x = $m[1] + 0;
                     65: 
                     66:                } else if (preg_match("/^RESOLUTION_Y (\d+)/", $buf, $m)) {
                     67:                        $res_y = $m[1] + 0;
                     68: 
                     69:                } else if (preg_match("/^ENDPROPERTIES/", $buf)) {
1.1.1.2 ! root       70:                        // h モードで QUAD_WIDTH がなければ作成
1.1       root       71:                        if ($opt == "h" && $quad_width == -1) {
                     72:                                if ($pixel_size > 0 && $res_x > 0 && $res_y > 0) {
                     73:                                        $quad_width = intval($pixel_size * $res_x / $res_y);
                     74:                                        print "QUAD_WIDTH {$quad_width}\n";
                     75:                                } else {
                     76:                                        fprintf(STDERR, "Warning: cannot generate QUAD_WIDTH\n");
                     77:                                }
                     78:                        }
                     79: 
1.1.1.2 ! root       80:                        // ここで自身も出力して終了
1.1       root       81:                        print $buf;
                     82:                        break;
                     83:                }
                     84:                print $buf;
                     85:        }
                     86: 
1.1.1.2 ! root       87:        // 本体ループ
1.1       root       88:        $body = "";
                     89:        $ischar = false;
                     90:        $num = 0;
                     91:        while (($buf = fgets(STDIN))) {
                     92:                if ($ischar) {
                     93:                        $body .= $buf;
                     94: 
1.1.1.2 ! root       95:                        // ENDCHAR を書き出したら一旦終わり
1.1       root       96:                        if (preg_match("/^ENDCHAR/", $buf)) {
                     97:                                $ischar = false;
                     98:                        }
                     99: 
                    100:                } else if (preg_match("/^STARTCHAR ([\d\w]+)/", $buf, $m)) {
                    101:                        list($code) = sscanf($m[1], "%x");
1.1.1.2 ! root      102:                        if (inrange($code)) {
1.1       root      103:                                $body .= $buf;
                    104:                                $ischar = true;
                    105:                                $num++;
                    106:                        }
                    107:                }
                    108:        }
                    109: 
1.1.1.2 ! root      110:        // 文字数とともに残りを出力
1.1       root      111:        print "CHARS {$num}\n";
                    112:        print "{$body}";
                    113: ?>
1.1.1.2 ! root      114: <?php
        !           115: function inrange($code)
        !           116: {
        !           117:        global $range;
        !           118: 
        !           119:        foreach ($range as $r) {
        !           120:                if ($r[0] <= $code && $code <= $r[1]) {
        !           121:                        return true;
        !           122:                }
        !           123:        }
        !           124:        return false;
        !           125: }
        !           126: ?>

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.