ext/tk/lib/tkfont.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #  tkfont.rb - the class to treat fonts on Ruby/Tk
   3  #
   4  #                               by  Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
   5  #
   6  require 'tk'
   7  
   8  class TkFont
   9    include Tk
  10    extend TkCore
  11  
  12    Tk_FontID = [0]
  13    Tk_FontNameTBL = {}
  14    Tk_FontUseTBL = {}
  15  
  16    # set default font
  17    case Tk::TK_VERSION
  18    when /^4\.*/
  19      DEFAULT_LATIN_FONT_NAME = 'a14'.freeze
  20      DEFAULT_KANJI_FONT_NAME = 'k14'.freeze
  21    when /^8\.*/
  22      if JAPANIZED_TK
  23        begin
  24          fontnames = tk_call('font', 'names')
  25          case fontnames
  26          when /defaultgui/
  27            # Tcl/Tk-JP for Windows
  28            ltn = 'defaultgui'
  29            knj = 'defaultgui'
  30          when /Mincho:Helvetica-12/
  31            # Tcl/Tk-JP for UNIX/X
  32            ltn, knj = tk_split_simplelist(tk_call('font', 'configure', 
  33                                                   'Mincho:Helvetica-12', 
  34                                                   '-compound'))
  35          else
  36            # unknown Tcl/Tk-JP
  37            platform = tk_call('set', 'tcl_platform(platform)')
  38            case platform
  39            when 'unix'
  40              ltn = {'family'=>'Helvetica'.freeze, 'size'=>-12}
  41              knj = 'k14'
  42              #knj = '-misc-fixed-medium-r-normal--14-*-*-*-c-*-jisx0208.1983-0'
  43            when 'windows'
  44              ltn = {'family'=>'MS Sans Serif'.freeze, 'size'=>8}
  45              knj = 'mincho'
  46            when 'macintosh'
  47              ltn = 'system'
  48              knj = 'mincho'
  49            else # unknown
  50              ltn = 'Helvetica'
  51              knj = 'mincho'
  52            end
  53          end
  54        rescue
  55          ltn = 'Helvetica'
  56          knj = 'mincho'
  57        end
  58  
  59      else # not JAPANIZED_TK
  60        begin
  61          platform = tk_call('set', 'tcl_platform(platform)')
  62          case platform
  63          when 'unix'
  64            ltn = {'family'=>'Helvetica'.freeze, 'size'=>-12}
  65            knj = 'k14'
  66            #knj = '-misc-fixed-medium-r-normal--14-*-*-*-c-*-jisx0208.1983-0'
  67          when 'windows'
  68            ltn = {'family'=>'MS Sans Serif'.freeze, 'size'=>8}
  69            knj = 'mincho'
  70          when 'macintosh'
  71            ltn = 'system'
  72            knj = 'mincho'
  73          else # unknown
  74            ltn = 'Helvetica'
  75            knj = 'mincho'
  76          end
  77        rescue
  78          ltn = 'Helvetica'
  79          knj = 'mincho'
  80        end
  81      end
  82  
  83      DEFAULT_LATIN_FONT_NAME = ltn.freeze
  84      DEFAULT_KANJI_FONT_NAME = knj.freeze
  85  
  86    else # unknown version
  87      DEFAULT_LATIN_FONT_NAME = 'Helvetica'.freeze
  88      DEFAULT_KANJI_FONT_NAME = 'mincho'.freeze
  89  
  90    end
  91  
  92    if $DEBUG
  93      print "default latin font = "; p DEFAULT_LATIN_FONT_NAME
  94      print "default kanji font = "; p DEFAULT_KANJI_FONT_NAME
  95    end
  96  
  97    ###################################
  98    # class methods
  99    ###################################
 100    def TkFont.families(window=nil)
 101      case (Tk::TK_VERSION)
 102      when /^4\.*/
 103        ['fixed']
 104  
 105      when /^8\.*/
 106        if window
 107          tk_split_simplelist(tk_call('font', 'families', '-displayof', window))
 108        else
 109          tk_split_simplelist(tk_call('font', 'families'))
 110        end
 111      end
 112    end
 113  
 114    def TkFont.names
 115      case (Tk::TK_VERSION)
 116      when /^4\.*/
 117        r = ['fixed']
 118        r += ['a14', 'k14'] if JAPANIZED_TK
 119        Tk_FontNameTBL.each_value{|obj| r.push(obj)}
 120        r | []
 121  
 122      when /^8\.*/
 123        tk_split_simplelist(tk_call('font', 'names'))
 124  
 125      end
 126    end
 127  
 128    def TkFont.create_copy(font)
 129      fail 'source-font need to be TkFont' unless font.kind_of? TkFont
 130      keys = {}
 131      font.configinfo.each{|key,value| keys[key] = value }
 132      TkFont.new(font.latin_font, font.kanji_font, keys)
 133    end
 134  
 135    def TkFont.get_obj(name)
 136      if name =~ /^(@font[0-9]+)(|c|l|k)$/
 137        Tk_FontNameTBL[$1]
 138      else
 139        nil
 140      end
 141    end
 142  
 143    def TkFont.init_widget_font(path, *args)
 144      case (Tk::TK_VERSION)
 145      when /^4\.*/
 146        conf = tk_split_simplelist(tk_call(*args)).
 147          find_all{|prop| prop[0..5]=='-font ' || prop[0..10]=='-kanjifont '}.
 148          collect{|prop| tk_split_simplelist(prop)}
 149        if font_inf = conf.assoc('-font')
 150          ltn = font_inf[4]
 151          ltn = nil if ltn == []
 152        else 
 153          #ltn = nil
 154          raise RuntimeError, "unknown option '-font'"
 155        end
 156        if font_inf = conf.assoc('-kanjifont')
 157          knj = font_inf[4]
 158          knj = nil if knj == []
 159        else
 160          knj = nil
 161        end
 162        TkFont.new(ltn, knj).call_font_configure(path, *(args + [{}]))
 163  
 164      when /^8\.*/
 165        font_prop = tk_split_simplelist(tk_call(*args)).find{|prop| 
 166          prop[0..5] == '-font '
 167        }
 168        unless font_prop
 169          raise RuntimeError, "unknown option '-font'"
 170        end
 171        fnt = tk_split_simplelist(font_prop)[4]
 172        if fnt == ""
 173          TkFont.new(nil, nil).call_font_configure(path, *(args + [{}]))
 174        else
 175          begin
 176            compound = tk_split_simplelist(
 177              Hash[*tk_split_simplelist(tk_call('font', 'configure', 
 178                                                 fnt))].collect{|key,value|
 179                [key[1..-1], value]
 180              }.assoc('compound')[1])
 181          rescue
 182            compound = []
 183          end
 184          if compound == []
 185            #TkFont.new(fnt, DEFAULT_KANJI_FONT_NAME) \
 186            #.call_font_configure(path, *(args + [{}]))
 187            TkFont.new(fnt).call_font_configure(path, *(args + [{}]))
 188          else
 189            TkFont.new(compound[0], compound[1]) \
 190            .call_font_configure(path, *(args + [{}]))
 191          end
 192        end
 193      end
 194    end
 195  
 196    def TkFont.used_on(path=nil)
 197      if path
 198        Tk_FontUseTBL[path]
 199      else
 200        Tk_FontUseTBL.values | []
 201      end
 202    end
 203  
 204    def TkFont.failsafe(font)
 205      begin
 206        if /^8\.*/ === Tk::TK_VERSION  && JAPANIZED_TK
 207          tk_call('font', 'failsafe', font)
 208        end
 209      rescue
 210      end
 211    end
 212  
 213    ###################################
 214    private
 215    ###################################
 216    def initialize(ltn=DEFAULT_LATIN_FONT_NAME, knj=nil, keys=nil)
 217      @id = format("@font%.4d", Tk_FontID[0])
 218      Tk_FontID[0] += 1
 219      Tk_FontNameTBL[@id] = self
 220      knj = DEFAULT_KANJI_FONT_NAME if JAPANIZED_TK && !knj
 221      create_compoundfont(ltn, knj, keys)
 222    end
 223  
 224    def _get_font_info_from_hash(font)
 225      font = _symbolkey2str(font)
 226      foundry  = (info = font['foundry'] .to_s)?  info: '*'
 227      family   = (info = font['family']  .to_s)?  info: '*'
 228      weight   = (info = font['weight']  .to_s)?  info: '*'
 229      slant    = (info = font['slant']   .to_s)?  info: '*'
 230      swidth   = (info = font['swidth']  .to_s)?  info: '*'
 231      adstyle  = (info = font['adstyle'] .to_s)?  info: '*'
 232      pixels   = (info = font['pixels']  .to_s)?  info: '*'
 233      points   = (info = font['points']  .to_s)?  info: '*'
 234      resx     = (info = font['resx']    .to_s)?  info: '*'
 235      resy     = (info = font['resy']    .to_s)?  info: '*'
 236      space    = (info = font['space']   .to_s)?  info: '*'
 237      avgWidth = (info = font['avgWidth'].to_s)?  info: '*'
 238      charset  = (info = font['charset'] .to_s)?  info: '*'
 239      encoding = (info = font['encoding'].to_s)?  info: '*'
 240  
 241      [foundry, family, weight, slant, swidth, adstyle,
 242        pixels, points, resx, resy, space, avgWidth, charset, encoding]
 243    end
 244  
 245    def create_latinfont_tk4x(font)
 246      if font.kind_of? Hash
 247        @latinfont = '-' + _get_font_info_from_hash(font).join('-') + '-'
 248  
 249      elsif font.kind_of? Array
 250        finfo = {}
 251        finfo['family'] = font[0].to_s
 252        if font[1]
 253          fsize = font[1].to_s
 254          if fsize != '0' && fsize =~ /^(|\+|-)([0-9]+)$/
 255            if $1 == '-'
 256              finfo['pixels'] = $2
 257            else
 258              finfo['points'] = $2
 259            end
 260          else
 261            finfo['points'] = '13'
 262          end
 263        end
 264        font[2..-1].each{|style|
 265          case (style)
 266          when 'normal'
 267            finfo['weight'] = style
 268          when 'bold'
 269            finfo['weight'] = style
 270          when 'roman'
 271            finfo['slant'] = 'r'
 272          when 'italic'
 273            finfo['slant'] = 'i'
 274          end
 275        }
 276  
 277        @latinfont = '-' + _get_font_info_from_hash(finfo).join('-') + '-'
 278  
 279      elsif font.kind_of? TkFont
 280        @latinfont = font.latin_font
 281  
 282      else
 283        if font
 284          @latinfont = font
 285        else
 286          @latinfont = DEFAULT_LATIN_FONT_NAME
 287        end
 288  
 289      end
 290    end
 291  
 292    def create_kanjifont_tk4x(font)
 293      unless JAPANIZED_TK
 294        @kanjifont = ""
 295        return
 296      end
 297  
 298      if font.kind_of? Hash
 299        @kanjifont = '-' + _get_font_info_from_hash(font).join('-') + '-'
 300  
 301      elsif font.kind_of? Array
 302        finfo = {}
 303        finfo['family'] = font[0].to_s
 304        if font[1]
 305          fsize = font[1].to_s
 306          if fsize != '0' && fsize =~ /^(|\+|-)([0-9]+)$/
 307            if $1 == '-'
 308              finfo['pixels'] = $2
 309            else
 310              finfo['points'] = $2
 311            end
 312          else
 313            finfo['points'] = '13'
 314          end
 315        end
 316        font[2..-1].each{|style|
 317          case (style)
 318          when 'normal'
 319            finfo['weight'] = style
 320          when 'bold'
 321            finfo['weight'] = style
 322          when 'roman'
 323            finfo['slant'] = 'r'
 324          when 'italic'
 325            finfo['slant'] = 'i'
 326          end
 327        }
 328  
 329        @kanjifont = '-' + _get_font_info_from_hash(finfo).join('-') + '-'
 330      elsif font.kind_of? TkFont
 331        @kanjifont = font.kanji_font
 332      else
 333        if font
 334          @kanjifont = font
 335        else
 336          @kanjifont = DEFAULT_KANJI_FONT_NAME
 337        end
 338      end
 339    end
 340  
 341    def create_compoundfont_tk4x(ltn, knj, keys)
 342      create_latinfont(ltn)
 343      create_kanjifont(knj)
 344  
 345      if JAPANIZED_TK
 346        @compoundfont = [[@latinfont], [@kanjifont]]
 347        @fontslot = {'font'=>@latinfont, 'kanjifont'=>@kanjifont}
 348      else
 349        @compoundfont = @latinfont
 350        @fontslot = {'font'=>@latinfont}
 351      end
 352    end
 353  
 354    def create_latinfont_tk8x(font)
 355      @latinfont = @id + 'l'
 356  
 357      if JAPANIZED_TK
 358        if font.kind_of? Hash
 359          if font[:charset] || font['charset']
 360            tk_call('font', 'create', @latinfont, *hash_kv(font))
 361          else
 362            tk_call('font', 'create', @latinfont, 
 363                    '-charset', 'iso8859', *hash_kv(font))
 364          end
 365        elsif font.kind_of? Array
 366          tk_call('font', 'create', @latinfont, '-copy', array2tk_list(font))
 367          tk_call('font', 'configure', @latinfont, '-charset', 'iso8859')
 368        elsif font.kind_of? TkFont
 369          tk_call('font', 'create', @latinfont, '-copy', font.latin_font)
 370        elsif font
 371          tk_call('font', 'create', @latinfont, '-copy', font, 
 372                  '-charset', 'iso8859')
 373        else
 374          tk_call('font', 'create', @latinfont, '-charset', 'iso8859')
 375        end
 376      else
 377        if font.kind_of? Hash
 378          tk_call('font', 'create', @latinfont, *hash_kv(font))
 379        else
 380          keys = {}
 381          if font.kind_of? Array
 382            actual_core(array2tk_list(font)).each{|key,val| keys[key] = val}
 383          elsif font.kind_of? TkFont
 384            actual_core(font.latin_font).each{|key,val| keys[key] = val}
 385          elsif font
 386            actual_core(font).each{|key,val| keys[key] = val}
 387          end
 388          tk_call('font', 'create', @latinfont, *hash_kv(keys))
 389        end
 390  
 391        if font && @compoundfont
 392          keys = {}
 393          actual_core(@latinfont).each{|key,val| keys[key] = val}
 394          tk_call('font', 'configure', @compoundfont, *hash_kv(keys))
 395        end
 396      end
 397    end
 398  
 399    def create_kanjifont_tk8x(font)
 400      @kanjifont = @id + 'k'
 401  
 402      if JAPANIZED_TK
 403        if font.kind_of? Hash
 404          if font[:charset] || font['charset']
 405            tk_call('font', 'create', @kanjifont, *hash_kv(font))
 406          else
 407            tk_call('font', 'create', @kanjifont, 
 408                    '-charset', 'jisx0208.1983', *hash_kv(font))
 409          end
 410        elsif font.kind_of? Array
 411          tk_call('font', 'create', @kanjifont, '-copy', array2tk_list(font))
 412          tk_call('font', 'configure', @kanjifont, '-charset', 'jisx0208.1983')
 413        elsif font.kind_of? TkFont
 414          tk_call('font', 'create', @kanjifont, '-copy', font.kanji_font)
 415        elsif font
 416          tk_call('font', 'create', @kanjifont, '-copy', font, 
 417                  '-charset', 'jisx0208.1983')
 418        else
 419          tk_call('font', 'create', @kanjifont, '-charset', 'jisx0208.1983')
 420        end
 421        # end of JAPANIZED_TK
 422  
 423      else
 424        if font.kind_of? Hash
 425          tk_call('font', 'create', @kanjifont, *hash_kv(font))
 426        else
 427          keys = {}
 428          if font.kind_of? Array
 429            actual_core(array2tk_list(font)).each{|key,val| keys[key] = val}
 430          elsif font.kind_of? TkFont
 431            actual_core(font.kanji_font).each{|key,val| keys[key] = val}
 432          elsif font
 433            actual_core(font).each{|key,val| keys[key] = val}
 434          end
 435          tk_call('font', 'create', @kanjifont, *hash_kv(keys))
 436        end
 437  
 438        if font && @compoundfont
 439          keys = {}
 440          actual_core(@kanjifont).each{|key,val| keys[key] = val}
 441          tk_call('font', 'configure', @compoundfont, *hash_kv(keys))
 442        end
 443      end
 444    end
 445  
 446    def create_compoundfont_tk8x(ltn, knj, keys)
 447      create_latinfont(ltn)
 448      create_kanjifont(knj)
 449  
 450      @compoundfont = @id + 'c'
 451      if JAPANIZED_TK
 452        @fontslot = {'font'=>@compoundfont}
 453        tk_call('font', 'create', @compoundfont, 
 454                '-compound', [@latinfont, @kanjifont], *hash_kv(keys))
 455      else
 456        tk_call('font', 'create', @compoundfont)
 457  
 458        latinkeys = {}
 459        begin
 460          actual_core(@latinfont).each{|key,val| latinkeys[key] = val}
 461        rescue
 462          latinkeys {}
 463        end
 464        if latinkeys != {}
 465          tk_call('font', 'configure', @compoundfont, *hash_kv(latinkeys))
 466        end
 467  
 468        if knj
 469          kanjikeys = {}
 470          begin
 471            actual_core(@kanjifont).each{|key,val| kanjikeys[key] = val}
 472          rescue
 473            kanjikeys {}
 474          end
 475          if kanjikeys != {}
 476            tk_call('font', 'configure', @compoundfont, *hash_kv(kanjikeys))
 477          end
 478        end
 479  
 480        @fontslot = {'font'=>@compoundfont}
 481        tk_call('font', 'configure', @compoundfont, *hash_kv(keys))
 482      end
 483    end
 484  
 485    def actual_core_tk4x(font, window=nil, option=nil)
 486      # dummy
 487      if option
 488        ""
 489      else
 490        [['family',[]], ['size',[]], ['weight',[]], ['slant',[]], 
 491          ['underline',[]], ['overstrike',[]], ['charset',[]], 
 492          ['pointadjust',[]]]
 493      end
 494    end
 495  
 496    def actual_core_tk8x(font, window=nil, option=nil)
 497      if option == 'compound'
 498        ""
 499      elsif option
 500        if window
 501          tk_call('font', 'actual', font, "-displayof", window, "-#{option}")
 502        else
 503          tk_call('font', 'actual', font, "-#{option}")
 504        end
 505      else
 506        l = tk_split_simplelist(if window
 507                                   tk_call('font', 'actual', font, 
 508                                                       "-displayof", window)
 509                                else
 510                                   tk_call('font', 'actual', font)
 511                                end)
 512        r = []
 513        while key=l.shift
 514          if key == '-compound'
 515            l.shift
 516          else
 517            r.push [key[1..-1], l.shift]
 518          end
 519        end
 520        r
 521      end
 522    end
 523  
 524    def configure_core_tk4x(font, slot, value=None)
 525      ""
 526    end
 527  
 528    def configinfo_core_tk4x(font, option=nil)
 529      # dummy
 530      if option
 531        ""
 532      else
 533        [['family',[]], ['size',[]], ['weight',[]], ['slant',[]], 
 534          ['underline',[]], ['overstrike',[]], ['charset',[]], 
 535          ['pointadjust',[]]]
 536      end
 537    end
 538  
 539    def configure_core_tk8x(font, slot, value=None)
 540      if slot.kind_of? Hash
 541        tk_call 'font', 'configure', font, *hash_kv(slot)
 542      else
 543        tk_call 'font', 'configure', font, "-#{slot}", value
 544      end
 545    end
 546  
 547    def configinfo_core_tk8x(font, option=nil)
 548      if option == 'compound'
 549        ""
 550      elsif option
 551        tk_call('font', 'configure', font, "-#{option}")
 552      else
 553        l = tk_split_simplelist(tk_call('font', 'configure', font))
 554        r = []
 555        while key=l.shift
 556          if key == '-compound'
 557            l.shift
 558          else
 559            r.push [key[1..-1], l.shift]
 560          end
 561        end
 562        r
 563      end
 564    end
 565  
 566    def delete_core_tk4x
 567      Tk_FontNameTBL[@id] = nil
 568      Tk_FontUseTBL.delete_if{|key,value| value == self}
 569    end
 570  
 571    def delete_core_tk8x
 572      begin
 573        tk_call('font', 'delete', @latinfont)
 574      rescue
 575      end
 576      begin
 577        tk_call('font', 'delete', @kanjifont)
 578      rescue
 579      end
 580      begin
 581        tk_call('font', 'delete', @compoundfont)
 582      rescue
 583      end
 584      Tk_FontNameTBL[@id] = nil
 585      Tk_FontUseTBL.delete_if{|key,value| value == self}
 586    end
 587  
 588    def latin_replace_core_tk4x(ltn)
 589      create_latinfont_tk4x(ltn)
 590      @compoundfont[0] = [@latinfont] if JAPANIZED_TK
 591      @fontslot['font'] = @latinfont
 592      Tk_FontUseTBL.dup.each{|w, fobj|
 593        if self == fobj
 594          begin
 595            if w.include?(';')
 596              win, tag = w.split(';')
 597              winobj = tk_tcl2ruby(win)
 598  #           winobj.tagfont_configure(tag, {'font'=>@latinfont})
 599              if winobj.kind_of? TkText
 600                tk_call(win, 'tag', 'configure', tag, '-font', @latinfont)
 601              elsif winobj.kind_of? TkCanvas
 602                tk_call(win, 'itemconfigure', tag, '-font', @latinfont)
 603              elsif winobj.kind_of? TkMenu
 604                tk_call(win, 'entryconfigure', tag, '-font', @latinfont)
 605              else
 606                raise RuntimeError, "unknown widget type"
 607              end
 608            else
 609  #           tk_tcl2ruby(w).font_configure('font'=>@latinfont)
 610              tk_call(w, 'configure', '-font', @latinfont)
 611            end
 612          rescue
 613            Tk_FontUseTBL[w] = nil
 614          end
 615        end
 616      }
 617      self
 618    end
 619  
 620    def kanji_replace_core_tk4x(knj)
 621      return self unless JAPANIZED_TK
 622  
 623      create_kanjifont_tk4x(knj)
 624      @compoundfont[1] = [@kanjifont]
 625      @fontslot['kanjifont'] = @kanjifont
 626      Tk_FontUseTBL.dup.each{|w, fobj|
 627        if self == fobj
 628          begin
 629            if w.include?(';')
 630              win, tag = w.split(';')
 631              winobj = tk_tcl2ruby(win)
 632  #           winobj.tagfont_configure(tag, {'kanjifont'=>@kanjifont})
 633              if winobj.kind_of? TkText
 634                tk_call(win, 'tag', 'configure', tag, '-kanjifont', @kanjifont)
 635              elsif winobj.kind_of? TkCanvas
 636                tk_call(win, 'itemconfigure', tag, '-kanjifont', @kanjifont)
 637              elsif winobj.kind_of? TkMenu
 638                tk_call(win, 'entryconfigure', tag, '-kanjifont', @latinfont)
 639              else
 640                raise RuntimeError, "unknown widget type"
 641              end
 642            else
 643  #           tk_tcl2ruby(w).font_configure('kanjifont'=>@kanjifont)
 644              tk_call(w, 'configure', '-kanjifont', @kanjifont)
 645            end
 646          rescue
 647            Tk_FontUseTBL[w] = nil
 648          end
 649        end
 650      }
 651      self
 652    end
 653  
 654    def latin_replace_core_tk8x(ltn)
 655      begin
 656        tk_call('font', 'delete', @latinfont)
 657      rescue
 658      end
 659      create_latinfont(ltn)
 660      self
 661    end
 662  
 663    def kanji_replace_core_tk8x(knj)
 664      begin
 665        tk_call('font', 'delete', @kanjifont)
 666      rescue
 667      end
 668      create_kanjifont(knj)
 669      self
 670    end
 671  
 672    def measure_core_tk4x(window, text)
 673      0
 674    end
 675  
 676    def measure_core_tk8x(window, text)
 677      if window
 678        number(tk_call('font', 'measure', @compoundfont, 
 679                       '-displayof', window, text))
 680      else
 681        number(tk_call('font', 'measure', @compoundfont, text))
 682      end
 683    end
 684  
 685    def metrics_core_tk4x(font, window, option=nil)
 686      # dummy
 687      if option
 688        ""
 689      else
 690        [['ascent',[]], ['descent',[]], ['linespace',[]], ['fixed',[]]]
 691      end
 692    end
 693  
 694    def metrics_core_tk8x(font, window, option=nil)
 695      if option
 696        if window
 697          number(tk_call('font', 'metrics', font, 
 698                         "-displayof", window, "-#{option}"))
 699        else
 700          number(tk_call('font', 'metrics', font, "-#{option}"))
 701        end
 702      else
 703        l = tk_split_list(if window
 704                            tk_call('font','metrics',font,"-displayof",window)
 705                          else
 706                            tk_call('font','metrics',font)
 707                          end)
 708        r = []
 709        while key=l.shift
 710          r.push [key[1..-1], l.shift.to_i]
 711        end
 712        r
 713      end
 714    end
 715  
 716    ###################################
 717    # private alias
 718    ###################################
 719    case (Tk::TK_VERSION)
 720    when /^4\.*/
 721      alias create_latinfont    create_latinfont_tk4x
 722      alias create_kanjifont    create_kanjifont_tk4x
 723      alias create_compoundfont create_compoundfont_tk4x
 724      alias actual_core         actual_core_tk4x
 725      alias configure_core      configure_core_tk4x
 726      alias configinfo_core     configinfo_core_tk4x
 727      alias delete_core         delete_core_tk4x
 728      alias latin_replace_core  latin_replace_core_tk4x
 729      alias kanji_replace_core  kanji_replace_core_tk4x
 730      alias measure_core        measure_core_tk4x
 731      alias metrics_core        metrics_core_tk4x
 732  
 733    when /^8\.[0123]/
 734      alias create_latinfont    create_latinfont_tk8x
 735      alias create_kanjifont    create_kanjifont_tk8x
 736      alias create_compoundfont create_compoundfont_tk8x
 737      alias actual_core         actual_core_tk8x
 738      alias configure_core      configure_core_tk8x
 739      alias configinfo_core     configinfo_core_tk8x
 740      alias delete_core         delete_core_tk8x
 741      alias latin_replace_core  latin_replace_core_tk8x
 742      alias kanji_replace_core  kanji_replace_core_tk8x
 743      alias measure_core        measure_core_tk8x
 744      alias metrics_core        metrics_core_tk8x
 745  
 746    when /^8\.*/
 747      alias create_latinfont    create_latinfont_tk8x
 748      alias create_kanjifont    create_kanjifont_tk8x
 749      alias create_compoundfont create_compoundfont_tk8x
 750      alias actual_core         actual_core_tk8x
 751      alias configure_core      configure_core_tk8x
 752      alias configinfo_core     configinfo_core_tk8x
 753      alias delete_core         delete_core_tk8x
 754      alias latin_replace_core  latin_replace_core_tk8x
 755      alias kanji_replace_core  kanji_replace_core_tk8x
 756      alias measure_core        measure_core_tk8x
 757      alias metrics_core        metrics_core_tk8x
 758  
 759    end
 760  
 761    ###################################
 762    public
 763    ###################################
 764    def method_missing(id, *args)
 765      name = id.id2name
 766      case args.length
 767      when 1
 768        configure name, args[0]
 769      when 0
 770        begin
 771          configinfo name
 772        rescue
 773          fail NameError, "undefined local variable or method `#{name}' for #{self.to_s}", error_at
 774        end
 775      else
 776        fail NameError, "undefined method `#{name}' for #{self.to_s}", error_at
 777      end
 778    end
 779  
 780    def call_font_configure(path, *args)
 781      args += hash_kv(args.pop.update(@fontslot))
 782      tk_call(*args)
 783      Tk_FontUseTBL[path] = self
 784      self
 785    end
 786  
 787    def used
 788      ret = []
 789      Tk_FontUseTBL.each{|key,value|
 790        if key.include?(';')
 791          win, tag = key.split(';')
 792          winobj = tk_tcl2ruby(win)
 793          if winobj.kind_of? TkText
 794            ret.push([winobj, winobj.tagid2obj(tag)])
 795          elsif winobj.kind_of? TkCanvas
 796            if (tagobj = TkcTag.id2obj(winobj, tag)).kind_of? TkcTag
 797              ret.push([winobj, tagobj])
 798            elsif (tagobj = TkcItem.id2obj(tag)).kind_of? TkcItem
 799              ret.push([winobj, tagobj])
 800            else
 801              ret.push([winobj, tag])
 802            end
 803          elsif winobj.kind_of? TkMenu
 804            ret.push([winobj, tag])
 805          else
 806            ret.push([win, tag])
 807          end
 808        else
 809          ret.push(tk_tcl2ruby(key)) if value == self
 810        end
 811      }
 812      ret
 813    end
 814  
 815    def id
 816      @id
 817    end
 818  
 819    def to_eval
 820      font
 821    end
 822  
 823    def font
 824      @compoundfont
 825    end
 826  
 827    def latin_font
 828      @latinfont
 829    end
 830  
 831    def kanji_font
 832      @kanjifont
 833    end
 834  
 835    def actual(option=nil)
 836      actual_core(@compoundfont, nil, option)
 837    end
 838  
 839    def actual_displayof(window, option=nil)
 840      window = '.' unless window
 841      actual_core(@compoundfont, window, option)
 842    end
 843  
 844    def latin_actual(option=nil)
 845      actual_core(@latinfont, nil, option)
 846    end
 847  
 848    def latin_actual_displayof(window, option=nil)
 849      window = '.' unless window
 850      actual_core(@latinfont, window, option)
 851    end
 852  
 853    def kanji_actual(option=nil)
 854      #if JAPANIZED_TK
 855      if @kanjifont != ""
 856        actual_core(@kanjifont, nil, option)
 857      else
 858        actual_core_tk4x(nil, nil, option)
 859      end
 860    end
 861  
 862    def kanji_actual_displayof(window, option=nil)
 863      #if JAPANIZED_TK
 864      if @kanjifont != ""
 865        window = '.' unless window
 866        actual_core(@kanjifont, window, option)
 867      else
 868        actual_core_tk4x(nil, window, option)
 869      end
 870    end
 871  
 872    def [](slot)
 873      configinfo slot
 874    end
 875  
 876    def []=(slot, val)
 877      configure slot, val
 878    end
 879  
 880    def configure(slot, value=None)
 881      configure_core(@compoundfont, slot, value)
 882    end
 883  
 884    def configinfo(slot=nil)
 885      configinfo_core(@compoundfont, slot)
 886    end
 887  
 888    def delete
 889      delete_core
 890    end
 891  
 892    def latin_configure(slot, value=None)
 893      if JAPANIZED_TK
 894        configure_core(@latinfont, slot, value)
 895      else
 896        configure(slot, value)
 897      end
 898    end
 899  
 900    def latin_configinfo(slot=nil)
 901      if JAPANIZED_TK
 902        configinfo_core(@latinfont, slot)
 903      else
 904        configinfo(slot)
 905      end
 906    end
 907  
 908    def kanji_configure(slot, value=None)
 909      #if JAPANIZED_TK
 910      if @kanjifont != ""
 911        configure_core(@kanjifont, slot, value)
 912        configure('size'=>configinfo('size')) # to reflect new configuration
 913      else
 914        #""
 915        configure(slot, value)
 916      end
 917    end
 918  
 919    def kanji_configinfo(slot=nil)
 920      #if JAPANIZED_TK
 921      if @kanjifont != ""
 922        configinfo_core(@kanjifont, slot)
 923      else
 924        #[]
 925        configinfo(slot)
 926      end
 927    end
 928  
 929    def replace(ltn, knj)
 930      latin_replace(ltn)
 931      kanji_replace(knj)
 932      self
 933    end
 934  
 935    def latin_replace(ltn)
 936      latin_replace_core(ltn)
 937      reset_pointadjust
 938    end
 939  
 940    def kanji_replace(knj)
 941      kanji_replace_core(knj)
 942      reset_pointadjust
 943    end
 944  
 945    def measure(text)
 946      measure_core(nil, text)
 947    end
 948  
 949    def measure_displayof(window, text)
 950      window = '.' unless window
 951      measure_core(window, text)
 952    end
 953  
 954    def metrics(option=nil)
 955      metrics_core(@compoundfont, nil, option)
 956    end
 957  
 958    def metrics_displayof(window, option=nil)
 959      window = '.' unless window
 960      metrics_core(@compoundfont, window, option)
 961    end
 962  
 963    def latin_metrics(option=nil)
 964      metrics_core(@latinfont, nil, option)
 965    end
 966  
 967    def latin_metrics_displayof(window, option=nil)
 968      window = '.' unless window
 969      metrics_core(@latinfont, window, option)
 970    end
 971  
 972    def kanji_metrics(option=nil)
 973      if JAPANIZED_TK
 974        metrics_core(@kanjifont, nil, option)
 975      else
 976        metrics_core_tk4x(nil, nil, option)
 977      end
 978    end
 979  
 980    def kanji_metrics_displayof(window, option=nil)
 981      if JAPANIZED_TK
 982        window = '.' unless window
 983        metrics_core(@kanjifont, window, option)
 984      else
 985        metrics_core_tk4x(nil, window, option)
 986      end
 987    end
 988  
 989    def reset_pointadjust
 990      begin
 991        if /^8\.*/ === Tk::TK_VERSION  && JAPANIZED_TK
 992          configure('pointadjust' => latin_actual.assoc('size')[1].to_f / 
 993                                        kanji_actual.assoc('size')[1].to_f )
 994        end
 995      rescue
 996      end
 997      self
 998    end
 999  
