Click here to Skip to main content
15,885,853 members
Articles / Programming Languages / C#

OOP in the Real World - Creating an Equation Editor

Rate me:
Please Sign up or sign in to vote.
4.87/5 (106 votes)
21 Apr 2015MIT11 min read 137.1K   10.5K   220  
Object Oriented Design and Programming process using a real world example
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using System.Globalization;

namespace Editor
{ 
    public class FontFactory
    {
        private FontFactory() { }
        
        public static FormattedText GetFormattedText(string textToFormat, FontType fontType, double fontSize)
        {
            return GetFormattedText(textToFormat, fontType, fontSize, FontStyles.Normal, FontWeights.Normal);
        }

        public static FormattedText GetFormattedText(string textToFormat, FontType fontType, double fontSize, FontWeight fontWeight)
        {
            return GetFormattedText(textToFormat, fontType, fontSize, FontStyles.Normal, fontWeight);
        }
        
        public static FormattedText GetFormattedText(string textToFormat, FontType fontType, double fontSize, FontStyle fontStyle, FontWeight fontWeight)
        {
            return GetFormattedText(textToFormat, fontType, fontSize, fontStyle, fontWeight, Brushes.Black);
        }

        public static FormattedText GetFormattedText(string textToFormat, FontType fontType, double fontSize, Brush brush)
        {
            return GetFormattedText(textToFormat, fontType, fontSize, FontStyles.Normal, FontWeights.Normal, brush);
        }


        public static FormattedText GetFormattedText(string textToFormat, FontType fontType, double fontSize, FontStyle fontStyle, FontWeight fontWeight, Brush brush)
        {
            Typeface typeface = GetTypeface(fontType, fontStyle, fontWeight);
            return new FormattedText(textToFormat, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, typeface, fontSize, brush);
        }

        public static FontFamily GetFontFamily(FontType fontType)
        {
            switch (fontType)
            {
                case FontType.STIXGeneral:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXGeneral");
                case FontType.STIXIntegralsD:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXIntegralsD");
                case FontType.STIXIntegralsSm:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXIntegralsSm");
                case FontType.STIXIntegralsUp:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXIntegralsUp");
                case FontType.STIXIntegralsUpD:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXIntegralsUpD");
                case FontType.STIXIntegralsUpSm:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXIntegralsUpSm");
                case FontType.STIXNonUnicode:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXNonUnicode");
                case FontType.STIXSizeFiveSym:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXSizeFiveSym");
                case FontType.STIXSizeFourSym:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXSizeFourSym");
                case FontType.STIXSizeOneSym:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXSizeOneSym");
                case FontType.STIXSizeThreeSym:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXSizeThreeSym");
                case FontType.STIXSizeTwoSym:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXSizeTwoSym");
                case FontType.STIXVariants:
                    return new FontFamily(new Uri("pack://application:,,,/STIX/"), "./#STIXVariants");
                case FontType.Arial:
                    return new FontFamily("Arial");
                case FontType.Segoe:
                    return new FontFamily("Segoe UI");
            }
            return new FontFamily("Segoe UI");
        }

        public static Typeface GetTypeface(FontType fontType, FontStyle fontStyle, FontWeight fontWeight)
        {
            return new Typeface(GetFontFamily(fontType), fontStyle, fontWeight, FontStretches.Normal);
        }        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Technical Lead https://mathiversity.com
Unknown
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

Comments and Discussions