ComponentFactory.Krypton.Toolkit.AccurateText.DrawString C# (CSharp) Method

DrawString() public static method

Pixel accurate drawing of the requested text memento information.
public static DrawString ( Graphics g, Brush brush, Rectangle rect, RightToLeft rtl, VisualOrientation orientation, bool composition, PaletteState state, AccurateTextMemento memento ) : bool
g System.Drawing.Graphics Graphics object used for drawing.
brush System.Drawing.Brush Brush for drawing text with.
rect System.Drawing.Rectangle Rectangle to draw text inside.
rtl RightToLeft Right to left setting for control.
orientation VisualOrientation Orientation for drawing text.
composition bool Should draw on a composition element.
state PaletteState State of the source element.
memento AccurateTextMemento Memento containing text context.
return bool
        public static bool DrawString(Graphics g,
                                      Brush brush,
                                      Rectangle rect,
                                      RightToLeft rtl,
                                      VisualOrientation orientation,
                                      bool composition,
                                      PaletteState state,
                                      AccurateTextMemento memento)
        {
            Debug.Assert(g != null);
            Debug.Assert(memento != null);

            // Cannot draw with a null graphics instance
            if (g == null)
                throw new ArgumentNullException("g");

            // Cannot draw with a null memento instance
            if (memento == null)
                throw new ArgumentNullException("memento");

            bool ret = true;

            // Is there a valid place to be drawn into
            if ((rect.Width > 0) && (rect.Height > 0))
            {
                // Does the memento contain something to draw?
                if (!memento.IsEmpty)
                {
                    int translateX = 0;
                    int translateY = 0;
                    float rotation = 0f;

                    // Perform any transformations needed for orientation
                    switch (orientation)
                    {
                        case VisualOrientation.Bottom:
                            // Translate to opposite side of origin, so the rotate can
                            // then bring it back to original position but mirror image
                            translateX = rect.X * 2 + rect.Width;
                            translateY = rect.Y * 2 + rect.Height;
                            rotation = 180f;
                            break;
                        case VisualOrientation.Left:
                            // Invert the dimensions of the rectangle for drawing upwards
                            rect = new Rectangle(rect.X, rect.Y, rect.Height, rect.Width);

                            // Translate back from a quater left turn to the original place
                            translateX = rect.X - rect.Y - 1;
                            translateY = rect.X + rect.Y + rect.Width;
                            rotation = 270;
                            break;
                        case VisualOrientation.Right:
                            // Invert the dimensions of the rectangle for drawing upwards
                            rect = new Rectangle(rect.X, rect.Y, rect.Height, rect.Width);

                            // Translate back from a quater right turn to the original place
                            translateX = rect.X + rect.Y + rect.Height + 1;
                            translateY = -(rect.X - rect.Y);
                            rotation = 90f;
                            break;
                    }

                    // Apply the transforms if we have any to apply
                    if ((translateX != 0) || (translateY != 0))
                        g.TranslateTransform(translateX, translateY);

                    if (rotation != 0f)
                        g.RotateTransform(rotation);

                    try
                    {
                        if (composition)
                            DrawCompositionGlowingText(g, memento.Text, memento.Font, rect, state,
                                                       SystemColors.ActiveCaptionText, true);
                        else
                            g.DrawString(memento.Text, memento.Font, brush, rect, memento.Format);
                    }
                    catch
                    {
                        // Ignore any error from the DrawString, usually because the display settings
                        // have changed causing Fonts to be invalid. Our controls will notice the change
                        // and refresh the fonts but sometimes the draw happens before the fonts are
                        // regenerated. Just ignore message and everything will sort itself out. Trust me!
                        ret = false;
                    }
                    finally
                    {
                        // Remove the applied transforms
                        if (rotation != 0f)
                            g.RotateTransform(-rotation);

                        if ((translateX != 0) || (translateY != 0))
                            g.TranslateTransform(-translateX, -translateY);
                    }
                }
            }

            return ret;
        }