Пример #1
0
            protected bool disposedValue; // To detect redundant calls

            protected virtual void Dispose(bool disposing)
            {
                disposedValue = true;

                if (disposing)
                {
                    _pos = -1;
                    _obj = null;
                }
            }
Пример #2
0
        /// <summary>
        /// Merges two font collections, returning a new collection object.
        /// </summary>
        /// <param name="col1">The first font collection.</param>
        /// <param name="col2">The second font collection.</param>
        /// <param name="sortProperty">Optionally specify a sort property ("Name" is the default).  If you specify a property that cannot be found, "Name" will be used.</param>
        /// <param name="SortOrder">Optionally specify ascending or descending order.</param>
        /// <returns></returns>
        public static FontCollection MergeCollections(FontCollection col1, FontCollection col2, string SortProperty = "Name", SortOrder SortOrder = SortOrder.Ascending)
        {
            var col3 = new FontCollection();

            foreach (var c in col1)
            {
                col3.Add(c);
            }
            foreach (var c in col2)
            {
                col3.Add(c);
            }
            col3.Sort(SortProperty, SortOrder);
            return(col3);
        }
Пример #3
0
 public FontEnumer(FontCollection obj)
 {
     _obj = obj;
 }
Пример #4
0
        /// <summary>
        /// Search for fonts whose names contain the specified string.
        /// </summary>
        /// <param name="pattern">String to look for.</param>
        /// <param name="caseSensitive">Specifies whether the search is case-sensitive.</param>
        /// <returns></returns>
        public FontCollection Search(string pattern, bool caseSensitive = false, FontSearchOptions searchOptions = FontSearchOptions.Contains)
        {
            var    l = new FontCollection();
            string s;
            string t;
            int    i = pattern.Length;
            int    j;

            s = pattern;
            if (caseSensitive == false)
            {
                s = s.ToLower();
            }
            foreach (var f in this)
            {
                t = f.elf.elfFullName;
                if (caseSensitive == false)
                {
                    t = t.ToLower();
                }
                switch (searchOptions)
                {
                case FontSearchOptions.Contains:
                {
                    if (t.Contains(s))
                    {
                        l.Add(f);
                    }

                    break;
                }

                case FontSearchOptions.BeginsWith:
                {
                    if (t.Length >= s.Length)
                    {
                        if ((t.Substring(0, i) ?? "") == (s ?? ""))
                        {
                            l.Add(f);
                        }
                    }

                    break;
                }

                case FontSearchOptions.EndsWith:
                {
                    if (t.Length >= s.Length)
                    {
                        j = t.Length - s.Length;
                        if ((t.Substring(j, i) ?? "") == (s ?? ""))
                        {
                            l.Add(f);
                        }
                    }

                    break;
                }
                }
            }

            if (l.Count == 0)
            {
                return(null);
            }
            return(l);
        }
Пример #5
0
 /// <summary>
 /// Initialize the master system font list.
 /// </summary>
 static FontCollection()
 {
     SystemFonts = GetFonts();
 }
Пример #6
0
        /// <summary>
        /// Gets a collection of fonts based on the specified criteria.
        /// </summary>
        /// <param name="families">Bit Field representing which font families to retrieve.</param>
        /// <param name="pitch">Specify the desired pitch.</param>
        /// <param name="charset">Specify the desired character set.</param>
        /// <param name="weight">Specify the desired weight.</param>
        /// <param name="Script">Specify the desired script(s) (this can be a String or an array of Strings).</param>
        /// <param name="Style">Specify the desired style(s) (this can be a String or an array of Strings).</param>
        /// <returns></returns>
        public static FontCollection GetFonts(FontFamilies families = FontFamilies.DontCare, FontPitch pitch = FontPitch.Default, FontCharSet charset = FontCharSet.Default, FontWeight weight = FontWeight.DontCare, object Script = null, object Style = null)
        {
            IntPtr hdc;

            var fonts = new List <ENUMLOGFONTEX>();

            var lf = new LOGFONT();

            string s;

            MemPtr mm = new MemPtr();

            string[] wscript;
            string[] wstyle;

            if (Script is null)
            {
                wscript = new[] { "Western" };
            }
            else if (Script is string)
            {
                wscript = new[] { (string)(Script) };
            }
            else if (Script is string[])
            {
                wscript = (string[])Script;
            }
            else
            {
                throw new ArgumentException("Invalid parameter type for Script");
            }

            if (Style is null)
            {
                wstyle = new[] { "", "Normal", "Regular" };
            }
            else if (Style is string)
            {
                wstyle = new[] { (string)(Style) };
            }
            else if (Style is string[])
            {
                wstyle = (string[])Style;
            }
            else
            {
                throw new ArgumentException("Invalid parameter type for Style");
            }

            lf.lfCharSet  = (byte)charset;
            lf.lfFaceName = "";
            mm.Alloc(Marshal.SizeOf(lf));
            mm.FromStruct(lf);
            hdc = User32.CreateDC("DISPLAY", null, IntPtr.Zero, IntPtr.Zero);

            int  e;
            bool bo = false;

            e = EnumFontFamiliesEx(hdc, mm, (ref ENUMLOGFONTEX lpelfe, IntPtr lpntme, uint FontType, IntPtr lParam) =>
            {
                int z;
                if (fonts is null)
                {
                    z = 0;
                }
                else
                {
                    z = fonts.Count;
                }


                // make sure it's the normal, regular version

                bo = false;
                foreach (var y in wstyle)
                {
                    if ((y.ToLower() ?? "") == (lpelfe.elfStyle.ToLower() ?? ""))
                    {
                        bo = true;
                        break;
                    }
                }

                if (bo == false)
                {
                    return(1);
                }
                bo = false;
                foreach (var y in wscript)
                {
                    if ((y.ToLower() ?? "") == (lpelfe.elfScript.ToLower() ?? ""))
                    {
                        bo = true;
                        break;
                    }
                }

                if (bo == false)
                {
                    return(1);
                }
                bo = false;
                if (weight != FontWeight.DontCare && lpelfe.elfLogFont.lfWeight != (int)weight)
                {
                    return(1);
                }

                // we don't really need two of the same font.
                if (z > 0)
                {
                    if ((lpelfe.elfFullName ?? "") == (fonts[z - 1].elfFullName ?? ""))
                    {
                        return(1);
                    }
                }

                // the @ indicates a vertical writing font which we definitely do not want.
                if (lpelfe.elfFullName.Substring(0, 1) == "@")
                {
                    return(1);
                }
                if (!CheckFamily(lpelfe.elfLogFont, families))
                {
                    return(1);
                }

                // lpelfe.elfLogFont.lfCharSet = charset
                // If (lpelfe.elfLogFont.lfCharSet <> charset) Then Return 1

                if (pitch != FontPitch.Default && (lpelfe.elfLogFont.lfPitchAndFamily & 3) != (int)pitch)
                {
                    return(1);
                }
                fonts.Add(lpelfe);
                return(1);
            }, IntPtr.Zero, 0U);
            User32.DeleteDC(hdc);
            mm.Free();
            if (e == 0)
            {
                e = User32.GetLastError();
                s = NativeError.Message;
            }

            FontInfo nf;
            var      ccol = new FontCollection();

            foreach (var f in fonts)
            {
                nf = new FontInfo(f);
                ccol.Add(nf);
            }

            ccol.Sort();
            return(ccol);
        }