/// <summary> /// Sort images by global style /// </summary> /// <remarks> /// <see cref="GetImage(IEnumerable{string})"/> is optimized for performace and therefore /// just loops over the images and selects the first best matching one based on given styles. /// The result is not predictable if the image list is not sorted and several alternatives /// match different global and explicit styles. /// Since we want to prioritize explicit styles first (<see cref="Image.WithStyles(string[])"/>), /// we need to make sure that we don't select an image with a given global style /// (<see cref="Context.SetGlobalStyle(string)"/>) but without an explicit match. /// The simplest approach without slowing down rendering by a sophisticated lookup is to /// sort images matching global styles last, so that <see cref="GetImage(IEnumerable{string})"/> /// would always select an alternative with an explicit rather than a global style. /// </remarks> static int StylePriorityComparison(Tuple <Image, string []> img1, Tuple <Image, string []> img2) { var hasGlobal1 = img1.Item2.Any(style => Context.HasGlobalStyle(style)); var hasGlobal2 = img2.Item2.Any(style => Context.HasGlobalStyle(style)); if (hasGlobal1 && hasGlobal2) { return(0); } if (hasGlobal1) { return(1); // sort global styles last } return(-1); // explicit styles go first }