lib/squib/layout_parser.rb in squib-0.15.3 vs lib/squib/layout_parser.rb in squib-0.16.0.pre.preview1

- old
+ new

@@ -1,14 +1,17 @@ require 'yaml' +require_relative 'args/xywh_shorthands' module Squib # Internal class for handling layouts # @api private class LayoutParser + include Args::XYWHShorthands - def initialize(dpi = 300) + def initialize(dpi = 300, cell_px = 37.5) @dpi = dpi + @cell_px = cell_px end # Load the layout file(s), if exists # @api private def load_layout(files, initial = {}) @@ -65,10 +68,13 @@ end return h end def handle_relative_operators(parent_val, child_val) + if uses_operator?(child_val) && !has_digits?(parent_val) && !has_digits?(child_val) + raise "Layout parse error: can't combine #{parent_val} and #{child_val}" + end if child_val.to_s.strip.start_with?('+=') add_parent_child(parent_val, child_val) elsif child_val.to_s.strip.start_with?('-=') sub_parent_child(parent_val, child_val) elsif child_val.to_s.strip.start_with?('*=') @@ -79,30 +85,41 @@ child_val # child overrides parent when merging, no += end end def add_parent_child(parent, child) - parent_pixels = Args::UnitConversion.parse(parent, @dpi).to_f - child_pixels = Args::UnitConversion.parse(child.sub('+=', ''), @dpi).to_f + parent_pixels = Args::UnitConversion.parse(parent, @dpi, @cell_px).to_f + child_pixels = Args::UnitConversion.parse(child.sub('+=', ''), @dpi, @cell_px).to_f parent_pixels + child_pixels end def sub_parent_child(parent, child) - parent_pixels = Args::UnitConversion.parse(parent, @dpi).to_f - child_pixels = Args::UnitConversion.parse(child.sub('-=', ''), @dpi).to_f + parent_pixels = Args::UnitConversion.parse(parent, @dpi, @cell_px).to_f + child_pixels = Args::UnitConversion.parse(child.sub('-=', ''), @dpi, @cell_px).to_f parent_pixels - child_pixels end def mul_parent_child(parent, child) - parent_pixels = Args::UnitConversion.parse(parent, @dpi).to_f + parent_pixels = Args::UnitConversion.parse(parent, @dpi, @cell_px).to_f child_float = child.sub('*=', '').to_f parent_pixels * child_float end def div_parent_child(parent, child) - parent_pixels = Args::UnitConversion.parse(parent, @dpi).to_f + parent_pixels = Args::UnitConversion.parse(parent, @dpi, @cell_px).to_f child_float = child.sub('/=', '').to_f parent_pixels / child_float + end + + # For relative operators, it's difficult for us to handle + # some of the shorthands - so let's just freak out if you're trying to use + # relative operators with words, e.g. "middle += 0.5in" + def has_digits?(arg) + /.*\d.*/ === arg.to_s + end + + def uses_operator?(arg) + /^[+=|\-=|\/=|*=].*/ === arg.to_s end # Does this layout entry have an extends field? # i.e. is it a base-case or will it need recursion? # :nodoc: