Пример #1
0
        public void TestAddErrors()
        {
            var list = new MathList();

            Assert.Throws <InvalidOperationException>(() => list.Add(null));
            Assert.Throws <InvalidOperationException>(() => list.Add(MathAtoms.Create(MathAtomType.Boundary, "")));
        }
Пример #2
0
        public void TestAppend()
        {
            var list1 = new MathList();
            var atom1 = MathAtoms.Placeholder;
            var atom2 = MathAtoms.Operator("+", false);
            var atom3 = MathAtoms.Operator("-", false);

            list1.Add(atom1);
            list1.Add(atom2);
            list1.Add(atom3);

            var list2 = new MathList();
            var atom5 = MathAtoms.Times;
            var atom6 = MathAtoms.Divide;

            list2.Add(atom5);
            list2.Add(atom6);

            Assert.Equal(3, list1.Count);
            Assert.Equal(2, list2.Count);

            list1.Append(list2);
            Assert.Equal(5, list1.Count);
            Assert.Equal(atom5, list1[3]);
            Assert.Equal(atom6, list1[4]);
        }
Пример #3
0
        public void TestListCopy()
        {
            var list  = new MathList();
            var atom  = MathAtoms.Placeholder;
            var atom2 = MathAtoms.Times;
            var atom3 = MathAtoms.Divide;

            list.Add(atom);
            list.Add(atom2);
            list.Add(atom3);

            var list2 = AtomCloner.Clone(list, false);

            CheckClone(list, list2);
        }
Пример #4
0
        public void TestRemoveAtomAtIndex()
        {
            var list  = new MathList();
            var atom  = MathAtoms.Placeholder;
            var atom2 = MathAtoms.Times;

            list.Add(atom);
            list.Add(atom2);
            Assert.Equal(2, list.Count);
            list.RemoveAt(0);
            Assert.Single(list);
            Assert.Equal(atom2, list[0]);
            Assert.Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(1));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(2));
        }
Пример #5
0
        public void TestListCopy()
        {
            var list  = new MathList();
            var atom  = LaTeXSettings.Placeholder;
            var atom2 = LaTeXSettings.Times;
            var atom3 = LaTeXSettings.Divide;

            list.Add(atom);
            list.Add(atom2);
            list.Add(atom3);

            var list2 = list.Clone(false);

            CheckClone(list, list2);
        }
Пример #6
0
        public void TestAdd()
        {
            var list = new MathList();

            Assert.Empty(list);
            var atom = MathAtoms.Placeholder;

            list.Add(atom);
            Assert.Single(list);
            Assert.Equal(atom, list[0]);
            var atom2 = MathAtoms.Times;

            list.Add(atom2);
            Assert.Equal(2, list.Count);
            Assert.Equal(atom2, list[1]);
        }
Пример #7
0
        public void TestRemoveAtomsInRange()
        {
            var list  = new MathList();
            var atom  = MathAtoms.Placeholder;
            var atom2 = MathAtoms.Times;
            var atom3 = MathAtoms.Divide;

            list.Add(atom);
            list.Add(atom2);
            list.Add(atom3);
            Assert.Equal(3, list.Count);
            list.RemoveAtoms(new Range(1, 2));
            Assert.Single(list);
            Assert.Equal(atom, list[0]);
            Assert.Throws <ArgumentException>(() => list.RemoveAtoms(new Range(1, 1)));
        }
Пример #8
0
        public void TestRemoveLast()
        {
            var list = new MathList();
            var atom = MathAtoms.Placeholder;

            list.Add(atom);
            Assert.Single(list);
            list.RemoveLastAtom();
            Assert.Empty(list);
            list.RemoveLastAtom(); // should not throw; just noop in this case
            Assert.Empty(list);
            var atom2 = MathAtoms.Times;

            list.Add(atom);
            list.Add(atom2);
            Assert.Equal(2, list.Count);
            list.RemoveLastAtom();
            Assert.Single(list);
            Assert.Equal(atom, list[0]);
        }
