Exemplo n.º 1
0
        /// <summary>
        /// Registers the font face.
        /// </summary>
        public static XFontSource RegisterFontFace(byte[] fontBytes)
        {
            try
            {
                Lock.EnterFontFactory();
                ulong       key = FontHelper.CalcChecksum(fontBytes);
                XFontSource fontSource;
                if (FontSourcesByKey.TryGetValue(key, out fontSource))
                {
                    throw new InvalidOperationException("Font face already registered.");
                }
                fontSource = XFontSource.GetOrCreateFrom(fontBytes);
                Debug.Assert(FontSourcesByKey.ContainsKey(key));
                Debug.Assert(fontSource.Fontface != null);

                //fontSource.Fontface = new OpenTypeFontface(fontSource);
                //FontSourcesByKey.Add(checksum, fontSource);
                //FontSourcesByFontName.Add(fontSource.FontName, fontSource);

                XGlyphTypeface glyphTypeface = new XGlyphTypeface(fontSource);
                FontSourcesByName.Add(glyphTypeface.Key, fontSource);
                GlyphTypefaceCache.AddGlyphTypeface(glyphTypeface);
                return(fontSource);
            }
            finally { Lock.ExitFontFactory(); }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Caches a font source under its face name and its key.
        /// </summary>
        public static XFontSource CacheFontSource(XFontSource fontSource)
        {
            try
            {
                Lock.EnterFontFactory();
                // Check whether an identical font source with a different face name already exists.
                XFontSource existingFontSource;
                if (FontSourcesByKey.TryGetValue(fontSource.Key, out existingFontSource))
                {
#if DEBUG
                    // Fonts have same length and check sum. Now check byte by byte identity.
                    int length = fontSource.Bytes.Length;
                    for (int idx = 0; idx < length; idx++)
                    {
                        if (existingFontSource.Bytes[idx] != fontSource.Bytes[idx])
                        {
                            //Debug.Assert(false,"Two fonts with identical checksum found.");
                            break;
                            //goto FontsAreNotIdentical;
                        }
                    }
                    Debug.Assert(existingFontSource.Fontface != null);
#endif
                    return(existingFontSource);

                    //FontsAreNotIdentical:
                    //// Incredible rare case: Two different fonts have the same size and check sum.
                    //// Give the new one a new key until it do not clash with an existing one.
                    //while (FontSourcesByKey.ContainsKey(fontSource.Key))
                    //    fontSource.IncrementKey();
                }

                var fontface = fontSource.Fontface;
                if (fontface == null)
                {
                    // Create OpenType fontface for this font source.
                    try
                    {
                        fontSource.Fontface = new OpenTypeFontface(fontSource);
                    }
                    catch (InvalidOperationException)
                    {
                        // todo: log
                    }
                }

                if (fontSource.FontName == null)
                {
                    return(null);
                }

                FontSourcesByKey.Add(fontSource.Key, fontSource);
                FontSourcesByName.Add(fontSource.FontName, fontSource);

                return(fontSource);
            }
            finally { Lock.ExitFontFactory(); }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Caches a font source under its face name and its key.
        /// </summary>
        public static XFontSource CacheNewFontSource(string typefaceKey, XFontSource fontSource)
        {
            // Debug.Assert(!FontSourcesByFaceName.ContainsKey(fontSource.FaceName));

            // Check whether an identical font source with a different face name already exists.
            XFontSource existingFontSource;

            if (FontSourcesByKey.TryGetValue(fontSource.Key, out existingFontSource))
            {
                //// Fonts have same length and check sum. Now check byte by byte identity.
                //int length = fontSource.Bytes.Length;
                //for (int idx = 0; idx < length; idx++)
                //{
                //    if (existingFontSource.Bytes[idx] != fontSource.Bytes[idx])
                //    {
                //        goto FontsAreNotIdentical;
                //    }
                //}
                return(existingFontSource);

                ////// The bytes are really identical. Register font source again with the new face name
                ////// but return the existing one to save memory.
                ////FontSourcesByFaceName.Add(fontSource.FaceName, existingFontSource);
                ////return existingFontSource;

                //FontsAreNotIdentical:
                //// Incredible rare case: Two different fonts have the same size and check sum.
                //// Give the new one a new key until it do not clash with an existing one.
                //while (FontSourcesByKey.ContainsKey(fontSource.Key))
                //    fontSource.IncrementKey();
            }

            OpenTypeFontface fontface = fontSource.Fontface;

            if (fontface == null)
            {
                fontface            = new OpenTypeFontface(fontSource);
                fontSource.Fontface = fontface;  // Also sets the font name in fontSource
            }

            FontSourcesByName.Add(typefaceKey, fontSource);
            FontSourcesByName.Add(fontSource.FontName, fontSource);
            FontSourcesByKey.Add(fontSource.Key, fontSource);

            return(fontSource);
        }
Exemplo n.º 4
0
 public static bool TryGetFontSourceByKey(ulong key, out XFontSource fontSource)
 {
     return(FontSourcesByKey.TryGetValue(key, out fontSource));
 }
Exemplo n.º 5
0
 public static bool TryGetFontSourceByKey(ulong key, out XFontSource fontSource) => FontSourcesByKey.TryGetValue(key, out fontSource);