예제 #1
0
        AcquireResourceStream(
            Type resourceType,
            String resourceID
            )
        {
            XpsResourceStream resourceStream = null;

            if (_packagingPolicy != null)
            {
                if (resourceType == typeof(GlyphRun))
                {
                    resourceStream = _packagingPolicy.AcquireResourceStreamForXpsFont(resourceID);
                }
                else if (resourceType == typeof(BitmapSource))
                {
                    resourceStream = _packagingPolicy.AcquireResourceStreamForXpsImage(resourceID);
                }
                else if (resourceType == typeof(ColorContext))
                {
                    resourceStream = _packagingPolicy.AcquireResourceStreamForXpsColorContext(resourceID);
                }
                else if (resourceType == typeof(ResourceDictionary))
                {
                    resourceStream = _packagingPolicy.AcquireResourceStreamForXpsResourceDictionary(resourceID);
                }
                else
                {
                    throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NotSupported));
                }
            }

            return(resourceStream);
        }
        AcquireResourceStreamForXpsResourceDictionary(
            String resourceId
            )
        {
            XpsResourceStream resourceStream = null;

            if (_currentXpsResourceDictionaryRef == 0)
            {
                //
                // We need to create the corresponding part in the Xps package
                // and then acquire the Stream
                //
                if (_currentFixedPageWriter != null)
                {
                    _resourceDictionaryResourceStream = new XpsResourceStream(new MemoryStream(),
                                                                              new Uri("package/colorcontext", UriKind.Relative));
                }
                else
                {
                    //
                    // throw the appropriate exception
                    //
                }
            }

            _currentXpsResourceDictionaryRef++;

            resourceStream = _resourceDictionaryResourceStream;

            return(resourceStream);
        }
예제 #3
0
        AcquireResourceStream(
            Type    resourceType
            )
        {
            XpsResourceStream resourceStream = null;

            if(_packagingPolicy != null)
            {
                if (resourceType == typeof(GlyphRun))
                {
                    resourceStream = _packagingPolicy.AcquireResourceStreamForXpsFont();
                }
                else if (resourceType == typeof(BitmapSource))
                {
                    resourceStream = _packagingPolicy.AcquireResourceStreamForXpsImage(XpsS0Markup.ImageUriPlaceHolder);
                }
                else if (resourceType == typeof(ColorContext))
                {
                    resourceStream = _packagingPolicy.AcquireResourceStreamForXpsColorContext(XpsS0Markup.ColorContextUriPlaceHolder);
                }
                else if (resourceType == typeof(ResourceDictionary))
                {
                    resourceStream = _packagingPolicy.AcquireResourceStreamForXpsResourceDictionary(XpsS0Markup.ResourceDictionaryUriPlaceHolder);
                }
                else
                {
                    throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NotSupported));
                }
            }

            return resourceStream;
        }
예제 #4
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);
        }
예제 #5
0
        AcquireResourceStreamForXpsFont(
            )
        {
            XpsResourceStream resourceStream = null;

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

                if (_currentXpsFontRef == 0)
                {
                    //
                    // We need to create the corresponding part in the Xps package
                    // and then acquire the Stream
                    //
                    if (_currentFixedPageWriter != null)
                    {
                        _currentXpsFont = _currentFixedPageWriter.AddFont();
                        _interleavingPolicy.AddItem((INode)_currentXpsFont, 0, (INode)_currentFixedPageWriter);
                    }
                    else
                    {
                        throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NoFixedPageWriter));
                    }

                    //
                    // retreive the appropriate stream and uri from the reach package api layer
                    //
                    if (_currentXpsFont != null)
                    {
                        _fontResourceStream = new XpsResourceStream(_currentXpsFont.GetStream(),
                                                                    _currentXpsFont.Uri);
                    }
                }

                _currentXpsFontRef++;

                resourceStream = _fontResourceStream;
            }

            return(resourceStream);
        }
예제 #6
0
        AcquireResourceStreamForXpsImage(
            String resourceId
            )
        {
            XpsResourceStream resourceStream = null;

            if (_currentXpsImageRef == 0)
            {
                //
                // We need to create the corresponding part in the Xps package
                // and then acquire the Stream
                //
                if (_currentFixedPageWriter != null)
                {
                    _currentXpsImage = _currentFixedPageWriter.AddImage(resourceId);
                    _interleavingPolicy.AddItem((INode)_currentXpsImage, 0, (INode)_currentFixedPageWriter);
                }
                else
                {
                    throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NoFixedPageWriter));
                }

                //
                // retreive the appropriate stream and uri from the reach package api layer
                //
                if (_currentXpsImage != null)
                {
                    _imageResourceStream = new XpsResourceStream(_currentXpsImage.GetStream(),
                                                                 _currentXpsImage.Uri);
                }
            }

            _currentXpsImageRef++;

            resourceStream = _imageResourceStream;

            return(resourceStream);
        }