Пример #9
0
        public void TestSuperScript()
        {
            var mathList    = new MathList();
            var x           = MathAtoms.ForCharacter('x');
            var superscript = new MathList {
                MathAtoms.ForCharacter('2')
            };

            x.Superscript = superscript;
            mathList.Add(x);

            var display = Typesetter <TFont, TGlyph> .CreateLine(mathList, _font, _context, LineStyle.Display);

            Assert.NotNull(display);

            Assert.Equal(LinePosition.Regular, display.LinePosition);
            Assert.Equal(new PointF(), display.Position);
            Assert.Equal(new Range(0, 1), display.Range);
            Assert.False(display.HasScript);
            Assert.Equal(display.IndexInParent, Range.UndefinedInt);
            Assert.Equal(2, display.Displays.Count());

            var super0 = display.Displays[0];
            var line   = super0 as TextLineDisplay <TFont, TGlyph>;

            Assert.NotNull(line);
            Assert.Single(line.Atoms);
            Assert.Equal("x", line.StringText());
            Assert.Equal(new PointF(), line.Position);
            Assert.True(line.HasScript);

            var super1 = display.Displays[1] as ListDisplay <TFont, TGlyph>;

            Assert.NotNull(super1);
            Assert.Equal(LinePosition.Superscript, super1.LinePosition);
            var super1Position = super1.Position;

            Assertions.ApproximatePoint(10.32, 7.26, super1Position, 0.01); // may change as we implement more details?
            Assert.Equal(new Range(0, 1), super1.Range);
            Assert.False(super1.HasScript);
            Assert.Equal(0, super1.IndexInParent);
            Assert.Single(super1.Displays);

            var super10 = super1.Displays[0] as TextLineDisplay <TFont, TGlyph>;

            Assert.NotNull(super10);
            Assert.Single(super10.Atoms);
            Assert.Equal(new PointF(), super10.Position);
            Assert.False(super10.HasScript);

            Assertions.ApproximatelyEqual(17.06, display.Ascent, 0.01);
            Assertions.ApproximatelyEqual(4, display.Descent, 0.01);
        }
Пример #10
0
        public static List <MathList> Split(this MathList list, MathAtom splitAtom)
        {
            var splitList = new List <MathList>();

            var sublist = new MathList();

            for (int i = 0; i < list.Count; i++)
            {
                var  item        = list[i];
                bool isSplitItem = item == splitAtom;
                if (!isSplitItem)
                {
                    sublist.Add(item);
                }

                if (isSplitItem || i + 1 == list.Count)
                {
                    splitList.Add(sublist);
                    sublist = new MathList();
                }
            }

            return(splitList);
        }
Пример #11
0
        public void TestBinomial()
        {
            var list     = new MathList();
            var fraction = new Fraction(false)
            {
                Numerator = new MathList {
                    MathAtoms.ForCharacter('1')
                },
                Denominator = new MathList {
                    MathAtoms.ForCharacter('3')
                },
                LeftDelimiter  = "(",
                RightDelimiter = ")"
            };

            list.Add(fraction);

            var display = Typesetter <TFont, TGlyph> .CreateLine(list, _font, _context, LineStyle.Display);

            Assert.Equal(LinePosition.Regular, display.LinePosition);
            Assert.Equal(new PointF(), display.Position);
            Assert.Equal(new Range(0, 1), display.Range);
            Assert.False(display.HasScript);
            Assert.Equal(Range.UndefinedInt, display.IndexInParent);
            Assert.Single(display.Displays);

            var display0 = display.Displays[0] as ListDisplay <TFont, TGlyph>;

            Assert.Equal(LinePosition.Regular, display0.LinePosition);
            Assert.Equal(new PointF(), display0.Position);
            Assert.Equal(new Range(0, 1), display.Range);
            Assert.False(display0.HasScript);
            Assert.Equal(Range.UndefinedInt, display0.IndexInParent);
            Assert.Equal(3, display0.Displays.Count);

            var glyph = display0.Displays[0] as GlyphDisplay <TFont, TGlyph>;

            Assert.Equal(new PointF(), glyph.Position);
            Assert.Equal(Range.NotFound, glyph.Range);
            Assert.False(glyph.HasScript);

            var subFraction = display0.Displays[1] as FractionDisplay <TFont, TGlyph>;

            Assert.Equal(new Range(0, 1), subFraction.Range);
            Assert.False(subFraction.HasScript);
            Assertions.ApproximatePoint(10, 0, subFraction.Position, 0.01);

            var numerator = subFraction.Numerator as ListDisplay <TFont, TGlyph>;

            Assert.NotNull(numerator);
            Assert.Equal(LinePosition.Regular, numerator.LinePosition);
            Assertions.ApproximatePoint(10, 13.54, numerator.Position, 0.01);
            Assert.Single(numerator.Displays);
            Assert.Equal(Range.UndefinedInt, numerator.IndexInParent);
            Assert.False(numerator.HasScript);

            var subNumerator = numerator.Displays[0] as TextLineDisplay <TFont, TGlyph>;

            Assert.Single(subNumerator.Atoms);
            Assert.Equal("1", subNumerator.StringText());
            Assert.Equal(new PointF(), subNumerator.Position);
            Assert.Equal(new Range(0, 1), subNumerator.Range);
            Assert.False(subNumerator.HasScript);

            var denominator = subFraction.Denominator as ListDisplay <TFont, TGlyph>;

            Assert.NotNull(denominator);
            Assert.Equal(LinePosition.Regular, denominator.LinePosition);
            Assertions.ApproximatePoint(10, -13.72, denominator.Position, 0.01);
            Assert.Equal(new Range(0, 1), denominator.Range);
            Assert.False(denominator.HasScript);
            Assert.Equal(Range.UndefinedInt, denominator.IndexInParent);
            Assert.Single(denominator.Displays);

            var subDenominator = denominator.Displays[0] as TextLineDisplay <TFont, TGlyph>;

            Assert.Single(subDenominator.Atoms);
            Assert.Equal("3", subDenominator.StringText());
            Assert.Equal(new PointF(), subDenominator.Position);
            Assert.Equal(new Range(0, 1), subDenominator.Range);
            Assert.False(subDenominator.HasScript);

            var subRight = display0.Displays[2] as GlyphDisplay <TFont, TGlyph>;

            Assert.False(subRight.HasScript);
            Assert.Equal(Range.NotFound, subRight.Range);
            Assertions.ApproximatePoint(20, 0, subRight.Position, 0.01);
            Assertions.ApproximatelyEqual(27.54, display.Ascent, 0.01);
            Assertions.ApproximatelyEqual(17.72, display.Descent, 0.01);
            Assertions.ApproximatelyEqual(30, display.Width, 0.01);
        }
