/// <summary> /// Checks if all text elements inside the given container are superscript. /// </summary> /// <returns> <c>true</c> if all text is superscript (level 1); <c>false</c> otherwise. </returns> private bool AllTextIsSuperscript(IInlineContainer container, int superscriptLevel = 0) { foreach (var inline in container.Inlines) { if (inline is SuperscriptTextInline textInline) { // Remove any nested superscripts. if (AllTextIsSuperscript(textInline, superscriptLevel + 1) == false) { return(false); } } else if (inline is IInlineContainer) { // Remove any superscripts. if (AllTextIsSuperscript((IInlineContainer)inline, superscriptLevel) == false) { return(false); } } else if (inline is IInlineLeaf && !ParseHelpers.IsMarkdownBlankOrWhiteSpace(((IInlineLeaf)inline).Text)) { if (superscriptLevel != 1) { return(false); } } } return(true); }
/// <summary> /// Removes all superscript elements from the given container. /// </summary> private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret) { for (int i = 0; i < container.Inlines.Count; i++) { var inline = container.Inlines[i]; if (inline is SuperscriptTextInline textInline) { // Remove any nested superscripts. RemoveSuperscriptRuns(textInline, insertCaret); // Remove the superscript element, insert all the children. container.Inlines.RemoveAt(i); if (insertCaret) { container.Inlines.Insert(i++, new TextRunInline { Text = "^" }); } foreach (var superscriptInline in textInline.Inlines) { container.Inlines.Insert(i++, superscriptInline); } i--; } else if (inline is IInlineContainer) { // Remove any superscripts. RemoveSuperscriptRuns((IInlineContainer)inline, insertCaret); } } }
/// <summary> /// Checks if all text elements inside the given container are superscript. /// </summary> /// <returns> <c>true</c> if all text is superscript (level 1); <c>false</c> otherwise. </returns> private bool AllTextIsSuperscript(IInlineContainer container, int superscriptLevel = 0) { foreach (var inline in container.Inlines) { var textInline = inline as SuperscriptTextInline; if (textInline != null) { // Remove any nested superscripts. if (AllTextIsSuperscript(textInline, superscriptLevel + 1) == false) { return(false); } } else if (inline is IInlineContainer) { // Remove any superscripts. if (AllTextIsSuperscript((IInlineContainer)inline, superscriptLevel) == false) { return(false); } } else if (inline is IInlineLeaf && !Common.IsBlankOrWhiteSpace(((IInlineLeaf)inline).Text)) { if (superscriptLevel != 1) { return(false); } } } return(true); }
internal static PlainText AppendWithLineInfo(this IInlineContainer container, string text, int lineNumber1, int linePosition1, int lineNumber2, int linePosition2) { Debug.Assert(container != null); Debug.Assert(text != null); Debug.Assert(lineNumber1 >= 0); Debug.Assert(linePosition1 >= 0); Debug.Assert(lineNumber2 >= 0); Debug.Assert(linePosition2 >= 0); Debug.Assert(lineNumber1 < lineNumber2 || lineNumber1 == lineNumber2 && linePosition1 <= linePosition2); var pt = container.Inlines.LastNode as PlainText; if (pt == null) { container.Inlines.Add(pt = new PlainText(text)); pt.SetLineInfo(lineNumber1, linePosition1, lineNumber2, linePosition2); } else { if (text.Length == 0) { return(pt); // ExtendLineInfo won't accept (0) } pt.Content += text; pt.ExtendLineInfo(lineNumber2, linePosition2); } ((Node)container).ExtendLineInfo(lineNumber2, linePosition2); return(pt); }
/// <summary> /// Removes all superscript elements from the given container. /// </summary> private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret) { for (int i = 0; i < container.Inlines.Count; i++) { var inline = container.Inlines[i]; if (inline is SuperscriptTextInline textInline) { RemoveSuperscriptRuns(textInline, insertCaret); container.Inlines.RemoveAt(i); if (insertCaret) { container.Inlines.Insert(i++, new TextRunInline { Text = "^" }); } foreach (var superscriptInline in textInline.Inlines) { container.Inlines.Insert(i++, superscriptInline); } i--; } else if (inline is IInlineContainer) { RemoveSuperscriptRuns((IInlineContainer)inline, insertCaret); } } }
public void Clear(IInlineContainer parent) { for (var i = parent.Children.Count - 1; i >= 0; i--) { var child = parent.Children[i]; parent.RemoveContent(child); } }
public static SimpleInlineElement AppendContent(this IInlineContainer container, string text, IStyleDefinition?localStyle = null) { var element = new SimpleInlineElement() { Text = text }; localStyle?.MergeInto(element.LocalStyles); container.AppendContent(element); return(element); }
public static SimpleInlineElement AppendContent(this IInlineContainer container, string text, params string[] styles) { var element = AppendContent(container, text); foreach (var style in styles) { element.AddStyle(style); } return(element); }
public void Append(IInlineContainer parent, InlineElement inlineElement) { if (inlineElement.Parent != null) { inlineElement.Paragraph?.Invalidate(Paragraph.InvalidationFlags.TextFlow); inlineElement.Parent?.RemoveContent(inlineElement); } _children ??= new List <InlineElement>(); _children.Add(inlineElement); inlineElement.Parent = parent; inlineElement.Paragraph?.Invalidate(Paragraph.InvalidationFlags.TextFlow); }
/// <summary> /// Append a <see cref="PlainText"/> node to the beginning of the paragraph. /// </summary> /// <param name="text">The text to be inserted.</param> /// <returns>Either the new <see cref="PlainText"/> node inserted, or the existing <see cref="PlainText"/> at the beginning of the paragraph.</returns> /// <exception cref="ArgumentNullException"><paramref name="text"/> is <c>null</c>.</exception> public static PlainText Prepend(this IInlineContainer container, string text) { if (text == null) { throw new ArgumentNullException(nameof(text)); } var pt = container.Inlines.FirstNode as PlainText; if (pt == null) { container.Inlines.AddFirst(pt = new PlainText()); } pt.Content = text + pt.Content; return(pt); }
public void Remove(IInlineContainer parent, InlineElement inlineElement) { if (inlineElement.Parent != parent) { throw new ArgumentException("Element is not in this parent"); } inlineElement.Paragraph?.Invalidate(Paragraph.InvalidationFlags.TextFlow); if (_children != null && _children.Remove(inlineElement)) { if (_children.Count == 0) { _children = null; } } inlineElement.Parent = null; }
/// <summary> /// RUN /// </summary> /// <returns><c>true</c> if one or more nodes has been parsed.</returns> private bool ParseRun(RunParsingMode mode, IInlineContainer container, bool setLineNumber) { ParseStart(); var parsedAny = false; while (!NeedsTerminate()) { // Read more InlineNode inline = null; if ((inline = ParseExpandable()) != null) { goto NEXT; } switch (mode) { case RunParsingMode.Run: // RUN if ((inline = ParseInline()) != null) { goto NEXT; } break; case RunParsingMode.ExpandableText: // EXPANDABLE_TEXT if ((inline = ParsePartialPlainText()) != null) { goto NEXT; } break; case RunParsingMode.ExpandableUrl: // EXPANDABLE_URL if ((inline = ParseUrlText()) != null) { goto NEXT; } break; default: throw new ArgumentOutOfRangeException(nameof(mode), mode, null); } break; NEXT: parsedAny = true; // Remember that ParsePartialText stops whenever there's a susceptable termination of PLAIN_TEXT // So we need to marge the consequent PlainText objects. if (inline is PlainText newtext) { if (container.Inlines.LastNode is PlainText lastText) { lastText.Content += newtext.Content; lastText.ExtendLineInfo(lineNumber, linePosition); continue; } } container.Inlines.Add(inline); } // Note that the content of RUN should not be empty. if (parsedAny) { ParseSuccessful((Node)container, setLineNumber); return(true); } else { Fallback(); return(false); } }
private void AppendHitPointDescription(GameObject critter, IInlineContainer content) { var subdualDamage = critter.GetStat(Stat.subdual_damage); var currentHp = critter.GetStat(Stat.hp_current); var maxHp = critter.GetStat(Stat.hp_max); StyleDefinition hpStyleDefinition = null; if (critter.IsPC()) { if (currentHp < maxHp) { hpStyleDefinition = new StyleDefinition() { Color = new PackedLinearColorA(0xFFFF0000) }; } } else if (GameSystems.Critter.IsDeadNullDestroyed(critter) || currentHp <= 0) { hpStyleDefinition = new StyleDefinition() { Color = new PackedLinearColorA(0xFF7F7F7F) }; } else { var injuryLevel = UiSystems.Tooltip.GetInjuryLevel(critter); hpStyleDefinition = new StyleDefinition() { Color = UiSystems.Tooltip.GetInjuryLevelColor(injuryLevel) }; } if (critter.IsPC()) { content.AppendContent(_translations[103]); content.AppendContent(": "); content.AppendContent(currentHp.ToString(), hpStyleDefinition); content.AppendContent($"/{maxHp}"); if (subdualDamage > 0) { content.AppendContent($"({subdualDamage})", CommonStyles.HpSubdualDamage); } } else if (GameSystems.Critter.IsDeadNullDestroyed(critter) || currentHp <= 0) { content.AppendContent(_translations[108]); content.AppendContent(": "); content.AppendContent((maxHp - currentHp).ToString(), hpStyleDefinition); } else { content.AppendContent(GetInjuryLevelDescription(critter), hpStyleDefinition); if (subdualDamage <= 0) { // Show the amount of damage dealt so far if (currentHp < maxHp) { content.AppendContent("\n"); content.AppendContent(_translations[108]); // "Damage" content.AppendContent(": "); content.AppendContent((maxHp - currentHp).ToString(), hpStyleDefinition); } } else { if (subdualDamage >= maxHp) { content.AppendContent("\n"); content.AppendContent(_translations[108]); // "Damage" content.AppendContent(": "); content.AppendContent($"({subdualDamage})", CommonStyles.HpSubdualDamage); } else { content.AppendContent("\n"); content.AppendContent(_translations[108]); // "Damage" content.AppendContent(": @1"); content.AppendContent((maxHp - currentHp).ToString(), hpStyleDefinition); content.AppendContent("@0 "); content.AppendContent($"({subdualDamage})", CommonStyles.HpSubdualDamage); } } } }
/// <summary> /// Removes all superscript elements from the given container. /// </summary> /// <param name="container"></param> /// <param name="insertCaret"></param> private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret) { for (int i = 0; i < container.Inlines.Count; i ++) { var inline = container.Inlines[i]; if (inline is SuperscriptTextInline) { // Remove any nested superscripts. RemoveSuperscriptRuns((IInlineContainer)inline, insertCaret); // Remove the superscript element, insert all the children. container.Inlines.RemoveAt(i); if (insertCaret) container.Inlines.Insert(i++, new TextRunInline { Text = "^" }); foreach (var superscriptInline in ((SuperscriptTextInline)inline).Inlines) container.Inlines.Insert(i++, superscriptInline); i--; } else if (inline is IInlineContainer) { // Remove any superscripts. RemoveSuperscriptRuns((IInlineContainer)inline, insertCaret); } } }
/// <summary> /// Checks if all text elements inside the given container are superscript. /// </summary> /// <param name="container"></param> /// <returns> <c>true</c> if all text is superscript (level 1); <c>false</c> otherwise. </returns> private bool AllTextIsSuperscript(IInlineContainer container, int superscriptLevel = 0) { foreach (var inline in container.Inlines) { if (inline is SuperscriptTextInline) { // Remove any nested superscripts. if (AllTextIsSuperscript((IInlineContainer)inline, superscriptLevel + 1) == false) return false; } else if (inline is IInlineContainer) { // Remove any superscripts. if (AllTextIsSuperscript((IInlineContainer)inline, superscriptLevel) == false) return false; } else if (inline is IInlineLeaf && !Common.IsBlankOrWhiteSpace(((IInlineLeaf)inline).Text)) { if (superscriptLevel != 1) return false; } } return true; }