~oubiwann/pyrtf/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
from types import StringType, ListType, TupleType, UnicodeType
from copy import deepcopy

from PropertySets import (
    ParagraphPropertySet, TabPropertySet, ShadingPropertySet,
    BorderPropertySet)
from Constants import Languages, ViewKind, ViewZoomKind, ViewScale

from rtfng.document.base import TAB, LINE, RawCode
from rtfng.document.section import Section
from rtfng.document.character import Text, Inline
from rtfng.document.paragraph import Paragraph, Table, Cell
from rtfng.object.picture import Image

DEFAULT_TAB_WIDTH = 720

ParagraphAlignmentMap = { ParagraphPropertySet.LEFT: 'ql',
                          ParagraphPropertySet.RIGHT: 'qr',
                          ParagraphPropertySet.CENTER: 'qc',
                          ParagraphPropertySet.JUSTIFY: 'qj',
                          ParagraphPropertySet.DISTRIBUTE: 'qd' }

TabAlignmentMap = { TabPropertySet.LEFT: '',
                    TabPropertySet.RIGHT: 'tqr',
                    TabPropertySet.CENTER: 'tqc',
                    TabPropertySet.DECIMAL: 'tqdec' }

TableAlignmentMap = { Table.LEFT: 'trql',
                      Table.RIGHT: 'trqr',
                      Table.CENTER: 'trqc' }

CellAlignmentMap = { Cell.ALIGN_TOP: '', # clvertalt
                     Cell.ALIGN_CENTER: 'clvertalc',
                     Cell.ALIGN_BOTTOM: 'clvertalb' }

CellFlowMap = {    Cell.FLOW_LR_TB: '', # cltxlrtb, Text in a cell flows from left to right and top to bottom (default)
                Cell.FLOW_RL_TB: 'cltxtbrl', # Text in a cell flows right to left and top to bottom
                Cell.FLOW_LR_BT: 'cltxbtlr', # Text in a cell flows left to right and bottom to top
                Cell.FLOW_VERTICAL_LR_TB: 'cltxlrtbv', # Text in a cell flows left to right and top to bottom, vertical
                Cell.FLOW_VERTICAL_TB_RL: 'cltxtbrlv' } # Text in a cell flows top to bottom and right to left, vertical

ShadingPatternMap = { ShadingPropertySet.HORIZONTAL: 'bghoriz',
                      ShadingPropertySet.VERTICAL: 'bgvert',
                      ShadingPropertySet.FORWARD_DIAGONAL: 'bgfdiag',
                      ShadingPropertySet.BACKWARD_DIAGONAL: 'bgbdiag',
                      ShadingPropertySet.VERTICAL_CROSS: 'bgcross',
                      ShadingPropertySet.DIAGONAL_CROSS: 'bgdcross',
                      ShadingPropertySet.DARK_HORIZONTAL: 'bgdkhoriz',
                      ShadingPropertySet.DARK_VERTICAL: 'bgdkvert',
                      ShadingPropertySet.DARK_FORWARD_DIAGONAL: 'bgdkfdiag',
                      ShadingPropertySet.DARK_BACKWARD_DIAGONAL: 'bgdkbdiag',
                      ShadingPropertySet.DARK_VERTICAL_CROSS: 'bgdkcross',
                      ShadingPropertySet.DARK_DIAGONAL_CROSS: 'bgdkdcross' }

TabLeaderMap = { TabPropertySet.DOTS: 'tldot',
                 TabPropertySet.HYPHENS: 'tlhyph',
                 TabPropertySet.UNDERLINE: 'tlul',
                 TabPropertySet.THICK_LINE: 'tlth',
                 TabPropertySet.EQUAL_SIGN: 'tleq' }

BorderStyleMap = { BorderPropertySet.SINGLE: 'brdrs',
                   BorderPropertySet.DOUBLE: 'brdrth',
                   BorderPropertySet.SHADOWED: 'brdrsh',
                   BorderPropertySet.DOUBLED: 'brdrdb',
                   BorderPropertySet.DOTTED: 'brdrdot',
                   BorderPropertySet.DASHED: 'brdrdash',
                   BorderPropertySet.HAIRLINE: 'brdrhair' }

