From 9dd6df0ee80168bc831beb98456cff7787630454 Mon Sep 17 00:00:00 2001 From: mark-sil Date: Wed, 27 Mar 2024 12:51:49 -0400 Subject: [PATCH] LT-21672: Add styles to table cells MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds support for when a style is applied to a specific cell or when different styles are applied to different pieces of the text in a cell. Note: The changes in SetRunStyle() also add support for styles that are applied to all or a portion of the text in other fields (notes, custom fields …). Change-Id: Id1ca7ccfaeaad7e7ea7e781263151b1714d9d90f --- Src/xWorks/ConfiguredLcmGenerator.cs | 6 ++-- Src/xWorks/ILcmContentGenerator.cs | 2 +- Src/xWorks/LcmJsonGenerator.cs | 2 +- Src/xWorks/LcmWordGenerator.cs | 42 ++++++++++++++++++++++------ Src/xWorks/LcmXhtmlGenerator.cs | 5 ++-- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/Src/xWorks/ConfiguredLcmGenerator.cs b/Src/xWorks/ConfiguredLcmGenerator.cs index 047a475ce6..5ffca9b3a1 100644 --- a/Src/xWorks/ConfiguredLcmGenerator.cs +++ b/Src/xWorks/ConfiguredLcmGenerator.cs @@ -2807,8 +2807,8 @@ private static void GenerateRunWithPossibleLink(GeneratorSettings settings, stri var cssStyle = CssGenerator.GenerateCssStyleFromLcmStyleSheet(style, settings.Cache.WritingSystemFactory.GetWsFromStr(writingSystem), settings.PropertyTable); var css = cssStyle.ToString(); - if (!String.IsNullOrEmpty(css)) - settings.ContentGenerator.SetRunStyle(writer, config, css); + + settings.ContentGenerator.SetRunStyle(writer, config, writingSystem, css, style); } if (linkDestination != Guid.Empty) { @@ -3031,7 +3031,7 @@ private static void GenerateError(string text, IFragmentWriter writer, Generator new ExCSS.Property("color") { Term = new HtmlColor(222, 0, 0) }, new ExCSS.Property("font-size") { Term = new PrimitiveTerm(UnitType.Ems, 1.5f) } }; - settings.ContentGenerator.SetRunStyle(writer, null, css.ToString()); + settings.ContentGenerator.SetRunStyle(writer, null, writingSystem, css.ToString(), null); if (text.Contains(TxtLineSplit)) { var txtContents = text.Split(TxtLineSplit); diff --git a/Src/xWorks/ILcmContentGenerator.cs b/Src/xWorks/ILcmContentGenerator.cs index e9b9301ee3..50254d5b62 100644 --- a/Src/xWorks/ILcmContentGenerator.cs +++ b/Src/xWorks/ILcmContentGenerator.cs @@ -33,7 +33,7 @@ IFragment GenerateGroupingNode(object field, string className, ConfigurableDicti void EndBiDiWrapper(IFragmentWriter writer); void StartRun(IFragmentWriter writer, string writingSystem); void EndRun(IFragmentWriter writer); - void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string css); + void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string writingSystem, string css, string runStyle); void StartLink(IFragmentWriter writer, ConfigurableDictionaryNode config, Guid destination); void StartLink(IFragmentWriter writer, ConfigurableDictionaryNode config, string externalDestination); void EndLink(IFragmentWriter writer); diff --git a/Src/xWorks/LcmJsonGenerator.cs b/Src/xWorks/LcmJsonGenerator.cs index a033b42400..ffb44b3d98 100644 --- a/Src/xWorks/LcmJsonGenerator.cs +++ b/Src/xWorks/LcmJsonGenerator.cs @@ -181,7 +181,7 @@ public void EndRun(IFragmentWriter writer) m_runBuilder.Value.Clear(); } - public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string css) + public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string writingSystem, string css, string runStyle) { if(!string.IsNullOrEmpty(css)) ((JsonFragmentWriter)writer).InsertJsonProperty("style", css); diff --git a/Src/xWorks/LcmWordGenerator.cs b/Src/xWorks/LcmWordGenerator.cs index 448c55f70b..00c28ef45e 100644 --- a/Src/xWorks/LcmWordGenerator.cs +++ b/Src/xWorks/LcmWordGenerator.cs @@ -677,14 +677,27 @@ public void EndRun(IFragmentWriter writer) // Beginning a new run is sufficient to end the old run // and to ensure new styles/content are applied to the new run. } - public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string css) - { - // TODO: Many runs don't ever call this function; this is not where we want to set the character styles. - /*if (config != null && !string.IsNullOrEmpty(config.Style)) + /// + /// Set the style for a specific run. + /// This is needed to set the specific style for any field that allows the + /// default style to be overridden (Table Cell, Custom Field, Note...). + /// + public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string writingSystem, string _, string runStyle) + { + if (!string.IsNullOrEmpty(runStyle)) { - ((WordFragmentWriter)writer).WordFragment.AddStyleLink(config.Style, ConfigurableDictionaryNode.StyleTypes.Character); - }*/ + // Add the style link. + ((WordFragmentWriter)writer).WordFragment.AddStyleLink(runStyle, ConfigurableDictionaryNode.StyleTypes.Character); + + // Only add the style to the styleSheet if not already there. + if (!_styleSheet.ChildElements.Any(p => ((Style)p).StyleId == runStyle)) + { + int ws = Cache.WritingSystemFactory.GetWsFromStr(writingSystem); + var wpStyle = WordStylesGenerator.GenerateWordStyleFromLcmStyleSheet(runStyle, ws, _propertyTable); + _styleSheet.Append(wpStyle); + } + } } public void StartLink(IFragmentWriter writer, ConfigurableDictionaryNode config, Guid destination) { @@ -751,9 +764,20 @@ public void StartTableRow(IFragmentWriter writer) } public void AddTableCell(IFragmentWriter writer, bool isHead, int colSpan, HorizontalAlign alignment, IFragment content) { - WP.TableCell tableCell = new WP.TableCell(); - tableCell.Append(new WP.Paragraph(new WP.Run(new WP.Text(content.ToString())))); - ((WordFragmentWriter)writer).CurrentTableRow.Append(tableCell); + // The runs contain the text and any cell-specific styling (in the run properties). + // Note: multiple runs will exist if the cell contains multiple styles. + WP.Paragraph paragraph = new WP.Paragraph(); + foreach (WP.Run run in ((DocFragment)content).DocBody.Elements()) + { + paragraph.Append(run.CloneNode(true)); + } + + if (paragraph.HasChildren) + { + WP.TableCell tableCell = new WP.TableCell(); + tableCell.Append(paragraph); + ((WordFragmentWriter)writer).CurrentTableRow.Append(tableCell); + } } public void EndTableRow(IFragmentWriter writer) { diff --git a/Src/xWorks/LcmXhtmlGenerator.cs b/Src/xWorks/LcmXhtmlGenerator.cs index 7489d971e4..23b4346fab 100644 --- a/Src/xWorks/LcmXhtmlGenerator.cs +++ b/Src/xWorks/LcmXhtmlGenerator.cs @@ -732,9 +732,10 @@ public void EndRun(IFragmentWriter writer) ((XmlFragmentWriter)writer).Writer.WriteEndElement(); // span } - public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string css) + public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string writingSystem, string css, string runStyle) { - ((XmlFragmentWriter)writer).Writer.WriteAttributeString("style", css); + if (!String.IsNullOrEmpty(css)) + ((XmlFragmentWriter)writer).Writer.WriteAttributeString("style", css); } public void StartLink(IFragmentWriter writer, ConfigurableDictionaryNode config, Guid destination)