/** * helper function to adjust the desired width of a column to those allowable by the maxium size */ protected function adjustDesiredWidths($desired_widths, $width) { $cols = array_keys($desired_widths); //we cannot fit all the rows as desired . //we will add width to the minimum width until we get to the maximum table width $width_to_add = $this->font_metric->getCharacterWidth('l', true); if ($width_to_add <= 0) { $width_to_add = 100; } $extra_width = $width - count($cols) * $this->min_cell_width; //the extra width we have over from the minimum if ($extra_width <= 0 || $width_to_add == 0) { //fail nicely return $desired_widths; } $widths = array(); foreach ($cols as $col) { $widths[$col] = $this->min_cell_width; //set all widths to the minimum } $cells_in_need = TRUE; $changed = FALSE; $col = $cols[0]; $c = 0; while ($extra_width > 0 && $cells_in_need) { $desired_extra = $desired_widths[$col] - $widths[$col]; if ($desired_extra > 0) { //our cell wants to be bigger then it currently is $changed = TRUE; if ($desired_extra < $width_to_add) { if ($extra_width >= $desired_extra) { $widths[$col] += $desired_extra; $extra_width -= $desired_extra; } else { $widths[$col] += $extra_width; $extra_width = 0; } } else { if ($extra_width >= $width_to_add) { $widths[$col] += $width_to_add; $extra_width -= $width_to_add; } else { $widths[$col] += $extra_width; $extra_width = 0; } } } if ($c >= count($cols) - 1) { //we restart looping through $c = 0; $col = $cols[0]; $cells_in_need = $changed; //if none were changed then we are done } else { //increment our loop counter $c++; $col = $cols[$c]; } } return $widths; }
/** * Gets the character width of a character * @param mixed $ch a character * @param bool use_font_size default false -- * @returns float the width */ public function getCharacterWidth($ch, $use_font_size = false) { if ($this->direction != -1) { return $this->font_metrics[$this->direction]->getCharacterWidth($ch, $use_font_size); } else { return parent::getCharacterWidth($ch, $use_font_size); } }