| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 { | 135 { |
| 136 if (attribute.isNull()) | 136 if (attribute.isNull()) |
| 137 return; | 137 return; |
| 138 | 138 |
| 139 if (attribute.is8Bit()) | 139 if (attribute.is8Bit()) |
| 140 parseImageCandidatesFromSrcsetAttribute<LChar>(attribute, attribute.char
acters8(), attribute.length(), imageCandidates); | 140 parseImageCandidatesFromSrcsetAttribute<LChar>(attribute, attribute.char
acters8(), attribute.length(), imageCandidates); |
| 141 else | 141 else |
| 142 parseImageCandidatesFromSrcsetAttribute<UChar>(attribute, attribute.char
acters16(), attribute.length(), imageCandidates); | 142 parseImageCandidatesFromSrcsetAttribute<UChar>(attribute, attribute.char
acters16(), attribute.length(), imageCandidates); |
| 143 } | 143 } |
| 144 | 144 |
| 145 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, int effect
iveSize, Vector<ImageCandidate>& imageCandidates) | 145 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, unsigned s
ourceSize, Vector<ImageCandidate>& imageCandidates) |
| 146 { | 146 { |
| 147 bool ignoreSrc = false; | 147 bool ignoreSrc = false; |
| 148 if (imageCandidates.isEmpty()) | 148 if (imageCandidates.isEmpty()) |
| 149 return ImageCandidate(); | 149 return ImageCandidate(); |
| 150 | 150 |
| 151 // http://picture.responsiveimages.org/#normalize-source-densities | 151 // http://picture.responsiveimages.org/#normalize-source-densities |
| 152 for (Vector<ImageCandidate>::iterator it = imageCandidates.begin(); it != im
ageCandidates.end(); ++it) { | 152 for (Vector<ImageCandidate>::iterator it = imageCandidates.begin(); it != im
ageCandidates.end(); ++it) { |
| 153 if (it->resourceWidth() > 0) { | 153 if (it->resourceWidth() > 0) { |
| 154 it->setScaleFactor((float)it->resourceWidth() / (float)effectiveSize
); | 154 it->setScaleFactor((float)it->resourceWidth() / (float)sourceSize); |
| 155 ignoreSrc = true; | 155 ignoreSrc = true; |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareBySc
aleFactor); | 159 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareBySc
aleFactor); |
| 160 | 160 |
| 161 unsigned i; | 161 unsigned i; |
| 162 for (i = 0; i < imageCandidates.size() - 1; ++i) { | 162 for (i = 0; i < imageCandidates.size() - 1; ++i) { |
| 163 if ((imageCandidates[i].scaleFactor() >= deviceScaleFactor) && (!ignoreS
rc || !imageCandidates[i].srcOrigin())) | 163 if ((imageCandidates[i].scaleFactor() >= deviceScaleFactor) && (!ignoreS
rc || !imageCandidates[i].srcOrigin())) |
| 164 break; | 164 break; |
| 165 } | 165 } |
| 166 | 166 |
| 167 float winningScaleFactor = imageCandidates[i].scaleFactor(); | 167 float winningScaleFactor = imageCandidates[i].scaleFactor(); |
| 168 unsigned winner = i; | 168 unsigned winner = i; |
| 169 // 16. If an entry b in candidates has the same associated ... pixel density
as an earlier entry a in candidates, | 169 // 16. If an entry b in candidates has the same associated ... pixel density
as an earlier entry a in candidates, |
| 170 // then remove entry b | 170 // then remove entry b |
| 171 while ((i > 0) && (imageCandidates[--i].scaleFactor() == winningScaleFactor)
) | 171 while ((i > 0) && (imageCandidates[--i].scaleFactor() == winningScaleFactor)
) |
| 172 winner = i; | 172 winner = i; |
| 173 | 173 |
| 174 return imageCandidates[winner]; | 174 return imageCandidates[winner]; |
| 175 } | 175 } |
| 176 | 176 |
| 177 ImageCandidate bestFitSourceForSrcsetAttribute(float deviceScaleFactor, int effe
ctiveSize, const String& srcsetAttribute) | 177 ImageCandidate bestFitSourceForSrcsetAttribute(float deviceScaleFactor, unsigned
sourceSize, const String& srcsetAttribute) |
| 178 { | 178 { |
| 179 Vector<ImageCandidate> imageCandidates; | 179 Vector<ImageCandidate> imageCandidates; |
| 180 | 180 |
| 181 parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates); | 181 parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates); |
| 182 | 182 |
| 183 return pickBestImageCandidate(deviceScaleFactor, effectiveSize, imageCandida
tes); | 183 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates
); |
| 184 } | 184 } |
| 185 | 185 |
| 186 ImageCandidate bestFitSourceForImageAttributes(float deviceScaleFactor, int effe
ctiveSize, const String& srcAttribute, const String& srcsetAttribute) | 186 ImageCandidate bestFitSourceForImageAttributes(float deviceScaleFactor, unsigned
sourceSize, const String& srcAttribute, const String& srcsetAttribute) |
| 187 { | 187 { |
| 188 DescriptorParsingResult defaultResult; | 188 DescriptorParsingResult defaultResult; |
| 189 defaultResult.scaleFactor = 1.0; | 189 defaultResult.scaleFactor = 1.0; |
| 190 | 190 |
| 191 if (srcsetAttribute.isNull()) { | 191 if (srcsetAttribute.isNull()) { |
| 192 if (srcAttribute.isNull()) | 192 if (srcAttribute.isNull()) |
| 193 return ImageCandidate(); | 193 return ImageCandidate(); |
| 194 return ImageCandidate(srcAttribute, 0, srcAttribute.length(), defaultRes
ult, ImageCandidate::SrcOrigin); | 194 return ImageCandidate(srcAttribute, 0, srcAttribute.length(), defaultRes
ult, ImageCandidate::SrcOrigin); |
| 195 } | 195 } |
| 196 | 196 |
| 197 Vector<ImageCandidate> imageCandidates; | 197 Vector<ImageCandidate> imageCandidates; |
| 198 | 198 |
| 199 parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates); | 199 parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates); |
| 200 | 200 |
| 201 if (!srcAttribute.isEmpty()) | 201 if (!srcAttribute.isEmpty()) |
| 202 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng
th(), defaultResult, ImageCandidate::SrcOrigin)); | 202 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng
th(), defaultResult, ImageCandidate::SrcOrigin)); |
| 203 | 203 |
| 204 return pickBestImageCandidate(deviceScaleFactor, effectiveSize, imageCandida
tes); | 204 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates
); |
| 205 } | 205 } |
| 206 | 206 |
| 207 String bestFitSourceForImageAttributes(float deviceScaleFactor, int effectiveSiz
e, const String& srcAttribute, ImageCandidate& srcsetImageCandidate) | 207 String bestFitSourceForImageAttributes(float deviceScaleFactor, unsigned sourceS
ize, const String& srcAttribute, ImageCandidate& srcsetImageCandidate) |
| 208 { | 208 { |
| 209 DescriptorParsingResult defaultResult; | 209 DescriptorParsingResult defaultResult; |
| 210 defaultResult.scaleFactor = 1.0; | 210 defaultResult.scaleFactor = 1.0; |
| 211 | 211 |
| 212 if (srcsetImageCandidate.isEmpty()) | 212 if (srcsetImageCandidate.isEmpty()) |
| 213 return srcAttribute; | 213 return srcAttribute; |
| 214 | 214 |
| 215 Vector<ImageCandidate> imageCandidates; | 215 Vector<ImageCandidate> imageCandidates; |
| 216 imageCandidates.append(srcsetImageCandidate); | 216 imageCandidates.append(srcsetImageCandidate); |
| 217 | 217 |
| 218 if (!srcAttribute.isEmpty()) | 218 if (!srcAttribute.isEmpty()) |
| 219 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng
th(), defaultResult, ImageCandidate::SrcOrigin)); | 219 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng
th(), defaultResult, ImageCandidate::SrcOrigin)); |
| 220 | 220 |
| 221 return pickBestImageCandidate(deviceScaleFactor, effectiveSize, imageCandida
tes).toString(); | 221 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates
).toString(); |
| 222 } | 222 } |
| 223 | 223 |
| 224 } | 224 } |
| OLD | NEW |