Exemplo n.º 1
0
 private static Brush GetFillBrush(ProjectLayoutElement zElement)
 {
     // prefer the background color
     return(CardRenderer.GetElementOpacityBrush(zElement,
                                                zElement.GetElementBackgroundColor() != CardMakerConstants.NoColor
             ? zElement.GetElementBackgroundColor()
             : zElement.GetElementColor()));
 }
Exemplo n.º 2
0
 private void UpdatePanelColors(ProjectLayoutElement zElement)
 {
     if (zElement != ElementManager.Instance.GetSelectedElement())
     {
         return;
     }
     panelBorderColor.BackColor  = zElement.GetElementBorderColor();
     panelOutlineColor.BackColor = zElement.GetElementOutlineColor();
     panelShapeColor.BackColor   = zElement.GetElementColor();
     panelFontColor.BackColor    = panelShapeColor.BackColor;
 }
Exemplo n.º 3
0
        public string ProcessInlineShape(IShapeRenderer zShapeRenderer, Graphics zGraphics, ProjectLayoutElement zElement, string sInput)
        {
            var   zExtendedMatch = regexShapeExtendedBG.Match(sInput);
            Match zMatch         = null;

            if (!zExtendedMatch.Success)
            {
                zMatch = regexShapeBG.Match(sInput);
                if (!zMatch.Success)
                {
                    return(sInput);
                }
            }

            var sToReplace = String.Empty;
            int nXOffset = 0, nYOffset = 0;

            var zBgShapeElement = new ProjectLayoutElement(Guid.NewGuid().ToString());

            if (zExtendedMatch.Success)
            {
                nXOffset                         = ParseUtil.ParseDefault(zExtendedMatch.Groups[6].Value, 0);
                nYOffset                         = ParseUtil.ParseDefault(zExtendedMatch.Groups[8].Value, 0);
                zBgShapeElement.x                = zElement.x;
                zBgShapeElement.y                = zElement.y;
                zBgShapeElement.width            = zElement.width + ParseUtil.ParseDefault(zExtendedMatch.Groups[10].Value, 0);
                zBgShapeElement.height           = zElement.height + ParseUtil.ParseDefault(zExtendedMatch.Groups[12].Value, 0);
                zBgShapeElement.outlinethickness = ParseUtil.ParseDefault(zExtendedMatch.Groups[14].Value, 0);
                zBgShapeElement.outlinecolor     = zExtendedMatch.Groups[16].Value;
                zBgShapeElement.elementcolor     = zExtendedMatch.Groups[4].Value;
                zBgShapeElement.variable         = zExtendedMatch.Groups[2].Value;
                zBgShapeElement.type             = ElementType.Shape.ToString();
                sToReplace                       = zExtendedMatch.Groups[0].Value;
            }
            else if (zMatch.Success)
            {
                zBgShapeElement.x            = zElement.x;
                zBgShapeElement.y            = zElement.y;
                zBgShapeElement.width        = zElement.width;
                zBgShapeElement.height       = zElement.height;
                zBgShapeElement.elementcolor = zMatch.Groups[4].Value;
                zBgShapeElement.variable     = zMatch.Groups[2].Value;
                zBgShapeElement.type         = ElementType.Shape.ToString();
                sToReplace = zMatch.Groups[0].Value;
            }

            zBgShapeElement.InitializeTranslatedFields();
            zBgShapeElement.opacity = zBgShapeElement.GetElementColor().A;

            zShapeRenderer.HandleShapeRender(zGraphics, zBgShapeElement.variable, zBgShapeElement, nXOffset, nYOffset);

            return(sInput.Replace(sToReplace, string.Empty));
        }
Exemplo n.º 4
0
 private void UpdatePanelColors(ProjectLayoutElement zElement)
 {
     if (zElement != ElementManager.Instance.GetSelectedElement())
     {
         return;
     }
     panelBorderColor.BackColor     = zElement.GetElementBorderColor();
     panelOutlineColor.BackColor    = zElement.GetElementOutlineColor();
     panelShapeColor.BackColor      = zElement.GetElementColor();
     panelFontColor.BackColor       = panelShapeColor.BackColor;
     panelBackgroundColor.BackColor = zElement.GetElementBackgroundColor() == CardMakerConstants.NoColor
         ? Control.DefaultBackColor
         : zElement.GetElementBackgroundColor();
 }
