protected function getWidthsFromAllCells($table_data, $font_sizes) { $rows = count($table_data); $cols = array_keys($table_data[0]); $padding = count($cols) * 2 * $this->col_spacing; // padding for the column spacing $width = $this->max_table_width - $padding; //the width we have available if ($this->max_table_width >= 0 && $this->min_cell_width * count($cols) > $width) { //can't fit all the columns even minimally $widths = array(); $c = 0; //so we will try to fit as many as possible while ($this->min_cell_width * $c <= $width) { $widths[$cols[$c]] = $this->min_cell_width; $c++; } if ($this->min_cell_width * ($c - 1) < $width) { $widths[$cols[$c]] = $width - $this->min_cell_width * ($c - 1); } return array_pad($widths, count($cols), 0); } //we can fit all the columns minimally $desired_widths = array(); $total_desired_width = 0; foreach ($cols as $col) { //get the maximum line length for each cell of text. $max = $this->min_cell_width; for ($r = 0; $r < $rows; $r++) { $paragraphs = $this->getParagraphs($table_data[$r][$col]); foreach ($paragraphs as $paragraph) { $this->font_metric->setFontSize($font_sizes[$r][$col]); $w = $this->font_metric->getStringWidth($paragraph, true); if ($w > $max) { $max = $w; } } } $desired_widths[$col] = $max; $total_desired_width += $max; } if ($this->max_table_width < 0 || $total_desired_width < $width) { return $desired_widths; } return $this->adjustDesiredWidths($desired_widths, $width); }