まぐろを堪能

最初に勤めた会社の同僚の企画で、神奈川県の最南端(?)の三崎口にある 割烹旅館 立花 へ。食事だけでしたが、マグロを堪能してきました。

頼んだのは『まぐろ一匹コース』。3,200円(税別)で、生ビールをジョッキで 2杯飲んだとはいえ、料理だけでも十分なボリュームでお腹が一杯になりました。


と言いつつ、マグロの白子も食べてみました。意外にサッパリした味で美味しかったですねぇ。

ちょっとワインを飲みすぎて*1帰りに頭痛がしましたが、実に楽しい時間でした。S木君、企画してくれてありがとう。K智君、気をつけて新潟まで帰ってね。M田君、新婚なのに奥さん置いてきちゃって大丈夫だったかな? お土産のマグロのカマと中トロ刺身で許してもらってね :)。N田さん、これに懲りずにまた参加してください。うちに来てくれても OK ですし。

*1:といってもグラス 2杯。弱いんです...

リハビリ継続中

で、マグロを食べに行く前に大よそは終わっていたのですが、帰宅してからも 1時間くらいゴニョゴニョしてようやく本日の課題終了。

問題:グローバル変数を使いまくっている既存ツールを改修する。

回答:

#!/usr/local/bin/ruby
#
# $Id: mkimage.rb,v 1.2 2007/07/31 01:32:15 ueda Exp $
#

require 'GD'
require 'rexml/document'
require 'time'
require 'fileutils'
require 'tempfile'


class TopVirusImg
    BASE_IMG = '/..../img/top_5_viruses_base.png'

    def initialize(path = nil)
        @im       = GD::Image.new_from_png(BASE_IMG)
        @path     = path
        @font     = {
            :path  => '/..../img/xxxxxx.ttc',
            :size  => 9,
            :angle => 0
        }

        @color = {
            :black => @im.colorAllocate(0,0,0),
            :red   => @im.colorAllocate(255,0,0),
            :blue  => @im.colorAllocate(0,0,255),
            :white => @im.colorAllocate(255,255,255),
            :gray  => @im.colorAllocate(204,204,204)
        }

        @position = {
            :x1 => 0, :x2 => 448,
            :y1 => 5, :y2 => 14,
            :bar  => {:top => 133, :max => 316},
            :date => {:x_max => 488, :y => 57}
        }
    end

    def draw_bars(data, max)
        data.each do |virus|
            place   = virus['place'].to_i - 1
            percent = virus['percents'].to_f
            name    = virus['vname']

            x = @position[:bar][:top] + (@position[:bar][:max]*percent/max)

            (0..name.size).each do |i|
                vname = name.slice(/\A.{0,#{name.size-i}}/m)
                next if font_width_by_pixel(vname) > 109
                @im.stringTTF(@color[:black] ,@font[:path], @font[:size], @font[:angle],
                    27, @position[:y2]+(11*place), vname)
            end

            @im.filledRectangle(x, @position[:y1]+(11*place),
                @position[:x2], @position[:y2]+(11*place), @color[:white])

            @im.stringTTF(@color[:black], @font[:path], @font[:size], @font[:angle],
                x+2, @position[:y2]+(11*place)+1, percent.to_s+'%')
        end
    end

    def write_date(date)
        @im.stringTTF(@color[:gray], @font[:path],@font[:size], @font[:angle],
            (@position[:date][:x_max]-font_width_by_pixel(date)), @position[:date][:y], date)
    end

    def dump(path = nil)
        path = @path unless path

        Tempfile.open('tmp-'+File.basename(path), File.dirname(path)) do |file|
            @im.png(File.open(file.path, 'w'))
            @im.destroy
            FileUtils.chmod(0644, file.path)
            FileUtils.mv(file.path, path)
        end
    end

    private
    def font_width_by_pixel(string)
        brect = @im.stringTTF(@color[:white], @font[:path], @font[:size], @font[:angle],
            100, 100, string)
        (brect[1][2]-brect[1][0]).to_i
    end

    def deg2rad(degree)
        degree*(Math::PI/180.0)
    end
end


if __FILE__ == $0
    include REXML

    xmldata = '/..../feeds/top5.xml'
    output  = '/..../img/top_5_viruses.png'

    # 更新要否の確認
    if File.exists?(output) && FileUtils.uptodate?(xmldata, [output])
        puts "[mkimage] INFO: \"#{File.basename(xmldata)}\" is NOT newer than \"#{File.basename(output)}\", so exit..."
        exit 0
    end

    # XMLデータのパース
    doc = REXML::Document.new(File.new(xmldata))
    lastupdate = Time.parse(doc.elements['virustop'].attributes['updatedjst']).strftime('%Y/%m/%d(%a)%X %Z')

    virus_top = []
    max_data = 0
    doc.elements.each('virustop/item'){|element|
        virus_type = {
            'place' => element.elements['place'].text,
            'vname' => element.elements['vname'].text,
            'percents' => element.elements['percents'].text
        }
        raise 'There is no XML data.' unless virus_type['place'] ||virus_type['percents'] ||virus_type['vname']
        virus_top << virus_type

        if virus_type['place'] == '1'
            max_data = virus_type['percents'].to_f
        end
    }


    # 画像の描画・出力
    topImage = TopVirusImg.new
    topImage.draw_bars(virus_top, max_data)
    topImage.write_date(lastupdate)
    topImage.dump(output)
end

感想:

  • 昔はグローバル変数を使わないと書けなかったけど、今なら何とかなる気がしてきました。
  • なぜか FileUtils.install が上手く動かない。仕方ないので FileUtils.mvFileUtils.chmod を使用した。

ちなみにこのツールは Dr.WEB サイトで使用していて、

という画像を

にするためのものです。大した処理ではないのですが、手作業ではやってられないので便利です。