Example #1
0
        private void AddFonts(IXpsFixedPageReader fixedPageReader, IXpsFixedPageWriter pageWriter)
        {
            // Adding font from source to resources.
            foreach (XpsFont font in fixedPageReader.Fonts)
            {
                if (!CheckIfFontAddedAlready(font.Uri.ToString()))
                {
                    XpsFont newFont = pageWriter.AddFont(false);

                    using (Stream dstFontStream = newFont.GetStream())
                    {
                        using (Stream srcFontStream = font.GetStream())
                        {
                            if (font.IsObfuscated)
                            {
                                WriteObfuscatedStream(font.Uri.ToString(),
                                                      dstFontStream, srcFontStream);
                            }
                            else
                            {
                                WriteToStream(dstFontStream, srcFontStream);
                            }
                            newFont.Commit();
                            XpsDetails xpsFont = new XpsDetails();
                            xpsFont.resource  = newFont;
                            xpsFont.sourceURI = font.Uri;
                            xpsFont.destURI   = newFont.Uri;
                            xpsFonts.Add(xpsFont);
                        }
                    }
                }
            }
        }
Example #2
0
        public static void SaveToDisk(this XpsFont font, string path)
        {
            using (Stream stm = font.GetStream())
            {
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    byte[] dta = new byte[stm.Length];
                    stm.Read(dta, 0, dta.Length);
                    if (font.IsObfuscated)
                    {
                        string guid      = new Guid(font.Uri.GetFileName().Split('.')[0]).ToString("N");
                        byte[] guidBytes = new byte[16];
                        for (int i = 0; i < guidBytes.Length; i++)
                        {
                            guidBytes[i] = Convert.ToByte(guid.Substring(i * 2, 2), 16);
                        }

                        for (int i = 0; i < 32; i++)
                        {
                            int gi = guidBytes.Length - (i % guidBytes.Length) - 1;
                            dta[i] ^= guidBytes[gi];
                        }
                    }
                    fs.Write(dta, 0, dta.Length);
                }
            }
        }
Example #3
0
        AcquireResourceStreamForXpsFont(
            String resourceId
            )
        {
            XpsResourceStream resourceStream = null;

            if (_fontAcquireMode != ResourceAcquireMode.SingleAcquired)
            {
                if (_fontAcquireMode == ResourceAcquireMode.NoneAcquired)
                {
                    _fontAcquireMode = ResourceAcquireMode.MultipleAcquired;
                }

                ResourceStreamCacheItem resourceStreamCacheItem = (ResourceStreamCacheItem)_fontsCache[resourceId];

                if (resourceStreamCacheItem == null)
                {
                    resourceStreamCacheItem = new ResourceStreamCacheItem();

                    //
                    // We need to create the corresponding part in the Xps package
                    // and then acquire the Stream
                    //
                    if (_currentFixedPageWriter != null)
                    {
                        XpsFont reachFont = _currentFixedPageWriter.AddFont();

                        if (reachFont != null)
                        {
                            _interleavingPolicy.AddItem((INode)reachFont, 0, (INode)_currentFixedPageWriter);
                            resourceStreamCacheItem.XpsResource = (XpsResource)reachFont;
                            //
                            // retreive the appropriate stream and uri from the reach package api layer
                            //
                            _fontResourceStream = new XpsResourceStream(reachFont.GetStream(),
                                                                        reachFont.Uri);

                            resourceStreamCacheItem.XpsResourceStream = _fontResourceStream;

                            _fontsCache[resourceId] = resourceStreamCacheItem;

                            resourceStream = _fontResourceStream;
                        }
                    }
                    else
                    {
                        throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NoFixedPageWriter));
                    }
                }
                else
                {
                    resourceStream = resourceStreamCacheItem.XpsResourceStream;
                    resourceStreamCacheItem.IncRef();
                }
            }

            return(resourceStream);
        }
        public static void SaveToDisk(XpsFont font, string path)
        {
            string folder = System.IO.Path.GetDirectoryName(path);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            using (Stream stm = font.GetStream()){
                using (FileStream fs = new FileStream(path, FileMode.Create)){
                    byte[] dta = new byte[stm.Length];
                    stm.Read(dta, 0, dta.Length);
                    if (font.IsObfuscated)
                    {
                        string guid = new Guid(GetFileName(font.Uri).Split('.')[0]).ToString("N");
                        DeobfuscateData(dta, guid);
                    }
                    fs.Write(dta, 0, dta.Length);
                }
            }
        }
Example #5
0
        }// end:AddGlyphRun()

        // --------------------------- GetXpsFont() ---------------------------
        /// <summary>
        ///   Returns the relative path to a font part in the package.</summary>
        /// <param name="pageWriter">
        ///   The page to associate the font with.</param>
        /// <param name="fontUri">
        ///   The URI for the source font file.</param>
        /// <returns>
        ///   The relative path to font part in the package.</returns>
        /// <remarks>
        ///   If the font has not been added previously, GetXpsFont creates a
        ///   new font part and copies the font data from the specified
        ///   source font URI.</remarks>
        private string GetXpsFont(IXpsFixedPageWriter pageWriter, string fontUri)
        {
            string packageFontUri;

            if (_fontDictionary.ContainsKey(fontUri))
            {
                packageFontUri = _fontDictionary[fontUri];
            }
            else
            {
                XpsFont xpsFont   = pageWriter.AddFont(false);
                Stream  dstStream = xpsFont.GetStream();
                Stream  srcStream =
                    new FileStream(fontUri, FileMode.Open, FileAccess.Read);
                Byte[] streamBytes = new Byte[srcStream.Length];
                srcStream.Read(streamBytes, 0, (int)srcStream.Length);
                dstStream.Write(streamBytes, 0, (int)srcStream.Length);
                _fontDictionary[fontUri] = xpsFont.Uri.ToString();
                packageFontUri           = xpsFont.Uri.ToString();
                xpsFont.Commit();
            }
            return(packageFontUri);
        }// end:GetXpsFont()
Example #6
0
		static byte[] DeobfuscateFont(XpsFont font)
		{
			using (var stm = font.GetStream())
			{
				byte[] dta = new byte[stm.Length];
				stm.Read(dta, 0, dta.Length);
				if (font.IsObfuscated)
				{
					string guid = new Guid(System.IO.Path.GetFileNameWithoutExtension(font.Uri.ToString())).ToString("N");
					byte[] guidBytes = new byte[16];
					for (int i = 0; i < guidBytes.Length; i++)
						guidBytes[i] = Convert.ToByte(guid.Substring(i * 2, 2), 16);

					for (int i = 0; i < 32; i++)
					{
						int gi = guidBytes.Length - (i % guidBytes.Length) - 1;
						dta[i] ^= guidBytes[gi];
					}
				}
				return dta;
			}
		}