Search code examples
c#.netwpfxps

XPS writer fails with .Net 4 / font rendering bug?


We have a WPF application that allows users to enter and style text as they wish. Their text is then transformed to XPS to be inserted in a PDF using ABCPdf.

We have recently switched to .Net 4, and now the XPS generation sometimes generates badly-formed XPS.

The only time when a badly-formed XPS is generated is when the user uses a font that overlaps two characters (such as Sevillana, available on Google Fonts). In .Net 4, the characters overlap, whereas in .Net 3.5, they do not. From another SO question I gather that it's because .Net 4.0 has changed its font rendering engine.

.Net 3.5 :
DotNet 3.5

.Net 4.0 : (note how the "d" and "A" overlap)
DotNet 4.0

It looks like .Net 4 does not take the apostrophe's spacing into account, or even negates it. If I remove the apostrophe, the spacing widens. I have created a simple test project to demonstrate the issue : https://github.com/tbroust-trepia/wpf-4-font-rendering

Our method to save a FlowDocument to XPS is pretty much the same as in creating an XPS Document from a FlowDocument and attach it on the fly (I suspect that the original developer just copy/pasted that code) ; it just saves the XPS to a file instead of in a stream.

The MS tool IsXPS says that a specific node is invalid. As a matter of fact, in Documents\1\Pages\1.fpage, the value of Indices is ;,-16;,84;;;;,30. I can see that "0.-16" is not really a number.

So, I have many questions:

  • Why is it happening ? I don't understand how the values are converted into these Indices. I guess that the transformation engine tried to set it to "-0.16" but messed up ?
  • Can I modify/check the value of this Indices before it's saved ?
  • If I cannot, how can I check afterwards ? I have found a sort of XPS validator here but I don't understand how I can check the Indices value from there. I could do it manually, reading and parsing the XML, but I'm really sure it's not a good idea.

Edit

I have opened a bug at Microsoft, but I don't expect much there. I think I will try to modify the generated XPS. It's dirty but it might work.


Solution

  • While waiting for MS to do whatever they want, I have found a way to fix the broken 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;
        }
    }