예제 #7
0
        ConvertTo(
            ITypeDescriptorContext context,
            System.Globalization.CultureInfo culture,
            object value,
            Type destinationType
            )
        {
            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertImageBegin);

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (!IsSupportedType(destinationType))
            {
                throw new NotSupportedException(SR.Get(SRID.Converter_ConvertToNotSupported));
            }

            //
            // Check that we have a valid BitmapSource instance.
            //
            BitmapSource bitmapSource = (BitmapSource)value;

            if (bitmapSource == null)
            {
                throw new ArgumentException(SR.Get(SRID.MustBeOfType, "value", "BitmapSource"));
            }

            //
            // Get the current serialization manager.
            //
            PackageSerializationManager manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));

            //Get the image Uri if it has already been serialized
            Uri imageUri = GetBitmapSourceFromImageTable(manager, bitmapSource);

            //
            // Get the current page image cache
            //
            Dictionary <int, Uri> currentPageImageTable = manager.ResourcePolicy.CurrentPageImageTable;

            if (imageUri != null)
            {
                int uriHashCode = imageUri.GetHashCode();
                if (!currentPageImageTable.ContainsKey(uriHashCode))
                {
                    //
                    // Also, add a relationship for the current page to this image
                    // resource.  This is needed to conform with Xps specification.
                    //
                    manager.AddRelationshipToCurrentPage(imageUri, XpsS0Markup.ResourceRelationshipName);
                    currentPageImageTable.Add(uriHashCode, imageUri);
                }
            }
            else
            {
                //
                // This image as never serialized before so we will do it now.
                // Retrieve the image serialization service from the resource policy
                //
                IServiceProvider resourceServiceProvider = manager.ResourcePolicy;

                XpsImageSerializationService imageService = (XpsImageSerializationService)resourceServiceProvider.GetService(typeof(XpsImageSerializationService));
                if (imageService == null)
                {
                    throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NoImageService));
                }

                //
                // Obtain a valid encoder for the image.
                //
                BitmapEncoder encoder       = imageService.GetEncoder(bitmapSource);
                string        imageMimeType = GetImageMimeType(encoder);

                bool isSupportedMimeType = imageService.IsSupportedMimeType(bitmapSource);

                //
                // Acquire a writable stream from the serialization manager and encode
                // and serialize the image into the stream.
                //
                XpsResourceStream resourceStream = manager.AcquireResourceStream(typeof(BitmapSource), imageMimeType);
                bool bCopiedStream = false;

                BitmapFrame bitmapFrame = bitmapSource as BitmapFrame;

                if (isSupportedMimeType &&
                    bitmapFrame != null &&
                    bitmapFrame.Decoder != null
                    )
                {
                    BitmapDecoder decoder = bitmapFrame.Decoder;
                    try
                    {
                        Uri    sourceUri = new Uri(decoder.ToString());
                        Stream srcStream = MS.Internal.WpfWebRequestHelper.CreateRequestAndGetResponseStream(sourceUri);
                        CopyImageStream(srcStream, resourceStream.Stream);
                        srcStream.Close();
                        bCopiedStream = true;
                    }
                    catch (UriFormatException)
                    {
                        //the uri was not valid we will re-encode the image below
                    }
                    catch (WebException)
                    {
                        //Web Request failed we will re-encode the image below
                    }
                }

                if (!bCopiedStream)
                {
                    Stream stream = new MemoryStream();
                    ReEncodeImage(bitmapSource, encoder, stream);
                    stream.Position = 0;
                    CopyImageStream(stream, resourceStream.Stream);
                }

                //
                // Make sure to commit the resource stream by releasing it.
                //
                imageUri = resourceStream.Uri;
                manager.ReleaseResourceStream(typeof(BitmapSource));

                AddBitmapSourceToImageTables(manager, imageUri);
            }

            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertImageEnd);

            return(imageUri);
        }
        SerializeColorContext(
            IServiceProvider context,
            ColorContext colorContext
            )
        {
            Uri profileUri = null;

            if (colorContext == null)
            {
                throw new ArgumentNullException("colorContext");
            }
            if (context != null)
            {
                PackageSerializationManager manager                      = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));
                Dictionary <int, Uri>       colorContextTable            = manager.ResourcePolicy.ColorContextTable;
                Dictionary <int, Uri>       currentPageColorContextTable = manager.ResourcePolicy.CurrentPageColorContextTable;

                if (currentPageColorContextTable == null)
                {
                    //
                    // throw the appropriae exception
                    //
                }

                if (colorContextTable == null)
                {
                    //
                    // throw the appropriae exception
                    //
                }

                if (colorContextTable.ContainsKey(colorContext.GetHashCode()))
                {
                    //
                    // The colorContext has already been cached (and therefore serialized).
                    // No need to serialize it again so we just use the Uri in the
                    // package where the original was serialized. For that Uri used
                    // a relationship is only created if this has not been included on
                    // the current page before.
                    //
                    profileUri = colorContextTable[colorContext.GetHashCode()];

                    if (!currentPageColorContextTable.ContainsKey(colorContext.GetHashCode()))
                    {
                        //
                        // Also, add a relationship for the current page to this Color Context
                        // resource.  This is needed to conform with Xps specification.
                        //
                        manager.AddRelationshipToCurrentPage(profileUri, XpsS0Markup.ResourceRelationshipName);
                        currentPageColorContextTable.Add(colorContext.GetHashCode(), profileUri);
                    }
                }
                else
                {
                    MS.Internal.ContentType colorContextMimeType = XpsS0Markup.ColorContextContentType;

                    XpsResourceStream resourceStream = manager.AcquireResourceStream(typeof(ColorContext), colorContextMimeType.ToString());

                    byte [] buffer = new byte[512];

                    Stream profileStream = colorContext.OpenProfileStream();
                    int    count;

                    while ((count = profileStream.Read(buffer, 0, buffer.GetLength(0))) > 0)
                    {
                        resourceStream.Stream.Write(buffer, 0, count);
                    }

                    profileStream.Close();

                    //
                    // Make sure to commit the resource stream by releasing it.
                    //
                    profileUri = resourceStream.Uri;
                    manager.ReleaseResourceStream(typeof(ColorContext));

                    colorContextTable.Add(colorContext.GetHashCode(), profileUri);
                    currentPageColorContextTable.Add(colorContext.GetHashCode(), profileUri);
                }
            }
            else // Testing only
            {
                profileUri = colorContext.ProfileUri;
            }

            //First Step make sure that nothing that should not be escaped is escaped
            Uri safeUnescapedUri = new Uri(profileUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.SafeUnescaped),
                                           profileUri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative);
            //Second Step make sure that everything that should escaped is escaped
            String uriString = safeUnescapedUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);

            return(uriString);
        }
        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(resourceId);

                        Stream fontStream = new MemoryStream();

                        if (fontStream != null)
                        {
                            resourceStreamCacheItem.XpsResource = null;
                            //
                            // retreive the appropriate stream and uri from the reach package api layer
                            //
                            _fontResourceStream = new XpsResourceStream(fontStream,
                                                                        new Uri("package/font", UriKind.Relative));

                            resourceStreamCacheItem.XpsResourceStream = _fontResourceStream;

                            _fontsCache[resourceId] = resourceStreamCacheItem;

                            resourceStream = _fontResourceStream;
                            //
                            // This is to handle PSharp bug claiming we do not dispose
                            // this class.  We can not dipose because ownership has been handed off.
                            // thus we set it to null
                            //
                            fontStream = null;
                        }
                        else
                        {
                            //
                            // throw the appropriate exception
                            //
                        }
                    }
                    else
                    {
                        throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NoFixedPageWriter));
                    }
                }
                else
                {
                    resourceStream = resourceStreamCacheItem.XpsResourceStream;
                    resourceStreamCacheItem.IncRef();
                }
            }
            else
            {
                //
                // throw the appropraite exception
                //
            }

            return(resourceStream);
        }
        AcquireResourceStreamForXpsFont(
            )
        {
            XpsResourceStream resourceStream = null;

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

                if (_currentXpsFontRef == 0)
                {
                    //
                    // We need to create the corresponding part in the Xps package
                    // and then acquire the Stream
                    //
                    Stream fontStream = null;

                    if (_currentFixedPageWriter != null)
                    {
                        //
                        // Create a new Font Stream
                        //
                        fontStream = new MemoryStream();
                    }
                    else
                    {
                        throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NoFixedPageWriter));
                    }

                    //
                    // retreive the appropriate stream and uri from the reach package api layer
                    //
                    if (fontStream != null)
                    {
                        _fontResourceStream = new XpsResourceStream(fontStream,
                                                                    new Uri("package/font", UriKind.Relative));
                        //
                        // This is to handle PSharp bug claiming we do not dispose
                        // this class.  We can not dipose because ownership has been handed off.
                        // thus we set it to null
                        //
                        fontStream = null;
                    }
                    else
                    {
                        //
                        // throw the appropriate exception
                        //
                    }
                }

                _currentXpsFontRef++;

                resourceStream = _fontResourceStream;
            }
            else
            {
                //
                // throw the appropraite exception
                //
            }

            return(resourceStream);
        }