Exemplo n.º 1
0
            private static char GetSimpleChar(LineChar charCenter, [NotNull] char[] map)
            {
                Debug.Assert(map.Length == 8);
                switch (charCenter)
                {
                case LineChar.Horizontal:
                    return(map[0]);

                case LineChar.Horizontal | LineChar.HorizontalWide:
                    return(map[1]);

                case LineChar.Vertical:
                    return(map[2]);

                case LineChar.Vertical | LineChar.VerticalWide:
                    return(map[3]);

                case LineChar.Horizontal | LineChar.Vertical:
                    return(map[4]);

                case LineChar.Horizontal | LineChar.Vertical | LineChar.HorizontalWide:
                    return(map[5]);

                case LineChar.Horizontal | LineChar.Vertical | LineChar.VerticalWide:
                    return(map[6]);

                case LineChar.Horizontal | LineChar.Vertical | LineChar.HorizontalWide | LineChar.VerticalWide:
                    return(map[7]);

                default:
                    throw new ArgumentOutOfRangeException(nameof(charCenter));
                }
            }
Exemplo n.º 2
0
 public override char GetChar(LineChar charCenter, LineChar charLeft, LineChar charTop, LineChar charRight, LineChar charBottom)
 {
     if (charCenter.IsEmpty())
     {
         return('\0');
     }
     if (charCenter.IsHorizontal() && charCenter.IsVertical())
     {
         return('+');
     }
     if (charCenter.IsHorizontal())
     {
         return(charCenter.IsHorizontalWide() ? '=' : '-');
     }
     if (charCenter.IsVertical())
     {
         return('|');
     }
     throw GetCharException(charCenter, charLeft, charTop, charRight, charBottom);
 }
Exemplo n.º 3
0
 private static char GetChar(LineChar charCenter, [NotNull] char[] map)
 {
     Debug.Assert(map.Length == 4);
     if (!charCenter.IsHorizontalWide() && !charCenter.IsVerticalWide())
     {
         return(map[0]);
     }
     if (charCenter.IsHorizontalWide() && !charCenter.IsVerticalWide())
     {
         return(map[1]);
     }
     if (!charCenter.IsHorizontalWide() && charCenter.IsVerticalWide())
     {
         return(map[2]);
     }
     if (charCenter.IsHorizontalWide() && charCenter.IsVerticalWide())
     {
         return(map[3]);
     }
     throw new ArgumentOutOfRangeException(nameof(charCenter));
 }
Exemplo n.º 4
0
            public override char GetChar(LineChar lineChar)
            {
                LineWidth horizontal = Max(lineChar.Left, lineChar.Right);
                LineWidth vertical   = Max(lineChar.Top, lineChar.Bottom);

                if (horizontal == LineWidth.None && vertical == LineWidth.None)
                {
                    return('\0');
                }
                if (horizontal != LineWidth.None && vertical != LineWidth.None)
                {
                    return('+');
                }
                if (horizontal != LineWidth.None)
                {
                    return(horizontal == LineWidth.Single ? '-' : '=');
                }
                if (vertical != LineWidth.None)
                {
                    return('|');
                }
                throw GetCharException(lineChar);
            }
Exemplo n.º 5
0
 public static bool IsVerticalWide(this LineChar @this) => (@this & LineChar.VerticalWide) != 0;
Exemplo n.º 6
0
 public static bool IsHorizontalWide(this LineChar @this) => (@this & LineChar.HorizontalWide) != 0;
Exemplo n.º 7
0
 public static bool IsNone(this LineChar @this) => @this == LineChar.None;
Exemplo n.º 8
0
 public static bool IsEmpty(this LineChar @this) => @this.IsNone() || [email protected]() && [email protected]();
