Exemplo n.º 1
0
        /// <summary>
        /// So this weird shit works as follows:
        /// PdfSharp passes a font name, whether or not it is italic
        /// and/or bold and expects a unique name based on that. <see cref="ResolveTypeface"/>
        /// Then it passes this name to <see cref="GetFont"/> and gets the binary data of the font
        /// So this constructor basically looks up the truetype fonts on a unix system and
        /// loads the binary data of the ones we are interested in
        /// <see cref="fontInfoCache"/>
        /// <see cref="fontDataCache"/>
        /// </summary>
        public NClassFontResolver()
        {
            fontInfoCache = EnumerateUnixFonts();
            fontDataCache = new Dictionary <string, byte[]>();

            var style = Style.CurrentStyle;

            foreach (var fontProperty in fontProperties)
            {
                var font = fontProperty(style);

                var facename = FontInfo.FromFont(font).FaceName;

                if (!fontInfoCache.ContainsKey(facename))
                {
                    continue;
                }

                var    fontInfo = fontInfoCache[facename];
                byte[] data;
                using (var fs = new FileStream(fontInfo.Filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var size = fs.Length;
                    if (size > int.MaxValue)
                    {
                        throw new Exception("Font file is too big.");
                    }
                    var length = (int)size;
                    data = new byte[length];
                    var read = fs.Read(data, 0, length);
                    if (length != read)
                    {
                        throw new Exception("Reading font file failed.");
                    }
                }

                fontDataCache[facename] = data;
            }
        }