/// <summary> /// Calculates the size of the specified parser token when rendered according to the current layout state. /// </summary> /// <param name="font">The current font face.</param> /// <param name="tokenType">The type of the current token.</param> /// <param name="tokenText">The text of the current token.</param> /// <param name="tokenNext">The next token after the current token, excluding command tokens.</param> /// <returns>The size of the specified token in pixels.</returns> private Size2 MeasureToken(SpriteFontFace font, TextParserTokenType tokenType, StringSegment tokenText, TextParserToken?tokenNext = null) { switch (tokenType) { case TextParserTokenType.Icon: { TextIconInfo icon; if (!registeredIcons.TryGetValue(tokenText, out icon)) { throw new InvalidOperationException(UltravioletStrings.UnrecognizedIcon.Format(tokenText)); } return(new Size2(icon.Width ?? icon.Icon.Controller.Width, icon.Height ?? icon.Icon.Controller.Height)); } case TextParserTokenType.Text: { var size = font.MeasureString(tokenText); if (tokenNext.HasValue) { var tokenNextValue = tokenNext.GetValueOrDefault(); if (tokenNextValue.TokenType == TextParserTokenType.Text && !tokenNextValue.Text.IsEmpty && !tokenNextValue.IsNewLine) { var charLast = tokenText[tokenText.Length - 1]; var charNext = tokenNextValue.Text[0]; var kerning = font.Kerning.Get(charLast, charNext); return(new Size2(size.Width + kerning, size.Height)); } } return(size); } } return(Size2.Zero); }
/// <summary> /// Initializes a new instance of the <see cref="TextParserToken"/> structure. /// </summary> /// <param name="tokenType">The token's type.</param> /// <param name="text">The token's text.</param> /// <param name="sourceOffset">The offset of the first character in the source text that produced this token.</param> /// <param name="sourceLength">The number of characters in the source text that produced this token.</param> /// <param name="isNonBreakingSpace">A value indicating whether this token represents a non-breaking space.</param> internal TextParserToken(TextParserTokenType tokenType, StringSegment text, Int32 sourceOffset, Int32 sourceLength, Boolean isNonBreakingSpace = false) { this.tokenType = tokenType; this.text = text; this.sourceOffset = sourceOffset; this.sourceLength = sourceLength; this.isNonBreakingSpace = isNonBreakingSpace; }
/// <summary> /// Initializes a new instance of the <see cref="TextParserToken"/> structure. /// </summary> /// <param name="tokenType">The token's type.</param> /// <param name="text">The token's text.</param> /// <param name="sourceOffset">The offset of the first character in the source text that produced this token.</param> /// <param name="sourceLength">The number of characters in the source text that produced this token.</param> /// <param name="isNonBreakingSpace">A value indicating whether this token represents a non-breaking space.</param> internal TextParserToken(TextParserTokenType tokenType, StringSegment text, Int32 sourceOffset, Int32 sourceLength, Boolean isNonBreakingSpace = false) { this.tokenType = tokenType; this.text = text; this.sourceOffset = sourceOffset; this.sourceLength = sourceLength; this.isNonBreakingSpace = isNonBreakingSpace; }
/// <summary> /// Calculates the size of the specified parser token when rendered according to the current layout state. /// </summary> /// <param name="font">The current font face.</param> /// <param name="tokenType">The type of the current token.</param> /// <param name="tokenText">The text of the current token.</param> /// <param name="tokenNext">The next token after the current token, excluding command tokens.</param> /// <returns>The size of the specified token in pixels.</returns> private Size2 MeasureToken(SpriteFontFace font, TextParserTokenType tokenType, StringSegment tokenText, TextParserToken? tokenNext = null) { switch (tokenType) { case TextParserTokenType.Icon: { TextIconInfo icon; if (!registeredIcons.TryGetValue(tokenText, out icon)) throw new InvalidOperationException(UltravioletStrings.UnrecognizedIcon.Format(tokenText)); return new Size2(icon.Width ?? icon.Icon.Controller.Width, icon.Height ?? icon.Icon.Controller.Height); } case TextParserTokenType.Text: { var size = font.MeasureString(tokenText); if (tokenNext.HasValue) { var tokenNextValue = tokenNext.GetValueOrDefault(); if (tokenNextValue.TokenType == TextParserTokenType.Text && !tokenNextValue.Text.IsEmpty && !tokenNextValue.IsNewLine) { var charLast = tokenText[tokenText.Length - 1]; var charNext = tokenNextValue.Text[0]; var kerning = font.Kerning.Get(charLast, charNext); return new Size2(size.Width + kerning, size.Height); } } return size; } } return Size2.Zero; }