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::CHAR_WIDTHS, Utility::COL_MAX, Utility::PERL_TRUE_VALUES, 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.
-
#anchor ⇒ Object
readonly
Returns the value of attribute anchor.
-
#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, anchor) ⇒ 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, #color, #convert_date_time, #convert_font_args, #dash_types, delete_files, #escape_url, #fill_properties, #float_to_str, #get_font_latin_attributes, #get_font_style_attributes, #get_image_properties, #layout_properties, #legend_properties, #line_fill_properties, #line_properties, #palette_color, #params_to_font, #pattern_properties, #pixels_to_points, #process_bmp, #process_gif, #process_jpg, #process_png, #process_workbook_options, #ptrue?, #put_deprecate_message, #quote_sheetname, #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_a_body_pr, #write_a_def_rpr, #write_a_end_para_rpr, #write_a_lst_style, #write_a_p_formula, #write_a_p_pr_formula, #write_a_solid_fill, #write_a_srgb_clr, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_def_rpr_r_pr_common, #write_div, #write_fill, #write_font, #write_stroke, #write_tx_pr, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xl_string_pixel_width, #xml_str
Constructor Details
#initialize(properties = {}) ⇒ Shape
Returns a new instance of Shape.
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 132 |
# File 'lib/write_xlsx/shape.rb', line 28 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.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def adjustments @adjustments end |
#align ⇒ Object (readonly)
Returns the value of attribute align.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def align @align end |
#anchor ⇒ Object (readonly)
Returns the value of attribute anchor.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def anchor @anchor end |
#column_end ⇒ Object
Returns the value of attribute column_end.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def column_end @column_end end |
#column_start ⇒ Object
Returns the value of attribute column_start.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def column_start @column_start end |
#connect ⇒ Object
Returns the value of attribute connect.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def connect @connect end |
#drawing ⇒ Object (readonly)
Returns the value of attribute drawing.
17 18 19 |
# File 'lib/write_xlsx/shape.rb', line 17 def drawing @drawing end |
#edit_as ⇒ Object (readonly)
Returns the value of attribute edit_as.
17 18 19 |
# File 'lib/write_xlsx/shape.rb', line 17 def edit_as @edit_as end |
#element ⇒ Object
Returns the value of attribute element.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def element @element end |
#end ⇒ Object
Returns the value of attribute end.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def end @end end |
#end_index ⇒ Object
Returns the value of attribute end_index.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def end_index @end_index end |
#end_side ⇒ Object
Returns the value of attribute end_side.
26 27 28 |
# File 'lib/write_xlsx/shape.rb', line 26 def end_side @end_side end |
#fill ⇒ Object (readonly)
Returns the value of attribute fill.
18 19 20 |
# File 'lib/write_xlsx/shape.rb', line 18 def fill @fill end |
#flip_h ⇒ Object
Returns the value of attribute flip_h.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def flip_h @flip_h end |
#flip_v ⇒ Object
Returns the value of attribute flip_v.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def flip_v @flip_v end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
18 19 20 |
# File 'lib/write_xlsx/shape.rb', line 18 def format @format end |
#height ⇒ Object
Returns the value of attribute height.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def height @height end |
#height_emu ⇒ Object
Returns the value of attribute height_emu.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def height_emu @height_emu end |
#id ⇒ Object
Returns the value of attribute id.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def id @id end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
18 19 20 |
# File 'lib/write_xlsx/shape.rb', line 18 def line @line end |
#line_type ⇒ Object
Returns the value of attribute line_type.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def line_type @line_type end |
#line_weight ⇒ Object
Returns the value of attribute line_weight.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def line_weight @line_weight end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def name @name end |
#palette ⇒ Object
Returns the value of attribute palette.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def palette @palette end |
#rotation ⇒ Object
Returns the value of attribute rotation.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def rotation @rotation end |
#row_end ⇒ Object
Returns the value of attribute row_end.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def row_end @row_end end |
#row_start ⇒ Object
Returns the value of attribute row_start.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def row_start @row_start end |
#scale_x ⇒ Object
Returns the value of attribute scale_x.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def scale_x @scale_x end |
#scale_y ⇒ Object
Returns the value of attribute scale_y.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def scale_y @scale_y end |
#start ⇒ Object
Returns the value of attribute start.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def start @start end |
#start_index ⇒ Object
Returns the value of attribute start_index.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def start_index @start_index end |
#start_side ⇒ Object
Returns the value of attribute start_side.
26 27 28 |
# File 'lib/write_xlsx/shape.rb', line 26 def start_side @start_side end |
#stencil ⇒ Object
Returns the value of attribute stencil.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def stencil @stencil end |
#text ⇒ Object
Returns the value of attribute text.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def text @text end |
#tx_box ⇒ Object (readonly)
Returns the value of attribute tx_box.
18 19 20 |
# File 'lib/write_xlsx/shape.rb', line 18 def tx_box @tx_box end |
#type ⇒ Object
Returns the value of attribute type.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def type @type end |
#valign ⇒ Object (readonly)
Returns the value of attribute valign.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def valign @valign end |
#width ⇒ Object
Returns the value of attribute width.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def width @width end |
#width_emu ⇒ Object
Returns the value of attribute width_emu.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def width_emu @width_emu end |
#x1 ⇒ Object
Returns the value of attribute x1.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def x1 @x1 end |
#x2 ⇒ Object
Returns the value of attribute x2.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def x2 @x2 end |
#x_abs ⇒ Object
Returns the value of attribute x_abs.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def x_abs @x_abs end |
#x_offset ⇒ Object
Returns the value of attribute x_offset.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def x_offset @x_offset end |
#y1 ⇒ Object
Returns the value of attribute y1.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def y1 @y1 end |
#y2 ⇒ Object
Returns the value of attribute y2.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def y2 @y2 end |
#y_abs ⇒ Object
Returns the value of attribute y_abs.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def y_abs @y_abs end |
#y_offset ⇒ Object
Returns the value of attribute y_offset.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 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.
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 |
# File 'lib/write_xlsx/shape.rb', line 204 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) 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. @adjustments = [-10, 50, 110] if @adjustments.empty? @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 @ @adjustments = [-10, 50, 110] if @adjustments.empty? @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 185 186 |
# 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
290 291 292 293 294 295 296 297 298 299 |
# File 'lib/write_xlsx/shape.rb', line 290 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, anchor) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/write_xlsx/shape.rb', line 188 def set_position(row_start, column_start, x_offset, y_offset, x_scale, y_scale, anchor) @row_start = row_start @column_start = column_start @x_offset = x_offset || 0 @y_offset = y_offset || 0 @anchor = anchor || 1 # 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
134 135 136 137 138 139 140 |
# File 'lib/write_xlsx/shape.rb', line 134 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'. instance_variable_set("@#{key}", value) end end |
#validate(index) ⇒ Object
Check shape attributes to ensure they are valid.
284 285 286 287 288 |
# File 'lib/write_xlsx/shape.rb', line 284 def validate(index) raise "Shape #{index} (#{@type}) alignment (#{@align}) not in ['l', 'ctr', 'r', 'just']\n" unless %w[l ctr r just].include?(@align) raise "Shape #{index} (#{@type}) vertical alignment (#{@valign}) not in ['t', 'ctr', 'v']\n" unless %w[t ctr b].include?(@valign) end |