示例#1
0
 public TextStyle WithChanged(Font?font = null, Color?brush = null, Color?background = null)
 {
     return(new TextStyle(
                font ?? this.Font,
                brush ?? this.Brush,
                background ?? this.Background));
 }
示例#2
0
        public static NSAttributedString ToNSAttributedString(
            this FormattedString formattedString,
            IFontManager fontManager,
            double defaultLineHeight = 0d,             // TODO: NET7 should be -1, but too late to change for net6
            TextAlignment defaultHorizontalAlignment = TextAlignment.Start,
            Font?defaultFont   = null,
            Color?defaultColor = null,
            TextTransform defaultTextTransform = TextTransform.Default)
        {
            if (formattedString == null)
            {
                return(new NSAttributedString(string.Empty));
            }

            var attributed = new NSMutableAttributedString();

            for (int i = 0; i < formattedString.Spans.Count; i++)
            {
                Span span = formattedString.Spans[i];
                if (span.Text == null)
                {
                    continue;
                }

                attributed.Append(span.ToNSAttributedString(fontManager, defaultLineHeight, defaultHorizontalAlignment, defaultFont, defaultColor, defaultTextTransform));
            }

            return(attributed);
        }
示例#3
0
 /// <summary>
 ///  Draws the specified text within the specified bounds, using the specified device context, font, and color.
 /// </summary>
 /// <param name="dc">The device context in which to draw the text.</param>
 /// <param name="text">The text to draw.</param>
 /// <param name="font">The <see cref="Font"/> to apply to the drawn text.</param>
 /// <param name="bounds">The <see cref="Rectangle"/> that represents the bounds of the text.</param>
 /// <param name="foreColor">The <see cref="Color"/> to apply to the drawn text.</param>
 /// <exception cref="ArgumentNullException"><paramref name="dc"/> is null.</exception>
 public static void DrawText(
     IDeviceContext dc,
     ReadOnlySpan <char> text,
     Font?font,
     Rectangle bounds,
     Color foreColor)
 => DrawTextInternal(dc, text, font, bounds, foreColor, Color.Empty);