1000    ###################################
1001    # public alias
1002    ###################################
1003    alias ascii_font             latin_font
1004    alias create_asciifont       create_latinfont
1005    alias ascii_actual           latin_actual
1006    alias ascii_actual_displayof latin_actual_displayof
1007    alias ascii_configure        latin_configure
1008    alias ascii_configinfo       latin_configinfo
1009    alias ascii_replace          latin_replace
1010    alias ascii_metrics          latin_metrics
1011  
1012  end
1013  
1014  module TkTreatTagFont
1015    def font_configinfo
1016      @parent.tagfont_configinfo(@id)
1017    end
1018  #  alias font font_configinfo
1019  
1020    def font_configure(slot)
1021      @parent.tagfont_configure(@id, slot)
1022    end
1023  
1024    def latinfont_configure(ltn, keys=nil)
1025      @parent.latintagfont_configure(@id, ltn, keys)
1026    end
1027    alias asciifont_configure latinfont_configure
1028  
1029    def kanjifont_configure(knj, keys=nil)
1030      @parent.kanjitagfont_configure(@id, ltn, keys)
1031    end
1032  
1033    def font_copy(window, wintag=nil)
1034      @parent.tagfont_copy(@id, window, wintag)
1035    end
1036  
1037    def latinfont_copy(window, wintag=nil)
1038      @parent.latintagfont_copy(@id, window, wintag)
1039    end
1040    alias asciifont_copy latinfont_copy
1041  
1042    def kanjifont_copy(window, wintag=nil)
1043      @parent.kanjitagfont_copy(@id, window, wintag)
1044    end
1045  end