Sha256: 3cb41db15dd8b0c7792d3f871a8334e590099a65822dabf5c3bb6267d44b4338

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

require_relative 'unit_conversion'

module Squib
  module Args
    module XYWHShorthands
      WIDTH_MINUS_REGEX = /^width\s*\-\s*/
      HEIGHT_MINUS_REGEX = /^height\s*\-\s*/
      WIDTH_DIV_REGEX = /^width\s*\/\s*/
      HEIGHT_DIV_REGEX = /^height\s*\/\s*/
      MIDDLE_PLUS_REGEX = /^middle\s*\+\s*/
      MIDDLE_MINUS_REGEX = /^middle\s*\-\s*/


      # dimension is usually either deck_width or deck_height
      def apply_shorthands(arg, deck, axis: :x)
        dimension = (axis == :x) ? deck.width : deck.height
        arg_s = arg.to_s
        case arg_s
        when 'middle'
          dimension / 2.0
        when 'center'
          dimension / 2.0
        when 'deck'
          dimension
        when WIDTH_MINUS_REGEX # e.g. width - 1.5in
          n = arg_s.sub WIDTH_MINUS_REGEX, ''
          n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
          deck.width - n
        when HEIGHT_MINUS_REGEX # e.g. height - 1.5in
          n = arg_s.sub HEIGHT_MINUS_REGEX, ''
          n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
          deck.height - n
        when WIDTH_DIV_REGEX # e.g. width / 3
          n = arg_s.sub WIDTH_DIV_REGEX, ''
          n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
          deck.width / n
        when HEIGHT_DIV_REGEX # e.g. height / 3
          n = arg_s.sub HEIGHT_DIV_REGEX, ''
          n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
          deck.height / n
        when MIDDLE_PLUS_REGEX # e.g. middle + 1.5in
          n = arg_s.sub MIDDLE_PLUS_REGEX, ''
          n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
          dimension / 2.0 + n
        when MIDDLE_MINUS_REGEX # e.g. middle - 1.5in
          n = arg_s.sub MIDDLE_MINUS_REGEX, ''
          n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
          dimension / 2.0 - n
        else
          arg
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
squib-0.16.0.pre.preview1 lib/squib/args/xywh_shorthands.rb