Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: Source/core/html/parser/HTMLSrcsetParser.cpp

Issue 667763004: Srcset resource selection use a geometric mean to determine resource. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Added test cases Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 { 314 {
315 if (attribute.isNull()) 315 if (attribute.isNull())
316 return; 316 return;
317 317
318 if (attribute.is8Bit()) 318 if (attribute.is8Bit())
319 parseImageCandidatesFromSrcsetAttribute<LChar>(attribute, attribute.char acters8(), attribute.length(), imageCandidates, document); 319 parseImageCandidatesFromSrcsetAttribute<LChar>(attribute, attribute.char acters8(), attribute.length(), imageCandidates, document);
320 else 320 else
321 parseImageCandidatesFromSrcsetAttribute<UChar>(attribute, attribute.char acters16(), attribute.length(), imageCandidates, document); 321 parseImageCandidatesFromSrcsetAttribute<UChar>(attribute, attribute.char acters16(), attribute.length(), imageCandidates, document);
322 } 322 }
323 323
324 static int selectionLogic(Vector<ImageCandidate>& imageCandidates, float deviceS caleFactor, bool ignoreSrc)
325 {
326 unsigned i = 0;
327 unsigned next;
328 float nextDensity;
329 float currentDensity;
Mike West 2014/10/22 13:41:39 Why do you define `next`, `nextDensity`, and `curr
330 float geometricMean;
331
332 for (; i < imageCandidates.size() - 1; ++i) {
333 next = i + 1;
334 if (ignoreSrc) {
335 if (imageCandidates[i].srcOrigin())
336 continue;
337 if (imageCandidates[next].srcOrigin()) {
338 ++next;
Mike West 2014/10/22 13:41:39 Should this be a loop? Or do you really only want
339 if (next >= imageCandidates.size())
340 break;
341 }
342 }
343
344 nextDensity = imageCandidates[next].density();
345 if (nextDensity < deviceScaleFactor)
346 continue;
347
348 currentDensity = imageCandidates[i].density();
349 geometricMean = sqrt(currentDensity * nextDensity);
350 if (deviceScaleFactor >= geometricMean)
351 return next;
352 break;
353 }
354 return i;
355 }
356
324 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour ceSize, Vector<ImageCandidate>& imageCandidates) 357 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour ceSize, Vector<ImageCandidate>& imageCandidates)
325 { 358 {
326 const float defaultDensityValue = 1.0; 359 const float defaultDensityValue = 1.0;
327 bool ignoreSrc = false; 360 bool ignoreSrc = false;
328 if (imageCandidates.isEmpty()) 361 if (imageCandidates.isEmpty())
329 return ImageCandidate(); 362 return ImageCandidate();
330 363
331 // http://picture.responsiveimages.org/#normalize-source-densities 364 // http://picture.responsiveimages.org/#normalize-source-densities
332 for (Vector<ImageCandidate>::iterator it = imageCandidates.begin(); it != im ageCandidates.end(); ++it) { 365 for (Vector<ImageCandidate>::iterator it = imageCandidates.begin(); it != im ageCandidates.end(); ++it) {
333 if (it->resourceWidth() > 0) { 366 if (it->resourceWidth() > 0) {
334 it->setDensity((float)it->resourceWidth() / sourceSize); 367 it->setDensity((float)it->resourceWidth() / sourceSize);
335 ignoreSrc = true; 368 ignoreSrc = true;
336 } else if (it->density() < 0) { 369 } else if (it->density() < 0) {
337 it->setDensity(defaultDensityValue); 370 it->setDensity(defaultDensityValue);
338 } 371 }
339 } 372 }
340 373
341 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDe nsity); 374 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDe nsity);
342 375
343 unsigned i; 376 unsigned i = selectionLogic(imageCandidates, deviceScaleFactor, ignoreSrc);
344 for (i = 0; i < imageCandidates.size() - 1; ++i) {
345 if ((imageCandidates[i].density() >= deviceScaleFactor) && (!ignoreSrc | | !imageCandidates[i].srcOrigin()))
346 break;
347 }
348 377
349 if (imageCandidates[i].srcOrigin() && ignoreSrc) {
350 ASSERT(i > 0);
351 --i;
352 }
353 float winningDensity = imageCandidates[i].density(); 378 float winningDensity = imageCandidates[i].density();
Mike West 2014/10/22 13:41:40 Perhaps ASSERT that `i` ended up in a valid range?
354 379
355 unsigned winner = i; 380 unsigned winner = i;
356 // 16. If an entry b in candidates has the same associated ... pixel density as an earlier entry a in candidates, 381 // 16. If an entry b in candidates has the same associated ... pixel density as an earlier entry a in candidates,
357 // then remove entry b 382 // then remove entry b
358 while ((i > 0) && (imageCandidates[--i].density() == winningDensity)) 383 while ((i > 0) && (imageCandidates[--i].density() == winningDensity))
359 winner = i; 384 winner = i;
360 385
361 return imageCandidates[winner]; 386 return imageCandidates[winner];
362 } 387 }
363 388
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 Vector<ImageCandidate> imageCandidates; 421 Vector<ImageCandidate> imageCandidates;
397 imageCandidates.append(srcsetImageCandidate); 422 imageCandidates.append(srcsetImageCandidate);
398 423
399 if (!srcAttribute.isEmpty()) 424 if (!srcAttribute.isEmpty())
400 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); 425 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin));
401 426
402 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates ).toString(); 427 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates ).toString();
403 } 428 }
404 429
405 } 430 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698