Squeak Smalltalk で万華鏡を書いてみた件 2


id:sumim:20080719:p1 に関連して、念のため、グラフィックスの扱いが手軽な Squeak システムで万華鏡を書くなら、オーソドックスにはこうだろうという例も書きました。

[Sensor anyButtonPressed] whileFalse: [
    | original mirror |
    original := Form fromDisplay: (Sensor cursorPoint extent: 100@100).
    original display.
    (mirror := original flipBy: #vertical centerAt: original center) display.
    (original flipBy: #horizontal centerAt: original center) display.
    (mirror flipBy: #horizontal centerAt: original center) display]


http://squab.no-ip.com/collab/uploads/61/squeakmngky.png



ついでに、変態向けのも。w

フォントをひっくり返すことはできず

http://d.hatena.ne.jp/hrkt0115311/20080718/kaleidoscope


それは是非ともひっくり返さねば! ということで、グリフをいじった鏡像フォントを用意して、出力文字列中の各文字もひっくり返るようにしました。

| 万華鏡の具 行列 行ペア群 左右逆 上下逆 上下左右逆 結果 出力 |

万華鏡の具 := ($あ to: $ん) collect: [:各々 | 各々 asString].

行列 := Matrix new: 5 element: ' '.
行ペア群 := {行列. 行列 copy} collect: [:各々 | {各々. 行列 copy}].

"具をまき散らす"
25 timesRepeat: [行列 at: 5 atRandom at: 5 atRandom put: 万華鏡の具 atRandom].

"鏡像フォントの作成"
左右逆 := TextStyle defaultFont copy.
(左右逆 fontArray at: 6) setGlyphs: (左右逆 glyphsEncoding: 5) deepCopy.
上下逆 := TextStyle defaultFont copy.
(上下逆 fontArray at: 6) setGlyphs: (上下逆 glyphsEncoding: 5) deepCopy.
上下左右逆 := TextStyle defaultFont copy.
(上下左右逆 fontArray at: 6) setGlyphs: (上下左右逆 glyphsEncoding: 5) deepCopy.
万華鏡の具 do: [:各々 |
    左右逆 characterFormAt: 各々 first
        put: ((左右逆 characterFormAt: 各々 first) flipBy: #horizontal centerAt: 0@0).
    上下逆 characterFormAt: 各々 first
        put: ((上下逆 characterFormAt: 各々 first) flipBy: #vertical centerAt: 0@0).
    上下左右逆 characterFormAt: 各々 first
        put: ((上下左右逆 characterFormAt: 各々 first) flipBy: #horizontal centerAt: 0@0).
    上下左右逆 characterFormAt: 各々 first
        put: ((上下左右逆 characterFormAt: 各々 first) flipBy: #vertical centerAt: 0@0).].

"鏡像の生成"
1 to: 5 do: [:# |
    行ペア群 second first atRow: 6-# put: (
        (行列 atRow: #) collect: [:各々 |
            Text string: 各々 attribute: (TextFontReference toFont: 上下逆)])].
1 to: 5 do: [:# |
    行ペア群 first second atColumn: 6-#  put: (
        (行列 atColumn: #) collect: [:各々 |
            Text string: 各々 attribute: (TextFontReference toFont: 左右逆)]).
    行ペア群 second second atColumn: 6-# put: (
        (行ペア群 second first atColumn: #) collect: [:各々 |
            Text string: 各々 asString attribute: (TextFontReference toFont: 上下左右逆)])].

結果 := 行列, 行ペア群 first second,, (行ペア群 second first, 行ペア群 second second).

"結果をクリップボードへ"
出力 := Text new writeStream.
1 to: 結果 rowCount do: [:行# |
    出力 cr.
    (結果 atRow: 行#) do: [:各々 | 出力 nextPutAll: 各々] separatedBy: [出力 space]].
Clipboard clipboardText: 出力 contents


トランスクリプト(Transcript。Squeak 環境の標準出力のようなもの)は、オブジェクトを出力時に #toString して文字列化しまうためフォント付きテキストのフォント情報(今回はこれが肝心!)が抜け落ちてしまうので、出力先をクリップボードにしています。スクリプトを評価後、Squeak 環境内でペースト操作をすると、文字も反転した出力結果が得られます。


“具”は、記号のたぐいだとせっかく作った鏡面フォントのありがたみが出にくいので、あ〜ん に変えてみました。

http://squab.no-ip.com/collab/uploads/61/mngky.3.png