tileOf method

Rect tileOf(
  1. Size box
)

The first tile in box: its size from width, height and ratio -- or a share of the box's (widthFactor, heightFactor) -- resized to a whole number of tiles on a round axis, placed by x and y in what the box leaves and moved on by dx and dy.

Implementation

Rect tileOf(Size box) {
  var w = _widthFactor != null ? math.max(0.0, _widthFactor! * box.width + (_width ?? 0)) : _width;
  var h = _heightFactor != null ? math.max(0.0, _heightFactor! * box.height + (_height ?? 0)) : _height;
  final r = _ratio != null && _ratio! > 0 ? _ratio : null;
  if (w == null && h == null) {
    if (r != null && box.width > 0 && box.height > 0) {
      // `contain`: the picture's shape, as large as the box holds.
      if (box.width / box.height > r) {
        h = box.height;
        w = h * r;
      } else {
        w = box.width;
        h = w / r;
      }
    } else {
      w = box.width;
      h = box.height;
    }
  } else if (w == null) {
    w = r != null ? h! * r : box.width;
  } else {
    h ??= r != null ? w / r : box.height;
  }
  // A side a share gives and the ratio the other: Blink holds that other
  // in layout units, 1/64 px with the rest dropped.
  if (r != null && (_widthFactor != null) != (_heightFactor != null)) {
    double lu(double v) => (v * 64 + 1e-6).floorToDouble() / 64;
    if (_widthFactor != null && _height == null) h = lu(h!);
    if (_heightFactor != null && _width == null) w = lu(w);
  }
  var tw = w, th = h!;
  // In Blink's layout units, 1/64 px: the room over the count with the
  // remainder dropped (a LayoutUnit division truncates). 300px of 7px
  // tiles is 43 of 6.96875, not of 6.9767 -- a third of a pixel short by
  // the last one, which is where Chrome draws them.
  double rounded(double room, double tile) =>
      ((room * 64 + 1e-6) / math.max(1, (room / tile).round())).floorToDouble() / 64;
  if (_roundX && tw > 0 && box.width > 0) {
    final k = rounded(box.width, tw) / tw;
    tw *= k;
    if (!_roundY && _height == null && _heightFactor == null) th *= k;
  }
  if (_roundY && th > 0 && box.height > 0) {
    final k = rounded(box.height, th) / th;
    th *= k;
    if (!_roundX && _width == null && _widthFactor == null) tw *= k;
  }
  final (gx, gy) = _spacing(box, tw, th);
  return Rect.fromLTWH(
    gx != null ? 0 : (box.width - tw) * _x + _dx,
    gy != null ? 0 : (box.height - th) * _y + _dy,
    tw,
    th,
  );
}