Class: Miyako::Font
Constant Summary
collapse
- @@font_cache =
{}
- @@name_2_font_path =
Hash.new
- @@font_base_path =
Hash.new
- @@font_base_name =
Hash.new
- @@initialized =
false
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#bold ⇒ Object
-
#bold=(f) ⇒ Object
-
#bold? ⇒ Boolean
-
#color_during(color) ⇒ Object
-
#dispose ⇒ Object
-
#draw_text(dst, str, x, y) ⇒ Object
-
#get_fit_size(size) ⇒ Object
-
#initialize(fname, size = 16) ⇒ Font
constructor
-
#initialize_copy(obj) ⇒ Object
-
#italic ⇒ Object
-
#italic=(f) ⇒ Object
-
#italic? ⇒ Boolean
-
#line_height ⇒ Object
-
#margin_height(align, height) ⇒ Object
-
#normal ⇒ Object
-
#render ⇒ Object
-
#render_str(text) ⇒ Object
-
#render_str_to(dst, text) ⇒ Object
-
#render_to(dst) ⇒ Object
-
#render_xy(x, y) ⇒ Object
-
#render_xy_to(dst, x, y) ⇒ Object
-
#shadow_during(color = @shadow_color, margin = @shadow_margin) ⇒ Object
-
#size_during(sz) ⇒ Object
-
#text_size(txt) ⇒ Object
-
#under_line ⇒ Object
-
#under_line=(f) ⇒ Object
-
#under_line? ⇒ Boolean
-
#visible ⇒ Object
-
#visible=(v) ⇒ Object
Methods included from Layout
#add_snap_child, #bottom, #bottom!, #broad_rect, #center, #center!, #centering, #centering!, #copy_layout, #delete_snap_child, #get_snap_children, #get_snap_sprite, #h, #include_snap_child?, #init_layout, #layout_dispose, #left, #left!, #middle, #middle!, #move, #move!, #move_to, #move_to!, #on_move, #outside_bottom, #outside_bottom!, #outside_left, #outside_left!, #outside_right, #outside_right!, #outside_top, #outside_top!, #pos, #rect, #relative_move_to, #relative_move_to!, #reset_snap, #right, #right!, #segment, #set_layout_size, #set_snap_children, #set_snap_sprite, #snap, #top, #top!, #update_layout, #update_layout_position, #w, #x, #y
Methods included from Animation
[], []=, anim_hash, #reset, reset, start, #start, #stop, stop, update, update_animation, #update_animation
Methods included from SpriteBase
#bitmap, #hide, #image_rect, #image_size, #oh, #oh=, #ow, #ow=, #ox, #ox=, #oy, #oy=, #part_rect, #rect, #render_d, #show, #to_sprite, #to_unit, #update
Constructor Details
#initialize(fname, size = 16) ⇒ Font
Returns a new instance of Font.
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/Miyako/API/font.rb', line 127
def initialize(fname, size=16)
init_layout
@size = size
@color = [255, 255, 255]
@fname = fname
@vspace = 0
@hspace = 0
@bold = false
@italic = false
@under_line = false
@fpath = Font.findFontPath(@fname) or raise MiyakoIOError.new(@fname)
@font = Font.get_font_inner(@fname, @fpath, @size)
@font.style = SDL::TTF::STYLE_NORMAL
init_height
@use_shadow = false
@shadow_color = [128, 128, 128]
@shadow_margin = [2, 2]
@unit = SpriteUnitFactory.create
@text = ""
@visible = true
set_layout_size(*self.text_size(@text))
end
|
Instance Attribute Details
OS_MAC_OS_X = “mac_osx”
ORG_ENC = "UTF-8"
NEW_ENC = "UTF-8-MAC"
34
35
36
|
# File 'lib/Miyako/API/font.rb', line 34
def ascent
@ascent
end
|
Returns the value of attribute color.
35
36
37
|
# File 'lib/Miyako/API/font.rb', line 35
def color
@color
end
|
OS_MAC_OS_X = “mac_osx”
ORG_ENC = "UTF-8"
NEW_ENC = "UTF-8-MAC"
34
35
36
|
# File 'lib/Miyako/API/font.rb', line 34
def descent
@descent
end
|
OS_MAC_OS_X = “mac_osx”
ORG_ENC = "UTF-8"
NEW_ENC = "UTF-8-MAC"
34
35
36
|
# File 'lib/Miyako/API/font.rb', line 34
def height
@height
end
|
Returns the value of attribute hspace.
35
36
37
|
# File 'lib/Miyako/API/font.rb', line 35
def hspace
@hspace
end
|
#line_skip ⇒ Object
OS_MAC_OS_X = “mac_osx”
ORG_ENC = "UTF-8"
NEW_ENC = "UTF-8-MAC"
34
35
36
|
# File 'lib/Miyako/API/font.rb', line 34
def line_skip
@line_skip
end
|
#shadow_color ⇒ Object
Returns the value of attribute shadow_color.
35
36
37
|
# File 'lib/Miyako/API/font.rb', line 35
def shadow_color
@shadow_color
end
|
#shadow_margin ⇒ Object
Returns the value of attribute shadow_margin.
35
36
37
|
# File 'lib/Miyako/API/font.rb', line 35
def shadow_margin
@shadow_margin
end
|
OS_MAC_OS_X = “mac_osx”
ORG_ENC = "UTF-8"
NEW_ENC = "UTF-8-MAC"
34
35
36
|
# File 'lib/Miyako/API/font.rb', line 34
def size
@size
end
|
OS_MAC_OS_X = “mac_osx”
ORG_ENC = "UTF-8"
NEW_ENC = "UTF-8-MAC"
34
35
36
|
# File 'lib/Miyako/API/font.rb', line 34
def text
@text
end
|
#use_shadow ⇒ Object
Returns the value of attribute use_shadow.
35
36
37
|
# File 'lib/Miyako/API/font.rb', line 35
def use_shadow
@use_shadow
end
|
Returns the value of attribute vspace.
35
36
37
|
# File 'lib/Miyako/API/font.rb', line 35
def vspace
@vspace
end
|
Class Method Details
.findFontPath(fname) ⇒ Object
101
102
103
104
|
# File 'lib/Miyako/API/font.rb', line 101
def Font.findFontPath(fname) fname = fname.encode(Encoding::UTF_8)
return @@name_2_font_path.fetch(fname, nil)
end
|
.get_font_inner(fname, fpath, size = 16) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/Miyako/API/font.rb', line 106
def Font.get_font_inner(fname, fpath, size=16) if Miyako.getOSName == "win" and RUBY_VERSION >= '1.9.0' and RUBY_VERSION < '1.9.2'
fname.force_encoding(Encoding::WINDOWS_31J)
fpath.force_encoding(Encoding::WINDOWS_31J)
end
fname = fname.encode(Encoding::UTF_8)
fpath = fpath.encode(Encoding::UTF_8)
@@font_cache[fname] ||= {}
@@font_cache[fname][size] ||= SDL::TTF.open(fpath, size)
return @@font_cache[fname][size]
end
|
68
69
70
71
72
73
|
# File 'lib/Miyako/API/font.rb', line 68
def Font.init
raise MiyakoError, "Already initialized!" if @@initialized
SDL::TTF.init
Font.create_font_path
@@initialized = true
end
|
.initialized? ⇒ Boolean
75
76
77
|
# File 'lib/Miyako/API/font.rb', line 75
def Font.initialized?
@@initialized
end
|
.sans_serif ⇒ Object
393
394
395
396
397
398
399
|
# File 'lib/Miyako/API/font.rb', line 393
def Font::sans_serif
filename = @@font_base_name[Miyako::getOSName].
detect{|base|
Font.findFontPath(base[:sans_serif])
}[:sans_serif]
return Font.new(filename)
end
|
.search_font_path_file(hash, path) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/Miyako/API/font.rb', line 79
def Font.search_font_path_file(hash, path) Dir.glob(File.join(path,"*")){|d|
hash = Font.search_font_path_file(hash, d) if test(?d, d)
if Miyako.getOSName == "win" and RUBY_VERSION >= '1.9.0' and RUBY_VERSION < '1.9.2'
d.force_encoding(Encoding::WINDOWS_31J)
end
d = d.encode(Encoding::UTF_8)
fname = File.split(d)[1]
hash[fname] = d if (fname =~ /\.tt[fc]\z/ || fname =~ /\.otf\z/) }
return hash
end
|
.system_font ⇒ Object
401
402
403
|
# File 'lib/Miyako/API/font.rb', line 401
def Font::system_font return Font.serif
end
|
Instance Method Details
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/Miyako/API/font.rb', line 231
def bold
if block_given?
tbold, self.bold = self.bold?, true
yield
self.bold = tbold
else
self.bold = true
end
return self
end
|
246
247
248
249
250
251
|
# File 'lib/Miyako/API/font.rb', line 246
def bold=(f)
@bold = f
@font.style |= SDL::TTF::STYLE_BOLD
@font.style -= SDL::TTF::STYLE_BOLD unless @bold
return self
end
|
#bold? ⇒ Boolean
242
243
244
|
# File 'lib/Miyako/API/font.rb', line 242
def bold?
return @bold
end
|
#color_during(color) ⇒ Object
197
198
199
200
201
202
203
|
# File 'lib/Miyako/API/font.rb', line 197
def color_during(color)
raise MiyakoError, "not given block!" unless block_given?
tcolor, self.color = @color, color
yield
self.color = tcolor
return self
end
|
227
228
229
|
# File 'lib/Miyako/API/font.rb', line 227
def dispose
@font = nil
end
|
#draw_text(dst, str, x, y) ⇒ Object
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/Miyako/API/font.rb', line 302
def draw_text(dst, str, x, y)
str = str.encode(Encoding::UTF_8)
str.chars{|c|
if @use_shadow
src2 = @font.renderBlendedUTF8(c, @shadow_color[0], @shadow_color[1], @shadow_color[2])
if src2
SpriteUnitFactory.apply(@unit, {:bitmap=>src2, :ow=>src2.w, :oh=>src2.h})
Miyako::Bitmap.blit_aa(@unit, dst.to_unit, x+@shadow_margin[0], y+@shadow_margin[1])
else
break x
end
end
src = @font.renderBlendedUTF8(c, @color[0], @color[1], @color[2])
if src
SpriteUnitFactory.apply(@unit, {:bitmap=>src, :ow=>src.w, :oh=>src.h})
Miyako::Bitmap.blit_aa(@unit, dst.to_unit, x, y)
else
break x
end
x += chr_size_inner(c)
}
return x
end
|
#get_fit_size(size) ⇒ Object
217
218
219
220
221
|
# File 'lib/Miyako/API/font.rb', line 217
def get_fit_size(size)
path = Font.findFontPath(@fname)
font = SDL::TTF.open(path, size)
return (size.to_f * (size.to_f / font.line_skip.to_f)).to_i
end
|
#initialize_copy(obj) ⇒ Object
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/Miyako/API/font.rb', line 150
def initialize_copy(obj) @size = @size.dup
@color = @color.dup
@fname = @fname.dup
@fpath = @fpath.dup
@shadow_color = @shadow_color.dup
@shadow_margin = @shadow_margin.dup
@unit = @unit.dup
@text = @text.dup
end
|
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/Miyako/API/font.rb', line 253
def italic
if block_given?
titalic, self.italic = self.italic?, true
yield
self.italic = titalic
else
self.italic = true
end
return self
end
|
#italic=(f) ⇒ Object
268
269
270
271
272
273
|
# File 'lib/Miyako/API/font.rb', line 268
def italic=(f)
@italic = f
@font.style |= SDL::TTF::STYLE_ITALIC
@font.style -= SDL::TTF::STYLE_ITALIC unless @italic
return self
end
|
#italic? ⇒ Boolean
264
265
266
|
# File 'lib/Miyako/API/font.rb', line 264
def italic?
return @italic
end
|
#line_height ⇒ Object
223
224
225
|
# File 'lib/Miyako/API/font.rb', line 223
def line_height
return @line_skip + @vspace + (@use_shadow ? @shadow_margin[1] : 0)
end
|
#margin_height(align, height) ⇒ Object
375
376
377
378
379
380
381
382
383
384
385
386
|
# File 'lib/Miyako/API/font.rb', line 375
def margin_height(align, height)
case align
when :top
return 0
when :middle
return (height - self.line_height) >> 1
when :bottom
return height - self.line_height
end
raise MiyakoError, "Illegal margin_height align! : #{align}"
end
|
297
298
299
300
|
# File 'lib/Miyako/API/font.rb', line 297
def normal
@font.style = 0
return self
end
|
326
327
328
329
330
|
# File 'lib/Miyako/API/font.rb', line 326
def render
return self unless @visible
draw_text(Screen, @text, @layout[:pos][0], @layout[:pos][1])
return self
end
|
#render_str(text) ⇒ Object
350
351
352
353
354
|
# File 'lib/Miyako/API/font.rb', line 350
def render_str(text)
return self unless @visible
draw_text(Screen, text.to_s, @layout[:pos][0], @layout[:pos][1])
return self
end
|
#render_str_to(dst, text) ⇒ Object
356
357
358
359
360
|
# File 'lib/Miyako/API/font.rb', line 356
def render_str_to(dst, text)
return self unless @visible
draw_text(dst, text.to_s, @layout[:pos][0], @layout[:pos][1])
return self
end
|
#render_to(dst) ⇒ Object
332
333
334
335
336
|
# File 'lib/Miyako/API/font.rb', line 332
def render_to(dst)
return self unless @visible
draw_text(dst, @text, @layout[:pos][0], @layout[:pos][1])
return self
end
|
#render_xy(x, y) ⇒ Object
338
339
340
341
342
|
# File 'lib/Miyako/API/font.rb', line 338
def render_xy(x, y)
return self unless @visible
draw_text(Screen, @text, x, y)
return self
end
|
#render_xy_to(dst, x, y) ⇒ Object
344
345
346
347
348
|
# File 'lib/Miyako/API/font.rb', line 344
def render_xy_to(dst, x, y)
return self unless @visible
draw_text(dst, @text, x, y)
return self
end
|
#shadow_during(color = @shadow_color, margin = @shadow_margin) ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/Miyako/API/font.rb', line 205
def shadow_during(color = @shadow_color, margin = @shadow_margin)
raise MiyakoError, "not given block!" unless block_given?
tflag, @use_shadow = @use_shadow, true
tcolor, @shadow_color = @shadow_color, color
tmargin, @shadow_margin = @shadow_margin, margin
yield
@use_shadow = tflag
@shadow_color = tcolor
@shadow_margin = tmargin
return self
end
|
#size_during(sz) ⇒ Object
188
189
190
191
192
193
194
195
|
# File 'lib/Miyako/API/font.rb', line 188
def size_during(sz)
raise MiyakoError, "not given block!" unless block_given?
tsize = @size
self.size = sz
yield
self.size = tsize
return self
end
|
#text_size(txt) ⇒ Object
368
369
370
371
372
373
|
# File 'lib/Miyako/API/font.rb', line 368
def text_size(txt)
width = txt.chars.inject(0){|r, c|
r += (c.bytesize == 1 ? @size >> 1 : @size) } +
((@use_shadow ? @shadow_margin[0] : 0) + @hspace) * (txt.chars.to_a.length - 1)
return [width, self.line_height]
end
|
#under_line ⇒ Object
275
276
277
278
279
280
281
282
283
284
|
# File 'lib/Miyako/API/font.rb', line 275
def under_line
if block_given?
tunder_line, self.under_line = self.under_line?, true
yield
self.under_line = tunder_line
else
self.under_line = true
end
return self
end
|
#under_line=(f) ⇒ Object
290
291
292
293
294
295
|
# File 'lib/Miyako/API/font.rb', line 290
def under_line=(f)
@under_line = f
@font.style |= SDL::TTF::STYLE_UNDERLINE
@font.style -= SDL::TTF::STYLE_UNDERLINE unless @under_line
return self
end
|
#under_line? ⇒ Boolean
286
287
288
|
# File 'lib/Miyako/API/font.rb', line 286
def under_line?
return @under_line
end
|
161
162
163
|
# File 'lib/Miyako/API/font.rb', line 161
def visible
return @visible
end
|
#visible=(v) ⇒ Object
165
166
167
168
|
# File 'lib/Miyako/API/font.rb', line 165
def visible=(v)
@visible = v
return self
end
|