summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tk/itemfont.rb
blob: ab9e3ff6c9cbb4b6f67115a38c15e8d85b17218c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#
# tk/itemfont.rb : control font of widget items
#
require 'tk'

module TkItemFontOptkeys
  def __item_font_optkeys(id)
    # maybe need to override
    ['font']
  end
  private :__item_font_optkeys
end

module TkTreatItemFont
  include TkItemFontOptkeys

  def __item_pathname(id)
    # maybe need to override
    [self.path, id].join(';')
  end
  private :__item_pathname

  ################################################

  def tagfont_configinfo(tagOrId, key = nil)
    optkeys = __item_font_optkeys(tagid(tagOrId))
    if key && !optkeys.find{|opt| opt.to_s == key.to_s}
      fail ArgumentError, "unknown font option name `#{key}'"
    end

    win, tag = __item_pathname(tagid(tagOrId)).split(';')

    if key
      pathname = [win, tag, key].join(';')
      TkFont.used_on(pathname) || 
        TkFont.init_widget_font(pathname, 
                                *(__item_confinfo_cmd(tagid(tagOrId))))
    elsif optkeys.size == 1
      pathname = [win, tag, optkeys[0]].join(';')
      TkFont.used_on(pathname) || 
        TkFont.init_widget_font(pathname, 
                                *(__item_confinfo_cmd(tagid(tagOrId))))
    else
      fonts = {}
      optkeys.each{|key|
        key = key.to_s
        pathname = [win, tag, key].join(';')
        fonts[key] = 
          TkFont.used_on(pathname) || 
          TkFont.init_widget_font(pathname, 
                                  *(__item_confinfo_cmd(tagid(tagOrId))))
      }
      fonts
    end
  end
  alias tagfontobj tagfont_configinfo

  def tagfont_configure(tagOrId, slot)
    pathname = __item_pathname(tagid(tagOrId))

    slot = _symbolkey2str(slot)

    __item_font_optkeys(tagid(tagOrId)).each{|optkey|
      optkey = optkey.to_s
      l_optkey = 'latin' << optkey
      a_optkey = 'ascii' << optkey
      k_optkey = 'kanji' << optkey

      if slot.key?(optkey)
        fnt = slot.delete(optkey)
        if fnt.kind_of?(TkFont)
          slot.delete(l_optkey)
          slot.delete(a_optkey)
          slot.delete(k_optkey)

          fnt.call_font_configure([pathname, optkey], 
                                  *(__item_config_cmd(tagid(tagOrId)) << {}))
          next
        else
          if fnt
            if (slot.key?(l_optkey) || 
                slot.key?(a_optkey) || 
                slot.key?(k_optkey))
              fnt = TkFont.new(fnt)

              lfnt = slot.delete(l_optkey)
              lfnt = slot.delete(a_optkey) if slot.key?(a_optkey)
              kfnt = slot.delete(k_optkey)

              fnt.latin_replace(lfnt) if lfnt
              fnt.kanji_replace(kfnt) if kfnt

              fnt.call_font_configure([pathname, optkey], 
                                      *(__item_config_cmd(tagid(tagOrId)) << {}))
              next
            else
              tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << fnt))
            end
          end
          next
        end
      end

      lfnt = slot.delete(l_optkey)
      lfnt = slot.delete(a_optkey) if slot.key?(a_optkey)
      kfnt = slot.delete(k_optkey)

      if lfnt && kfnt
        TkFont.new(lfnt, kfnt).call_font_configure([pathname, optkey], 
                                                   *(__item_config_cmd(tagid(tagOrId)) << {}))
      elsif lfnt
        latintagfont_configure([lfnt, optkey])
      elsif kfnt
        kanjitagfont_configure([kfnt, optkey])
      end
    }

    # configure other (without font) options
    tk_call(*(__item_config_cmd(tagid(tagOrId)).concat(hash_kv(slot)))) if slot != {}
    self
  end

  def latintagfont_configure(tagOrId, ltn, keys=nil)
    if ltn.kind_of?(Array)
      key = ltn[1]
      ltn = ltn[0]
    else
      key = nil
    end

    optkeys = __item_font_optkeys(tagid(tagOrId))
    if key && !optkeys.find{|opt| opt.to_s == key.to_s}
      fail ArgumentError, "unknown font option name `#{key}'"
    end

    win, tag = __item_pathname(tagid(tagOrId)).split(';')

    optkeys = [key] if key

    optkeys.each{|optkey|
      optkey = optkey.to_s

      pathname = [win, tag, optkey].join(';')

      if (fobj = TkFont.used_on(pathname))
        fobj = TkFont.new(fobj) # create a new TkFont object
      elsif Tk::JAPANIZED_TK
        fobj = fontobj          # create a new TkFont object
      else
        tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << ltn))
        next
      end

      if fobj.kind_of?(TkFont)
        if ltn.kind_of?(TkFont)
          conf = {}
          ltn.latin_configinfo.each{|key,val| conf[key] = val}
          if keys
            fobj.latin_configure(conf.update(keys))
          else
            fobj.latin_configure(conf)
          end
        else
          fobj.latin_replace(ltn)
        end
      end

      fobj.call_font_configure([pathname, optkey], *(__item_config_cmd(tagid(tagOrId)) << {}))
    }
    self
  end
  alias asciitagfont_configure latintagfont_configure

  def kanjitagfont_configure(tagOrId, knj, keys=nil)
    if knj.kind_of?(Array)
      key = knj[1]
      knj = knj[0]
    else
      key = nil
    end

    optkeys = __item_font_optkeys(tagid(tagOrId))
    if key && !optkeys.find{|opt| opt.to_s == key.to_s}
      fail ArgumentError, "unknown font option name `#{key}'"
    end

    win, tag = __item_pathname(tagid(tagOrId)).split(';')

    optkeys = [key] if key

    optkeys.each{|optkey|
      optkey = optkey.to_s

      pathname = [win, tag, optkey].join(';')

      if (fobj = TkFont.used_on(pathname))
        fobj = TkFont.new(fobj) # create a new TkFont object
      elsif Tk::JAPANIZED_TK
        fobj = fontobj          # create a new TkFont object
      else
        tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << knj))
        next
      end

      if fobj.kind_of?(TkFont)
        if knj.kind_of?(TkFont)
          conf = {}
          knj.kanji_configinfo.each{|key,val| conf[key] = val}
          if keys
            fobj.kanji_configure(conf.update(keys))
          else
            fobj.kanji_configure(conf)
          end
        else
          fobj.kanji_replace(knj)
        end
      end

      fobj.call_font_configure([pathname, optkey], *(__item_config_cmd(tagid(tagOrId)) << {}))
    }
    self
  end

  def tagfont_copy(tagOrId, win, wintag=nil, winkey=nil, targetkey=nil)
    if wintag
      if winkey
        fnt = win.tagfontobj(wintag, winkey).dup
      else
        fnt = win.tagfontobj(wintag).dup
      end
    else
      if winkey
        fnt = win.fontobj(winkey).dup
      else
        fnt = win.fontobj.dup
      end
    end

    if targetkey
      fnt.call_font_configure([__item_pathname(tagid(tagOrId)), targetkey], 
                              *(__item_config_cmd(tagid(tagOrId)) << {}))
    else
      fnt.call_font_configure(__item_pathname(tagid(tagOrId)), 
                              *(__item_config_cmd(tagid(tagOrId)) << {}))
    end
    self
  end


  def latintagfont_copy(tagOrId, win, wintag=nil, winkey=nil, targetkey=nil)
    if targetkey
      fontobj(targetkey).dup.call_font_configure([__item_pathname(tagid(tagOrId)), targetkey], 
                                                 *(__item_config_cmd(tagid(tagOrId)) << {}))
    else
      fontobj.dup.call_font_configure(__item_pathname(tagid(tagOrId)), 
                                      *(__item_config_cmd(tagid(tagOrId)) << {}))
    end

    if wintag
      if winkey
        fontobj.latin_replace(win.tagfontobj(wintag, winkey).latin_font_id)
      else
        fontobj.latin_replace(win.tagfontobj(wintag).latin_font_id)
      end
    else
      if winkey
        fontobj.latin_replace(win.fontobj(winkey).latin_font_id)
      else
        fontobj.latin_replace(win.fontobj.latin_font_id)
      end
    end
    self
  end
  alias asciitagfont_copy latintagfont_copy

  def kanjifont_copy(tagOrId, win, wintag=nil, winkey=nil, targetkey=nil)
    if targetkey
      fontobj(targetkey).dup.call_font_configure([__item_pathname(tagid(tagOrId)), targetkey], 
                                                 *(__item_config_cmd(tagid(tagOrId)) << {}))
    else
        fontobj.dup.call_font_configure(__item_pathname(tagid(tagOrId)), 
                                        *(__item_config_cmd(tagid(tagOrId)) << {}))
    end

    if wintag
      if winkey
        fontobj.kanji_replace(win.tagfontobj(wintag, winkey).kanji_font_id)
      else
        fontobj.kanji_replace(win.tagfontobj(wintag).kanji_font_id)
      end
    else
      if winkey
        fontobj.kanji_replace(win.fontobj(winkey).kanji_font_id)
      else
        fontobj.kanji_replace(win.fontobj.kanji_font_id)
      end
    end
    self
  end
end