Skip to content

Commit

Permalink
LT-22025: Fix font issues in image captions (#255)
Browse files Browse the repository at this point in the history
* LT-22025: Fix font issues in image captions

Root problem is that some image caption component styles
were not based on writing system styles in the Word export.

* Create function "GenerateContentForSimpleString" that gets a
writing system and passes it to AddProperty to generate the
string property content.
* Use first analysis language as the default writing system
for string properties.
* In AddProperty, use AddRun with the writing system to
get/create a style that is based on the writing system.
* Delete unused WriteElementContents method.
  • Loading branch information
aror92 authored Feb 4, 2025
1 parent 0ea8e06 commit 430b73c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
68 changes: 52 additions & 16 deletions Src/xWorks/ConfiguredLcmGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2512,18 +2512,15 @@ private static IFragment GenerateContentForValue(object field, object propertyVa

if (propertyValue is int)
{
var cssClassName = settings.StylesGenerator.AddStyles(config).Trim('.'); ;
return settings.ContentGenerator.AddProperty(config, cssClassName, false, propertyValue.ToString());
return GenerateContentForSimpleString(config, settings, false, propertyValue.ToString());
}
if (propertyValue is DateTime)
{
var cssClassName = settings.StylesGenerator.AddStyles(config).Trim('.'); ;
return settings.ContentGenerator.AddProperty(config, cssClassName, false, ((DateTime)propertyValue).ToLongDateString());
return GenerateContentForSimpleString(config, settings, false, ((DateTime)propertyValue).ToLongDateString());
}
else if (propertyValue is GenDate)
{
var cssClassName = settings.StylesGenerator.AddStyles(config).Trim('.'); ;
return settings.ContentGenerator.AddProperty(config, cssClassName, false, ((GenDate)propertyValue).ToLongString());
return GenerateContentForSimpleString(config, settings, false, ((GenDate)propertyValue).ToLongString());
}
else if (propertyValue is IMultiAccessorBase)
{
Expand All @@ -2533,8 +2530,7 @@ private static IFragment GenerateContentForValue(object field, object propertyVa
}
else if (propertyValue is string)
{
var cssClassName = settings.StylesGenerator.AddStyles(config).Trim('.');
return settings.ContentGenerator.AddProperty(config, cssClassName, false, propertyValue.ToString());
return GenerateContentForSimpleString(config, settings, false, propertyValue.ToString());
}
else if (propertyValue is IStText)
{
Expand Down Expand Up @@ -2570,16 +2566,18 @@ private static IFragment GenerateContentForValue(object field, object propertyVa
}
}

private static IFragment WriteElementContents(object propertyValue,
ConfigurableDictionaryNode config, GeneratorSettings settings)
/// <summary>
/// This method will add a property containing the string, using the first selected writing system,
/// or the first analysis writing system if no writing system is selected.
/// </summary>
private static IFragment GenerateContentForSimpleString(ConfigurableDictionaryNode config,
GeneratorSettings settings, bool isBlockProperty, string simpleString)
{
var content = propertyValue.ToString();
if (!String.IsNullOrEmpty(content))
{
return settings.ContentGenerator.AddProperty(config, GetClassNameAttributeForConfig(config), IsBlockProperty(config), content);
}
var writingSystem = GetLanguageFromFirstOptionOrAnalysis(config.DictionaryNodeOptions as DictionaryNodeWritingSystemOptions,
settings.Cache);
var cssClassName = settings.StylesGenerator.AddStyles(config).Trim('.');
return settings.ContentGenerator.AddProperty(config, settings.PropertyTable, cssClassName, false, simpleString, writingSystem);

return settings.ContentGenerator.CreateFragment();
}

private static IFragment GenerateContentForStrings(IMultiStringAccessor multiStringAccessor, ConfigurableDictionaryNode config,
Expand Down Expand Up @@ -3084,6 +3082,29 @@ internal static bool IsBlockProperty(ConfigurableDictionaryNode config)

/// <summary>
/// This method returns the lang attribute value from the first selected writing system in the given options.
/// It defaults to the first analysis writing system if no options are given, and English if no analysis writing system is specified.
/// </summary>
/// <param name="wsOptions"></param>
/// <param name="cache"></param>
/// <returns></returns>
private static string GetLanguageFromFirstOptionOrAnalysis(DictionaryNodeWritingSystemOptions wsOptions, LcmCache cache)
{
if (wsOptions == null)
{
const string defaultLang = "en";
var analWs = cache.WritingSystemFactory.GetStrFromWs(cache.DefaultAnalWs);
if (analWs == null)
return defaultLang;

return analWs;
}

return GetLanguageFromFirstWs(wsOptions, cache);
}

/// <summary>
/// This method returns the lang attribute value from the first selected writing system in the given options.
/// It defaults to English if no options are given.
/// </summary>
/// <param name="wsOptions"></param>
/// <param name="cache"></param>
Expand All @@ -3093,6 +3114,21 @@ private static string GetLanguageFromFirstOption(DictionaryNodeWritingSystemOpti
const string defaultLang = "en";
if (wsOptions == null)
return defaultLang;
return GetLanguageFromFirstWs(wsOptions, cache);
}

/// <summary>
/// This method returns the lang attribute value from the first selected writing system in the given options.
/// Returns null if no options are given.
/// </summary>
/// <param name="wsOptions"></param>
/// <param name="cache"></param>
/// <returns></returns>
private static string GetLanguageFromFirstWs(DictionaryNodeWritingSystemOptions wsOptions, LcmCache cache)
{
if (wsOptions == null)
return null;

foreach (var option in wsOptions.Options)
{
if (option.IsEnabled)
Expand Down
2 changes: 1 addition & 1 deletion Src/xWorks/ILcmContentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ IFragment GenerateGroupingNode(ConfigurableDictionaryNode config, object field,
Func<object, ConfigurableDictionaryNode, DictionaryPublicationDecorator, ConfiguredLcmGenerator.GeneratorSettings, IFragment> childContentGenerator);
IFragment AddSenseData(ConfigurableDictionaryNode config, IFragment senseNumberSpan, Guid ownerGuid, IFragment senseContent, bool first);
IFragment AddCollectionItem(ConfigurableDictionaryNode config, bool isBlock, string collectionItemClass, IFragment content, bool first);
IFragment AddProperty(ConfigurableDictionaryNode config, string className, bool isBlockProperty, string content);
IFragment AddProperty(ConfigurableDictionaryNode config, ReadOnlyPropertyTable propTable, string className, bool isBlockProperty, string content, string writingSystem);
IFragment CreateFragment();
IFragment CreateFragment(string str);
IFragmentWriter CreateWriter(IFragment fragment);
Expand Down
2 changes: 1 addition & 1 deletion Src/xWorks/LcmJsonGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public IFragment AddCollectionItem(ConfigurableDictionaryNode config, bool isBlo
return fragment;
}

public IFragment AddProperty(ConfigurableDictionaryNode config, string className, bool isBlockProperty, string content)
public IFragment AddProperty(ConfigurableDictionaryNode config, ReadOnlyPropertyTable propTable, string className, bool isBlockProperty, string content, string writingSystem)
{
var fragment = new StringFragment($"\"{className}\": \"{content}\",");
return fragment;
Expand Down
38 changes: 17 additions & 21 deletions Src/xWorks/LcmWordGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,39 +1121,35 @@ config.DictionaryNodeOptions is IParaOption &&

return collData;
}
public IFragment AddProperty(ConfigurableDictionaryNode config, string className, bool isBlockProperty, string content)
public IFragment AddProperty(ConfigurableDictionaryNode config, ReadOnlyPropertyTable propTable, string className, bool isBlockProperty, string content, string writingSystem)
{
var propFrag = new DocFragment();
Run contentRun = null;
string styleDisplayName = null;

// Add the content with the style.
if (!string.IsNullOrEmpty(content))
if (content == null)
{
if (!string.IsNullOrEmpty(config.Style))
{
string displayNameBase = !string.IsNullOrEmpty(config.DisplayLabel) ? config.DisplayLabel : config.Style;

Style style = GetOrCreateCharacterStyle(config.Style, displayNameBase, _propertyTable);
if (style != null)
{
styleDisplayName = style.StyleId;
}
}
contentRun = CreateRun(content, styleDisplayName);
// In this case, we should not generate the run or any before/after text for it.
return propFrag;
}

// Create a run with the correct style.
var writer = CreateWriter(propFrag);
((WordFragmentWriter)writer).AddRun(Cache, config, propTable, writingSystem, true);

// Add the content to the run.
AddToRunContent(writer, content);
var currentRun = ((WordFragmentWriter)writer).WordFragment.GetLastRun();

// Get the run's styleDisplayName for use in before/after text runs.
if (currentRun.RunProperties != null)
styleDisplayName = currentRun.RunProperties.RunStyle?.Val;

// Add Before text.
if (!string.IsNullOrEmpty(config.Before))
{
var beforeRun = CreateBeforeAfterBetweenRun(config.Before, styleDisplayName);
propFrag.DocBody.Append(beforeRun);
}

// Add the content.
if (contentRun != null)
{
propFrag.DocBody.Append(contentRun);
propFrag.DocBody.PrependChild(beforeRun);
}

// Add After text.
Expand Down
2 changes: 1 addition & 1 deletion Src/xWorks/LcmXhtmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ public IFragment AddCollectionItem(ConfigurableDictionaryNode config, bool isBlo
}
}

public IFragment AddProperty(ConfigurableDictionaryNode config, string className, bool isBlockProperty, string content)
public IFragment AddProperty(ConfigurableDictionaryNode config, ReadOnlyPropertyTable propTable, string className, bool isBlockProperty, string content, string writingSystem)
{
var bldr = new StringBuilder();
var fragment = new StringFragment(bldr);
Expand Down
7 changes: 7 additions & 0 deletions Src/xWorks/WordStylesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ private static StyleRunProperties AddFontInfoWordStyles(BaseStyleInfo projectSty
var fontName = wsFontInfo.m_fontName.ValueIsSet ? wsFontInfo.m_fontName.Value
: defaultFontInfo.FontName.ValueIsSet ? defaultFontInfo.FontName.Value : null;

// If font is explicitly set in FLEx to "<default font>", this gets picked up as the fontname.
// In that case, we want to set fontNome to null in the word style so that it can be inherited from the WS.
if (fontName == "<default font>")
{
fontName = null;
}

// fontName still null means not set in Normal Style, then get default fonts from WritingSystems configuration.
// Comparison, projectStyle.Name == "Normal", required to limit the font-family definition to the
// empty span (ie span[lang="en"]{}. If not included, font-family will be added to many more spans.
Expand Down

0 comments on commit 430b73c

Please sign in to comment.