Lots more implementation
* implement middle+/- * cells unit * samples for both * moved shorthands to the units/ field Note: cell_px is currently unconfigurabledev
parent
2969957c5b
commit
f8eaf757a2
|
|
@ -1,9 +1,10 @@
|
||||||
require_relative '../constants'
|
require_relative '../constants'
|
||||||
|
|
||||||
|
|
||||||
module Squib
|
module Squib
|
||||||
module Args
|
module Args
|
||||||
module UnitConversion
|
module UnitConversion
|
||||||
module_function def parse(arg, dpi=300)
|
module_function def parse(arg, dpi=300, cell_px=75)
|
||||||
case arg.to_s.rstrip
|
case arg.to_s.rstrip
|
||||||
when /in$/ # ends with "in"
|
when /in$/ # ends with "in"
|
||||||
arg.rstrip[0..-2].to_f * dpi
|
arg.rstrip[0..-2].to_f * dpi
|
||||||
|
|
@ -15,6 +16,8 @@ module Squib
|
||||||
arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM / 10.0
|
arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM / 10.0
|
||||||
when /deg$/ # ends with "deg"
|
when /deg$/ # ends with "deg"
|
||||||
arg.rstrip[0..-3].to_f * (Math::PI / 180.0)
|
arg.rstrip[0..-3].to_f * (Math::PI / 180.0)
|
||||||
|
when /c(ell)?[s]?$/ # ends with 'c', 'cell', or 'cells'
|
||||||
|
arg.rstrip[0..-2].to_f * cell_px
|
||||||
else
|
else
|
||||||
arg
|
arg
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,11 @@ module Squib
|
||||||
HEIGHT_MINUS_REGEX = /^height\s*\-\s*/
|
HEIGHT_MINUS_REGEX = /^height\s*\-\s*/
|
||||||
WIDTH_DIV_REGEX = /^width\s*\/\s*/
|
WIDTH_DIV_REGEX = /^width\s*\/\s*/
|
||||||
HEIGHT_DIV_REGEX = /^height\s*\/\s*/
|
HEIGHT_DIV_REGEX = /^height\s*\/\s*/
|
||||||
|
CELL_REGEX = /^c[ell]?[s]?\s*/
|
||||||
|
MIDDLE_PLUS_REGEX = /^middle\s*\+\s*/
|
||||||
|
MIDDLE_MINUS_REGEX = /^middle\s*\-\s*/
|
||||||
|
|
||||||
|
|
||||||
# dimension is usually either deck_width or deck_height
|
# dimension is usually either deck_width or deck_height
|
||||||
def apply_shorthands(arg, deck, axis: :x)
|
def apply_shorthands(arg, deck, axis: :x)
|
||||||
dimension = (axis == :x) ? deck.width : deck.height
|
dimension = (axis == :x) ? deck.width : deck.height
|
||||||
|
|
@ -33,7 +37,15 @@ module Squib
|
||||||
when HEIGHT_DIV_REGEX # e.g. height / 3
|
when HEIGHT_DIV_REGEX # e.g. height / 3
|
||||||
n = (arg_s.sub HEIGHT_DIV_REGEX, '').to_f
|
n = (arg_s.sub HEIGHT_DIV_REGEX, '').to_f
|
||||||
deck.height / n
|
deck.height / n
|
||||||
else
|
when MIDDLE_PLUS_REGEX # e.g. middle + 1.5in
|
||||||
|
n = arg_s.sub MIDDLE_PLUS_REGEX, ''
|
||||||
|
n = UnitConversion.parse(n)
|
||||||
|
dimension / 2.0 + n
|
||||||
|
when MIDDLE_MINUS_REGEX # e.g. middle - 1.5in
|
||||||
|
n = arg_s.sub MIDDLE_MINUS_REGEX, ''
|
||||||
|
n = UnitConversion.parse(n)
|
||||||
|
dimension / 2.0 - n
|
||||||
|
else
|
||||||
arg
|
arg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -28,5 +28,10 @@ module Squib
|
||||||
n.to_f * (Math::PI / 180.0)
|
n.to_f * (Math::PI / 180.0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# DSL method. See http://squib.readthedocs.io
|
||||||
|
def cells(n)
|
||||||
|
n.to_f * @cell_px
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
require_relative '../../lib/squib'
|
||||||
|
# Use the following header instead if you are copy-pasta'ing
|
||||||
|
# require 'squib'
|
||||||
|
|
||||||
|
Squib::Deck.new(width: '1.5in', height: '1.5in') do
|
||||||
|
background color: :white
|
||||||
|
|
||||||
|
# Squib has a custom unit, called "cell"
|
||||||
|
# A "cell" unit defaults to 75px, which at 300dpi is 1/8in or 3.175mm
|
||||||
|
# This is a very common multiple for layouts.
|
||||||
|
# This helps us lay things out in grids without doing much math in our heads
|
||||||
|
# Here's an example... with grid!
|
||||||
|
grid width: '1 cell', height: '1 cell'
|
||||||
|
|
||||||
|
# Plurals are fine or just 'c' as a unit is fine
|
||||||
|
# Whitespace is pretty lenient too.
|
||||||
|
rect fill_color: :blue,
|
||||||
|
x: '1 cell', y: '2 cells',
|
||||||
|
width: '1c', height: '1cell '
|
||||||
|
|
||||||
|
# Technically, the "cell" is actually a "unit", so you can even combine
|
||||||
|
# with xywh shorhands!!
|
||||||
|
rect fill_color: :red,
|
||||||
|
x: 'middle + 0.5c', y: 'height - 1.5c',
|
||||||
|
width: '1c', height: '1c'
|
||||||
|
|
||||||
|
# And, unlike xywh shorthands, this applies basically everywhere we support
|
||||||
|
# unit conversion.
|
||||||
|
circle fill_color: :green,
|
||||||
|
x: '3c', y: '2c', radius: '1c'
|
||||||
|
# Decimals are fine too
|
||||||
|
circle fill_color: :green,
|
||||||
|
x: '5c', y: '2c', radius: '0.5c'
|
||||||
|
# Even dashes!
|
||||||
|
circle fill_color: '#0000', stroke_color: :purple,
|
||||||
|
x: '1c', y: '4c', radius: '0.5c', dash: '0.25c 0.25c'
|
||||||
|
|
||||||
|
# We can also do stuff in layout. Check out the yml file...
|
||||||
|
# (even cleaner in Yaml since we don't need quotes!)
|
||||||
|
use_layout file: 'cells.yml'
|
||||||
|
rect layout: :example
|
||||||
|
rect layout: :extends_example
|
||||||
|
|
||||||
|
save_png prefix: 'cells_'
|
||||||
|
end
|
||||||
|
|
@ -1,35 +1,36 @@
|
||||||
require_relative '../../lib/squib'
|
require_relative '../../lib/squib'
|
||||||
|
|
||||||
# Lots of DSL methods have shorthands that are accepted for
|
# Lots of DSL methods have shorthands that are accepted for
|
||||||
# x, y, width, and height parameters.
|
# x, y, width, and height parameters.
|
||||||
Squib::Deck.new(width: '0.5in', height: '0.25in') do
|
Squib::Deck.new(width: '0.5in', height: '0.25in') do
|
||||||
background color: :white
|
background color: :white
|
||||||
|
|
||||||
text str: 'xymiddle', font: 'Sans Bold 3', hint: :red,
|
# middle for x and y will resolve to half the height
|
||||||
|
text str: 'xymiddle', font: 'Sans Bold 3', hint: :red,
|
||||||
x: 'middle', y: :middle
|
x: 'middle', y: :middle
|
||||||
|
|
||||||
# 'center' also works
|
# 'center' also works
|
||||||
rect width: 30, height: 30,
|
rect width: 30, height: 30,
|
||||||
x: :center, y: 'center'
|
x: :center, y: 'center'
|
||||||
|
|
||||||
# Applies to shapes
|
# Applies to shapes
|
||||||
triangle x1: 20, y1: 20,
|
triangle x1: 20, y1: 20,
|
||||||
x2: 60, y2: 20,
|
x2: 60, y2: 20,
|
||||||
x3: :middle, y3: :middle
|
x3: :middle, y3: :middle
|
||||||
|
|
||||||
# We can also do width-, height-, width/, height/
|
# We can also do width-, height-, width/, height/
|
||||||
rect x: 20, y: 5, stroke_color: :green,
|
rect x: 20, y: 5, stroke_color: :green,
|
||||||
width: 'width - 0.1in', height: 10
|
width: 'width - 0.1in', height: 10
|
||||||
|
|
||||||
rect x: 10, y: 50, width: 10, height: 'height / 3',
|
rect x: 10, y: 50, width: 10, height: 'height / 3',
|
||||||
stroke_color: :purple
|
stroke_color: :purple
|
||||||
|
|
||||||
# Layouts apply this too.
|
# Layouts apply this too.
|
||||||
use_layout file: 'shorthands.yml'
|
use_layout file: 'shorthands.yml'
|
||||||
rect layout: :example
|
rect layout: :example
|
||||||
# The x and y coordinates can also be "centered", assuming the
|
# The x and y coordinates can also be "centered", assuming the
|
||||||
|
|
||||||
# HOWEVER! Shorthands don't combine in an "extends" situation,
|
# HOWEVER! Shorthands don't combine in an "extends" situation,
|
||||||
# e.g. this won't work:
|
# e.g. this won't work:
|
||||||
# parent:
|
# parent:
|
||||||
# x: middle
|
# x: middle
|
||||||
|
|
@ -37,5 +38,10 @@ Squib::Deck.new(width: '0.5in', height: '0.25in') do
|
||||||
# extends: parent
|
# extends: parent
|
||||||
# x: += 0.5in
|
# x: += 0.5in
|
||||||
|
|
||||||
|
# These shorthands are not intended for every xywh parameter or length parameter
|
||||||
|
# e.g. this won't work
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
save_png prefix: 'shorthand_'
|
save_png prefix: 'shorthand_'
|
||||||
end
|
end
|
||||||
|
|
@ -34,4 +34,6 @@ Squib::Deck.new(width: '1.5in', height: '1.5in') do
|
||||||
svg file: '../spanner.svg', layout: :angled
|
svg file: '../spanner.svg', layout: :angled
|
||||||
|
|
||||||
save prefix: 'units_', format: :png
|
save prefix: 'units_', format: :png
|
||||||
|
|
||||||
|
# But wait... there's more! See _shorthands.rb for more fanciness with units
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
example:
|
||||||
|
x: 2c
|
||||||
|
y: middle + 0.25 cells
|
||||||
|
width: 2 cell
|
||||||
|
height: 0.75cell
|
||||||
|
fill_color: cyan
|
||||||
|
|
||||||
|
extends_example:
|
||||||
|
extends: example
|
||||||
|
x: += 2c
|
||||||
|
height: -= 0.25c
|
||||||
|
fill_color: yellow
|
||||||
|
|
||||||
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Loading…
Reference in New Issue