From 63d737e532d131a38a72249a2f8100f558d7fbae Mon Sep 17 00:00:00 2001 From: mark-sil Date: Thu, 4 Apr 2024 14:24:50 -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: Iee3a869f049f876e4b2cb03c6cbb5896b6a95826 --- Src/xWorks/ConfiguredLcmGenerator.cs | 14 ++------------ Src/xWorks/ILcmContentGenerator.cs | 3 ++- Src/xWorks/LcmJsonGenerator.cs | 13 ++++++++++--- Src/xWorks/LcmWordGenerator.cs | 2 +- Src/xWorks/LcmXhtmlGenerator.cs | 22 +++++++++++++++++++++- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Src/xWorks/ConfiguredLcmGenerator.cs b/Src/xWorks/ConfiguredLcmGenerator.cs index 5ffca9b3a1..891ceaea1e 100644 --- a/Src/xWorks/ConfiguredLcmGenerator.cs +++ b/Src/xWorks/ConfiguredLcmGenerator.cs @@ -2804,11 +2804,7 @@ private static void GenerateRunWithPossibleLink(GeneratorSettings settings, stri } if (!String.IsNullOrEmpty(style)) { - var cssStyle = CssGenerator.GenerateCssStyleFromLcmStyleSheet(style, - settings.Cache.WritingSystemFactory.GetWsFromStr(writingSystem), settings.PropertyTable); - var css = cssStyle.ToString(); - - settings.ContentGenerator.SetRunStyle(writer, config, writingSystem, css, style); + settings.ContentGenerator.SetRunStyle(writer, config, settings.PropertyTable, writingSystem, style, false); } if (linkDestination != Guid.Empty) { @@ -3025,13 +3021,7 @@ private static void GenerateError(string text, IFragmentWriter writer, Generator { var writingSystem = settings.Cache.WritingSystemFactory.GetStrFromWs(settings.Cache.WritingSystemFactory.UserWs); settings.ContentGenerator.StartRun(writer, writingSystem); - // Make the error red and slightly larger than the surrounding text - var css = new StyleDeclaration - { - 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, writingSystem, css.ToString(), null); + settings.ContentGenerator.SetRunStyle(writer, null, settings.PropertyTable, writingSystem, null, true); if (text.Contains(TxtLineSplit)) { var txtContents = text.Split(TxtLineSplit); diff --git a/Src/xWorks/ILcmContentGenerator.cs b/Src/xWorks/ILcmContentGenerator.cs index 50254d5b62..0b74607f3f 100644 --- a/Src/xWorks/ILcmContentGenerator.cs +++ b/Src/xWorks/ILcmContentGenerator.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls; +using XCore; namespace SIL.FieldWorks.XWorks { @@ -33,7 +34,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 writingSystem, string css, string runStyle); + void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, ReadOnlyPropertyTable propertyTable, string writingSystem, string runStyle, bool error); 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 ffb44b3d98..84a30f1e74 100644 --- a/Src/xWorks/LcmJsonGenerator.cs +++ b/Src/xWorks/LcmJsonGenerator.cs @@ -181,10 +181,17 @@ public void EndRun(IFragmentWriter writer) m_runBuilder.Value.Clear(); } - public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string writingSystem, string css, string runStyle) + public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, ReadOnlyPropertyTable propertyTable, string writingSystem, string runStyle, bool error) { - if(!string.IsNullOrEmpty(css)) - ((JsonFragmentWriter)writer).InsertJsonProperty("style", css); + if (!string.IsNullOrEmpty(runStyle)) + { + var cache = propertyTable.GetValue("cache", null); + var cssStyle = CssGenerator.GenerateCssStyleFromLcmStyleSheet(runStyle, + cache.WritingSystemFactory.GetWsFromStr(writingSystem), propertyTable); + string css = cssStyle?.ToString(); + if (!string.IsNullOrEmpty(css)) + ((JsonFragmentWriter)writer).InsertJsonProperty("style", css); + } } public void StartLink(IFragmentWriter writer, ConfigurableDictionaryNode config, Guid destination) diff --git a/Src/xWorks/LcmWordGenerator.cs b/Src/xWorks/LcmWordGenerator.cs index 00c28ef45e..819faa1194 100644 --- a/Src/xWorks/LcmWordGenerator.cs +++ b/Src/xWorks/LcmWordGenerator.cs @@ -683,7 +683,7 @@ public void EndRun(IFragmentWriter writer) /// 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) + public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, ReadOnlyPropertyTable propertyTable, string writingSystem, string runStyle, bool error) { if (!string.IsNullOrEmpty(runStyle)) { diff --git a/Src/xWorks/LcmXhtmlGenerator.cs b/Src/xWorks/LcmXhtmlGenerator.cs index 23b4346fab..df48443425 100644 --- a/Src/xWorks/LcmXhtmlGenerator.cs +++ b/Src/xWorks/LcmXhtmlGenerator.cs @@ -2,6 +2,7 @@ // This software is licensed under the LGPL, version 2.1 or later // (http://www.gnu.org/licenses/lgpl-2.1.html) +using ExCSS; using Icu.Collation; using SIL.FieldWorks.Common.Controls; using SIL.FieldWorks.Common.FwUtils; @@ -732,8 +733,27 @@ public void EndRun(IFragmentWriter writer) ((XmlFragmentWriter)writer).Writer.WriteEndElement(); // span } - public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, string writingSystem, string css, string runStyle) + public void SetRunStyle(IFragmentWriter writer, ConfigurableDictionaryNode config, ReadOnlyPropertyTable propertyTable, string writingSystem, string runStyle, bool error) { + StyleDeclaration cssStyle = null; + + // This is primarily intended to make formatting errors stand out in the GUI. + // Make the error red and slightly larger than the surrounding text. + if (error) + { + cssStyle = new StyleDeclaration + { + new ExCSS.Property("color") { Term = new HtmlColor(222, 0, 0) }, + new ExCSS.Property("font-size") { Term = new PrimitiveTerm(ExCSS.UnitType.Ems, 1.5f) } + }; + } + else if (!string.IsNullOrEmpty(runStyle)) + { + var cache = propertyTable.GetValue("cache", null); + cssStyle = CssGenerator.GenerateCssStyleFromLcmStyleSheet(runStyle, + cache.WritingSystemFactory.GetWsFromStr(writingSystem), propertyTable); + } + string css = cssStyle?.ToString(); if (!String.IsNullOrEmpty(css)) ((XmlFragmentWriter)writer).Writer.WriteAttributeString("style", css); }