Exemplo n.º 5
0
        public string ProcessInlineShape(IShapeRenderer zShapeRenderer, Graphics zGraphics, ProjectLayoutElement zElement, string sInput)
        {
            var   kvpMatchedProcessor = new KeyValuePair <Regex, Action <Match, ProjectLayoutElement, ProjectLayoutElement, PointOffset> >();
            Match zMatch = null;

            foreach (var kvpShapeProcessor in listShapeProcessingPairs)
            {
                zMatch = kvpShapeProcessor.Key.Match(sInput);
                if (zMatch.Success)
                {
                    kvpMatchedProcessor = kvpShapeProcessor;
                    break;
                }
            }

            if (kvpMatchedProcessor.Key == null ||
                zMatch == null ||
                !zMatch.Success)
            {
                return(sInput);
            }

            var sToReplace      = zMatch.Groups[0].Value;
            var pointOffset     = new PointOffset();
            var zBgShapeElement = new ProjectLayoutElement(Guid.NewGuid().ToString());

            zBgShapeElement.opacity = -1;

            kvpMatchedProcessor.Value(zMatch, zBgShapeElement, zElement, pointOffset);

            zBgShapeElement.InitializeTranslatedFields();

            // the processor method didn't tweak the opacity, default to the element color alpha channel
            if (zBgShapeElement.opacity == -1)
            {
                zBgShapeElement.opacity = zBgShapeElement.GetElementColor().A;
            }

            zShapeRenderer.HandleShapeRender(zGraphics, zBgShapeElement.variable, zBgShapeElement, pointOffset.X, pointOffset.Y);

            return(sInput.Replace(sToReplace, string.Empty));
        }
Exemplo n.º 6
0
        public static void DrawElement(Graphics zGraphics, Deck zDeck, ProjectLayoutElement zElement, ElementType eType, int nX, int nY, string sInput, bool bExport)
        {
            switch (eType)
            {
            case ElementType.Graphic:
            case ElementType.Shape:
                sInput = sInput.Trim();
                break;
            }

            Font  zFont      = null;
            Brush zBrush     = null;
            Pen   zBorderPen = null;

            Color colorFont = Color.Black;

            if (0 != zElement.borderthickness)
            {
                zBorderPen = 255 != zElement.opacity
                    ? new Pen(Color.FromArgb(zElement.opacity, zElement.GetElementBorderColor()), zElement.borderthickness)
                    : new Pen(zElement.GetElementBorderColor(), zElement.borderthickness);
            }

            // Setup
            switch (eType)
            {
            case ElementType.Text:
            case ElementType.FormattedText:
                zFont     = zElement.GetElementFont();
                colorFont = zElement.GetElementColor();
                zBrush    = new SolidBrush(colorFont);
                break;

            case ElementType.Graphic:
            case ElementType.Shape:
                break;

            default:
                return;
            }

            // NOTE: this is the first transform
            if (0 != zElement.rotation)
            {
                // center the internal element then rotate and restore
                zGraphics.TranslateTransform(zElement.x + nX + (zElement.width >> 1), zElement.y + nY + (zElement.height >> 1));
                zGraphics.RotateTransform(zElement.rotation);
                zGraphics.TranslateTransform(-(zElement.width >> 1), -(zElement.height >> 1));
                if (CardMakerInstance.DrawElementBorder && CardMakerInstance.DrawSelectedElementRotationBounds && !bExport)
                {
                    zGraphics.DrawRectangle(Pens.LightGreen, 0, 0, zElement.width - 1, zElement.height - 1);
                }
            }
            else
            {
                zGraphics.TranslateTransform(zElement.x + nX, zElement.y + nY);
            }
            // TODO: an interface for all these would be more appropriate


            // Draw
            switch (eType)
            {
            case ElementType.Text:
                s_zDrawText.DrawText(zGraphics, zElement, sInput, zBrush, zFont, colorFont);
                break;

            case ElementType.FormattedText:
                DrawFormattedText(zGraphics, zDeck, zElement, sInput, zBrush, zFont, colorFont);
                break;

            case ElementType.Graphic:
                DrawGraphic(zGraphics, sInput, zElement);
                break;

            case ElementType.Shape:
                ShapeManager.HandleShapeRender(zGraphics, sInput.ToLower(), zElement);
                break;
            }

            if (null != zBorderPen)
            {
                // note that the border is inclusive in the width/height consuming 2 pixels (0 to total-1)
                zGraphics.DrawRectangle(zBorderPen, 0, 0, zElement.width - 1, zElement.height - 1);
            }

            zGraphics.ResetTransform();
        }
