Exemplo n.º 1
0
 protected internal virtual PdfXObject TryResolveBase64ImageSource(String src)
 {
     try {
         String fixedSrc = iText.IO.Util.StringUtil.ReplaceAll(src, "\\s", "");
         fixedSrc = fixedSrc.Substring(fixedSrc.IndexOf(BASE64IDENTIFIER, StringComparison.Ordinal) + 7);
         PdfXObject imageXObject = imageCache.GetImage(fixedSrc);
         if (imageXObject == null)
         {
             imageXObject = new PdfImageXObject(ImageDataFactory.Create(Convert.FromBase64String(fixedSrc)));
             imageCache.PutImage(fixedSrc, imageXObject);
         }
         return(imageXObject);
     }
     catch (Exception) {
     }
     return(null);
 }
        public virtual void SimpleImageCacheTest01()
        {
            SimpleImageCache cache  = new SimpleImageCache();
            String           imgSrc = "src1.jpg";

            SimpleImageCacheTest.ImageXObjectStub imageData = new SimpleImageCacheTest.ImageXObjectStub();
            NUnit.Framework.Assert.AreEqual(0, cache.Size());
            cache.PutImage(imgSrc, imageData);
            NUnit.Framework.Assert.AreEqual(1, cache.Size());
            NUnit.Framework.Assert.AreEqual(imageData, cache.GetImage(imgSrc));
        }
Exemplo n.º 3
0
        public virtual void OrderRemovingFromCacheTest()
        {
            SimpleImageCache cache = new SimpleImageCache(10);

            for (int j = 0; j <= 9; j++)
            {
                cache.PutImage("src" + j + ".jpg", new SimpleImageCacheTest.ImageXObjectStub());
            }
            for (int i = 0; i <= 9; i++)
            {
                cache.PutImage("src" + i + 10 + ".jpg", new SimpleImageCacheTest.ImageXObjectStub());
                NUnit.Framework.Assert.IsNull(cache.GetImage("src" + i + ".jpg"));
            }
        }
        public virtual void SimpleImageCacheTest02()
        {
            String[] imgSrc = new String[] { "src0.jpg", "src1.jpg", "src2.jpg", "src3.jpg", "src4.jpg", "src5.jpg" };
            SimpleImageCacheTest.ImageXObjectStub[] imgData = new SimpleImageCacheTest.ImageXObjectStub[] { new SimpleImageCacheTest.ImageXObjectStub
                                                                                                                (), new SimpleImageCacheTest.ImageXObjectStub(), new SimpleImageCacheTest.ImageXObjectStub(), new SimpleImageCacheTest.ImageXObjectStub
                                                                                                                (), new SimpleImageCacheTest.ImageXObjectStub(), new SimpleImageCacheTest.ImageXObjectStub() };
            SimpleImageCache cache = new SimpleImageCache(4);

            // imgs frequency is increased on getImage call
            cache.GetImage(imgSrc[1]);
            cache.GetImage(imgSrc[2]);
            cache.PutImage(imgSrc[0], imgData[0]);
            cache.PutImage(imgSrc[1], imgData[1]);
            cache.PutImage(imgSrc[2], imgData[2]);
            NUnit.Framework.Assert.AreEqual(3, cache.Size());
            cache.GetImage(imgSrc[0]);
            cache.GetImage(imgSrc[1]);
            cache.GetImage(imgSrc[2]);
            cache.PutImage(imgSrc[3], imgData[3]);
            NUnit.Framework.Assert.AreEqual(4, cache.Size());
            cache.PutImage(imgSrc[4], imgData[4]);
            NUnit.Framework.Assert.AreEqual(4, cache.Size());
            NUnit.Framework.Assert.IsNull(cache.GetImage(imgSrc[3]));
            NUnit.Framework.Assert.AreEqual(imgData[0], cache.GetImage(imgSrc[0]));
            NUnit.Framework.Assert.AreEqual(imgData[1], cache.GetImage(imgSrc[1]));
            NUnit.Framework.Assert.AreEqual(imgData[2], cache.GetImage(imgSrc[2]));
            NUnit.Framework.Assert.AreEqual(imgData[4], cache.GetImage(imgSrc[4]));
            cache.GetImage(imgSrc[0]);
            cache.GetImage(imgSrc[1]);
            cache.GetImage(imgSrc[2]);
            cache.GetImage(imgSrc[4]);
            cache.PutImage(imgSrc[5], imgData[5]);
            NUnit.Framework.Assert.AreEqual(4, cache.Size());
            NUnit.Framework.Assert.IsNull(cache.GetImage(imgSrc[4]));
            NUnit.Framework.Assert.AreEqual(imgData[0], cache.GetImage(imgSrc[0]));
            NUnit.Framework.Assert.AreEqual(imgData[1], cache.GetImage(imgSrc[1]));
            NUnit.Framework.Assert.AreEqual(imgData[2], cache.GetImage(imgSrc[2]));
            NUnit.Framework.Assert.AreEqual(imgData[5], cache.GetImage(imgSrc[5]));
            cache.PutImage(imgSrc[3], imgData[3]);
            NUnit.Framework.Assert.AreEqual(4, cache.Size());
            NUnit.Framework.Assert.AreEqual(imgData[3], cache.GetImage(imgSrc[3]));
            NUnit.Framework.Assert.IsNull(cache.GetImage(imgSrc[5]));
            cache.PutImage(imgSrc[5], imgData[5]);
            NUnit.Framework.Assert.AreEqual(4, cache.Size());
            NUnit.Framework.Assert.AreEqual(imgData[5], cache.GetImage(imgSrc[5]));
            NUnit.Framework.Assert.IsNull(cache.GetImage(imgSrc[3]));
            cache.PutImage(imgSrc[3], imgData[3]);
            NUnit.Framework.Assert.AreEqual(4, cache.Size());
            NUnit.Framework.Assert.AreEqual(imgData[3], cache.GetImage(imgSrc[3]));
            NUnit.Framework.Assert.IsNull(cache.GetImage(imgSrc[5]));
            cache.PutImage(imgSrc[5], imgData[5]);
            NUnit.Framework.Assert.AreEqual(4, cache.Size());
            NUnit.Framework.Assert.AreEqual(imgData[5], cache.GetImage(imgSrc[5]));
            NUnit.Framework.Assert.AreEqual(imgData[3], cache.GetImage(imgSrc[3]));
            NUnit.Framework.Assert.AreEqual(imgData[1], cache.GetImage(imgSrc[1]));
            NUnit.Framework.Assert.AreEqual(imgData[2], cache.GetImage(imgSrc[2]));
            NUnit.Framework.Assert.IsNull(cache.GetImage(imgSrc[0]));
            NUnit.Framework.Assert.IsNull(cache.GetImage(imgSrc[4]));
        }