| Class | Spreadsheet::Excel::Writer::Format |
| In: |
lib/spreadsheet/excel/writer/format.rb
|
| Parent: | DelegateClassSpreadsheet::Format |
This class encapsulates everything that is needed to write an XF record.
| format | [R] | |
| xf_index | [RW] |
# File lib/spreadsheet/excel/writer/format.rb, line 12
12: def Format.boolean *args
13: args.each do |key|
14: define_method key do
15: @format.send("#{key}?") ? 1 : 0
16: end
17: end
18: end
# File lib/spreadsheet/excel/writer/format.rb, line 19
19: def Format.color key, default
20: define_method key do
21: color_code(@format.send(key) || default)
22: end
23: end
# File lib/spreadsheet/excel/writer/format.rb, line 35
35: def initialize writer, workbook, format=workbook.default_format, opts={}
36: @opts = { :type => :format }.merge opts
37: @format = format
38: @writer = writer
39: @workbook = workbook
40: super format
41: end
# File lib/spreadsheet/excel/writer/format.rb, line 42
42: def color_code color
43: SEDOC_ROLOC[color]
44: end
# File lib/spreadsheet/excel/writer/format.rb, line 45
45: def font_index
46: @writer.font_index @workbook, font.key
47: end
# File lib/spreadsheet/excel/writer/format.rb, line 48
48: def horizontal_align
49: XF_H_ALIGN.fetch @format.horizontal_align, 0
50: end
# File lib/spreadsheet/excel/writer/format.rb, line 51
51: def num_format
52: @writer.number_format_index @workbook, @format.number_format
53: end
# File lib/spreadsheet/excel/writer/format.rb, line 54
54: def text_direction
55: XF_TEXT_DIRECTION.fetch @format.text_direction, 0
56: end
# File lib/spreadsheet/excel/writer/format.rb, line 57
57: def vertical_align
58: XF_V_ALIGN.fetch @format.vertical_align, 2
59: end
# File lib/spreadsheet/excel/writer/format.rb, line 60
60: def write_op writer, op, *args
61: data = args.join
62: writer.write [op,data.size].pack("v2")
63: writer.write data
64: end
# File lib/spreadsheet/excel/writer/format.rb, line 65
65: def write_xf writer, type=@opts[:type]
66: xf_type = xf_type_prot type
67: data = [
68: font_index, # Index to FONT record (➜ 6.43)
69: num_format, # Index to FORMAT record (➜ 6.45)
70: xf_type, # Bit Mask Contents
71: # 2-0 0x0007 XF_TYPE_PROT – XF type, cell protection
72: # Bit Mask Contents
73: # 0 0x01 1 = Cell is locked
74: # 1 0x02 1 = Formula is hidden
75: # 2 0x04 0 = Cell XF; 1 = Style XF
76: # 15-4 0xfff0 Index to parent style XF
77: # (always 0xfff in style XFs)
78: xf_align, # Bit Mask Contents
79: # 2-0 0x07 XF_HOR_ALIGN – Horizontal alignment
80: # Value Horizontal alignment
81: # 0x00 General
82: # 0x01 Left
83: # 0x02 Centred
84: # 0x03 Right
85: # 0x04 Filled
86: # 0x05 Justified (BIFF4-BIFF8X)
87: # 0x06 Centred across selection
88: # (BIFF4-BIFF8X)
89: # 0x07 Distributed (BIFF8X)
90: # 3 0x08 1 = Text is wrapped at right border
91: # 6-4 0x70 XF_VERT_ALIGN – Vertical alignment
92: # Value Vertical alignment
93: # 0x00 Top
94: # 0x01 Centred
95: # 0x02 Bottom
96: # 0x03 Justified (BIFF5-BIFF8X)
97: # 0x04 Distributed (BIFF8X)
98: xf_rotation, # XF_ROTATION: Text rotation angle
99: # Value Text rotation
100: # 0 Not rotated
101: # 1-90 1 to 90 degrees counterclockwise
102: # 91-180 1 to 90 degrees clockwise
103: # 255 Letters are stacked top-to-bottom,
104: # but not rotated
105: xf_indent, # Bit Mask Contents
106: # 3-0 0x0f Indent level
107: # 4 0x10 1 = Shrink content to fit into cell
108: # 5 0x40 1 = Merge Range (djberger)
109: # 7-6 0xc0 Text direction (BIFF8X only)
110: # 0 = According to context
111: # 1 = Left-to-right
112: # 2 = Right-to-left
113: xf_used_attr, # Bit Mask Contents
114: # 7-2 0xfc XF_USED_ATTRIB – Used attributes
115: # Each bit describes the validity of a
116: # specific group of attributes. In cell XFs
117: # a cleared bit means the attributes of the
118: # parent style XF are used (but only if the
119: # attributes are valid there), a set bit
120: # means the attributes of this XF are used.
121: # In style XFs a cleared bit means the
122: # attribute setting is valid, a set bit
123: # means the attribute should be ignored.
124: # Bit Mask Contents
125: # 0 0x01 Flag for number format
126: # 1 0x02 Flag for font
127: # 2 0x04 Flag for horizontal and
128: # vertical alignment, text wrap,
129: # indentation, orientation,
130: # rotation, and text direction
131: # 3 0x08 Flag for border lines
132: # 4 0x10 Flag for background area style
133: # 5 0x20 Flag for cell protection (cell
134: # locked and formula hidden)
135: xf_borders, # Cell border lines and background area:
136: # Bit Mask Contents
137: # 3- 0 0x0000000f Left line style (➜ 3.10)
138: # 7- 4 0x000000f0 Right line style (➜ 3.10)
139: # 11- 8 0x00000f00 Top line style (➜ 3.10)
140: # 15-12 0x0000f000 Bottom line style (➜ 3.10)
141: # 22-16 0x007f0000 Colour index (➜ 6.70)
142: # for left line colour
143: # 29-23 0x3f800000 Colour index (➜ 6.70)
144: # for right line colour
145: # 30 0x40000000 1 = Diagonal line
146: # from top left to right bottom
147: # 31 0x80000000 1 = Diagonal line
148: # from bottom left to right top
149: xf_brdcolors, # Bit Mask Contents
150: # 6- 0 0x0000007f Colour index (➜ 6.70)
151: # for top line colour
152: # 13- 7 0x00003f80 Colour index (➜ 6.70)
153: # for bottom line colour
154: # 20-14 0x001fc000 Colour index (➜ 6.70)
155: # for diagonal line colour
156: # 24-21 0x01e00000 Diagonal line style (➜ 3.10)
157: # 31-26 0xfc000000 Fill pattern (➜ 3.11)
158: xf_pattern # Bit Mask Contents
159: # 6-0 0x007f Colour index (➜ 6.70)
160: # for pattern colour
161: # 13-7 0x3f80 Colour index (➜ 6.70)
162: # for pattern background
163: ]
164: write_op writer, 0x00e0, data.pack(binfmt(:xf))
165: end
# File lib/spreadsheet/excel/writer/format.rb, line 166
166: def xf_align
167: align = horizontal_align
168: align |= text_wrap << 3
169: align |= vertical_align << 4
170: align |= text_justlast << 7
171: align
172: end
# File lib/spreadsheet/excel/writer/format.rb, line 173
173: def xf_borders
174: border = left
175: border |= right << 4
176: border |= top << 8
177: border |= bottom << 12
178: border |= left_color << 16
179: border |= right_color << 23
180: border |= cross_down << 30
181: border |= cross_up << 31
182: border
183: end
# File lib/spreadsheet/excel/writer/format.rb, line 184
184: def xf_brdcolors
185: border = top_color
186: border |= bottom_color << 7
187: border |= diagonal_color << 14
188: border |= pattern << 26
189: border
190: end
# File lib/spreadsheet/excel/writer/format.rb, line 191
191: def xf_indent
192: indent = indent_level & 0x0f
193: indent |= shrink << 4
194: indent |= merge_range << 5
195: indent |= text_direction << 6
196: indent
197: end
# File lib/spreadsheet/excel/writer/format.rb, line 198
198: def xf_pattern
199: ptrn = pattern_fg_color
200: ptrn |= pattern_bg_color << 7
201: ptrn
202: end
# File lib/spreadsheet/excel/writer/format.rb, line 203
203: def xf_rotation
204: rot = @format.rotation
205: if @format.rotation_stacked?
206: rot = 255
207: elsif rot >= -90 or rotation <= 90
208: rot = -rot + 90 if rot < 0
209: else
210: warn "rotation outside -90..90; rotation set to 0"
211: rot = 0
212: end
213: rot
214: end
# File lib/spreadsheet/excel/writer/format.rb, line 215
215: def xf_type_prot type
216: type = type.to_s.downcase == 'style' ? 0xfff5 : 0x0000
217: type |= locked
218: type |= hidden << 1
219: type
220: end
# File lib/spreadsheet/excel/writer/format.rb, line 221
221: def xf_used_attr
222: atr_num = num_format & 1
223: atr_fnt = font_index & 1
224: atr_fnt = 1 unless @format.font.color == :text
225: atr_alc = 0
226: if horizontal_align != 0 \
227: || vertical_align != 2 \
228: || indent_level > 0 \
229: || shrink? || merge_range? || text_wrap?
230: then
231: atr_alc = 1
232: end
233: atr_bdr = [top, bottom, left, right, cross_up, cross_down].max
234: atr_pat = 0
235: if @format.pattern_fg_color != :border \
236: || @format.pattern_bg_color != :pattern_bg \
237: || pattern != 0x00
238: then
239: atr_pat = 1
240: end
241: atr_prot = hidden? || locked? ? 1 : 0
242: attrs = atr_num
243: attrs |= atr_fnt << 1
244: attrs |= atr_alc << 2
245: attrs |= atr_bdr << 3
246: attrs |= atr_pat << 4
247: attrs |= atr_prot << 5
248: attrs << 2
249: end