Exemplo n.º 7
0
        public static void HandleShapeRender(Graphics zGraphics, string sShapeInfo, ProjectLayoutElement zElement)
        {
            if (s_regexShapes.IsMatch(sShapeInfo))
            {
                var           zMatch     = s_regexShapes.Match(sShapeInfo);
                ShapeInfo     zInfo      = null;
                var           bParse     = false;
                var           arraySplit = zMatch.Groups[3].ToString().Split(new char[] { ';' });
                var           sShapeName = arraySplit[(int)AbstractShape.ShapeInformationIndex.Name];
                AbstractShape zShape;
                if (s_dictionaryShapeByName.TryGetValue(sShapeName, out zShape))
                {
                    // allow any shapes with extended settings to read in the values (ShapeInformationIndex extension)
                    zShape.InitializeItem(sShapeInfo);

                    if ((int)AbstractShape.ShapeInformationIndex.BasicShapeInformation < arraySplit.Length)
                    {
                        int nThickness;
                        var nOverrideWidth  = int.MinValue;
                        var nOverrideHeight = int.MinValue;
                        bParse = int.TryParse(arraySplit[(int)AbstractShape.ShapeInformationIndex.Thickness], out nThickness);
                        if (!arraySplit[(int)AbstractShape.ShapeInformationIndex.OverrideWidth].Equals(AbstractShape.NO_SIZE_OVERRIDE))
                        {
                            bParse &= int.TryParse(arraySplit[(int)AbstractShape.ShapeInformationIndex.OverrideWidth], out nOverrideWidth);
                        }
                        if (!arraySplit[(int)AbstractShape.ShapeInformationIndex.OverrideHeight].Equals(AbstractShape.NO_SIZE_OVERRIDE))
                        {
                            bParse &= int.TryParse(arraySplit[(int)AbstractShape.ShapeInformationIndex.OverrideHeight], out nOverrideHeight);
                        }
                        zInfo = new ShapeInfo(nThickness, nOverrideWidth, nOverrideHeight, arraySplit);
                    }
                    if (!bParse)
                    {
                        return; // invalid (error?)
                    }

                    var previousTransform = zGraphics.Transform;
                    var zPath             = new GraphicsPath();

                    var targetRect = new Rectangle(0, 0, zElement.width - 1, zElement.height - 1);

                    // internally int.MinValue indicates no override
                    if (int.MinValue != zInfo.OverrideWidth || int.MinValue != zInfo.OverrideHeight)
                    {
                        var nOverrideWidth  = int.MinValue == zInfo.OverrideWidth ? zElement.width : zInfo.OverrideWidth;
                        var nOverrideHeight = int.MinValue == zInfo.OverrideHeight ? zElement.height : zInfo.OverrideHeight;

                        if (0 == nOverrideWidth || 0 == nOverrideHeight)
                        {
                            // nothing to draw
                            return;
                        }
                        targetRect = GetZeroRectangle(nOverrideWidth, nOverrideHeight);
                        zGraphics.TranslateTransform(targetRect.X, targetRect.Y);
                        targetRect = new Rectangle(0, 0, Math.Abs(nOverrideWidth), Math.Abs(nOverrideHeight));
                    }

                    zShape.DrawShape(zPath, targetRect, zInfo);
                    DrawItem.DrawOutline(zElement, zGraphics, zPath);

                    if (0 == zInfo.Thickness)
                    {
                        var zShapeBrush = 255 != zElement.opacity
                            ? new SolidBrush(Color.FromArgb(zElement.opacity, zElement.GetElementColor()))
                            : new SolidBrush(zElement.GetElementColor());
                        zGraphics.FillPath(zShapeBrush, zPath);
                    }
                    else
                    {
                        var zShapePen = 255 != zElement.opacity
                            ? new Pen(Color.FromArgb(zElement.opacity, zElement.GetElementColor()), zInfo.Thickness)
                            : new Pen(zElement.GetElementColor(), zInfo.Thickness);
                        zGraphics.DrawPath(zShapePen, zPath);
                    }
                    zGraphics.Transform = previousTransform;
                }
            }
        }
Exemplo n.º 8
0
        public void Draw(Graphics zGraphics, Deck zDeck, ProjectLayoutElement zElement, string sInput)
        {
            var zFont     = zElement.GetElementFont();
            var colorFont = zElement.GetElementColor();

            // check the cache for this item
            var zDataFormattedCache = zDeck.GetCachedMarkup(zElement.name);

            // cached, render and exit
            if (null != zDataFormattedCache)
            {
                zDataFormattedCache.Render(zElement, zGraphics);
                return;
            }

            if (null == zFont) // default to something!
            {
                // font will show up in red if it's not yet set
                zFont = FontLoader.DefaultFont;
            }

            var zBrush = 255 == zElement.opacity
                ? new SolidBrush(colorFont)
                : new SolidBrush(Color.FromArgb(zElement.opacity, colorFont));

            zDataFormattedCache = new FormattedTextDataCache();
            var zFormattedData = new FormattedTextData(FormattedTextParser.GetMarkups(sInput));
            var zProcessData   = new FormattedTextProcessData
            {
                FontBrush              = zBrush,
                CurrentLineHeight      = zElement.lineheight,
                CurrentStringAlignment = zElement.GetHorizontalAlignment()
            };

            // set the initial font
            zProcessData.SetFont(zFont, zGraphics);

            var listPassMarkups = new List <MarkupBase>(); // only contains the markups that will be actively drawn (for caching)

            // Pass 1:
            // - Create rectangles
            // - Configure per-markup settings based on state of markup stack
            // - Generate list of markups to continue to process (those that are used in the next pass)
            // - Specify Markup rectanlges
            // - Generate markup rows (LineNumber is usable AFTER this process)
            int        nIdx;
            MarkupBase zMarkup;

            for (nIdx = 0; nIdx < zFormattedData.AllMarkups.Count; nIdx++)
            {
                zMarkup = zFormattedData.AllMarkups[nIdx];
                if (zMarkup.ProcessMarkup(zElement, zFormattedData, zProcessData, zGraphics))
                {
                    zMarkup.LineNumber = zProcessData.CurrentLine;
                    listPassMarkups.Add(zMarkup);
                }
            }

            // Pass 2:
            // - Trim spaces from line endings
            if (listPassMarkups.Count > 0)
            {
                nIdx    = listPassMarkups.Count - 1;
                zMarkup = listPassMarkups[nIdx];
                var currentLineNumber = zMarkup.LineNumber;
                var bFindNextLine     = false;
                while (nIdx > -1)
                {
                    zMarkup = listPassMarkups[nIdx];
                    if (zMarkup.LineNumber != currentLineNumber)
                    {
                        currentLineNumber = zMarkup.LineNumber;
                        bFindNextLine     = false;
                    }

                    if (!bFindNextLine && zMarkup is SpaceMarkup && ((SpaceMarkup)zMarkup).Optional)
                    {
                        listPassMarkups.RemoveAt(nIdx);
                    }
                    else
                    {
                        bFindNextLine = true;
                    }
                    nIdx--;
                }
            }

            // Pass 3:
            // - Align lines (horizontal/vertical)

            // Reprocess for align (before backgroundcolor is configured)
            AlignmentController.UpdateAlignment(zElement, listPassMarkups);

            // Pass 4: process the remaining items
            nIdx = 0;
            while (nIdx < listPassMarkups.Count)
            {
                zMarkup = listPassMarkups[nIdx];
                if (!zMarkup.PostProcessMarkupRectangle(zElement, listPassMarkups, nIdx))
                {
                    listPassMarkups.RemoveAt(nIdx);
                    nIdx--;
                }
                else
                {
                    zDataFormattedCache.AddMarkup(zMarkup);
                }
                nIdx++;
            }

            // update the cache
            zDeck.AddCachedMarkup(zElement.name, zDataFormattedCache);

            // render!
            zDataFormattedCache.Render(zElement, zGraphics);
        }
