/// <summary> /// Returns the background size node with the correctly adjusted values for dpi and output units. /// </summary> /// <param name="assembledImage">The assembled image.</param> /// <param name="dpiFactor">The background dpi.</param> /// <returns>The background-size node.</returns> private IEnumerable <DeclarationNode> CreateBackgroundSizeNode(AssembledImage assembledImage, float dpiFactor) { var calcWidth = (float?)Math.Round((assembledImage.SpriteWidth ?? 0f) * this.outputUnitFactor / dpiFactor, 3); var calcHeight = (float?)Math.Round((assembledImage.SpriteHeight ?? 0f) * this.outputUnitFactor / dpiFactor, 3); var widthTermNode = new TermNode( calcWidth.UnaryOperator(), calcWidth.CssUnitValue(this.outputUnit), null, null, null, null); var heightTermNode = new TermNode( calcHeight.UnaryOperator(), calcHeight.CssUnitValue(this.outputUnit), null, null, null, null); var termWithOperatorNodes = new List <TermWithOperatorNode> { new TermWithOperatorNode(ImageAssembleConstants.SingleSpace, heightTermNode) }; var newBackgroundSizeNode = new DeclarationNode( ImageAssembleConstants.BackgroundSize, new ExprNode( widthTermNode, termWithOperatorNodes.ToSafeReadOnlyCollection(), null), null, null); return(new[] { // add a comment to help with sprite debugging CreateDebugDeclarationComment("-wg-background-size-params", " (sprite size: " + assembledImage.SpriteWidth + "px " + assembledImage.SpriteHeight + "px) (output unit factor: " + this.outputUnitFactor + ") (dpi: " + dpiFactor + ") (imageposition:" + assembledImage.ImagePosition + ")"), newBackgroundSizeNode }); }
/// <summary>Gets the assembled image information from the dictionary</summary> /// <param name="parsedImagePath">The parsed url</param> /// <param name="backgroundPosition">The background position node</param> /// <param name="assembledImage">The assembled image path</param> /// <returns>The assembled image object</returns> private bool TryGetAssembledImage(string parsedImagePath, BackgroundPosition backgroundPosition, out AssembledImage assembledImage) { if (this.availableSourceImages == null && string.IsNullOrWhiteSpace(this.cssPath)) { throw new BuildWorkflowException("Need either images or css path to be able to set a valid image file."); } assembledImage = null; if (this.inputImages == null) { return(false); } // TODO - Spec. issue: What are the supported path formats supported in CSS? // For optimization, should the relative paths be enforced? // Get the full path of parsed image file (convert from ../../ to absolute path) if (parsedImagePath.StartsWith("hash://", StringComparison.OrdinalIgnoreCase)) { parsedImagePath = parsedImagePath.Substring(7); } if (this.availableSourceImages != null) { if (!this.availableSourceImages.TryGetValue(parsedImagePath.NormalizeUrl(), out parsedImagePath)) { if (!string.IsNullOrWhiteSpace(this.missingImage)) { parsedImagePath = this.availableSourceImages.TryGetValue(this.missingImage); } } } else { parsedImagePath = parsedImagePath.MakeAbsoluteTo(this.cssPath); } var imagePosition = ImagePosition.Left; if (backgroundPosition != null) { imagePosition = backgroundPosition.GetImagePositionInVerticalSprite(); } // Try to locate the input image in the list assembledImage = this.inputImages.FirstOrDefault(inputImage => inputImage.ImagePosition == imagePosition && inputImage.OriginalFilePath.Equals(parsedImagePath, StringComparison.OrdinalIgnoreCase)); if (assembledImage != null && assembledImage.OutputFilePath != null) { assembledImage.RelativeOutputFilePath = !string.IsNullOrWhiteSpace(this.destinationDirectory) ? Path.Combine(this.prependToDestination, assembledImage.OutputFilePath.MakeRelativeToDirectory(this.destinationDirectory).Replace('\\', '/')) : assembledImage.OutputFilePath.MakeRelativeTo(this.cssPath); if (!string.IsNullOrWhiteSpace(assembledImage.RelativeOutputFilePath)) { assembledImage.RelativeOutputFilePath = assembledImage.RelativeOutputFilePath.Replace('\\', '/'); } return(true); } return(false); }
/// <summary> /// Sets the background-size priority node if the dpi does not equal 1, also replace an existing one if it exists. /// </summary> /// <param name="updatedDeclarations">The updated declarations.</param> /// <param name="backgroundSizeNode">The node containing the possible existing background size.</param> /// <param name="dpiFactor">The background dpi.</param> /// <param name="assembledImage">The assembled image.</param> private void SetBackgroundSize(List <DeclarationNode> updatedDeclarations, DeclarationNode backgroundSizeNode, float dpiFactor, AssembledImage assembledImage) { if (backgroundSizeNode != null) { updatedDeclarations.Remove(backgroundSizeNode); } // if the DPI factor is not 1.0, then we need to output a properly-scaled background-size property, // regardless of the the output units we want (if we don't specify an output unit, pixels is used). // but if we specify an output unit, we need to output the background-size property even if the DPI is 1.0, // in order to properly handle the browser default font-size change functionality. if ((dpiFactor != 1f || this.outputUnit != null) && assembledImage.SpriteHeight.HasValue && assembledImage.SpriteWidth.HasValue) { updatedDeclarations.AddRange(this.CreateBackgroundSizeNode(assembledImage, dpiFactor)); } }