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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
class TkFont
include Tk
extend TkCore
Tk_FontID = [0]
Tk_FontNameTBL = {}
Tk_FontUseTBL = {}
DEFAULT_LATIN_FONT_NAME = 'a14'.freeze
DEFAULT_KANJI_FONT_NAME = 'k14'.freeze
###################################
# class methods
###################################
def TkFont.families(window=nil)
case (Tk::TK_VERSION)
when /^4.*/
['fixed']
when /^8.*/
if window
list(tk_call('font', 'families', '-displayof', window))
else
list(tk_call('font', 'families'))
end
end
end
def TkFont.names
r = []
case (Tk::TK_VERSION)
when /^4.*/
r += ['fixed', 'a14', 'k14']
Tk_FontNameTBL.each_value{|obj| r.push(obj)}
when /^8.*/
list(tk_call('font', 'names')).each{|f|
if f =~ /^(@font[0-9]+)(c|l|k)$/
r.push(Tk_FontNameTBL[$1]) if $2 == 'c'
else
r.push(f)
end
}
end
r
end
###################################
private
###################################
def initialize(ltn=nil, knj=nil, keys=nil)
@id = format("@font%.4d", Tk_FontID[0])
Tk_FontID[0] += 1
Tk_FontNameTBL[@id] = self
ltn = DEFAULT_LATIN_FONT_NAME unless ltn
create_latinfont(ltn)
knj = DEFAULT_KANJI_FONT_NAME unless knj
create_kanjifont(knj)
create_compoundfont(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: '*'
Array([foundry, family, weight, slant, swidth, adstyle,
pixels, points, resx, resy, space, avgWidth, charset, encoding])
end
def create_latinfont_tk4x(font=nil)
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] && font[1] != '0' && font[1] =~ /^(|\+|-)([0-9]+)$/
if $1 == '-'
finfo['pixels'] = font[1].to_s
else
finfo['points'] = font[1].to_s
end
end
finfo[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
@latinfont = font
end
end
def create_kanjifont_tk4x(font=nil)
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] && font[1] != '0' && font[1] =~ /^(|\+|-)([0-9]+)$/
if $1 == '-'
finfo['pixels'] = $2
else
finfo['points'] = $2
end
else
finfo['points'] = '13'
end
finfo[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
@kanjifont = font
end
end
def create_compoundfont_tk4x(keys)
@compoundfont = [[@latinfont], [@kanjifont]]
@fontslot = {'font'=>@latinfont, 'kanjifont'=>@kanjifont}
end
def create_latinfont_tk80(font=nil)
@latinfont = @id + 'l'
if font.kind_of? Hash
tk_call('font', 'create', @latinfont, *hash_kv(font))
elsif font.kind_of? Array
tk_call('font', 'create', @latinfont, '-copy', array2tk_list(font))
elsif font.kind_of? TkFont
tk_call('font', 'create', @latinfont, '-copy', font.latin_font)
else
tk_call('font', 'create', @latinfont, '-copy', font)
end
end
def create_kanjifont_tk80(font=nil)
@kanjifont = @id + 'k'
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)
else
tk_call('font', 'create', @kanjifont, '-copy', font,
'-charset', 'jisx0208.1983')
end
end
def create_compoundfont_tk80(keys)
@compoundfont = @id + 'c'
@fontslot = {'font'=>@compoundfont}
tk_call('font', 'create', @compoundfont,
'-compound', "#{@latinfont} #{@kanjifont}", *hash_kv(keys))
end
def set_font_core_tk4x(window)
Tk_FontUseTBL[window.path] = @id
window.configure(@fontslot)
end
def set_font_core_tk80(window)
window.configure(@fontslot)
end
def actual_core_tk4x(font, window=nil, option=nil)
# dummy
if option
""
else
Array([ ['family',[]], ['size',[]], ['weight',[]], ['slant',[]],
['underline',[]], ['overstrike',[]], ['charset',[]],
['pointadjust',[]] ])
end
end
def actual_core_tk80(font, window=nil, option=nil)
if option == 'compound'
""
elsif option
if window
tk_call('font', 'actual', font, "-#{option}")
else
tk_call('font', 'actual', font, "-displayof", window, "-#{option}")
end
else
l = tk_split_list(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)
""
end
def configinfo_core_tk4x(font, option=nil)
# dummy
if option
""
else
Array([ ['family',[]], ['size',[]], ['weight',[]], ['slant',[]],
['underline',[]], ['overstrike',[]], ['charset',[]],
['pointadjust',[]] ])
end
end
def configure_core_tk80(font, slot, value=None)
if slot.kind_of? Hash
tk_call 'font', 'configure', font, *hash_kv(slot)
else
tk_call 'font', 'configure', font, "-#{slot}", value
end
end
def configinfo_core_tk80(font, option=nil)
if option == 'compound'
""
elsif option
tk_call('font', 'configure', font, "-#{option}")
else
l = tk_split_list(tk_call('font', 'configure', font))
r = []
while key=l.shift
if key == '-compound'
l.shift
else
r.push [key[1..-1], l.shift]
end
end
r
end
end
def latin_replace_core_tk4x(ltn)
create_latinfont_tk4x(ltn)
@compoundfont[0] = [@latinfont]
@fontslot['font'] = @latinfont
Tk_FontUseTBL.dup.each{|w, id|
if id == @id
begin
w.configure('font', @latinfont)
rescue
Tk_FontUseTBL[w] = nil
end
end
}
self
end
def kanji_replace_core_tk4x(knj)
create_kanjifont_tk4x(knj)
@compoundfont[1] = [@kanjifont]
@fontslot['kanjifont'] = @kanjifont
Tk_FontUseTBL.dup.each{|w, id|
if id == @id
begin
w.configure('kanjifont', @kanjifont)
rescue
Tk_FontUseTBL[w] = nil
end
end
}
self
end
def latin_replace_core_tk80(ltn)
tk_call('font', 'delete', @latinfont)
create_latinfont_tk80(ltn)
self
end
def kanji_replace_core_tk80(knj)
tk_call('font', 'delete', @kanjifont)
create_kanjifont_tk80(knj)
self
end
def measure_core_tk4x(window, text)
0
end
def measure_core_tk80(window, text)
if window
number(tk_call('font', 'measure', @compoundfont,
'-displayof', window, text))
else
number(tk_call('font', 'measure', @compoundfont, text))
end
end
def metrics_core_tk4x(font, window, option=nil)
# dummy
if option
""
else
Array([ ['ascent',[]], ['descent',[]], ['linespace',[]], ['fixed',[]] ])
end
end
def metrics_core_tk80(font, window, option=nil)
if option
if window
number(tk_call('font', 'metrics', font, "-#{option}"))
else
number(tk_call('font', 'metrics', font,
"-displayof", window, "-#{option}"))
end
else
l = tk_split_list(if window
tk_call('font','metrics',font,"-displayof",window)
else
tk_call('font','metrics',font)
end)
r = []
while key=l.shift
r.push [key[1..-1], l.shift.to_i]
end
r
end
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
alias set_font_core set_font_core_tk4x
alias actual_core actual_core_tk4x
alias configure_core configure_core_tk4x
alias configinfo_core configinfo_core_tk4x
alias latin_replace_core latin_replace_core_tk4x
alias kanji_replace_core kanji_replace_core_tk4x
alias measure_core measure_core_tk4x
alias metrics_core metrics_core_tk4x
when /^8\.0/
alias create_latinfont create_latinfont_tk80
alias create_kanjifont create_kanjifont_tk80
alias create_compoundfont create_compoundfont_tk80
alias set_font_core set_font_core_tk80
alias actual_core actual_core_tk80
alias configure_core configure_core_tk80
alias configinfo_core configinfo_core_tk80
alias latin_replace_core latin_replace_core_tk80
alias kanji_replace_core kanji_replace_core_tk80
alias measure_core measure_core_tk80
alias metrics_core metrics_core_tk80
end
###################################
public
###################################
def set_font(window)
set_font_core(window)
end
def latin_font
@latinfont
end
def kanji_font
@kanjifont
end
def actual(option=nil)
actual_core(@compoundfont, nil, option)
end
def actual_displayof(window, option=nil)
window = '.' unless window
actual_core(@compoundfont, window, option)
end
def latin_actual(option=nil)
actual_core(@latinfont, nil, option)
end
def latin_actual_displayof(window, option=nil)
window = '.' unless window
actual_core(@latinfont, window, option)
end
def kanji_actual(option=nil)
actual_core(@kanjifont, nil, option)
end
def kanji_actual_displayof(window, option=nil)
window = '.' unless window
actual_core(@kanjifont, window, option)
end
def [](slot)
configinfo slot
end
def []=(slot, val)
configure slot, val
end
def configure(slot, value=None)
configure_core(@compoundfont, slot, value)
end
def configinfo(slot=nil)
configinfo_core(@compoundfont, slot)
end
def latin_configure(slot, value=None)
configure_core(@latinfont, slot, value)
end
def latin_configinfo(slot=nil)
configinfo_core(@latinfont, slot)
end
def kanji_configure(slot, value=None)
configure_core(@kanjifont, slot, value)
end
def kanji_configinfo(slot=nil)
configinfo_core(@kanjifont, slot)
end
def replace(ltn, knj)
latin_replace(ltn)
kanji_replace(ltn)
end
def latin_replace(ltn)
latin_replace_core(ltn)
end
def kanji_replace(knj)
kanji_replace_core(knj)
end
def measure(text)
measure_core(nil, text)
end
def measure_displayof(window, text)
window = '.' unless window
measure_core(window, text)
end
def metrics(option=nil)
metrics_core(@compoundfont, nil, option)
end
def metrics_displayof(window, option=nil)
window = '.' unless window
metrics_core(@compoundfont, window, option)
end
def latin_metrics(option=nil)
metrics_core(@latinfont, nil, option)
end
def latin_metrics_displayof(window, option=nil)
window = '.' unless window
metrics_core(@latinfont, window, option)
end
def kanji_metrics(option=nil)
metrics_core(@kanjifont, nil, option)
end
def kanji_metrics_displayof(window, option=nil)
window = '.' unless window
metrics_core(@kanjifont, window, option)
end
###################################
# public alias
###################################
alias ascii_font latin_font
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
end
|