Class: Writexlsx::Shape

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, #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_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


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

#adjustmentsObject

Returns the value of attribute adjustments


20
21
22
# File 'lib/write_xlsx/shape.rb', line 20

def adjustments
  @adjustments
end

#alignObject (readonly)

Returns the value of attribute align


18
19
20
# File 'lib/write_xlsx/shape.rb', line 18

def align
  @align
end

#column_endObject

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_startObject

Returns the value of attribute column_start


21
22
23
# File 'lib/write_xlsx/shape.rb', line 21

def column_start
  @column_start
end

#connectObject

Returns the value of attribute connect


19
20
21
# File 'lib/write_xlsx/shape.rb', line 19

def connect
  @connect
end

#drawingObject (readonly)

Returns the value of attribute drawing


16
17
18
# File 'lib/write_xlsx/shape.rb', line 16

def drawing
  @drawing
end

#edit_asObject (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

#elementObject

Returns the value of attribute element


24
25
26
# File 'lib/write_xlsx/shape.rb', line 24

def element
  @element
end

#endObject

Returns the value of attribute end


19
20
21
# File 'lib/write_xlsx/shape.rb', line 19

def end
  @end
end

#end_indexObject

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_sideObject

Returns the value of attribute end_side


25
26
27
# File 'lib/write_xlsx/shape.rb', line 25

def end_side
  @end_side
end

#fillObject (readonly)

Returns the value of attribute fill


17
18
19
# File 'lib/write_xlsx/shape.rb', line 17

def fill
  @fill
end

#flip_hObject

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_vObject

Returns the value of attribute flip_v


20
21
22
# File 'lib/write_xlsx/shape.rb', line 20

def flip_v
  @flip_v
end

#formatObject (readonly)

Returns the value of attribute format


17
18
19
# File 'lib/write_xlsx/shape.rb', line 17

def format
  @format
end

#heightObject

Returns the value of attribute height


23
24
25
# File 'lib/write_xlsx/shape.rb', line 23

def height
  @height
end

#height_emuObject

Returns the value of attribute height_emu


24
25
26
# File 'lib/write_xlsx/shape.rb', line 24

def height_emu
  @height_emu
end

#idObject

Returns the value of attribute id


19
20
21
# File 'lib/write_xlsx/shape.rb', line 19

def id
  @id
end

#lineObject (readonly)

Returns the value of attribute line


17
18
19
# File 'lib/write_xlsx/shape.rb', line 17

def line
  @line
end

#line_typeObject

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_weightObject

Returns the value of attribute line_weight


24
25
26
# File 'lib/write_xlsx/shape.rb', line 24

def line_weight
  @line_weight
end

#nameObject

Returns the value of attribute name


19
20
21
# File 'lib/write_xlsx/shape.rb', line 19

def name
  @name
end

#paletteObject

Returns the value of attribute palette


20
21
22
# File 'lib/write_xlsx/shape.rb', line 20

def palette
  @palette
end

#rotationObject

Returns the value of attribute rotation


19
20
21
# File 'lib/write_xlsx/shape.rb', line 19

def rotation
  @rotation
end

#row_endObject

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_startObject

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_xObject

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_yObject

Returns the value of attribute scale_y


23
24
25
# File 'lib/write_xlsx/shape.rb', line 23

def scale_y
  @scale_y
end

#startObject

Returns the value of attribute start


19
20
21
# File 'lib/write_xlsx/shape.rb', line 19

def start
  @start
end

#start_indexObject

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_sideObject

Returns the value of attribute start_side


25
26
27
# File 'lib/write_xlsx/shape.rb', line 25

def start_side
  @start_side
end

#stencilObject

Returns the value of attribute stencil


20
21
22
# File 'lib/write_xlsx/shape.rb', line 20

def stencil
  @stencil
end

#textObject

Returns the value of attribute text


20
21
22
# File 'lib/write_xlsx/shape.rb', line 20

def text
  @text
end

#tx_boxObject (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

#typeObject

Returns the value of attribute type


16
17
18
# File 'lib/write_xlsx/shape.rb', line 16

def type
  @type
end

#valignObject (readonly)

Returns the value of attribute valign


18
19
20
# File 'lib/write_xlsx/shape.rb', line 18

def valign
  @valign
end

#widthObject

Returns the value of attribute width


23
24
25
# File 'lib/write_xlsx/shape.rb', line 23

def width
  @width
end

#width_emuObject

Returns the value of attribute width_emu


24
25
26
# File 'lib/write_xlsx/shape.rb', line 24

def width_emu
  @width_emu
end

#x1Object

Returns the value of attribute x1


22
23
24
# File 'lib/write_xlsx/shape.rb', line 22

def x1
  @x1
end

#x2Object

Returns the value of attribute x2


22
23
24
# File 'lib/write_xlsx/shape.rb', line 22

def x2
  @x2
end

#x_absObject

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_offsetObject

Returns the value of attribute x_offset


23
24
25
# File 'lib/write_xlsx/shape.rb', line 23

def x_offset
  @x_offset
end

#y1Object

Returns the value of attribute y1


22
23
24
# File 'lib/write_xlsx/shape.rb', line 22

def y1
  @y1
end

#y2Object

Returns the value of attribute y2


22
23
24
# File 'lib/write_xlsx/shape.rb', line 22

def y2
  @y2
end

#y_absObject

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_offsetObject

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

#dimensionsObject


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