Beispiel #1
0
        private void LoadLocalFont(string fontPath, SvgWindow ownedWindow, SvgFontFaceElement fontFace)
        {
            if (string.IsNullOrWhiteSpace(fontPath) || !File.Exists(fontPath))
            {
//                Trace.WriteLine("Private font not found: " + fontPath);
                return;
            }

            string fileExt = Path.GetExtension(fontPath);

            if (string.Equals(fileExt, ".svg", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase))
            {
                SvgDocument document = new SvgDocument(ownedWindow);

                document.Load(fontPath);
                var svgFonts = document.SvgFonts;

                if (svgFonts != null && svgFonts.Count != 0)
                {
                    foreach (var svgFont in svgFonts)
                    {
                        if (fontFace != null && fontFace.HasAttribute("unicode-range"))
                        {
                            svgFont.SetAttribute("unicode-range", fontFace.GetAttribute("unicode-range"));
                        }

                        var fontNode = this.ImportNode(svgFont, true);
                        this.DocumentElement.AppendChild(fontNode);

//                        this.SvgFonts.Add(svgFont);
                    }
                }

                document = null;
            }
            else if (string.Equals(fileExt, ".ttf", StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(fileExt, ".otf", StringComparison.OrdinalIgnoreCase))
            {
                Debug.Assert(false, "Testing files with this format");
            }
            else if (string.Equals(fileExt, ".woff", StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(fileExt, ".woff2", StringComparison.OrdinalIgnoreCase))
            {
                Debug.Assert(false, "Testing files with this format");
            }
        }
Beispiel #2
0
        private void LoadLocalFont(string fontPath, SvgWindow ownedWindow, SvgFontFaceElement fontFace)
        {
            if (string.IsNullOrWhiteSpace(fontPath) || !File.Exists(fontPath))
            {
//                Trace.WriteLine("Private font not found: " + fontPath);
                return;
            }

            string fileExt = Path.GetExtension(fontPath);

            if (string.Equals(fileExt, ".svg", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase))
            {
                SvgDocument document = new SvgDocument(ownedWindow);

                document.Load(fontPath);
                var svgFonts = document.SvgFonts;

                if (svgFonts != null && svgFonts.Count != 0)
                {
                    foreach (var svgFont in svgFonts)
                    {
                        if (fontFace != null && fontFace.HasAttribute("unicode-range"))
                        {
                            svgFont.SetAttribute("unicode-range", fontFace.GetAttribute("unicode-range"));
                        }

                        var fontNode = this.ImportNode(svgFont, true);
                        this.DocumentElement.AppendChild(fontNode);

//                        this.SvgFonts.Add(svgFont);
                    }
                }

                document = null;
            }
            else if (string.Equals(fileExt, ".woff", StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(fileExt, ".woff2", StringComparison.OrdinalIgnoreCase))
            {
                if (_fontFamilies == null)
                {
                    _fontFamilies = new List <SvgFontFamily>();
                }

                var woffParser = new SvgWoffParser();
                if (woffParser.Import(fontPath))
                {
                    string fontExportPath = woffParser.DefaultExportPath;
                    if (File.Exists(fontExportPath))
                    {
                        var fontFamily = new SvgFontFamily(true, fontExportPath, fontPath);
                        _fontFamilies.Add(fontFamily);
                    }
                    else
                    {
                        fontExportPath = woffParser.GetExportPath();
                        if (woffParser.Export(fontExportPath))
                        {
                            var fontFamily = new SvgFontFamily(true, fontExportPath, fontPath);

                            _fontFamilies.Add(fontFamily);
                        }
                    }
                }
            }
            else if (string.Equals(fileExt, ".ttf", StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(fileExt, ".otf", StringComparison.OrdinalIgnoreCase))
            {
                if (_fontFamilies == null)
                {
                    _fontFamilies = new List <SvgFontFamily>();
                }
                var fontFamily = new SvgFontFamily(false, fontPath);

                _fontFamilies.Add(fontFamily);
            }
            else if (string.Equals(fileExt, ".ttc", StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(fileExt, ".otc", StringComparison.OrdinalIgnoreCase))
            {
                if (_fontFamilies == null)
                {
                    _fontFamilies = new List <SvgFontFamily>();
                }
                var fontFamily = new SvgFontFamily(false, fontPath);

                _fontFamilies.Add(fontFamily);
            }
        }
Beispiel #3
0
        protected virtual IList <Tuple <string, SvgFontFaceElement> > GetFontUrls()
        {
            List <Tuple <string, SvgFontFaceElement> > fontUrls = new List <Tuple <string, SvgFontFaceElement> >();

            if (this.IsLoaded == false)
            {
                return(fontUrls);
            }

            XmlNodeList xmlList = this.SelectNodes(".//svg:font-face-uri", this.NamespaceManager);

            if (xmlList != null && xmlList.Count != 0)
            {
                foreach (XmlElement xmlNode in xmlList)
                {
                    SvgFontFaceElement fontFace = null;
                    var fontSource = xmlNode.ParentNode as SvgFontFaceSrcElement;
                    if (fontSource != null)
                    {
                        fontFace = fontSource.ParentNode as SvgFontFaceElement;
                    }

                    if (xmlNode.HasAttribute("href", SvgDocument.XLinkNamespace))
                    {
                        string fontUrl = xmlNode.GetAttribute("href", SvgDocument.XLinkNamespace);
                        if (!string.IsNullOrWhiteSpace(fontUrl))
                        {
                            fontUrls.Add(new Tuple <string, SvgFontFaceElement>(fontUrl, fontFace));
                        }
                    }
                    else if (xmlNode.HasAttribute("href"))
                    {
                        string fontUrl = xmlNode.GetAttribute("href");
                        if (!string.IsNullOrWhiteSpace(fontUrl))
                        {
                            fontUrls.Add(new Tuple <string, SvgFontFaceElement>(fontUrl, fontFace));
                        }
                    }
                }
            }

            IStyleSheetList styleSheets = this.StyleSheets;

            if (styleSheets != null && styleSheets.Count != 0)
            {
                foreach (var styleSheet in styleSheets)
                {
                    var cssSheet = styleSheet as CssStyleSheet;
                    if (cssSheet == null)
                    {
                        continue;
                    }

                    GetFontUrl(cssSheet, fontUrls, _styledFontIds);
                }
            }

            GetFontUrl(this.UserAgentStyleSheet, fontUrls, _styledFontIds);
            GetFontUrl(this.UserStyleSheet, fontUrls, _styledFontIds);

            return(fontUrls);
        }