Class: Writexlsx::Shape

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ Shape

Returns a new instance of Shape.



26
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
# File 'lib/write_xlsx/shape.rb', line 26

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.



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

def adjustments
  @adjustments
end

#alignObject (readonly)

Returns the value of attribute align.



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

def align
  @align
end

#column_endObject

Returns the value of attribute column_end.



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

def column_end
  @column_end
end

#column_startObject

Returns the value of attribute column_start.



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

def column_start
  @column_start
end

#connectObject

Returns the value of attribute connect.



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

def connect
  @connect
end

#drawingObject (readonly)

Returns the value of attribute drawing.



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

def drawing
  @drawing
end

#edit_asObject (readonly)

Returns the value of attribute edit_as.



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

def edit_as
  @edit_as
end

#elementObject

Returns the value of attribute element.



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

def element
  @element
end

#endObject

Returns the value of attribute end.



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

def end
  @end
end

#end_indexObject

Returns the value of attribute end_index.



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

def end_index
  @end_index
end

#end_sideObject

Returns the value of attribute end_side.



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

def end_side
  @end_side
end

#fillObject (readonly)

Returns the value of attribute fill.



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

def fill
  @fill
end

#flip_hObject

Returns the value of attribute flip_h.



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

def flip_h
  @flip_h
end

#flip_vObject

Returns the value of attribute flip_v.



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

def flip_v
  @flip_v
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#heightObject

Returns the value of attribute height.



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

def height
  @height
end

#height_emuObject

Returns the value of attribute height_emu.



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

def height_emu
  @height_emu
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

#line_typeObject

Returns the value of attribute line_type.



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

def line_type
  @line_type
end

#line_weightObject

Returns the value of attribute line_weight.



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

def line_weight
  @line_weight
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#paletteObject

Returns the value of attribute palette.



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

def palette
  @palette
end

#rotationObject

Returns the value of attribute rotation.



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

def rotation
  @rotation
end

#row_endObject

Returns the value of attribute row_end.



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

def row_end
  @row_end
end

#row_startObject

Returns the value of attribute row_start.



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

def row_start
  @row_start
end

#scale_xObject

Returns the value of attribute scale_x.



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

def scale_x
  @scale_x
end

#scale_yObject

Returns the value of attribute scale_y.



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

def scale_y
  @scale_y
end

#startObject

Returns the value of attribute start.



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

def start
  @start
end

#start_indexObject

Returns the value of attribute start_index.



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

def start_index
  @start_index
end

#start_sideObject

Returns the value of attribute start_side.



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

def start_side
  @start_side
end

#stencilObject

Returns the value of attribute stencil.



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

def stencil
  @stencil
end

#textObject

Returns the value of attribute text.



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

def text
  @text
end

#tx_boxObject (readonly)

Returns the value of attribute tx_box.



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

def tx_box
  @tx_box
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#valignObject (readonly)

Returns the value of attribute valign.



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

def valign
  @valign
end

#widthObject

Returns the value of attribute width.



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

def width
  @width
end

#width_emuObject

Returns the value of attribute width_emu.



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

def width_emu
  @width_emu
end

#x1Object

Returns the value of attribute x1.



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

def x1
  @x1
end

#x2Object

Returns the value of attribute x2.



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

def x2
  @x2
end

#x_absObject

Returns the value of attribute x_abs.



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

def x_abs
  @x_abs
end

#x_offsetObject

Returns the value of attribute x_offset.



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

def x_offset
  @x_offset
end

#y1Object

Returns the value of attribute y1.



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

def y1
  @y1
end

#y2Object

Returns the value of attribute y2.



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

def y2
  @y2
end

#y_absObject

Returns the value of attribute y_abs.



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

def y_abs
  @y_abs
end

#y_offsetObject

Returns the value of attribute y_offset.



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

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.



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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/write_xlsx/shape.rb', line 216

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.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/write_xlsx/shape.rb', line 170

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,
                           @drawing
                           )

  # 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



312
313
314
315
316
317
318
319
320
321
# File 'lib/write_xlsx/shape.rb', line 312

def dimensions
  [
   @column_start, @row_start,
   @x1,           @y1,
   @column_end,   @row_end,
   @x2,           @y2,
   @x_abs,        @y_abs,
   @width_emu,    @height_emu
  ]
end

#get_palette_color(index) ⇒ Object

Convert from an Excel internal colour index to a XML style #RRGGBB index based on the default or user defined values in the Workbook palette. Note: This version doesn’t add an alpha channel.



153
154
155
156
157
158
159
160
161
# File 'lib/write_xlsx/shape.rb', line 153

def get_palette_color(index)
  # Adjust the colour index.
  idx = index - 8

  # Palette is passed in from the Workbook class.
  rgb = @palette[idx]

  sprintf("%02X%02X%02X", *rgb)
end

#set_position(row_start, column_start, x_offset, y_offset, x_scale, y_scale) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/write_xlsx/shape.rb', line 201

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



132
133
134
135
136
137
138
139
# File 'lib/write_xlsx/shape.rb', line 132

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.



302
303
304
305
306
307
308
309
310
# File 'lib/write_xlsx/shape.rb', line 302

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