public void SerializeTsStringToXml_MultiStringLaterRunsLessProps() { ITsIncStrBldr tisb = TsStringUtils.MakeIncStrBldr(); tisb.SetIntValue(FwTextPropType.ktptWs, FwTextPropVar.ktpvDefault, EnWS); tisb.SetStringValue(FwTextPropType.ktptNamedStyle, "Monkey"); tisb.Append("This is a "); tisb.ClearProps(); tisb.SetIntValue(FwTextPropType.ktptWs, FwTextPropVar.ktpvDefault, EsWS); tisb.Append("Laa yra la m\u00E9n"); ITsString tss = tisb.GetString(); string xml = TsStringSerializer.SerializeTsStringToXml(tss, WritingSystemManager, EnWS); Assert.That(StripNewLines(xml), Is.EqualTo("<AStr ws=\"en\"><Run ws=\"en\" namedStyle=\"Monkey\">This is a </Run><Run ws=\"es\">Laa yra la m\u00E9n</Run></AStr>")); }
public void SerializeTsStringToXml_WithLinkDoesNotWriteObjData() { Guid expectedGuid = Guid.NewGuid(); ITsIncStrBldr tisb = TsStringUtils.MakeIncStrBldr(); tisb.SetIntValue(FwTextPropType.ktptWs, FwTextPropVar.ktpvDefault, EnWS); tisb.Append("This is a link:"); tisb.ClearProps(); tisb.SetIntValue(FwTextPropType.ktptWs, FwTextPropVar.ktpvDefault, EnWS); tisb.SetStringValue(FwTextPropType.ktptObjData, CreateObjData(FwObjDataTypes.kodtNameGuidHot, expectedGuid.ToByteArray())); tisb.Append(StringUtils.kChObject.ToString()); ITsString tss = tisb.GetString(); string xml = TsStringSerializer.SerializeTsStringToXml(tss, WritingSystemManager, writeObjData: false); Assert.That(StripNewLines(xml), Is.EqualTo("<Str><Run ws=\"en\">This is a link:</Run></Str>")); }
public static ITsString SpanStrToTsString(string source, int mainWs, ILgWritingSystemFactory wsf) { // How to build up an ITsString via an ITsIncStrBldr - // 1. Use SetIntPropValues or SetStrPropValues to set a property "to be applied to any subsequent append operations". // 2. THEN use Append(string s) to add a string, which will "pick up" the properties set in step 1. // See ScrFootnoteFactory.CreateRunFromStringRep() in FdoFactoryAdditions.cs for a good example. if (source == null) { return(null); } List <Run> runs = GetSpanRuns(source); ITsIncStrBldr builder = TsIncStrBldrClass.Create(); // Will become: ITsIncStrBldr builder = TsStringUtils.MakeIncStrBldr(); // Add "using SIL.CoreImpl;" when this line is uncommented. foreach (Run run in runs) { builder.ClearProps(); // Make sure there aren't leftover properties from previous run // To remove a string property, you set it to null, so we can just use StyleName directly whether or not it's null. builder.SetStrPropValue((int)FwTextPropType.ktptNamedStyle, run.StyleName); int runWs = (run.Lang == null) ? mainWs : wsf.GetWsFromStr(run.Lang); builder.SetIntPropValues((int)FwTextPropType.ktptWs, (int)FwTextPropVar.ktpvDefault, runWs); // We don't care about Guids in this function, so run.Guid is ignored // But we do need to set any other int or string properties that were in the original if (run.IntProperties != null) { foreach (KeyValuePair <int, IntProperty> prop in run.IntProperties) { builder.SetIntPropValues(prop.Key, prop.Value.Variation, prop.Value.Value); } } if (run.StringProperties != null) { foreach (KeyValuePair <int, string> prop in run.StringProperties) { builder.SetStrPropValue(prop.Key, prop.Value); } } builder.Append(run.Content); } return(builder.GetString()); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the prop attributes for element and applies them to a string builder /// (to take effect on the next string added). /// </summary> /// <param name="xml">The XML.</param> /// <param name="lgwsf">The writing system factory.</param> /// <param name="strBldr"></param> /// <returns>Rather strangely, it returns a boolean indicating whether an ORC is needed if the /// text of the run is empty, that is, whether we set an objdata property</returns> /// ------------------------------------------------------------------------------------ internal static bool GetPropAttributesForElement(XElement xml, ILgWritingSystemFactory lgwsf, ITsIncStrBldr strBldr) { bool isOrcNeeded = false; strBldr.ClearProps(); foreach (XAttribute attr in xml.Attributes()) { switch (attr.Name.LocalName) { case "align": strBldr.SetIntPropValues((int)FwTextPropType.ktptAlign, (int)FwTextPropVar.ktpvEnum, GetAlignValueForStr(attr.Value)); break; case "backcolor": strBldr.SetIntPropValues((int)FwTextPropType.ktptBackColor, 0, GetColorValueForStr(attr.Value)); break; case "bold": strBldr.SetIntPropValues((int)FwTextPropType.ktptBold, (int)FwTextPropVar.ktpvEnum, GetToggleValueForStr(attr.Value)); break; case "borderBottom": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderBottom, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "borderColor": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderColor, 0, GetColorValueForStr(attr.Value)); break; case "borderLeading": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderLeading, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "borderTop": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderTop, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "borderTrailing": strBldr.SetIntPropValues((int)FwTextPropType.ktptBorderTrailing, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "bulNumScheme": strBldr.SetIntPropValues((int)FwTextPropType.ktptBulNumScheme, (int)FwTextPropVar.ktpvEnum, GetIntValueFromStr(attr.Value, false)); break; case "bulNumStartAt": strBldr.SetIntPropValues((int)FwTextPropType.ktptBulNumStartAt, 0, GetIntValueFromStr(attr.Value, false)); break; case "bulNumTxtAft": strBldr.SetStrPropValue((int)FwTextPropType.ktptBulNumTxtAft, attr.Value); break; case "bulNumTxtBef": strBldr.SetStrPropValue((int)FwTextPropType.ktptBulNumTxtBef, attr.Value); break; case "charStyle": Debug.Fail("We don't support the old charStyle attribute!"); break; case "contextString": AddObjDataToBldr(strBldr, attr, FwObjDataTypes.kodtContextString); isOrcNeeded = true; break; case "embedded": strBldr.SetStrPropValue((int)FwTextPropType.ktptObjData, (char)FwObjDataTypes.kodtEmbeddedObjectData + attr.Value); break; case "externalLink": strBldr.SetStrPropValue((int)FwTextPropType.ktptObjData, (char)FwObjDataTypes.kodtExternalPathName + attr.Value); break; case "firstIndent": strBldr.SetIntPropValues((int)FwTextPropType.ktptFirstIndent, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, true)); break; case "fontFamily": strBldr.SetStrPropValue((int)FwTextPropType.ktptFontFamily, attr.Value); break; case "fontsize": AddSizePropertyToBldr(strBldr, attr.Value, (int)FwTextPropType.ktptFontSize, xml.Attribute("fontsizeUnit"), null); break; case "fontsizeUnit": break; // Ignore. Its handled in the fontsize. case "fontVariations": strBldr.SetStrPropValue((int)FwTextPropType.ktptFontVariations, attr.Value); break; case "forecolor": strBldr.SetIntPropValues((int)FwTextPropType.ktptForeColor, 0, GetColorValueForStr(attr.Value)); break; case "italic": strBldr.SetIntPropValues((int)FwTextPropType.ktptItalic, (int)FwTextPropVar.ktpvEnum, GetToggleValueForStr(attr.Value)); break; case "keepTogether": strBldr.SetIntPropValues((int)FwTextPropType.ktptKeepTogether, (int)FwTextPropVar.ktpvEnum, GetIntValueFromStr(attr.Value, false)); break; case "keepWithNext": strBldr.SetIntPropValues((int)FwTextPropType.ktptKeepWithNext, (int)FwTextPropVar.ktpvEnum, GetIntValueFromStr(attr.Value, false)); break; case "leadingIndent": strBldr.SetIntPropValues((int)FwTextPropType.ktptLeadingIndent, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "lineHeight": AddSizePropertyToBldr(strBldr, attr.Value, (int)FwTextPropType.ktptLineHeight, xml.Attribute("lineHeightUnit"), xml.Attribute("lineHeightType")); break; case "lineHeightType": break; // Ignore. Its handled in the lineHeight case "lineHeightUnit": break; // Ignore. Its handled in the lineHeight case "link": AddObjDataToBldr(strBldr, attr, FwObjDataTypes.kodtNameGuidHot); isOrcNeeded = true; break; case "marginBottom": strBldr.SetIntPropValues((int)FwTextPropType.ktptMarginBottom, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "marginLeading": strBldr.SetIntPropValues((int)FwTextPropType.ktptMarginLeading, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "marginTop": strBldr.SetIntPropValues((int)FwTextPropType.ktptMarginTop, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "marginTrailing": strBldr.SetIntPropValues((int)FwTextPropType.ktptMarginTrailing, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "moveableObj": AddObjDataToBldr(strBldr, attr, FwObjDataTypes.kodtGuidMoveableObjDisp); isOrcNeeded = true; break; case "namedStyle": strBldr.SetStrPropValue((int)FwTextPropType.ktptNamedStyle, attr.Value); break; case "offset": AddSizePropertyToBldr(strBldr, attr.Value, (int)FwTextPropType.ktptOffset, xml.Attribute("offsetUnit"), null); break; case "offsetUnit": break; // Ignore. Its handled in the offset case "ownlink": AddObjDataToBldr(strBldr, attr, FwObjDataTypes.kodtOwnNameGuidHot); isOrcNeeded = true; break; case "padBottom": strBldr.SetIntPropValues((int)FwTextPropType.ktptPadBottom, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "padLeading": strBldr.SetIntPropValues((int)FwTextPropType.ktptPadLeading, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "padTop": strBldr.SetIntPropValues((int)FwTextPropType.ktptPadTop, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); break; case "padTrailing": strBldr.SetIntPropValues((int)FwTextPropType.ktptPadTrailing, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, false)); 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)); break; case "spaceAfter": strBldr.SetIntPropValues((int)FwTextPropType.ktptSpaceAfter, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, true)); break; case "spaceBefore": strBldr.SetIntPropValues((int)FwTextPropType.ktptSpaceBefore, (int)FwTextPropVar.ktpvMilliPoint, GetIntValueFromStr(attr.Value, true)); break; case "spellcheck": strBldr.SetIntPropValues((int)FwTextPropType.ktptSpellCheck, (int)FwTextPropVar.ktpvEnum, GetSpellCheckValueForStr(attr.Value)); break; case "superscript": 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)); break; case "type": Debug.Assert(attr.Value == "chars", "Embedded pictures are not supported!"); break; case "undercolor": strBldr.SetIntPropValues((int)FwTextPropType.ktptUnderColor, 0, GetColorValueForStr(attr.Value)); break; case "underline": strBldr.SetIntPropValues((int)FwTextPropType.ktptUnderline, (int)FwTextPropVar.ktpvEnum, GetUnderlineTypeForStr(attr.Value)); break; case "widowOrphan": strBldr.SetIntPropValues((int)FwTextPropType.ktptWidowOrphanControl, (int)FwTextPropVar.ktpvEnum, GetIntValueFromStr(attr.Value, false)); break; case "ws": strBldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, TsStringSerializer.GetWsForId(attr.Value, lgwsf)); break; case "wsBase": strBldr.SetIntPropValues((int)FwTextPropType.ktptBaseWs, 0, TsStringSerializer.GetWsForId(attr.Value, lgwsf)); break; case "wsStyle": Debug.Fail("We don't support the old wsStyle attribute!"); break; case "space": case "editable": break; default: throw new XmlSchemaException("Unknown Prop attribute: " + attr.Name.LocalName); } } return isOrcNeeded; }