⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tkfont.rb

📁 ruby的源代码
💻 RB
📖 第 1 页 / 共 2 页
字号:
##  tkfont.rb - the class to treat fonts on Ruby/Tk##                               by  Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)#require 'tk'class TkFont  include Tk  extend TkCore  Tk_FontID = [0]  Tk_FontNameTBL = {}  Tk_FontUseTBL = {}  # set default font  case Tk::TK_VERSION  when /^4\.*/    DEFAULT_LATIN_FONT_NAME = 'a14'.freeze    DEFAULT_KANJI_FONT_NAME = 'k14'.freeze  when /^8\.*/    if JAPANIZED_TK      begin        fontnames = tk_call('font', 'names')	case fontnames	when /defaultgui/          # Tcl/Tk-JP for Windows          ltn = 'defaultgui'          knj = 'defaultgui'	when /Mincho:Helvetica-12/          # Tcl/Tk-JP for UNIX/X          ltn, knj = tk_split_simplelist(tk_call('font', 'configure',                                                  'Mincho:Helvetica-12',                                                  '-compound'))        else          # unknown Tcl/Tk-JP	  platform = tk_call('set', 'tcl_platform(platform)')	  case platform	  when 'unix'	    ltn = {'family'=>'Helvetica'.freeze, 'size'=>-12}	    knj = 'k14'	    #knj = '-misc-fixed-medium-r-normal--14-*-*-*-c-*-jisx0208.1983-0'	  when 'windows'	    ltn = {'family'=>'MS Sans Serif'.freeze, 'size'=>8}	    knj = 'mincho'	  when 'macintosh'	    ltn = 'system'	    knj = 'mincho'	  else # unknown	    ltn = 'Helvetica'	    knj = 'mincho'	  end        end      rescue        ltn = 'Helvetica'        knj = 'mincho'      end    else # not JAPANIZED_TK      begin	platform = tk_call('set', 'tcl_platform(platform)')	case platform	when 'unix'	  ltn = {'family'=>'Helvetica'.freeze, 'size'=>-12}	  knj = 'k14'	  #knj = '-misc-fixed-medium-r-normal--14-*-*-*-c-*-jisx0208.1983-0'	when 'windows'	  ltn = {'family'=>'MS Sans Serif'.freeze, 'size'=>8}	  knj = 'mincho'	when 'macintosh'	  ltn = 'system'	  knj = 'mincho'	else # unknown	  ltn = 'Helvetica'	  knj = 'mincho'	end      rescue	ltn = 'Helvetica'	knj = 'mincho'      end    end    DEFAULT_LATIN_FONT_NAME = ltn.freeze    DEFAULT_KANJI_FONT_NAME = knj.freeze  else # unknown version    DEFAULT_LATIN_FONT_NAME = 'Helvetica'.freeze    DEFAULT_KANJI_FONT_NAME = 'mincho'.freeze  end  if $DEBUG    print "default latin font = "; p DEFAULT_LATIN_FONT_NAME    print "default kanji font = "; p DEFAULT_KANJI_FONT_NAME  end  ###################################  # class methods  ###################################  def TkFont.families(window=nil)    case (Tk::TK_VERSION)    when /^4\.*/      ['fixed']    when /^8\.*/      if window	tk_split_simplelist(tk_call('font', 'families', '-displayof', window))      else	tk_split_simplelist(tk_call('font', 'families'))      end    end  end  def TkFont.names    case (Tk::TK_VERSION)    when /^4\.*/      r = ['fixed']      r += ['a14', 'k14'] if JAPANIZED_TK      Tk_FontNameTBL.each_value{|obj| r.push(obj)}      r | []    when /^8\.*/      tk_split_simplelist(tk_call('font', 'names'))    end  end  def TkFont.create_copy(font)    fail 'source-font need to be TkFont' unless font.kind_of? TkFont    keys = {}    font.configinfo.each{|key,value| keys[key] = value }    TkFont.new(font.latin_font, font.kanji_font, keys)  end  def TkFont.get_obj(name)    if name =~ /^(@font[0-9]+)(|c|l|k)$/      Tk_FontNameTBL[$1]    else      nil    end  end  def TkFont.init_widget_font(path, *args)    case (Tk::TK_VERSION)    when /^4\.*/      conf = tk_split_simplelist(tk_call(*args)).	find_all{|prop| prop[0..5]=='-font ' || prop[0..10]=='-kanjifont '}.	collect{|prop| tk_split_simplelist(prop)}      if font_inf = conf.assoc('-font')	ltn = font_inf[4]	ltn = nil if ltn == []      else 	#ltn = nil	raise RuntimeError, "unknown option '-font'"      end      if font_inf = conf.assoc('-kanjifont')	knj = font_inf[4]	knj = nil if knj == []      else	knj = nil      end      TkFont.new(ltn, knj).call_font_configure(path, *(args + [{}]))    when /^8\.*/      font_prop = tk_split_simplelist(tk_call(*args)).find{|prop| 	prop[0..5] == '-font '      }      unless font_prop	raise RuntimeError, "unknown option '-font'"      end      fnt = tk_split_simplelist(font_prop)[4]      if fnt == ""	TkFont.new(nil, nil).call_font_configure(path, *(args + [{}]))      else	begin	  compound = Hash[*tk_split_simplelist(tk_call('font', 'configure', 					       fnt))].collect{|key,value|	    [key[1..-1], value]	  }.assoc('compound')[1]	rescue	  compound = []	end	if compound == []	  TkFont.new(fnt, DEFAULT_KANJI_FONT_NAME) \	  .call_font_configure(path, *(args + [{}]))	else	  TkFont.new(compound[0], compound[1]) \	  .call_font_configure(path, *(args + [{}]))	end      end    end  end  def TkFont.used_on(path=nil)    if path      Tk_FontUseTBL[path]    else      Tk_FontUseTBL.values | []    end  end  def TkFont.failsafe(font)    begin      if /^8\.*/ === Tk::TK_VERSION  && JAPANIZED_TK        tk_call('font', 'failsafe', font)      end    rescue    end  end  ###################################  private  ###################################  def initialize(ltn=DEFAULT_LATIN_FONT_NAME, knj=nil, keys=nil)    @id = format("@font%.4d", Tk_FontID[0])    Tk_FontID[0] += 1    Tk_FontNameTBL[@id] = self    knj = DEFAULT_KANJI_FONT_NAME if JAPANIZED_TK && !knj    create_compoundfont(ltn, knj, keys)  end  def _get_font_info_from_hash(font)    foundry  = (info = font['foundry'] .to_s)?  info: '*'    family   = (info = font['family']  .to_s)?  info: '*'    weight   = (info = font['weight']  .to_s)?  info: '*'    slant    = (info = font['slant']   .to_s)?  info: '*'    swidth   = (info = font['swidth']  .to_s)?  info: '*'    adstyle  = (info = font['adstyle'] .to_s)?  info: '*'    pixels   = (info = font['pixels']  .to_s)?  info: '*'    points   = (info = font['points']  .to_s)?  info: '*'    resx     = (info = font['resx']    .to_s)?  info: '*'    resy     = (info = font['resy']    .to_s)?  info: '*'    space    = (info = font['space']   .to_s)?  info: '*'    avgWidth = (info = font['avgWidth'].to_s)?  info: '*'    charset  = (info = font['charset'] .to_s)?  info: '*'    encoding = (info = font['encoding'].to_s)?  info: '*'    [foundry, family, weight, slant, swidth, adstyle,      pixels, points, resx, resy, space, avgWidth, charset, encoding]  end  def create_latinfont_tk4x(font)    if font.kind_of? Hash      @latinfont = '-' + _get_font_info_from_hash(font).join('-') + '-'    elsif font.kind_of? Array      finfo = {}      finfo['family'] = font[0].to_s      if font[1]	fsize = font[1].to_s	if fsize != '0' && fsize =~ /^(|\+|-)([0-9]+)$/	  if $1 == '-'	    finfo['pixels'] = $2	  else	    finfo['points'] = $2	  end	else	  finfo['points'] = '13'	end      end      font[2..-1].each{|style|	case (style)	when 'normal'	  finfo['weight'] = style	when 'bold'	  finfo['weight'] = style	when 'roman'	  finfo['slant'] = 'r'	when 'italic'	  finfo['slant'] = 'i'	end      }      @latinfont = '-' + _get_font_info_from_hash(finfo).join('-') + '-'    elsif font.kind_of? TkFont      @latinfont = font.latin_font    else      if font        @latinfont = font      else        @latinfont = DEFAULT_LATIN_FONT_NAME      end    end  end  def create_kanjifont_tk4x(font)    unless JAPANIZED_TK      @kanjifont = ""      return    end    if font.kind_of? Hash      @kanjifont = '-' + _get_font_info_from_hash(font).join('-') + '-'    elsif font.kind_of? Array      finfo = {}      finfo['family'] = font[0].to_s      if font[1]	fsize = font[1].to_s	if fsize != '0' && fsize =~ /^(|\+|-)([0-9]+)$/	  if $1 == '-'	    finfo['pixels'] = $2	  else	    finfo['points'] = $2	  end	else	  finfo['points'] = '13'	end      end      font[2..-1].each{|style|	case (style)	when 'normal'	  finfo['weight'] = style	when 'bold'	  finfo['weight'] = style	when 'roman'	  finfo['slant'] = 'r'	when 'italic'	  finfo['slant'] = 'i'	end      }      @kanjifont = '-' + _get_font_info_from_hash(finfo).join('-') + '-'    elsif font.kind_of? TkFont      @kanjifont = font.kanji_font    else      if font        @kanjifont = font      else        @kanjifont = DEFAULT_KANJI_FONT_NAME      end    end  end  def create_compoundfont_tk4x(ltn, knj, keys)    create_latinfont(ltn)    create_kanjifont(knj)    if JAPANIZED_TK      @compoundfont = [[@latinfont], [@kanjifont]]      @fontslot = {'font'=>@latinfont, 'kanjifont'=>@kanjifont}    else      @compoundfont = @latinfont      @fontslot = {'font'=>@latinfont}    end  end  def create_latinfont_tk8x(font)    @latinfont = @id + 'l'    if JAPANIZED_TK      if font.kind_of? Hash	if font['charset']	  tk_call('font', 'create', @latinfont, *hash_kv(font))	else	  tk_call('font', 'create', @latinfont,                   '-charset', 'iso8859', *hash_kv(font))	end      elsif font.kind_of? Array	tk_call('font', 'create', @latinfont, '-copy', array2tk_list(font))        tk_call('font', 'configure', @latinfont, '-charset', 'iso8859')      elsif font.kind_of? TkFont	tk_call('font', 'create', @latinfont, '-copy', font.latin_font)      elsif font	tk_call('font', 'create', @latinfont, '-copy', font,                 '-charset', 'iso8859')      else	tk_call('font', 'create', @latinfont, '-charset', 'iso8859')      end    else      if font.kind_of? Hash	tk_call('font', 'create', @latinfont, *hash_kv(font))      else	keys = {}	if font.kind_of? Array	  actual_core(array2tk_list(font)).each{|key,val| keys[key] = val}	elsif font.kind_of? TkFont	  actual_core(font.latin_font).each{|key,val| keys[key] = val}	elsif font	  actual_core(font).each{|key,val| keys[key] = val}	end	tk_call('font', 'create', @latinfont, *hash_kv(keys))      end      if font && @compoundfont        keys = {}        actual_core(@latinfont).each{|key,val| keys[key] = val}	tk_call('font', 'configure', @compoundfont, *hash_kv(keys))      end    end  end  def create_kanjifont_tk8x(font)    @kanjifont = @id + 'k'    if JAPANIZED_TK      if font.kind_of? Hash        if font['charset']	  tk_call('font', 'create', @kanjifont, *hash_kv(font))        else	  tk_call('font', 'create', @kanjifont, 		  '-charset', 'jisx0208.1983', *hash_kv(font))        end      elsif font.kind_of? Array        tk_call('font', 'create', @kanjifont, '-copy', array2tk_list(font))        tk_call('font', 'configure', @kanjifont, '-charset', 'jisx0208.1983')      elsif font.kind_of? TkFont        tk_call('font', 'create', @kanjifont, '-copy', font.kanji_font)      elsif font        tk_call('font', 'create', @kanjifont, '-copy', font, 	        '-charset', 'jisx0208.1983')      else        tk_call('font', 'create', @kanjifont, '-charset', 'jisx0208.1983')      end      # end of JAPANIZED_TK    else      if font.kind_of? Hash        tk_call('font', 'create', @kanjifont, *hash_kv(font))      else        keys = {}        if font.kind_of? Array	  actual_core(array2tk_list(font)).each{|key,val| keys[key] = val}        elsif font.kind_of? TkFont	  actual_core(font.kanji_font).each{|key,val| keys[key] = val}        elsif font	  actual_core(font).each{|key,val| keys[key] = val}        end        tk_call('font', 'create', @kanjifont, *hash_kv(keys))      end      if font && @compoundfont        keys = {}        actual_core(@kanjifont).each{|key,val| keys[key] = val}        tk_call('font', 'configure', @compoundfont, *hash_kv(keys))      end    end  end  def create_compoundfont_tk8x(ltn, knj, keys)    create_latinfont(ltn)    create_kanjifont(knj)    @compoundfont = @id + 'c'    if JAPANIZED_TK      @fontslot = {'font'=>@compoundfont}      tk_call('font', 'create', @compoundfont, 	      '-compound', [@latinfont, @kanjifont], *hash_kv(keys))    else      tk_call('font', 'create', @compoundfont)      latinkeys = {}      begin	actual_core(@latinfont).each{|key,val| latinkeys[key] = val}      rescue	latinkeys {}      end      if latinkeys != {}	tk_call('font', 'configure', @compoundfont, *hash_kv(latinkeys))      end      if knj	kanjikeys = {}	begin	  actual_core(@kanjifont).each{|key,val| kanjikeys[key] = val}	rescue	  kanjikeys {}	end	if kanjikeys != {}	  tk_call('font', 'configure', @compoundfont, *hash_kv(kanjikeys))	end      end      @fontslot = {'font'=>@compoundfont}      tk_call('font', 'configure', @compoundfont, *hash_kv(keys))    end  end  def actual_core_tk4x(font, window=nil, option=nil)    # dummy    if option      ""    else      [['family',[]], ['size',[]], ['weight',[]], ['slant',[]], 	['underline',[]], ['overstrike',[]], ['charset',[]], 	['pointadjust',[]]]    end  end  def actual_core_tk8x(font, window=nil, option=nil)    if option == 'compound'      ""    elsif option      if window	tk_call('font', 'actual', font, "-displayof", window, "-#{option}")      else	tk_call('font', 'actual', font, "-#{option}")      end    else      l = tk_split_simplelist(if window			 	 tk_call('font', 'actual', font, 					             "-displayof", window)			      else			  	 tk_call('font', 'actual', font)			      end)      r = []      while key=l.shift	if key == '-compound'	  l.shift	else	  r.push [key[1..-1], l.shift]	end      end      r    end  end  def configure_core_tk4x(font, slot, value=None)    ""

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -