@@ -432,6 +432,25 @@ static void export_rtf_emit_char_style_changes(text_buffer_t& buffer, export_rtf
432432 buffer . appendtextf (" \\ cb%d\\ chcbpat%d " , p_new . background_color_index + 1 , p_new . background_color_index + 1 );
433433}
434434
435+ // Properly close any open style.
436+ static void close_open_styles (export_rtf_t & ctxt)
437+ {
438+ // We close all the previous styles when a link is over
439+ while (ctxt . style_index > 0 )
440+ {
441+ if (ctxt . styles[ctxt . style_index - 1 ] . metadata != ctxt . styles[ctxt.style_index ] . metadata)
442+ ctxt . buffer . appendcstring (" }}" );
443+
444+ if (ctxt . styles[ctxt . style_index - 1 ] . link_text != ctxt . styles[ctxt.style_index ] . link_text)
445+ ctxt . buffer . appendcstring (" }}" );
446+
447+ if (ctxt . styles[ctxt . style_index - 1 ] . background_color_index != ctxt . styles[ctxt.style_index ] . background_color_index)
448+ ctxt . buffer . appendcstring (" }" );
449+
450+ ctxt . style_index -= 1 ;
451+ }
452+ }
453+
435454// Emit the paragraph content. This is the second pass of rtf generation, the
436455// first pass constructs the necessary tables.
437456static bool export_rtf_emit_paragraphs (void *p_context, MCFieldExportEventType p_event_type, const MCFieldExportEventData& p_event_data)
@@ -668,17 +687,7 @@ static bool export_rtf_emit_paragraphs(void *p_context, MCFieldExportEventType p
668687 }
669688 else if (p_event_type == kMCFieldExportEventEndParagraph )
670689 {
671- // Make sure any nested styles are finished.
672- while (ctxt . style_index > 0 )
673- {
674- // MW-2014-06-11: [[ Bug 12556 ]] Make sure the metadata field is synced.
675- if (ctxt . styles[ctxt . style_index] . metadata != nil)
676- ctxt . buffer . appendcstring (" }" );
677- if (ctxt . styles[ctxt . style_index] . link_text != nil)
678- ctxt . buffer . appendcstring (" }" );
679- ctxt . buffer . appendcstring (" }" );
680- ctxt . style_index -= 1 ;
681- }
690+ close_open_styles (ctxt);
682691
683692 if (ctxt . has_metadata)
684693 ctxt . buffer . appendcstring (" }" );
@@ -693,22 +702,21 @@ static bool export_rtf_emit_paragraphs(void *p_context, MCFieldExportEventType p
693702 // with tags.
694703 export_rtf_char_style_t t_new_style;
695704 export_rtf_fetch_char_style (ctxt, t_new_style, p_event_data . character_style);
705+
706+ // SN-2015-11-17: [[ Bug 16308 ]] Fix RTF export with styles
707+ // For any change in either the background colour, the metadata or the
708+ // link associated with the text, we want to reset the style and start
709+ // with all of them synchronised.
710+ // That may not be the most efficient way, but that allows styles to
711+ // overlap without any friction.
712+ if (t_new_style . link_text != ctxt . styles[ctxt . style_index] . link_text
713+ || t_new_style . metadata != ctxt . styles[ctxt . style_index] . metadata
714+ || t_new_style . background_color_index != ctxt . styles[ctxt . style_index] . background_color_index)
715+ close_open_styles (ctxt);
696716
697717 // Handle a change in link text.
698718 if (t_new_style . link_text != ctxt . styles[ctxt . style_index] . link_text)
699719 {
700- // SN-2015-11-16: [[ Bug 16308 ]] Only add another '}' if metadata
701- if (ctxt . styles[ctxt . style_index] . metadata != nil)
702- ctxt . buffer . appendcstring (" }" );
703- if (ctxt . styles[ctxt . style_index] . link_text != nil)
704- ctxt . buffer . appendcstring (" }" );
705-
706- while (ctxt . style_index > 0 )
707- {
708- ctxt . buffer . appendcstring (" }" );
709- ctxt . style_index -= 1 ;
710- }
711-
712720 if (t_new_style . link_text != nil)
713721 {
714722 ctxt . buffer . appendtextf (" {\\ field{\\ *\\ fldinst %s \" %s\" }{\\ fldrslt " , t_new_style . link_on ? " HYPERLINK" : " LCANCHOR" , MCNameGetCString (t_new_style . link_text));
@@ -721,16 +729,6 @@ static bool export_rtf_emit_paragraphs(void *p_context, MCFieldExportEventType p
721729 // Handle a change in metadata.
722730 if (t_new_style . metadata != ctxt . styles[ctxt . style_index] . metadata)
723731 {
724- // MW-2014-06-11: [[ Bug 12556 ]] Make sure the metadata field is synced.
725- if (ctxt . styles[ctxt . style_index] . metadata != nil)
726- ctxt . buffer . appendcstring (" }}" );
727-
728- while (ctxt . style_index > 0 && ctxt . styles[ctxt . style_index] . link_text == nil)
729- {
730- ctxt . buffer . appendcstring (" }" );
731- ctxt . style_index -= 1 ;
732- }
733-
734732 if (t_new_style . metadata != nil)
735733 {
736734 ctxt . buffer . appendtextf (" {\\ field{\\ *\\ fldinst LCMETADATA \" %s\" }{\\ fldrslt " , MCNameGetCString (t_new_style . metadata));
@@ -743,12 +741,7 @@ static bool export_rtf_emit_paragraphs(void *p_context, MCFieldExportEventType p
743741 // Handle a change in background color.
744742 if (t_new_style . background_color_index != ctxt . styles[ctxt . style_index] . background_color_index)
745743 {
746- if (t_new_style . background_color_index == -1 )
747- {
748- ctxt . buffer . appendcstring (" }" );
749- ctxt . style_index -= 1 ;
750- }
751- else if (ctxt . styles[ctxt . style_index] . background_color_index == -1 )
744+ if (t_new_style . background_color_index != -1 )
752745 {
753746 ctxt . buffer . appendcstring (" {" );
754747 ctxt . styles[ctxt . style_index + 1 ] = ctxt . styles[ctxt . style_index];
0 commit comments