Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
フォントのweight値を書き換える
#!/usr/bin/env ruby
fail unless ARGV.length == 2
file = ARGV[0]
weight = ARGV[1].to_i
open(file, "r+b:ASCII-8BIT") do |io|
ver, ntables, srange, eselector, rshift = io.read(12).unpack("Nn*")
fail unless Math.log2(ntables).floor == eselector and
(2**eselector)*16 == srange and
ntables*16-srange == rshift
header_pos = nil
ntables.times do
if io.read(4) == "OS/2"
header_pos = io.pos - 4
break
end
io.pos += 12
end
fail unless header_pos
checksum, offset, length = io.read(12).unpack("N*")
io.pos = offset + 4
io.write([weight].pack("n"))
io.pos = offset
sum = 0
(length/4.0).ceil.times { sum += io.read(4).unpack("N")[0] }
sum &= 0xffffffff
io.pos = header_pos + 4
io.write([sum].pack("N"))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment