Skip to content

Commit

Permalink
Added "rotate" field in template file schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixleong committed Apr 19, 2017
1 parent 56322d4 commit 178ec5b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
17 changes: 10 additions & 7 deletions lib/squib/graphics/save_templated_sheet.rb
Expand Up @@ -13,7 +13,8 @@ def render_sheet(range, sheet)
cc.scale(72.0 / @deck.dpi, 72.0 / @deck.dpi) # make it like pixels
card_set = @tmpl.cards
per_sheet = card_set.size
angle = detect_card_orientation
default_angle = (
(@tmpl.rotate == 0)? check_card_orientation: @tmpl.rotate)

if range.size
draw_overlay_below_cards cc
Expand All @@ -24,10 +25,12 @@ def render_sheet(range, sheet)
next_page_if_needed(cc, i, per_sheet)

card = @deck.cards[i]
x = card_set[i % per_sheet]['x']
y = card_set[i % per_sheet]['y']
slot = card_set[i % per_sheet]
x = slot['x']
y = slot['y']
angle = (slot['rotate'] != 0)? slot['rotate']: default_angle

if angle
if angle != 0
draw_rotated_card cc, card, x, y, angle
else
cc.set_source card.cairo_surface, x, y
Expand Down Expand Up @@ -114,13 +117,13 @@ def draw_crop_line(cc)
}
end

def detect_card_orientation
def check_card_orientation
clockwise = 1.5 * Math::PI
# Simple detection
if (
@deck.width == @tmpl.card_width and
@deck.height == @tmpl.card_height)
return false
return 0
elsif (
@deck.width == @tmpl.card_height and
@deck.height == @tmpl.card_width)
Expand All @@ -137,7 +140,7 @@ def detect_card_orientation
if is_tmpl_card_landscape == is_deck_card_landscape
clockwise
else
false
0
end
end

Expand Down
33 changes: 32 additions & 1 deletion lib/squib/template_parser.rb
Expand Up @@ -48,6 +48,7 @@ class Template
'card_height' => '88mm',
'dpi' => 300,
'position_reference' => :topleft,
'rotate' => 0.0,
'crop_line' => {
'style' => :solid,
'width' => '0.02mm',
Expand Down Expand Up @@ -145,6 +146,10 @@ def margin
}
end

def rotate
parse_rotate_param @template_hash['rotate']
end

private

# Template file schema
Expand All @@ -155,6 +160,9 @@ def margin
"card_width" => UNIT_REGEX,
"card_height" => UNIT_REGEX,
"position_reference" => ClassyHash::G.enum(:topleft, :center),
"rotate" => [
:optional, Numeric,
ClassyHash::G.enum(:clockwise, :counterclockwise, :turnaround)],
"crop_line" => {
"style" => [
ClassyHash::G.enum(:solid, :dotted, :dashed),
Expand All @@ -173,7 +181,15 @@ def margin
"color" => [:optional, String, Symbol],
}]]
},
"cards" => [[{ "x" => UNIT_REGEX, "y" => UNIT_REGEX }]]
"cards" => [[{
"x" => UNIT_REGEX,
"y" => UNIT_REGEX,
# NOTE: Don't think that we should specify rotation on a per card
# basis, but just included here for now
"rotate" => [
:optional, Numeric,
ClassyHash::G.enum(:clockwise, :counterclockwise, :turnaround)],
}]]
}

# Return path for built-in sheet templates
Expand Down Expand Up @@ -216,8 +232,23 @@ def parse_card(card)

new_card["x"] = x
new_card["y"] = y
new_card["rotate"] = parse_rotate_param card["rotate"]
new_card
end

def parse_rotate_param val
if val == :clockwise
0.5 * Math::PI
elsif val == :counterclockwise
1.5 * Math::PI
elsif val == :turnaround
Math::PI
elsif val.nil?
0
else
val
end
end
end


Expand Down

0 comments on commit 178ec5b

Please sign in to comment.