我们有一个 WPF 应用程序,它允许用户按照自己的意愿输入和设置文本样式。然后将它们的文本转换为 XPS 以使用 ABCPdf 插入到 PDF 中。

我们最近切换到 .Net 4,现在 XPS 生成有时会生成格式错误的 XPS。

生成格式错误的 XPS 的唯一时间是用户使用重叠两个字符的字体(例如 Sevillana、available on Google Fonts)。在 .Net 4 中,字符重叠,而在 .Net 3.5 中,它们不重叠。从 another SO question 我了解到这是因为 .Net 4.0 改变了它的字体渲染引擎。

.Net 3.5:

.Net 4.0 :(注意“d”和“A”是如何重叠的)

看起来 .Net 4 没有考虑撇号的间距,甚至否定它。如果我删除撇号,间距会变宽。我创建了一个简单的测试项目来演示这个问题:https://github.com/tbroust-trepia/wpf-4-font-rendering

我们将 FlowDocument 保存到 XPS 的方法与 creating an XPS Document from a FlowDocument and attach it on the fly 中的几乎相同(我怀疑原始开发人员只是复制/粘贴了该代码);它只是将 XPS 保存到文件而不是流中。

The MS tool IsXPS 表示特定节点无效。事实上,在 Documents\1\Pages\1.fpage 中,Indices 的值为 ;,-16;,84;;;;,30 。我可以看到“0.-16”并不是一个真正的数字。

所以,我有很多问题:

  • 为什么会这样?我不明白这些值是如何转换成这些索引的。我猜转换引擎试图将其设置为“-0.16”但搞砸了?
  • 我可以在保存之前修改/检查这个索引的值吗?
  • 如果我不能,我之后如何检查?我找到了一种 XPS validator here 但我不明白如何从那里检查索引值。我可以手动完成,读取和解析 XML,但我确信这不是一个好主意。

  • 编辑

    I have opened a bug at Microsoft ,但我对那里的期望并不高。我想我会尝试修改生成的XPS。它很脏,但可能有用。

    最佳答案

    在等待 MS 为所欲为时,我找到了修复损坏的 XPS 的方法。

    Warning: dirty hack ahead

    public class XpsFile
    {
        /// <summary>
        /// Regex to validate XPS "indices" property (gotten from ABCPDF)
        /// </summary>
        private static readonly string IndicesRegex = @"(((\(([1-9][0-9]*)(:([1-9][0-9]*))?\))?([0-9]+))?(,(\+?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)?(,((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)?(,((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?))?)?)?)(;((\(([1-9][0-9]*)(:([1-9][0-9]*))?\))?([0-9]+))?(,(\+?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)?(,((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)?(,((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?))?)?)?)*";
    
        /// <summary>
        /// Fixes the XPS problems it encounters and knows about
        /// </summary>
        public static void FixXps(string filePath)
        {
            // first we'll load the XPS file
            using (var currentPackage = Package.Open(filePath, FileMode.Open))
            {
                var pageUri = new Uri("/Documents/1/Pages/1.fpage", UriKind.Relative);
    
                // check that the file we'll modify exists
                if (!currentPackage.PartExists(pageUri))
                {
                    throw new Exception(string.Format("Unable to find first page in XPS {0} - unable to fix this XPS !", filePath));
                }
    
                // assume the broken part is in the first page
                var firstPage = currentPackage.GetPart(pageUri);
                var relationships = firstPage.GetRelationships();
                var pageContent = XDocument.Load(firstPage.GetStream());
    
                // then we'll look up each glyph and check if their "Indices" property is valid
                XNamespace ns = pageContent.Root.GetDefaultNamespace();
                var glyphs = (from g in pageContent.Descendants(ns + "Glyphs")
                              where g.Attribute("Indices") != null
                              select g).ToList();
                for (var i = 0; i < glyphs.Count(); i ++)
                {
                    glyphs[i] = FixGlyph(glyphs[i]);
                }
    
                // remove the current (corrupted) file from the package
                currentPackage.DeletePart(pageUri);
    
                // add the new (shiny) file to the package
                var newPage = currentPackage.CreatePart(pageUri, "application/vnd.ms-package.xps-fixedpage+xml", CompressionOption.NotCompressed);
                using (var ms = new MemoryStream())
                {
                    // we need to remove XML declaration, so we need to use the XmlWriter
                    var settings = new XmlWriterSettings();
                    settings.Indent = false;
                    settings.NewLineChars = string.Empty;
                    settings.NewLineHandling = NewLineHandling.Replace;
                    settings.OmitXmlDeclaration = true;
                    using (var xw = XmlWriter.Create(ms, settings))
                    {
                        pageContent.WriteTo(xw);
                    }
    
                    ms.Seek(0, SeekOrigin.Begin);
                    CopyStream(newPage.GetStream(), ms);
                }
    
                // now we need to re-create the relationships between the Page file and the fonts
                foreach (var relation in relationships)
                {
                    newPage.CreateRelationship(relation.TargetUri, relation.TargetMode, relation.RelationshipType, relation.Id);
                }
            }
        }
    
        /// <summary>
        /// Tries to load the XPS, and returns false if it fails
        /// </summary>
        public static bool IsValidXps(string filePath)
        {
            try
            {
                using (var xpsOld = new XpsDocument(filePath, FileAccess.Read))
                {
                    var unused = xpsOld.GetFixedDocumentSequence();
                }
    
                return true;
            }
            catch (System.Windows.Markup.XamlParseException)
            {
                return false;
            }
        }
    
        /// <summary>
        /// Writes the whole content of a stream into another
        /// </summary>
        /// <remarks>
        /// http://stackoverflow.com/a/18885954/2354542
        /// </remarks>
        private static void CopyStream(Stream target, Stream source)
        {
            const int bufSize = 0x1000;
            byte[] buf = new byte[bufSize];
            int bytesRead = 0;
            while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
            {
                target.Write(buf, 0, bytesRead);
            }
        }
    
        /// <summary>
        /// Fixes the glyph, if necessary
        /// </summary>
        private static XElement FixGlyph(XElement g)
        {
            var matchAttribute = Regex.Match(g.Attribute("Indices").Value, IndicesRegex);
            if (!matchAttribute.Success)
            {
                return g;
            }
    
            var hasProblem = false;
            foreach (var token in matchAttribute.Value.Split(";".ToCharArray()))
            {
                if (token == ",")
                {
                    hasProblem = true;
                    break;
                }
            }
    
            if (hasProblem)
            {
                // the Indices attribute is not well-formed: let's try to fix the one(s) that are wrong
                var fixedTokens = new List<string>();
                foreach (var token in g.Attribute("Indices").Value.Split(";".ToCharArray()))
                {
                    var newToken = token;
                    var matchToken = Regex.Match(token, @",(-\d+)");
                    if (matchToken.Success) // negative number, yay ! it's not allowed :-(
                    {
                        newToken = ",0"; // it should be zero, I believe
                    }
    
                    fixedTokens.Add(newToken);
                }
    
                g.Attribute("Indices").Value = string.Join(";", fixedTokens);
            }
    
            return g;
        }
    }
    

    关于c# - XPS 编写器因 .Net 4/字体渲染错误而失败?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23246254/

    10-10 19:55