2009年3月19日木曜日

Flex_SDKでAIRアプリを作る

AIRアプリは無料で作れる。
手順は次の通り。

1. Javaをダウンロード&インストールする。
2. Adobe® Flex® 3 SDKをダウンロード&インストールする。
3. Sample.mxmlを書いてコンパイルする。
$ amxmlc Sample.mxml
4. Sample-app.xmlを書いて実行してみる。
$ adl Sample-app.xml
5. パッケージングする。
$ adt -certificate -cn SelfSigned 1024-RSA Sample.pfx password
※自己署名証明書は1回作れば使い回せる
$ adt -package -storetype pkcs12 -keystore Sample.pfx -storepass password Sample.air Sample-app.xml Sample.swf icons
※この例ではアプリケーションにアイコンを設定しているのでパッケージにiconsディレクトリを含めている。

Sample.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
title="Sample"
invoke="invokeHandler()">
<mx:Style>
WindowedApplication
{
background-color:"0xffffff";
font-size:"12";
}
</mx:Style>
<mx:Script>
<![CDATA[
private static var PROLOGUE:String = '<div class="mycode1"><pre class="mycode2">';
private static var EPILOGUE:String = '</pre></div>';

private function invokeHandler():void {
_srcText.setFocus();
}
private function changeHandler(event:Event):void
{
_dstText.text = toMyCode(_srcText.text);
}
private function toMyCode(x:String):String
{
x = x.replace(/&/g, '&amp;');
x = x.replace(/</g, '&lt;');
x = x.replace(/>/g, '&gt;');
x = x.replace(/"/g, '&quot;');
return PROLOGUE + x + EPILOGUE;
}
]]>
</mx:Script>
<mx:HDividedBox width="100%" height="100%">
<mx:TextArea id="_srcText" width="50%" height="100%" change="changeHandler(event)"/>
<mx:TextArea id="_dstText" width="50%" height="100%" editable="false"/>
</mx:HDividedBox>
</mx:WindowedApplication>

Sample-app.xml
<?xml version="1.0" encoding="utf-8"?>
<application xmlns="http://ns.adobe.com/air/application/1.5">
<id>com.blogspot.takumakei.Sample</id>
<version>0.1</version>
<filename>Sample</filename>
<initialWindow>
<content>Sample.swf</content>
<visible>true</visible>
<!--<systemChrome>none</systemChrome>-->
<!--<transparent>true</transparent>-->
<width>320</width>
<height>240</height>
</initialWindow>
<icon>
<image16x16>icons/icon_016.png</image16x16>
<image32x32>icons/icon_032.png</image32x32>
<image48x48>icons/icon_048.png</image48x48>
<image128x128>icons/icon_128.png</image128x128>
</icon>
</application>


Adobe Developer Box

気になる。あ~気になる。でも今は時間がないから試せない。気になるぅ~。

Adobe Developer Box

成型用スクリプト

Bloggerは記事をHTMLで編集することができる。それに独自のCSSを編集することもできる。

記事にコードを書く時に使うスタイルを定義してみたので、ついでに、コードを投稿する時に整形するためのスクリプトも作ってみた。

カスタマイズのHTMLの編集のテンプレートの編集で定義したスタイルと成型用スクリプト。

/* MyCode
----------------------------------------------- */
.mycode1 {
border-width: 1px;
border-style: dashed;
border-color: maroon;
background: papayawhip;
}
.mycode2 {
color: black;
padding: 0 0 0 5px;
}

#!/usr/local/bin/ruby -Ku
print '<div class="mycode1"><pre class="mycode2">'
while line = gets
line.gsub!(/&/, '&amp;')
line.gsub!(/</, '&lt;')
line.gsub!(/>/, '&gt;')
line.gsub!(/"/, '&quot;')
print line
end
puts '</pre></div>'

Bluffでグラフ描画

以下、配布サイト(http://bluff.jcoglan.com/)からの抜粋。

BluffはGruff graphing library for RubyをJavaScriptに移植したもの。
他のライブラリへの依存を最低限にしつつ、Gruffの全ての機能をサポートするようにデザインした。
必要なサードパーティーのスクリプトはJS.Class(約2kb gzip圧縮時)と、Internet Explorerでcanvasをを使えるようにするGoogleのExCanvasだ。
これら2つのスクリプトはBluffと共に提供されている。Bluff自身はgzip圧縮時に約8kb程度。

とのこと。

で試しに書いたスクリプトが以下。

<html>
<head>
<script language="javascript" src="js-class.js" type="text/javascript"></script>
<script language="javascript" src="bluff-min.js" type="text/javascript"></script>
<script language="javascript" src="excanvas.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function drawSample(){
var g = new Bluff.Line('example', 800);
//g.theme_keynote();
//g.theme_37signals();
//g.theme_rails_keynote();
//g.theme_odeo();
//g.theme_pastel();
//g.theme_greyscale();
//g.hide_line_markers = true;
g.hide_dots = true;
//g.hide_legend = true;
g.minimum_value = 0;
g.maximum_value = 200;
g.x_axis_label = 'Year';
g.y_axis_increment = 25;
g.zero_degree = 90;
g.title = 'グラフ描画サンプル';
g.data('Apples', [1,2,3,4,4,3,4, 1,2,3,4,4,3,4]);
g.data('マンゴー',[3,4,5,6,7,8,9, 10,12,13,140,15,16]);
g.data('Oranges', [4,8,7,7,3,1,4, 4,8,7,7,3,1,4]);
g.data('なにか', [100, 120, 100, 119, 98, 102, 100, 100, 120, 100, 119, 98, 102, 100]);
g.labels = {0:'2003', 4:'2004', 8:'2005', 12:'2006' };
g.draw();
}
</script>
</head>
<body onload="drawSample()">
<canvas id="example" width="100%"></canvas>
</body>
</html>

2009年3月13日金曜日

Windowsでwhich

whichがないと何かと不便。
whichはとっても便利なのになんで標準装備されていないのだろう・・・

#! ruby

def search_dir(result, target_path)
puts " #{target_path}" if $DEBUG
Dir[target_path].each do |file|
unless result.index(file)
puts(file.gsub(/\//, '\\'))
result << file
end
end
end

def main(args)
if args.size == 0
puts "Usage error: which keyword"
exit
end

target = args[0]
result = []

(['.'] + ENV['PATH'].split(';')).each do |path|
path.sub!(/^"*/, '').sub!(/"*$/, '')
search_dir(result, File.expand_path(File.join(path, "#{target}")))
search_dir(result, File.expand_path(File.join(path, "#{target}.*")))
end
end

main(ARGV)

2009年3月5日木曜日

Win32APIでDLLの関数を呼ぶ

DLL
extern "C" {
struct P3 {
char* a;
char* b;
wchar_t* c;
};

void __declspec(dllexport) study2(P3* ptr)
{
ptr = ptr;
ptr->c[0] = L'希';
}
}
Ruby
#!/usr/local/bin/ruby -Ku
# -*- encoding: utf-8 -*-
require 'Win32API'
require 'nkf'

def sjis(x)
NKF.nkf('-m0 --ic=utf8 --oc=cp932', x)
end

def utf16le(x)
NKF.nkf('-m0 --ic=utf8 --oc=utf-16le', x)
end

fn_study2 = Win32API.new('Study.dll', 'study2', %w(p), 'v')
x = [ sjis("hello"), sjis("world"), utf16le("あかさた") ]
fn_study2.call(x.pack("ppp"))
puts(NKF.nkf('-m0 --ic=utf-16le --oc=cp932', x[2]))

2009年3月2日月曜日

RubyのWin32APIとUNICODE

Ruby-1.9.1でWin32APIを使ってUNICODE文字列を渡す例。
#ほとんどRubyリファレンスマニュアルのコピペ。

#!/usr/local/bin/ruby
# -*- encoding: utf-8 -*-

require 'iconv'
require 'Win32API'

class Win32API
MB_OK = 0

@@messagebox = nil

def self.MessageBox(wnd, text, caption, type = MB_OK)
@@messagebox ||= Win32API.new('user32', 'MessageBoxW', %W(p p p i), 'i')
@@messagebox.call(wnd, text, caption, type)
end
end

a = Iconv.iconv('utf-16le', 'utf-8', 'やぁ')[0]
b = Iconv.iconv('utf-16le', 'utf-8', 'うあは??')[0]
Win32API.MessageBox(0, a, b)