public void AddImage(BackgroundImageClass image) { var imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl); Bitmap bitmap = null; using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes))) { using (var writer = new SpriteWriter(image.Width ?? originalBitmap.Width, image.Height ?? originalBitmap.Height)) { var width = image.Width ?? originalBitmap.Width; if (width > originalBitmap.Width) width = originalBitmap.Width; var height = image.Height ?? originalBitmap.Height; if (height > originalBitmap.Height) height = originalBitmap.Height; var x = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0; var y = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0; writer.WriteImage(originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat)); bitmap = writer.SpriteImage; if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height)) Size += writer.GetBytes("image/png").Length; else Size += imageBytes.Length; } } images.Add(bitmap); Width += bitmap.Width; if (Height < bitmap.Height) Height = bitmap.Height; }
private static void InheritMissingProperties(BackgroundImageClass derrivableClass, BackgroundImageClass imageClass) { if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasYOffset)) imageClass.YOffset = derrivableClass.YOffset; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasXOffset)) imageClass.XOffset = derrivableClass.XOffset; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasRepeat)) imageClass.Repeat = derrivableClass.Repeat; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasImage)) imageClass.ImageUrl = derrivableClass.ImageUrl; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasPaddingBottom)) imageClass.PaddingBottom = derrivableClass.PaddingBottom; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasPaddingTop)) imageClass.PaddingTop = derrivableClass.PaddingTop; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasPaddingLeft)) imageClass.PaddingLeft = derrivableClass.PaddingLeft; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasPaddingRight)) imageClass.PaddingRight = derrivableClass.PaddingRight; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasWidth)) imageClass.ExplicitWidth = derrivableClass.ExplicitWidth; if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasHeight)) imageClass.ExplicitHeight = derrivableClass.ExplicitHeight; }
public IEnumerable<BackgroundImageClass> ExtractImageUrls(string cssContent) { var finalUrls = new List<BackgroundImageClass>(); var draftUrls = new List<BackgroundImageClass>(); var classCounter = 0; foreach (var classMatch in classPattern.Matches(cssContent)) { var imageClass = new BackgroundImageClass(classMatch.ToString(), ++classCounter); if (!ShouldFlatten(imageClass)) continue; if (!IsComplete(imageClass) && ShouldFlatten(imageClass)) { var workList = new List<BackgroundImageClass>(); for (var n = draftUrls.Count - 1; n > -1; n--) { var selectors = draftUrls[n].Selector.Split(new [] {','}); var targetSelectors = imageClass.Selector.Split(new[] { ',' }); foreach (var selector in selectors) { if (targetSelectors .Any(targetSelector => cssSelectorAnalyzer.IsInScopeOfTarget(targetSelector.Trim(), selector.Trim()))) workList.Add(draftUrls[n]); } } workList.Sort(selectorComparer); foreach (var cls in workList.Where(cls => !IsComplete(imageClass))) InheritMissingProperties(cls, imageClass); } draftUrls.Add(imageClass); if (CanSprite(imageClass)) finalUrls.Add(imageClass); } return finalUrls; }
public virtual void Add(BackgroundImageClass image) { var imageKey = new ImageMetadata(image); if (spriteList.ContainsKey(imageKey)) return; SpritedImage spritedImage = null; try { spritedImage = SpriteContainer.AddImage(image); } catch (InvalidOperationException ex) { var message = string.Format("There were errors reducing {0}", image.ImageUrl); var wrappedException = new ApplicationException(message, ex); RRTracer.Trace(message); RRTracer.Trace(ex.ToString()); if (RequestReduceModule.CaptureErrorAction != null) RequestReduceModule.CaptureErrorAction(wrappedException); return; } spriteList.Add(imageKey, spritedImage); if (SpriteContainer.Size >= config.SpriteSizeLimit || (SpriteContainer.Colors >= config.SpriteColorLimit && !config.ImageQuantizationDisabled && !config.ImageOptimizationDisabled)) Flush(); }
public SpritedImage(int averageColor, BackgroundImageClass cssClass, Bitmap image) { AverageColor = averageColor; CssClass = cssClass; Image = image; Position = -1; }
public virtual void Add(BackgroundImageClass image) { var imageKey = new ImageMetadata(image); if (RRContainer.Current.GetAllInstances<IFilter>().Where(x => x is SpriteFilter).FirstOrDefault(y => y.IgnoreTarget(new SpriteFilterContext(image))) != null || spriteList.ContainsKey(imageKey)) return; SpritedImage spritedImage; try { spritedImage = SpriteContainer.AddImage(image); } catch (InvalidOperationException ex) { var message = string.Format("There were errors reducing {0}", image.ImageUrl); var wrappedException = new ApplicationException(message, ex); RRTracer.Trace(message); RRTracer.Trace(ex.ToString()); if (Registry.CaptureErrorAction != null) Registry.CaptureErrorAction(wrappedException); return; } spriteList.Add(imageKey, spritedImage); if (SpriteContainer.Size >= config.SpriteSizeLimit || (SpriteContainer.Colors >= config.SpriteColorLimit && !config.ImageQuantizationDisabled && !config.ImageOptimizationDisabled)) Flush(); }
public virtual Sprite this[BackgroundImageClass image] { get { var imageKey = new ImageMetadata(image); return spriteList.ContainsKey(imageKey) ? spriteList[imageKey] : null; } }
private static bool DerivableHasMissingProperty(BackgroundImageClass derrivableClass, BackgroundImageClass imageClass, PropertyCompletion property) { if ((imageClass.PropertyCompletion & property) != property && (derrivableClass.PropertyCompletion & property) == property) { imageClass.PropertyCompletion = imageClass.PropertyCompletion | property; return true; } return false; }
public void WillLeaveImageUrlNullIfBackgroundImageUrlIsDataUri() { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover { background-image: url(""data:image/png;base64,d09GRgABAAAAAGGwABAAAAAAmXgAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAABbAAAABsAAAAcX1JF+UdERUYAAAGIAAAAHgAAACABCwAET1MvMgAAAagAAABeAAAAYJ95kIljbWFwAAACCAAAAYIAAAHSJ4j+bmN2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B9aAw75b8GGfp4xxR1y0JOAoibVUQFE9HNazVgIOJ+BwgrVuhsP1LEacpUoBbjHkinW3C1nr5okAV+rqi2cRXkJb4Uxi/cTN6IzWB/hSiSygKdiXGaJAyrWQAOqqkFOrKMgACoxUgFXb3K4gaXhJhMEh8lqZGF25fOXqhQBXPLB1IpO+hePIIK9og9mtxUxWi3LBJ1Vt67lzU+3qg47uP7n8Lvl2kFO1B6Ym4GwswHOcGl2XmxgfzGgxNvjk5slz5ya1dpo6NDHz8JAsHb15dt/+2e3pQVmWlWx+enb/i5PTZJEcn578eBbpDPKVvbfNpQeH0NmpnXfvv3WuPycpZKs8mL/51v3P/xm5Ga6dsXlo/5UPnMec3ye2EAvEZ4nPEaUhhJ9NCD+z/nKpGaHmeB0g4QRGwqRQXuQmCdAfu4GVJlnrMNIdcNjGWgYc9jFl63PwvnsSgOsDdWIeDpQ23dUF4DfbOHNH1jQCpfj6PPrcx5lDgJTjswHuPMG1rc/fdQDwgrFh80gPZcN0hWmu1UNkf/WEUWMdUo26VvCHeCVT46BU9VKyen6Ftar3SPfZD0OY288L7cnu6DZXOOSRGvkA0yQY8fH8jtmhsa5YWBTFuJ7MZsmdYhMXYhp4+hWWlxklnAiToZ+8EFtnGJmh8Q37t84eUFq+wXGtUjo5WIilowkl1lw5Fk92RkSWOepxBX2K0JHpVdtAJfr9YrhFjc4aSfLY1GAhkVPktEdqqo/Go5""); }"; var testable = new BackgroundImageClass(css, 0); Assert.Null(testable.ImageUrl); }
public void WillLeaveImageUrlNullIfBackgroundImageUrlIsEmpty() { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover { background-image: url(""""); }"; var testable = new BackgroundImageClass(css, 0); Assert.Null(testable.ImageUrl); }
public void WillSetImageUrlFromBackgroundImageStyle() { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover { background-image: url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png""); }"; var testable = new BackgroundImageClass(css, 0); Assert.Equal("http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png", testable.ImageUrl); }
public void WillSetImageUrlFromShortcutStyleWhwenLastPropertyWithNoSemicolon() { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover { background: #fff url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png"") repeat }"; var testable = new BackgroundImageClass(css, 0); Assert.Equal("http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png", testable.ImageUrl); }
public virtual Sprite Add(BackgroundImageClass image) { var imageKey = new ImageMetadata(image); if (spriteList.ContainsKey(imageKey)) return spriteList[imageKey]; var currentPositionToReturn = SpriteContainer.Width; SpriteContainer.AddImage(image); var sprite = new Sprite(currentPositionToReturn, spriteIndex); if (SpriteContainer.Size >= config.SpriteSizeLimit) Flush(); spriteList.Add(imageKey, sprite); return sprite; }
public void WillSetSecondOffsetAsXOffsetWhenFirstOffsetIsBottom(string statedOffset, PositionMode expectedPositionMode, int expectedOffset) { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover {{ background: url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png"") no-repeat; background-position: bottom {0}; }}"; var testable = new BackgroundImageClass(string.Format(css, statedOffset), "http://server/content/style.css"); Assert.Equal(expectedPositionMode, testable.XOffset.PositionMode); Assert.Equal(expectedOffset, expectedPositionMode == PositionMode.Direction ? (int)testable.XOffset.Direction : testable.XOffset.Offset); }
public SpritedImage AddImage(BackgroundImageClass image) { var imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl); Bitmap bitmap; using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes))) { using (var writer = new SpriteWriter(image.Width ?? originalBitmap.Width, image.Height ?? originalBitmap.Height)) { var width = image.Width ?? originalBitmap.Width; if (width > originalBitmap.Width) width = originalBitmap.Width; var height = image.Height ?? originalBitmap.Height; if (height > originalBitmap.Height) height = originalBitmap.Height; var x = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0; var y = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0; try { using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat)) { writer.WriteImage(bm); } } catch (OutOfMemoryException) { throw new InvalidOperationException(string.Format("Unable to Add {0} to Sprite.", image.OriginalImageUrl)); } bitmap = writer.SpriteImage; if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height)) Size += writer.GetBytes("image/png").Length; else Size += imageBytes.Length; } } var avgColor = rrConfiguration.IsFullTrust ? GetColors(bitmap) : 0; var spritedImage = new SpritedImage(avgColor, image, bitmap); images.Add(spritedImage); Width += bitmap.Width + 1; if (Height < bitmap.Height) Height = bitmap.Height; return spritedImage; }
public IEnumerable<BackgroundImageClass> ExtractImageUrls(ref string cssContent, string cssUrl) { var urls = new List<BackgroundImageClass>(); foreach (var classMatch in classPattern.Matches(cssContent)) { var imageClass = new BackgroundImageClass(classMatch.ToString(), cssUrl); if(imageClass.OriginalImageUrl != null) cssContent = cssContent.Replace(classMatch.ToString(), imageClass.OriginalClassString); if (imageClass.ImageUrl != null) { if (imageClass.Width > 0 && imageClass.Repeat == RepeatStyle.NoRepeat && ((imageClass.XOffset.PositionMode == PositionMode.Direction && imageClass.XOffset.Direction == Direction.Left) || (imageClass.XOffset.PositionMode != PositionMode.Direction && imageClass.XOffset.Offset <= 0)) && ((imageClass.YOffset.PositionMode == PositionMode.Direction && imageClass.YOffset.Direction == Direction.Top) || (imageClass.YOffset.PositionMode == PositionMode.Percent && imageClass.YOffset.Offset == 0) || (imageClass.YOffset.PositionMode == PositionMode.Unit))) urls.Add(imageClass); } } return urls; }
public virtual void Add(BackgroundImageClass image) { var imageKey = new ImageMetadata(image); if (RRContainer.Current.GetAllInstances<IFilter>().Where(x => x is SpriteFilter).FirstOrDefault(y => y.IgnoreTarget(new SpriteFilterContext(image))) != null) return; if (SpriteList.Any(x => x.Key.Equals(imageKey))) { var originalImage = SpriteList.First(x => x.Key.Equals(imageKey)).Value; var clonedImage = new SpritedImage(originalImage.AverageColor, image, originalImage.Image) { Position = originalImage.Position, Url = originalImage.Url, Metadata = imageKey }; //SpriteContainer.AddImage(clonedImage); SpriteList.Add(new KeyValuePair<ImageMetadata, SpritedImage>(imageKey, clonedImage)); return; } SpritedImage spritedImage; var sprite = SpriteList.FirstOrDefault(x => x.Value.CssClass.ImageUrl == image.ImageUrl); if(sprite.Value != null) { image.IsSprite = true; sprite.Value.CssClass.IsSprite = true; } try { spritedImage = SpriteContainer.AddImage(image); spritedImage.Metadata = imageKey; } catch (Exception ex) { var message = string.Format("There were errors reducing {0}", image.ImageUrl); var wrappedException = new ApplicationException(message, ex); RRTracer.Trace(message); RRTracer.Trace(ex.ToString()); if (Registry.CaptureErrorAction != null) Registry.CaptureErrorAction(wrappedException); return; } SpriteList.Add(new KeyValuePair<ImageMetadata, SpritedImage>(imageKey, spritedImage)); if (SpriteContainer.Size >= config.SpriteSizeLimit || (SpriteContainer.Colors >= config.SpriteColorLimit && !config.ImageQuantizationDisabled && !config.ImageOptimizationDisabled)) Flush(); }
private static void InheritMissingProperties(BackgroundImageClass derrivableClass, BackgroundImageClass imageClass) { if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasYOffset)) { imageClass.YOffset = derrivableClass.YOffset; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasXOffset)) { imageClass.XOffset = derrivableClass.XOffset; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasRepeat)) { imageClass.Repeat = derrivableClass.Repeat; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasImage)) { imageClass.ImageUrl = derrivableClass.ImageUrl; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasPaddingBottom)) { imageClass.PaddingBottom = derrivableClass.PaddingBottom; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasPaddingTop)) { imageClass.PaddingTop = derrivableClass.PaddingTop; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasPaddingLeft)) { imageClass.PaddingLeft = derrivableClass.PaddingLeft; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasPaddingRight)) { imageClass.PaddingRight = derrivableClass.PaddingRight; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasWidth)) { imageClass.ExplicitWidth = derrivableClass.ExplicitWidth; } if (DerivableHasMissingProperty(derrivableClass, imageClass, PropertyCompletion.HasHeight)) { imageClass.ExplicitHeight = derrivableClass.ExplicitHeight; } }
private static bool DerivableHasMissingProperty(BackgroundImageClass derrivableClass, BackgroundImageClass imageClass, PropertyCompletion property) { if ((imageClass.PropertyCompletion & property) != property && (derrivableClass.PropertyCompletion & property) == property) { imageClass.PropertyCompletion = imageClass.PropertyCompletion | property; return(true); } return(false); }
public SpritedImage(int averageColor, BackgroundImageClass cssClass, Bitmap Image) { AverageColor = averageColor; CssClass = cssClass; this.Image = Image; }
public SpritedImage AddImage(BackgroundImageClass image) { var imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl); Bitmap bitmap; using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes))) { using (var writer = new SpriteWriter(image.Width ?? originalBitmap.Width, image.Height ?? originalBitmap.Height)) { var x = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0; var y = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0; var width = image.Width ?? originalBitmap.Width; if (width + x > originalBitmap.Width) { width = originalBitmap.Width - x; } var height = image.Height ?? originalBitmap.Height; if (height + y > originalBitmap.Height) { height = originalBitmap.Height - y; } var offsetX = 0; var offsetY = 0; if (image.XOffset.Direction == Direction.Right) { if (originalBitmap.Width > width) { x = originalBitmap.Width - width; } else { offsetX = (image.Width ?? 0) - originalBitmap.Width; } } else if (image.XOffset.Direction == Direction.Center && image.XOffset.PositionMode == PositionMode.Direction) { if (originalBitmap.Width > width) { x = (originalBitmap.Width - width) / 2; } else { offsetX = ((image.Width ?? 0) - originalBitmap.Width) / 2; } } if (image.YOffset.Direction == Direction.Bottom) { if (originalBitmap.Height > height) { y = originalBitmap.Height - height; } else { offsetY = (image.Height ?? 0) - originalBitmap.Height; } } else if (image.YOffset.Direction == Direction.Center && image.YOffset.PositionMode == PositionMode.Direction) { if (originalBitmap.Height > height) { y = (originalBitmap.Height - height) / 2; } else { offsetY = ((image.Height ?? 0) - originalBitmap.Height) / 2; } } try { using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat)) { writer.WriteImage(bm, offsetX, offsetY); } } catch (OutOfMemoryException) { throw new InvalidOperationException(string.Format("Unable to Add {0} to Sprite.", image.OriginalImageUrl)); } bitmap = writer.SpriteImage; if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height)) { Size += writer.GetBytes("image/png").Length; } else { Size += imageBytes.Length; } } } var avgColor = rrConfiguration.IsFullTrust ? GetColors(bitmap) : 0; var spritedImage = new SpritedImage(avgColor, image, bitmap); images.Add(spritedImage); Width += bitmap.Width + 1; if (Height < bitmap.Height) { Height = bitmap.Height; } return(spritedImage); }
public ImageMetadata(BackgroundImageClass image) : this() { Url = image.ImageUrl; Width = image.Width ?? 0; Height = image.Height ?? 0; XOffset = image.XOffset.Offset; YOffset = image.YOffset.Offset; }
public void WillSetYOffsetAsCenterWhenXOffsetIsDirectionAndYOffsetIsNotGiven(string xDirection) { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover {{ background: url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png"") no-repeat {0}; }}"; var testable = new BackgroundImageClass(string.Format(css, xDirection), "http://server/content/style.css"); Assert.Equal(PositionMode.Direction, testable.YOffset.PositionMode); Assert.Equal(Direction.Center, testable.YOffset.Direction); }
public void WillSetYRepeatIfYRepeatIsSpecifiedLast() { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover { background-image: url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png"") no-repeat; background-repeat: y-repeat; }"; var testable = new BackgroundImageClass(css, "http://server/content/style.css"); Assert.Equal(RepeatStyle.YRepeat, testable.Repeat); }
public void WillSetWidthFromWidthWhenLastPropertyWithNoSemicolon() { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover {{ background-image: url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png"") no-repeat; width: 50px}}"; var testable = new BackgroundImageClass(css, "http://server/content/style.css"); Assert.Equal(50, testable.Width); }
public void WillSetImageAbsoluteUrlFromBackgroundImageStyle(string css) { var testable = new BackgroundImageClass(css, "http://server/content/style.css"); Assert.Equal("http://server/content/subnav_on_technet.png", testable.ImageUrl); }
public SpritedImage AddImage(BackgroundImageClass image) { byte[] imageBytes; if (DownloadedImages.ContainsKey(image.ImageUrl) && image.IsSprite) { imageBytes = DownloadedImages[image.ImageUrl]; } else { imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl); if (image.IsSprite) { DownloadedImages.Add(image.ImageUrl, imageBytes); } } Bitmap bitmap; using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes))) { var width = image.Width ?? originalBitmap.Width; //cliped width of original image var height = image.Height ?? originalBitmap.Height; using (var writer = new SpriteWriter(width, height)) { var x = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0; //offset on original var y = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0; var imageWidth = width; //canvas width if (width + x > originalBitmap.Width) { width = originalBitmap.Width - x; } var imageHeight = height; if (height + y > originalBitmap.Height) { height = originalBitmap.Height - y; } var offsetX = 0; var offsetY = 0; if (image.XOffset.PositionMode == PositionMode.Direction) { switch (image.XOffset.Direction) { case Direction.Left: image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 0 }; break; case Direction.Center: image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 50 }; break; case Direction.Right: image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 100 }; break; } } if (image.YOffset.PositionMode == PositionMode.Direction) { switch (image.YOffset.Direction) { case Direction.Top: image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 0 }; break; case Direction.Center: image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 50 }; break; case Direction.Bottom: image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 100 }; break; } } if (image.XOffset.PositionMode == PositionMode.Percent) { if (originalBitmap.Width > imageWidth) { x = (int)Math.Round((originalBitmap.Width - imageWidth) * (image.XOffset.Offset / 100f), 0); } else { offsetX = (int)Math.Round((imageWidth - originalBitmap.Width) * (image.XOffset.Offset / 100f), 0); } } else if (image.XOffset.PositionMode == PositionMode.Unit && image.XOffset.Offset > 0) { offsetX = image.XOffset.Offset; if (originalBitmap.Width + offsetX > imageWidth) { width = imageWidth - offsetX; } } if (image.YOffset.PositionMode == PositionMode.Percent) { if (originalBitmap.Height > imageHeight) { y = (int)Math.Round((originalBitmap.Height - height) * (image.YOffset.Offset / 100f), 0); } else { offsetY = (int)Math.Round((imageHeight - originalBitmap.Height) * (image.YOffset.Offset / 100f), 0); } } try { using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat)) { writer.WriteImage(bm, offsetX, offsetY); } } catch (OutOfMemoryException) { throw new InvalidOperationException(string.Format("Unable to Add {0} to Sprite.", image.ImageUrl)); } bitmap = writer.SpriteImage; if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height)) { Size += writer.GetBytes("image/png").Length; } else { Size += imageBytes.Length; } } } var avgColor = rrConfiguration.IsFullTrust ? GetColors(bitmap) : 0; var spritedImage = new SpritedImage(avgColor, image, bitmap); Images.Add(spritedImage); Width += bitmap.Width + 1; if (Height < bitmap.Height) { Height = bitmap.Height; } return(spritedImage); }
public SpritedImage AddImage (BackgroundImageClass image) { byte[] imageBytes; if(DownloadedImages.ContainsKey(image.ImageUrl) && image.IsSprite) imageBytes = DownloadedImages[image.ImageUrl]; else { imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl); if(image.IsSprite) DownloadedImages.Add(image.ImageUrl, imageBytes); } Bitmap bitmap; using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes))) { var width = image.Width ?? originalBitmap.Width; //cliped width of original image var height = image.Height ?? originalBitmap.Height; using (var writer = new SpriteWriter(width, height)) { var x = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0; //offset on original var y = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0; var imageWidth = width; //canvas width if (width + x > originalBitmap.Width) width = originalBitmap.Width - x; var imageHeight = height; if (height + y > originalBitmap.Height) height = originalBitmap.Height - y; var offsetX = 0; var offsetY = 0; if (image.XOffset.PositionMode == PositionMode.Direction) { switch (image.XOffset.Direction) { case Direction.Left: image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 0}; break; case Direction.Center: image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 50}; break; case Direction.Right: image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 100 }; break; } } if (image.YOffset.PositionMode == PositionMode.Direction) switch (image.YOffset.Direction) { case Direction.Top: image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 0 }; break; case Direction.Center: image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 50 }; break; case Direction.Bottom: image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 100 }; break; } if (image.XOffset.PositionMode == PositionMode.Percent) { if (originalBitmap.Width > imageWidth) x = (int)Math.Round((originalBitmap.Width - imageWidth) * (image.XOffset.Offset / 100f), 0); else offsetX = (int)Math.Round((imageWidth - originalBitmap.Width) * (image.XOffset.Offset / 100f), 0); } else if (image.XOffset.PositionMode == PositionMode.Unit && image.XOffset.Offset > 0) { offsetX = image.XOffset.Offset; if (originalBitmap.Width + offsetX > imageWidth) width = imageWidth - offsetX; } if (image.YOffset.PositionMode == PositionMode.Percent) { if (originalBitmap.Height > imageHeight) y = (int)Math.Round((originalBitmap.Height - height) * (image.YOffset.Offset / 100f), 0); else offsetY = (int)Math.Round((imageHeight - originalBitmap.Height) * (image.YOffset.Offset / 100f), 0); } try { using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat)) { writer.WriteImage(bm, offsetX, offsetY); } } catch (OutOfMemoryException) { throw new InvalidOperationException(string.Format("Unable to Add {0} to Sprite.", image.ImageUrl)); } bitmap = writer.SpriteImage; if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height)) Size += writer.GetBytes("image/png").Length; else Size += imageBytes.Length; } } var avgColor = rrConfiguration.IsFullTrust ? GetColors(bitmap) : 0; var spritedImage = new SpritedImage(avgColor, image, bitmap); Images.Add(spritedImage); Width += bitmap.Width + 1; if (Height < bitmap.Height) Height = bitmap.Height; return spritedImage; }
public void WillSetImportantIfShorthandpositionIsImportant() { var css = @" img.icon { background: url(""http://galchameleon.redmond.corp.microsoft.com/contentservice/d046de6b-2d8e-43ec-9b37-9f5d010e51dd/icons_windows.png"") no-repeat 0 0 !important;width: 20px; }}"; var testable = new BackgroundImageClass(css, "http://server/content/style.css"); Assert.True(testable.Important); }
public void WillSetImportantIfLonghandpositionIsImportant() { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover {{ background: url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png"") no-repeat; background-position: 50 60 !important }}"; var testable = new BackgroundImageClass(css, "http://server/content/style.css"); Assert.True(testable.Important); }
public string InjectSprite(string originalCss, BackgroundImageClass image, Sprite sprite) { return originalCss.Replace(image.OriginalClassString, image.Render(sprite)); }
public void WillSetOriginalClassStringToPassedString() { var testable = new BackgroundImageClass("original string", "http://server/content/style.css"); Assert.Equal("original string", testable.OriginalClassString); }
public void WillSetOffsetsFromLastOffsets() { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover {{ background: url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png"") no-repeat 0px 0px; background-position: 10px -33px; }}"; var testable = new BackgroundImageClass(css, "http://server/content/style.css"); Assert.Equal(PositionMode.Unit, testable.XOffset.PositionMode); Assert.Equal(10, testable.XOffset.Offset); Assert.Equal(PositionMode.Unit, testable.YOffset.PositionMode); Assert.Equal(-33, testable.YOffset.Offset); }
public void WillSetWidthFromWidth(string statedWidth, int? expectedWidth) { var css = @" .LocalNavigation .TabOn,.LocalNavigation .TabOn:hover {{ background-image: url(""http://i3.social.microsoft.com/contentservice/1f22465a-498c-46f1-83d3-9dad00d8a950/subnav_on_technet.png"") no-repeat; width: {0}; }}"; var testable = new BackgroundImageClass(string.Format(css, statedWidth), "http://server/content/style.css"); Assert.Equal(expectedWidth, testable.Width); }