File: MultiColumnFragmentainerGroup.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (601 lines) | stat: -rw-r--r-- 26,615 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/layout/MultiColumnFragmentainerGroup.h"

#include "core/layout/ColumnBalancer.h"
#include "core/layout/FragmentationContext.h"
#include "core/layout/LayoutMultiColumnSet.h"

namespace blink {

MultiColumnFragmentainerGroup::MultiColumnFragmentainerGroup(
    const LayoutMultiColumnSet& columnSet)
    : m_columnSet(columnSet) {}

bool MultiColumnFragmentainerGroup::isFirstGroup() const {
  return &m_columnSet.firstFragmentainerGroup() == this;
}

bool MultiColumnFragmentainerGroup::isLastGroup() const {
  return &m_columnSet.lastFragmentainerGroup() == this;
}

LayoutSize MultiColumnFragmentainerGroup::offsetFromColumnSet() const {
  LayoutSize offset(LayoutUnit(), logicalTop());
  if (!m_columnSet.flowThread()->isHorizontalWritingMode())
    return offset.transposedSize();
  return offset;
}

LayoutUnit
MultiColumnFragmentainerGroup::blockOffsetInEnclosingFragmentationContext()
    const {
  return logicalTop() + m_columnSet.logicalTopFromMulticolContentEdge() +
         m_columnSet.multiColumnFlowThread()
             ->blockOffsetInEnclosingFragmentationContext();
}

void MultiColumnFragmentainerGroup::resetColumnHeight() {
  m_maxColumnHeight = calculateMaxColumnHeight();

  LayoutMultiColumnFlowThread* flowThread = m_columnSet.multiColumnFlowThread();
  if (m_columnSet.heightIsAuto()) {
    FragmentationContext* enclosingFragmentationContext =
        flowThread->enclosingFragmentationContext();
    if (enclosingFragmentationContext &&
        enclosingFragmentationContext->isFragmentainerLogicalHeightKnown()) {
      // Set an initial height, based on the fragmentainer height in the outer
      // fragmentation context, in order to tell how much content this
      // MultiColumnFragmentainerGroup can hold, and when we need to append a
      // new one.
      m_columnHeight = m_maxColumnHeight;
      return;
    }
  }
  // If the multicol container has a definite height, use it as the column
  // height. This even applies when we are to balance the columns. We'll still
  // use the definite height as an initial height, and lay out once at that
  // column height. If it turns out that the content needs less than this
  // height, we have to balance and shrink the height and lay out the columns
  // over again.
  if (LayoutUnit logicalHeight = flowThread->columnHeightAvailable()) {
    setAndConstrainColumnHeight(heightAdjustedForRowOffset(logicalHeight));
  } else {
    m_columnHeight = LayoutUnit();
  }
}

bool MultiColumnFragmentainerGroup::recalculateColumnHeight(
    LayoutMultiColumnSet& columnSet) {
  LayoutUnit oldColumnHeight = m_columnHeight;

  m_maxColumnHeight = calculateMaxColumnHeight();

  // Only the last row may have auto height, and thus be balanced. There are no
  // good reasons to balance the preceding rows, and that could potentially lead
  // to an insane number of layout passes as well.
  if (isLastGroup() && columnSet.heightIsAuto()) {
    LayoutUnit newColumnHeight;
    if (!columnSet.isInitialHeightCalculated()) {
      // Initial balancing: Start with the lowest imaginable column height. Also
      // calculate the height of the tallest piece of unbreakable content.
      // Columns should never get any shorter than that (unless constrained by
      // max-height). Propagate this to our containing column set, in case there
      // is an outer multicol container that also needs to balance. After having
      // calculated the initial column height, the multicol container needs
      // another layout pass with the column height that we just calculated.
      InitialColumnHeightFinder initialHeightFinder(
          columnSet, logicalTopInFlowThread(), logicalBottomInFlowThread());
      columnSet.propagateTallestUnbreakableLogicalHeight(
          initialHeightFinder.tallestUnbreakableLogicalHeight());
      newColumnHeight = initialHeightFinder.initialMinimalBalancedHeight();
    } else {
      // Rebalancing: After having laid out again, we'll need to rebalance if
      // the height wasn't enough and we're allowed to stretch it, and then
      // re-lay out.  There are further details on the column balancing
      // machinery in ColumnBalancer and its derivates.
      newColumnHeight = rebalanceColumnHeightIfNeeded();
    }
    setAndConstrainColumnHeight(newColumnHeight);
  } else {
    // The position of the column set may have changed, in which case height
    // available for columns may have changed as well.
    setAndConstrainColumnHeight(m_columnHeight);
  }

  if (m_columnHeight == oldColumnHeight)
    return false;  // No change. We're done.

  return true;  // Need another pass.
}

LayoutSize MultiColumnFragmentainerGroup::flowThreadTranslationAtOffset(
    LayoutUnit offsetInFlowThread,
    LayoutBox::PageBoundaryRule rule,
    CoordinateSpaceConversion mode) const {
  LayoutMultiColumnFlowThread* flowThread = m_columnSet.multiColumnFlowThread();

  // A column out of range doesn't have a flow thread portion, so we need to
  // clamp to make sure that we stay within the actual columns. This means that
  // content in the overflow area will be mapped to the last actual column,
  // instead of being mapped to an imaginary column further ahead.
  unsigned columnIndex = offsetInFlowThread >= logicalBottomInFlowThread()
                             ? actualColumnCount() - 1
                             : columnIndexAtOffset(offsetInFlowThread, rule);

  LayoutRect portionRect(flowThreadPortionRectAt(columnIndex));
  flowThread->flipForWritingMode(portionRect);
  portionRect.moveBy(flowThread->physicalLocation());

  LayoutRect columnRect(columnRectAt(columnIndex));
  columnRect.move(offsetFromColumnSet());
  m_columnSet.flipForWritingMode(columnRect);
  columnRect.moveBy(m_columnSet.physicalLocation());

  LayoutSize translationRelativeToFlowThread =
      columnRect.location() - portionRect.location();
  if (mode == CoordinateSpaceConversion::Containing)
    return translationRelativeToFlowThread;

  LayoutSize enclosingTranslation;
  if (LayoutMultiColumnFlowThread* enclosingFlowThread =
          flowThread->enclosingFlowThread()) {
    const MultiColumnFragmentainerGroup& firstRow =
        flowThread->firstMultiColumnSet()->firstFragmentainerGroup();
    // Translation that would map points in the coordinate space of the
    // outermost flow thread to visual points in the first column in the first
    // fragmentainer group (row) in our multicol container.
    LayoutSize enclosingTranslationOrigin =
        enclosingFlowThread->flowThreadTranslationAtOffset(
            firstRow.blockOffsetInEnclosingFragmentationContext(),
            LayoutBox::AssociateWithLatterPage, mode);

    // Translation that would map points in the coordinate space of the
    // outermost flow thread to visual points in the first column in this
    // fragmentainer group.
    enclosingTranslation = enclosingFlowThread->flowThreadTranslationAtOffset(
        blockOffsetInEnclosingFragmentationContext(),
        LayoutBox::AssociateWithLatterPage, mode);

    // What we ultimately return from this method is a translation that maps
    // points in the coordinate space of our flow thread to a visual point in a
    // certain column in this fragmentainer group. We had to go all the way up
    // to the outermost flow thread, since this fragmentainer group may be in a
    // different outer column than the first outer column that this multicol
    // container lives in. It's the visual distance between the first
    // fragmentainer group and this fragmentainer group that we need to add to
    // the translation.
    enclosingTranslation -= enclosingTranslationOrigin;
  }

  return enclosingTranslation + translationRelativeToFlowThread;
}

LayoutUnit MultiColumnFragmentainerGroup::columnLogicalTopForOffset(
    LayoutUnit offsetInFlowThread) const {
  unsigned columnIndex = columnIndexAtOffset(
      offsetInFlowThread, LayoutBox::AssociateWithLatterPage);
  return logicalTopInFlowThreadAt(columnIndex);
}

LayoutPoint MultiColumnFragmentainerGroup::visualPointToFlowThreadPoint(
    const LayoutPoint& visualPoint,
    SnapToColumnPolicy snap) const {
  unsigned columnIndex = columnIndexAtVisualPoint(visualPoint);
  LayoutRect columnRect = columnRectAt(columnIndex);
  LayoutPoint localPoint(visualPoint);
  localPoint.moveBy(-columnRect.location());
  if (!m_columnSet.isHorizontalWritingMode()) {
    if (snap == SnapToColumn) {
      LayoutUnit columnStart = m_columnSet.style()->isLeftToRightDirection()
                                   ? LayoutUnit()
                                   : columnRect.height();
      if (localPoint.x() < 0)
        localPoint = LayoutPoint(LayoutUnit(), columnStart);
      else if (localPoint.x() > logicalHeight())
        localPoint = LayoutPoint(logicalHeight(), columnStart);
    }
    return LayoutPoint(localPoint.x() + logicalTopInFlowThreadAt(columnIndex),
                       localPoint.y());
  }
  if (snap == SnapToColumn) {
    LayoutUnit columnStart = m_columnSet.style()->isLeftToRightDirection()
                                 ? LayoutUnit()
                                 : columnRect.width();
    if (localPoint.y() < 0)
      localPoint = LayoutPoint(columnStart, LayoutUnit());
    else if (localPoint.y() > logicalHeight())
      localPoint = LayoutPoint(columnStart, logicalHeight());
  }
  return LayoutPoint(localPoint.x(),
                     localPoint.y() + logicalTopInFlowThreadAt(columnIndex));
}

LayoutRect MultiColumnFragmentainerGroup::fragmentsBoundingBox(
    const LayoutRect& boundingBoxInFlowThread) const {
  // Find the start and end column intersected by the bounding box.
  LayoutRect flippedBoundingBoxInFlowThread(boundingBoxInFlowThread);
  LayoutFlowThread* flowThread = m_columnSet.flowThread();
  flowThread->flipForWritingMode(flippedBoundingBoxInFlowThread);
  bool isHorizontalWritingMode = m_columnSet.isHorizontalWritingMode();
  LayoutUnit boundingBoxLogicalTop = isHorizontalWritingMode
                                         ? flippedBoundingBoxInFlowThread.y()
                                         : flippedBoundingBoxInFlowThread.x();
  LayoutUnit boundingBoxLogicalBottom =
      isHorizontalWritingMode ? flippedBoundingBoxInFlowThread.maxY()
                              : flippedBoundingBoxInFlowThread.maxX();
  if (boundingBoxLogicalBottom <= logicalTopInFlowThread() ||
      boundingBoxLogicalTop >= logicalBottomInFlowThread()) {
    // The bounding box doesn't intersect this fragmentainer group.
    return LayoutRect();
  }
  unsigned startColumn;
  unsigned endColumn;
  columnIntervalForBlockRangeInFlowThread(
      boundingBoxLogicalTop, boundingBoxLogicalBottom, startColumn, endColumn);

  LayoutRect startColumnFlowThreadOverflowPortion =
      flowThreadPortionOverflowRectAt(startColumn);
  flowThread->flipForWritingMode(startColumnFlowThreadOverflowPortion);
  LayoutRect startColumnRect(boundingBoxInFlowThread);
  startColumnRect.intersect(startColumnFlowThreadOverflowPortion);
  startColumnRect.move(flowThreadTranslationAtOffset(
      logicalTopInFlowThreadAt(startColumn), LayoutBox::AssociateWithLatterPage,
      CoordinateSpaceConversion::Containing));
  if (startColumn == endColumn)
    return startColumnRect;  // It all takes place in one column. We're done.

  LayoutRect endColumnFlowThreadOverflowPortion =
      flowThreadPortionOverflowRectAt(endColumn);
  flowThread->flipForWritingMode(endColumnFlowThreadOverflowPortion);
  LayoutRect endColumnRect(boundingBoxInFlowThread);
  endColumnRect.intersect(endColumnFlowThreadOverflowPortion);
  endColumnRect.move(flowThreadTranslationAtOffset(
      logicalTopInFlowThreadAt(endColumn), LayoutBox::AssociateWithLatterPage,
      CoordinateSpaceConversion::Containing));
  return unionRect(startColumnRect, endColumnRect);
}

LayoutRect MultiColumnFragmentainerGroup::calculateOverflow() const {
  // Note that we just return the bounding rectangle of the column boxes here.
  // We currently don't examine overflow caused by the actual content that ends
  // up in each column.
  LayoutRect overflowRect;
  if (unsigned columnCount = actualColumnCount()) {
    overflowRect = columnRectAt(0);
    if (columnCount > 1)
      overflowRect.uniteEvenIfEmpty(columnRectAt(columnCount - 1));
  }
  return overflowRect;
}

unsigned MultiColumnFragmentainerGroup::actualColumnCount() const {
  // We must always return a value of 1 or greater. Column count = 0 is a
  // meaningless situation, and will confuse and cause problems in other parts
  // of the code.
  if (!m_columnHeight)
    return 1;

  // Our flow thread portion determines our column count. We have as many
  // columns as needed to fit all the content.
  LayoutUnit flowThreadPortionHeight = logicalHeightInFlowThread();
  if (!flowThreadPortionHeight)
    return 1;

  unsigned count = (flowThreadPortionHeight / m_columnHeight).floor();
  // flowThreadPortionHeight may be saturated, so detect the remainder manually.
  if (count * m_columnHeight < flowThreadPortionHeight)
    count++;
  ASSERT(count >= 1);
  return count;
}

LayoutUnit MultiColumnFragmentainerGroup::heightAdjustedForRowOffset(
    LayoutUnit height) const {
  // Let's avoid zero height, as that would cause an infinite amount of columns
  // to be created.
  return std::max(
      height - logicalTop() - m_columnSet.logicalTopFromMulticolContentEdge(),
      LayoutUnit(1));
}

LayoutUnit MultiColumnFragmentainerGroup::calculateMaxColumnHeight() const {
  LayoutMultiColumnFlowThread* flowThread = m_columnSet.multiColumnFlowThread();
  LayoutUnit maxColumnHeight = flowThread->maxColumnLogicalHeight();
  LayoutUnit maxHeight = heightAdjustedForRowOffset(maxColumnHeight);
  if (FragmentationContext* enclosingFragmentationContext =
          flowThread->enclosingFragmentationContext()) {
    if (enclosingFragmentationContext->isFragmentainerLogicalHeightKnown()) {
      // We're nested inside another fragmentation context whose fragmentainer
      // heights are known. This constrains the max height.
      LayoutUnit remainingOuterLogicalHeight =
          enclosingFragmentationContext->remainingLogicalHeightAt(
              blockOffsetInEnclosingFragmentationContext());
      ASSERT(remainingOuterLogicalHeight > 0);
      if (maxHeight > remainingOuterLogicalHeight)
        maxHeight = remainingOuterLogicalHeight;
    }
  }
  return maxHeight;
}

void MultiColumnFragmentainerGroup::setAndConstrainColumnHeight(
    LayoutUnit newHeight) {
  m_columnHeight = newHeight;
  if (m_columnHeight > m_maxColumnHeight)
    m_columnHeight = m_maxColumnHeight;
}

LayoutUnit MultiColumnFragmentainerGroup::rebalanceColumnHeightIfNeeded()
    const {
  if (actualColumnCount() <= m_columnSet.usedColumnCount()) {
    // With the current column height, the content fits without creating
    // overflowing columns. We're done.
    return m_columnHeight;
  }

  if (m_columnHeight >= m_maxColumnHeight) {
    // We cannot stretch any further. We'll just have to live with the
    // overflowing columns. This typically happens if the max column height is
    // less than the height of the tallest piece of unbreakable content (e.g.
    // lines).
    return m_columnHeight;
  }

  MinimumSpaceShortageFinder shortageFinder(
      columnSet(), logicalTopInFlowThread(), logicalBottomInFlowThread());

  if (shortageFinder.forcedBreaksCount() + 1 >= m_columnSet.usedColumnCount()) {
    // Too many forced breaks to allow any implicit breaks. Initial balancing
    // should already have set a good height. There's nothing more we should do.
    return m_columnHeight;
  }

  // If the initial guessed column height wasn't enough, stretch it now. Stretch
  // by the lowest amount of space.
  LayoutUnit minSpaceShortage = shortageFinder.minimumSpaceShortage();

  ASSERT(minSpaceShortage > 0);  // We should never _shrink_ the height!
  ASSERT(minSpaceShortage !=
         LayoutUnit::max());  // If this happens, we probably have a bug.
  if (minSpaceShortage == LayoutUnit::max())
    return m_columnHeight;  // So bail out rather than looping infinitely.

  return m_columnHeight + minSpaceShortage;
}

LayoutRect MultiColumnFragmentainerGroup::columnRectAt(
    unsigned columnIndex) const {
  LayoutUnit columnLogicalWidth = m_columnSet.pageLogicalWidth();
  LayoutUnit columnLogicalHeight = m_columnHeight;
  LayoutUnit columnLogicalTop;
  LayoutUnit columnLogicalLeft;
  LayoutUnit columnGap = m_columnSet.columnGap();
  LayoutUnit portionOutsideFlowThread =
      logicalTopInFlowThread() + (columnIndex + 1) * columnLogicalHeight -
      logicalBottomInFlowThread();
  if (portionOutsideFlowThread > 0) {
    // The last column may not be using all available space.
    ASSERT(columnIndex + 1 == actualColumnCount());
    columnLogicalHeight -= portionOutsideFlowThread;
    ASSERT(columnLogicalHeight >= 0);
  }

  if (m_columnSet.multiColumnFlowThread()->progressionIsInline()) {
    if (m_columnSet.style()->isLeftToRightDirection())
      columnLogicalLeft += columnIndex * (columnLogicalWidth + columnGap);
    else
      columnLogicalLeft += m_columnSet.contentLogicalWidth() -
                           columnLogicalWidth -
                           columnIndex * (columnLogicalWidth + columnGap);
  } else {
    columnLogicalTop += columnIndex * (m_columnHeight + columnGap);
  }

  LayoutRect columnRect(columnLogicalLeft, columnLogicalTop, columnLogicalWidth,
                        columnLogicalHeight);
  if (!m_columnSet.isHorizontalWritingMode())
    return columnRect.transposedRect();
  return columnRect;
}

LayoutRect MultiColumnFragmentainerGroup::flowThreadPortionRectAt(
    unsigned columnIndex) const {
  LayoutUnit logicalTop = logicalTopInFlowThreadAt(columnIndex);
  LayoutUnit logicalBottom = logicalTop + m_columnHeight;
  if (logicalBottom > logicalBottomInFlowThread()) {
    // The last column may not be using all available space.
    ASSERT(columnIndex + 1 == actualColumnCount());
    logicalBottom = logicalBottomInFlowThread();
    ASSERT(logicalBottom >= logicalTop);
  }
  LayoutUnit portionLogicalHeight = logicalBottom - logicalTop;
  if (m_columnSet.isHorizontalWritingMode())
    return LayoutRect(LayoutUnit(), logicalTop, m_columnSet.pageLogicalWidth(),
                      portionLogicalHeight);
  return LayoutRect(logicalTop, LayoutUnit(), portionLogicalHeight,
                    m_columnSet.pageLogicalWidth());
}

LayoutRect MultiColumnFragmentainerGroup::flowThreadPortionOverflowRectAt(
    unsigned columnIndex) const {
  // This function determines the portion of the flow thread that paints for the
  // column. Along the inline axis, columns are unclipped at outside edges
  // (i.e., the first and last column in the set), and they clip to half the
  // column gap along interior edges.
  //
  // In the block direction, we will not clip overflow out of the top of the
  // first column, or out of the bottom of the last column. This applies only to
  // the true first column and last column across all column sets.
  //
  // FIXME: Eventually we will know overflow on a per-column basis, but we can't
  // do this until we have a painting mode that understands not to paint
  // contents from a previous column in the overflow area of a following column.
  bool isFirstColumnInRow = !columnIndex;
  bool isLastColumnInRow = columnIndex == actualColumnCount() - 1;
  bool isLTR = m_columnSet.style()->isLeftToRightDirection();
  bool isLeftmostColumn = isLTR ? isFirstColumnInRow : isLastColumnInRow;
  bool isRightmostColumn = isLTR ? isLastColumnInRow : isFirstColumnInRow;

  LayoutRect portionRect = flowThreadPortionRectAt(columnIndex);
  bool isFirstColumnInMulticolContainer =
      isFirstColumnInRow && this == &m_columnSet.firstFragmentainerGroup() &&
      !m_columnSet.previousSiblingMultiColumnSet();
  bool isLastColumnInMulticolContainer =
      isLastColumnInRow && this == &m_columnSet.lastFragmentainerGroup() &&
      !m_columnSet.nextSiblingMultiColumnSet();
  // Calculate the overflow rectangle, based on the flow thread's, clipped at
  // column logical top/bottom unless it's the first/last column.
  LayoutRect overflowRect = m_columnSet.overflowRectForFlowThreadPortion(
      portionRect, isFirstColumnInMulticolContainer,
      isLastColumnInMulticolContainer);

  // Avoid overflowing into neighboring columns, by clipping in the middle of
  // adjacent column gaps. Also make sure that we avoid rounding errors.
  LayoutUnit columnGap = m_columnSet.columnGap();
  if (m_columnSet.isHorizontalWritingMode()) {
    if (!isLeftmostColumn)
      overflowRect.shiftXEdgeTo(portionRect.x() - columnGap / 2);
    if (!isRightmostColumn)
      overflowRect.shiftMaxXEdgeTo(portionRect.maxX() + columnGap -
                                   columnGap / 2);
  } else {
    if (!isLeftmostColumn)
      overflowRect.shiftYEdgeTo(portionRect.y() - columnGap / 2);
    if (!isRightmostColumn)
      overflowRect.shiftMaxYEdgeTo(portionRect.maxY() + columnGap -
                                   columnGap / 2);
  }
  return overflowRect;
}

unsigned MultiColumnFragmentainerGroup::columnIndexAtOffset(
    LayoutUnit offsetInFlowThread,
    LayoutBox::PageBoundaryRule pageBoundaryRule) const {
  // Handle the offset being out of range.
  if (offsetInFlowThread < m_logicalTopInFlowThread)
    return 0;

  if (!m_columnHeight)
    return 0;
  unsigned columnIndex =
      ((offsetInFlowThread - m_logicalTopInFlowThread) / m_columnHeight)
          .floor();
  if (pageBoundaryRule == LayoutBox::AssociateWithFormerPage &&
      columnIndex > 0 &&
      logicalTopInFlowThreadAt(columnIndex) == offsetInFlowThread) {
    // We are exactly at a column boundary, and we've been told to associate
    // offsets at column boundaries with the former column, not the latter.
    columnIndex--;
  }
  return columnIndex;
}

unsigned MultiColumnFragmentainerGroup::columnIndexAtVisualPoint(
    const LayoutPoint& visualPoint) const {
  bool isColumnProgressionInline =
      m_columnSet.multiColumnFlowThread()->progressionIsInline();
  bool isHorizontalWritingMode = m_columnSet.isHorizontalWritingMode();
  LayoutUnit columnLengthInColumnProgressionDirection =
      isColumnProgressionInline ? m_columnSet.pageLogicalWidth()
                                : logicalHeight();
  LayoutUnit offsetInColumnProgressionDirection =
      isHorizontalWritingMode == isColumnProgressionInline ? visualPoint.x()
                                                           : visualPoint.y();
  if (!m_columnSet.style()->isLeftToRightDirection() &&
      isColumnProgressionInline)
    offsetInColumnProgressionDirection =
        m_columnSet.logicalWidth() - offsetInColumnProgressionDirection;
  LayoutUnit columnGap = m_columnSet.columnGap();
  if (columnLengthInColumnProgressionDirection + columnGap <= 0)
    return 0;
  // Column boundaries are in the middle of the column gap.
  int index = ((offsetInColumnProgressionDirection + columnGap / 2) /
               (columnLengthInColumnProgressionDirection + columnGap))
                  .toInt();
  if (index < 0)
    return 0;
  return std::min(unsigned(index), actualColumnCount() - 1);
}

void MultiColumnFragmentainerGroup::columnIntervalForBlockRangeInFlowThread(
    LayoutUnit logicalTopInFlowThread,
    LayoutUnit logicalBottomInFlowThread,
    unsigned& firstColumn,
    unsigned& lastColumn) const {
  logicalTopInFlowThread =
      std::max(logicalTopInFlowThread, this->logicalTopInFlowThread());
  logicalBottomInFlowThread =
      std::min(logicalBottomInFlowThread, this->logicalBottomInFlowThread());
  ASSERT(logicalTopInFlowThread <= logicalBottomInFlowThread);
  firstColumn = columnIndexAtOffset(logicalTopInFlowThread,
                                    LayoutBox::AssociateWithLatterPage);
  if (logicalBottomInFlowThread == logicalTopInFlowThread) {
    // Zero-height block range. There'll be one column in the interval. Set it
    // right away. This is important if we're at a column boundary, since
    // calling columnIndexAtOffset() with the end-exclusive bottom offset would
    // actually give us the *previous* column.
    lastColumn = firstColumn;
  } else {
    lastColumn = columnIndexAtOffset(logicalBottomInFlowThread,
                                     LayoutBox::AssociateWithFormerPage);
  }
}

void MultiColumnFragmentainerGroup::columnIntervalForVisualRect(
    const LayoutRect& rect,
    unsigned& firstColumn,
    unsigned& lastColumn) const {
  bool isColumnProgressionInline =
      m_columnSet.multiColumnFlowThread()->progressionIsInline();
  bool isFlippedColumnProgression =
      !m_columnSet.style()->isLeftToRightDirection() &&
      isColumnProgressionInline;
  if (m_columnSet.isHorizontalWritingMode() == isColumnProgressionInline) {
    if (isFlippedColumnProgression) {
      firstColumn = columnIndexAtVisualPoint(rect.maxXMinYCorner());
      lastColumn = columnIndexAtVisualPoint(rect.minXMinYCorner());
    } else {
      firstColumn = columnIndexAtVisualPoint(rect.minXMinYCorner());
      lastColumn = columnIndexAtVisualPoint(rect.maxXMinYCorner());
    }
  } else {
    if (isFlippedColumnProgression) {
      firstColumn = columnIndexAtVisualPoint(rect.minXMaxYCorner());
      lastColumn = columnIndexAtVisualPoint(rect.minXMinYCorner());
    } else {
      firstColumn = columnIndexAtVisualPoint(rect.minXMinYCorner());
      lastColumn = columnIndexAtVisualPoint(rect.minXMaxYCorner());
    }
  }
  ASSERT(firstColumn <= lastColumn);
}

MultiColumnFragmentainerGroupList::MultiColumnFragmentainerGroupList(
    LayoutMultiColumnSet& columnSet)
    : m_columnSet(columnSet) {
  append(MultiColumnFragmentainerGroup(m_columnSet));
}

// An explicit empty destructor of MultiColumnFragmentainerGroupList should be
// in MultiColumnFragmentainerGroup.cpp, because if an implicit destructor is
// used, msvc 2015 tries to generate its destructor (because the class is
// dll-exported class) and causes a compile error because of lack of
// MultiColumnFragmentainerGroup::operator=.  Since
// MultiColumnFragmentainerGroup is non-copyable, we cannot define the
// operator=.
MultiColumnFragmentainerGroupList::~MultiColumnFragmentainerGroupList() {}

MultiColumnFragmentainerGroup&
MultiColumnFragmentainerGroupList::addExtraGroup() {
  append(MultiColumnFragmentainerGroup(m_columnSet));
  return last();
}

void MultiColumnFragmentainerGroupList::deleteExtraGroups() {
  shrink(1);
}

}  // namespace blink