/// <summary> /// Measures a number string. /// </summary> /// <param name="text">The string to measure.</param> /// <returns>The size of the string.</returns> public virtual Size MeasureNumber(string text) { FormattedNumber fn = FormattedNumber.Parse(text); string SignificandPart = fn.SignificandPart; string ExponentPart = fn.ExponentPart; string InvalidText = fn.InvalidText; Brush Brush; Brush = GetBrush(BrushSettings.NumberSignificand); FormattedText ftSignificand = CreateFormattedText(SignificandPart, EmSize, Brush); Brush = GetBrush(BrushSettings.NumberExponent); FormattedText ftExponent = CreateFormattedText(ExponentPart, EmSize * SubscriptRatio, Brush); Brush = GetBrush(BrushSettings.NumberInvalid); FormattedText ftInvalid = CreateFormattedText(InvalidText, EmSize, Brush); Measure Width = new Measure() { Draw = ftSignificand.WidthIncludingTrailingWhitespace + ftExponent.WidthIncludingTrailingWhitespace + ftInvalid.WidthIncludingTrailingWhitespace, Print = text.Length }; Measure Height = LineHeight; return(new Size(Width, Height)); }
private Number ComputeSide(ICSharpWriter writer, ICSharpExpression expression) { string ValueString; ICSharpExpressionAsConstant ExpressionAsConstant = expression as ICSharpExpressionAsConstant; Debug.Assert(ExpressionAsConstant != null); if (ExpressionAsConstant.IsDirectConstant) { ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext(); expression.WriteCSharp(writer, SourceExpressionContext, -1); ValueString = SourceExpressionContext.ReturnValue; } else { ICSharpComputableExpression ComputableExpression = ExpressionAsConstant as ICSharpComputableExpression; Debug.Assert(ComputableExpression != null); ComputableExpression.Compute(writer); ValueString = ComputableExpression.ComputedValue; } FormattedNumber Result = FormattedNumber.Parse(ValueString); return(Result.Value); }
/// <summary> /// Finds the matching nodes of a <see cref="IManifestNumberExpression"/>. /// </summary> /// <param name="node">The agent expression to check.</param> /// <param name="errorList">The list of errors found.</param> /// <param name="resolvedResult">The expression result types upon return.</param> /// <param name="resolvedException">Exceptions the expression can throw upon return.</param> /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param> /// <param name="expressionConstant">The constant type upon return.</param> public static bool ResolveCompilerReferences(IManifestNumberExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant) { resolvedResult = null; resolvedException = null; constantSourceList = new SealableList <IExpression>(); expressionConstant = NeutralLanguageConstant.NotConstant; IClass EmbeddingClass = node.EmbeddingClass; string NumberText = node.ValidText.Item; ISealableDictionary <ITypeName, ICompiledType> TypeTable = EmbeddingClass.TypeTable; if (!Expression.IsLanguageTypeAvailable(LanguageClasses.Number.Guid, node, out ITypeName NumberTypeName, out ICompiledType NumberType)) { errorList.AddError(new ErrorNumberTypeMissing(node)); return(false); } resolvedResult = new ResultType(NumberTypeName, NumberType, string.Empty); resolvedException = new ResultException(); FormattedNumber FormattedNumber = FormattedNumber.Parse(NumberText); Debug.Assert(string.IsNullOrEmpty(FormattedNumber.InvalidText)); expressionConstant = new NumberLanguageConstant(FormattedNumber.Value); #if COVERAGE Debug.Assert(!node.IsComplex); #endif return(true); }
public PluralForm [] SelectPluralForms(IFormatProvider provider, FormatString formatString, object [] arguments) { var selection = new PluralForm [arguments.Length]; var length = Math.Min(formatString.Arguments.Length, arguments.Length); for (var index = 0; index < length; index++) { var argument = formatString.Arguments [index]; if (argument.NumberArgumentHole == null) { continue; } var number = FormattedNumber.Parse(provider ?? Culture, argument.NumberArgumentHole, arguments [index]); if (number == null) { continue; } var numberForm = CardinalForm; if (argument.NumberForm == NumberForm.Ordinal) { numberForm = OrdinalForm; } selection [index] = numberForm.SelectPluralForm(number.Value, argument.AvailablePluralForms); } return(selection); }
/// <summary> /// Draws a number string, at the location specified in <paramref name="origin"/>. /// </summary> /// <param name="text">The text to draw.</param> /// <param name="origin">The location where to start drawing.</param> public virtual void DrawNumber(string text, Point origin) { Debug.Assert(WpfDrawingContext != null); FormattedNumber fn = FormattedNumber.Parse(text); string SignificandPart = fn.SignificandPart; string ExponentString0 = fn.ExponentPart.Length > 0 ? fn.ExponentPart.Substring(0, 1) : string.Empty; string ExponentString1 = fn.ExponentPart.Length > 1 ? fn.ExponentPart.Substring(1) : string.Empty; string InvalidText = fn.InvalidText; Brush Brush; double X = PagePadding.Left.Draw + origin.X.Draw; double Y = PagePadding.Top.Draw + origin.Y.Draw; Brush = GetBrush(BrushSettings.NumberSignificand); FormattedText ftSignificand = CreateFormattedText(SignificandPart, EmSize, Brush); WpfDrawingContext.DrawText(ftSignificand, new System.Windows.Point(X, Y)); X += ftSignificand.WidthIncludingTrailingWhitespace; Brush = GetBrush(BrushSettings.NumberExponent); FormattedText ftExponent0 = CreateFormattedText(ExponentString0, EmSize, Brush); WpfDrawingContext.DrawText(ftExponent0, new System.Windows.Point(X, Y)); X += ftExponent0.WidthIncludingTrailingWhitespace; FormattedText ftExponent1 = CreateFormattedText(ExponentString1, EmSize * SubscriptRatio, Brush); WpfDrawingContext.DrawText(ftExponent1, new System.Windows.Point(X, Y)); X += ftExponent1.WidthIncludingTrailingWhitespace; Brush = GetBrush(BrushSettings.NumberInvalid); FormattedText ftInvalid = CreateFormattedText(InvalidText, EmSize, Brush); WpfDrawingContext.DrawText(ftInvalid, new System.Windows.Point(X, Y)); }
/// <summary> /// Prints a string, at the location specified in <paramref name="origin"/>. /// </summary> /// <param name="text">The text to print.</param> /// <param name="origin">The location where to start printing.</param> public virtual void PrintNumber(string text, Point origin) { FormattedNumber fn = FormattedNumber.Parse(text); string SignificandPart = fn.SignificandPart; string ExponentPart = fn.ExponentPart; string InvalidText = fn.InvalidText; // Try to use superscript digits in the exponent. string SuperscriptExponentString = string.Empty; for (int i = 0; i < ExponentPart.Length; i++) { int Index = "0123456789+-".IndexOf(ExponentPart[i]); SuperscriptExponentString += (Index < 0) ? ExponentPart[i] : "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻"[Index]; } int X = origin.X.Print; int Y = origin.Y.Print; PrintableArea.Print(SignificandPart, X, Y, BrushSettings.NumberSignificand); X += SignificandPart.Length; PrintableArea.Print(SuperscriptExponentString, X, Y, BrushSettings.NumberExponent); X += ExponentPart.Length; PrintableArea.Print(InvalidText, X, Y, BrushSettings.NumberInvalid); }
/// <summary> /// Check number types. /// </summary> /// <param name="isChanged">True upon return if a number type was changed.</param> public void CheckNumberType(ref bool isChanged) { FormattedNumber n = FormattedNumber.Parse(ValidText.Item); bool IsInteger = n.Value.IsInteger; NumberKinds NumberKind = IsInteger ? NumberKinds.Integer : NumberKinds.Real; Debug.Assert(ResolvedResult.IsAssigned); ResolvedResult.Item.UpdateNumberKind(NumberKind, ref isChanged); }
/// <summary> /// Initializes a new instance of the <see cref="CSharpManifestNumberExpression"/> class. /// </summary> /// <param name="context">The creation context.</param> /// <param name="source">The Easly expression from which the C# expression is created.</param> protected CSharpManifestNumberExpression(ICSharpContext context, IManifestNumberExpression source) : base(context, source) { FormattedNumber FormattedNumber = FormattedNumber.Parse(Source.ValidText.Item); Debug.Assert(FormattedNumber.IsValid); NumberType = FormattedNumber.Value.IsInteger ? CSharpNumberTypes.Integer : CSharpNumberTypes.Real; Debug.Assert(NumberType != CSharpNumberTypes.NotApplicable && NumberType != CSharpNumberTypes.Unknown); }
public void TestCreate0() { FormattedNumber NaN = FormattedNumber.Parse("NaN"); Assert.IsTrue(NaN.Value.IsNaN); FormattedNumber Zero = FormattedNumber.Parse("0"); Assert.IsTrue(Zero.Value.IsZero); FormattedNumber Sum = Zero + Zero; Assert.IsTrue(Sum.Value.IsZero); }
/// <summary></summary> protected virtual void ShowNumberCaret(Point origin, string text, CaretModes mode, int position) { FormattedNumber fn = FormattedNumber.Parse(text); GetNumberCaretParts(fn, position, out string BeforeExponent, out string ExponentString0, out string ExponentString1, out string InvalidText); double X = origin.X.Draw; double Y = origin.Y.Draw; Brush Brush; Brush = GetBrush(BrushSettings.NumberSignificand); FormattedText ftSignificand = CreateFormattedText(BeforeExponent, EmSize, Brush); X += ftSignificand.WidthIncludingTrailingWhitespace; Brush = GetBrush(BrushSettings.NumberExponent); FormattedText ftExponent0 = CreateFormattedText(ExponentString0, EmSize, Brush); X += ftExponent0.WidthIncludingTrailingWhitespace; FormattedText ftExponent1 = CreateFormattedText(ExponentString1, EmSize * SubscriptRatio, Brush); X += ftExponent1.WidthIncludingTrailingWhitespace; Brush = GetBrush(BrushSettings.NumberInvalid); FormattedText ftInvalid = CreateFormattedText(InvalidText, EmSize, Brush); X += ftInvalid.WidthIncludingTrailingWhitespace; double CaretEmSize; double CaretHeight; if (position <= fn.SignificandPart.Length || fn.ExponentPart.Length == 0 || position > fn.SignificandPart.Length + 1) { CaretEmSize = EmSize; CaretHeight = LineHeight.Draw; } else { CaretEmSize = EmSize * SubscriptRatio; CaretHeight = LineHeight.Draw * SubscriptRatio; } ChangeFlashClockOpacity(isVisible: true); ShowNumberCaretParts(fn, mode, position, X, Y, CaretEmSize, CaretHeight); }
public static void ParseThrowsOnOutOfRangeArgumentIndex(string format, int argumentIndex) { Assert.Throws <ArgumentOutOfRangeException> (() => FormattedNumber.Parse(CultureInfo.InvariantCulture, format, argumentIndex, 1)); }
public static void ParseExtractsTheSpecifiedArgument(string format, int argumentIndex, object value, string expectedNumber) { var number = FormattedNumber.Parse(CultureInfo.InvariantCulture, format, argumentIndex, value); Assert.Equal(expectedNumber, number?.ToString(CultureInfo.InvariantCulture)); }