Class: Writexlsx::Shape
- Inherits:
-
Object
- Object
- Writexlsx::Shape
- Includes:
- Utility
- Defined in:
- lib/write_xlsx/shape.rb
Overview
Shape - A class for writing Excel shapes.
Used in conjunction with WriteXLSX.
Copyright 2000-2012, John McNamara, [email protected] Converted to ruby by Hideo NAKAMURA, [email protected]
Constant Summary
Constants included from Utility
Utility::COL_MAX, Utility::ROW_MAX, Utility::SHEETNAME_MAX, Utility::STR_MAX
Instance Attribute Summary collapse
-
#adjustments ⇒ Object
Returns the value of attribute adjustments.
-
#align ⇒ Object
readonly
Returns the value of attribute align.
-
#column_end ⇒ Object
Returns the value of attribute column_end.
-
#column_start ⇒ Object
Returns the value of attribute column_start.
-
#connect ⇒ Object
Returns the value of attribute connect.
-
#drawing ⇒ Object
readonly
Returns the value of attribute drawing.
-
#edit_as ⇒ Object
readonly
Returns the value of attribute edit_as.
-
#element ⇒ Object
Returns the value of attribute element.
-
#end ⇒ Object
Returns the value of attribute end.
-
#end_index ⇒ Object
Returns the value of attribute end_index.
-
#end_side ⇒ Object
Returns the value of attribute end_side.
-
#fill ⇒ Object
readonly
Returns the value of attribute fill.
-
#flip_h ⇒ Object
Returns the value of attribute flip_h.
-
#flip_v ⇒ Object
Returns the value of attribute flip_v.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#height ⇒ Object
Returns the value of attribute height.
-
#height_emu ⇒ Object
Returns the value of attribute height_emu.
-
#id ⇒ Object
Returns the value of attribute id.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#line_type ⇒ Object
Returns the value of attribute line_type.
-
#line_weight ⇒ Object
Returns the value of attribute line_weight.
-
#name ⇒ Object
Returns the value of attribute name.
-
#palette ⇒ Object
Returns the value of attribute palette.
-
#rotation ⇒ Object
Returns the value of attribute rotation.
-
#row_end ⇒ Object
Returns the value of attribute row_end.
-
#row_start ⇒ Object
Returns the value of attribute row_start.
-
#scale_x ⇒ Object
Returns the value of attribute scale_x.
-
#scale_y ⇒ Object
Returns the value of attribute scale_y.
-
#start ⇒ Object
Returns the value of attribute start.
-
#start_index ⇒ Object
Returns the value of attribute start_index.
-
#start_side ⇒ Object
Returns the value of attribute start_side.
-
#stencil ⇒ Object
Returns the value of attribute stencil.
-
#text ⇒ Object
Returns the value of attribute text.
-
#tx_box ⇒ Object
readonly
Returns the value of attribute tx_box.
-
#type ⇒ Object
Returns the value of attribute type.
-
#valign ⇒ Object
readonly
Returns the value of attribute valign.
-
#width ⇒ Object
Returns the value of attribute width.
-
#width_emu ⇒ Object
Returns the value of attribute width_emu.
-
#x1 ⇒ Object
Returns the value of attribute x1.
-
#x2 ⇒ Object
Returns the value of attribute x2.
-
#x_abs ⇒ Object
Returns the value of attribute x_abs.
-
#x_offset ⇒ Object
Returns the value of attribute x_offset.
-
#y1 ⇒ Object
Returns the value of attribute y1.
-
#y2 ⇒ Object
Returns the value of attribute y2.
-
#y_abs ⇒ Object
Returns the value of attribute y_abs.
-
#y_offset ⇒ Object
Returns the value of attribute y_offset.
Instance Method Summary collapse
-
#auto_locate_connectors(shapes, shape_hash) ⇒ Object
Re-size connector shapes if they are connected to other shapes.
-
#calc_position_emus(worksheet) ⇒ Object
Calculate the vertices that define the position of a shape object within the worksheet in EMUs.
- #dimensions ⇒ Object
-
#initialize(properties = {}) ⇒ Shape
constructor
A new instance of Shape.
- #set_position(row_start, column_start, x_offset, y_offset, x_scale, y_scale) ⇒ Object
- #set_properties(properties) ⇒ Object
-
#validate(index) ⇒ Object
Check shape attributes to ensure they are valid.
Methods included from Utility
#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #convert_date_time, #dash_types, delete_files, #fill_properties, #float_to_str, #layout_properties, #line_fill_properties, #line_properties, #palette_color, #pixels_to_points, #ptrue?, #put_deprecate_message, #r_id_attributes, #row_col_notation, #shape_style_base, #store_col_max_min_values, #store_row_max_min_values, #substitute_cellref, #underline_attributes, #v_shape_attributes_base, #v_shape_style_base, #value_or_raise, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xml_str
Constructor Details
#initialize(properties = {}) ⇒ Shape
Returns a new instance of Shape.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/write_xlsx/shape.rb', line 27 def initialize(properties = {}) @writer = Package::XMLWriterSimple.new @name = nil @type = 'rect' # Is a Connector shape. 1/0 Value is a hash lookup from type. @connect = 0 # Is a Drawing. Always 0, since a single shape never fills an entire sheet. @drawing = 0 # OneCell or Absolute: options to move and/or size with cells. @edit_as = nil # Auto-incremented, unless supplied by user. @id = 0 # Shape text (usually centered on shape geometry). @text = 0 # Shape stencil mode. A copy (child) is created when inserted. # The link to parent is broken. @stencil = 1 # Index to _shapes array when inserted. @element = -1 # Shape ID of starting connection, if any. @start = nil # Shape vertex, starts at 0, numbered clockwise from 12 o'clock. @start_index = nil @end = nil @end_index = nil # Number and size of adjustments for shapes (usually connectors). @adjustments = [] # Start and end sides. t)op, b)ottom, l)eft, or r)ight. @start_side = '' @end_side = '' # Flip shape Horizontally. eg. arrow left to arrow right. @flip_h = 0 # Flip shape Vertically. eg. up arrow to down arrow. @flip_v = 0 # shape rotation (in degrees 0-360). @rotation = 0 # An alternate way to create a text box, because Excel allows it. # It is just a rectangle with text. @tx_box = false # Shape outline colour, or 0 for noFill (default black). @line = '000000' # Line type: dash, sysDot, dashDot, lgDash, lgDashDot, lgDashDotDot. @line_type = '' # Line weight (integer). @line_weight = 1 # Shape fill colour, or 0 for noFill (default noFill). @fill = 0 # Formatting for shape text, if any. @format = {} # copy of colour palette table from Workbook.pm. @palette = [] # Vertical alignment: t, ctr, b. @valign = 'ctr' # Alignment: l, ctr, r, just @align = 'ctr' @x_offset = 0 @y_offset = 0 # Scale factors, which also may be set when the shape is inserted. @scale_x = 1 @scale_y = 1 # Default size, which can be modified and/or scaled. @width = 50 @height = 50 # Initial assignment. May be modified when prepared. @column_start = 0 @row_start = 0 @x1 = 0 @y1 = 0 @column_end = 0 @row_end = 0 @x2 = 0 @y2 = 0 @x_abs = 0 @y_abs = 0 set_properties(properties) end |
Instance Attribute Details
#adjustments ⇒ Object
Returns the value of attribute adjustments.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def adjustments @adjustments end |
#align ⇒ Object (readonly)
Returns the value of attribute align.
18 19 20 |
# File 'lib/write_xlsx/shape.rb', line 18 def align @align end |
#column_end ⇒ Object
Returns the value of attribute column_end.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def column_end @column_end end |
#column_start ⇒ Object
Returns the value of attribute column_start.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def column_start @column_start end |
#connect ⇒ Object
Returns the value of attribute connect.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def connect @connect end |
#drawing ⇒ Object (readonly)
Returns the value of attribute drawing.
16 17 18 |
# File 'lib/write_xlsx/shape.rb', line 16 def drawing @drawing end |
#edit_as ⇒ Object (readonly)
Returns the value of attribute edit_as.
16 17 18 |
# File 'lib/write_xlsx/shape.rb', line 16 def edit_as @edit_as end |
#element ⇒ Object
Returns the value of attribute element.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def element @element end |
#end ⇒ Object
Returns the value of attribute end.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def end @end end |
#end_index ⇒ Object
Returns the value of attribute end_index.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def end_index @end_index end |
#end_side ⇒ Object
Returns the value of attribute end_side.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def end_side @end_side end |
#fill ⇒ Object (readonly)
Returns the value of attribute fill.
17 18 19 |
# File 'lib/write_xlsx/shape.rb', line 17 def fill @fill end |
#flip_h ⇒ Object
Returns the value of attribute flip_h.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def flip_h @flip_h end |
#flip_v ⇒ Object
Returns the value of attribute flip_v.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def flip_v @flip_v end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
17 18 19 |
# File 'lib/write_xlsx/shape.rb', line 17 def format @format end |
#height ⇒ Object
Returns the value of attribute height.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def height @height end |
#height_emu ⇒ Object
Returns the value of attribute height_emu.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def height_emu @height_emu end |
#id ⇒ Object
Returns the value of attribute id.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def id @id end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
17 18 19 |
# File 'lib/write_xlsx/shape.rb', line 17 def line @line end |
#line_type ⇒ Object
Returns the value of attribute line_type.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def line_type @line_type end |
#line_weight ⇒ Object
Returns the value of attribute line_weight.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def line_weight @line_weight end |
#name ⇒ Object
Returns the value of attribute name.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def name @name end |
#palette ⇒ Object
Returns the value of attribute palette.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def palette @palette end |
#rotation ⇒ Object
Returns the value of attribute rotation.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def rotation @rotation end |
#row_end ⇒ Object
Returns the value of attribute row_end.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def row_end @row_end end |
#row_start ⇒ Object
Returns the value of attribute row_start.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def row_start @row_start end |
#scale_x ⇒ Object
Returns the value of attribute scale_x.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def scale_x @scale_x end |
#scale_y ⇒ Object
Returns the value of attribute scale_y.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def scale_y @scale_y end |
#start ⇒ Object
Returns the value of attribute start.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def start @start end |
#start_index ⇒ Object
Returns the value of attribute start_index.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def start_index @start_index end |
#start_side ⇒ Object
Returns the value of attribute start_side.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def start_side @start_side end |
#stencil ⇒ Object
Returns the value of attribute stencil.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def stencil @stencil end |
#text ⇒ Object
Returns the value of attribute text.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def text @text end |
#tx_box ⇒ Object (readonly)
Returns the value of attribute tx_box.
17 18 19 |
# File 'lib/write_xlsx/shape.rb', line 17 def tx_box @tx_box end |
#type ⇒ Object
Returns the value of attribute type.
16 17 18 |
# File 'lib/write_xlsx/shape.rb', line 16 def type @type end |
#valign ⇒ Object (readonly)
Returns the value of attribute valign.
18 19 20 |
# File 'lib/write_xlsx/shape.rb', line 18 def valign @valign end |
#width ⇒ Object
Returns the value of attribute width.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def width @width end |
#width_emu ⇒ Object
Returns the value of attribute width_emu.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def width_emu @width_emu end |
#x1 ⇒ Object
Returns the value of attribute x1.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def x1 @x1 end |
#x2 ⇒ Object
Returns the value of attribute x2.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def x2 @x2 end |
#x_abs ⇒ Object
Returns the value of attribute x_abs.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def x_abs @x_abs end |
#x_offset ⇒ Object
Returns the value of attribute x_offset.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def x_offset @x_offset end |
#y1 ⇒ Object
Returns the value of attribute y1.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def y1 @y1 end |
#y2 ⇒ Object
Returns the value of attribute y2.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def y2 @y2 end |
#y_abs ⇒ Object
Returns the value of attribute y_abs.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def y_abs @y_abs end |
#y_offset ⇒ Object
Returns the value of attribute y_offset.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def y_offset @y_offset end |
Instance Method Details
#auto_locate_connectors(shapes, shape_hash) ⇒ Object
Re-size connector shapes if they are connected to other shapes.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/write_xlsx/shape.rb', line 201 def auto_locate_connectors(shapes, shape_hash) # Valid connector shapes. connector_shapes = { :straightConnector => 1, :Connector => 1, :bentConnector => 1, :curvedConnector => 1, :line => 1 } shape_base = @type.chop.to_sym # Remove the number of segments from end of type. @connect = connector_shapes[shape_base] ? 1 : 0 return if @connect == 0 # Both ends have to be connected to size it. return if @start == 0 && @end == 0 # Both ends need to provide info about where to connect. return if @start_side == 0 && @end_side == 0 sid = @start eid = @end slink_id = shape_hash[sid] || 0 sls = shapes.fetch(slink_id, Shape.new) elink_id = shape_hash[eid] || 0 els = shapes.fetch(elink_id, Shape.new) # Assume shape connections are to the middle of an object, and # not a corner (for now). connect_type = @start_side + @end_side smidx = sls.x_offset + sls.width / 2 emidx = els.x_offset + els.width / 2 smidy = sls.y_offset + sls.height / 2 emidy = els.y_offset + els.height / 2 netx = (smidx - emidx).abs nety = (smidy - emidy).abs if connect_type == 'bt' sy = sls.y_offset + sls.height ey = els.y_offset @width = (emidx - smidx).to_i.abs @x_offset = [smidx, emidx].min.to_i @height = (els.y_offset - (sls.y_offset + sls.height)).to_i.abs @y_offset = [sls.y_offset + sls.height, els.y_offset].min.to_i @flip_h = smidx < emidx ? 1 : 0 @rotation = 90 if sy > ey @flip_v = 1 # Create 3 adjustments for an end shape vertically above a # start @ Adjustments count from the upper left object. if @adjustments.empty? @adjustments = [-10, 50, 110] end @type = 'bentConnector5' end elsif connect_type == 'rl' @width = (els.x_offset - (sls.x_offset + sls.width)).to_i.abs @height = (emidy - smidy).to_i.abs @x_offset = [sls.x_offset + sls.width, els.x_offset].min @y_offset = [smidy, emidy].min @flip_h = 1 if smidx < emidx && smidy > emidy @flip_h = 1 if smidx > emidx && smidy < emidy if smidx > emidx # Create 3 adjustments for an end shape to the left of a # start @ if @adjustments.empty? @adjustments = [-10, 50, 110] end @type = 'bentConnector5' end end end |
#calc_position_emus(worksheet) ⇒ Object
Calculate the vertices that define the position of a shape object within the worksheet in EMUs. Save the vertices with the object.
The vertices are expressed as English Metric Units (EMUs). There are 12,700 EMUs per point. Therefore, 12,700 * 3 /4 = 9,525 EMUs per pixel.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/write_xlsx/shape.rb', line 156 def calc_position_emus(worksheet) c_start, r_start, xx1, yy1, c_end, r_end, xx2, yy2, x_abslt, y_abslt = worksheet.position_object_pixels( @column_start, @row_start, @x_offset, @y_offset, @width * @scale_x, @height * @scale_y ) # Now that x2/y2 have been calculated with a potentially negative # width/height we use the absolute value and convert to EMUs. @width_emu = (@width * 9_525).abs.to_i @height_emu = (@height * 9_525).abs.to_i @column_start = c_start.to_i @row_start = r_start.to_i @column_end = c_end.to_i @row_end = r_end.to_i # Convert the pixel values to EMUs. See above. @x1 = (xx1 * 9_525).to_i @y1 = (yy1 * 9_525).to_i @x2 = (xx2 * 9_525).to_i @y2 = (yy2 * 9_525).to_i @x_abs = (x_abslt * 9_525).to_i @y_abs = (y_abslt * 9_525).to_i end |
#dimensions ⇒ Object
297 298 299 300 301 302 303 304 305 306 |
# File 'lib/write_xlsx/shape.rb', line 297 def dimensions [ @column_start, @row_start, @x1, @y1, @column_end, @row_end, @x2, @y2, @x_abs, @y_abs, @width_emu, @height_emu ] end |
#set_position(row_start, column_start, x_offset, y_offset, x_scale, y_scale) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/write_xlsx/shape.rb', line 186 def set_position(row_start, column_start, x_offset, y_offset, x_scale, y_scale) @row_start = row_start @column_start = column_start @x_offset = x_offset || 0 @y_offset = y_offset || 0 # Override shape scale if supplied as an argument. Otherwise, use the # existing shape scale factors. @scale_x = x_scale if x_scale @scale_y = y_scale if y_scale end |
#set_properties(properties) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/write_xlsx/shape.rb', line 133 def set_properties(properties) # Override default properties with passed arguments properties.each do |key, value| # Strip leading "-" from Tk style properties e.g. -color => 'red'. k = key.to_s.sub(/^-/, '') self.instance_variable_set("@#{key}", value) end end |
#validate(index) ⇒ Object
Check shape attributes to ensure they are valid.
287 288 289 290 291 292 293 294 295 |
# File 'lib/write_xlsx/shape.rb', line 287 def validate(index) unless %w[l ctr r just].include?(@align) raise "Shape #{index} (#{@type}) alignment (#{@align}) not in ['l', 'ctr', 'r', 'just']\n" end unless %w[t ctr b].include?(@valign) raise "Shape #{index} (#{@type}) vertical alignment (#{@valign}) not in ['t', 'ctr', 'v']\n" end end |