private static Bitmap CreateInfomationBitmap(string content, int width, int height,
			StringAlignment hAlign = StringAlignment.Near,
			StringAlignment vAlign = StringAlignment.Near)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(b);
            GraphicsPath path = new GraphicsPath();
            FontFamily fontFamily = new FontFamily("å¾®č½Æ雅黑");
            StringFormat format = new StringFormat();
            format.Alignment = hAlign;
            format.LineAlignment = vAlign;
            path.AddString(content,
                fontFamily, (int)FontStyle.Bold, 16, new Rectangle(0, 0, width, height), format);
            fontFamily.Dispose();
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.FillPath(Brushes.Black, path);
            Pen pen = new Pen(Color.Black, 2);
            g.DrawPath(pen, path);
            g.Dispose();
            pen.Dispose();
            Bitmap infoBitmap = RenderUtils.BoxBlur(b, 1);
            g = Graphics.FromImage(infoBitmap);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.FillPath(Brushes.White, path);
            g.Dispose();
            return infoBitmap;
        }
        /// <summary>
        /// ē”»čƒŒę™Æå¹²ę‰°ę–‡å­—
        /// </summary>
        /// <returns></returns>
        private Bitmap DrawRandBgString()
        {
            Bitmap b = new Bitmap(bgWidth, bgHeight);
            String[] randStr =
            {
                "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
                "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
            };
            b.MakeTransparent();
            Graphics g = Graphics.FromImage(b);

            g.Clear(Color.Transparent);
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;

            //č®¾ē½®å­—ä½“ę˜¾ē¤ŗę ¼å¼
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            FontFamily f = new FontFamily(GenericFontFamilies.Serif);
            Font textFont = new Font(f, randomStringFontSize, FontStyle.Underline);

            int randAngle = 60; //随ęœŗč½¬åŠØ角åŗ¦

            for (int i = 0; i < RandomStringCount; i++)
            {
                Brush brush = new System.Drawing.SolidBrush(GetRandomLightColor());
                Point pot = new Point(random.Next(5, bgWidth - 5), random.Next(5, bgHeight - 5));
                //随ęœŗč½¬åŠØēš„åŗ¦ę•°
                float angle = random.Next(-randAngle, randAngle);

                //č½¬åŠØē”»åøƒ
                g.RotateTransform(angle);
                g.DrawString(randStr[random.Next(randStr.Length)], textFont, brush, pot, format);
                //č½¬å›žåŽ»ļ¼Œäøŗäø‹äø€äøŖ字ē¬¦åšå‡†å¤‡
                g.RotateTransform(-angle);
                //é‡Šę”¾čµ„ęŗ
                brush.Dispose();
            }
            textFont.Dispose();
            format.Dispose();
            f.Dispose();

            return b;
        }
        private void rtxShow_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                #region Aglin
                if (rtxShow.SelectionAlignment == ExtendedRichTextBox.RichTextAlign.Left)
                {
                    btnAlignLeft.Checked = true;
                    btnAlignCenter.Checked = false;
                    btnAlignRight.Checked = false;
                }
                else if (rtxShow.SelectionAlignment == ExtendedRichTextBox.RichTextAlign.Center)
                {
                    btnAlignLeft.Checked = false;
                    btnAlignCenter.Checked = true;
                    btnAlignRight.Checked = false;
                }
                else if (rtxShow.SelectionAlignment == ExtendedRichTextBox.RichTextAlign.Right)
                {
                    btnAlignLeft.Checked = false;
                    btnAlignCenter.Checked = false;
                    btnAlignRight.Checked = true;
                }
                else
                {
                    btnAlignLeft.Checked = true;
                    btnAlignCenter.Checked = false;
                    btnAlignRight.Checked = false;
                }
                #endregion

                #region Font
                FontFamily ff = new FontFamily("Tahoma");
                if (ff.IsStyleAvailable(FontStyle.Bold) == true)
                {
                    btnBold.Enabled = true;
                    btnBold.Checked = rtxShow.SelectionCharStyle.Bold;
                }
                else
                {
                    btnBold.Enabled = false;
                    btnBold.Checked = false;
                }

                if (ff.IsStyleAvailable(FontStyle.Italic) == true)
                {
                    btnItalic.Enabled = true;
                    btnItalic.Checked = rtxShow.SelectionCharStyle.Italic;
                }
                else
                {
                    btnItalic.Enabled = false;
                    btnItalic.Checked = false;
                }

                if (ff.IsStyleAvailable(FontStyle.Underline) == true)
                {
                    btnUnderline.Enabled = true;
                    btnUnderline.Checked = rtxShow.SelectionCharStyle.Underline;
                }
                else
                {
                    btnUnderline.Enabled = false;
                    btnUnderline.Checked = false;
                }

                ff.Dispose();
                #endregion

                rtxShow.UpdateObjects();
            }
            catch (Exception)
            { }
        }
