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

Side by Side Diff: Source/core/rendering/RenderVTTCue.cpp

Issue 882023003: Refactor RenderVTTCue/SnapToLinesLayouter (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Victor Carbune (victor@rosedu.org) 2 * Copyright (C) 2012 Victor Carbune (victor@rosedu.org)
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 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 , m_cueWritingDirection(writingDirection) 52 , m_cueWritingDirection(writingDirection)
53 , m_linePosition(linePosition) 53 , m_linePosition(linePosition)
54 { 54 {
55 } 55 }
56 56
57 void layout(); 57 void layout();
58 58
59 private: 59 private:
60 bool isOutside() const; 60 bool isOutside() const;
61 bool isOverlapping() const; 61 bool isOverlapping() const;
62 LayoutUnit computeInitialPositionAdjustment(LayoutUnit&) const;
63 void initializeFallbackPositioningState();
62 bool shouldSwitchDirection(InlineFlowBox*, LayoutUnit) const; 64 bool shouldSwitchDirection(InlineFlowBox*, LayoutUnit) const;
63 65
64 void moveBoxesByStep(LayoutUnit); 66 void moveBoxesBy(LayoutUnit distance)
65 bool switchDirection(bool&, LayoutUnit&); 67 {
68 if (m_cueWritingDirection == VTTCue::Horizontal)
69 m_cueBox.setY(m_cueBox.location().y() + distance);
70 else
71 m_cueBox.setX(m_cueBox.location().x() + distance);
72 }
66 73
67 bool findFirstLineBox(InlineFlowBox*&); 74 InlineFlowBox* findFirstLineBox() const;
68 bool initializeLayoutParameters(InlineFlowBox*, LayoutUnit&, LayoutUnit&);
69 void placeBoxInDefaultPosition(LayoutUnit, bool&);
70 75
71 LayoutPoint m_specifiedPosition; 76 LayoutPoint m_specifiedPosition;
72 RenderVTTCue& m_cueBox; 77 RenderVTTCue& m_cueBox;
73 VTTCue::WritingDirection m_cueWritingDirection; 78 VTTCue::WritingDirection m_cueWritingDirection;
74 float m_linePosition; 79 float m_linePosition;
75 }; 80 };
76 81
77 bool SnapToLinesLayouter::findFirstLineBox(InlineFlowBox*& firstLineBox) 82 InlineFlowBox* SnapToLinesLayouter::findFirstLineBox() const
78 { 83 {
79 if (m_cueBox.firstChild()->isRenderInline()) 84 ASSERT(m_cueBox.firstChild());
philipj_slow 2015/01/28 16:35:58 This was moved from elsewhere and now it's basical
fs 2015/01/28 17:03:05 Yes... maybe RenderVTTCue::layout would be a bette
80 firstLineBox = toRenderInline(m_cueBox.firstChild())->firstLineBox(); 85 if (!m_cueBox.firstChild()->isRenderInline())
81 else 86 return nullptr;
82 return false; 87 return toRenderInline(m_cueBox.firstChild())->firstLineBox();
83
84 return true;
85 } 88 }
86 89
87 bool SnapToLinesLayouter::initializeLayoutParameters(InlineFlowBox* firstLineBox , LayoutUnit& step, LayoutUnit& position) 90 LayoutUnit SnapToLinesLayouter::computeInitialPositionAdjustment(LayoutUnit& ste p) const
88 { 91 {
89 ASSERT(m_cueBox.firstChild());
90
91 // 4. Horizontal: Let step be the height of the first line box in boxes.
92 // Vertical: Let step be the width of the first line box in boxes.
93 step = m_cueWritingDirection == VTTCue::Horizontal ? firstLineBox->size().he ight() : firstLineBox->size().width();
94
95 // 5. If step is zero, then jump to the step labeled done positioning below.
96 if (!step)
97 return false;
98
99 // 6. Let line position be the text track cue computed line position. 92 // 6. Let line position be the text track cue computed line position.
100 // 7. Round line position to an integer by adding 0.5 and then flooring it. 93 // 7. Round line position to an integer by adding 0.5 and then flooring it.
101 LayoutUnit linePosition = floorf(m_linePosition + 0.5f); 94 LayoutUnit linePosition = floorf(m_linePosition + 0.5f);
102 95
103 // 8. Vertical Growing Left: Add one to line position then negate it. 96 // 8. Vertical Growing Left: Add one to line position then negate it.
104 if (m_cueWritingDirection == VTTCue::VerticalGrowingLeft) 97 if (m_cueWritingDirection == VTTCue::VerticalGrowingLeft)
105 linePosition = -(linePosition + 1); 98 linePosition = -(linePosition + 1);
106 99
107 // 9. Let position be the result of multiplying step and line position. 100 // 9. Let position be the result of multiplying step and line position.
108 position = step * linePosition; 101 LayoutUnit position = step * linePosition;
109 102
110 // 10. Vertical Growing Left: Decrease position by the width of the 103 // 10. Vertical Growing Left: Decrease position by the width of the
111 // bounding box of the boxes in boxes, then increase position by step. 104 // bounding box of the boxes in boxes, then increase position by step.
112 if (m_cueWritingDirection == VTTCue::VerticalGrowingLeft) { 105 if (m_cueWritingDirection == VTTCue::VerticalGrowingLeft) {
113 position -= m_cueBox.size().width(); 106 position -= m_cueBox.size().width();
philipj_slow 2015/01/28 16:35:58 Off-topic: shouldn't this be firstLineBox->size().
fs 2015/01/28 17:03:05 'height' (i.e. width in this case because it's ver
philipj_slow 2015/01/28 17:21:07 Ah, right.
114 position += step; 107 position += step;
115 } 108 }
116 109
117 // 11. If line position is less than zero... 110 // 11. If line position is less than zero...
118 if (linePosition < 0) { 111 if (linePosition < 0) {
119 RenderBlock* parentBlock = m_cueBox.containingBlock(); 112 RenderBlock* parentBlock = m_cueBox.containingBlock();
120 113
121 // Horizontal / Vertical: ... then increase position by the 114 // Horizontal / Vertical: ... then increase position by the
122 // height / width of the video's rendering area ... 115 // height / width of the video's rendering area ...
123 position += m_cueWritingDirection == VTTCue::Horizontal ? parentBlock->s ize().height() : parentBlock->size().width(); 116 position += m_cueWritingDirection == VTTCue::Horizontal ? parentBlock->s ize().height() : parentBlock->size().width();
124 117
125 // ... and negate step. 118 // ... and negate step.
126 step = -step; 119 step = -step;
127 } 120 }
128 return true; 121 return position;
129 } 122 }
130 123
131 void SnapToLinesLayouter::placeBoxInDefaultPosition(LayoutUnit position, bool& s witched) 124 void SnapToLinesLayouter::initializeFallbackPositioningState()
132 { 125 {
133 // 12. Move all boxes in boxes ...
134 if (m_cueWritingDirection == VTTCue::Horizontal) {
135 // Horizontal: ... down by the distance given by position
136 m_cueBox.setY(m_cueBox.location().y() + position);
137 } else {
138 // Vertical: ... right by the distance given by position
139 m_cueBox.setX(m_cueBox.location().x() + position);
140 }
141
142 // 13. Remember the position of all the boxes in boxes as their specified po sition. 126 // 13. Remember the position of all the boxes in boxes as their specified po sition.
143 m_specifiedPosition = m_cueBox.location(); 127 m_specifiedPosition = m_cueBox.location();
144 128
145 // 14. Let best position be null. It will hold a position for boxes, much li ke specified position in the previous step. 129 // 14. Let best position be null. It will hold a position for boxes, much li ke specified position in the previous step.
146 // 15. Let best position score be null. 130 // 15. Let best position score be null.
147
148 // 16. Let switched be false.
149 switched = false;
150 } 131 }
151 132
152 bool SnapToLinesLayouter::isOutside() const 133 bool SnapToLinesLayouter::isOutside() const
153 { 134 {
154 return !m_cueBox.containingBlock()->absoluteBoundingBoxRect().contains(m_cue Box.absoluteContentBox()); 135 return !m_cueBox.containingBlock()->absoluteBoundingBoxRect().contains(m_cue Box.absoluteContentBox());
155 } 136 }
156 137
157 bool SnapToLinesLayouter::isOverlapping() const 138 bool SnapToLinesLayouter::isOverlapping() const
158 { 139 {
159 for (RenderObject* box = m_cueBox.previousSibling(); box; box = box->previou sSibling()) { 140 for (RenderObject* box = m_cueBox.previousSibling(); box; box = box->previou sSibling()) {
(...skipping 26 matching lines...) Expand all
186 // if step is positive and the right edge of the first line box in boxes is 167 // if step is positive and the right edge of the first line box in boxes is
187 // now to the right of the right edge of the title area, jump to the step 168 // now to the right of the right edge of the title area, jump to the step
188 // labeled switch direction. 169 // labeled switch direction.
189 LayoutUnit parentWidth = m_cueBox.containingBlock()->size().width(); 170 LayoutUnit parentWidth = m_cueBox.containingBlock()->size().width();
190 if (m_cueWritingDirection != VTTCue::Horizontal && ((step < 0 && left < 0) | | (step > 0 && right > parentWidth))) 171 if (m_cueWritingDirection != VTTCue::Horizontal && ((step < 0 && left < 0) | | (step > 0 && right > parentWidth)))
191 return true; 172 return true;
192 173
193 return false; 174 return false;
194 } 175 }
195 176
196 void SnapToLinesLayouter::moveBoxesByStep(LayoutUnit step)
197 {
198 // 22. Horizontal: Move all the boxes in boxes down by the distance
199 // given by step. (If step is negative, then this will actually
200 // result in an upwards movement of the boxes in absolute terms.)
201 if (m_cueWritingDirection == VTTCue::Horizontal)
202 m_cueBox.setY(m_cueBox.location().y() + step);
203
204 // 22. Vertical: Move all the boxes in boxes right by the distance
205 // given by step. (If step is negative, then this will actually
206 // result in a leftwards movement of the boxes in absolute terms.)
207 else
208 m_cueBox.setX(m_cueBox.location().x() + step);
209 }
210
211 bool SnapToLinesLayouter::switchDirection(bool& switched, LayoutUnit& step)
212 {
213 // 24. Switch direction: If switched is true, then move all the boxes in
214 // boxes back to their best position, and jump to the step labeled done
215 // positioning below.
216
217 // 25. Otherwise, move all the boxes in boxes back to their specified
218 // position as determined in the earlier step.
219 m_cueBox.setLocation(m_specifiedPosition);
220
221 // XX. If switched is true, jump to the step labeled done
222 // positioning below.
223 if (switched)
224 return false;
225
226 // 26. Negate step.
227 step = -step;
228
229 // 27. Set switched to true.
230 switched = true;
231 return true;
232 }
233
234 void SnapToLinesLayouter::layout() 177 void SnapToLinesLayouter::layout()
235 { 178 {
236 // http://dev.w3.org/html5/webvtt/#dfn-apply-webvtt-cue-settings 179 // http://dev.w3.org/html5/webvtt/#dfn-apply-webvtt-cue-settings
237 // Step 13, "If cue's text track cue snap-to-lines flag is set". 180 // Step 13, "If cue's text track cue snap-to-lines flag is set".
238 181
239 InlineFlowBox* firstLineBox; 182 InlineFlowBox* firstLineBox = findFirstLineBox();
240 if (!findFirstLineBox(firstLineBox)) 183 if (!firstLineBox)
241 return; 184 return;
242 185
243 // Step 1 skipped. 186 // Steps 1-3 skipped.
187 // 4. Horizontal: Let step be the height of the first line box in boxes.
188 // Vertical: Let step be the width of the first line box in boxes.
189 LayoutUnit step = m_cueWritingDirection == VTTCue::Horizontal ? firstLineBox ->size().height() : firstLineBox->size().width();
244 190
245 LayoutUnit step; 191 // 5. If step is zero, then jump to the step labeled done positioning below.
246 LayoutUnit position; 192 if (!step)
247 if (!initializeLayoutParameters(firstLineBox, step, position))
248 return; 193 return;
249 194
250 bool switched; 195 // Steps 6-11.
251 placeBoxInDefaultPosition(position, switched); 196 LayoutUnit positionAdjustment = computeInitialPositionAdjustment(step);
197
198 // 12. Move all boxes in boxes ...
199 // Horizontal: ... down by the distance given by position
200 // Vertical: ... right by the distance given by position
201 moveBoxesBy(positionAdjustment);
202
203 // Steps 13-15.
204 initializeFallbackPositioningState();
philipj_slow 2015/01/28 16:35:58 I don't know, this is only called here and contain
fs 2015/01/28 17:03:05 This is sort of dependent on how we end up resolvi
philipj_slow 2015/01/28 17:21:07 I see. Let's move it around some more in that CL :
205
206 // 16. Let switched be false.
207 bool switched = false;
252 208
253 // Step 17 skipped. (margin == 0; title area == video area) 209 // Step 17 skipped. (margin == 0; title area == video area)
254 210
255 // 18. Step loop: If none of the boxes in boxes would overlap any of the 211 // 18. Step loop: If none of the boxes in boxes would overlap any of the
256 // boxes in output, and all of the boxes in output are entirely within the 212 // boxes in output, and all of the boxes in output are entirely within the
257 // title area box, then jump to the step labeled done positioning below. 213 // title area box, then jump to the step labeled done positioning below.
258 while (isOutside() || isOverlapping()) { 214 while (isOutside() || isOverlapping()) {
259 // 19. Let current position score be the percentage of the area of the 215 // 19. Let current position score be the percentage of the area of the
260 // bounding box of the boxes in boxes that is outside the title area 216 // bounding box of the boxes in boxes that is outside the title area
261 // box. 217 // box.
262 // 20. If best position is null (i.e. this is the first run through 218 // 20. If best position is null (i.e. this is the first run through
263 // this loop, switched is still false, the boxes in boxes are at their 219 // this loop, switched is still false, the boxes in boxes are at their
264 // specified position, and best position score is still null), or if 220 // specified position, and best position score is still null), or if
265 // current position score is a lower percentage than that in best 221 // current position score is a lower percentage than that in best
266 // position score, then remember the position of all the boxes in boxes 222 // position score, then remember the position of all the boxes in boxes
267 // as their best position, and set best position score to current 223 // as their best position, and set best position score to current
268 // position score. 224 // position score.
269 if (!shouldSwitchDirection(firstLineBox, step)) { 225 if (!shouldSwitchDirection(firstLineBox, step)) {
270 // 22. Move all the boxes in boxes ... 226 // 22. Horizontal: Move all the boxes in boxes down by the distance
271 moveBoxesByStep(step); 227 // given by step. (If step is negative, then this will actually
228 // result in an upwards movement of the boxes in absolute terms.)
229 // Vertical: Move all the boxes in boxes right by the distance
230 // given by step. (If step is negative, then this will actually
231 // result in a leftwards movement of the boxes in absolute terms.)
232 moveBoxesBy(step);
233
272 // 23. Jump back to the step labeled step loop. 234 // 23. Jump back to the step labeled step loop.
273 } else if (!switchDirection(switched, step)) { 235 continue;
236 }
237
238 // 24. Switch direction: If switched is true, then move all the boxes in
239 // boxes back to their best position, and jump to the step labeled done
240 // positioning below.
241
242 // 25. Otherwise, move all the boxes in boxes back to their specified
243 // position as determined in the earlier step.
244 m_cueBox.setLocation(m_specifiedPosition);
245
246 // XX. If switched is true, jump to the step labeled done
247 // positioning below.
248 if (switched)
274 break; 249 break;
275 } 250
251 // 26. Negate step.
252 step = -step;
253
254 // 27. Set switched to true.
255 switched = true;
276 256
277 // 28. Jump back to the step labeled step loop. 257 // 28. Jump back to the step labeled step loop.
278 } 258 }
279 } 259 }
280 260
281 void RenderVTTCue::repositionCueSnapToLinesNotSet() 261 void RenderVTTCue::repositionCueSnapToLinesNotSet()
282 { 262 {
283 // FIXME: Implement overlapping detection when snap-to-lines is not set. htt p://wkb.ug/84296 263 // FIXME: Implement overlapping detection when snap-to-lines is not set. htt p://wkb.ug/84296
284 264
285 // http://dev.w3.org/html5/webvtt/#dfn-apply-webvtt-cue-settings 265 // http://dev.w3.org/html5/webvtt/#dfn-apply-webvtt-cue-settings
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 SnapToLinesLayouter(*this, m_cue->getWritingDirection(), m_cue->calculat eComputedLinePosition()).layout(); 344 SnapToLinesLayouter(*this, m_cue->getWritingDirection(), m_cue->calculat eComputedLinePosition()).layout();
365 345
366 adjustForTopAndBottomMarginBorderAndPadding(); 346 adjustForTopAndBottomMarginBorderAndPadding();
367 } else { 347 } else {
368 repositionCueSnapToLinesNotSet(); 348 repositionCueSnapToLinesNotSet();
369 } 349 }
370 } 350 }
371 351
372 } // namespace blink 352 } // namespace blink
373 353
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698