diff --git a/AGENTS.md b/AGENTS.md index d2563570..041e2e32 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -41,6 +41,7 @@ Failure signature (both commands): GitVersion.MsBuild (netcoreapp3.1 gitversion. ### Known prerequisites and gotchas - GitVersion.MsBuild is used across projects; it requires git metadata. CI checks out with fetch-depth 0. +- Building from a detached HEAD (e.g. a review worktree) makes GitVersion fail; set the environment variable `DisableGitVersionTask=true` (an environment variable, not `-p:`, so the nested GenerateKernelCs build sees it). - net462 builds on Windows require the .NET Framework 4.6.1 targeting pack (CI installs it). - ICU data generation requires ICU binaries (CI installs icu-fw on Ubuntu). - Some projects warn on NU1701; treat as warnings unless build breaks. diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a172e5d..03fb23f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- [SIL.LCModel.Core] Text property XML no longer writes what its own reader rejects (#409); a custom bullet (LT-16341) is now saved. - [SIL.LCModel] BaseStyleInfo now loads default font features (ktptFontVariations) from style rules, fixing font features set as style defaults being dropped on reload (LT-22351) - [SIL.LCModel] BaseStyleInfo no longer clobbers ws-specific font overrides inherited from a based-on style with the default font info's merely-inherited values; only explicitly-set default properties are propagated to ws overrides (LT-22351) - [SIL.LCModel] BulletInfo now decodes all properties of an encoded bullet font info instead of stopping at the first string property, fixing bullet font features (ktptFontVariations) being lost when a bullet font name was also set (LT-22351) @@ -42,6 +43,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- [SIL.LCModel.Core] The text property XML reader accepts negative and numeric values and `MarginTop`; the writer writes `marginTop`, and no longer writes tabDef, tabList, charStyle, paraStyle or editable. - [SIL.LCModel] The domain-model `Generated*.cs` sources are now produced at compile time by the new `SIL.LCModel.SourceGenerators` Roslyn source generator, instead of the `LcmGenerate` MSBuild task running the NVelocity templates in a child MSBuild process. The NVelocity engine and model wrappers moved to a shared `SIL.LCModel.ModelGeneration` project used by both the source generator and the (retained) `LcmGenerate` task, which still renders other templates from the model (e.g. the FieldWorks C++ cellar-constants header). Templates and generated output are byte-for-byte identical. No public API change. - [SIL.LCModel] Replaced the internal StructureMap (`structuremap.patched`) IoC container with `Microsoft.Extensions.DependencyInjection` (8.x). No public API change. - [SIL.LCModel] Trim 12 overwordy semantic domain descriptions and fix 22 punctuation/whitespace issues in the SemDom.xml template, matching sillsdev/FwLocalizations#5 and sillsdev/FwLocalizations#7 diff --git a/src/SIL.LCModel.Core/Text/TsPropsSerializer.cs b/src/SIL.LCModel.Core/Text/TsPropsSerializer.cs index 5b07b0e8..2559c50e 100644 --- a/src/SIL.LCModel.Core/Text/TsPropsSerializer.cs +++ b/src/SIL.LCModel.Core/Text/TsPropsSerializer.cs @@ -11,6 +11,7 @@ using System.Globalization; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Xml; using System.Xml.Linq; using System.Xml.Schema; @@ -99,7 +100,7 @@ internal static void WriteIntProperty(XmlWriter writer, ILgWritingSystemFactory } break; case FwTextPropType.ktptFontSize: - writer.WriteAttributeString("fontsize", value.ToString()); + writer.WriteAttributeString("fontsize", value.ToString(CultureInfo.InvariantCulture)); if (var != FwTextPropVar.ktpvDefault) writer.WriteAttributeString("fontsizeUnit", GetVariationString(var)); break; @@ -110,7 +111,7 @@ internal static void WriteIntProperty(XmlWriter writer, ILgWritingSystemFactory writer.WriteAttributeString("italic", GetToggleValueString(value)); break; case FwTextPropType.ktptOffset: - writer.WriteAttributeString("offset", value.ToString()); + writer.WriteAttributeString("offset", value.ToString(CultureInfo.InvariantCulture)); writer.WriteAttributeString("offsetUnit", GetVariationString(var)); break; case FwTextPropType.ktptSuperscript: @@ -131,27 +132,28 @@ internal static void WriteIntProperty(XmlWriter writer, ILgWritingSystemFactory writer.WriteAttributeString("align", GetAlignmentTypeString(value)); break; case FwTextPropType.ktptFirstIndent: - writer.WriteAttributeString("firstIndent", value.ToString()); + writer.WriteAttributeString("firstIndent", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptLeadingIndent: - writer.WriteAttributeString("leadingIndent", value.ToString()); + writer.WriteAttributeString("leadingIndent", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptTrailingIndent: - writer.WriteAttributeString("trailingIndent", value.ToString()); + writer.WriteAttributeString("trailingIndent", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptSpaceBefore: - writer.WriteAttributeString("spaceBefore", value.ToString()); + writer.WriteAttributeString("spaceBefore", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptSpaceAfter: - writer.WriteAttributeString("spaceAfter", value.ToString()); - break; - case FwTextPropType.ktptTabDef: - writer.WriteAttributeString("tabDef", value.ToString()); + writer.WriteAttributeString("spaceAfter", value.ToString(CultureInfo.InvariantCulture)); break; + // ktptTabDef is not written: tabs are not supported and the readers have always + // ignored the attribute. case FwTextPropType.ktptLineHeight: - writer.WriteAttributeString("lineHeight", Math.Abs(value).ToString()); + // negative millipoints means "exact" internally (long, so that int.MinValue can + // be negated); older readers reject a negative number here, so no sign is + // written for any variation + writer.WriteAttributeString("lineHeight", Math.Abs((long) value).ToString(CultureInfo.InvariantCulture)); writer.WriteAttributeString("lineHeightUnit", GetVariationString(var)); - // negative means "exact" internally if (var == FwTextPropVar.ktpvMilliPoint) writer.WriteAttributeString("lineHeightType", value < 0 ? "exact" : "atLeast"); break; @@ -161,80 +163,84 @@ internal static void WriteIntProperty(XmlWriter writer, ILgWritingSystemFactory // Properties from Views case FwTextPropType.ktptRightToLeft: - writer.WriteAttributeString("rightToLeft", value.ToString()); + writer.WriteAttributeString("rightToLeft", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptPadLeading: - writer.WriteAttributeString("padLeading", value.ToString()); + writer.WriteAttributeString("padLeading", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptPadTrailing: - writer.WriteAttributeString("padTrailing", value.ToString()); + writer.WriteAttributeString("padTrailing", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptMarginTop: - writer.WriteAttributeString("MarginTop", value.ToString()); + // Older versions wrote "MarginTop", which no reader accepted; the readers accept both. + writer.WriteAttributeString("marginTop", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptPadTop: - writer.WriteAttributeString("padTop", value.ToString()); + writer.WriteAttributeString("padTop", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptPadBottom: - writer.WriteAttributeString("padBottom", value.ToString()); + writer.WriteAttributeString("padBottom", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptBorderTop: - writer.WriteAttributeString("borderTop", value.ToString()); + writer.WriteAttributeString("borderTop", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptBorderBottom: - writer.WriteAttributeString("borderBottom", value.ToString()); + writer.WriteAttributeString("borderBottom", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptBorderLeading: - writer.WriteAttributeString("borderLeading", value.ToString()); + writer.WriteAttributeString("borderLeading", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptBorderTrailing: - writer.WriteAttributeString("borderTrailing", value.ToString()); + writer.WriteAttributeString("borderTrailing", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptBorderColor: writer.WriteAttributeString("borderColor", GetColorString(value)); break; case FwTextPropType.ktptBulNumScheme: - writer.WriteAttributeString("bulNumScheme", value.ToString()); + writer.WriteAttributeString("bulNumScheme", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptBulNumStartAt: - writer.WriteAttributeString("bulNumStartAt", (value == int.MinValue ? 0 : value).ToString()); + writer.WriteAttributeString("bulNumStartAt", (value == int.MinValue ? 0 : value).ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptDirectionDepth: - writer.WriteAttributeString("directionDepth", value.ToString()); + writer.WriteAttributeString("directionDepth", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptKeepWithNext: - writer.WriteAttributeString("keepWithNext", value.ToString()); + writer.WriteAttributeString("keepWithNext", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptKeepTogether: - writer.WriteAttributeString("keepTogether", value.ToString()); + writer.WriteAttributeString("keepTogether", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptHyphenate: - writer.WriteAttributeString("hyphenate", value.ToString()); + writer.WriteAttributeString("hyphenate", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptWidowOrphanControl: - writer.WriteAttributeString("widowOrphan", value.ToString()); + writer.WriteAttributeString("widowOrphan", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptMaxLines: - writer.WriteAttributeString("maxLines", value.ToString()); + writer.WriteAttributeString("maxLines", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptCellBorderWidth: - writer.WriteAttributeString("cellBorderWidth", value.ToString()); + writer.WriteAttributeString("cellBorderWidth", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptCellSpacing: - writer.WriteAttributeString("cellSpacing", value.ToString()); + writer.WriteAttributeString("cellSpacing", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptCellPadding: - writer.WriteAttributeString("cellPadding", value.ToString()); - break; - case FwTextPropType.ktptEditable: - writer.WriteAttributeString("editable", GetEditableString(value)); + writer.WriteAttributeString("cellPadding", value.ToString(CultureInfo.InvariantCulture)); break; + // ktptEditable is not written: it is set by view constructors on display strings, + // and the string reader has always ignored it, so persisting it would make + // existing text read-only once loaded. + // ktptTableRule is not written either: it is only set by the views table layout, + // and an attribute name older readers do not know would make the record unloadable + // for them. case FwTextPropType.ktptSetRowDefaults: - writer.WriteAttributeString("setRowDefaults", value.ToString()); + writer.WriteAttributeString("setRowDefaults", value.ToString(CultureInfo.InvariantCulture)); break; case FwTextPropType.ktptRelLineHeight: - writer.WriteAttributeString("relLineHeight", value.ToString()); + writer.WriteAttributeString("relLineHeight", value.ToString(CultureInfo.InvariantCulture)); break; } } @@ -243,11 +249,11 @@ internal static void WriteStringProperty(XmlWriter writer, FwTextPropType tpt, s { switch (tpt) { - case FwTextPropType.ktptCharStyle: - writer.WriteAttributeString("charStyle", Normalizer.Normalize(value, Normalizer.UNormalizationMode.UNORM_NFC)); - break; + // ktptCharStyle, ktptParaStyle and ktptTabList are not written: they are obsolete + // (superseded by ktptNamedStyle; tabs are not supported) and the readers have always + // ignored them. case FwTextPropType.ktptFontFamily: - writer.WriteAttributeString("fontFamily", Normalizer.Normalize(value, Normalizer.UNormalizationMode.UNORM_NFC)); + writer.WriteAttributeString("fontFamily", GetXmlSafeString(value)); break; case FwTextPropType.ktptObjData: if (string.IsNullOrEmpty(value)) @@ -262,25 +268,25 @@ internal static void WriteStringProperty(XmlWriter writer, FwTextPropType tpt, s writer.WriteAttributeString("type", "picture"); break; case FwObjDataTypes.kodtNameGuidHot: - writer.WriteAttributeString("link", GetGuidString(value)); + WriteGuidAttribute(writer, "link", value); break; case FwObjDataTypes.kodtExternalPathName: - writer.WriteAttributeString("externalLink", Normalizer.Normalize(value.Substring(1), Normalizer.UNormalizationMode.UNORM_NFC)); + writer.WriteAttributeString("externalLink", GetXmlSafeString(value.Substring(1))); break; case FwObjDataTypes.kodtOwnNameGuidHot: - writer.WriteAttributeString("ownlink", GetGuidString(value)); + WriteGuidAttribute(writer, "ownlink", value); break; case FwObjDataTypes.kodtEmbeddedObjectData: // This is only used for copying to the clipboard // We assume that the buffer contains valid XML and that the receiving code // knows what to do with it! - writer.WriteAttributeString("embedded", Normalizer.Normalize(value.Substring(1), Normalizer.UNormalizationMode.UNORM_NFC)); + writer.WriteAttributeString("embedded", GetXmlSafeString(value.Substring(1))); break; case FwObjDataTypes.kodtContextString: // This is a generated context-sensitive string. The next 8 characters give a // GUID, which is from to a known set of GUIDs that have special meaning to a // view contructor. - writer.WriteAttributeString("contextString", GetGuidString(value)); + WriteGuidAttribute(writer, "contextString", value); break; case FwObjDataTypes.kodtGuidMoveableObjDisp: // This results in a call-back to the VC, with a new VwEnv, to create any @@ -289,20 +295,23 @@ internal static void WriteStringProperty(XmlWriter writer, FwTextPropType tpt, s // occur immediately following the paragraph line that contains the ORC, // which functions as an anchor, but may be moved down past following text // to improve page breaking. - writer.WriteAttributeString("moveableObj", GetGuidString(value)); + WriteGuidAttribute(writer, "moveableObj", value); break; } break; // Properties from Views case FwTextPropType.ktptNamedStyle: - writer.WriteAttributeString("namedStyle", Normalizer.Normalize(value, Normalizer.UNormalizationMode.UNORM_NFC)); + writer.WriteAttributeString("namedStyle", GetXmlSafeString(value)); break; case FwTextPropType.ktptBulNumTxtBef: - writer.WriteAttributeString("bulNumTxtBef", Normalizer.Normalize(value, Normalizer.UNormalizationMode.UNORM_NFC)); + writer.WriteAttributeString("bulNumTxtBef", GetXmlSafeString(value)); break; case FwTextPropType.ktptBulNumTxtAft: - writer.WriteAttributeString("bulNumTxtAft", Normalizer.Normalize(value, Normalizer.UNormalizationMode.UNORM_NFC)); + writer.WriteAttributeString("bulNumTxtAft", GetXmlSafeString(value)); + break; + case FwTextPropType.ktptCustomBullet: + writer.WriteAttributeString("bulCusTxt", GetXmlSafeString(value)); break; case FwTextPropType.ktptBulNumFontInfo: // BulNumFontInfo is written separately. @@ -311,18 +320,20 @@ internal static void WriteStringProperty(XmlWriter writer, FwTextPropType tpt, s // WsStyle is written separately. break; case FwTextPropType.ktptFontVariations: - writer.WriteAttributeString("fontVariations", Normalizer.Normalize(value, Normalizer.UNormalizationMode.UNORM_NFC)); - break; - case FwTextPropType.ktptParaStyle: - writer.WriteAttributeString("paraStyle", Normalizer.Normalize(value, Normalizer.UNormalizationMode.UNORM_NFC)); - break; - case FwTextPropType.ktptTabList: - writer.WriteAttributeString("tabList", Normalizer.Normalize(value, Normalizer.UNormalizationMode.UNORM_NFC)); + writer.WriteAttributeString("fontVariations", GetXmlSafeString(value)); break; case FwTextPropType.ktptTags: + if (string.IsNullOrEmpty(value)) + break; const int guidLength = 16; byte[] bytes = Encoding.Unicode.GetBytes(value); int guidCount = bytes.Length / guidLength; + if (guidCount == 0) + { + // nothing but an incomplete GUID: malformed, leave it out + Debug.WriteLine("TsPropsSerializer: ktptTags value shorter than one GUID is not written."); + break; + } var sb = new StringBuilder(); for (int i = 0; i < guidCount; i++) { @@ -353,15 +364,30 @@ private static IEnumerable> DecodeBulNumFontInfo(st // Write the integer valued properties in alphabetical order var props = new SortedDictionary(); var otherProps = new List>(); + var strProps = new List>(); int i = 0; while (i < bulNumFontInfo.Length) { var tpt = (FwTextPropType) bulNumFontInfo[i]; i++; - if (tpt == FwTextPropType.ktptFontFamily) + if (tpt == FwTextPropType.ktptFontFamily || tpt == FwTextPropType.ktptFontVariations) + { + // String properties are terminated by a NUL (BulletInfo writes the font family + // then the font features). + int end = bulNumFontInfo.IndexOf('\0', i); + if (end < 0) + end = bulNumFontInfo.Length; + string strValue = bulNumFontInfo.Substring(i, end - i); + strProps.Add(new KeyValuePair(tpt == FwTextPropType.ktptFontFamily ? "fontFamily" : "fontVariations", + GetXmlSafeString(strValue))); + i = end + 1; + continue; + } + + if (i + 2 > bulNumFontInfo.Length) { - string fontFamily = bulNumFontInfo.Substring(i, bulNumFontInfo.Length - i - 1); - otherProps.Add(new KeyValuePair("fontFamily", fontFamily)); + // truncated: leave out the incomplete property rather than fail the save + Debug.WriteLine("TsPropsSerializer: truncated ktptBulNumFontInfo value; the incomplete property is not written."); break; } @@ -382,10 +408,10 @@ private static IEnumerable> DecodeBulNumFontInfo(st props["underline"] = GetUnderlineTypeString(value); break; case FwTextPropType.ktptFontSize: - props["fontsize"] = string.Format("{0}mpt", value); + props["fontsize"] = string.Format(CultureInfo.InvariantCulture, "{0}mpt", value); break; case FwTextPropType.ktptOffset: - props["offset"] = string.Format("{0}mpt", value); + props["offset"] = string.Format(CultureInfo.InvariantCulture, "{0}mpt", value); break; case FwTextPropType.ktptForeColor: props["forecolor"] = GetColorString(value); @@ -397,14 +423,20 @@ private static IEnumerable> DecodeBulNumFontInfo(st props["undercolor"] = GetColorString(value); break; default: - otherProps.Add(new KeyValuePair(string.Format("prop_{0}", (int) tpt), value.ToString())); + otherProps.Add(new KeyValuePair(string.Format(CultureInfo.InvariantCulture, "{0}{1}", BulNumFontInfoOtherPropPrefix, (int) tpt), value.ToString(CultureInfo.InvariantCulture))); break; } } - return props.Concat(otherProps); + return props.Concat(otherProps).Concat(strProps); } + /// + /// Prefix of the attribute used for an integer property in BulNumFontInfo that has no + /// attribute name of its own. + /// + private const string BulNumFontInfoOtherPropPrefix = "prop_"; + private static void WriteWsStyles(XmlWriter writer, ILgWritingSystemFactory lgwsf, string wsStyles) { IDictionary wsStyleInfos = DecodeWSStyles(wsStyles); @@ -450,12 +482,21 @@ private static void WriteStringPropertyIfSpecified(XmlWriter writer, FwTextPropT WriteStringProperty(writer, tpt, value); } + private static IDictionary MalformedWsStyles(IDictionary complete) + { + Debug.WriteLine("TsPropsSerializer: truncated ktptWsStyle value; the incomplete entry is not written."); + return complete; + } + private static IDictionary DecodeWSStyles(string wsStyles) { + // A truncated (malformed) entry is left out rather than failing the whole save. var results = new SortedDictionary(); int i = 0; while (i < wsStyles.Length) { + if (i + 3 > wsStyles.Length) + return MalformedWsStyles(results); int ws = wsStyles[i] | (wsStyles[i + 1] << 16); i += 2; @@ -463,6 +504,8 @@ private static IDictionary DecodeWSStyles(string wsStyles) int len = wsStyles[i]; i++; + if (i + len + 1 > wsStyles.Length) + return MalformedWsStyles(results); if (len > 0) wsStyleInfo.FontFamily = ConvertSpecialFontNames(wsStyles.Substring(i, len)); i += len; @@ -476,20 +519,28 @@ private static IDictionary DecodeWSStyles(string wsStyles) // propCount = -1 * the number of them. for (int j = propCount; j < 0; j++) { + if (i + 2 > wsStyles.Length) + return MalformedWsStyles(results); var tpt = (FwTextPropType) wsStyles[i]; i++; len = wsStyles[i]; i++; + if (i + len > wsStyles.Length) + return MalformedWsStyles(results); if (tpt == FwTextPropType.ktptFontVariations) wsStyleInfo.FontVariations = wsStyles.Substring(i, len); i += len; } + if (i + 1 > wsStyles.Length) + return MalformedWsStyles(results); propCount = (short) wsStyles[i]; i++; } // Integer properties + if (propCount < 0 || i + propCount * 4 > wsStyles.Length) + return MalformedWsStyles(results); for (int j = 0; j < propCount; j++) { var tpt = (FwTextPropType) wsStyles[i]; @@ -637,7 +688,7 @@ private static string GetToggleValueString(int value) case FwTextToggleVal.kttvInvert: return "invert"; default: - return value.ToString(); + return value.ToString(CultureInfo.InvariantCulture); } } @@ -654,7 +705,7 @@ private static string GetVariationString(FwTextPropVar var) case FwTextPropVar.ktpvRelative: return "rel"; default: - return ((int) var).ToString(); + return ((int) var).ToString(CultureInfo.InvariantCulture); } } @@ -669,7 +720,7 @@ private static string GetSuperscriptString(int value) case FwSuperscriptVal.kssvSub: return "sub"; default: - return value.ToString(); + return value.ToString(CultureInfo.InvariantCulture); } } @@ -692,7 +743,7 @@ private static string GetUnderlineTypeString(int value) case FwUnderlineType.kuntSquiggle: return "squiggle"; default: - return value.ToString(); + return value.ToString(CultureInfo.InvariantCulture); } } @@ -707,7 +758,7 @@ private static string GetSpellingModeString(int value) case SpellingModes.ksmForceCheck: return "forceCheck"; default: - return value.ToString(); + return value.ToString(CultureInfo.InvariantCulture); } } @@ -728,29 +779,49 @@ private static string GetAlignmentTypeString(int value) case FwTextAlign.ktalJustify: return "justify"; default: - return value.ToString(); + return value.ToString(CultureInfo.InvariantCulture); } } - private static string GetEditableString(int value) + /// + /// Writes the GUID that follows the object data type character. Object data too short to + /// hold a GUID is malformed; it is left out rather than failing the whole save. + /// + private static void WriteGuidAttribute(XmlWriter writer, string attrName, string objData) { - switch ((TptEditable) value) + const int guidLength = 8; // in UTF-16 characters + if (objData.Length < 1 + guidLength) { - case TptEditable.ktptNotEditable: - return "not"; - case TptEditable.ktptIsEditable: - return "is"; - case TptEditable.ktptSemiEditable: - return "semi"; - default: - return value.ToString(); + Debug.WriteLine("TsPropsSerializer: ktptObjData value too short to hold a GUID is not written."); + return; } + Guid guid = MiscUtils.GetGuidFromObjData(objData.Substring(1)); + writer.WriteAttributeString(attrName, guid.ToString()); } - private static string GetGuidString(string value) + private static readonly Regex UnpairedSurrogateRegex = new Regex( + @"[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(? + /// Gets the NFC form of a string attribute value, without unpaired surrogates or other + /// characters XML cannot represent. Unpaired surrogates are removed before normalizing, + /// because an unpaired surrogate blocks composition and is not valid in XML. + /// + /// + /// Deliberately separate from the run-text handling in : + /// offsets index into run text, so run text must keep its length, whereas nothing indexes + /// into a property value (a font name, bullet text), so a property value can simply drop + /// what XML cannot hold. + /// + private static string GetXmlSafeString(string value) { - Guid guid = MiscUtils.GetGuidFromObjData(value.Substring(1)); - return guid.ToString(); + // liblcm stores an empty string property as null; there is nothing to clean. + if (string.IsNullOrEmpty(value)) + return value; + string stripped = TsStringSerializer.StripInvalidXmlChars(UnpairedSurrogateRegex.Replace(value, string.Empty)); + if (stripped.Length != value.Length) + Debug.WriteLine("TsPropsSerializer: characters XML cannot represent were removed from a string property."); + return TsStringSerializer.StripInvalidXmlChars(Normalizer.Normalize(stripped, Normalizer.UNormalizationMode.UNORM_NFC)); } #endregion @@ -817,7 +888,32 @@ private static void AddBulletFontInfoToBldr(ITsPropsBldr bldr, XElement bulFontI { int intValue, type, var; string strValue; - ITsPropsBldr fontProps = GetPropAttributesForElement(bulFontInfo, lgwsf); + // The writer writes an integer property that has no attribute name of its own as + // prop_; read those directly. + var otherProps = new List>(); + var namedAttrs = new XElement(bulFontInfo.Name); + foreach (XAttribute attr in bulFontInfo.Attributes()) + { + int otherType; + if (attr.Name.LocalName.StartsWith(BulNumFontInfoOtherPropPrefix, StringComparison.Ordinal) + && int.TryParse(attr.Name.LocalName.Substring(BulNumFontInfoOtherPropPrefix.Length), NumberStyles.None, + CultureInfo.InvariantCulture, out otherType)) + { + // The encoding holds the property number in one character, so the writer never + // writes a number above 0xFFFF; skip one rather than store a different property. + if (otherType > ushort.MaxValue) + { + Debug.WriteLine("TsPropsSerializer: BulNumFontInfo attribute " + attr.Name.LocalName + " is out of range and was skipped."); + continue; + } + otherProps.Add(new KeyValuePair(otherType, GetIntValueFromStr(attr.Value))); + } + else + { + namedAttrs.Add(new XAttribute(attr)); + } + } + ITsPropsBldr fontProps = GetPropAttributesForElement(namedAttrs, lgwsf); // Add the integer properties to the bullet props string StringBuilder bulletProps = new StringBuilder(fontProps.IntPropCount * 3 + fontProps.StrPropCount * 20); @@ -827,6 +923,11 @@ private static void AddBulletFontInfoToBldr(ITsPropsBldr bldr, XElement bulFontI bulletProps.Append((char)type); WriteIntToStrBuilder(bulletProps, intValue); } + foreach (KeyValuePair otherProp in otherProps) + { + bulletProps.Append((char)otherProp.Key); + WriteIntToStrBuilder(bulletProps, otherProp.Value); + } // Add the string properties to the bullet props string for (int i = 0; i < fontProps.StrPropCount; i++) @@ -938,7 +1039,7 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin break; case "borderBottom": propsBldr.SetIntPropValues((int)FwTextPropType.ktptBorderBottom, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "borderColor": propsBldr.SetIntPropValues((int)FwTextPropType.ktptBorderColor, 0, @@ -946,23 +1047,23 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin break; case "borderLeading": propsBldr.SetIntPropValues((int)FwTextPropType.ktptBorderLeading, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "borderTop": propsBldr.SetIntPropValues((int)FwTextPropType.ktptBorderTop, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "borderTrailing": propsBldr.SetIntPropValues((int)FwTextPropType.ktptBorderTrailing, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "bulNumScheme": propsBldr.SetIntPropValues((int)FwTextPropType.ktptBulNumScheme, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "bulNumStartAt": propsBldr.SetIntPropValues((int)FwTextPropType.ktptBulNumStartAt, 0, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "bulNumTxtAft": propsBldr.SetStrPropValue((int)FwTextPropType.ktptBulNumTxtAft, attr.Value); @@ -974,7 +1075,11 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin propsBldr.SetStrPropValue((int)FwTextPropType.ktptCustomBullet, attr.Value); break; case "charStyle": - Debug.Fail("We don't support the old charStyle attribute!"); + case "paraStyle": + case "tabDef": + case "tabList": + // Obsolete or unsupported; older versions wrote them, but they have never + // been read back. break; case "contextString": AddObjDataToBldr(propsBldr, attr, FwObjDataTypes.kodtContextString); @@ -989,7 +1094,7 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin break; case "firstIndent": propsBldr.SetIntPropValues((int)FwTextPropType.ktptFirstIndent, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, true)); + GetIntValueFromStr(attr.Value)); break; case "fontFamily": propsBldr.SetStrPropValue((int)FwTextPropType.ktptFontFamily, attr.Value); @@ -1013,15 +1118,15 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin break; case "keepTogether": propsBldr.SetIntPropValues((int)FwTextPropType.ktptKeepTogether, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "keepWithNext": propsBldr.SetIntPropValues((int)FwTextPropType.ktptKeepWithNext, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "leadingIndent": propsBldr.SetIntPropValues((int)FwTextPropType.ktptLeadingIndent, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "lineHeight": AddSizePropertyToBldr(propsBldr, attr.Value, (int)FwTextPropType.ktptLineHeight, @@ -1036,19 +1141,20 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin break; case "marginBottom": propsBldr.SetIntPropValues((int)FwTextPropType.ktptMarginBottom, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "marginLeading": propsBldr.SetIntPropValues((int)FwTextPropType.ktptMarginLeading, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "marginTop": + case "MarginTop": // written by older versions propsBldr.SetIntPropValues((int)FwTextPropType.ktptMarginTop, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "marginTrailing": propsBldr.SetIntPropValues((int)FwTextPropType.ktptMarginTrailing, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "moveableObj": AddObjDataToBldr(propsBldr, attr, FwObjDataTypes.kodtGuidMoveableObjDisp); @@ -1058,7 +1164,7 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin break; case "offset": AddSizePropertyToBldr(propsBldr, attr.Value, (int)FwTextPropType.ktptOffset, - xml.Attribute("offsetUnit"), null, true); + xml.Attribute("offsetUnit"), null); break; case "offsetUnit": break; // Ignore. Its handled in the offset @@ -1067,38 +1173,35 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin break; case "padBottom": propsBldr.SetIntPropValues((int)FwTextPropType.ktptPadBottom, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "padLeading": propsBldr.SetIntPropValues((int)FwTextPropType.ktptPadLeading, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "padTop": propsBldr.SetIntPropValues((int)FwTextPropType.ktptPadTop, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "padTrailing": propsBldr.SetIntPropValues((int)FwTextPropType.ktptPadTrailing, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "paracolor": propsBldr.SetIntPropValues((int)FwTextPropType.ktptParaColor, 0, GetColorValueForStr(attr.Value)); break; - case "paraStyle": - Debug.Fail("We don't support the old paraStyle attribute!"); - break; case "rightToLeft": propsBldr.SetIntPropValues((int)FwTextPropType.ktptRightToLeft, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "spaceAfter": propsBldr.SetIntPropValues((int)FwTextPropType.ktptSpaceAfter, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, true)); + GetIntValueFromStr(attr.Value)); break; case "spaceBefore": propsBldr.SetIntPropValues((int)FwTextPropType.ktptSpaceBefore, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, true)); + GetIntValueFromStr(attr.Value)); break; case "spellcheck": propsBldr.SetIntPropValues((int)FwTextPropType.ktptSpellCheck, (int)FwTextPropVar.ktpvEnum, @@ -1108,22 +1211,19 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin propsBldr.SetIntPropValues((int)FwTextPropType.ktptSuperscript, (int)FwTextPropVar.ktpvEnum, GetSuperscriptValueForStr(attr.Value)); break; - case "tabDef": - Debug.Fail("We don't support the tabDef property!"); - break; - case "tabList": - Debug.Fail("We don't support the tabList property!"); - break; case "tags": propsBldr.SetStrPropValue((int)FwTextPropType.ktptTags, GetGuidValuesForStr(attr.Value)); break; case "trailingIndent": propsBldr.SetIntPropValues((int)FwTextPropType.ktptTrailingIndent, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "type": - Debug.Assert(attr.Value == "chars", "Embedded pictures are not supported!"); + // The writer writes type="picture" for picture object data (an antique kludge); + // the picture is not read back. Not an assertion: this is data the writer emits. + if (attr.Value != "chars") + Trace.TraceWarning("TsPropsSerializer: embedded pictures are not supported; type=\"" + attr.Value + "\" ignored."); break; case "undercolor": propsBldr.SetIntPropValues((int)FwTextPropType.ktptUnderColor, 0, @@ -1135,7 +1235,39 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin break; case "widowOrphan": propsBldr.SetIntPropValues((int)FwTextPropType.ktptWidowOrphanControl, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); + break; + case "cellBorderWidth": + propsBldr.SetIntPropValues((int)FwTextPropType.ktptCellBorderWidth, (int)FwTextPropVar.ktpvMilliPoint, + GetIntValueFromStr(attr.Value)); + break; + case "cellPadding": + propsBldr.SetIntPropValues((int)FwTextPropType.ktptCellPadding, (int)FwTextPropVar.ktpvMilliPoint, + GetIntValueFromStr(attr.Value)); + break; + case "cellSpacing": + propsBldr.SetIntPropValues((int)FwTextPropType.ktptCellSpacing, (int)FwTextPropVar.ktpvMilliPoint, + GetIntValueFromStr(attr.Value)); + break; + case "directionDepth": + propsBldr.SetIntPropValues((int)FwTextPropType.ktptDirectionDepth, (int)FwTextPropVar.ktpvDefault, + GetIntValueFromStr(attr.Value)); + break; + case "hyphenate": + propsBldr.SetIntPropValues((int)FwTextPropType.ktptHyphenate, (int)FwTextPropVar.ktpvEnum, + GetIntValueFromStr(attr.Value)); + break; + case "maxLines": + propsBldr.SetIntPropValues((int)FwTextPropType.ktptMaxLines, (int)FwTextPropVar.ktpvDefault, + GetIntValueFromStr(attr.Value)); + break; + case "relLineHeight": + propsBldr.SetIntPropValues((int)FwTextPropType.ktptRelLineHeight, (int)FwTextPropVar.ktpvMilliPoint, + GetIntValueFromStr(attr.Value)); + break; + case "setRowDefaults": + propsBldr.SetIntPropValues((int)FwTextPropType.ktptSetRowDefaults, (int)FwTextPropVar.ktpvEnum, + GetIntValueFromStr(attr.Value)); break; case "ws": propsBldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, @@ -1149,6 +1281,7 @@ internal static ITsPropsBldr GetPropAttributesForElement(XElement xml, ILgWritin Debug.Fail("We don't support the old wsStyle attribute!"); break; case "space": + case "editable": // a display-only property older versions wrote; never read back break; default: throw new XmlSchemaException("Unknown Prop attribute: " + attr.Name.LocalName); @@ -1191,7 +1324,7 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF break; case "borderBottom": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderBottom, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "borderColor": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderColor, 0, @@ -1199,23 +1332,23 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF break; case "borderLeading": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderLeading, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "borderTop": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderTop, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "borderTrailing": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderTrailing, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "bulNumScheme": strBldr.SetIntPropValues((int)FwTextPropType.ktptBulNumScheme, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "bulNumStartAt": strBldr.SetIntPropValues((int)FwTextPropType.ktptBulNumStartAt, 0, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "bulNumTxtAft": strBldr.SetStrPropValue((int)FwTextPropType.ktptBulNumTxtAft, attr.Value); @@ -1227,7 +1360,11 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF strBldr.SetStrPropValue((int)FwTextPropType.ktptCustomBullet, attr.Value); break; case "charStyle": - Debug.Fail("We don't support the old charStyle attribute!"); + case "paraStyle": + case "tabDef": + case "tabList": + // Obsolete or unsupported; older versions wrote them, but they have never + // been read back. break; case "contextString": AddObjDataToBldr(strBldr, attr, FwObjDataTypes.kodtContextString); @@ -1243,7 +1380,7 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF break; case "firstIndent": strBldr.SetIntPropValues((int)FwTextPropType.ktptFirstIndent, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, true)); + GetIntValueFromStr(attr.Value)); break; case "fontFamily": strBldr.SetStrPropValue((int)FwTextPropType.ktptFontFamily, attr.Value); @@ -1267,15 +1404,15 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF break; case "keepTogether": strBldr.SetIntPropValues((int)FwTextPropType.ktptKeepTogether, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "keepWithNext": strBldr.SetIntPropValues((int)FwTextPropType.ktptKeepWithNext, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "leadingIndent": strBldr.SetIntPropValues((int)FwTextPropType.ktptLeadingIndent, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "lineHeight": AddSizePropertyToBldr(strBldr, attr.Value, (int)FwTextPropType.ktptLineHeight, @@ -1291,19 +1428,20 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF break; case "marginBottom": strBldr.SetIntPropValues((int)FwTextPropType.ktptMarginBottom, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "marginLeading": strBldr.SetIntPropValues((int)FwTextPropType.ktptMarginLeading, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "marginTop": + case "MarginTop": // written by older versions strBldr.SetIntPropValues((int)FwTextPropType.ktptMarginTop, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "marginTrailing": strBldr.SetIntPropValues((int)FwTextPropType.ktptMarginTrailing, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "moveableObj": AddObjDataToBldr(strBldr, attr, FwObjDataTypes.kodtGuidMoveableObjDisp); @@ -1324,38 +1462,35 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF break; case "padBottom": strBldr.SetIntPropValues((int)FwTextPropType.ktptPadBottom, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "padLeading": strBldr.SetIntPropValues((int)FwTextPropType.ktptPadLeading, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "padTop": strBldr.SetIntPropValues((int)FwTextPropType.ktptPadTop, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "padTrailing": strBldr.SetIntPropValues((int)FwTextPropType.ktptPadTrailing, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "paracolor": strBldr.SetIntPropValues((int)FwTextPropType.ktptParaColor, 0, GetColorValueForStr(attr.Value)); break; - case "paraStyle": - Debug.Fail("We don't support the old paraStyle attribute!"); - break; case "rightToLeft": strBldr.SetIntPropValues((int)FwTextPropType.ktptRightToLeft, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "spaceAfter": strBldr.SetIntPropValues((int)FwTextPropType.ktptSpaceAfter, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, true)); + GetIntValueFromStr(attr.Value)); break; case "spaceBefore": strBldr.SetIntPropValues((int)FwTextPropType.ktptSpaceBefore, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, true)); + GetIntValueFromStr(attr.Value)); break; case "spellcheck": strBldr.SetIntPropValues((int)FwTextPropType.ktptSpellCheck, (int)FwTextPropVar.ktpvEnum, @@ -1365,22 +1500,19 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF strBldr.SetIntPropValues((int)FwTextPropType.ktptSuperscript, (int)FwTextPropVar.ktpvEnum, GetSuperscriptValueForStr(attr.Value)); break; - case "tabDef": - Debug.Fail("We don't support the tabDef property!"); - break; - case "tabList": - Debug.Fail("We don't support the tabList property!"); - break; case "tags": strBldr.SetStrPropValue((int)FwTextPropType.ktptTags, GetGuidValuesForStr(attr.Value)); break; case "trailingIndent": strBldr.SetIntPropValues((int)FwTextPropType.ktptTrailingIndent, (int)FwTextPropVar.ktpvMilliPoint, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); break; case "type": - Debug.Assert(attr.Value == "chars", "Embedded pictures are not supported!"); + // The writer writes type="picture" for picture object data (an antique kludge); + // the picture is not read back. Not an assertion: this is data the writer emits. + if (attr.Value != "chars") + Trace.TraceWarning("TsPropsSerializer: embedded pictures are not supported; type=\"" + attr.Value + "\" ignored."); break; case "undercolor": strBldr.SetIntPropValues((int)FwTextPropType.ktptUnderColor, 0, @@ -1392,7 +1524,39 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF break; case "widowOrphan": strBldr.SetIntPropValues((int)FwTextPropType.ktptWidowOrphanControl, (int)FwTextPropVar.ktpvEnum, - GetIntValueFromStr(attr.Value, false)); + GetIntValueFromStr(attr.Value)); + break; + case "cellBorderWidth": + strBldr.SetIntPropValues((int)FwTextPropType.ktptCellBorderWidth, (int)FwTextPropVar.ktpvMilliPoint, + GetIntValueFromStr(attr.Value)); + break; + case "cellPadding": + strBldr.SetIntPropValues((int)FwTextPropType.ktptCellPadding, (int)FwTextPropVar.ktpvMilliPoint, + GetIntValueFromStr(attr.Value)); + break; + case "cellSpacing": + strBldr.SetIntPropValues((int)FwTextPropType.ktptCellSpacing, (int)FwTextPropVar.ktpvMilliPoint, + GetIntValueFromStr(attr.Value)); + break; + case "directionDepth": + strBldr.SetIntPropValues((int)FwTextPropType.ktptDirectionDepth, (int)FwTextPropVar.ktpvDefault, + GetIntValueFromStr(attr.Value)); + break; + case "hyphenate": + strBldr.SetIntPropValues((int)FwTextPropType.ktptHyphenate, (int)FwTextPropVar.ktpvEnum, + GetIntValueFromStr(attr.Value)); + break; + case "maxLines": + strBldr.SetIntPropValues((int)FwTextPropType.ktptMaxLines, (int)FwTextPropVar.ktpvDefault, + GetIntValueFromStr(attr.Value)); + break; + case "relLineHeight": + strBldr.SetIntPropValues((int)FwTextPropType.ktptRelLineHeight, (int)FwTextPropVar.ktpvMilliPoint, + GetIntValueFromStr(attr.Value)); + break; + case "setRowDefaults": + strBldr.SetIntPropValues((int)FwTextPropType.ktptSetRowDefaults, (int)FwTextPropVar.ktpvEnum, + GetIntValueFromStr(attr.Value)); break; case "ws": strBldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, @@ -1406,7 +1570,7 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF Debug.Fail("We don't support the old wsStyle attribute!"); break; case "space": - case "editable": + case "editable": // a display-only property older versions wrote; never read back break; default: throw new XmlSchemaException("Unknown Prop attribute: " + attr.Name.LocalName); @@ -1421,7 +1585,6 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF /// ------------------------------------------------------------------------------------ /// /// Adds a size property to the specified props builder with the specified prop type. - /// This default does not allow negative sizes. /// /// The props builder. /// The string representation of the size value. @@ -1431,33 +1594,17 @@ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemF /// ------------------------------------------------------------------------------------ private static void AddSizePropertyToBldr(ITsPropsBldr propsBldr, string value, int proptype, XAttribute unitAttr, XAttribute typeAttr) - { - AddSizePropertyToBldr(propsBldr, value, proptype, unitAttr, typeAttr, false); - } - /// ------------------------------------------------------------------------------------ - /// - /// Adds a size property to the specified props builder with the specified prop type. - /// - /// The props builder. - /// The string representation of the size value. - /// The property type. - /// The attribute containing the size unit (can be null). - /// The attribute containing the size type (can be null). - /// true if negative size is allowed (e.g., baseline offset) - /// ------------------------------------------------------------------------------------ - private static void AddSizePropertyToBldr(ITsPropsBldr propsBldr, string value, int proptype, - XAttribute unitAttr, XAttribute typeAttr, bool allowNegative) { int var; - int intValue = GetSizeValue(value, unitAttr, typeAttr, allowNegative, out var); + int intValue = GetSizeValue(value, unitAttr, typeAttr, out var); propsBldr.SetIntPropValues(proptype, var, intValue); } /// ------------------------------------------------------------------------------------ /// - /// Adds a size property to the specified props builder with the specified prop type. + /// Adds a size property to the specified string builder with the specified prop type. /// - /// The props builder. + /// The string builder. /// The string representation of the size value. /// The property type. /// The attribute containing the size unit (can be null). @@ -1467,10 +1614,11 @@ private static void AddSizePropertyToBldr(ITsIncStrBldr propsBldr, string value, XAttribute unitAttr, XAttribute typeAttr) { int var; - int intValue = GetSizeValue(value, unitAttr, typeAttr, false, out var); + int intValue = GetSizeValue(value, unitAttr, typeAttr, out var); propsBldr.SetIntPropValues(proptype, var, intValue); } - private static int GetSizeValue(string value, XAttribute unitAttr, XAttribute typeAttr, bool allowNegative, out int var) + + private static int GetSizeValue(string value, XAttribute unitAttr, XAttribute typeAttr, out int var) { if (value.EndsWith("mpt")) { @@ -1479,14 +1627,14 @@ private static int GetSizeValue(string value, XAttribute unitAttr, XAttribute ty } var = GetUnitValueForStr(unitAttr); - int intValue = GetIntValueFromStr(value, allowNegative); + int intValue = GetIntValueFromStr(value); if (typeAttr != null) { switch (typeAttr.Value) { case "exact": if (var == (int)FwTextPropVar.ktpvMilliPoint) - intValue = -intValue; // negative means "exact" internally. See FWC-20. + intValue = unchecked(-intValue); // negative means "exact" internally. See FWC-20. break; case "atLeast": break; // This is the default value @@ -1552,6 +1700,8 @@ private static int GetSuperscriptValueForStr(string str) case "super": return (int)FwSuperscriptVal.kssvSuper; case "sub": return (int)FwSuperscriptVal.kssvSub; default: + if (TryGetEnumNumber(str, out int value)) + return value; // the writer writes values outside the enumeration as numbers throw new XmlSchemaException("Invalid superscript value: " + str); } } @@ -1570,6 +1720,8 @@ private static int GetSpellCheckValueForStr(string str) case "forceCheck": return (int)SpellingModes.ksmForceCheck; case "doNotCheck": return (int)SpellingModes.ksmDoNotCheck; default: + if (TryGetEnumNumber(str, out int value)) + return value; // the writer writes values outside the enumeration as numbers throw new XmlSchemaException("Invalid spell check value: " + str); } } @@ -1592,6 +1744,8 @@ private static int GetUnderlineTypeForStr(string str) case "squiggle": return (int)FwUnderlineType.kuntSquiggle; case "strikethrough": return (int)FwUnderlineType.kuntStrikethrough; default: + if (TryGetEnumNumber(str, out int value)) + return value; // the writer writes values outside the enumeration as numbers throw new XmlSchemaException("Invalid underline type: " + str); } } @@ -1612,6 +1766,9 @@ private static int GetUnitValueForStr(XAttribute attr) case "mpt": return (int)FwTextPropVar.ktpvMilliPoint; case "rel": return (int)FwTextPropVar.ktpvRelative; default: + // The writer writes other variations as numbers; a variation is never negative. + if (int.TryParse(attr.Value, NumberStyles.None, CultureInfo.InvariantCulture, out int value)) + return value; throw new XmlSchemaException("Invalid value for unit type: " + attr.Value); } } @@ -1637,30 +1794,31 @@ private static string GetGuidValuesForStr(string str) /// ------------------------------------------------------------------------------------ /// - /// Gets the integer value from the specified string. + /// Gets the integer value from the specified string. Any 32-bit value is accepted: the + /// writer writes values as signed numbers, and values above int.MaxValue (an unsigned + /// form) are read as the same 32 bits. /// /// The string. - /// True to allow negative numbers, false otherwise. - /// /// ------------------------------------------------------------------------------------ - private static int GetIntValueFromStr(string str, bool fAllowNegative) + private static int GetIntValueFromStr(string str) { - if (fAllowNegative) - return int.Parse(str, CultureInfo.InvariantCulture); - - try - { - return (int)uint.Parse(str, CultureInfo.InvariantCulture); - } - catch (OverflowException) - { - throw new XmlSchemaException("Negative number is not valid for this attribute"); - } - catch (FormatException) - { + long value; + if (!long.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) throw new XmlSchemaException("Invalid number for this attribute: " + str); - } + if (value < int.MinValue || value > uint.MaxValue) + throw new XmlSchemaException("Number out of range for this attribute: " + str); + return unchecked((int)value); + } + + /// + /// Gets the number for an enumerated attribute written as a number, which the writer + /// does for values that have no name. + /// + private static bool TryGetEnumNumber(string str, out int value) + { + return int.TryParse(str, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out value); } + /// ------------------------------------------------------------------------------------ /// /// Gets the integer representation of an 'on', 'off', or 'invert' value @@ -1675,6 +1833,8 @@ private static int GetToggleValueForStr(string str) case "invert": return (int)FwTextToggleVal.kttvInvert; case "off": return (int)FwTextToggleVal.kttvOff; default: + if (TryGetEnumNumber(str, out int value)) + return value; // the writer writes values outside the enumeration as numbers throw new XmlSchemaException("Invalid value for toggle attribute. Needed 'on', 'off', or 'invert'"); } } @@ -1726,7 +1886,10 @@ private static int GetAlignValueForStr(string str) case "left": return (int)FwTextAlign.ktalLeft; case "right": return (int)FwTextAlign.ktalRight; case "trailing": return (int)FwTextAlign.ktalTrailing; - default: throw new XmlSchemaException("Invalid value for align attribute: " + str); + default: + if (TryGetEnumNumber(str, out int value)) + return value; // the writer writes values outside the enumeration as numbers + throw new XmlSchemaException("Invalid value for align attribute: " + str); } } #endregion diff --git a/src/SIL.LCModel.Core/Text/TsStringSerializer.cs b/src/SIL.LCModel.Core/Text/TsStringSerializer.cs index 173b6a7a..0c227f7f 100644 --- a/src/SIL.LCModel.Core/Text/TsStringSerializer.cs +++ b/src/SIL.LCModel.Core/Text/TsStringSerializer.cs @@ -9,6 +9,7 @@ // using System; +using System.Diagnostics; using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -77,7 +78,11 @@ public static string SerializeTsStringToXml(ITsString tss, ILgWritingSystemFacto } string runFieldName; - if (textProps.TryGetStringValue(FwTextPropType.ktptFieldName, out runFieldName) && fieldName != runFieldName) + bool hasFieldName = textProps.TryGetStringValue(FwTextPropType.ktptFieldName, out runFieldName); + // A name that is only characters XML cannot hold is no name: a Field element with an empty name does not load. + if (hasFieldName && runFieldName != null) + runFieldName = StripInvalidXmlChars(runFieldName); + if (hasFieldName && fieldName != runFieldName) { if (!string.IsNullOrEmpty(fieldName)) writer.WriteEndElement(); @@ -174,7 +179,7 @@ public static string SerializeTsStringToXml(ITsString tss, ILgWritingSystemFacto if (runText != string.Empty && runText.All(char.IsWhiteSpace)) writer.WriteAttributeString("xml", "space", "", "preserve"); // TODO: should we escape quotation marks? this is not necessary but different than the behavior of the C++ implementation - writer.WriteString(StripInvalidXmlChars(Normalizer.Normalize(runText, Normalizer.UNormalizationMode.UNORM_NFC))); + writer.WriteString(GetXmlSafeText(runText)); } writer.WriteEndElement(); @@ -188,14 +193,30 @@ public static string SerializeTsStringToXml(ITsString tss, ILgWritingSystemFacto return xml.ToString(); } - private static readonly Regex InvalidXmlRegex = new Regex(@"[\x00-\x08\x0B\x0C\x0E-\x1F\uFFFE\uFFFF]", RegexOptions.Compiled); + // Characters not allowed in XML, including UTF-16 surrogates that are not part of a pair. + // Documented here: https://en.wikipedia.org/wiki/Valid_characters_in_XML + private static readonly Regex InvalidXmlRegex = new Regex( + @"[\x00-\x08\x0B\x0C\x0E-\x1F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(? + /// Gets the NFC form of text for writing to XML, without the characters XML cannot + /// represent. They are removed before normalizing too, because ICU cannot encode an + /// unpaired surrogate. + /// + internal static string GetXmlSafeText(string text) + { + string stripped = StripInvalidXmlChars(text); + if (stripped.Length != text.Length) + Debug.WriteLine("TsStringSerializer: characters XML cannot represent were removed from text being written."); + return StripInvalidXmlChars(Normalizer.Normalize(stripped, Normalizer.UNormalizationMode.UNORM_NFC)); + } + #endregion #region Serialization Helper Methods diff --git a/tests/SIL.LCModel.Core.Tests/Text/TextPropsRoundTripTests.cs b/tests/SIL.LCModel.Core.Tests/Text/TextPropsRoundTripTests.cs new file mode 100644 index 00000000..44491435 --- /dev/null +++ b/tests/SIL.LCModel.Core.Tests/Text/TextPropsRoundTripTests.cs @@ -0,0 +1,791 @@ +// Copyright (c) 2026 SIL International +// This software is licensed under the LGPL, version 2.1 or later +// (http://www.gnu.org/licenses/lgpl-2.1.html) + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading; +using System.Xml.Schema; +using NUnit.Framework; +using SIL.LCModel.Core.KernelInterfaces; +using SIL.LCModel.Utils; + +namespace SIL.LCModel.Core.Text +{ + /// + /// Checks that the text property XML writer and readers agree: every text property liblcm + /// defines (every member of ) either survives a write then read + /// through both (StyleRules) and + /// (string runs), or is deliberately not persisted, in which case the writer must leave it + /// out. Whatever the writer emits, the reader must load: a record the reader rejects is + /// reported as corrupted and cannot be opened. + /// + [TestFixture] + public class TextPropsRoundTripTests : TsSerializerTestsBase + { + #region Property classification + // Ws handles are only known once the fixture's writing system manager exists, so the + // ws values are produced lazily. + private const int EnWsMarker = int.MinValue + 1; + private const int EsWsMarker = int.MinValue + 2; + + private static readonly int[] Toggles = + { (int)FwTextToggleVal.kttvOff, (int)FwTextToggleVal.kttvForceOn, (int)FwTextToggleVal.kttvInvert }; + private static readonly int[] ComBools = { 0, 1 }; + private static readonly int[] Lengths = { 0, 1, 12000, int.MaxValue }; + private static readonly int[] SignedLengths = { int.MinValue, -12000, -1, 0, 1, 12000, int.MaxValue }; + private static readonly int[] Counts = { 0, 1, 7, int.MaxValue }; + private static readonly int[] Colors = + { + (int)FwTextColor.kclrWhite, (int)FwTextColor.kclrBlack, (int)FwTextColor.kclrRed, + (int)FwTextColor.kclrGreen, (int)FwTextColor.kclrBlue, (int)FwTextColor.kclrYellow, + (int)FwTextColor.kclrMagenta, (int)FwTextColor.kclrCyan, (int)FwTextColor.kclrTransparent, + 0x05F1F8, 0x000001 + }; + + private sealed class IntCase + { + public FwTextPropVar Var; + public int[] Values; + } + + private static IntCase Int(FwTextPropVar var, params int[] values) + { + return new IntCase { Var = var, Values = values }; + } + + /// + /// Integer properties that persist, keyed by constant name, with the variation the reader + /// assigns (the XML does not store a variation for most properties) and representative + /// valid values, including boundaries and every enumeration value. + /// + private static readonly Dictionary IntProps = new Dictionary + { + { nameof(FwTextPropType.ktptWs), new[] { Int(FwTextPropVar.ktpvDefault, EnWsMarker, EsWsMarker) } }, + { nameof(FwTextPropType.ktptItalic), new[] { Int(FwTextPropVar.ktpvEnum, Toggles) } }, + { nameof(FwTextPropType.ktptBold), new[] { Int(FwTextPropVar.ktpvEnum, Toggles) } }, + { nameof(FwTextPropType.ktptSuperscript), new[] { Int(FwTextPropVar.ktpvEnum, + (int)FwSuperscriptVal.kssvOff, (int)FwSuperscriptVal.kssvSuper, (int)FwSuperscriptVal.kssvSub) } }, + { nameof(FwTextPropType.ktptUnderline), new[] { Int(FwTextPropVar.ktpvEnum, + Enum.GetValues(typeof(FwUnderlineType)).Cast().Where(v => v < (int)FwUnderlineType.kuntLim).Distinct().ToArray()) } }, + { nameof(FwTextPropType.ktptFontSize), new[] { Int(FwTextPropVar.ktpvMilliPoint, Lengths), + Int(FwTextPropVar.ktpvRelative, 0, 5000, int.MaxValue) } }, + { nameof(FwTextPropType.ktptOffset), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths), + Int(FwTextPropVar.ktpvRelative, -5000, 0, 5000) } }, + { nameof(FwTextPropType.ktptForeColor), new[] { Int(FwTextPropVar.ktpvDefault, Colors) } }, + { nameof(FwTextPropType.ktptBackColor), new[] { Int(FwTextPropVar.ktpvDefault, Colors) } }, + { nameof(FwTextPropType.ktptUnderColor), new[] { Int(FwTextPropVar.ktpvDefault, Colors) } }, + { nameof(FwTextPropType.ktptBaseWs), new[] { Int(FwTextPropVar.ktpvDefault, EnWsMarker, EsWsMarker) } }, + { nameof(FwTextPropType.ktptAlign), new[] { Int(FwTextPropVar.ktpvEnum, + Enum.GetValues(typeof(FwTextAlign)).Cast().Where(v => v < (int)FwTextAlign.ktalLim).Distinct().ToArray()) } }, + { nameof(FwTextPropType.ktptFirstIndent), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptLeadingIndent), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptTrailingIndent), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptSpaceBefore), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptSpaceAfter), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptLineHeight), new[] { + // negative millipoint means "exact", positive "at least" + Int(FwTextPropVar.ktpvMilliPoint, int.MinValue, -12000, -1, 0, 1, 12000, int.MaxValue), + Int(FwTextPropVar.ktpvRelative, 0, 10000, 15000, int.MaxValue) } }, + { nameof(FwTextPropType.ktptParaColor), new[] { Int(FwTextPropVar.ktpvDefault, Colors) } }, + { nameof(FwTextPropType.ktptSpellCheck), new[] { Int(FwTextPropVar.ktpvEnum, + (int)SpellingModes.ksmNormalCheck, (int)SpellingModes.ksmDoNotCheck, (int)SpellingModes.ksmForceCheck) } }, + { nameof(FwTextPropType.ktptMarginTop), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptRightToLeft), new[] { Int(FwTextPropVar.ktpvEnum, ComBools) } }, + { nameof(FwTextPropType.ktptDirectionDepth), new[] { Int(FwTextPropVar.ktpvDefault, Counts) } }, + { nameof(FwTextPropType.ktptPadLeading), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptPadTrailing), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptPadTop), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptPadBottom), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptBorderTop), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptBorderBottom), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptBorderLeading), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptBorderTrailing), new[] { Int(FwTextPropVar.ktpvMilliPoint, SignedLengths) } }, + { nameof(FwTextPropType.ktptBorderColor), new[] { Int(FwTextPropVar.ktpvDefault, Colors) } }, + { nameof(FwTextPropType.ktptBulNumScheme), new[] { Int(FwTextPropVar.ktpvEnum, 0, 1, 10, 101, int.MaxValue) } }, + // int.MinValue is written as 0 on purpose (it means "not set"), so it is not a round-trip value. + { nameof(FwTextPropType.ktptBulNumStartAt), new[] { Int(FwTextPropVar.ktpvDefault, -1, 0, 1, 99, int.MaxValue) } }, + { nameof(FwTextPropType.ktptKeepWithNext), new[] { Int(FwTextPropVar.ktpvEnum, ComBools) } }, + { nameof(FwTextPropType.ktptKeepTogether), new[] { Int(FwTextPropVar.ktpvEnum, ComBools) } }, + { nameof(FwTextPropType.ktptHyphenate), new[] { Int(FwTextPropVar.ktpvEnum, ComBools) } }, + { nameof(FwTextPropType.ktptMaxLines), new[] { Int(FwTextPropVar.ktpvDefault, Counts) } }, + { nameof(FwTextPropType.ktptCellBorderWidth), new[] { Int(FwTextPropVar.ktpvMilliPoint, Lengths) } }, + { nameof(FwTextPropType.ktptCellSpacing), new[] { Int(FwTextPropVar.ktpvMilliPoint, Lengths) } }, + { nameof(FwTextPropType.ktptCellPadding), new[] { Int(FwTextPropVar.ktpvMilliPoint, Lengths) } }, + { nameof(FwTextPropType.ktptSetRowDefaults), new[] { Int(FwTextPropVar.ktpvEnum, ComBools) } }, + { nameof(FwTextPropType.ktptRelLineHeight), new[] { Int(FwTextPropVar.ktpvMilliPoint, 0, 10000, int.MaxValue) } }, + { nameof(FwTextPropType.ktptWidowOrphanControl), new[] { Int(FwTextPropVar.ktpvEnum, ComBools) } }, + }; + + /// + /// String properties that persist, keyed by constant name, with representative values. + /// Produced lazily because some values need the fixture's writing systems. + /// + private Dictionary StrProps() + { + Guid g1 = new Guid("397F43AE-E2B2-4f20-928A-1DF193C07674"); + Guid g2 = new Guid("85EE15C6-0799-46c6-8769-F9B3CE313AE2"); + Guid g3 = new Guid("644DF48A-3B60-45f4-80C7-739BE6E56A96"); + string ObjData(FwObjDataTypes type, Guid guid) => (char)type + MiscUtils.GetObjDataFromGuid(guid); + return new Dictionary + { + { nameof(FwTextPropType.ktptFontFamily), new[] { "Times New Roman", "", "Charis SIL" } }, + { nameof(FwTextPropType.ktptTags), new[] { + MiscUtils.GetObjDataFromGuid(g1), + MiscUtils.GetObjDataFromGuid(g1) + MiscUtils.GetObjDataFromGuid(g2) + MiscUtils.GetObjDataFromGuid(g3) } }, + { nameof(FwTextPropType.ktptObjData), new[] { + ObjData(FwObjDataTypes.kodtNameGuidHot, g1), + ObjData(FwObjDataTypes.kodtOwnNameGuidHot, g2), + ObjData(FwObjDataTypes.kodtContextString, g3), + ObjData(FwObjDataTypes.kodtGuidMoveableObjDisp, g1), + (char)FwObjDataTypes.kodtExternalPathName + "https://example.org/a b.html", + (char)FwObjDataTypes.kodtEmbeddedObjectData + "" } }, + { nameof(FwTextPropType.ktptCustomBullet), new[] { "»", "*", "→ " } }, + { nameof(FwTextPropType.ktptFontVariations), new[] { "smcp=1,liga=0", "185=1,851=2,57=12" } }, + { nameof(FwTextPropType.ktptNamedStyle), new[] { "Normal", "Emphasized Text" } }, + { nameof(FwTextPropType.ktptBulNumTxtBef), new[] { "(", "Chapter " } }, + { nameof(FwTextPropType.ktptBulNumTxtAft), new[] { ")", ". " } }, + { nameof(FwTextPropType.ktptBulNumFontInfo), new[] { + // ints only, in the order the reader builds them (by property number) + Bul(FwTextPropType.ktptItalic, 1) + Bul(FwTextPropType.ktptBold, 1) + Bul(FwTextPropType.ktptSuperscript, 2) + + Bul(FwTextPropType.ktptUnderline, 2) + Bul(FwTextPropType.ktptFontSize, 20000) + Bul(FwTextPropType.ktptOffset, -3000) + + Bul(FwTextPropType.ktptForeColor, 0xFF0000) + Bul(FwTextPropType.ktptBackColor, 0xFFFFFF) + + Bul(FwTextPropType.ktptUnderColor, 0x0000FF), + BulStr(FwTextPropType.ktptFontFamily, "Times New Roman"), + // BulletInfo writes font name then font features (LT-22351) + Bul(FwTextPropType.ktptBold, 1) + BulStr(FwTextPropType.ktptFontFamily, "Charis SIL") + + BulStr(FwTextPropType.ktptFontVariations, "smcp=1"), + BulStr(FwTextPropType.ktptFontVariations, "smcp=1"), + // an integer property with no attribute name of its own is written as prop_ + Bul(FwTextPropType.ktptBold, 1) + Bul(FwTextPropType.ktptBorderTop, 500) } }, + { nameof(FwTextPropType.ktptWsStyle), new[] { + WsStyle(EnWS, "", null, WsInt(FwTextPropType.ktptFontSize, FwTextPropVar.ktpvMilliPoint, 80), + WsInt(FwTextPropType.ktptForeColor, FwTextPropVar.ktpvDefault, 0xFF602F)), + WsStyle(EnWS, "Times New Roman", "185=1,851=2,57=12", + WsInt(FwTextPropType.ktptFontSize, FwTextPropVar.ktpvMilliPoint, 80)), + WsStyle(EnWS, "Charis SIL", null, WsInt(FwTextPropType.ktptBold, FwTextPropVar.ktpvEnum, 1)) + + WsStyle(EsWS, "Doulos SIL", null, WsInt(FwTextPropType.ktptOffset, FwTextPropVar.ktpvMilliPoint, -2000)) } }, + }; + } + + /// Properties persisted in StyleRules only; a string run has no place for them. + private static readonly HashSet StyleRulesOnly = new HashSet + { + nameof(FwTextPropType.ktptBulNumFontInfo), // written as a child element of + nameof(FwTextPropType.ktptWsStyle), // written as a child element of + }; + + /// + /// Properties deliberately not persisted. The writer must leave them out (and a reader + /// must still load an old file that has them, see ). + /// + private static readonly Dictionary NotPersisted = new Dictionary + { + { nameof(FwTextPropType.ktptCharStyle), (true, "obsolete, superseded by namedStyle; the reader has always ignored it") }, + { nameof(FwTextPropType.ktptParaStyle), (true, "obsolete, superseded by namedStyle; the reader has always ignored it") }, + { nameof(FwTextPropType.ktptTabList), (true, "tabs are not supported; the reader has always ignored it") }, + { nameof(FwTextPropType.ktptTabDef), (false, "tabs are not supported; the reader has always ignored it") }, + { nameof(FwTextPropType.ktptEditable), (false, "a view-construction property; the run reader has always ignored it, so persisting it would make existing text read-only") }, + { nameof(FwTextPropType.ktptTableRule), (false, "a view-construction property never written; a new attribute name would make older readers reject the record") }, + }; + + /// Members that are not stored as text property attributes at all. + private static readonly Dictionary NotAttributes = new Dictionary + { + { nameof(FwTextPropType.ktptFieldName), "export-only: written as a element around runs" }, + { nameof(FwTextPropType.ktptMarkItem), "export-only: written as an element around a run" }, + }; + + /// Constants that share a number with another constant of the same kind. + private static readonly Dictionary Aliases = new Dictionary + { + { nameof(FwTextPropType.ktptMarginLeading), nameof(FwTextPropType.ktptLeadingIndent) }, + { nameof(FwTextPropType.ktptMarginTrailing), nameof(FwTextPropType.ktptTrailingIndent) }, + { nameof(FwTextPropType.ktptMswMarginTop), nameof(FwTextPropType.ktptSpaceBefore) }, + { nameof(FwTextPropType.ktptMarginBottom), nameof(FwTextPropType.ktptSpaceAfter) }, + }; + #endregion + + #region Completeness + /// + /// Every constant liblcm defines must be classified, so a new text property cannot be + /// added without deciding (and testing) how it persists. + /// + [Test] + public void EveryTextPropertyIsClassified() + { + var strNames = StrProps().Keys; + var unclassified = Enum.GetNames(typeof(FwTextPropType)) + .Where(n => !IntProps.ContainsKey(n) && !strNames.Contains(n) && !NotPersisted.ContainsKey(n) + && !NotAttributes.ContainsKey(n) && !Aliases.ContainsKey(n)) + .ToList(); + Assert.That(unclassified, Is.Empty, + "Text properties with no round-trip test; add them to IntProps/StrProps (or NotPersisted with a reason)"); + foreach (KeyValuePair alias in Aliases) + Assert.That(Enum.Parse(typeof(FwTextPropType), alias.Key), Is.EqualTo(Enum.Parse(typeof(FwTextPropType), alias.Value))); + } + #endregion + + #region Round trip of every valid value + /// One case per integer property, variation and value. + public static IEnumerable IntCases() + { + foreach (KeyValuePair prop in IntProps) + { + foreach (IntCase c in prop.Value) + { + foreach (int value in c.Values) + yield return new TestCaseData(prop.Key, c.Var, value).SetName($"{{m}}({prop.Key},{c.Var},{ValueName(value)})"); + } + } + } + + /// One case per string property and value (values indexed; see StrProps). + public static IEnumerable StrCases() + { + var fixture = new TextPropsRoundTripTests(); + // The writing system handles are not needed to count the cases. + foreach (KeyValuePair prop in fixture.StrPropsForEnumeration()) + { + for (int i = 0; i < prop.Value.Length; i++) + yield return new TestCaseData(prop.Key, i).SetName($"{{m}}({prop.Key},{i})"); + } + } + + private Dictionary StrPropsForEnumeration() + { + // EnWS/EsWS are 0 before fixture setup; the values built here are only counted. + return StrProps(); + } + + [TestCaseSource(nameof(IntCases))] + public void IntProp_RoundTripsThroughStyleRules(string name, FwTextPropVar var, int value) + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)Tpt(name), (int)var, ResolveWs(value)); + AssertStyleRulesRoundTrip(bldr.GetTextProps()); + } + + [TestCaseSource(nameof(IntCases))] + public void IntProp_RoundTripsThroughStringRun(string name, FwTextPropVar var, int value) + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + bldr.SetIntPropValues((int)Tpt(name), (int)var, ResolveWs(value)); + AssertRunRoundTrip(bldr.GetTextProps(), "text"); + } + + [TestCaseSource(nameof(StrCases))] + public void StrProp_RoundTripsThroughStyleRules(string name, int index) + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetStrPropValue((int)Tpt(name), StrProps()[name][index]); + AssertStyleRulesRoundTrip(bldr.GetTextProps()); + } + + [TestCaseSource(nameof(StrCases))] + public void StrProp_RoundTripsThroughStringRun(string name, int index) + { + if (StyleRulesOnly.Contains(name)) + { + // Not representable on a run: the writer leaves it out, and the string loads. + ITsPropsBldr b = TsStringUtils.MakePropsBldr(); + b.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + b.SetStrPropValue((int)Tpt(name), StrProps()[name][index]); + ITsString tss = RunRoundTrip(b.GetTextProps(), "text", out _); + Assert.That(tss.get_Properties(0).StrPropCount, Is.EqualTo(0)); + return; + } + string value = StrProps()[name][index]; + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + bldr.SetStrPropValue((int)Tpt(name), value); + string text = "text"; + if (name == nameof(FwTextPropType.ktptObjData)) + { + switch ((FwObjDataTypes)value[0]) + { + case FwObjDataTypes.kodtNameGuidHot: + case FwObjDataTypes.kodtOwnNameGuidHot: + case FwObjDataTypes.kodtContextString: + case FwObjDataTypes.kodtGuidMoveableObjDisp: + text = StringUtils.kszObject; + break; + } + } + AssertRunRoundTrip(bldr.GetTextProps(), text); + } + + [Test] + public void AllPropertiesAtOnce_RoundTripThroughStyleRules() + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + foreach (KeyValuePair prop in IntProps) + { + IntCase c = prop.Value[0]; + bldr.SetIntPropValues((int)Tpt(prop.Key), (int)c.Var, ResolveWs(c.Values[c.Values.Length - 1])); + } + foreach (KeyValuePair prop in StrProps()) + bldr.SetStrPropValue((int)Tpt(prop.Key), prop.Value[prop.Value.Length - 1]); + AssertStyleRulesRoundTrip(bldr.GetTextProps()); + } + #endregion + + #region Not persisted + public static IEnumerable NotPersistedCases() + { + foreach (string name in NotPersisted.Keys) + yield return new TestCaseData(name).SetName($"NotPersisted_IsLeftOutAndLoads({name})"); + } + + [TestCaseSource(nameof(NotPersistedCases))] + public void NotPersisted_IsLeftOutAndLoads(string name) + { + FwTextPropType tpt = Tpt(name); + bool isString = NotPersisted[name].IsString; + + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + if (isString) + bldr.SetStrPropValue((int)tpt, "x"); + else + bldr.SetIntPropValues((int)tpt, (int)FwTextPropVar.ktpvEnum, 1); + ITsTextProps props = bldr.GetTextProps(); + + string rulesXml = TsPropsSerializer.SerializePropsToXml(props, WritingSystemManager); + ITsTextProps rules = TsPropsSerializer.DeserializePropsFromXml(rulesXml, WritingSystemManager); + Assert.That(rules.IntPropCount, Is.EqualTo(1), rulesXml); + Assert.That(rules.StrPropCount, Is.EqualTo(0), rulesXml); + + ITsString run = RunRoundTrip(props, "text", out string runXml); + Assert.That(run.get_Properties(0).IntPropCount, Is.EqualTo(1), runXml); + Assert.That(run.get_Properties(0).StrPropCount, Is.EqualTo(0), runXml); + } + #endregion + + #region Old files + /// + /// Attribute forms that earlier versions of liblcm wrote must still load. + /// + [TestCase("MarginTop='1500'", FwTextPropType.ktptMarginTop, 1500, FwTextPropVar.ktpvMilliPoint)] + [TestCase("marginTop='1500'", FwTextPropType.ktptMarginTop, 1500, FwTextPropVar.ktpvMilliPoint)] + [TestCase("leadingIndent='-1000'", FwTextPropType.ktptLeadingIndent, -1000, FwTextPropVar.ktpvMilliPoint)] + [TestCase("padTop='4294967295'", FwTextPropType.ktptPadTop, -1, FwTextPropVar.ktpvMilliPoint)] + [TestCase("bold='7'", FwTextPropType.ktptBold, 7, FwTextPropVar.ktpvEnum)] + [TestCase("align='99'", FwTextPropType.ktptAlign, 99, FwTextPropVar.ktpvEnum)] + [TestCase("offset='-3000' offsetUnit='mpt'", FwTextPropType.ktptOffset, -3000, FwTextPropVar.ktpvMilliPoint)] + [TestCase("offset='5' offsetUnit='0'", FwTextPropType.ktptOffset, 5, FwTextPropVar.ktpvDefault)] + [TestCase("hyphenate='1'", FwTextPropType.ktptHyphenate, 1, FwTextPropVar.ktpvEnum)] + [TestCase("cellPadding='300'", FwTextPropType.ktptCellPadding, 300, FwTextPropVar.ktpvMilliPoint)] + public void OldFileIntAttribute_Loads(string attrs, FwTextPropType tpt, int value, FwTextPropVar var) + { + ITsTextProps rules = TsPropsSerializer.DeserializePropsFromXml($"", WritingSystemManager); + Assert.That(rules.GetIntPropValues((int)tpt, out int rulesVar), Is.EqualTo(value)); + Assert.That(rulesVar, Is.EqualTo((int)var)); + + ITsString tss = TsStringSerializer.DeserializeTsStringFromXml($"text", WritingSystemManager); + Assert.That(tss.get_Properties(0).GetIntPropValues((int)tpt, out int runVar), Is.EqualTo(value)); + Assert.That(runVar, Is.EqualTo((int)var)); + } + + /// + /// Legacy attributes that earlier versions wrote but that are not persisted any more + /// must load (and be ignored) in both readers. + /// + [TestCase("tabList='abc'")] + [TestCase("tabDef='36000'")] + [TestCase("charStyle='Emphasis'")] + [TestCase("paraStyle='Normal'")] + [TestCase("editable='not'")] + public void OldFileAttributes_Load(string attrs) + { + ITsTextProps rules = TsPropsSerializer.DeserializePropsFromXml($"", WritingSystemManager); + Assert.That(rules.IntPropCount + rules.StrPropCount, Is.EqualTo(0)); + + ITsString tss = TsStringSerializer.DeserializeTsStringFromXml($"text", WritingSystemManager); + Assert.That(tss.get_Properties(0).IntPropCount, Is.EqualTo(1)); // ws only + Assert.That(tss.get_Properties(0).StrPropCount, Is.EqualTo(0)); + } + + [Test] + public void MarginTop_IsWrittenWithTheNameOlderReadersAccept() + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptMarginTop, (int)FwTextPropVar.ktpvMilliPoint, 1500); + string xml = TsPropsSerializer.SerializePropsToXml(bldr.GetTextProps(), WritingSystemManager); + Assert.That(StripNewLines(xml), Is.EqualTo("")); + } + #endregion + + #region Values outside the valid range + /// + /// Whatever value a property holds, the writer must not produce XML the reader rejects: + /// a rejected record cannot be opened. Values outside a property's documented range are + /// tolerated by the reader (and kept), malformed binary values are left out by the writer. + /// + public static IEnumerable AnyIntValueCases() + { + int[] values = { int.MinValue, -12000, -1, 0, 1, 2, 3, 7, 99, 0xFFFFFF, 0x7F000000, int.MaxValue }; + FwTextPropVar[] vars = { FwTextPropVar.ktpvDefault, FwTextPropVar.ktpvMilliPoint, FwTextPropVar.ktpvRelative, FwTextPropVar.ktpvEnum }; + IEnumerable numbers = Enum.GetValues(typeof(FwTextPropType)).Cast().Distinct() + .Where(n => n != (int)FwTextPropType.ktptWs && n != (int)FwTextPropType.ktptBaseWs) + .Concat(new[] { 1000, 30000 }); + foreach (int tpt in numbers) + { + foreach (FwTextPropVar var in vars) + yield return new TestCaseData(tpt, var, values).SetName($"AnyIntValue_Loads({tpt},{var})"); + } + } + + [TestCase(FwTextPropType.ktptWs)] + [TestCase(FwTextPropType.ktptBaseWs)] + public void AnyWsValue_Loads(FwTextPropType tpt) + { + // An unknown handle is not covered: it is written as ws="", which the readers reject. + foreach (int value in new[] { EnWS, EsWS, 0, -1, int.MinValue }) + { + foreach (FwTextPropVar var in new[] { FwTextPropVar.ktpvDefault, FwTextPropVar.ktpvEnum }) + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + try + { + bldr.SetIntPropValues((int)tpt, (int)var, value); + } + catch (ArgumentOutOfRangeException) + { + continue; // text props cannot hold this value, so it can never be written + } + AssertWrittenXmlLoads(bldr.GetTextProps(), $"{tpt}, var {var}, value {value}"); + } + } + } + + [TestCaseSource(nameof(AnyIntValueCases))] + public void AnyIntValue_Loads(int tpt, FwTextPropVar var, int[] values) + { + foreach (int value in values) + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + bldr.SetIntPropValues(tpt, (int)var, value); + AssertWrittenXmlLoads(bldr.GetTextProps(), $"tpt {tpt}, var {var}, value {value}"); + } + } + + public static IEnumerable AnyStrValueCases() + { + string[] values = + { + "x", "ab", "abc", "\u0001", "\u0001\u0002\u0003", "\u0006\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008", + "\u0003\u0001", "\u0003\u0001\u0002\u0003\u0004\u0005\u0006\u0007", "\uFFFF\uFFFF\uFFFF", "\u0002\u0002\u0002\u0002", + "a\uD800b", "\uDC00x", "x\uD800", "\U0001F600 paired" + }; + IEnumerable numbers = Enum.GetValues(typeof(FwTextPropType)).Cast().Distinct().Concat(new[] { 1000, 30000 }); + foreach (int tpt in numbers) + yield return new TestCaseData(tpt, values).SetName($"AnyStrValue_Loads({tpt})"); + } + + [TestCaseSource(nameof(AnyStrValueCases))] + public void AnyStrValue_Loads(int tpt, string[] values) + { + foreach (string value in values) + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + bldr.SetStrPropValue(tpt, value); + AssertWrittenXmlLoads(bldr.GetTextProps(), $"tpt {tpt}, value {Escape(value)}"); + } + } + + [Test] + public void NegativeValue_RoundTripsWhenCultureRedefinesNegativeSign() + { + CultureInfo oldCulture = Thread.CurrentThread.CurrentCulture; + try + { + var culture = new CultureInfo("fi"); + culture.NumberFormat.NegativeSign = "+"; + Thread.CurrentThread.CurrentCulture = culture; + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + bldr.SetIntPropValues((int)FwTextPropType.ktptLeadingIndent, (int)FwTextPropVar.ktpvMilliPoint, -1000); + bldr.SetIntPropValues((int)FwTextPropType.ktptOffset, (int)FwTextPropVar.ktpvMilliPoint, -3000); + bldr.SetIntPropValues((int)FwTextPropType.ktptLineHeight, (int)FwTextPropVar.ktpvMilliPoint, -12000); + AssertStyleRulesRoundTrip(bldr.GetTextProps()); + AssertRunRoundTrip(bldr.GetTextProps(), "text"); + } + finally + { + Thread.CurrentThread.CurrentCulture = oldCulture; + } + } + + [Test] + public void NegativeRelativeLineHeight_IsWrittenWithoutSign() + { + // Older readers reject a negative line height, and a negative relative height means + // nothing to Views, so the writer keeps writing the absolute value as it always has. + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptLineHeight, (int)FwTextPropVar.ktpvRelative, -5000); + string xml = TsPropsSerializer.SerializePropsToXml(bldr.GetTextProps(), WritingSystemManager); + Assert.That(StripNewLines(xml), Is.EqualTo("")); + } + + // Built in code: attribute arguments cannot carry an unpaired surrogate (it becomes U+FFFD). + private static readonly string[][] SurrogateCases = + { + new[] { "a\uD800b", "ab" }, + new[] { "\uDC00a", "a" }, + new[] { "a\uD800", "a" }, + new[] { "a\uDC00\uD800b", "ab" }, + new[] { "a\U0001F600b", "a\U0001F600b" }, + }; + + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + [TestCase(3)] + [TestCase(4)] + public void UnpairedSurrogates_AreStrippedFromStringProperties(int index) + { + string value = SurrogateCases[index][0], expected = SurrogateCases[index][1]; + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetStrPropValue((int)FwTextPropType.ktptCustomBullet, value); + string xml = TsPropsSerializer.SerializePropsToXml(bldr.GetTextProps(), WritingSystemManager); + ITsTextProps props = TsPropsSerializer.DeserializePropsFromXml(xml, WritingSystemManager); + Assert.That(props.GetStrPropValue((int)FwTextPropType.ktptCustomBullet), Is.EqualTo(expected)); + } + + /// + /// A string property whose value is empty, or becomes empty once the characters XML cannot hold are + /// removed, is stored as null by liblcm. It must write, read back and write again without throwing. + /// + public static IEnumerable EmptyStrValueCases() + { + foreach (int tpt in Enum.GetValues(typeof(FwTextPropType)).Cast().Distinct()) + yield return new TestCaseData(tpt).SetName($"{{m}}({tpt})"); + } + + private static readonly int[] PlainTextStrProps = + { + (int)FwTextPropType.ktptFontFamily, (int)FwTextPropType.ktptNamedStyle, (int)FwTextPropType.ktptBulNumTxtBef, + (int)FwTextPropType.ktptBulNumTxtAft, (int)FwTextPropType.ktptCustomBullet, (int)FwTextPropType.ktptFontVariations + }; + + [TestCaseSource(nameof(EmptyStrValueCases))] + public void EmptyStrValue_SavesAgainThroughStyleRules(int tpt) + { + foreach (string value in new[] { string.Empty, "\u0001" }) + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetStrPropValue(tpt, value); + string xml = null; + Assert.DoesNotThrow(() => + { + xml = TsPropsSerializer.SerializePropsToXml(bldr.GetTextProps(), WritingSystemManager); + ITsTextProps back = TsPropsSerializer.DeserializePropsFromXml(xml, WritingSystemManager); + TsPropsSerializer.SerializePropsToXml(back, WritingSystemManager); + if (Array.IndexOf(PlainTextStrProps, tpt) >= 0) + Assert.That(back.GetStrPropValue(tpt), Is.Null.Or.Empty); + }, $"tpt {tpt}, value {Escape(value)}, xml {xml}"); + } + } + + [TestCaseSource(nameof(EmptyStrValueCases))] + public void EmptyStrValue_SavesAgainThroughStringRun(int tpt) + { + foreach (string value in new[] { string.Empty, "\u0001" }) + { + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, EnWS); + bldr.SetStrPropValue(tpt, value); + string xml = null; + Assert.DoesNotThrow(() => + { + ITsString back = RunRoundTrip(bldr.GetTextProps(), "text", out xml); + TsStringSerializer.SerializeTsStringToXml(back, WritingSystemManager); + if (Array.IndexOf(PlainTextStrProps, tpt) >= 0) + Assert.That(back.get_Properties(0).GetStrPropValue(tpt), Is.Null.Or.Empty); + }, $"tpt {tpt}, value {Escape(value)}, xml {xml}"); + } + } + + [Test] + public void BulNumFontInfo_PropertyNumberAboveCharRange_IsSkipped() + { + // Only hand-made data has this; storing it would renumber it to another property. + ITsTextProps props = TsPropsSerializer.DeserializePropsFromXml( + "", WritingSystemManager); + Assert.That(props.GetStrPropValue((int)FwTextPropType.ktptBulNumFontInfo), + Is.EqualTo(Bul(FwTextPropType.ktptBold, 1) + Bul(FwTextPropType.ktptBorderTop, 500))); + } + + [TestCase("-1")] + [TestCase("-3")] + public void NegativeUnit_IsRejected(string unit) + { + // No writer emits a negative variation, and setting one would remove the property. + Assert.That(() => TsPropsSerializer.DeserializePropsFromXml($"", WritingSystemManager), + Throws.TypeOf()); + } + + [Test] + public void PartialTags_AreLeftOutAndLoad() + { + // Fewer characters than one GUID needs: nothing to write. + ITsPropsBldr bldr = TsStringUtils.MakePropsBldr(); + bldr.SetStrPropValue((int)FwTextPropType.ktptTags, "abc"); + string xml = TsPropsSerializer.SerializePropsToXml(bldr.GetTextProps(), WritingSystemManager); + ITsTextProps props = TsPropsSerializer.DeserializePropsFromXml(xml, WritingSystemManager); + Assert.That(props.StrPropCount, Is.EqualTo(0), xml); + // Re-save must work too. + TsPropsSerializer.SerializePropsToXml(props, WritingSystemManager); + } + #endregion + + #region Helpers + private static FwTextPropType Tpt(string name) + { + return (FwTextPropType)Enum.Parse(typeof(FwTextPropType), name); + } + + private int ResolveWs(int value) + { + if (value == EnWsMarker) + return EnWS; + if (value == EsWsMarker) + return EsWS; + return value; + } + + private static string ValueName(int value) + { + if (value == EnWsMarker) + return "en"; + if (value == EsWsMarker) + return "es"; + return value.ToString(); + } + + private static string Escape(string value) + { + return string.Concat(value.Select(c => c < 0x20 || c > 0x7E ? $"\\u{(int)c:X4}" : c.ToString())); + } + + private static string Bul(FwTextPropType tpt, int value) + { + return new string(new[] { (char)tpt, (char)(value & 0xFFFF), (char)((value >> 16) & 0xFFFF) }); + } + + private static string BulStr(FwTextPropType tpt, string value) + { + return (char)tpt + value + "\u0000"; + } + + private static string WsInt(FwTextPropType tpt, FwTextPropVar var, int value) + { + return new string(new[] { (char)tpt, (char)var, (char)(value & 0xFFFF), (char)((value >> 16) & 0xFFFF) }); + } + + private static string WsStyle(int ws, string fontFamily, string fontVariations, params string[] intProps) + { + var sb = new StringBuilder(); + sb.Append((char)(ws & 0xFFFF)).Append((char)((ws >> 16) & 0xFFFF)); + sb.Append((char)fontFamily.Length).Append(fontFamily); + if (fontVariations != null) + { + sb.Append(unchecked((char)-1)); + sb.Append((char)FwTextPropType.ktptFontVariations).Append((char)fontVariations.Length).Append(fontVariations); + } + sb.Append((char)intProps.Length); + foreach (string p in intProps) + sb.Append(p); + return sb.ToString(); + } + + private void AssertStyleRulesRoundTrip(ITsTextProps expected) + { + string xml = TsPropsSerializer.SerializePropsToXml(expected, WritingSystemManager); + ITsTextProps actual; + try + { + actual = TsPropsSerializer.DeserializePropsFromXml(xml, WritingSystemManager); + } + catch (Exception e) + { + Assert.Fail($"StyleRules written by liblcm failed to load ({e.GetType().Name}: {e.Message}): {xml}"); + return; + } + AssertSameProps(expected, actual, xml); + } + + private ITsString RunRoundTrip(ITsTextProps props, string text, out string xml) + { + ITsStrBldr strBldr = TsStringUtils.MakeStrBldr(); + strBldr.Replace(0, 0, text, props); + xml = TsStringSerializer.SerializeTsStringToXml(strBldr.GetString(), WritingSystemManager); + try + { + return TsStringSerializer.DeserializeTsStringFromXml(xml, WritingSystemManager); + } + catch (Exception e) + { + Assert.Fail($"String written by liblcm failed to load ({e.GetType().Name}: {e.Message}): {xml}"); + return null; + } + } + + private void AssertRunRoundTrip(ITsTextProps expected, string text) + { + ITsString actual = RunRoundTrip(expected, text, out string xml); + Assert.That(actual.RunCount, Is.EqualTo(1), xml); + Assert.That(actual.Text, Is.EqualTo(text), xml); + AssertSameProps(expected, actual.get_Properties(0), xml); + } + + private void AssertWrittenXmlLoads(ITsTextProps props, string what) + { + string rulesXml = null, runXml = null; + try + { + rulesXml = TsPropsSerializer.SerializePropsToXml(props, WritingSystemManager); + TsPropsSerializer.DeserializePropsFromXml(rulesXml, WritingSystemManager); + ITsStrBldr strBldr = TsStringUtils.MakeStrBldr(); + strBldr.Replace(0, 0, "text", props); + runXml = TsStringSerializer.SerializeTsStringToXml(strBldr.GetString(), WritingSystemManager); + TsStringSerializer.DeserializeTsStringFromXml(runXml, WritingSystemManager); + } + catch (Exception e) + { + Assert.Fail($"{what}: {e.GetType().Name}: {e.Message}\nStyleRules: {rulesXml}\nRun: {runXml}"); + } + } + + private static void AssertSameProps(ITsTextProps expected, ITsTextProps actual, string xml) + { + Assert.That(Describe(actual), Is.EqualTo(Describe(expected)), xml); + } + + private static string Describe(ITsTextProps props) + { + var parts = new List(); + for (int i = 0; i < props.IntPropCount; i++) + { + int value = props.GetIntProp(i, out int tpt, out int var); + parts.Add($"int{tpt}/{var}={value}"); + } + for (int i = 0; i < props.StrPropCount; i++) + { + string value = props.GetStrProp(i, out int tpt); + parts.Add($"str{tpt}={Escape(value)}"); + } + parts.Sort(StringComparer.Ordinal); + return string.Join("; ", parts); + } + #endregion + } +} diff --git a/tests/SIL.LCModel.Core.Tests/Text/TsPropsSerializerTests.cs b/tests/SIL.LCModel.Core.Tests/Text/TsPropsSerializerTests.cs index 9bad1c39..b7dff080 100644 --- a/tests/SIL.LCModel.Core.Tests/Text/TsPropsSerializerTests.cs +++ b/tests/SIL.LCModel.Core.Tests/Text/TsPropsSerializerTests.cs @@ -375,8 +375,8 @@ public void SerializePropsToXml_MarginLeading() [Test] public void SerializePropsToXml_MarginTop() { - CheckSerializeIntProp(FwTextPropType.ktptMarginTop, 0, FwTextPropVar.ktpvMilliPoint, "MarginTop", "0"); - CheckSerializeIntProp(FwTextPropType.ktptMarginTop, 12, FwTextPropVar.ktpvMilliPoint, "MarginTop", "12"); + CheckSerializeIntProp(FwTextPropType.ktptMarginTop, 0, FwTextPropVar.ktpvMilliPoint, "marginTop", "0"); + CheckSerializeIntProp(FwTextPropType.ktptMarginTop, 12, FwTextPropVar.ktpvMilliPoint, "marginTop", "12"); } /// ------------------------------------------------------------------------------------ @@ -790,15 +790,14 @@ public void DeserializePropsFromXml_BorderBottom() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'borderBottom' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'borderBottom' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_BorderBottom_Fail() + public void DeserializePropsFromXml_BorderBottom_Negative() { - Assert.That(() => CheckDeserializeIntProp("borderBottom", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("borderBottom", "-1", FwTextPropType.ktptBorderBottom, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -843,15 +842,14 @@ public void DeserializePropsFromXml_BorderLeading() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'borderLeading' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'borderLeading' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_BorderLeading_Fail() + public void DeserializePropsFromXml_BorderLeading_Negative() { - Assert.That(() => CheckDeserializeIntProp("borderLeading", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("borderLeading", "-1", FwTextPropType.ktptBorderLeading, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -868,15 +866,14 @@ public void DeserializePropsFromXml_BorderTop() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'borderTop' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'borderTop' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_BorderTop_Fail() + public void DeserializePropsFromXml_BorderTop_Negative() { - Assert.That(() => CheckDeserializeIntProp("borderTop", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("borderTop", "-1", FwTextPropType.ktptBorderTop, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -893,15 +890,14 @@ public void DeserializePropsFromXml_BorderTrailing() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'borderTrailing' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'borderTrailing' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_BorderTrailing_Fail() + public void DeserializePropsFromXml_BorderTrailing_Negative() { - Assert.That(() => CheckDeserializeIntProp("borderTrailing", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("borderTrailing", "-1", FwTextPropType.ktptBorderTrailing, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -941,15 +937,14 @@ public void DeserializePropsFromXml_BulNumScheme() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'bulNumScheme' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'bulNumScheme' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_BulNumScheme_Fail() + public void DeserializePropsFromXml_BulNumScheme_Negative() { - Assert.That(() => CheckDeserializeIntProp("bulNumScheme", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("bulNumScheme", "-1", FwTextPropType.ktptBulNumScheme, -1, FwTextPropVar.ktpvEnum); } /// ------------------------------------------------------------------------------------ @@ -966,15 +961,14 @@ public void DeserializePropsFromXml_BulNumStartAt() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'bulNumStartAt' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'bulNumStartAt' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_BulNumStartAt_Fail() + public void DeserializePropsFromXml_BulNumStartAt_Negative() { - Assert.That(() => CheckDeserializeIntProp("bulNumStartAt", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("bulNumStartAt", "-1", FwTextPropType.ktptBulNumStartAt, -1, FwTextPropVar.ktpvDefault); } /// ------------------------------------------------------------------------------------ @@ -1055,16 +1049,15 @@ public void DeserializePropsFromXml_Fontsize() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'fontsize' attribute with an invalid - /// value. + /// Tests the DeserializePropsFromXml method with a 'fontsize' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_Fontsize_Fail1() + public void DeserializePropsFromXml_Fontsize_Negative() { - Assert.That(() => CheckDeserializeIntProp("fontsize='-10' fontsizeUnit='mpt'", - FwTextPropType.ktptFontSize, 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("fontsize='-10' fontsizeUnit='mpt'", + FwTextPropType.ktptFontSize, -10, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1214,15 +1207,14 @@ public void DeserializePropsFromXml_LeadingIndent() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'leadingIndent' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'leadingIndent' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_LeadingIndent_Fail() + public void DeserializePropsFromXml_LeadingIndent_Negative() { - Assert.That(() => CheckDeserializeIntProp("leadingIndent", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("leadingIndent", "-1", FwTextPropType.ktptLeadingIndent, -1, FwTextPropVar.ktpvMilliPoint); } @@ -1244,15 +1236,14 @@ public void DeserializePropsFromXml_LineHeight() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'lineHeight' attribute with an invalid - /// value. + /// Tests the DeserializePropsFromXml method with a negative 'lineHeight' and no type, + /// which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_LineHeight_Fail1() + public void DeserializePropsFromXml_LineHeight_Negative() { - Assert.That(() => CheckDeserializeIntProp("lineHeight='-10' lineHeightUnit='mpt'", FwTextPropType.ktptFontSize, 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("lineHeight='-10' lineHeightUnit='mpt'", FwTextPropType.ktptLineHeight, -10, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1306,15 +1297,14 @@ public void DeserializePropsFromXml_MarginBottom() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'marginBottom' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'marginBottom' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_MarginBottom_Fail() + public void DeserializePropsFromXml_MarginBottom_Negative() { - Assert.That(() => CheckDeserializeIntProp("marginBottom", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("marginBottom", "-1", FwTextPropType.ktptMarginBottom, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1331,15 +1321,14 @@ public void DeserializePropsFromXml_MarginLeading() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'marginLeading' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'marginLeading' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_MarginLeading_Fail() + public void DeserializePropsFromXml_MarginLeading_Negative() { - Assert.That(() => CheckDeserializeIntProp("marginLeading", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("marginLeading", "-1", FwTextPropType.ktptMarginLeading, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1356,15 +1345,14 @@ public void DeserializePropsFromXml_MarginTop() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'marginTop' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'marginTop' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_MarginTop_Fail() + public void DeserializePropsFromXml_MarginTop_Negative() { - Assert.That(() => CheckDeserializeIntProp("marginTop", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("marginTop", "-1", FwTextPropType.ktptMarginTop, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1381,15 +1369,14 @@ public void DeserializePropsFromXml_MarginTrailing() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'marginTrailing' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'marginTrailing' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_MarginTrailing_Fail() + public void DeserializePropsFromXml_MarginTrailing_Negative() { - Assert.That(() => CheckDeserializeIntProp("marginTrailing", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("marginTrailing", "-1", FwTextPropType.ktptMarginTrailing, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1484,15 +1471,14 @@ public void DeserializePropsFromXml_PadBottom() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'padBottom' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'padBottom' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_PadBottom_Fail() + public void DeserializePropsFromXml_PadBottom_Negative() { - Assert.That(() => CheckDeserializeIntProp("padBottom", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("padBottom", "-1", FwTextPropType.ktptPadBottom, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1509,15 +1495,14 @@ public void DeserializePropsFromXml_PadLeading() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'padLeading' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'padLeading' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_PadLeading_Fail() + public void DeserializePropsFromXml_PadLeading_Negative() { - Assert.That(() => CheckDeserializeIntProp("padLeading", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("padLeading", "-1", FwTextPropType.ktptPadLeading, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1534,15 +1519,14 @@ public void DeserializePropsFromXml_PadTop() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'padTop' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'padTop' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_PadTop_Fail() + public void DeserializePropsFromXml_PadTop_Negative() { - Assert.That(() => CheckDeserializeIntProp("padTop", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("padTop", "-1", FwTextPropType.ktptPadTop, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1559,15 +1543,14 @@ public void DeserializePropsFromXml_PadTrailing() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'padTrailing' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'padTrailing' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_PadTrailing_Fail() + public void DeserializePropsFromXml_PadTrailing_Negative() { - Assert.That(() => CheckDeserializeIntProp("padTrailing", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("padTrailing", "-1", FwTextPropType.ktptPadTrailing, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------ @@ -1613,15 +1596,14 @@ public void DeserializePropsFromXml_RightToLeft() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'rightToLeft' attribute with an - /// invalid attribute. + /// Tests the DeserializePropsFromXml method with a 'rightToLeft' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_RightToLeft_Fail() + public void DeserializePropsFromXml_RightToLeft_Negative() { - Assert.That(() => CheckDeserializeIntProp("rightToLeft", "-1", FwTextPropType.ktptRightToLeft, 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("rightToLeft", "-1", FwTextPropType.ktptRightToLeft, -1, FwTextPropVar.ktpvEnum); } /// ------------------------------------------------------------------------------------ @@ -1759,15 +1741,14 @@ public void DeserializePropsFromXml_TrailingIndent() /// ------------------------------------------------------------------------------------ /// - /// Tests the DeserializePropsFromXml method with a 'trailingIndent' attribute with an - /// invalid value. + /// Tests the DeserializePropsFromXml method with a 'trailingIndent' attribute with a negative + /// value, which loads: the writer writes whatever value the property holds. /// /// ------------------------------------------------------------------------------------ [Test] - public void DeserializePropsFromXml_TrailingIndent_Fail() + public void DeserializePropsFromXml_TrailingIndent_Negative() { - Assert.That(() => CheckDeserializeIntProp("trailingIndent", "-1", 0, 0), - Throws.TypeOf()); + CheckDeserializeIntProp("trailingIndent", "-1", FwTextPropType.ktptTrailingIndent, -1, FwTextPropVar.ktpvMilliPoint); } /// ------------------------------------------------------------------------------------