Exemplo n.º 9
0
            public override char GetChar(LineChar charCenter, LineChar charLeft, LineChar charTop, LineChar charRight, LineChar charBottom)
            {
                if (charCenter.IsEmpty())
                {
                    return('\0');
                }
                // 0 connections
                if (charLeft.IsNone() && charTop.IsNone() && charRight.IsNone() && charBottom.IsNone())
                {
                    return(GetSimpleChar(charCenter, MapSimple));
                }

                bool connectLeft   = charCenter.IsHorizontal() && charLeft.IsHorizontal();
                bool connectTop    = charCenter.IsVertical() && charTop.IsVertical();
                bool connectRight  = charCenter.IsHorizontal() && charRight.IsHorizontal();
                bool connectBottom = charCenter.IsVertical() && charBottom.IsVertical();

                // 1 connection
                if ((connectLeft ? 1 : 0) + (connectTop ? 1 : 0) + (connectRight ? 1 : 0) + (connectBottom ? 1 : 0) <= 1)
                {
                    return(GetSimpleChar(charCenter, MapSimple));
                }
                // 4 connections
                if (connectLeft && connectTop && connectRight && connectBottom)
                {
                    return(GetChar(charCenter, MapLeftTopRightBottom));
                }
                // 3 connections
                if (connectLeft && connectTop && connectRight)
                {
                    return(GetChar(charCenter, MapLeftTopRight));
                }
                if (connectLeft && connectTop && connectBottom)
                {
                    return(GetChar(charCenter, MapLeftTopBottom));
                }
                if (connectLeft && connectRight && connectBottom)
                {
                    return(GetChar(charCenter, MapLeftRightBottom));
                }
                if (connectTop && connectRight && connectBottom)
                {
                    return(GetChar(charCenter, MapTopRightBottom));
                }
                // 2 connections
                if (connectTop && connectLeft)
                {
                    return(GetChar(charCenter, MapTopLeft));
                }
                if (connectTop && connectRight)
                {
                    return(GetChar(charCenter, MapTopRight));
                }
                if (connectBottom && connectRight)
                {
                    return(GetChar(charCenter, MapBottomRight));
                }
                if (connectBottom && connectLeft)
                {
                    return(GetChar(charCenter, MapBottomLeft));
                }
                if ((connectLeft && connectRight) || (connectTop && connectBottom))
                {
                    return(GetSimpleChar(charCenter, MapSimple));
                }
                throw GetCharException(charCenter, charLeft, charTop, charRight, charBottom);
            }
Exemplo n.º 10
0
 public override char GetChar(LineChar charCenter, LineChar charLeft, LineChar charTop, LineChar charRight, LineChar charBottom)
 {
     return(charCenter.IsEmpty() ? '\0' : _c);
 }
Exemplo n.º 11
0
 protected static Exception GetCharException(LineChar charCenter, LineChar charLeft, LineChar charTop, LineChar charRight, LineChar charBottom)
 {
     return(new NotSupportedException($"Line joint not supported: {charCenter} ({charLeft} {charTop} {charRight} {charBottom})."));
 }
Exemplo n.º 12
0
 public abstract char GetChar(LineChar charCenter, LineChar charLeft, LineChar charTop, LineChar charRight, LineChar charBottom);
