1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add Tk::EncodedString and Tk::UTF8_String class to support characters

using the \uXXXX escape to the UNICODE string.
sample/{demos-en,demos-jp}/unicodeout.rb are samples of Tk::UTF8_String.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nagai 2003-10-16 07:45:51 +00:00
parent e45c4cd1ac
commit 98a3a9db97
7 changed files with 301 additions and 29 deletions

View file

@ -1,3 +1,15 @@
Thu Oct 16 16:40:51 2003 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tk/lib/tk.rb:
Add Tk::EncodedString and Tk::UTF8_String class to support
characters using the \uXXXX escape to the UNICODE string.
* ext/tk/sample/{demos-en,demos-jp}/unicodeout.rb
new demo-scripts (samples of Tk::UTF8_String)
* ext/tk/sample/{demos-en,demos-jp}/widget
add entries for 'unicodeout.rb'
Thu Oct 16 08:38:06 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* test/digest/test_digest.rb (test_eq): show failed class.

View file

@ -1524,6 +1524,50 @@ module Tk
end
end
###########################################
# string with Tcl's encoding
###########################################
module Tk
class EncodedString < String
@@enc_buf = '__rb_encoding_buffer__'
def self.tk_escape(str)
s = '"' + str.gsub(/[\[\]$"]/, '\\\\\&') + '"'
TkCore::INTERP.__eval(format('global %s; set %s %s',
@@enc_buf, @@enc_buf, s))
end
def self.new(str, enc = Tk.encoding_system)
obj = super(self.tk_escape(str))
obj.instance_eval{@enc = enc}
obj
end
def self.new_without_escape(str, enc = Tk.encoding_system)
obj = super(str)
obj.instance_eval{@enc = enc}
obj
end
def encoding
@enc
end
end
def Tk.EncodedString(str, enc = Tk.encoding_system)
Tk::EncodedString.new(str, enc)
end
class UTF8_String < EncodedString
def self.new(str)
super(str, 'utf-8')
end
end
def Tk.UTF8_String(str)
Tk::UTF8_String.new(str)
end
end
###########################################
# convert kanji string to/from utf-8
###########################################
@ -1537,7 +1581,11 @@ if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK
def _eval(cmd)
if defined? @encoding
_fromUTF8(__eval(_toUTF8(cmd, @encoding)), @encoding)
if cmd.kind_of?(Tk::EncodedString)
_fromUTF8(__eval(_toUTF8(cmd, cmd.encoding)), @encoding)
else
_fromUTF8(__eval(_toUTF8(cmd, @encoding)), @encoding)
end
else
__eval(cmd)
end
@ -1545,7 +1593,13 @@ if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK
def _invoke(*cmds)
if defined? @encoding
cmds = cmds.collect{|cmd| _toUTF8(cmd, @encoding)}
cmds = cmds.collect{|cmd|
if cmd.kind_of?(Tk::EncodedString)
_toUTF8(cmd, cmd.encoding)
else
_toUTF8(cmd, @encoding)
end
}
_fromUTF8(__invoke(*cmds), @encoding)
else
__invoke(*cmds)

View file

@ -0,0 +1,100 @@
# unicodeout.rb --
#
# This demonstration script shows how you can produce output (in label
# widgets) using many different alphabets.
#
# based on Tcl/Tk8.4.4 widget demos
if defined?($unicodeout_demo) && $unicodeout_demo
$unicodeout_demo.destroy
$unicodeout_demo = nil
end
$unicodeout_demo = TkToplevel.new {|w|
title("Unicode Label Demonstration")
iconname("unicodeout")
positionWindow(w)
}
TkLabel.new($unicodeout_demo,
:font=>$font, :wraplength=>'4i', :justify=>:left,
:text=><<EOL).pack(:side=>:top)
This is a sample of Tk's support for languages that use non-Western \
character sets. However, what you will actually see below depends \
largely on what character sets you have installed, and what you see \
for characters that are not present varies greatly between platforms \
as well. The strings are written in Tcl using UNICODE characters \
using the \\uXXXX escape so as to do so in a portable fashion.
ATTENTION:
The strings are converted to the encoded string objects \
(completed to rewrite Tcl's escapes) by Tk::UTF8_String method. \
And the Tk::UTF8_String objects are passed to the label widgets.
EOL
TkFrame.new($unicodeout_demo){|f|
pack(:side=>:bottom, :fill=>:x, :pady=>'2m')
TkButton.new(f, :text=>'Dismiss', :width=>15, :command=>proc{
$unicodeout_demo.destroy
$unicodeout_demo = nil
}).pack(:side=>:left, :expand=>true)
TkButton.new(f, :text=>'See Code', :width=>15, :command=>proc{
showCode 'unicodeout'
}).pack(:side=>:left, :expand=>true)
}
wait_msg = TkLabel.new($unicodeout_demo,
:text=>"Please wait while loading fonts...",
:font=>"Helvetica 12 italic").pack
class Unicodeout_SampleFrame < TkFrame
@@font = $font
def initialize()
super($unicodeout_demo)
grid_columnconfig(1, :weight=>1)
end
def add_sample(lang, *args)
sample_txt = Tk::UTF8_String(args.join(''))
l = TkLabel.new(self, :font=>@@font, :text=>lang+':',
:anchor=>:nw, :pady=>0)
s = TkLabel.new(self, :font=>TkFont.new(@@font), :text=>sample_txt,
:anchor=>:nw, :width=>30, :pady=>0)
Tk.grid(l, s, :sticky=>:ew, :pady=>0)
l.grid_config(:padx, '1m')
end
end
f = Unicodeout_SampleFrame.new
f.pack(:expand=>true, :fill=>:both, :padx=>'2m', :pady=>'1m')
# Processing when some characters are missing might take a while, so make
# sure we're displaying something in the meantime...
oldCursor = $unicodeout_demo.cursor
$unicodeout_demo.cursor('watch')
Tk.update
f.add_sample('Arabic',
'\uFE94\uFEF4\uFE91\uFEAE\uFECC\uFEDF\uFE8D\uFE94',
'\uFEE4\uFEE0\uFEDC\uFEDF\uFE8D')
f.add_sample('Trad. Chinese', '\u4E2D\u570B\u7684\u6F22\u5B57')
f.add_sample('Simpl. Chinese', '\u6C49\u8BED')
f.add_sample('Greek',
'\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AE ',
'\u03B3\u03BB\u03CE\u03C3\u03C3\u03B1')
f.add_sample('Hebrew',
'\u05DD\u05D9\u05DC\u05E9\u05D5\u05E8\u05D9 ',
'\u05DC\u05D9\u05D0\u05E8\u05E9\u05D9')
f.add_sample('Japanese',
'\u65E5\u672C\u8A9E\u306E\u3072\u3089\u304C\u306A, ',
'\u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA')
f.add_sample('Korean', '\uB300\uD55C\uBBFC\uAD6D\uC758 \uD55C\uAE00')
f.add_sample('Russian',
'\u0420\u0443\u0441\u0441\u043A\u0438\u0439 ',
'\u044F\u0437\u044B\u043A')
wait_msg.destroy
$unicodeout_demo.cursor(oldCursor)

View file

@ -200,25 +200,27 @@ txt.insert('end', "Labels, buttons, checkbuttons, and radiobuttons.\n", tag_titl
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "1. Labels (text and bitmaps).\n", tag_demo, "demo-label")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "2. Buttons.\n", tag_demo, "demo-button")
txt.insert('end', "2. Labels and UNICODE text. (if supported)\n", tag_demo, "demo-unicodeout")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "3. Checkbuttons (select any of a group).\n", tag_demo, "demo-check")
txt.insert('end', "3. Buttons.\n", tag_demo, "demo-button")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "4. Radiobuttons (select one of a group).\n", tag_demo, "demo-radio")
txt.insert('end', "4. Checkbuttons (select any of a group).\n", tag_demo, "demo-check")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "5. Radiobuttons (if supported 'compound' option).\n", tag_demo, "demo-radio2")
txt.insert('end', "5. Radiobuttons (select one of a group).\n", tag_demo, "demo-radio")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "6. A 15-puzzle game made out of buttons.\n", tag_demo, "demo-puzzle")
txt.insert('end', "6. Radiobuttons (if supported 'compound' option).\n", tag_demo, "demo-radio2")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "7. Iconic buttons that use bitmaps.\n", tag_demo, "demo-icon")
txt.insert('end', "7. A 15-puzzle game made out of buttons.\n", tag_demo, "demo-puzzle")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "8. Two labels displaying images.\n", tag_demo, "demo-image1")
txt.insert('end', "8. Iconic buttons that use bitmaps.\n", tag_demo, "demo-icon")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "9. A simple user interface for viewing images.\n", tag_demo, "demo-image2")
txt.insert('end', "9. Two labels displaying images.\n", tag_demo, "demo-image1")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "10. A simple user interface for viewing images. (if supported)\n", tag_demo, "demo-image3")
txt.insert('end', "10. A simple user interface for viewing images.\n", tag_demo, "demo-image2")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "11. Labelled frames (if supported)\n", tag_demo, "demo-labelframe")
txt.insert('end', "11. A simple user interface for viewing images. (if supported)\n", tag_demo, "demo-image3")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "12. Labelled frames (if supported)\n", tag_demo, "demo-labelframe")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "\n")

