Exemple #1
0
        private static Bitmap DrawCaptionLine(Bitmap bmp, string caption, float fontsize, TextPosition where, bool localizable, Color fontColour)
        {
            // The upper 10/64 and lower 14/64 of the button are not considered drawing area
            // so discount them when calculating the row position. This also knocks the centre down by 4/64.

            using (Font font = AppController.GetButtonFont(fontsize, localizable))
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

                    // Use width of actual string for left placement:
                    // This only takes tiny amount of time - 14ms for 10000 iterations..
                    SizeF stringSize = g.MeasureString(caption, font);

                    int left = (bmp.Width / 2) - (int)(stringSize.Width / 2);
                    int top  = 0;

                    // Vertical centre justify within button and row:
                    switch (@where)
                    {
                    case TextPosition.Middle:
                        // Remove cast to int, get 'possible loss of fraction'. Oh well..
                        top = (int)(((bmp.Height - stringSize.Height) / 2) + (int)(bmp.Height / 28));
                        break;

                    case TextPosition.TextTop:
                        top = (int)(bmp.Height * 14F / 64F);
                        break;

                    case TextPosition.Bottom:
                        top = bmp.Height / 2;
                        break;

                    case TextPosition.SymbolTop:
                        top = (int)(bmp.Height * 8F / 64F);
                        break;
                    }

                    using (SolidBrush b = new SolidBrush(fontColour))
                    {
                        g.DrawString(caption, font, b, new Point(left, top));
                    }
                }

            return(bmp);
        }
Exemple #2
0
        public static void SetFontSizes(float scale)
        {
            // See what font size fits the scaled-down button
            float baseFontSize = 36F;

            // Not using ButtonImages.GetButtonImage as that is where we were called from..
            using (var font = AppController.GetButtonFont(baseFontSize, false))
                using (var bmp = ButtonImages.ResizeBitmap(ButtonImages.GetBitmap(BlankButton.Blank), scale, false))
                    using (var g = Graphics.FromImage(bmp))
                    {
                        // Helps MeasureString. Can also pass StringFormat.GenericTypographic apparently ??

                        g.TextRenderingHint = TextRenderingHint.AntiAlias;
                        var characterWidth = (int)g.MeasureString(((char)77).ToString(CultureInfo.InvariantCulture), font).Width;

                        // Only use 90% of the bitmap's size to allow for the edges (especially at small sizes)
                        float ratio = (((0.9F * bmp.Height) / 2)) / characterWidth;
                        baseFontSize = (baseFontSize * ratio);
                    }

            BaseFontSize = baseFontSize;
        }
Exemple #3
0
        private static float GetMultiLineFontSize(Bitmap bmp, string caption, bool localizable, float fontsize)
        {
            string[] words = caption.Split();

            // Ignore words in brackets as they won't be displayed.
            string longestWord = words[0];

            for (int i = 1; i < words.GetLength(0); i++)
            {
                string word = words[i];
                if (word.StartsWith("(", StringComparison.Ordinal) && word.EndsWith(")", StringComparison.Ordinal))
                {
                    continue;
                }

                if (word.Length > longestWord.Length)
                {
                    longestWord = word;
                }
            }

            using (Graphics g = Graphics.FromImage(bmp))
                using (Font font = AppController.GetButtonFont(fontsize, localizable))
                {
                    g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

                    SizeF stringSize;
                    stringSize = g.MeasureString(longestWord, font);

                    if (stringSize.Width > (bmp.Width * 0.8F))
                    {
                        fontsize *= ((bmp.Width * 0.8F) / stringSize.Width);
                    }
                }

            return(fontsize);
        }