SectionBreakTypeMap = { Section.NONE: 'sbknone',
                        Section.COLUMN: 'sbkcol',
                        Section.PAGE: 'sbkpage',
                        Section.EVEN: 'sbkeven',
                        Section.ODD: 'sbkodd' }

class Settings(list):
    def __init__(self):
        super(Settings, self).__init__()
        self._append = super(Settings, self).append

    def append(self, value, mask=None, fallback=None):
        if (value is not 0) and value in [ False, None, '' ]:
            if fallback: self._append(self, fallback)

        else:
            if mask:
                if value is True:
                    value = mask
                else:
                    value = mask % value
            self._append(value)

    def Join(self):
        if self: return r'\%s' % '\\'.join(self)
        return ''

    def __repr__(self):
        return self.Join()

class Renderer:
    def __init__(self, write_custom_element_callback=None):
        self.character_style_map = {}
        self.paragraph_style_map = {}
        self.WriteCustomElement  = write_custom_element_callback

    #
    #    All of the Rend* Functions populate a Settings object with values
    #
    def _RendPageProperties(self, section, settings, in_section):
        #  this one is different from the others as it takes the settings from a
        if in_section:
            #paper_size_code   = 'psz%s'
            paper_width_code  = 'pgwsxn%s'
            paper_height_code = 'pghsxn%s'
            landscape         = 'lndscpsxn'
            margin_suffix     = 'sxn'

        else:
            #paper_size_code   = 'psz%s'
            paper_width_code  = 'paperw%s'
            paper_height_code = 'paperh%s'
            landscape         = 'landscape'
            margin_suffix     = ''

        #settings.append(section.Paper.Code, paper_size_code)
        settings.append(section.Paper.Width, paper_width_code)
        settings.append(section.Paper.Height, paper_height_code)

        if section.Landscape:
            settings.append(landscape)

        if section.FirstPageNumber:
            settings.append(section.FirstPageNumber, 'pgnstarts%s')
            settings.append('pgnrestart')

        self._RendMarginsPropertySet(section.Margins, settings, margin_suffix)

    def _RendShadingPropertySet(self, shading_props, settings, prefix=''):
        if not shading_props: return

        settings.append(shading_props.Shading, prefix + 'shading%s')
        settings.append(ShadingPatternMap.get(shading_props.Pattern, False))

        settings.append(self._colour_map.get(shading_props.Foreground, False), prefix + 'cfpat%s')
        settings.append(self._colour_map.get(shading_props.Background, False), prefix + 'cbpat%s')

    def _RendBorderPropertySet(self, edge_props, settings):
        settings.append(BorderStyleMap[ edge_props.Style ])
        settings.append(edge_props.Width, 'brdrw%s')
        settings.append(self._colour_map.get(edge_props.Colour, False), 'brdrcf%s')
        settings.append(edge_props.Spacing or False, 'brsp%s')

    def _RendFramePropertySet(self, frame_props, settings, tag_prefix=''):
        if not frame_props: return

        if frame_props.Top:
            settings.append(tag_prefix + 'brdrt')
            self._RendBorderPropertySet(frame_props.Top, settings)

        if frame_props.Left:
            settings.append(tag_prefix + 'brdrl')
            self._RendBorderPropertySet(frame_props.Left, settings)

        if frame_props.Bottom:
            settings.append(tag_prefix + 'brdrb')
            self._RendBorderPropertySet(frame_props.Bottom, settings)

        if frame_props.Right:
            settings.append(tag_prefix + 'brdrr')
            self._RendBorderPropertySet(frame_props.Right, settings)

    def _RendMarginsPropertySet(self, margin_props, settings, suffix=''):
        if not margin_props: return

        settings.append(margin_props.Top, 'margt' + suffix + '%s')
        settings.append(margin_props.Left, 'margl' + suffix + '%s')
        settings.append(margin_props.Bottom, 'margb' + suffix + '%s')
        settings.append(margin_props.Right, 'margr' + suffix + '%s')

    def _RendParagraphPropertySet(self, paragraph_props, settings):
        if not paragraph_props: return
        settings.append(ParagraphAlignmentMap[ paragraph_props.Alignment ])

        settings.append(paragraph_props.SpaceBefore, 'sb%s')
        settings.append(paragraph_props.SpaceAfter, 'sa%s')

        #    then we have to find out all of the tabs
        width = 0
        for tab in paragraph_props.Tabs:
            settings.append(TabAlignmentMap[ tab.Alignment ])
            settings.append(TabLeaderMap.get(tab.Leader, ''))

            width += tab.Width or DEFAULT_TAB_WIDTH
            settings.append('tx%s' % width)

        settings.append(paragraph_props.PageBreakBefore, 'pagebb')

        settings.append(paragraph_props.FirstLineIndent, 'fi%s')
        settings.append(paragraph_props.LeftIndent, 'li%s')
        settings.append(paragraph_props.RightIndent, 'ri%s')

        if paragraph_props.SpaceBetweenLines:
            if paragraph_props.SpaceBetweenLines < 0:
                settings.append(paragraph_props.SpaceBetweenLines, r'sl%s\slmult0')
            else:
                settings.append(paragraph_props.SpaceBetweenLines, r'sl%s\slmult1')

    def _RendTextPropertySet(self, textProps, settings):
        if not textProps:
            return
        if textProps.expansion:
            settings.append(textProps.expansion, 'expndtw%s')
        settings.append(textProps.bold, 'b')
        settings.append(textProps.italic, 'i')
        settings.append(textProps.underline, 'ul')
        settings.append(textProps.dottedUnderline, 'uld')
        settings.append(textProps.doubleUnderline, 'uldb')
        settings.append(textProps.wordUnderline, 'ulw')
        settings.append(textProps.wordUnderline, 'ulw')
        settings.append(textProps.unicode, 'u')
        settings.append(self._font_map.get(textProps.font, False), 'f%s')
        settings.append(textProps.size, 'fs%s')
        settings.append(self._colour_map.get(textProps.colour, False), 'cf%s')

        if textProps.frame:
            frame = textProps.frame
            settings.append('chbrdr')
            settings.append(BorderStyleMap[ frame.Style ])
            settings.append(frame.Width, 'brdrw%s')
            settings.append(self._colour_map.get(frame.Colour, False), 'brdrcf%s')

    #
    #    All of the Write* functions will write to the internal file object
    #
    #    the _ ones probably don't need to be used by anybody outside
    #    but the other ones like WriteTextElement could be used in the Custom
    #    callback.
    def Write(self, document, fout):
        #  write all of the standard stuff based upon the first document
        self._doc  = document
        self._fout = fout
        self._WriteDocument  ()
        self._WriteColours   ()
        self._WriteFonts     ()
        self._WriteStyleSheet()

        settings = Settings()
        self._RendPageProperties(self._doc.Sections[ 0 ], settings, in_section=False)
        self._write(repr(settings))

        #  handle the simplest case first, we don't need to do anymore mucking around
        #  with section headers, etc we can just rip the document out
        if len(document.Sections) == 1:
            self._WriteSection(document.Sections[ 0 ],
                                is_first   = True,
                                add_header = False)

        else:
            for section_idx, section in enumerate(document.Sections):
                is_first       = section_idx == 0
                add_header     = True
                self._WriteSection(section, is_first, add_header)

        self._write('}')

        del self._fout, self._doc, self._CurrentStyle

    def _write(self, data, *params):
        #----------------------------------
        # begin modification
        # by Herbert Weinhandl
        # to convert accented characters
        # to their rtf-compatible form
        #for c in range(128, 256):
        #    data = data.replace(chr(c), "\'%x" % c)
        # end modification
        #
        #  This isn't the right place for this as it is going to do
        #  this loop for all sorts of writes, including settings, control codes, etc.
        #
        #  I will create a def _WriteText (or something) method that is used when the
        #  actual string that is to be viewed in the document is written, this can then
        #  do the final accented character check.
        #
        #  I left it here so that I remember to do the right thing when I have time
        #----------------------------------

        if params: data = data % params
        self._fout.write(data)

    def _WriteDocument(self):
        settings = Settings()

        assert Languages.IsValid   (self._doc.DefaultLanguage)
        assert ViewKind.IsValid    (self._doc.ViewKind)
        assert ViewZoomKind.IsValid(self._doc.ViewZoomKind)
        assert ViewScale.IsValid   (self._doc.ViewScale)

        settings.append(self._doc.DefaultLanguage, 'deflang%s')
        settings.append(self._doc.ViewKind, 'viewkind%s')
        settings.append(self._doc.ViewZoomKind, 'viewzk%s')
        settings.append(self._doc.ViewScale, 'viewscale%s')

        self._write("{\\rtf1\\ansi\\ansicpg1252\\deff0%s\n" % settings)

    def _WriteColours(self):
        self._write(r"{\colortbl ;")

        self._colour_map = {}
        offset = 0
        for colour in self._doc.StyleSheet.Colours:
            self._write(r'\red%s\green%s\blue%s;', colour.Red, colour.Green, colour.Blue)
            self._colour_map[ colour ] = offset + 1
            offset += 1
        self._write("}\n")

    def _WriteFonts(self):
        self._write(r'{\fonttbl')

        self._font_map = {}
        offset = 0
        for font in self._doc.StyleSheet.Fonts:
            pitch     = ''
            panose    = ''
            alternate = ''
            if font.Pitch: pitch     = r'\fprq%s'    % font.Pitch
            if font.Panose: panose    = r'{\*\panose %s}' % font.Panose
            if font.Alternate: alternate = r'{\*\falt %s}'   % font.Alternate.name

            self._write(r'{\f%s\f%s%s\fcharset%s%s %s%s;}',
                         offset,
                         font.Family,
                         pitch,
                         font.CharacterSet,
                         panose,
                         font.name,
                         alternate)

            self._font_map[ font ] = offset
            offset += 1

        self._write("}\n")

    def _WriteStyleSheet(self):
        self._write(r"{\stylesheet")

        #    TO DO: character styles, does anybody actually use them?

        offset_map = {}
        for idx, style in enumerate(self._doc.StyleSheet.ParagraphStyles):
            offset_map[ style ] = idx

        #    paragraph styles
        self.paragraph_style_map = {}
        for idx, style in enumerate(self._doc.StyleSheet.ParagraphStyles):

            if idx == 0:
                default = style
            else:
                self._write('\n')

            settings = Settings()

            #    paragraph properties
            self._RendParagraphPropertySet(style.ParagraphPropertySet, settings)
            self._RendFramePropertySet    (style.FramePropertySet, settings)
            self._RendShadingPropertySet  (style.ShadingPropertySet, settings)

            #    text properties
            self._RendTextPropertySet   (style.TextStyle.textProps, settings)
            self._RendShadingPropertySet(style.TextStyle.ShadingPropertySet, settings)

            #    have to take
            based_on = '\\sbasedon%s' % offset_map.get(style.BasedOn, 0)
            next     = '\\snext%s'    % offset_map.get(style.Next, 0)

            inln = '\\s%s%s' % (idx, settings)
            self._write("{%s%s%s %s;}", inln, based_on, next, style.name)

            self.paragraph_style_map[ style ] = inln

        #    if now style is specified for the first paragraph to be written, this one
        #    will be used
        self._CurrentStyle = self.paragraph_style_map[ default ]

        self._write("}\n")

    def _WriteSection(self, section, is_first, add_header):

        def WriteHF(hf, rtfword):
            #if not hf: return

            #  if we don't have anything in the header/footer then include
            #  a blank paragraph, this stops it from picking up the header/footer
            #  from the previous section
            # if not hf:    hf = [ Paragraph('') ]
            if not hf:    hf = []

            self._write('{\\%s' % rtfword)
            self._WriteElements(hf)
            self._write('}\n')

        settings = Settings()

        if not is_first:
            #  we need to finish off the preceding section
            #  and reset all of our defaults back to standard
            settings.append('sect')

        #  reset to our defaults
        settings.append('sectd')

        if add_header:
            settings.append(SectionBreakTypeMap[ section.BreakType ])
            self._RendPageProperties(section, settings, in_section=True)

        settings.append(section.HeaderY, 'headery%s')
        settings.append(section.FooterY, 'footery%s')

        #  write all of these out now as we need to do a write elements in the
        #  next section
        self._write(repr(settings))

        #    finally after all that has settled down we can do the
        #    headers and footers
        if section.FirstHeader or section.FirstFooter:
            #  include the titlepg flag if the first page has a special format
            self._write(r'\titlepg')
            WriteHF(section.FirstHeader, 'headerf')
            WriteHF(section.FirstFooter, 'footerf')

        WriteHF(section.Header, 'header')
        WriteHF(section.Footer, 'footer')

        #    and at last the contents of the section that actually appear on the page
        self._WriteElements(section)

    def _WriteElements(self, elements):
        new_line = ''
        for element in elements:
            self._write(new_line)
            new_line = '\n'

            clss = element.__class__

            if clss == Paragraph:
                self.WriteParagraphElement(element)

            elif clss == Table:
                self.WriteTableElement(element)

            elif clss == StringType:
                self.WriteParagraphElement(Paragraph(element))

            elif clss in [ RawCode, Image ]:
                self.WriteRawCode(element)

            #elif clss == List:
            #    self._HandleListElement(element)

            elif self.WriteCustomElement:
                self.WriteCustomElement(self, element)

            else:
                raise Exception("Don't know how to handle elements of type %s" % clss)

    def WriteParagraphElement(self, paragraph_elem, tag_prefix='', tag_suffix=r'\par', opening='{', closing='}'):

        #    the tag_prefix and the tag_suffix take care of paragraphs in tables.  A
        #    paragraph in a table requires and extra tag at the front (intbl) and we
        #    don't want the ending tag everytime.  We want it for all paragraphs but
        #    the last.

        overrides = Settings()
        self._RendParagraphPropertySet(paragraph_elem.Properties, overrides)
        self._RendFramePropertySet    (paragraph_elem.Frame, overrides)
        self._RendShadingPropertySet  (paragraph_elem.Shading, overrides)

        #    when writing the RTF the style is carried from the previous paragraph to the next,
        #    so if the currently written paragraph has a style then make it the current one,
        #    otherwise leave it as it was
        self._CurrentStyle = self.paragraph_style_map.get(paragraph_elem.Style, self._CurrentStyle)

        self._write(r'%s\pard\plain%s %s%s ' % (opening, tag_prefix, self._CurrentStyle, overrides))

        for element in paragraph_elem:
            if isinstance(element, StringType):
                self._write(element)
            elif isinstance(element, UnicodeType):
                self.writeUnicodeElement(element)
            elif isinstance(element, RawCode):
                self._write(element.Data)
            elif isinstance(element, Text):
                self.WriteTextElement(element)
            elif isinstance(element, Inline):
                self.WriteInlineElement(element)
            elif element == TAB:
                self._write(r'\tab ')
            elif element == LINE:
                self._write(r'\line ')
            elif self.WriteCustomElement:
                self.WriteCustomElement(self, element)
            else:
                raise Exception('Don\'t know how to handle %s' % element)
        self._write(tag_suffix + closing)

    def writeUnicodeElement(self, element):
        text = ''.join(['\u%s?' % str(ord(e)) for e in element])
        self._write(text or '')

    def WriteRawCode(self, raw_elem):
        self._write(raw_elem.Data)

    def WriteTextElement(self, text_elem):
        overrides = Settings()

        self._RendTextPropertySet   (text_elem.Properties, overrides)
        self._RendShadingPropertySet(text_elem.Shading, overrides, 'ch')

        #    write the wrapper and then let the custom handler have a go
        if overrides: self._write('{%s ' % repr(overrides))

        #    if the data is just a string then we can now write it
        if isinstance(text_elem.Data, StringType):
            self._write(text_elem.Data or '')

        elif text_elem.Data == TAB:
            self._write(r'\tab ')

        else:
            self.WriteCustomElement(self, text_elem.Data)

        if overrides: self._write('}')

    def WriteInlineElement(self, inline_elem):
        overrides = Settings()

        self._RendTextPropertySet   (inline_elem.Properties, overrides)
        self._RendShadingPropertySet(inline_elem.Shading, overrides, 'ch')

        #    write the wrapper and then let the custom handler have a go
        if overrides: self._write('{%s ' % repr(overrides))

        for element in inline_elem:
            #    if the data is just a string then we can now write it
            if isinstance(element, StringType):
                self._write(element)

            elif isinstance(element, RawCode):
                self._write(element.Data)

            elif element == TAB:
                self._write(r'\tab ')

            elif element == LINE:
                self._write(r'\line ')

            else:
                self.WriteCustomElement(self, element)

        if overrides: self._write('}')

    def WriteText(self, text):
        self._write(text or '')

    def WriteTableElement(self, table_elem):

        vmerge = [ False ] * table_elem.ColumnCount
        for height, cells in table_elem.Rows:

            #    calculate the right hand edge of the cells taking into account the spans
            offset   = table_elem.LeftOffset or 0
            cellx    = []
            cell_idx = 0
            for cell in cells:
                cellx.append(offset + sum(table_elem.ColumnWidths[: cell_idx + cell.Span ]))
                cell_idx += cell.Span

            self._write(r'{\trowd')

            settings = Settings()

            #    the spec says that this value is mandatory and I think that 108 is the default value
            #    so I'll take care of it here
            settings.append(table_elem.GapBetweenCells or 108, 'trgaph%s')
            settings.append(TableAlignmentMap[ table_elem.Alignment ])
            settings.append(height, 'trrh%s')
            settings.append(table_elem.LeftOffset, 'trleft%s')

            width = table_elem.LeftOffset or 0
            for idx, cell in enumerate(cells):
                self._RendFramePropertySet  (cell.Frame, settings, 'cl')

                #  cells don't have margins so I don't know why I was doing this
                #  I think it might have an affect in some versions of some WPs.
                #self._RendMarginsPropertySet(cell.Margins, settings, 'cl')

                #  if we are starting to merge or if this one is the first in what is
                #  probably a series of merges then start the vertical merging
                if cell.StartVerticalMerge or (cell.VerticalMerge and not vmerge[ idx ]):
                    settings.append('clvmgf')
                    vmerge[ idx ] = True

                elif cell.VerticalMerge:
                    #..continuing a merge
                    settings.append('clvmrg')

                else:
                    #..no merging going on so make sure that it is off
                    vmerge[ idx ] = False

                #  for any cell in the next row that is covered by this span we
                #  need to run off the vertical merging as we don't want them
                #  merging up into this spanned cell
                for vmerge_idx in range(idx + 1, idx + cell.Span - 1):
                    vmerge[ vmerge_idx ] = False

                settings.append(CellAlignmentMap[ cell.Alignment ])
                settings.append(CellFlowMap[ cell.Flow ])

                #  this terminates the definition of a cell and represents the right most edge of the cell from the left margin
                settings.append(cellx[ idx ], 'cellx%s')

            self._write(repr(settings))

            for cell in cells:
                if len(cell):
                    last_idx = len(cell) - 1
                    for element_idx, element in enumerate(cell):
                        #    wrap plain strings in paragraph tags
                        if isinstance(element, StringType):
                            element = Paragraph(element)

                        #    don't forget the prefix or else word crashes and does all sorts of strange things
                        if element_idx == last_idx:
                            self.WriteParagraphElement(element, tag_prefix=r'\intbl', tag_suffix='', opening='', closing='')

                        else:
                            self.WriteParagraphElement(element, tag_prefix=r'\intbl', opening='', closing='')

                    self._write(r'\cell')

                else:
                    self._write(r'\pard\intbl\cell')

            self._write('\\row}\n')