Tons of docs, xlsx can be used outside Deck now

This commit is contained in:
Andy Meneely
2014-07-29 00:16:47 -04:00
parent ec0adf0345
commit 8b12344c7b
10 changed files with 156 additions and 89 deletions
+5 -4
View File
@@ -1,11 +1,12 @@
module Squib
class Deck
# Fills the background with the given color
# @example
# background color: :white
#
# @params opts: the parameter hash.
# @option range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option color: the color the font will render to. See {file:API.md#label-Specifying+Colors Specifying Colors}
# @return [nil]
# @option range [Enumerable] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option color [String] (:black) the color the font will render to. See {file:API.md#label-Specifying+Colors Specifying Colors}
# @return [nil] nothing
# @api public
def background(opts = {})
opts = needs(opts,[:range, :color])
+43 -24
View File
@@ -1,32 +1,51 @@
require 'roo'
module Squib
class Deck
# Convenience method for pulling Excel data from `.xlsx` files
# Pulls the data into a Hash of arrays based on the columns. First row is assumed to be the header row.
# See the example at {file:samples/excel.rb samples/excel.rb}. The accompanying Excel file is in the [source repository](https://github.com/andymeneely/squib/tree/master/samples)
#
# @param file: [String] the file to open. Must end in `.xlsx`. Opens relative to the current directory.
# @param sheet: [Integer] The zero-based index of the sheet from which to read.
# @api public
# Pulls Excel data from `.xlsx` files into a column-based hash
#
# Pulls the data into a Hash of arrays based on the columns. First row is assumed to be the header row.
# See the example `samples/excel.rb` in the [source repository](https://github.com/andymeneely/squib/tree/master/samples)
#
# @example
# # Excel file looks like this:
# # | h1 | h2 |
# # ------------
# # | 1 | 2 |
# # | 3 | 4 |
# data = xlsx file: 'data.xlsx', sheet: 0
# {'h1' => [1,3], 'h2' => [2,4]}
#
# @option opts file [String] the file to open. Must end in `.xlsx`. Opens relative to the current directory.
# @option opts sheet [Integer] (0) The zero-based index of the sheet from which to read.
# @return [Hash] a hash of arrays based on columns in the spreadsheet
# @api public
def xlsx(opts = {})
opts = Squib::SYSTEM_DEFAULTS.merge(opts)
opts = Squib::InputHelpers.fileify(opts)
s = Roo::Excelx.new(opts[:file])
s.default_sheet = s.sheets[opts[:sheet]]
data = {}
s.first_column.upto(s.last_column) do |col|
header = s.cell(s.first_row,col).to_s
data[header] = []
(s.first_row+1).upto(s.last_row) do |row|
cell = s.cell(row,col)
# Roo hack for avoiding unnecessary .0's on whole integers
cell = s.excelx_value(row,col) if s.excelx_type(row,col) == [:numeric_or_formula, "General"]
data[header] << cell
end#row
end#col
data
end#xlsx
module_function :xlsx
class Deck
# Convenience call for Squib.xlsx
def xlsx(opts = {})
opts = needs(opts, [:file, :sheet])
s = Roo::Excelx.new(opts[:file])
s.default_sheet = s.sheets[opts[:sheet]]
data = {}
s.first_column.upto(s.last_column) do |col|
header = s.cell(s.first_row,col).to_s
data[header] = []
(s.first_row+1).upto(s.last_row) do |row|
cell = s.cell(row,col)
# Roo hack for avoiding unnecessary .0's on whole integers
cell = s.excelx_value(row,col) if s.excelx_type(row,col) == [:numeric_or_formula, "General"]
data[header] << cell
end#row
end#col
data
end#xlsx
Squib.xlsx(opts)
end
end
end
+20 -18
View File
@@ -2,15 +2,18 @@ module Squib
class Deck
# Renders a png file at the given location.
# See {file:samples/image.rb samples/image.rb} and {file:samples/tgc-overlay.rb samples/tgc-overlay.rb} as examples.
# Note: scaling not currently supported for PNGs.
#
# @param range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @param file: file(s) to read in. If it's a single file, then it's use for every card. If the parameter is an Array of files, then each file is looked up for each card. See {file:API.md#Specifying+Files Specifying Files}
# @param x: the x-coordinate to place
# @param y: the y-coordinate to place
# @param alpha: the alpha-transparency percentage used to blend this image
# @return [nil] intended to be void
# See {file:samples/image.rb samples/image.rb} and {file:samples/tgc-overlay.rb samples/tgc-overlay.rb} as examples.
# Note: scaling not currently supported for PNGs.
# @example
# png file: 'img.png', x: 50, y: 50
#
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. See {file:API.md#Specifying+Files Specifying Files}
# @option opts x [Integer] (0) the x-coordinate to place
# @option opts y [Integer] (0) the y-coordinate to place
# @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image
# @return [nil] Returns nil
# @api public
def png(opts = {})
opts = needs(opts, [:range, :files, :x, :y, :alpha])
@@ -20,19 +23,18 @@ module Squib
end
# Renders an entire svg file at the given location. Uses the SVG-specified units and DPI to determine the pixel width and height.
#
# See {file:samples/load-images.rb samples/load-images.rb} and {file:samples/tgc-overlay.rb samples/tgc-overlay.rb} as examples.
# @example
# svg 1..2, 'icon.svg', '#stone', x: 50, y:50
#
# See {file:samples/load-images.rb samples/load-images.rb} and {file:samples/tgc-overlay.rb samples/tgc-overlay.rb} as examples.
#
# @param range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @param file: the svg file to render. See {file:API.md#Specifying+Files Specifying Files}
# @param id: if specified, only render the SVG element with the supplied id. Otherwise, render the entire SVG file
# @param x: the x-coordinate to place
# @param y: the y-coordinate to place
# @param width: the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document.
# @param height: the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document.
# @return [nil] essentially a void method
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. See {file:API.md#Specifying+Files Specifying Files}
# @option opts x [Integer] (0) the x-coordinate to place
# @option opts y [Integer] (0) the y-coordinate to place
# @option opts width [Integer] (:native) the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document.
# @option opts height [Integer] (:native) the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document.
# @return [nil] Returns nil
# @api public
def svg(opts = {})
p = needs(opts,[:range, :files, :svgid, :x, :y, :width, :height])
+6 -1
View File
@@ -16,11 +16,16 @@ module Squib
self
end
# Saves the given range of cards to PNG
# Saves the given range of cards to a PNG
#
# @example
# save range: 1..8, dir: '_pnp', prefix: 'bw_'
#
# @option opts [Enumerable] range (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option opts [String] dir (_output) the directory for the output to be sent to. Will be created if it doesn't exist.
# @option opts [String] prefix (card_) the prefix of the file name to be printed.
# @return [nil] Returns nothing
# @api public
def save_png(opts = {})
opts = needs(opts,[:range, :creatable_dir, :prefix])
opts[:range].each do |i|
+7 -5
View File
@@ -2,10 +2,13 @@ module Squib
class Deck
# Toggle hints globally.
# Text hints are rectangles around where the text will be laid out. They are intended to be temporary.
# Setting a hint to nil or to :off will disable hints. @see samples/text.rb
#
# @param [Color] text the color of the text hint. To turn off use nil or :off. @see API.md
# Text hints are rectangles around where the text will be laid out. They are intended to be temporary.
# Setting a hint to nil or to :off will disable hints. @see samples/text.rb
# @example
# hint text:cyan
#
# @param [String] text the color of the text hint. To turn off use nil or :off. @see API.md
# @return [nil] Returns nothing
# @api public
def hint(text: nil)
@@ -20,8 +23,7 @@ module Squib
# text 'blah24', font: 'Arial 24' # in Arial 24
# set font: :default # Back to Squib-wide default
#
# @param opts: the hash of options.
# @option font: the font string to set as default. Can also be set to `:default` to use the Squib-wide default.
# @option opts font: the font string to set as default. Can also be set to `:default` to use the Squib-wide default.
# @return [nil] Returns nothing
# @api public
def set(opts = {})
+14 -11
View File
@@ -2,18 +2,21 @@ module Squib
class Deck
# Draw a rounded rectangle
#
# @example
# rect x: 0, y: 0, width: 825, height: 1125, radius: 25
#
# @param opts: the hash of options.
# @option x: the x-coordinate to place
# @option y: the y-coordinate to place
# @option width: the width of the rectangle.
# @option height: the height of the rectangle.
# @option x_radius: the radius of the rounded corner horiztonally. Zero is a non-rounded corner.
# @option y_radius: the radius of the rounded corner vertically. Zero is a non-rounded corner.
# @option radius: when set, overrides both x_radius and y_radius
# @option fill_color: the color with which to fill the rectangle
# @option stroke_color: the color with which to stroke the outside of the rectangle
# @option stroke_width: the width of the outside stroke
# @option opts x [Integer] (0) the x-coordinate to place
# @option opts y [Integer] (0) the y-coordinate to place
# @option opts width [Integer] the width of the rectangle.
# @option opts height [Integer] the height of the rectangle.
# @option opts x_radius [Integer] (0) the radius of the rounded corner horiztonally. Zero is a non-rounded corner.
# @option opts y_radius [Integer] (0) the radius of the rounded corner vertically. Zero is a non-rounded corner.
# @option opts radius [Integer] (nil) when set, overrides both x_radius and y_radius
# @option opts fill_color [String] ('#0000') the color with which to fill the rectangle
# @option opts stroke_color [String] (:black) the color with which to stroke the outside of the rectangle
# @option opts stroke_width [Decimal] (2.0) the width of the outside stroke
# @return [nil] intended to be void
# @api public
def rect(opts = {})
opts = needs(opts, [:range, :x, :y, :width, :height, :radius,
+21 -18
View File
@@ -2,31 +2,34 @@ module Squib
class Deck
# Renders a string at a given location, width, alignment, font, etc.
# Unix-like newlines are interpreted even on Windows.
#
# @example See the {file:samples/text-options.rb samples/text.rb} for a lengthy example.
# Unix-like newlines are interpreted even on Windows.
# See the {file:samples/text-options.rb samples/text.rb} for a lengthy example.
#
# @example
# text str: 'hello'
# text str: 'hello', x: 50, y:50, align: center
#
# @param range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @param str: the string to be rendered. Must support `#to_s`. If the card responds to `#each`, it's mapped out one at a time across the cards.
# @param font: the Font description string, including family, styles, and size.
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option opts str [String, Array] ('') the string to be rendered. Must support `#to_s`. If the card responds to `#each`, it's mapped out one at a time across the cards.
# @option opts font [String] (Arial 36 or whatever was set with `set`) the Font description string, including family, styles, and size.
# (e.g. `'Arial bold italic 12'`)
# For the official documentation, see the [Pango docs](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Pango%3A%3AFontDescription#style).
# This [description](http://www.pygtk.org/pygtk2reference/class-pangofontdescription.html) is also quite good.
# See the {file:samples/text-options.rb samples/text.rb} as well.
# @param x: the x-coordinate to place
# @param y: the y-coordinate to place
# @param options: captures the additional input options below
# @option color: (default: :black) the color the font will render to. See {file:API.md#label-Specifying+Colors Specifying Colors}
# @option markup: [Boolean] (default: false) Enable markup parsing of `str` using the HTML-like Pango Markup syntax, defined [here](http://ruby-gnome2.sourceforge.jp/hiki.cgi?pango-markup) and [here](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html).
# @option width: the width of the box the string will be placed in. Stretches to the content by default.
# @option height: the height of the box the string will be placed in. Stretches to the content by default.
# @option wrap: When height is set, determines the behavior of how the string wraps. The `:word_char` option will break at words, but then fall back to characters when the word cannot fit. #
# @option opts x [Integer] (0) the x-coordinate to place
# @option opts y [Integer] (0) the y-coordinate to place
# @option opts color [String] (:black) the color the font will render to. See {file:API.md#label-Specifying+Colors Specifying Colors}
# @option opts markup: [Boolean] (false) Enable markup parsing of `str` using the HTML-like Pango Markup syntax, defined [here](http://ruby-gnome2.sourceforge.jp/hiki.cgi?pango-markup) and [here](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html).
# @option opts width [Integer, :native] (:native) the width of the box the string will be placed in. Stretches to the content by default.
# @option opts height [Integer, :native] the height of the box the string will be placed in. Stretches to the content by default.
# @option opts wrap [:none, :word, :char, :word_char, true, false] (:word_char) When height is set, determines the behavior of how the string wraps. The `:word_char` option will break at words, but then fall back to characters when the word cannot fit. #
# Options are `:none, :word, :char, :word_char`. Also: `true` is the same as `:word_char`, `false` is the same as `:none`. Default `:word_char`
# @option align: options `:left, :right, and :center`. Default `:left`
# @option justify: [Boolean] toggles whether or not the text is justified or not. Default `false`
# @option valign: When width and height are set, align text vertically according to the logical extents of the text. Options are `:top, :middle, :bottom`. Default `:top`
# @option ellipsize: When width and height are set, determines the behavior of overflowing text. Options are `:none, :start, :middle, :end`. Also: `true` maps to `:end` and `false` maps to `:none`. Default `:end`
# @option hint: show a text hint with the given color. Overrides global hints (see {Deck#hint}).
# @option opts align [:left, right, :center] (:left) The alignment of the text
# @option opts justify [Boolean] (false) toggles whether or not the text is justified or not.
# @option opts valign [:top, :middle, :bottom] (:top) When width and height are set, align text vertically according to the logical extents of the text.
# @option opts ellipsize [:none, :start, :middle, :end, true, false] (:end) When width and height are set, determines the behavior of overflowing text. Also: `true` maps to `:end` and `false` maps to `:none`. Default `:end`
# @option opts hint [String] (:nil) draw a rectangle around the text with the given color. Overrides global hints (see {Deck#hint}).
# @return [nil] Returns nothing
# @api public
def text(opts = {})