Exemplo n.º 9
0
        public void DrawText(Graphics zGraphics, ProjectLayoutElement zElement, string sInput)
        {
            var zFont     = zElement.GetElementFont();
            var colorFont = zElement.GetElementColor();

            if (null == zFont) // default to something!
            {
                // font will show up in red if it's not yet set
                zFont = FontLoader.DefaultFont;
            }
            var zFormat = new StringFormat
            {
                LineAlignment = zElement.GetVerticalAlignment(),
                Alignment     = zElement.GetHorizontalAlignment()
            };

            var zBrush = 255 == zElement.opacity
                ? new SolidBrush(colorFont)
                : new SolidBrush(Color.FromArgb(zElement.opacity, colorFont));

            if (zElement.autoscalefont)
            {
                SizeF zSize = zGraphics.MeasureString(sInput, zFont, new SizeF(zElement.width, int.MaxValue), zFormat);

                if (zSize.Height > zElement.height || zSize.Width > zElement.width)
                {
                    float newSizeRatio;
                    if ((zSize.Height - zElement.height) > (zSize.Width - zElement.width))
                    {
                        newSizeRatio = (float)zElement.height / (float)zSize.Height;
                    }
                    else
                    {
                        newSizeRatio = (float)zElement.width / (float)zSize.Width;
                    }

                    var scaledFont = FontLoader.GetFont(zFont.FontFamily, newSizeRatio * zFont.Size, zFont.Style);
                    //Support.IO.Logger.AddLogLine(scaledFont.Size + " was [" + zFont.Size + "]");
                    zFont = scaledFont;

#if true            // the preprocessing above will get the font size close but not perfect, the slow code below further refines the size
                    // slow mode - but perfect precision (well arguably with the Graphics.MeasureString)
                    bool bUpscaled = false;
                    if (0 < sInput.Trim().Length)
                    {
                        while (true)
                        {
                            zSize = zGraphics.MeasureString(sInput, zFont, new SizeF(zElement.width, int.MaxValue), zFormat);
                            if (zSize.Height > zElement.height || zSize.Width > zElement.width)
                            {
                                if (zFont.Size == 1)
                                {
                                    break;
                                }
                                zFont = FontLoader.GetFont(zFont.FontFamily, zFont.Size - 1, zFont.Style);
                                if (bUpscaled)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                zFont     = FontLoader.GetFont(zFont.FontFamily, zFont.Size + 1, zFont.Style);
                                bUpscaled = true;
                            }
                        }
                        //Support.IO.Logger.AddLogLine("[" + zFont.Size + "]");
                    }
#endif
                }
                // else -- font size is fine for this element
            }
            else
            {
                zFormat.Trimming = StringTrimming.EllipsisCharacter;
            }

            var fEmSize = zFont.Size;

            switch (zFont.Unit)
            {
            case GraphicsUnit.Point:
                fEmSize = zGraphics.DpiY * (zFont.Size / 72f);
                break;

            default:
                Logger.AddLogLine("This font is using the Unit: {0} (not currently supported)".FormatString(zFont.Unit.ToString()));
                break;
            }

            if (0 == zElement.outlinethickness)
            {
                try
                {
                    zGraphics.DrawString(sInput, zFont, zBrush,
                                         new RectangleF(0, 0, zElement.width, zElement.height), zFormat);
                }
                catch (Exception)
                {
                    Logger.AddLogLine("Unable to render text (font issue?)");
                }
            }
            else
            {
                try
                {
                    var zPath = new GraphicsPath();
                    zPath.AddString(sInput, zFont.FontFamily, (int)zFont.Style, fEmSize, new RectangleF(0, 0, zElement.width, zElement.height), zFormat);
                    CardRenderer.DrawElementPath(zElement, zGraphics, zPath, zBrush);
                }
                catch (Exception)
                {
                    Logger.AddLogLine("Unable to render text (font issue?)");
                }
            }
        }