示例#4
0
        private static void DrawTextInternal(
            IDeviceContext dc,
            string?text,
            Font?font,
            Rectangle bounds,
            Color foreColor,
            Color backColor = default,
            User32.DT flags = User32.DT.CENTER | User32.DT.VCENTER)
        {
            if (dc is null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            // Avoid creating the HDC, etc if we're not going to do any drawing
            if (string.IsNullOrEmpty(text) || foreColor == Color.Transparent)
            {
                return;
            }

            // This MUST come before retreiving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            using var hdc            = new DeviceContextHdcScope(dc, applyGraphicsState: false);
            using WindowsGraphics wg = WindowsGraphics.FromHdc(hdc);
            using WindowsFont? wf    = WindowsGraphicsCacheManager.GetWindowsFont(font, quality);
            wg.DrawText(text, wf, bounds, foreColor, backColor, flags);
        }
示例#5
0
        /// <summary>
        ///  Converts the managed value into a native value
        /// </summary>
        public override object?ConvertManagedToNative(object?managedValue, Com2PropertyDescriptor pd, ref bool cancelSet)
        {
            // we default to black.
            if (managedValue is null)
            {
                managedValue = Control.DefaultFont;
            }

            cancelSet = true;

            if (_lastFont is not null && _lastFont.Equals(managedValue))
            {
                // don't do anything here.
                return(null);
            }

            _lastFont = (Font)managedValue;
            IFont nativeFont = (IFont)pd.GetNativeValue(pd.TargetObject);

            // now, push all the values into the native side
            if (nativeFont is not null)
            {
                bool changed = ControlPaint.FontToIFont(_lastFont, nativeFont);

                if (changed)
                {
                    // here, we want to pick up a new font from the handle
                    _lastFont = null;
                    ConvertNativeToManaged(nativeFont, pd);
                }
            }

            return(null);
        }
示例#6
0
 public static Size MeasureText(
     IDeviceContext dc,
     string?text,
     Font?font,
     Size proposedSize,
     TextFormatFlags flags)
 => MeasureTextInternal(dc, text, font, proposedSize, flags);
示例#7
0
        internal static void DrawTextInternal(
            IDeviceContext dc,
            ReadOnlySpan <char> text,
            Font?font,
            Rectangle bounds,
            Color foreColor,
            Color backColor,
            TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter)
        {
            if (dc is null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            // Avoid creating the HDC, etc if we're not going to do any drawing
            if (text.IsEmpty || foreColor == Color.Transparent)
            {
                return;
            }

            // This MUST come before retreiving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            using var hdc = new DeviceContextHdcScope(dc, GetApplyStateFlags(dc, flags));

            DrawTextInternal(hdc, text, font, bounds, foreColor, quality, backColor, flags);
        }
示例#8
0
        private static Size MeasureTextInternal(
            IDeviceContext dc,
            ReadOnlySpan <char> text,
            Font?font,
            Size proposedSize,
            TextFormatFlags flags = TextFormatFlags.Bottom)
        {
            if (dc is null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            if (text.IsEmpty)
            {
                return(Size.Empty);
            }

            // This MUST come before retreiving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            // Applying state may not impact text size measurements. Rather than risk missing some
            // case we'll apply as we have historically to avoid suprise regressions.
            using var hdc   = new DeviceContextHdcScope(dc, GetApplyStateFlags(dc, flags));
            using var hfont = GdiCache.GetHFONT(font, quality, hdc);
            return(hdc.HDC.MeasureText(text, hfont, proposedSize, flags));
        }
示例#9
0
 public static void DrawText(
     IDeviceContext dc,
     string?text, Font?
     font, Rectangle bounds,
     Color foreColor,
     Color backColor)
 => DrawTextInternal(dc, text, font, bounds, foreColor, backColor);
示例#10
0
 /// <summary>
 ///  Provides the size, in pixels, of the specified text when drawn with the specified device context, font,
 ///  and formatting instructions, using the specified size to create the initial bounding rectangle for the text.
 /// </summary>
 /// <param name="dc">The device context in which to measure the text.</param>
 /// <param name="text">The text to measure.</param>
 /// <param name="font">The <see cref="Font"/> to apply to the measured text.</param>
 /// <param name="proposedSize">The <see cref="Size"/> of the initial bounding rectangle.</param>
 /// <param name="flags">The formatting instructions to apply to the measured text.</param>
 /// <returns>
 ///  The <see cref="Size"/>, in pixels, of <paramref name="text"/> drawn with the specified
 ///  <paramref name="font"/> and format.
 /// </returns>
 /// <exception cref="ArgumentNullException"><paramref name="dc"/> is null.</exception>
 /// <exception cref="ArgumentOutOfRangeException">
 ///  Thrown if <see cref="TextFormatFlags.ModifyString"/> is set.
 /// </exception>
 public static Size MeasureText(
     IDeviceContext dc,
     ReadOnlySpan <char> text,
     Font?font,
     Size proposedSize,
     TextFormatFlags flags)
 => MeasureTextInternal(dc, text, font, proposedSize, BlockModifyString(flags));
示例#11
0
        private static Size MeasureTextInternal(
            IDeviceContext dc,
            ReadOnlySpan <char> text,
            Font?font,
            Size proposedSize,
            TextFormatFlags flags  = TextFormatFlags.Bottom,
            bool blockModifyString = false)
        {
            if (dc is null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            User32.DT drawTextFlags = GetTextFormatFlags(flags, blockModifyString);

            if (text.IsEmpty)
            {
                return(Size.Empty);
            }

            // This MUST come before retreiving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            using var hdc   = new DeviceContextHdcScope(dc);
            using var hfont = GdiCache.GetHFONT(font, quality, hdc);
            return(hdc.MeasureText(text, hfont, proposedSize, drawTextFlags));
        }
示例#12
0
        public static Size MeasureText(IDeviceContext dc, string?text, Font?font, Size proposedSize)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            Gdi32.QUALITY fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);

            IntPtr hdc = dc.GetHdc();

            try
            {
                using WindowsGraphics wg = WindowsGraphics.FromHdc(hdc);
                using WindowsFont? wf    = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality);
                return(wg.MeasureText(text, wf, proposedSize));
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }
示例#13
0
        public FloatingText()
        {
            // Read scale
            var asf = ParentForm as AutoScaleForm;
            var dpi = asf?.Dpi ?? DeviceDpi;

            _scale = (dpi > 120) ? 2 : 1;

            if (LargeFont == null)
            {
                LargeFont = new Font("Arial Black", 16);
            }

            if (SmallFont == null)
            {
                SmallFont = new Font("Consolas", 10);
            }

            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            InitializeComponent();

            if (textBox != null)
            {
                textBox.Font = SmallFont;
            }
            Cursor = Cursors.Arrow;
        }
            ///  GetTextSize
            ///  Given constraints, format flags a font and text, determine the size of the string
            ///  employs an MRU of the last several constraints passed in via a ring-buffer of size MaxCacheSize.
            ///  Assumes Text and TextFormatFlags are the same, if either were to change, a call to
            ///  InvalidateCache should be made
            public Size GetTextSize(string?text, Font?font, Size proposedConstraints, TextFormatFlags flags)
            {
                if (!TextRequiresWordBreak(text, font, proposedConstraints, flags))
                {
                    // Text fits within proposed width

                    // IF we're here, this means we've got text that can fit into the proposedConstraints
                    // without wrapping.  We've determined this because our

                    // as a side effect of calling TextRequiresWordBreak,
                    // unconstrainedPreferredSize is set.
                    return(_unconstrainedPreferredSize);
                }
                else
                {
                    // Text does NOT fit within proposed width - requires WordBreak

                    // IF we're here, this means that the wrapping width is smaller
                    // than our max width.  For example: we measure the text with infinite
                    // bounding box and we determine the width to fit all the characters
                    // to be 200 px wide.  We would come here only for proposed widths less
                    // than 200 px.

                    // Create our ring buffer if we don't have one
                    if (_sizeCacheList is null)
                    {
                        _sizeCacheList = new PreferredSizeCache[MaxCacheSize];
                    }

                    // check the existing constraints from previous calls
                    foreach (PreferredSizeCache sizeCache in _sizeCacheList)
                    {
                        if (sizeCache.ConstrainingSize == proposedConstraints)
                        {
                            return(sizeCache.PreferredSize);
                        }
                        else if ((sizeCache.ConstrainingSize.Width == proposedConstraints.Width) &&
                                 (sizeCache.PreferredSize.Height <= proposedConstraints.Height))
                        {
                            // Caching a common case where the width matches perfectly, and the stored preferred height
                            // is smaller or equal to the constraining size.
                            //        prefSize = GetPreferredSize(w,Int32.MaxValue);
                            //        prefSize = GetPreferredSize(w,prefSize.Height);

                            return(sizeCache.PreferredSize);
                        }

                        //
                    }

                    // if we've gotten here, it means we don't have a cache entry, therefore
                    // we should add a new one in the next available slot.
                    Size prefSize = TextRenderer.MeasureText(text, font, proposedConstraints, flags);
                    _nextCacheEntry = (_nextCacheEntry + 1) % MaxCacheSize;
                    _sizeCacheList[_nextCacheEntry] = new PreferredSizeCache(proposedConstraints, prefSize);

                    return(prefSize);
                }
            }
示例#15
0
 public static void DrawText(
     IDeviceContext dc,
     string?text,
     Font?font,
     Point pt,
     Color foreColor,
     Color backColor)
 => DrawTextInternal(dc, text, font, pt, foreColor, backColor);
示例#16
0
 internal static void DrawTextInternal(
     PaintEventArgs e,
     string?text,
     Font?font,
     Rectangle bounds,
     Color foreColor,
     TextFormatFlags flags)
 => DrawTextInternal(e, text, font, bounds, foreColor, Color.Empty, flags);
示例#17
0
 public static void DrawText(
     IDeviceContext dc,
     string?text,
     Font?font,
     Point pt,
     Color foreColor,
     TextFormatFlags flags)
 => DrawTextInternal(dc, text, font, pt, foreColor, flags: GetTextFormatFlags(flags));
示例#18
0
 public static void DrawText(
     IDeviceContext dc,
     string?text,
     Font?font,
     Rectangle bounds,
     Color foreColor,
     TextFormatFlags flags)
 => DrawTextInternal(dc, text, font, bounds, foreColor, flags: GetTextFormatFlags(flags));
示例#19
0
 /// <summary>
 ///  Renders a GroupBox control.
 /// </summary>
 public static void DrawGroupBox(
     Graphics g,
     Rectangle bounds,
     string?groupBoxText,
     Font?font,
     Color textColor,
     GroupBoxState state)
 => DrawGroupBox(g, bounds, groupBoxText, font, textColor, TextFormatFlags.Top | TextFormatFlags.Left, state);
示例#20
0
 /// <summary>
 ///  Renders a GroupBox control. Uses the text color specified by the theme.
 /// </summary>
 public static void DrawGroupBox(
     Graphics g,
     Rectangle bounds,
     string?groupBoxText,
     Font?font,
     TextFormatFlags flags,
     GroupBoxState state)
 => DrawGroupBox((IDeviceContext)g, bounds, groupBoxText, font, flags, state);
示例#21
0
 internal static void DrawTextInternal(
     Gdi32.HDC hdc,
     string?text,
     Font?font,
     Rectangle bounds,
     Color foreColor,
     Gdi32.QUALITY fontQuality,
     TextFormatFlags flags)
 => DrawTextInternal(hdc, text, font, bounds, foreColor, fontQuality, Color.Empty, flags);
示例#22
0
 private void WaterMark_FontChanged(object sender, EventArgs args)
 {
     if (waterMarkTextEnabled)
     {
         oldFont = new System.Drawing.Font(Font.FontFamily, Font.Size, Font.Style,
                                           Font.Unit);
         Refresh();
     }
 }
示例#23
0
 private static void DrawTextInternal(
     IDeviceContext dc,
     ReadOnlySpan<char> text,
     Font? font,
     Point pt,
     Color foreColor,
     Color backColor,
     User32.DT flags = User32.DT.DEFAULT)
     => DrawTextInternal(dc, text, font, new Rectangle(pt, MaxSize), foreColor, backColor, flags);
示例#24
0
 private static void DrawTextInternal(
     IDeviceContext dc,
     ReadOnlySpan <char> text,
     Font?font,
     Point pt,
     Color foreColor,
     Color backColor,
     TextFormatFlags flags = TextFormatFlags.Default)
 => DrawTextInternal(dc, text, font, new Rectangle(pt, MaxSize), foreColor, backColor, flags);
示例#25
0
        public static Bitmap GetMapWithBuildings(MapTerrainStructure m, Font?f, int[] scale1, int[] scaleX, Bitmap map, int scale = 4, int index = -1)
        {
            CreateMap(m.Terrain, scale1, scaleX, map, scale);
            using var gfx = Graphics.FromImage(map);

            gfx.DrawPlaza(m.Terrain, (ushort)m.PlazaX, (ushort)m.PlazaY, scale);
            gfx.DrawBuildings(m.Terrain, m.Buildings, f, scale, index);
            return(map);
        }
示例#26
0
 internal DrawItemEventArgs(
     Gdi32.HDC hdc,
     Font?font,
     Rectangle rect,
     uint index,
     User32.ODS state)
     : this(hdc, font, rect, index, state, SystemColors.WindowText, SystemColors.Window)
 {
 }
示例#27
0
        /// <summary>
        /// Writes the keys name on a given template
        /// </summary>
        /// <param name="template">Image on which the name of the key should be written</param>
        /// <param name="name">The name of the key</param>
        /// <returns>An image with the name centered on it</returns>
        protected Image WriteOnTemplate(Image template, string name)
        {
            try
            {
                using (Graphics g = Graphics.FromImage(template))
                {
                    int size = 150;
                    _font = new Font(_fontFamily, size);

                    // measure the size of the font so that the text fits the image
                    while (g.MeasureString(name, _font).Width > template.Width || g.MeasureString(name, _font).Height > template.Height)
                    {
                        _font.Dispose();
                        size -= 5;
                        _font = new Font(_fontFamily, size);
                        if (size < 6)
                        {
                            break;
                        }
                    }

                    //actually write the text, centered, on the image
                    g.DrawString(name, _font, Brushes.Gray, new Point((int)((template.Width - g.MeasureString(name, _font).Width) / 2), (int)((template.Height - g.MeasureString(name, _font).Height) / 2)));
                    try
                    {
                        if (File.Exists(Path.Join(ImagesPath, "/" + name + ".png")))
                        {
                            File.Delete(Path.Join(ImagesPath, "/" + name + ".png"));
                        }
                        template.Save(Path.Join(ImagesPath, "/" + name + ".png"));
                    }
                    catch (Exception e)
                    {
                        var exception = new  ChainingException(e.Message);
                        exception.AddErrorToChain("While trying to delete image");
                        throw exception;
                    }

                    _font.Dispose();
                }

                return(template);
            }
            catch (Exception e)
            {
                if (e is ChainingException exception)
                {
                    exception.AddErrorToChain("While trying to write on template image");
                }
                else
                {
                    exception = new ChainingException(e.Message);
                    exception.AddErrorToChain("While trying to write on template image");
                }
                throw exception;
            }
        }
示例#28
0
 private static void DrawTextInternal(
     IDeviceContext dc,
     string?text,
     Font?font,
     Point pt,
     Color foreColor,
     Color backColor = default,
     User32.DT flags = User32.DT.DEFAULT)
 => DrawTextInternal(dc, text, font, new Rectangle(pt, WindowsGraphics.MaxSize), foreColor, backColor, flags);
示例#29
0
        public static Size MeasureText(string?text, Font?font)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            using WindowsFont? wf = WindowsGraphicsCacheManager.GetWindowsFont(font);
            return(WindowsGraphicsCacheManager.MeasurementGraphics.MeasureText(text, wf));
        }
示例#30
0
        public static Size MeasureText(string?text, Font?font, Size proposedSize, TextFormatFlags flags)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            using WindowsFont? wf = WindowsGraphicsCacheManager.GetWindowsFont(font);
            return(WindowsGraphicsCacheManager.MeasurementGraphics.MeasureText(text, wf, proposedSize, GetTextFormatFlags(flags)));
        }