Sha256: 0dc52ceeb28413c63f1c4d99369ae7bb72b668b88007ae5cbee897ad5918f6b7

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

module Bioshogi
  module CoverImage
    concern :CoreMethods do
      class_methods do
        def default_params
          {
            :text         => nil,
            :width        => 1200,
            :height       => 630,
            :image_format => "png",
            :font_regular => ASSETS_DIR.join("fonts/RictyDiminished-Regular.ttf"),  # 駒のフォント(普通)
            :font_bold    => ASSETS_DIR.join("fonts/RictyDiminished-Bold.ttf"),     # 駒のフォント(太字)
            :bg_color     => "hsl(0,0%,15%)",
            :font_color   => "hsl(0,0%,100%)",
          }
        end
      end

      attr_accessor :params

      def initialize(params = {})
        require "rmagick"
        @params = self.class.default_params.merge(params)
      end

      def render
        @canvas_layer = canvas_layer_create
        main_text_render
        bottom_text_render
        @canvas_layer
      end

      private

      # Ricty フォントでは全角が "「」" になってしまうため透明にする
      def text_normalize(str)
        str.to_s.gsub(/\u3000/, "  ")
      end

      def canvas_layer_create
        Magick::Image.new(*image_rect) do |e|
          e.background_color = params[:bg_color]
          if false
            # 予想に反してこれを指定しても RGB で保存されない
            # たまたま白黒だった画像をRGBで保存するには PNG24: としないといけない
            e.colorspace = Magick::SRGBColorspace
          end
        end
      end

      def image_rect
        Rect.new(params[:width], params[:height])
      end

      def ext_name
        params[:image_format]
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bioshogi-0.0.15 lib/bioshogi/cover_image/core_methods.rb
bioshogi-0.0.14 lib/bioshogi/cover_image/core_methods.rb
bioshogi-0.0.10 lib/bioshogi/cover_image/core_methods.rb
bioshogi-0.0.9 lib/bioshogi/cover_image/core_methods.rb
bioshogi-0.0.8 lib/bioshogi/cover_image/core_methods.rb
bioshogi-0.0.7 lib/bioshogi/cover_image/core_methods.rb
bioshogi-0.0.5 lib/bioshogi/cover_image/core_methods.rb
bioshogi-0.0.4 lib/bioshogi/cover_image/core_methods.rb
bioshogi-0.0.3 lib/bioshogi/cover_image/core_methods.rb