View file

@ -29,7 +29,7 @@ TkLabel.new($entry3_demo,
\
\
\
\
\
\
\
\
@ -41,12 +41,12 @@ EOL
TkFrame.new($entry3_demo){|f|
pack(:side=>:bottom, :fill=>:x, :pady=>'2m')
TkButton.new(f, :text=>'Dismiss', :width=>15, :command=>proc{
TkButton.new(f, :text=>'了解', :width=>15, :command=>proc{
$entry3_demo.destroy
$entry3_demo = nil
}).pack(:side=>:left, :expand=>true)
TkButton.new(f, :text=>'See Code', :width=>15, :command=>proc{
TkButton.new(f, :text=>'コード参照', :width=>15, :command=>proc{
showCode 'entry3'
}).pack(:side=>:left, :expand=>true)
}
@ -71,7 +71,7 @@ def focusAndFlash(widget, fg, bg, count=5)
widget.focus(true)
end
l1 = TkLabelFrame.new($entry3_demo, :text=>"Integer Entry")
l1 = TkLabelFrame.new($entry3_demo, :text=>"整数エントリ")
TkEntry.new(l1, :validate=>:focus,
:vcmd=>[
proc{|s| s == '' || /^[+-]?\d+$/ =~ s }, '%P'
@ -80,7 +80,7 @@ TkEntry.new(l1, :validate=>:focus,
pack(:fill=>:x, :expand=>true, :padx=>'1m', :pady=>'1m')
}
l2 = TkLabelFrame.new($entry3_demo, :text=>"Length-Constrained Entry")
l2 = TkLabelFrame.new($entry3_demo, :text=>"長さ制約付きエントリ")
TkEntry.new(l2, :validate=>:key, :invcmd=>proc{Tk.bell},
:vcmd=>[proc{|s| s.length < 10}, '%P']
).pack(:fill=>:x, :expand=>true, :padx=>'1m', :pady=>'1m')
@ -167,7 +167,7 @@ def validatePhoneChange(widget, vmode, idx, char)
end
l3 = TkLabelFrame.new($entry3_demo, :text=>"US Phone-Number Entry")
l3 = TkLabelFrame.new($entry3_demo, :text=>"米国電話番号エントリ")
TkEntry.new(l3, :validate=>:key, :invcmd=>proc{Tk.bell},
:textvariable=>entry3content,
:vcmd=>[
@ -186,7 +186,7 @@ TkEntry.new(l3, :validate=>:key, :invcmd=>proc{Tk.bell},
pack(:fill=>:x, :expand=>true, :padx=>'1m', :pady=>'1m')
}
l4 = TkLabelFrame.new($entry3_demo, :text=>"Password Entry")
l4 = TkLabelFrame.new($entry3_demo, :text=>"パスワードエントリ")
TkEntry.new(l4, :validate=>:key, :show=>'*',
:vcmd=>[
proc{|s| s.length <= 8},

View file

@ -0,0 +1,102 @@
# unicodeout.rb --
#
# This demonstration script shows how you can produce output (in label
# widgets) using many different alphabets.
#
# based on Tcl/Tk8.4.4 widget demos
if defined?($unicodeout_demo) && $unicodeout_demo
$unicodeout_demo.destroy
$unicodeout_demo = nil
end
$unicodeout_demo = TkToplevel.new {|w|
title("Unicode Label Demonstration")
iconname("unicodeout")
positionWindow(w)
}
TkLabel.new($unicodeout_demo,
:font=>$font, :wraplength=>'4i', :justify=>:left,
:text=><<EOL).pack(:side=>:top)
Tkにおける非欧米文字集合を用いる言語に対するサポートについての\
\
\
\
\
\\uXXXXという\
Tclのエスケープ表現を用いたUNICODE文字列で書かれています
Tk::UTF8_StringメソッドによってUTF8形式の文字列であるという\
\
(Tclのエスケープ表現の変換済み)\
EOL
#'
TkFrame.new($unicodeout_demo){|f|
pack(:side=>:bottom, :fill=>:x, :pady=>'2m')
TkButton.new(f, :text=>'了解', :width=>15, :command=>proc{
$unicodeout_demo.destroy
$unicodeout_demo = nil
}).pack(:side=>:left, :expand=>true)
TkButton.new(f, :text=>'コード参照', :width=>15, :command=>proc{
showCode 'unicodeout'
}).pack(:side=>:left, :expand=>true)
}
wait_msg = TkLabel.new($unicodeout_demo,
:text=>"フォント読み込みの完了まで" +
"しばらくお待ち下さい...",
:font=>"Helvetica 12 italic").pack
class Unicodeout_SampleFrame < TkFrame
@@font = $font
def initialize()
super($unicodeout_demo)
grid_columnconfig(1, :weight=>1)
end
def add_sample(lang, *args)
sample_txt = Tk::UTF8_String(args.join(''))
l = TkLabel.new(self, :font=>@@font, :text=>lang+':',
:anchor=>:nw, :pady=>0)
s = TkLabel.new(self, :font=>TkFont.new(@@font), :text=>sample_txt,
:anchor=>:nw, :width=>30, :pady=>0)
Tk.grid(l, s, :sticky=>:ew, :pady=>0)
l.grid_config(:padx, '1m')
end
end
f = Unicodeout_SampleFrame.new
f.pack(:expand=>true, :fill=>:both, :padx=>'2m', :pady=>'1m')
# Processing when some characters are missing might take a while, so make
# sure we're displaying something in the meantime...
oldCursor = $unicodeout_demo.cursor
$unicodeout_demo.cursor('watch')
Tk.update
f.add_sample('Arabic',
'\uFE94\uFEF4\uFE91\uFEAE\uFECC\uFEDF\uFE8D\uFE94',
'\uFEE4\uFEE0\uFEDC\uFEDF\uFE8D')
f.add_sample('Trad. Chinese', '\u4E2D\u570B\u7684\u6F22\u5B57')
f.add_sample('Simpl. Chinese', '\u6C49\u8BED')
f.add_sample('Greek',
'\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AE ',
'\u03B3\u03BB\u03CE\u03C3\u03C3\u03B1')
f.add_sample('Hebrew',
'\u05DD\u05D9\u05DC\u05E9\u05D5\u05E8\u05D9 ',
'\u05DC\u05D9\u05D0\u05E8\u05E9\u05D9')
f.add_sample('Japanese',
'\u65E5\u672C\u8A9E\u306E\u3072\u3089\u304C\u306A, ',
'\u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA')
f.add_sample('Korean', '\uB300\uD55C\uBBFC\uAD6D\uC758 \uD55C\uAE00')
f.add_sample('Russian',
'\u0420\u0443\u0441\u0441\u043A\u0438\u0439 ',
'\u044F\u0437\u044B\u043A')
wait_msg.destroy
$unicodeout_demo.cursor(oldCursor)

View file

@ -211,33 +211,35 @@ txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "1. ラベル (テキスト, ビットマップ)\n",
tag_demo, "demo-label")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "2. ボタン \n", tag_demo, "demo-button")
txt.insert('end', "2. ラベルとUNICODEテキスト (機能に対応したバージョンのTkが必要)\n", tag_demo, "demo-unicodeout")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "3. チェックボタン (複数を選択可能)\n",
txt.insert('end', "3. ボタン \n", tag_demo, "demo-button")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "4. チェックボタン (複数を選択可能)\n",
tag_demo, "demo-check")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "4. ラジオボタン (任意の一つを選択可能)\n",
txt.insert('end', "5. ラジオボタン (任意の一つを選択可能)\n",
tag_demo, "demo-radio")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "5. ラジオボタン (機能に対応したバージョンのTkが必要)\n",
txt.insert('end', "6. ラジオボタン (機能に対応したバージョンのTkが必要)\n",
tag_demo, "demo-radio2")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "6. ボタンで作られた15-パズルゲーム\n",
txt.insert('end', "7. ボタンで作られた15-パズルゲーム\n",
tag_demo, "demo-puzzle")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "7. ビットマップを使用したアイコンボタン\n",
txt.insert('end', "8. ビットマップを使用したアイコンボタン\n",
tag_demo, "demo-icon")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "8. 画像を表示する二つのラベル\n",
txt.insert('end', "9. 画像を表示する二つのラベル\n",
tag_demo, "demo-image1")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "9. 画像を見るための簡単なユーザインターフェース\n",
txt.insert('end', "10. 画像を見るための簡単なユーザインターフェース\n",
tag_demo, "demo-image2")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "10. 画像を見るための簡単なユーザインターフェース (機能に対応したバージョンのTkが必要)\n",
txt.insert('end', "11. 画像を見るための簡単なユーザインターフェース (機能に対応したバージョンのTkが必要)\n",
tag_demo, "demo-image3")
txt.insert('end', " \n ", tag_demospace)
txt.insert('end', "11. ラベル付きフレーム (機能に対応したバージョンのTkが必要)\n",
txt.insert('end', "12. ラベル付きフレーム (機能に対応したバージョンのTkが必要)\n",
tag_demo, "demo-labelframe")
txt.insert('end', " \n ", tag_demospace)