Class: Resedit::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/resedit/font/font.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, count: 256, bpp: 1) ⇒ Font

charWidth, charHeight, characters count



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/resedit/font/font.rb', line 12

def initialize(width, height, count: 256, bpp: 1)
    @width, @height, @count = width, height, count
    @gridColor = 0xFFEEEEEE
    @charColor = 0xFF000000
    @bgColor = 0xFFFFFFFF
    @widthColor = 0xFFFF0000
    @chars = {}
    @bpp = bpp
    @userData = nil
    @colmap = nil
    @valmap = nil
end

Instance Attribute Details

#bgColorObject

Returns the value of attribute bgColor.



9
10
11
# File 'lib/resedit/font/font.rb', line 9

def bgColor
  @bgColor
end

#bppObject (readonly)

Returns the value of attribute bpp.



8
9
10
# File 'lib/resedit/font/font.rb', line 8

def bpp
  @bpp
end

#charColorObject

Returns the value of attribute charColor.



9
10
11
# File 'lib/resedit/font/font.rb', line 9

def charColor
  @charColor
end

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/resedit/font/font.rb', line 8

def count
  @count
end

#gridColorObject

Returns the value of attribute gridColor.



9
10
11
# File 'lib/resedit/font/font.rb', line 9

def gridColor
  @gridColor
end

#heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/resedit/font/font.rb', line 8

def height
  @height
end

#userDataObject

Returns the value of attribute userData.



9
10
11
# File 'lib/resedit/font/font.rb', line 9

def userData
  @userData
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/resedit/font/font.rb', line 8

def width
  @width
end

#widthColorObject

Returns the value of attribute widthColor.



9
10
11
# File 'lib/resedit/font/font.rb', line 9

def widthColor
  @widthColor
end

Instance Method Details

#buildBppMapObject



25
26
27
28
# File 'lib/resedit/font/font.rb', line 25

def buildBppMap()
    return [@bgColor, @charColor] if @bpp==1
    return ColorMap.new(@bgColor, @charColor).mapBpp(@bpp)
end

#charFlags(id) ⇒ Object



67
68
69
70
# File 'lib/resedit/font/font.rb', line 67

def charFlags(id)
    return nil if !@chars[id]
    return @chars[id].flags
end

#charWidth(id) ⇒ Object



62
63
64
65
# File 'lib/resedit/font/font.rb', line 62

def charWidth(id)
    return nil if !@chars[id]
    return @chars[id].realWidth ? @chars[id].realWidth : @width
end

#colorMap(val) ⇒ Object



30
31
32
33
34
# File 'lib/resedit/font/font.rb', line 30

def colorMap(val)
    @colmap = buildBppMap() if !@colmap
    #puts "#{val} = #{@colmap[val].to_s(16)}"
    return @colmap[val]
end

#getChar(id) ⇒ Object



50
51
52
# File 'lib/resedit/font/font.rb', line 50

def getChar(id)
    @chars[id].data if @chars[id]
end

#load(filename) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/resedit/font/font.rb', line 95

def load(filename)
    img = Resedit.loadImage(filename)
    rows = @count/16 + (@count%16 == 0 ? 0 : 1)
    raise "Wrong image size" if (img.width!=@width*16+17 || img.height!=@height*rows+rows+1)
    for idx in 0..@count-1
        x = idx%16
        y = idx/16
        x += 1+x*@width
        y += 1+y*@height
        c = FontChar.new(self, height, idx)
        c.scan(img, x, y)
        @chars[c.index] = c if c.data
    end
    @valmap = nil
end

#maxCharObject



58
59
60
# File 'lib/resedit/font/font.rb', line 58

def maxChar
    return @chars.keys().max
end

#minCharObject



54
55
56
# File 'lib/resedit/font/font.rb', line 54

def minChar
    return @chars.keys().min
end

#save(filename) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/resedit/font/font.rb', line 72

def save(filename)
    rows = @count/16 + (@count%16 == 0 ? 0 : 1)
    img = Resedit.createImage(@width*16+17 , @height*rows+rows+1, filename)
    img.fill(@bgColor)
    #draw grid
    for i in 0..16
        img.vline(i*(@width+1), @gridColor)
    end
    for j in 0..rows
        img.hline(j*(@height+1), @gridColor)
    end
    #draw letters
    @chars.each { |idx,c|
        x = idx%16
        y = idx/16
        x += 1+x*@width
        y += 1+y*@height
        c.draw(img, x, y)
    }
    img.save(filename)
    @colmap = nil
end

#setChar(id, data, width = nil, flags = nil) ⇒ Object



45
46
47
48
# File 'lib/resedit/font/font.rb', line 45

def setChar(id, data, width=nil, flags=nil)
    width=@width if !width
    @chars[id] = FontChar.new(self, @height, id, data, width, flags)
end

#valueMap(col) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/resedit/font/font.rb', line 36

def valueMap(col)
    if !@valmap
        @valmap = Hash[buildBppMap().each_with_index.map {|x,i| [x, i]}] if !@valmap
    end
    val=@valmap[col]
    raise "Wrong color in font #{col.to_s(16)}" if val==nil
    return val
end