| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2013 Google Inc. All rights reserved. | 3 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 | 349 |
| 350 currentDensity = imageCandidates[i].density(); | 350 currentDensity = imageCandidates[i].density(); |
| 351 geometricMean = sqrt(currentDensity * nextDensity); | 351 geometricMean = sqrt(currentDensity * nextDensity); |
| 352 if (deviceScaleFactor >= geometricMean) | 352 if (deviceScaleFactor >= geometricMean) |
| 353 return next; | 353 return next; |
| 354 break; | 354 break; |
| 355 } | 355 } |
| 356 return i; | 356 return i; |
| 357 } | 357 } |
| 358 | 358 |
| 359 static unsigned avoidDownloadIfHigherDensityResourceIsInCache(Vector<ImageCandid
ate>& imageCandidates, unsigned winner, Document* document) | 359 static unsigned avoidDownloadIfHigherDensityResourceIsInCache(Vector<ImageCandid
ate>& imageCandidates, unsigned winner, Document* document, bool ignoreSrc) |
| 360 { | 360 { |
| 361 if (!document) | 361 if (!document) |
| 362 return winner; | 362 return winner; |
| 363 for (unsigned i = imageCandidates.size() - 1; i > winner; --i) { | 363 for (unsigned i = imageCandidates.size() - 1; i > winner; --i) { |
| 364 if (ignoreSrc && imageCandidates[i].srcOrigin()) |
| 365 continue; |
| 364 KURL url = document->completeURL(stripLeadingAndTrailingHTMLSpaces(image
Candidates[i].url())); | 366 KURL url = document->completeURL(stripLeadingAndTrailingHTMLSpaces(image
Candidates[i].url())); |
| 365 if (memoryCache()->resourceForURL(url, document->fetcher()->getCacheIden
tifier())) | 367 if (memoryCache()->resourceForURL(url, document->fetcher()->getCacheIden
tifier())) |
| 366 return i; | 368 return i; |
| 367 } | 369 } |
| 368 return winner; | 370 return winner; |
| 369 } | 371 } |
| 370 | 372 |
| 371 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour
ceSize, Vector<ImageCandidate>& imageCandidates, Document* document = nullptr) | 373 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour
ceSize, Vector<ImageCandidate>& imageCandidates, Document* document = nullptr) |
| 372 { | 374 { |
| 373 const float defaultDensityValue = 1.0; | 375 const float defaultDensityValue = 1.0; |
| 374 bool ignoreSrc = false; | 376 bool ignoreSrc = false; |
| 375 if (imageCandidates.isEmpty()) | 377 if (imageCandidates.isEmpty()) |
| 376 return ImageCandidate(); | 378 return ImageCandidate(); |
| 377 | 379 |
| 378 // http://picture.responsiveimages.org/#normalize-source-densities | 380 // http://picture.responsiveimages.org/#normalize-source-densities |
| 379 for (ImageCandidate& image : imageCandidates) { | 381 for (ImageCandidate& image : imageCandidates) { |
| 380 if (image.resourceWidth() > 0) { | 382 if (image.resourceWidth() > 0) { |
| 381 image.setDensity((float)image.resourceWidth() / sourceSize); | 383 image.setDensity((float)image.resourceWidth() / sourceSize); |
| 382 ignoreSrc = true; | 384 ignoreSrc = true; |
| 383 } else if (image.density() < 0) { | 385 } else if (image.density() < 0) { |
| 384 image.setDensity(defaultDensityValue); | 386 image.setDensity(defaultDensityValue); |
| 385 } | 387 } |
| 386 } | 388 } |
| 387 | 389 |
| 388 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDe
nsity); | 390 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDe
nsity); |
| 389 | 391 |
| 390 unsigned winner = selectionLogic(imageCandidates, deviceScaleFactor, ignoreS
rc); | 392 unsigned winner = selectionLogic(imageCandidates, deviceScaleFactor, ignoreS
rc); |
| 391 ASSERT(winner < imageCandidates.size()); | 393 ASSERT(winner < imageCandidates.size()); |
| 392 winner = avoidDownloadIfHigherDensityResourceIsInCache(imageCandidates, winn
er, document); | 394 winner = avoidDownloadIfHigherDensityResourceIsInCache(imageCandidates, winn
er, document, ignoreSrc); |
| 393 | 395 |
| 394 float winningDensity = imageCandidates[winner].density(); | 396 float winningDensity = imageCandidates[winner].density(); |
| 395 // 16. If an entry b in candidates has the same associated ... pixel density
as an earlier entry a in candidates, | 397 // 16. If an entry b in candidates has the same associated ... pixel density
as an earlier entry a in candidates, |
| 396 // then remove entry b | 398 // then remove entry b |
| 397 while ((winner > 0) && (imageCandidates[winner - 1].density() == winningDens
ity)) | 399 while ((winner > 0) && (imageCandidates[winner - 1].density() == winningDens
ity)) |
| 398 --winner; | 400 --winner; |
| 399 | 401 |
| 400 return imageCandidates[winner]; | 402 return imageCandidates[winner]; |
| 401 } | 403 } |
| 402 | 404 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 Vector<ImageCandidate> imageCandidates; | 437 Vector<ImageCandidate> imageCandidates; |
| 436 imageCandidates.append(srcsetImageCandidate); | 438 imageCandidates.append(srcsetImageCandidate); |
| 437 | 439 |
| 438 if (!srcAttribute.isEmpty()) | 440 if (!srcAttribute.isEmpty()) |
| 439 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng
th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); | 441 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng
th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); |
| 440 | 442 |
| 441 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates
).toString(); | 443 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates
).toString(); |
| 442 } | 444 } |
| 443 | 445 |
| 444 } | 446 } |
| OLD | NEW |