diff options
Diffstat (limited to 'ext/tk/lib/tk/font.rb')
-rw-r--r-- | ext/tk/lib/tk/font.rb | 842 |
1 files changed, 471 insertions, 371 deletions
diff --git a/ext/tk/lib/tk/font.rb b/ext/tk/lib/tk/font.rb index 92f3a82434..389f001f44 100644 --- a/ext/tk/lib/tk/font.rb +++ b/ext/tk/lib/tk/font.rb @@ -163,6 +163,82 @@ class TkFont ################################### # class methods ################################### + def TkFont.actual(fnt, option=nil) + if fnt.kind_of?(TkFont) + fnt.actual(option) + else + actual_core(fnt, nil, option) + end + end + + def TkFont.actual_displayof(fnt, win, option=nil) + if fnt.kind_of?(TkFont) + fnt.actual_displayof(win, option) + else + win = '.' unless win + actual_core(fnt, win, option) + end + end + + def TkFont.configure(fnt, slot, value=None) + if fnt.kind_of?(TkFont) + fnt.configure(fnt, slot, value) + else + configure_core(fnt, slot, value) + end + fnt + end + + def TkFont.configinfo(fnt, slot=nil) + if fnt.kind_of?(TkFont) + fnt.configinfo(fnt, slot) + else + configinfo_core(fnt, slot) + end + end + + def TkFont.current_configinfo(fnt, slot=nil) + if fnt.kind_of?(TkFont) + fnt.current_configinfo(fnt, slot) + else + current_configinfo_core(fnt, slot) + end + end + + def TkFont.measure(fnt, text) + if fnt.kind_of?(TkFont) + fnt.measure(text) + else + measure_core(fnt, nil, text) + end + end + + def TkFont.measure_displayof(fnt, win, text) + if fnt.kind_of?(TkFont) + fnt.measure_displayof(win, text) + else + win = '.' unless win + measure_core(fnt, win, text) + end + end + + def TkFont.metrics(fnt, option=nil) + if fnt.kind_of?(TkFont) + fnt.metrics(option) + else + metrics_core(fnt, nil, option) + end + end + + def TkFont.metrics_displayof(fnt, win, option=nil) + if fnt.kind_of?(TkFont) + font.metrics_displayof(win, option=nil) + else + win = '.' unless win + metrics_core(fnt, win, option) + end + end + def TkFont.families(win=nil) case (Tk::TK_VERSION) when /^4\.*/ @@ -675,12 +751,380 @@ class TkFont end end + ################################### + public + ################################### + def method_missing(id, *args) + name = id.id2name + case args.length + when 1 + configure name, args[0] + when 0 + begin + configinfo name + rescue + fail NameError, "undefined local variable or method `#{name}' for #{self.to_s}", error_at + end + else + fail NameError, "undefined method `#{name}' for #{self.to_s}", error_at + end + end + + def call_font_configure(path, *args) + if path.kind_of?(Array) + # [path, optkey] + win, tag = path[0].split(';') + optkey = path[1].to_s + else + win, tag, optkey = path.split(';') + end + + fontslot = _symbolkey2str(@fontslot) + if optkey && optkey != "" + ltn = fontslot.delete('font') + knj = fontslot.delete('kanjifont') + fontslot[optkey] = ltn if ltn + fontslot["kanji#{optkey}"] = knj if knj + end + + keys = _symbolkey2str(args.pop).update(fontslot) + args.concat(hash_kv(keys)) + tk_call(*args) + Tk_FontUseTBL[[win, tag, optkey].join(';')] = self + self + end + + def used + ret = [] + Tk_FontUseTBL.each{|key,value| + next unless self == value + if key.include?(';') + win, tag, optkey = key.split(';') + winobj = tk_tcl2ruby(win) + if winobj.kind_of? TkText + if optkey + ret.push([winobj, winobj.tagid2obj(tag), optkey]) + else + ret.push([winobj, winobj.tagid2obj(tag)]) + end + elsif winobj.kind_of? TkCanvas + if (tagobj = TkcTag.id2obj(winobj, tag)).kind_of? TkcTag + if optkey + ret.push([winobj, tagobj, optkey]) + else + ret.push([winobj, tagobj]) + end + elsif (tagobj = TkcItem.id2obj(winobj, tag)).kind_of? TkcItem + if optkey + ret.push([winobj, tagobj, optkey]) + else + ret.push([winobj, tagobj]) + end + else + if optkey + ret.push([winobj, tag, optkey]) + else + ret.push([winobj, tag]) + end + end + elsif winobj.kind_of? TkMenu + if optkey + ret.push([winobj, tag, optkey]) + else + ret.push([winobj, tag]) + end + else + if optkey + ret.push([win, tag, optkey]) + else + ret.push([win, tag]) + end + end + else + ret.push(tk_tcl2ruby(key)) + end + } + ret + end + + def id + @id + end + + def to_eval + font + end + + def font + @compoundfont + end + alias font_id font + + def latin_font_id + @latinfont + end + + def latin_font + # @latinfont + if @latin_descendant + @latin_descendant + else + @latin_descendant = DescendantFont.new(self, 'latin') + end + end + alias latinfont latin_font + + def kanji_font_id + @kanjifont + end + + def kanji_font + # @kanjifont + if @kanji_descendant + @kanji_descendant + else + @kanji_descendant = DescendantFont.new(self, 'kanji') + end + end + alias kanjifont kanji_font + + def actual(option=nil) + actual_core(@compoundfont, nil, option) + end + + def actual_displayof(win, option=nil) + win = '.' unless win + actual_core(@compoundfont, win, option) + end + + def latin_actual(option=nil) + actual_core(@latinfont, nil, option) + end + + def latin_actual_displayof(win, option=nil) + win = '.' unless win + actual_core(@latinfont, win, option) + end + + def kanji_actual(option=nil) + #if JAPANIZED_TK + if @kanjifont != "" + actual_core(@kanjifont, nil, option) + else + actual_core_tk4x(nil, nil, option) + end + end + + def kanji_actual_displayof(win, option=nil) + #if JAPANIZED_TK + if @kanjifont != "" + win = '.' unless win + actual_core(@kanjifont, win, option) + else + actual_core_tk4x(nil, win, option) + end + end + + def [](slot) + configinfo slot + end + + def []=(slot, val) + configure slot, val + val + end + + def configure(slot, value=None) + configure_core(@compoundfont, slot, value) + self + end + + def configinfo(slot=nil) + configinfo_core(@compoundfont, slot) + end + + def current_configinfo(slot=nil) + current_configinfo_core(@compoundfont, slot) + end + + def delete + delete_core + end + + def latin_configure(slot, value=None) + if JAPANIZED_TK + configure_core(@latinfont, slot, value) + else + configure(slot, value) + end + self + end + + def latin_configinfo(slot=nil) + if JAPANIZED_TK + configinfo_core(@latinfont, slot) + else + configinfo(slot) + end + end + + def kanji_configure(slot, value=None) + #if JAPANIZED_TK + if @kanjifont != "" + configure_core(@kanjifont, slot, value) + configure('size'=>configinfo('size')) # to reflect new configuration + else + #"" + configure(slot, value) + end + self + end + + def kanji_configinfo(slot=nil) + #if JAPANIZED_TK + if @kanjifont != "" + configinfo_core(@kanjifont, slot) + else + #[] + configinfo(slot) + end + end + + def replace(ltn, knj=None) + knj = ltn if knj == None + latin_replace(ltn) + kanji_replace(knj) + self + end + + def latin_replace(ltn) + latin_replace_core(ltn) + reset_pointadjust + self + end + + def kanji_replace(knj) + kanji_replace_core(knj) + reset_pointadjust + self + end + + def measure(text) + measure_core(@compoundfont, nil, text) + end + + def measure_displayof(win, text) + win = '.' unless win + measure_core(@compoundfont, win, text) + end + + def metrics(option=nil) + metrics_core(@compoundfont, nil, option) + end + + def metrics_displayof(win, option=nil) + win = '.' unless win + metrics_core(@compoundfont, win, option) + end + + def latin_metrics(option=nil) + metrics_core(@latinfont, nil, option) + end + + def latin_metrics_displayof(win, option=nil) + win = '.' unless win + metrics_core(@latinfont, win, option) + end + + def kanji_metrics(option=nil) + if JAPANIZED_TK + metrics_core(@kanjifont, nil, option) + else + metrics_core_tk4x(nil, nil, option) + end + end + + def kanji_metrics_displayof(win, option=nil) + if JAPANIZED_TK + win = '.' unless win + metrics_core(@kanjifont, win, option) + else + metrics_core_tk4x(nil, win, option) + end + end + + def reset_pointadjust + begin + if /^8\.*/ === Tk::TK_VERSION && JAPANIZED_TK + configure('pointadjust' => latin_actual.assoc('size')[1].to_f / + kanji_actual.assoc('size')[1].to_f ) + end + rescue + end + self + end + + ################################### + # private alias + ################################### + case (Tk::TK_VERSION) + when /^4\.*/ + alias create_latinfont create_latinfont_tk4x + alias create_kanjifont create_kanjifont_tk4x + alias create_compoundfont create_compoundfont_tk4x + + when /^8\.[0-5]/ + alias create_latinfont create_latinfont_tk8x + alias create_kanjifont create_kanjifont_tk8x + alias create_compoundfont create_compoundfont_tk8x + + else + alias create_latinfont create_latinfont_tk8x + alias create_kanjifont create_kanjifont_tk8x + alias create_compoundfont create_compoundfont_tk8x + + end + + ################################### + # public alias + ################################### + alias ascii_font latin_font + alias asciifont latinfont + alias create_asciifont create_latinfont + alias ascii_actual latin_actual + alias ascii_actual_displayof latin_actual_displayof + alias ascii_configure latin_configure + alias ascii_configinfo latin_configinfo + alias ascii_replace latin_replace + alias ascii_metrics latin_metrics + + ################################### + def dup + src = self + obj = super() + obj.instance_eval{ initialize(src) } + obj + end + def clone + src = self + obj = super() + obj.instance_eval{ initialize(src) } + obj + end +end + +module TkFont::CoreMethods + include Tk + extend TkCore + + private + def actual_core_tk4x(font, win=nil, option=nil) # dummy if option == 'pointadjust' || option == :pointadjust 1.0 elsif option - case OptionType[option.to_s] + case TkFont::OptionType[option.to_s] when ?n 0 when ?b @@ -705,7 +1149,7 @@ class TkFont else val = tk_call('font', 'actual', font, "-#{option}") end - case OptionType[option.to_s] + case TkFont::OptionType[option.to_s] when ?n num_or_str(val) when ?b @@ -727,7 +1171,7 @@ class TkFont else key = key[1..-1] val = l.shift - case OptionType[key] + case TkFont::OptionType[key] when ?n r.push [key, num_or_str(val)] when ?b @@ -752,7 +1196,7 @@ class TkFont if option == 'pointadjust' || option == :pointadjust 1.0 elsif option - case OptionType[option.to_s] + case TkFont::OptionType[option.to_s] when ?n 0 when ?b @@ -772,7 +1216,7 @@ class TkFont def current_configinfo_core_tk4x(font, option=nil) if option - case OptionType[option.to_s] + case TkFont::OptionType[option.to_s] when ?n 0 when ?b @@ -860,7 +1304,7 @@ class TkFont "" elsif option val = tk_call('font', 'configure', font, "-#{option}") - case OptionType[option.to_s] + case TkFont::OptionType[option.to_s] when ?n num_or_str(val) when ?b @@ -877,7 +1321,7 @@ class TkFont else key = key[1..-1] val = l.shift - case OptionType[key] + case TkFont::OptionType[key] when ?n r.push [key, num_or_str(val)] when ?b @@ -899,7 +1343,7 @@ class TkFont "" elsif option val = tk_call('font', 'configure', font, "-#{option}") - case OptionType[option.to_s] + case TkFont::OptionType[option.to_s] when ?n num_or_str(val) when ?b @@ -916,7 +1360,7 @@ class TkFont else key = key[1..-1] val = l.shift - case OptionType[key] + case TkFont::OptionType[key] when ?n r.push [key, num_or_str(val)] when ?b @@ -931,8 +1375,8 @@ class TkFont end def delete_core_tk4x - Tk_FontNameTBL.delete(@id) - Tk_FontUseTBL.delete_if{|key,value| value == self} + TkFont::Tk_FontNameTBL.delete(@id) + TkFont::Tk_FontUseTBL.delete_if{|key,value| value == self} end def delete_core_tk8x @@ -948,15 +1392,15 @@ class TkFont tk_call('font', 'delete', @compoundfont) rescue end - Tk_FontNameTBL.delete(@id) - Tk_FontUseTBL.delete_if{|key,value| value == self} + TkFont::Tk_FontNameTBL.delete(@id) + TkFont::Tk_FontUseTBL.delete_if{|key,value| value == self} end def latin_replace_core_tk4x(ltn) create_latinfont_tk4x(ltn) @compoundfont[0] = [@latinfont] if JAPANIZED_TK @fontslot['font'] = @latinfont - Tk_FontUseTBL.dup.each{|w, fobj| + TkFont::Tk_FontUseTBL.dup.each{|w, fobj| if self == fobj begin if w.include?(';') @@ -978,7 +1422,7 @@ class TkFont tk_call(w, 'configure', '-font', @latinfont) end rescue - Tk_FontUseTBL.delete(w) + TkFont::Tk_FontUseTBL.delete(w) end end } @@ -991,7 +1435,7 @@ class TkFont create_kanjifont_tk4x(knj) @compoundfont[1] = [@kanjifont] @fontslot['kanjifont'] = @kanjifont - Tk_FontUseTBL.dup.each{|w, fobj| + TkFont::Tk_FontUseTBL.dup.each{|w, fobj| if self == fobj begin if w.include?(';') @@ -1013,7 +1457,7 @@ class TkFont tk_call(w, 'configure', '-kanjifont', @kanjifont) end rescue - Tk_FontUseTBL.delete(w) + TkFont::Tk_FontUseTBL.delete(w) end end } @@ -1030,7 +1474,7 @@ class TkFont fnt_bup = tk_call('font', 'create', '@font_tmp', '-copy', @latinfont) rescue #fnt_bup = '' - fnt_bup = DEFAULT_LATIN_FONT_NAME + fnt_bup = TkFont::DEFAULT_LATIN_FONT_NAME end end @@ -1093,7 +1537,7 @@ class TkFont fnt_bup = tk_call('font', 'create', '@font_tmp', '-copy', @kanjifont) rescue #fnt_bup = '' - fnt_bup = DEFAULT_KANJI_FONT_NAME + fnt_bup = TkFont::DEFAULT_KANJI_FONT_NAME end end @@ -1124,16 +1568,16 @@ class TkFont self end - def measure_core_tk4x(win, text) + def measure_core_tk4x(font, win, text) 0 end - def measure_core_tk8x(win, text) + def measure_core_tk8x(font, win, text) if win - number(tk_call('font', 'measure', @compoundfont, + number(tk_call('font', 'measure', font, '-displayof', win, text)) else - number(tk_call('font', 'measure', @compoundfont, text)) + number(tk_call('font', 'measure', font, text)) end end @@ -1173,9 +1617,6 @@ class TkFont ################################### case (Tk::TK_VERSION) when /^4\.*/ - alias create_latinfont create_latinfont_tk4x - alias create_kanjifont create_kanjifont_tk4x - alias create_compoundfont create_compoundfont_tk4x alias actual_core actual_core_tk4x alias configure_core configure_core_tk4x alias configinfo_core configinfo_core_tk4x @@ -1187,9 +1628,6 @@ class TkFont alias metrics_core metrics_core_tk4x when /^8\.[0-5]/ - alias create_latinfont create_latinfont_tk8x - alias create_kanjifont create_kanjifont_tk8x - alias create_compoundfont create_compoundfont_tk8x alias actual_core actual_core_tk8x alias configure_core configure_core_tk8x alias configinfo_core configinfo_core_tk8x @@ -1201,9 +1639,6 @@ class TkFont alias metrics_core metrics_core_tk8x else - alias create_latinfont create_latinfont_tk8x - alias create_kanjifont create_kanjifont_tk8x - alias create_compoundfont create_compoundfont_tk8x alias actual_core actual_core_tk8x alias configure_core configure_core_tk8x alias configinfo_core configinfo_core_tk8x @@ -1215,344 +1650,9 @@ class TkFont alias metrics_core metrics_core_tk8x end +end - ################################### - public - ################################### - def method_missing(id, *args) - name = id.id2name - case args.length - when 1 - configure name, args[0] - when 0 - begin - configinfo name - rescue - fail NameError, "undefined local variable or method `#{name}' for #{self.to_s}", error_at - end - else - fail NameError, "undefined method `#{name}' for #{self.to_s}", error_at - end - end - - def call_font_configure(path, *args) - if path.kind_of?(Array) - # [path, optkey] - win, tag = path[0].split(';') - optkey = path[1].to_s - else - win, tag, optkey = path.split(';') - end - - fontslot = _symbolkey2str(@fontslot) - if optkey && optkey != "" - ltn = fontslot.delete('font') - knj = fontslot.delete('kanjifont') - fontslot[optkey] = ltn if ltn - fontslot["kanji#{optkey}"] = knj if knj - end - - keys = _symbolkey2str(args.pop).update(fontslot) - args.concat(hash_kv(keys)) - tk_call(*args) - Tk_FontUseTBL[[win, tag, optkey].join(';')] = self - self - end - - def used - ret = [] - Tk_FontUseTBL.each{|key,value| - next unless self == value - if key.include?(';') - win, tag, optkey = key.split(';') - winobj = tk_tcl2ruby(win) - if winobj.kind_of? TkText - if optkey - ret.push([winobj, winobj.tagid2obj(tag), optkey]) - else - ret.push([winobj, winobj.tagid2obj(tag)]) - end - elsif winobj.kind_of? TkCanvas - if (tagobj = TkcTag.id2obj(winobj, tag)).kind_of? TkcTag - if optkey - ret.push([winobj, tagobj, optkey]) - else - ret.push([winobj, tagobj]) - end - elsif (tagobj = TkcItem.id2obj(winobj, tag)).kind_of? TkcItem - if optkey - ret.push([winobj, tagobj, optkey]) - else - ret.push([winobj, tagobj]) - end - else - if optkey - ret.push([winobj, tag, optkey]) - else - ret.push([winobj, tag]) - end - end - elsif winobj.kind_of? TkMenu - if optkey - ret.push([winobj, tag, optkey]) - else - ret.push([winobj, tag]) - end - else - if optkey - ret.push([win, tag, optkey]) - else - ret.push([win, tag]) - end - end - else - ret.push(tk_tcl2ruby(key)) - end - } - ret - end - - def id - @id - end - - def to_eval - font - end - - def font - @compoundfont - end - alias font_id font - - def latin_font_id - @latinfont - end - - def latin_font - # @latinfont - if @latin_descendant - @latin_descendant - else - @latin_descendant = DescendantFont.new(self, 'latin') - end - end - alias latinfont latin_font - - def kanji_font_id - @kanjifont - end - - def kanji_font - # @kanjifont - if @kanji_descendant - @kanji_descendant - else - @kanji_descendant = DescendantFont.new(self, 'kanji') - end - end - alias kanjifont kanji_font - - def actual(option=nil) - actual_core(@compoundfont, nil, option) - end - - def actual_displayof(win, option=nil) - win = '.' unless win - actual_core(@compoundfont, win, option) - end - - def latin_actual(option=nil) - actual_core(@latinfont, nil, option) - end - - def latin_actual_displayof(win, option=nil) - win = '.' unless win - actual_core(@latinfont, win, option) - end - - def kanji_actual(option=nil) - #if JAPANIZED_TK - if @kanjifont != "" - actual_core(@kanjifont, nil, option) - else - actual_core_tk4x(nil, nil, option) - end - end - - def kanji_actual_displayof(win, option=nil) - #if JAPANIZED_TK - if @kanjifont != "" - win = '.' unless win - actual_core(@kanjifont, win, option) - else - actual_core_tk4x(nil, win, option) - end - end - - def [](slot) - configinfo slot - end - - def []=(slot, val) - configure slot, val - val - end - - def configure(slot, value=None) - configure_core(@compoundfont, slot, value) - self - end - - def configinfo(slot=nil) - configinfo_core(@compoundfont, slot) - end - - def current_configinfo(slot=nil) - current_configinfo_core(@compoundfont, slot) - end - - def delete - delete_core - end - - def latin_configure(slot, value=None) - if JAPANIZED_TK - configure_core(@latinfont, slot, value) - else - configure(slot, value) - end - self - end - - def latin_configinfo(slot=nil) - if JAPANIZED_TK - configinfo_core(@latinfont, slot) - else - configinfo(slot) - end - end - - def kanji_configure(slot, value=None) - #if JAPANIZED_TK - if @kanjifont != "" - configure_core(@kanjifont, slot, value) - configure('size'=>configinfo('size')) # to reflect new configuration - else - #"" - configure(slot, value) - end - self - end - - def kanji_configinfo(slot=nil) - #if JAPANIZED_TK - if @kanjifont != "" - configinfo_core(@kanjifont, slot) - else - #[] - configinfo(slot) - end - end - - def replace(ltn, knj=None) - knj = ltn if knj == None - latin_replace(ltn) - kanji_replace(knj) - self - end - - def latin_replace(ltn) - latin_replace_core(ltn) - reset_pointadjust - self - end - - def kanji_replace(knj) - kanji_replace_core(knj) - reset_pointadjust - self - end - - def measure(text) - measure_core(nil, text) - end - - def measure_displayof(win, text) - win = '.' unless win - measure_core(win, text) - end - - def metrics(option=nil) - metrics_core(@compoundfont, nil, option) - end - - def metrics_displayof(win, option=nil) - win = '.' unless win - metrics_core(@compoundfont, win, option) - end - - def latin_metrics(option=nil) - metrics_core(@latinfont, nil, option) - end - - def latin_metrics_displayof(win, option=nil) - win = '.' unless win - metrics_core(@latinfont, win, option) - end - - def kanji_metrics(option=nil) - if JAPANIZED_TK - metrics_core(@kanjifont, nil, option) - else - metrics_core_tk4x(nil, nil, option) - end - end - - def kanji_metrics_displayof(win, option=nil) - if JAPANIZED_TK - win = '.' unless win - metrics_core(@kanjifont, win, option) - else - metrics_core_tk4x(nil, win, option) - end - end - - def reset_pointadjust - begin - if /^8\.*/ === Tk::TK_VERSION && JAPANIZED_TK - configure('pointadjust' => latin_actual.assoc('size')[1].to_f / - kanji_actual.assoc('size')[1].to_f ) - end - rescue - end - self - end - - ################################### - # public alias - ################################### - alias ascii_font latin_font - alias asciifont latinfont - alias create_asciifont create_latinfont - alias ascii_actual latin_actual - alias ascii_actual_displayof latin_actual_displayof - alias ascii_configure latin_configure - alias ascii_configinfo latin_configinfo - alias ascii_replace latin_replace - alias ascii_metrics latin_metrics - - ################################### - def dup - src = self - obj = super() - obj.instance_eval{ initialize(src) } - obj - end - def clone - src = self - obj = super() - obj.instance_eval{ initialize(src) } - obj - end +class TkFont + include TkFont::CoreMethods + extend TkFont::CoreMethods end |