Exemplo n.º 13
0
            public override char GetChar(LineChar lineChar)
            {
                const LineWidth None   = LineWidth.None;
                const LineWidth Single = LineWidth.Single;
                const LineWidth Double = LineWidth.Double;
                const LineWidth Heavy  = LineWidth.Heavy;

                LineWidth left   = lineChar.Left;
                LineWidth top    = lineChar.Top;
                LineWidth right  = lineChar.Right;
                LineWidth bottom = lineChar.Bottom;

                bool hasSingle = HasLineWidth(Single);
                bool hasDouble = HasLineWidth(Double);
                bool hasHeavy  = HasLineWidth(Heavy);
                int  lineCount = CountAllLines();

                if (hasHeavy && hasDouble)
                {
                    throw new InvalidOperationException("Heavy and Double line widths can't be combined.");
                }

                bool isHor         = left != None && right != None;
                bool isVer         = top != None && bottom != None;
                int  horIndex      = EvenIndex(left, right);
                int  verIndex      = EvenIndex(top, bottom);
                int  combinedIndex = CombineIndex(verIndex, horIndex);

                return(!hasSingle && !hasHeavy && !hasDouble ? '\0' : hasHeavy?GetHeavyChar() : GetDoubleChar());

                bool HasLineWidth(LineWidth width) => left == width || top == width || right == width || bottom == width;
                int CountAllLines() => (left != None ? 1 : 0) + (top != None ? 1 : 0) + (right != None ? 1 : 0) + (bottom != None ? 1 : 0);
                int CountLines(LineWidth width) => (left == width ? 1 : 0) + (top == width ? 1 : 0) + (right == width ? 1 : 0) + (bottom == width ? 1 : 0);
                int EvenIndex(LineWidth a, LineWidth b) => Max(a, b) == Single ? 0 : 1;
                int UnevenIndex(LineWidth a, LineWidth b) => a != None && b != None && a != b ? (a == Heavy ? 0 : 1) : -1;
                int CombineIndex(int index1, int index2) => index1 * 2 + index2;
                int FirstIndex(LineWidth width) => left == width ? 0 : top == width ? 1 : right == width ? 2 : bottom == width ? 3 : -1;

                char GetCornerChar(
                    char[] charsCornerTopLeft, char[] charsCornerTopRight,
                    char[] charsCornerBottomLeft, char[] charsCornerBottomRight)
                {
                    if (top != None && left != None)
                    {
                        return(charsCornerTopLeft[combinedIndex]);
                    }
                    else if (top != None && right != None)
                    {
                        return(charsCornerTopRight[combinedIndex]);
                    }
                    else if (bottom != None && left != None)
                    {
                        return(charsCornerBottomLeft[combinedIndex]);
                    }
                    else if (bottom != None && right != None)
                    {
                        return(charsCornerBottomRight[combinedIndex]);
                    }
                    throw GetCharException(lineChar);
                }

                char GetEvenTChar(char[] charsTRight, char[] charsTBottom, char[] charsTLeft, char[] charsTTop)
                {
                    if (left == None)
                    {
                        return(charsTRight[combinedIndex]);
                    }
                    else if (top == None)
                    {
                        return(charsTBottom[combinedIndex]);
                    }
                    else if (right == None)
                    {
                        return(charsTLeft[combinedIndex]);
                    }
                    else if (bottom == None)
                    {
                        return(charsTTop[combinedIndex]);
                    }
                    throw GetCharException(lineChar);
                }

                char GetHeavyChar()
                {
                    int  lineCountHeavy   = CountLines(Heavy);
                    int  horUnevenIndex   = UnevenIndex(left, right);
                    int  verUnevenIndex   = UnevenIndex(top, bottom);
                    int  firstSingleIndex = FirstIndex(Single);
                    int  firstHeavyIndex  = FirstIndex(Heavy);
                    bool isEven           = horUnevenIndex == -1 && verUnevenIndex == -1;

                    switch (lineCount)
                    {
                    case 1:
                        return(firstSingleIndex != -1 ? CharsHeavySideSingle[firstSingleIndex] : CharsHeavySideHeavy[firstHeavyIndex]);

                    case 2:
                        if (isHor)
                        {
                            return(horUnevenIndex == -1 ? CharsHeavyHorizontal[horIndex] : CharsHeavyHorizontalUneven[horUnevenIndex]);
                        }
                        else if (isVer)
                        {
                            return(verUnevenIndex == -1 ? CharsHeavyVertical[verIndex] : CharsHeavyVerticalUneven[horUnevenIndex]);
                        }
                        else
                        {
                            return(GetCornerChar(
                                       CharsHeavyCornerTopLeft, CharsHeavyCornerTopRight,
                                       CharsHeavyCornerBottomLeft, CharsHeavyCornerBottomRight));
                        }

                    case 3:
                        if (isEven)
                        {
                            return(GetEvenTChar(CharsHeavyTRight, CharsHeavyTBottom, CharsHeavyTLeft, CharsHeavyTTop));
                        }
                        else if (left == None)
                        {
                            return(CharsHeavyTRightUneven[CombineIndex(right == Single ? 1 : 0, verUnevenIndex)]);
                        }
                        else if (top == None)
                        {
                            return(CharsHeavyTBottomUneven[CombineIndex(bottom == Single ? 1 : 0, horUnevenIndex)]);
                        }
                        else if (right == None)
                        {
                            return(CharsHeavyTLeftUneven[CombineIndex(left == Single ? 1 : 0, verUnevenIndex)]);
                        }
                        else if (bottom == None)
                        {
                            return(CharsHeavyTTopUneven[CombineIndex(top == Single ? 1 : 0, horUnevenIndex)]);
                        }
                        break;

                    case 4:
                        if (isEven)
                        {
                            return(CharsHeavyCross[combinedIndex]);
                        }
                        switch (lineCountHeavy)
                        {
                        case 1:
                            return(CharsHeavyCrossUnevenSide[firstHeavyIndex]);

                        case 2:
                            return(CharsHeavyCrossUnevenCorner[CombineIndex(verUnevenIndex, horUnevenIndex)]);

                        case 3:
                            return(CharsHeavyCrossUnevenT[firstSingleIndex]);
                        }
                        break;
                    }
                    throw GetCharException(lineChar);
                }

                char GetDoubleChar()
                {
                    int firstSingleIndex = FirstIndex(Single);

                    switch (lineCount)
                    {
                    case 1:
                        return(_supportExtended
                                ? CharsHeavySideSingle[firstSingleIndex]
                                : left != None || right != None ? CharsDoubleHorizontal[horIndex] : CharsDoubleVertical[verIndex]);

                    case 2:
                        if (isHor)
                        {
                            return(CharsDoubleHorizontal[horIndex]);
                        }
                        else if (isVer)
                        {
                            return(CharsDoubleVertical[verIndex]);
                        }
                        else
                        {
                            return(GetCornerChar(
                                       CharsDoubleCornerTopLeft, CharsDoubleCornerTopRight,
                                       CharsDoubleCornerBottomLeft, CharsDoubleCornerBottomRight));
                        }

                    case 3:
                        return(GetEvenTChar(CharsDoubleTRight, CharsDoubleTBottom, CharsDoubleTLeft, CharsDoubleTTop));

                    case 4:
                        return(CharsDoubleCross[combinedIndex]);
                    }
                    throw GetCharException(lineChar);
                }
            }
Exemplo n.º 14
0
 public override char GetChar(LineChar lineChar)
 {
     return(lineChar.IsEmpty ? '\0' : _c);
 }
Exemplo n.º 15
0
 protected static Exception GetCharException(LineChar lineChar)
 {
     return(new NotSupportedException($"Line char '{lineChar}' not supported."));
 }
Exemplo n.º 16
0
 public abstract char GetChar(LineChar lineChar);