예제 #1
0
        public override Box CreateBox(TexStyle style)
        {
            float lineHeight = lineThickness * TexUtility.SizeFactor(style);

            // Create boxes for numerator and demoninator atoms, and make them of equal width.
            var numeratorBox = numerator == null ? StrutBox.Empty :
                               numerator.CreateBox(TexUtility.GetNumeratorStyle(style));
            var denominatorBox = denominator == null ? StrutBox.Empty :
                                 denominator.CreateBox(TexUtility.GetDenominatorStyle(style));

            float maxWidth = (numeratorBox.width < denominatorBox.width ? denominatorBox.width : numeratorBox.width) + TEXConfiguration.main.FractionMargin * TexUtility.SizeFactor(style);

            numeratorBox   = HorizontalBox.Get(numeratorBox, maxWidth, numeratorAlignment);
            denominatorBox = HorizontalBox.Get(denominatorBox, maxWidth, denominatorAlignment);

            // Calculate preliminary shift-up and shift-down amounts.
            float shiftUp, shiftDown;
            var   styleFactor = TexUtility.SizeFactor(style);

            if (style >= TexStyle.Text)
            {
                styleFactor *= TEXConfiguration.main.FractionNarrowFactor;
            }

            shiftUp   = TEXConfiguration.main.NumeratorShift * styleFactor;
            shiftDown = TEXConfiguration.main.DenominatorShift * styleFactor;

            // Create result box.
            var resultBox = VerticalBox.Get();

            // add box for numerator.
            resultBox.Add(numeratorBox);

            // Calculate clearance and adjust shift amounts.
            //var axis = TEXConfiguration.main.AxisHeight * TexUtility.SizeFactor(style);

            // Calculate clearance amount.
            float clearance = lineHeight > 0 ? TEXConfiguration.main.FractionGap : TEXConfiguration.main.FractionGapNoLine;

            // Adjust shift amounts.
            var kern1  = shiftUp - numeratorBox.depth;
            var kern2  = shiftDown - denominatorBox.height;
            var delta1 = clearance - kern1;
            var delta2 = clearance - kern2;

            if (delta1 > 0)
            {
                shiftUp += delta1;
                kern1   += delta1;
            }
            if (delta2 > 0)
            {
                shiftDown += delta2;
                kern2     += delta2;
            }

            if (lineHeight > 0)
            {
                // Draw fraction line.

                resultBox.Add(StrutBox.Get(0, kern1, 0, 0));
                resultBox.Add(HorizontalRule.Get(lineHeight, numeratorBox.width, 0));
                resultBox.Add(StrutBox.Get(0, kern2, 0, 0));
            }
            else
            {
                // Do not draw fraction line.

                var kern = kern1 + kern2;
                resultBox.Add(StrutBox.Get(0, kern, 0, 0));
            }

            // add box for denominator.
            resultBox.Add(denominatorBox);

            // Adjust height and depth of result box.
            resultBox.height = shiftUp + numeratorBox.height;
            resultBox.depth  = shiftDown + lineHeight + denominatorBox.depth;

            TexUtility.CentreBox(resultBox, style);
            return(resultBox);
        }