Пример #12
0
        public void TestSuperSubscript()
        {
            var mathList    = new MathList();
            var x           = MathAtoms.ForCharacter('x');
            var superscript = new MathList {
                MathAtoms.ForCharacter('2')
            };
            var subscript = new MathList {
                MathAtoms.ForCharacter('1')
            };

            x.Subscript   = subscript;
            x.Superscript = superscript;
            mathList.Add(x);

            var display = Typesetter <TFont, TGlyph> .CreateLine(mathList, _font, _context, LineStyle.Display);

            Assert.NotNull(display);
            Assert.Equal(LinePosition.Regular, display.LinePosition);
            Assert.Equal(new PointF(), display.Position);
            Assert.Equal(new Range(0, 1), display.Range);
            Assert.False(display.HasScript);
            Assert.Equal(Range.UndefinedInt, display.IndexInParent);
            Assert.Equal(3, display.Displays.Count());

            var line = display.Displays[0] as TextLineDisplay <TFont, TGlyph>;

            Assert.Single(line.Atoms);
            Assert.Equal("x", line.StringText());
            Assert.Equal(new PointF(), line.Position);
            Assert.True(line.HasScript);

            var display2 = display.Displays[1] as ListDisplay <TFont, TGlyph>;

            Assert.Equal(LinePosition.Superscript, display2.LinePosition);
            Assertions.ApproximatePoint(10.32, 9.68, display2.Position, 0.01);
            Assert.Equal(new Range(0, 1), display2.Range);
            Assert.False(display2.HasScript);
            Assert.Equal(0, display2.IndexInParent);
            Assert.Single(display2.Displays);

            var line2 = display2.Displays[0] as TextLineDisplay <TFont, TGlyph>;

            Assert.Single(line2.Atoms);
            Assert.Equal("2", line2.StringText());
            Assert.Equal(new PointF(), line2.Position);
            Assert.False(line2.HasScript);

            var display3 = display.Displays[2] as ListDisplay <TFont, TGlyph>;

            Assert.Equal(LinePosition.Subscript, display3.LinePosition);

            // Because both subscript and superscript are present, coords are
            // different from the subscript-only case.
            Assertions.ApproximatePoint(10, -6.12, display3.Position, 0.01);
            Assert.Equal(new Range(0, 1), display3.Range);
            Assert.False(display3.HasScript);
            Assert.Equal(0, display3.IndexInParent);
            Assert.Single(display3.Displays);

            var line3 = display3.Displays[0] as TextLineDisplay <TFont, TGlyph>;

            Assert.Single(line3.Atoms);
            Assert.Equal("1", line3.StringText());
            Assert.Equal(new PointF(), line3.Position);
            Assert.False(line3.HasScript);

            Assertions.ApproximatelyEqual(19.48, display.Ascent, 0.01);
            Assertions.ApproximatelyEqual(8.92, display.Descent, 0.01);
            Assertions.ApproximatelyEqual(17.32, display.Width, 0.01);
            Assertions.ApproximatelyEqual(display.Ascent, display2.Position.Y + line2.Ascent, 0.01);
            Assertions.ApproximatelyEqual(display.Descent, line3.Descent - display3.Position.Y, 0.01);
        }