Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetCaptionOrHeadword() to CmPicture #288

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- [SIL.LCModel] Add GetCaptionOrHeadword() to CmPicture
- [SIL.LCModel] `LCModelStrings.NotSure` to allow clients to know if a grammatical category is the placeholder
- [SIL.LCModel.Utils] `DateTime` extension method `ToLCMTimeFormatWithMillisString()` (replaces `ReadWriteServices.FormatDateTime`)

Expand Down
49 changes: 49 additions & 0 deletions src/SIL.LCModel/DomainImpl/CmPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,55 @@ public string GetTextRepOfPicture(bool fFileNameOnly, string sReference,
// TODO (TE-7759) Include LocationRangeType and ScaleFactor
return sResult;
}

/// <summary>
/// First try to get the Caption property of this CmPicture,for the given writing system name. If
/// there isn't one then try to get the Headword property, for the given writing system name.
/// </summary>
/// <param name="wsName">The name of the writing system, which could be "magic".</param>
/// <param name="wsActual">Output the id of the writing system to which the TsString belongs.</param>
/// <returns>The TsString of the Caption or Headword property.</returns>
public ITsString GetCaptionOrHeadword(string wsName, out int wsActual)
{
ITsString bestString;
var wsId = WritingSystemServices.GetMagicWsIdFromName(wsName);
// Non-magic ws.
if (wsId == 0)
{
wsId = Cache.WritingSystemFactory.GetWsFromStr(wsName);
// The config is bad or stale, so just return null
if (wsId == 0)
{
Debug.WriteLine("Writing system requested that is not known in the local store: {0}", wsName);
wsActual = 0;
return null;
}
// First try to get the caption.
bestString = Caption.get_String(wsId);
if (String.IsNullOrEmpty(bestString.Text))
{
// Second try to get the headword.
bestString = this.OwningSense.Entry.HeadWordForWs(wsId);
}
wsActual = wsId;
}
// Magic ws. (i.e. default analysis)
else
{
// First try to get the caption (and actual ws).
bestString = Caption.GetAlternativeOrBestTss(wsId, out wsActual);
if (String.IsNullOrEmpty(bestString.Text))
{
// Need to call this to get the actual ws.
WritingSystemServices.GetMagicStringAlt(Cache, wsId, Hvo, Caption.Flid, true, out wsActual);

// Second try to get the headword.
bestString = this.OwningSense.Entry.HeadWordForWs(wsActual);
}
}

return bestString;
}
#endregion

#region Public properties
Expand Down
9 changes: 9 additions & 0 deletions src/SIL.LCModel/InterfaceAdditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4614,6 +4614,15 @@ string GetTextRepOfPicture(bool fFileNameOnly, string sReference,
/// Get the sense number of the owning LexSense.
/// </summary>
ITsString SenseNumberTSS { get; }

/// <summary>
/// First try to get the Caption property of this CmPicture,for the given writing system name. If
/// there isn't one then try to get the Headword property, for the given writing system name.
/// </summary>
/// <param name="wsName">The name of the writing system, which could be "magic".</param>
/// <param name="wsActual">Output the id of the writing system to which the TsString belongs.</param>
/// <returns>The TsString of the Caption or Headword property.</returns>
ITsString GetCaptionOrHeadword(string wsName, out int wsActual);
}

/// ----------------------------------------------------------------------------------------
Expand Down
58 changes: 58 additions & 0 deletions tests/SIL.LCModel.Tests/DomainImpl/CmPictureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,63 @@ public void ParseScale_PercentSign()
typeof(CmPictureFactory),
"ParseScaleFactor", "43%"));
}

/// ------------------------------------------------------------------------------------
/// <summary>
/// Get the Caption for the writing system.
/// </summary>
/// ------------------------------------------------------------------------------------
[Test]
public void CaptionOrHeadword_CaptionBeatsHeadword()
{
var wsId = Cache.LangProject.DefaultVernacularWritingSystem.Id;
int wsHandle = Cache.LangProject.DefaultVernacularWritingSystem.Handle;
var entry = Cache.ServiceLocator.GetInstance<ILexEntryFactory>().Create();
var sense = Cache.ServiceLocator.GetInstance<ILexSenseFactory>().Create();
entry.SensesOS.Add(sense);
var lexform = Cache.ServiceLocator.GetInstance<IMoStemAllomorphFactory>().Create();
entry.LexemeFormOA = lexform;
lexform.Form.VernacularDefaultWritingSystem = TsStringUtils.MakeString("Headword", wsHandle);
var picture = MakePicture(sense.Hvo, TsStringUtils.MakeString("Caption", wsHandle));

// SUT
var outStr = picture.GetCaptionOrHeadword(wsId, out _);
Assert.That(outStr.Text, Contains.Substring("Caption"));
}

/// ------------------------------------------------------------------------------------
/// <summary>
/// If the Caption is null or empty then get the Headword for the writing system.
/// </summary>
/// ------------------------------------------------------------------------------------
[Test]
public void CaptionOrHeadword_CaptionNullOrEmptyGivesHeadword()
{
var wsId = Cache.LangProject.DefaultVernacularWritingSystem.Id;
int wsHandle = Cache.LangProject.DefaultVernacularWritingSystem.Handle;
var entry = Cache.ServiceLocator.GetInstance<ILexEntryFactory>().Create();
var sense = Cache.ServiceLocator.GetInstance<ILexSenseFactory>().Create();
entry.SensesOS.Add(sense);
var lexform = Cache.ServiceLocator.GetInstance<IMoStemAllomorphFactory>().Create();
entry.LexemeFormOA = lexform;
lexform.Form.VernacularDefaultWritingSystem = TsStringUtils.MakeString("Headword", wsHandle);
var pictureNull = MakePicture(sense.Hvo, null);
var pictureEmpty = MakePicture(sense.Hvo, TsStringUtils.EmptyString(wsHandle));

// SUT
var outStr1 = pictureNull.GetCaptionOrHeadword(wsId, out _);
var outStr2 = pictureEmpty.GetCaptionOrHeadword(wsId, out _);
Assert.That(outStr1.Text, Contains.Substring("Headword"));
Assert.That(outStr2.Text, Contains.Substring("Headword"));
}

private ICmPicture MakePicture(int hvoOwner, ITsString captionTss)
{
var sda = Cache.DomainDataByFlid;
var hvoPicture = sda.MakeNewObject(CmPictureTags.kClassId, hvoOwner, LexSenseTags.kflidPictures, 0);
var picture = Cache.ServiceLocator.GetInstance<ICmPictureRepository>().GetObject(hvoPicture);
picture.UpdatePicture(m_internalPath, captionTss, CmFolderTags.LocalPictures, Cache.DefaultVernWs);
return picture;
}
}
}
Loading