Example #4
0
        private void DrawFontItem(DrawItemEventArgs e)
        {
            FontStyle[] styles = new FontStyle[4] { FontStyle.Regular, FontStyle.Bold, FontStyle.Italic, FontStyle.Bold | FontStyle.Italic };
            if (!BarFunctions.IsOffice2007Style(m_Style))
                e.DrawBackground();
            string fontname = this.Items[e.Index].ToString();
            FontFamily family = new FontFamily(fontname);

            int iWidth = this.DropDownWidth / 2 - 4;
            if (iWidth <= 0)
                iWidth = this.Width;
            foreach (FontStyle style in styles)
            {
                if (family.IsStyleAvailable(style))
                {
                    eTextFormat format = eTextFormat.Default | eTextFormat.NoPrefix;
                    Color textColor = (e.State & DrawItemState.Selected) != 0 ? SystemColors.HighlightText : SystemColors.ControlText;

                    Office2007ButtonItemStateColorTable ct = null;
                    if (BarFunctions.IsOffice2007Style(m_Style))
                    {
                        Office2007ButtonItemColorTable bct = ((Office2007Renderer)GlobalManager.Renderer).ColorTable.ButtonItemColors[0];
                        if ((e.State & DrawItemState.Selected) != 0 || (e.State & DrawItemState.HotLight) != 0)
                            ct = bct.MouseOver;
                        else if ((e.State & DrawItemState.Disabled) != 0 || !this.Enabled)
                            ct = bct.Disabled;
                        else
                            ct = bct.Default;
                        //if (ct == null)
                        if (!this.Enabled)
                            textColor = _DisabledForeColor.IsEmpty ? SystemColors.ControlDark : _DisabledForeColor;
                        else
                            textColor = SystemColors.ControlText;

                        if ((e.State & DrawItemState.Selected) != 0 || (e.State & DrawItemState.HotLight) != 0)
                            Office2007ButtonItemPainter.PaintBackground(e.Graphics, ct, e.Bounds, RoundRectangleShapeDescriptor.RoundCorner2);
                        else
                        {
                            e.DrawBackground();
                        }
                    }

                    if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
                    {
                        Rectangle rt = e.Bounds;
                        rt.Height = 0; // Prevents clipping based on height
                        format |= eTextFormat.NoClipping | eTextFormat.NoPadding;
                        TextDrawing.DrawString(e.Graphics, fontname, this.Font, textColor, rt, format);
                    }
                    else
                    {
                        Size szFont = TextDrawing.MeasureString(e.Graphics, fontname, this.Font);
                        int iDiff = (int)((e.Bounds.Height - szFont.Height) / 2);
                        Rectangle rFontName = new Rectangle(e.Bounds.X, e.Bounds.Y + iDiff,
                            ((e.State & DrawItemState.Disabled) == DrawItemState.Disabled ? e.Bounds.Width : Math.Max(e.Bounds.Width - 100, 32)), e.Bounds.Height - iDiff);
                        TextDrawing.DrawString(e.Graphics, fontname, this.Font, textColor, rFontName, format);
                        Rectangle rRemainder = new Rectangle(e.Bounds.X + iWidth + 4, e.Bounds.Y, e.Bounds.Width + 100, e.Bounds.Height);
                        //using(Font f = new Font(family, (float)e.Bounds.Height - 8, style))
                        using (Font f = new Font(family, this.Font.Size, style))
                            TextDrawing.DrawString(e.Graphics, fontname, f, textColor, rRemainder, format);
                    }
                    break;
                }
            }
            if (family != null) family.Dispose();
        }
        private void AddString4(Graphics g)
        {
            g.Clear (Color.FromArgb(255,204,204,204));

            var myPath = new GraphicsPath ();

            FontFamily fntFamily = new FontFamily("Times New Roman");
            string s="Embossed Text";

            myPath.AddString (s, fntFamily, 0, 50, new PointF (28, 28), StringFormat.GenericTypographic);
            g.FillPath (Brushes.White, myPath);

            myPath.Reset ();
            myPath.AddString (s, fntFamily, 0, 50, new PointF (32, 32), StringFormat.GenericTypographic);
            g.FillPath (Brushes.DarkGray, myPath);

            myPath.Reset ();
            myPath.AddString (s, fntFamily, 0, 50, new PointF (30, 30), StringFormat.GenericTypographic);
            g.FillPath (Brushes.Black, myPath);

            s="Chiseled Text";
            myPath.Reset ();
            myPath.AddString (s, fntFamily, 0, 50, new PointF (28, 108), StringFormat.GenericTypographic);
            g.FillPath (Brushes.DarkGray, myPath);

            myPath.Reset ();
            myPath.AddString (s, fntFamily, 0, 50, new PointF (32, 112), StringFormat.GenericTypographic);
            g.FillPath (Brushes.LightGray, myPath);

            myPath.Reset ();
            myPath.AddString (s, new FontFamily ("Times New Roman"), 0, 50, new PointF (30, 110), StringFormat.GenericTypographic);
            g.FillPath (Brushes.SeaShell, myPath);

            fntFamily.Dispose();
        }
        public void Render(Graphics g, Nullable<Color> overlayColor, string replacementText)
        {
            string text = null;
            bool horizontal = true;
            string fontName;
            double fontSize = 0;
            bool fauxBold = false;
            bool fauxItalic = false;
            bool underline = false;
            bool strikethrough = false;

            Color fillColor;
            bool fillFlag = false;
            Color strokeColor;
            bool strokeFlag = false;
            double outlineWidth = 0;

            bool fillFirst = true;

            Color glowColor = Color.Yellow;
            bool glowFlag = false;
            double glowWidth = 0;
            double glowOpacity = 1;

            Rectangle rect = l.Rect;
            StringAlignment hAlign = StringAlignment.Center;
            StringAlignment vAlign = StringAlignment.Center;

            //Parse information from file.
            Glow outerGlow = null;
            TypeToolObject tto = null;
            TypeTool tt = null;
            bool foundTxt2 = false;
            //Loop through adjustment info
            List<PhotoshopFile.Layer.AdjustmentLayerInfo> adjustments = l.AdjustmentInfo;
            for (int j = 0; j < adjustments.Count; j++)
            {
                if (adjustments[j].Key.Equals("TySh"))
                    tto = new TypeToolObject(adjustments[j]);
                if (adjustments[j].Key.Equals("oglw"))
                    outerGlow = new Glow(adjustments[j]);
                //if (adjustments[j].Key.Equals("iglw"))
                //    innerGlow = new Glow(adjustments[j]);
                if (adjustments[j].Key.Equals("lrFX"))
                    //throw new Exception("We don't parse these effects do we?");
                if (adjustments[j].Key.Equals("tySh"))
                    tt = new TypeTool(adjustments[j]);
                if (adjustments[j].Key.Equals("Txt2"))
                    foundTxt2 = true;
            }
            //We know the TySh format best.
            if (tto != null){
                Dictionary<string,object> d = tto.StylesheetReader.GetStylesheetDataFromLongestRun();
                Matrix2D mat = tto.Transform;
                text = tto.StylesheetReader.Text;
                horizontal = tto.isTextHorizontal;
                fontName = TdTaParser.getString(tto.StylesheetReader.getFontSet()[(int)TdTaParser.query(d,"Font")],"Name$");
                fontSize = (double)TdTaParser.query(d,"FontSize");
                fauxBold = TdTaParser.getBool(d,"FauxBold");
                fauxItalic = TdTaParser.getBool(d,"FauxItalic");
                underline = TdTaParser.getBool(d,"Underline");
                strikethrough = TdTaParser.getBool(d,"Strikethrough");
                int styleRunAlignment = (int)TdTaParser.query(d,"StyleRunAlignment");//No idea what this maps to.
                //string info = tto.TxtDescriptor.getString();
                outlineWidth =  (double)TdTaParser.query(d,"OutlineWidth");

                fillColor = TdTaParser.getColor(d,"FillColor");
                strokeColor = TdTaParser.getColor(d,"StrokeColor");
                fillFlag = TdTaParser.getBool(d,"FillFlag");
                strokeFlag = TdTaParser.getBool(d,"StrokeFlag");
                fillFirst = TdTaParser.getBool(d,"FillFirst");

                //TODO: get alignment data
            }else if (tt != null){
                throw new Exception("Old style tySh font syntax not implemented, found on layer " + l.Name + ". Use a newer version of Photoshop");
            }else if (foundTxt2){
                throw new Exception("Txt2 text layer info not supported, found on layer " + l.Name + ". Where did you find this file? What version of photoshop?");
            }else{
                throw new Exception("No text layer information found on " + l.Name + "!");
            }

            if (outerGlow != null){
                glowColor = outerGlow.Color;
                glowFlag = outerGlow.Enabled;
                glowWidth = (int)outerGlow.Blur;
                glowOpacity = (double)outerGlow.Opacity / 255.0;
            }

            //Replace text if requested.
            if (replacementText != null) text = replacementText;

            //Fix newlines
            text = text.Replace("\r\n", "\n").Replace("\r", "\n");

            //Do graphics stuff
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            using(GraphicsPath path = new GraphicsPath()){
                FontFamily fontFamily = null;
                StringFormat strformat = null;
                try{
                    FontStyle style = FontStyle.Regular;
                    //Remove MT
                    if (fontName.EndsWith("MT")) fontName = fontName.Substring(0, fontName.Length - 2);
                    //Remove -Bold, -Italic, -BoldItalic
                    if (fontName.EndsWith("-Bold", StringComparison.OrdinalIgnoreCase)) style |= FontStyle.Bold;
                    if (fontName.EndsWith("-Italic", StringComparison.OrdinalIgnoreCase)) style |= FontStyle.Italic;
                    if (fontName.EndsWith("-BoldItalic", StringComparison.OrdinalIgnoreCase)) style |= FontStyle.Bold | FontStyle.Italic;
                    //Remove from fontName
                    fontName = new Regex("\\-(Bold|Italic|BoldItalic)$", RegexOptions.IgnoreCase | RegexOptions.IgnoreCase).Replace(fontName, "");
                    //Remove PS
                    if (fontName.EndsWith("PS")) fontName = fontName.Substring(0, fontName.Length - 2);
                    //Find font family
                    try {
                        fontFamily = new FontFamily(fontName);
                    } catch (ArgumentException ae) {
                        if (IgnoreMissingFonts) {
                            fontFamily = FontFamily.GenericSansSerif;
                        } else throw ae;

                    }
                    if (fauxBold) style |= FontStyle.Bold;
                    if (fauxItalic) style |= FontStyle.Italic;
                    if (underline) style |= FontStyle.Underline;
                    if (strikethrough) style |= FontStyle.Strikeout;

                    strformat = new StringFormat();
                    strformat.LineAlignment = hAlign;
                    strformat.Alignment = vAlign;
                    strformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip;
                    if (!horizontal) strformat.FormatFlags |= StringFormatFlags.DirectionVertical;
                    strformat.Trimming = StringTrimming.None;

                    path.AddString(text, fontFamily,
                        (int)style, (float)(fontSize),rect, strformat);
                }finally{
                    fontFamily.Dispose();
                    strformat.Dispose();
                }

                if (glowFlag)
                {
                    if (glowWidth == 3) glowWidth = 2; //To replicate photoshop rounding bug

                    double finalOpacity = (glowOpacity + 1.5)/2.5; //Because Photoshop doesn't actually use the opacity as the final opacity...
                    //Add a 30% fade inside the glow width
                    int fadeWidth = (int)Math.Floor(glowWidth * 0.33);

                    double totalOpacity = 0; //Start at transparent

                    //start with outermost ring, work inwards
                    for (int i = fadeWidth; i >=0; i--)
                    {
                        //How much opacity do we lack to complete the desired opacity?
                        //totalOpacity = prevOpacity * (1-curOpacity) + curOpacity
                        //a=b *(1-c) + c
                        //>>> where b!=1, c=(b-a/b-1)
                        double missingOpacity = (totalOpacity - finalOpacity) / (totalOpacity - 1);

                        int remainingRings = fadeWidth;

                        double currentOpacity = missingOpacity / (fadeWidth + 1);
                        //Update total opacity
                        totalOpacity = totalOpacity * (1 - currentOpacity) + currentOpacity;
                        //Draw it
                        using (Pen pen = new Pen(Color.FromArgb((int)Math.Round(currentOpacity * 255.0), glowColor), (float)((glowWidth - fadeWidth + (double)i) * 2)))
                        {
                            pen.LineJoin = LineJoin.Round;
                            g.DrawPath(pen, path);
                        }
                    }
                }

                if (fillFirst){
                    //Draw fill
                    if (fillFlag){
                        using(SolidBrush brush = new SolidBrush(fillColor)){
                            g.FillPath(brush, path);
                        }
                    }
                    //Draw stroke
                    if (strokeFlag){
                        using (Pen p = new Pen(strokeColor,(float)outlineWidth)){
                            p.LineJoin = LineJoin.Round;
                            g.DrawPath(p,path);
                        }
                    }
                }else{
                    //Draw stroke
                    if (strokeFlag){
                        using (Pen p = new Pen(strokeColor, (float)outlineWidth))
                        {
                            p.LineJoin = LineJoin.Round;
                            g.DrawPath(p,path);
                        }
                    }
                                        //Draw fill
                    if (fillFlag){
                        using(SolidBrush brush = new SolidBrush(fillColor)){
                            g.FillPath(brush, path);
                        }
                    }
                }
            }
        }
        public static Font GetFont(String familyName, float size, FontStyle style)
        {
            Font result = null;
              FontFamily family = null;
              try
              {
            family = new FontFamily(familyName);
            if (family.IsStyleAvailable(style))
              result = new Font(familyName, size, style);
            else
            {
              // On some systems even the regular style throws an exception (which actually should not happen
              // as Windows should automatically pick an alternative font if the one requested does not exist, but
              // there are cases...).
              result = new Font(familyName, size, FontStyle.Regular);
            }
              }
              catch
              {
            result = new Font(FontFamily.GenericSansSerif, size, style);
              }

              if (family != null)
            family.Dispose();
              return result;
        }
        private void txtTaskDes_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                #region Aglin
                if (txtTaskDes.SelectionAlignment == ExtendedRichTextBox.RichTextAlign.Left)
                {
                    btnLeft2.Checked = true;
                    btnCenter2.Checked = false;
                    btnRight2.Checked = false;
                }
                else if (txtTaskDes.SelectionAlignment == ExtendedRichTextBox.RichTextAlign.Center)
                {
                    btnLeft2.Checked = false;
                    btnCenter2.Checked = true;
                    btnRight2.Checked = false;
                }
                else if (txtTaskDes.SelectionAlignment == ExtendedRichTextBox.RichTextAlign.Right)
                {
                    btnLeft2.Checked = false;
                    btnCenter2.Checked = false;
                    btnRight2.Checked = true;
                }
                else
                {
                    btnLeft2.Checked = true;
                    btnCenter2.Checked = false;
                    btnRight2.Checked = false;
                }
                #endregion

                #region Font
                FontFamily ff = new FontFamily("Tahoma");
                if (ff.IsStyleAvailable(FontStyle.Bold) == true)
                {
                    btnB2.Enabled = true;
                    btnB2.Checked = txtTaskDes.SelectionCharStyle.Bold;
                }
                else
                {
                    btnB2.Enabled = false;
                    btnB2.Checked = false;
                }

                if (ff.IsStyleAvailable(FontStyle.Italic) == true)
                {
                    btnI2.Enabled = true;
                    btnI2.Checked = txtTaskDes.SelectionCharStyle.Italic;
                }
                else
                {
                    btnI2.Enabled = false;
                    btnI2.Checked = false;
                }

                if (ff.IsStyleAvailable(FontStyle.Underline) == true)
                {
                    btnU2.Enabled = true;
                    btnU2.Checked = txtTaskDes.SelectionCharStyle.Underline;
                }
                else
                {
                    btnU2.Enabled = false;
                    btnU2.Checked = false;
                }

                ff.Dispose();
                #endregion

                //txtTaskDes.UpdateObjects();
            }
            catch (Exception)
            { }
        }
        public override void PaintValue(PaintValueEventArgs e)
        {
            string name = e.Value as string;
            switch (name)
            {
                case null:
                    break;

                case "":
                    return;

                default:
                {
                    e.Graphics.FillRectangle(SystemBrushes.ActiveCaption, e.Bounds);
                    FontFamily fontFamily = null;
                    try
                    {
                        fontFamily = new FontFamily(name);
                    }
                    catch
                    {
                    }
                    if (fontFamily != null)
                    {
                        try
                        {
                            DrawFontSample(e, fontFamily, FontStyle.Regular);
                        }
                        catch
                        {
                            try
                            {
                                DrawFontSample(e, fontFamily, FontStyle.Italic);
                            }
                            catch
                            {
                                try
                                {
                                    DrawFontSample(e, fontFamily, FontStyle.Bold);
                                }
                                catch
                                {
                                    try
                                    {
                                        DrawFontSample(e, fontFamily, FontStyle.Italic | FontStyle.Bold);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                        }
                        finally
                        {
                            fontFamily.Dispose();
                        }
                    }
                    e.Graphics.DrawLine(SystemPens.WindowFrame, e.Bounds.Right, e.Bounds.Y, e.Bounds.Right, e.Bounds.Bottom);
                    break;
                }
            }
        }
Example #10
0
File: Util.cs Project: jonc/carto
 public static bool FontExists(string fontname)
 {
     #if false
     // Get the glyphTypeface to see if the font exists.
     GlyphTypeface glyphTypeface;
     Typeface typeface = new Typeface(fontname);
     return typeface.TryGetGlyphTypeface(out glyphTypeface);
     #else
     // Doesn't seem to be an easy way to determine if a font exists.
     try {
         FontFamily family = new FontFamily(fontname);
         family.Dispose();
         return true;
     }
     catch {
         return false;
     }
     #endif
 }