Source/WebCore: https://bugs.webkit.org/show_bug.cgi?id=78122
authorhyatt@apple.com <hyatt@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 8 Feb 2012 17:34:22 +0000 (17:34 +0000)
committerhyatt@apple.com <hyatt@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 8 Feb 2012 17:34:22 +0000 (17:34 +0000)
Add support for the "contains" value for line-grid-snap. This value centers the line box in between the
text-top and text-bottom of the minimum number of grid lines that enclose the line box. This is useful for
centering headers in a line grid.

Reviewed by Adam Roben.

Added a new test in fast/line-grid.

* rendering/RootInlineBox.cpp:
(WebCore::RootInlineBox::lineGridSnapAdjustment):

LayoutTests: https://bugs.webkit.org/show_bug.cgi?id=78122

Add a new test that shows the "contains" value in action.

Reviewed by Adam Roben.

* fast/line-grid/line-grid-contains-value.html: Added.
* platform/mac/fast/line-grid/line-grid-contains-value-expected.png: Added.
* platform/mac/fast/line-grid/line-grid-contains-value-expected.txt: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@107099 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/line-grid/line-grid-contains-value.html [new file with mode: 0644]
LayoutTests/platform/mac/fast/line-grid/line-grid-contains-value-expected.png [new file with mode: 0644]
LayoutTests/platform/mac/fast/line-grid/line-grid-contains-value-expected.txt [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/rendering/RootInlineBox.cpp

index 3b19fee..5cb7f22 100644 (file)
@@ -1,3 +1,15 @@
+2012-02-08  David Hyatt  <hyatt@apple.com>
+
+        https://bugs.webkit.org/show_bug.cgi?id=78122
+        
+        Add a new test that shows the "contains" value in action.
+
+        Reviewed by Adam Roben.
+
+        * fast/line-grid/line-grid-contains-value.html: Added.
+        * platform/mac/fast/line-grid/line-grid-contains-value-expected.png: Added.
+        * platform/mac/fast/line-grid/line-grid-contains-value-expected.txt: Added.
+
 2012-02-08  Dan Bernstein  <mitz@apple.com>
 
         Moved a Mac-specific test to platform/mac.
diff --git a/LayoutTests/fast/line-grid/line-grid-contains-value.html b/LayoutTests/fast/line-grid/line-grid-contains-value.html
new file mode 100644 (file)
index 0000000..96f78f0
--- /dev/null
@@ -0,0 +1,36 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<style>
+.grid { -webkit-line-grid: simple; -webkit-line-grid-snap: baseline; 
+        font-size:36px; position:absolute;
+        padding:10px; }
+</style>
+</head>
+<body>
+<div style="left:10px; top:10px; width:600px" class="grid">
+<div style="-webkit-line-grid-snap:contain;font-size:48px">
+This header should be centered in the grid lines.
+</div>
+<div style="font-size:12px">
+This text should snap<br>
+to a 36px font-size grid.<br>
+There should be lots of spacing between these lines.
+
+</div>
+</div>
+
+<div style="left:10px; top:10px; z-index:-1; width:600px" class="grid">
+<div style="font-family:'Ahem'; color:lime">
+xxxxxxxxxxxxxxxxx<br>
+xxxxxxxxxxxxxxxxx<br>
+xxxxxxxxxxxxxxxxx<br>
+xxxxxxxxxxxxxxxxx<br>
+xxxxxxxxxxxxxxxxx<br>
+xxxxxxxxxxxxxxxxx<br>
+xxxxxxxxxxxxxxxxx<br>
+</div>
+</div>
+
+</body>
+</html>
diff --git a/LayoutTests/platform/mac/fast/line-grid/line-grid-contains-value-expected.png b/LayoutTests/platform/mac/fast/line-grid/line-grid-contains-value-expected.png
new file mode 100644 (file)
index 0000000..e8ecaaf
Binary files /dev/null and b/LayoutTests/platform/mac/fast/line-grid/line-grid-contains-value-expected.png differ
diff --git a/LayoutTests/platform/mac/fast/line-grid/line-grid-contains-value-expected.txt b/LayoutTests/platform/mac/fast/line-grid/line-grid-contains-value-expected.txt
new file mode 100644 (file)
index 0000000..7c341a2
--- /dev/null
@@ -0,0 +1,45 @@
+layer at (0,0) size 800x600
+  RenderView at (0,0) size 800x600
+layer at (0,0) size 800x600 layerType: background only
+layer at (10,10) size 620x305
+  RenderBlock (positioned) zI: -1 {DIV} at (10,10) size 620x305
+    RenderBlock {DIV} at (10,10) size 600x285 [color=#00FF00]
+      RenderText {#text} at (0,3) size 612x36
+        text run at (0,3) width 612: "xxxxxxxxxxxxxxxxx"
+      RenderBR {BR} at (0,0) size 0x0
+      RenderText {#text} at (0,44) size 612x36
+        text run at (0,44) width 612: "xxxxxxxxxxxxxxxxx"
+      RenderBR {BR} at (0,0) size 0x0
+      RenderText {#text} at (0,85) size 612x36
+        text run at (0,85) width 612: "xxxxxxxxxxxxxxxxx"
+      RenderBR {BR} at (0,0) size 0x0
+      RenderText {#text} at (0,126) size 612x36
+        text run at (0,126) width 612: "xxxxxxxxxxxxxxxxx"
+      RenderBR {BR} at (0,0) size 0x0
+      RenderText {#text} at (0,167) size 612x36
+        text run at (0,167) width 612: "xxxxxxxxxxxxxxxxx"
+      RenderBR {BR} at (0,0) size 0x0
+      RenderText {#text} at (0,208) size 612x36
+        text run at (0,208) width 612: "xxxxxxxxxxxxxxxxx"
+      RenderBR {BR} at (0,0) size 0x0
+      RenderText {#text} at (0,249) size 612x36
+        text run at (0,249) width 612: "xxxxxxxxxxxxxxxxx"
+      RenderBR {BR} at (0,0) size 0x0
+layer at (0,0) size 800x600 layerType: foreground only
+  RenderBlock {HTML} at (0,0) size 800x600
+    RenderBody {BODY} at (8,8) size 784x584
+layer at (10,10) size 620x301
+  RenderBlock (positioned) {DIV} at (10,10) size 620x301
+    RenderBlock {DIV} at (10,10) size 600x150
+      RenderText {#text} at (0,13) size 594x137
+        text run at (0,13) width 594: "This header should be centered"
+        text run at (0,95) width 310: "in the grid lines."
+    RenderBlock {DIV} at (10,160) size 600x131
+      RenderText {#text} at (0,35) size 101x14
+        text run at (0,35) width 101: "This text should snap"
+      RenderBR {BR} at (101,46) size 0x0
+      RenderText {#text} at (0,76) size 113x14
+        text run at (0,76) width 113: "to a 36px font-size grid."
+      RenderBR {BR} at (113,87) size 0x0
+      RenderText {#text} at (0,117) size 245x14
+        text run at (0,117) width 245: "There should be lots of spacing between these lines."
index 26f0a18..aba101c 100644 (file)
@@ -1,3 +1,18 @@
+2012-02-08  David Hyatt  <hyatt@apple.com>
+
+        https://bugs.webkit.org/show_bug.cgi?id=78122
+        
+        Add support for the "contains" value for line-grid-snap. This value centers the line box in between the
+        text-top and text-bottom of the minimum number of grid lines that enclose the line box. This is useful for
+        centering headers in a line grid.
+
+        Reviewed by Adam Roben.
+
+        Added a new test in fast/line-grid.
+
+        * rendering/RootInlineBox.cpp:
+        (WebCore::RootInlineBox::lineGridSnapAdjustment):
+
 2012-02-08  Cary Clark  <caryclark@google.com>
 
         [Skia Mac] Make misspelling underline dots unclipped
index 081b3b7..0bffabc 100644 (file)
@@ -330,7 +330,7 @@ LayoutUnit RootInlineBox::lineGridSnapAdjustment(LayoutUnit delta) const
 {
     // If our block doesn't have snapping turned on, do nothing.
     // FIXME: Implement bounds snapping.
-    if (block()->style()->lineGridSnap() != LineGridSnapBaseline)
+    if (block()->style()->lineGridSnap() == LineGridSnapNone)
         return 0;
 
     // Get the current line grid and offset.
@@ -352,33 +352,54 @@ LayoutUnit RootInlineBox::lineGridSnapAdjustment(LayoutUnit delta) const
     // as established by the line box.
     // FIXME: Need to handle crazy line-box-contain values that cause the root line box to not be considered. I assume
     // the grid should honor line-box-contain.
-    LayoutUnit baselineMultiple = lineGridBox->lineBottomWithLeading() - lineGridBox->lineTopWithLeading();
-    if (!baselineMultiple)
+    LayoutUnit gridLineHeight = lineGridBox->lineBottomWithLeading() - lineGridBox->lineTopWithLeading();
+    if (!gridLineHeight)
         return 0;
 
-    LayoutUnit lineGridAscent = lineGrid->style()->fontMetrics().ascent(baselineType());
-    LayoutUnit firstBaselinePosition = lineGridBlockOffset + lineGridAscent + lineGridBox->logicalTop();
-    LayoutUnit currentBaselinePosition = blockOffset + logicalTop() + delta + block()->style()->fontMetrics().ascent(baselineType());
+    LayoutUnit lineGridFontAscent = lineGrid->style()->fontMetrics().ascent(baselineType());
+    LayoutUnit lineGridFontHeight = lineGridBox->logicalHeight();
+    LayoutUnit firstTextTop = lineGridBlockOffset + lineGridBox->logicalTop();
+    LayoutUnit firstBaselinePosition = firstTextTop + lineGridFontAscent;
+
+    LayoutUnit currentTextTop = blockOffset + logicalTop() + delta;
+    LayoutUnit currentFontAscent = block()->style()->fontMetrics().ascent(baselineType());
+    LayoutUnit currentBaselinePosition = currentTextTop + currentFontAscent;
 
     // If we're paginated, see if we're on a page after the first one. If so, the grid resets on subsequent pages.
     // FIXME: If the grid is an ancestor of the pagination establisher, then this is incorrect.
     LayoutUnit pageLogicalTop = 0;
     if (layoutState->isPaginated() && layoutState->pageLogicalHeight()) {
         pageLogicalTop = block()->pageLogicalTopForOffset(logicalTop() + delta);
-        if (pageLogicalTop > lineGridBlockOffset + lineGridBox->logicalTop())
-            firstBaselinePosition = lineGridAscent + pageLogicalTop + lineGridBox->logicalTop() - lineGrid->borderBefore() - lineGrid->paddingBefore();
+        if (pageLogicalTop > firstTextTop)
+            firstTextTop = pageLogicalTop + lineGridBox->logicalTop() - lineGrid->borderBefore() - lineGrid->paddingBefore();
     }
-            
+
+    if (block()->style()->lineGridSnap() == LineGridSnapContain) {
+        // Compute the desired offset from the text-top of a grid line.
+        // Look at our height (logicalHeight()).
+        // Look at the total available height. It's going to be (textBottom - textTop) + (n-1)*(multiple with leading)
+        // where n is number of grid lines required to enclose us.
+        if (logicalHeight() <= lineGridFontHeight)
+            firstTextTop += (lineGridFontHeight - logicalHeight()) / 2;
+        else {
+            LayoutUnit numberOfLinesWithLeading = ceilf(static_cast<float>(logicalHeight() - lineGridFontHeight) / gridLineHeight);
+            LayoutUnit totalHeight = lineGridFontHeight + numberOfLinesWithLeading * gridLineHeight;
+            firstTextTop += (totalHeight - logicalHeight()) / 2;
+        }
+        firstBaselinePosition = firstTextTop + currentFontAscent;
+    } else
+        firstBaselinePosition = firstTextTop + lineGridFontAscent;
+
     // If we're above the first line, just push to the first line.
     if (currentBaselinePosition < firstBaselinePosition)
         return delta + firstBaselinePosition - currentBaselinePosition;
 
     // Otherwise we're in the middle of the grid somewhere. Just push to the next line.
     LayoutUnit baselineOffset = currentBaselinePosition - firstBaselinePosition;
-    LayoutUnit remainder = baselineOffset % baselineMultiple;
+    LayoutUnit remainder = baselineOffset % gridLineHeight;
     LayoutUnit result = delta;
     if (remainder)
-        result += baselineMultiple - remainder;
+        result += gridLineHeight - remainder;
 
     // If we aren't paginated we can return the result.
     if (!layoutState->isPaginated() || !layoutState->pageLogicalHeight() || result == delta)