Exemplo n.º 10
0
        public void HandleShapeRender(Graphics zGraphics, string sShapeInfo, ProjectLayoutElement zElement, int nXOffset = 0, int nYOffset = 0)
        {
            if (!s_regexShapes.IsMatch(sShapeInfo))
            {
                return;
            }

            var           zMatch     = s_regexShapes.Match(sShapeInfo);
            ShapeInfo     zShapeInfo = null;
            var           bParse     = false;
            var           arraySplit = zMatch.Groups[3].ToString().Split(new char[] { ';' });
            var           sShapeName = arraySplit[(int)AbstractShape.ShapeInformationIndex.Name];
            AbstractShape zShape;

            if (s_dictionaryShapeByName.TryGetValue(sShapeName, out zShape))
            {
                // allow any shapes with extended settings to read in the values (ShapeInformationIndex extension)
                zShape.InitializeItem(sShapeInfo);

                if ((int)AbstractShape.ShapeInformationIndex.BasicShapeInformation < arraySplit.Length)
                {
                    int nThickness;
                    var nOverrideWidth  = Int32.MinValue;
                    var nOverrideHeight = Int32.MinValue;
                    bParse = Int32.TryParse(arraySplit[(int)AbstractShape.ShapeInformationIndex.Thickness], out nThickness);
                    if (!arraySplit[(int)AbstractShape.ShapeInformationIndex.OverrideWidth].Equals(AbstractShape.NO_SIZE_OVERRIDE))
                    {
                        bParse &= Int32.TryParse(arraySplit[(int)AbstractShape.ShapeInformationIndex.OverrideWidth], out nOverrideWidth);
                    }
                    if (!arraySplit[(int)AbstractShape.ShapeInformationIndex.OverrideHeight].Equals(AbstractShape.NO_SIZE_OVERRIDE))
                    {
                        bParse &= Int32.TryParse(arraySplit[(int)AbstractShape.ShapeInformationIndex.OverrideHeight], out nOverrideHeight);
                    }
                    zShapeInfo = new ShapeInfo(nThickness, nOverrideWidth, nOverrideHeight, arraySplit);
                }
                if (!bParse)
                {
                    return; // invalid (error?)
                }

                var previousTransform = zGraphics.Transform;
                var zPath             = new GraphicsPath();

                var targetRect = new Rectangle(0, 0, zElement.width - 1, zElement.height - 1);

                // internally int.MinValue indicates no override
                if (Int32.MinValue != zShapeInfo.OverrideWidth || Int32.MinValue != zShapeInfo.OverrideHeight)
                {
                    var nOverrideWidth  = Int32.MinValue == zShapeInfo.OverrideWidth ? zElement.width : zShapeInfo.OverrideWidth;
                    var nOverrideHeight = Int32.MinValue == zShapeInfo.OverrideHeight ? zElement.height : zShapeInfo.OverrideHeight;

                    if (0 == nOverrideWidth || 0 == nOverrideHeight)
                    {
                        // nothing to draw
                        return;
                    }
                    targetRect = GetZeroRectangle(nOverrideWidth, nOverrideHeight);
                    zGraphics.TranslateTransform(targetRect.X, targetRect.Y);
                    targetRect = new Rectangle(0, 0, Math.Abs(nOverrideWidth), Math.Abs(nOverrideHeight));
                }
                else if (nXOffset != 0 || nYOffset != 0)
                {
                    zGraphics.TranslateTransform(nXOffset, nYOffset);
                }

                zShape.DrawShape(zPath, targetRect, zShapeInfo);

                if (0 == zShapeInfo.Thickness)
                {
                    CardRenderer.DrawElementPath(zElement, zGraphics, zPath, GetFillBrush(zElement));
                }
                else
                {
                    var zFillBrush = zElement.GetElementBackgroundColor() != CardMakerConstants.NoColor
                        ? CardRenderer.GetElementOpacityBrush(zElement, zElement.GetElementBackgroundColor())
                        : null;

                    CardRenderer.DrawElementPath(zElement, zGraphics, zPath, zFillBrush);
                    zGraphics.DrawPath(CardRenderer.GetElementOpacityPen(zElement, zElement.GetElementColor(), zShapeInfo.Thickness), zPath);
                }
                zGraphics.Transform = previousTransform;
            }
        }
        public void DrawText(Graphics zGraphics, ProjectLayoutElement zElement, string sInput)
        {
            var zFont     = zElement.GetElementFont();
            var colorFont = zElement.GetElementColor();

            if (null == zFont) // default to something!
            {
                // font will show up in red if it's not yet set
                zFont = FontLoader.DefaultFont;
            }

            var zBrush = 255 == zElement.opacity
                ? new SolidBrush(colorFont)
                : new SolidBrush(Color.FromArgb(zElement.opacity, colorFont));

            TextFormatFlags zFormatFlags =
                TextFormatFlags.WordBreak
                | TextFormatFlags.NoClipping;

            switch (zElement.GetVerticalAlignment())
            {
            case StringAlignment.Center:
                zFormatFlags |= TextFormatFlags.VerticalCenter;
                break;

            case StringAlignment.Far:
                zFormatFlags |= TextFormatFlags.Bottom;
                break;
            }

            switch (zElement.GetHorizontalAlignment())
            {
            case StringAlignment.Center:
                zFormatFlags |= TextFormatFlags.HorizontalCenter;
                break;

            case StringAlignment.Far:
                zFormatFlags |= TextFormatFlags.Right;
                break;
            }

#warning TextRenderer apparently does not support opactiy?!
            Bitmap zOpacityBitmap = null;
            if (255 != zElement.opacity)
            {
                zOpacityBitmap = new Bitmap(zElement.width, zElement.height, PixelFormat.Format32bppArgb);
                zOpacityBitmap.SetResolution(zGraphics.DpiY, zGraphics.DpiY);
            }

            if (zElement.autoscalefont)
            {
                var zSize = TextRenderer.MeasureText(sInput, zFont, new Size(zElement.width, int.MaxValue), zFormatFlags);

                if (zSize.Height > zElement.height || zSize.Width > zElement.width)
                {
                    float newSizeRatio;
                    if ((zSize.Height - zElement.height) > (zSize.Width - zElement.width))
                    {
                        newSizeRatio = (float)zElement.height / (float)zSize.Height;
                    }
                    else
                    {
                        newSizeRatio = (float)zElement.width / (float)zSize.Width;
                    }

                    //var scaledFont = new Font(zFont.FontFamily, newSizeRatio * zFont.Size, zFont.Style);
                    var scaledFont = new Font(zFont.FontFamily, newSizeRatio * zFont.Size, zFont.Style);
                    //Logger.AddLogLine(scaledFont.Size + " was [" + zFont.Size + "]");
                    zFont = scaledFont;

#if true            // the preprocessing above will get the font size close but not perfect, the slow code below further refines the size
                    // slow mode - but perfect precision (well arguably with the Graphics.MeasureString)
                    bool        bUpscaled         = false;
                    const float FONT_SCALE_ADJUST = 0.25f;
                    if (0 < sInput.Trim().Length)
                    {
                        while (true)
                        {
                            zSize = TextRenderer.MeasureText(sInput, zFont, new Size(zElement.width, int.MaxValue), zFormatFlags);
                            if (zSize.Height > zElement.height || zSize.Width > zElement.width)
                            {
                                if (zFont.Size <= 1)
                                {
                                    break;
                                }
                                zFont = new Font(zFont.FontFamily, zFont.Size - FONT_SCALE_ADJUST, zFont.Style);
                                //Logger.AddLogLine("ADJ A [" + zFont.Size + "]");
                                if (bUpscaled)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                zFont = new Font(zFont.FontFamily, zFont.Size + FONT_SCALE_ADJUST, zFont.Style);
                                //Logger.AddLogLine("ADJ B [" + zFont.Size + "]");
                                bUpscaled = true;
                            }
                        }
                    }
#endif
                }
                // else -- font size is fine for this element
            }
            else
            {
                zFormatFlags |= TextFormatFlags.EndEllipsis;
            }

            Logger.AddLogLine("[" + zFont.Size + "]");

            var arrayDrawLines = new string[] { sInput };
            var nLineOffset    = 0;

            var fEmSize = zFont.Size;

            switch (zFont.Unit)
            {
            case GraphicsUnit.Point:
                fEmSize = zGraphics.DpiY * (zFont.Size / 72f);
                break;

            default:
                Logger.AddLogLine("This font is using the Unit: {0} (not currently supported)".FormatString(zFont.Unit.ToString()));
                break;
            }

            foreach (var sLine in arrayDrawLines)
            {
                if (0 == zElement.outlinethickness)
                {
                    try
                    {
                        // https://stackoverflow.com/questions/849531/textrenderer-drawtext-in-bitmap-vs-onpaintbackground/1578056#1578056
                        if (null != zOpacityBitmap)
                        {
                            zFont = new Font("SkyScrappers Regular", zFont.Size);

                            // TODO: https://stackoverflow.com/questions/18838037/drawing-text-to-a-bitmap-with-textrenderer

#warning too bad this makes the font look terrible
#if false
                            var image = new Bitmap(zElement.width, zElement.height, PixelFormat.Format32bppArgb);

                            // create memory buffer from desktop handle that supports alpha channel
                            IntPtr dib;
                            var    memoryHdc = CreateMemoryHdc(IntPtr.Zero, image.Width, image.Height, out dib);
                            try
                            {
                                // create memory buffer graphics to use for HTML rendering
                                using (var memoryGraphics = Graphics.FromHdc(memoryHdc))
                                {
                                    // must not be transparent background
                                    memoryGraphics.Clear(Color.White);

                                    TextRenderer.DrawText(memoryGraphics, sLine, zFont, new Rectangle(0, 0, zElement.width, zElement.height), colorFont, zFormatFlags);
                                }

                                // copy from memory buffer to image
                                using (var imageGraphics = Graphics.FromImage(image))
                                {
                                    var imgHdc = imageGraphics.GetHdc();
                                    BitBlt(imgHdc, 0, 0, image.Width, image.Height, memoryHdc, 0, 0, 0x00CC0020);
                                    imageGraphics.ReleaseHdc(imgHdc);
                                }
                            }
                            finally
                            {
                                // release memory buffer
                                DeleteObject(dib);
                                DeleteDC(memoryHdc);
                            }
                            zGraphics.DrawImageUnscaled(image, 0, 0);
#else
#if false
                            using (Bitmap buffer = new Bitmap(zElement.width, zElement.height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                            {
                                using (Graphics graphics = Graphics.FromImage(buffer))
                                {
                                    graphics.FillRectangle(Brushes.Transparent, 0, 0, zElement.width, zElement.height);
                                    // Produces the result below
                                    //graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                                    // Produces clean text, but I'd really like ClearType!
                                    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                                    TextRenderer.DrawText(graphics, sLine, zFont, new Rectangle(0, 0, zElement.width, zElement.height), colorFont, zFormatFlags);
                                }
                                zGraphics.DrawImageUnscaled(buffer, 0, 0);
                            }
#else
                            var zGraphicsTemp = Graphics.FromImage(zOpacityBitmap);
                            zGraphicsTemp.SmoothingMode     = SmoothingMode.AntiAlias;
                            zGraphicsTemp.TextRenderingHint = TextRenderingHint.AntiAlias;

                            TextRenderer.DrawText(zGraphicsTemp, sLine, zFont, new Rectangle(0, 0, zElement.width, zElement.height), colorFont, zFormatFlags);

                            var zColor = new ColorMatrix
                            {
                                Matrix33 = (float)zElement.opacity / 255.0f
                            };
                            var zAttrib = new ImageAttributes();
                            zAttrib.SetColorMatrix(zColor);
                            var zBitmap    = new Bitmap(zOpacityBitmap.Width, zOpacityBitmap.Height); // target image
                            var zGraphicsX = Graphics.FromImage(zBitmap);
                            // draw the source image into the destination with the desired opacity
                            zGraphicsX.DrawImage(zOpacityBitmap, new Rectangle(0, 0, zBitmap.Width, zBitmap.Height), 0, 0, zBitmap.Width, zBitmap.Height, GraphicsUnit.Pixel,
                                                 zAttrib);

                            /**
                             *
                             *             var zImageAttributes = new ImageAttributes();
                             * if (255 != zElement.opacity)
                             * {
                             * var zColor = new ColorMatrix
                             * {
                             * Matrix33 = (float)zElement.opacity / 255.0f
                             * };
                             * zImageAttributes.SetColorMatrix(zColor);
                             * }
                             *
                             * zDestinationBitmap = new Bitmap(nTargetWidth, nTargetHeight); // target image
                             * var zGraphics = Graphics.FromImage(zDestinationBitmap);
                             * // draw the source image into the destination with the desired opacity
                             * zGraphics.DrawImage(zSourceBitmap, new Rectangle(0, 0, nTargetWidth, nTargetHeight), 0, 0, zSourceBitmap.Width, zSourceBitmap.Height, GraphicsUnit.Pixel,
                             * zImageAttributes);
                             * */


                            zGraphics.DrawImageUnscaled(zBitmap, 0, 0);
#endif
#endif
                        }
                        else
                        {
                            TextRenderer.DrawText(zGraphics, sLine, zFont, new Rectangle((int)zGraphics.Transform.OffsetX, (int)zGraphics.Transform.OffsetY, zElement.width,
                                                                                         zElement.height), colorFont, zFormatFlags);
                        }
                    }
                    catch (Exception)
                    {
                        Logger.AddLogLine("Unable to render text (font issue?)");
                    }
                }
                else
                {
                    // prepare to draw text
                    var zPath = new GraphicsPath();

                    try
                    {
#warning is there a path based text renderer thing to use?
                        var zFormat = new StringFormat
                        {
                            LineAlignment = zElement.GetVerticalAlignment(),
                            Alignment     = zElement.GetHorizontalAlignment(),
                            Trimming      = StringTrimming.None,
                            FormatFlags   = StringFormatFlags.NoClip
                        };

                        zPath.AddString(sLine, zFont.FontFamily, (int)zFont.Style, fEmSize, new RectangleF(0, nLineOffset, zElement.width, zElement.height), zFormat);
                        CardRenderer.DrawPathOutline(zElement, zGraphics, zPath);
                    }
                    catch (Exception)
                    {
                        Logger.AddLogLine("Unable to render text (font issue?)");
                    }

                    // fill in the outline
                    zGraphics.FillPath(zBrush, zPath);
                }
                nLineOffset += zElement.lineheight;
            }
        }
Exemplo n.º 12
0
 public static Bitmap LoadCustomImageFromCache(string sFile, ProjectLayoutElement zElement,
                                               int nTargetWidth = -1, int nTargetHeight = -1)
 {
     return(LoadCustomImageFromCache(sFile, zElement, zElement.GetElementColor(), nTargetWidth, nTargetHeight));
 }
Exemplo n.º 13
0
        /// <summary>
        /// Processes the formatted text data into rendered markups.
        /// </summary>
        /// <param name="zGraphics">The graphics to use to plan the render</param>
        /// <param name="zElement">The element</param>
        /// <param name="fMainFontSize">The font size to use for the default font</param>
        /// <param name="zFormattedTextData">The processed formatted text data</param>
        /// <returns>List of markups to be rendered</returns>
        protected List <MarkupBase> processMarkupDefinition(Graphics zGraphics, ProjectLayoutElement zElement, float fMainFontSize, FormattedTextData zFormattedTextData)
        {
            var colorFont = zElement.GetElementColor();
            var zBrush    = 255 == zElement.opacity
                ? new SolidBrush(colorFont)
                : new SolidBrush(Color.FromArgb(zElement.opacity, colorFont));

            var zProcessData = new FormattedTextProcessData
            {
                FontBrush              = zBrush,
                CurrentLineHeight      = zElement.lineheight,
                CurrentStringAlignment = zElement.GetHorizontalAlignment(),
                CurrentMarginLeft      = 0,
                CurrentMarginRight     = zElement.width
            };

            var zFont = zElement.GetElementFont();

            if (null == zFont) // default to something!
            {
                // font will show up in red if it's not yet set
                zFont = FontLoader.DefaultFont;
            }

            var zScaledFont = FontLoader.GetFont(zFont.FontFamily, fMainFontSize, zFont.Style);

            // set the initial font
            zProcessData.SetFont(zScaledFont, zGraphics);

            var listPassMarkups =
                new List <MarkupBase>(); // only contains the markups that will be actively drawn (for caching)

            // Pass 1:
            // - Create rectangles
            // - Configure per-markup settings based on state of markup stack
            // - Generate list of markups to continue to process (those that are used in the next pass)
            // - Specify Markup rectanlges
            // - Generate markup rows (LineNumber is usable AFTER this process)
            int        nIdx;
            MarkupBase zMarkup;

            for (nIdx = 0; nIdx < zFormattedTextData.AllMarkups.Count; nIdx++)
            {
                zMarkup = zFormattedTextData.AllMarkups[nIdx];
                if (zMarkup.ProcessMarkup(zElement, zFormattedTextData, zProcessData, zGraphics))
                {
                    zMarkup.LineNumber = zProcessData.CurrentLine;
                    listPassMarkups.Add(zMarkup);
                }
            }

            // Pass 2:
            // - Trim spaces from line endings
            if (listPassMarkups.Count > 0)
            {
                nIdx    = listPassMarkups.Count - 1;
                zMarkup = listPassMarkups[nIdx];
                var currentLineNumber = zMarkup.LineNumber;
                var bFindNextLine     = false;
                while (nIdx > -1)
                {
                    zMarkup = listPassMarkups[nIdx];
                    if (zMarkup.LineNumber != currentLineNumber)
                    {
                        currentLineNumber = zMarkup.LineNumber;
                        bFindNextLine     = false;
                    }

                    if (!bFindNextLine && zMarkup is SpaceMarkup && ((SpaceMarkup)zMarkup).Optional)
                    {
                        listPassMarkups.RemoveAt(nIdx);
                    }
                    else
                    {
                        bFindNextLine = true;
                    }

                    nIdx--;
                }
            }

            // Pass 3:
            // - Align lines (horizontal/vertical)

            // Reprocess for align (before backgroundcolor is configured)
            AlignmentController.UpdateAlignment(zElement, listPassMarkups);

            // Pass 4: process the remaining items
            nIdx = 0;
            while (nIdx < listPassMarkups.Count)
            {
                zMarkup = listPassMarkups[nIdx];
                if (!zMarkup.PostProcessMarkupRectangle(zElement, listPassMarkups, nIdx))
                {
                    listPassMarkups.RemoveAt(nIdx);
                    continue;
                }
                nIdx++;
            }

            return(listPassMarkups);
        }