Best Phoronix-test-suite code snippet using pts_svg_dom.estimate_text_dimensions
pts_graph_core.php
Source:pts_graph_core.php  
...321		return $maximum;322	}323	protected function text_size_bounds($string, $font_size, $minimum_font_size, $bound_width, $bound_height = -1)324	{325		list($string_width, $string_height) = pts_svg_dom::estimate_text_dimensions($string, $font_size);326		while($font_size > $minimum_font_size && $string_width > $bound_width || ($bound_height > 0 && $string_height > $bound_height))327		{328			$font_size -= 0.3;329			list($string_width, $string_height) = pts_svg_dom::estimate_text_dimensions($string, $font_size);330		}331		return $font_size;332	}333	protected function update_graph_dimensions($width = -1, $height = -1, $recalculate_offsets = false)334	{335		// Allow render area to be increased, but not decreased336		$this->i['graph_width'] = max($this->i['graph_width'], $width);337		$this->i['graph_height'] = max($this->i['graph_height'], $height);338		if($recalculate_offsets)339		{340			$this->i['graph_top_end'] = $this->i['graph_height'] - $this->i['top_end_bottom'];341			$this->i['graph_left_end'] = $this->i['graph_width'] - $this->i['left_end_right'];342		}343	}344	protected function identifier_to_branded_color($identifier, $fallback_color = null)345	{346		if($this->i['support_color_branding'] == false || !isset($identifier[6]))347		{348			return $fallback_color;349		}350		// See if the result identifier matches something to be color-coded better351		$identifier = strtolower($identifier) . ' ';352		if(strpos($identifier, 'geforce') !== false || strpos($identifier, 'nvidia') !== false || strpos($identifier, 'quadro') !== false)353		{354			$paint_color = '#77b900';355		}356		else if(strpos($identifier, 'radeon') !== false || strpos($identifier, 'amd ') !== false || strpos($identifier, 'a10-') !== false || strpos($identifier, 'opteron ') !== false || strpos($identifier, 'fx-') !== false || strpos($identifier, 'firepro ') !== false || strpos($identifier, 'ryzen ') !== false)357		{358			$paint_color = '#f1052d';359		}360		else if(strpos($identifier, 'intel ') !== false || strpos($identifier, 'xeon ') !== false || strpos($identifier, 'core i') !== false || strpos($identifier, 'pentium') !== false || strpos($identifier, 'celeron') !== false)361		{362			$paint_color = '#0b5997';363		}364		else if(strpos($identifier, 'bsd') !== false)365		{366			$paint_color = '#850000';367		}368		else if(strpos($identifier, 'windows ') !== false)369		{370			$paint_color = '#0078d7';371		}372		else373		{374			$paint_color = $fallback_color;375		}376		if($paint_color != $fallback_color && strpos($identifier, ' - '))377		{378			// If there is " - " in string, darken the color... based upon idea when doing AMDGPU vs. Mesa vs. stock NVIDIA comparison for RX 480379			$paint_color = $this->darken_color($paint_color);380		}381		return $paint_color;382	}383	//384	// Render Functions385	//386	public function darken_color(&$paint_color)387	{388		$new_color = null;389		foreach(str_split(str_replace('#', null, $paint_color), 2) as $color)390		{391			$dec = hexdec($color);392			$dec = min(max(0, $dec * 0.7), 255);393			$new_color .= str_pad(dechex($dec), 2, 0, STR_PAD_LEFT);394		}395		return '#' . substr($new_color, 0, 6);396	}397	public function renderGraph()398	{399		$this->render_graph_start();400		$this->render_graph_finish();401	}402	public function render_graph_start()403	{404		$this->render_graph_dimensions();405		$this->render_graph_pre_init();406		$this->render_graph_init();407	}408	public function render_graph_dimensions()409	{410		$this->i['graph_max_value'] = $this->maximum_graph_value();411		$longest_identifier = $this->test_result->test_result_buffer->get_longest_identifier();412		// Make room for tick markings, left hand side413		if($this->i['iveland_view'] == false)414		{415			if($this->i['graph_value_type'] == 'NUMERICAL')416			{417				$this->i['left_start'] += self::text_string_width($this->i['graph_max_value'], self::$c['size']['tick_mark']) + 2;418			}419			if($this->i['hide_graph_identifiers'])420			{421				$this->i['graph_top_end'] += $this->i['top_end_bottom'] / 2;422			}423			$this->i['top_start'] += $this->graph_key_height();424		}425		else426		{427			if($this->i['graph_orientation'] == 'HORIZONTAL')428			{429				if($this->is_multi_way_comparison && count($this->results) > 1)430				{431					$longest_r = $longest_identifier;432					$longest_r = explode(' - ', $longest_r);433					$plus_extra = 0;434					if(count($longest_r) > 1)435					{436						$plus_extra = count($longest_r) * $this->i['identifier_size'] * 1.2;437					}438					$longest_identifier_width = self::text_string_width($this->i['graph_max_value'], $this->i['identifier_size']) + 60 + $plus_extra;439				}440				else441				{442					$longest_identifier_width = self::text_string_width($longest_identifier, $this->i['identifier_size']) + 8;443				}444				$longest_identifier_max = ($this->i['graph_width'] * 0.5) + 0.01;445				$this->i['left_start'] = min($longest_identifier_max, max($longest_identifier_width, 70));446				$this->i['left_end_right'] = 15;447				$this->i['graph_left_end'] = $this->i['graph_width'] - $this->i['left_end_right'];448			}449			else if($this->i['graph_value_type'] == 'NUMERICAL')450			{451				$this->i['left_start'] += max(20, self::text_string_width($this->i['graph_max_value'] + 0.01, self::$c['size']['tick_mark']) + 2);452			}453			// Pad 8px on top and bottom + title bar + sub-headings454			$this->i['top_heading_height'] = 16 + self::$c['size']['headers'] + (count($this->graph_sub_titles) * (self::$c['size']['sub_headers'] + 4));455			if($this->i['iveland_view'])456			{457				// Ensure there is enough room to print PTS logo458				$this->i['top_heading_height'] = max($this->i['top_heading_height'], 46);459			}460			$key_height = $this->graph_key_height();461			if($key_height > $this->i['key_line_height'])462			{463				// Increase height so key doesn't take up too much room464				$this->i['graph_height'] += $key_height;465				$this->i['graph_top_end'] += $key_height;466			}467			$this->i['top_start'] = $this->i['top_heading_height'] + $key_height + 16; // + spacing before graph starts468			$bottom_heading = 14;469			if($this->i['graph_orientation'] == 'HORIZONTAL')470			{471				if($this->is_multi_way_comparison && count($this->results) > 1)472				{473					$longest_string = explode(' - ', $longest_identifier);474					$longest_string = pts_strings::find_longest_string($longest_string);475					$rotated_text = round(self::text_string_width($longest_string, $this->i['identifier_size']) * 0.96);476					$per_identifier_height = 26; // default477					if(ceil($rotated_text * 1.25) >= floor($per_identifier_height * count($this->results)))478					{479						// this is to avoid having a rotated text bar overrun other results480						// XXX the 50 number might be too hacky481						$per_identifier_height = max(54, ceil($rotated_text / count($this->results) * 1.1));482					}483				}484				else if(count($this->results) > 3)485				{486					$per_identifier_height = count($this->results) * 18;487				}488				else489				{490					// If there's too much to plot, reduce the size so each graph doesn't take too much room491					$id_count = count(pts_arrays::first_element($this->results));492					if($id_count < 10)493					{494						$per_identifier_height = 46;495					}496					else if($id_count < 20)497					{498						$per_identifier_height = 36;499					}500					else if($id_count <= 38)501					{502						$this->i['compact_result_view'] = true;503						$per_identifier_height = 30;504					}505					else506					{507						$this->i['compact_result_view'] = true;508						$per_identifier_height = 26;509					}510				}511				$num_identifiers = $this->test_result->test_result_buffer->get_count();512				$this->i['graph_top_end'] = $this->i['top_start'] + ($num_identifiers * $per_identifier_height);513				// $this->i['top_end_bottom']514				$this->i['graph_height'] = $this->i['graph_top_end'] + 25 + $bottom_heading;515			}516			else517			{518				$this->i['graph_height'] += $bottom_heading + 4;519			}520			if(!empty($this->i['notes']))521			{522				$this->i['graph_height'] += $this->note_display_height();523			}524		}525	}526	public function render_graph_finish()527	{528		$this->render_graph_key();529		$this->render_graph_base($this->i['left_start'], $this->i['top_start'], $this->i['graph_left_end'], $this->i['graph_top_end']);530		$this->render_graph_heading();531		if($this->i['hide_graph_identifiers'] == false)532		{533			$this->render_graph_identifiers();534		}535		if($this->i['graph_value_type'] == 'NUMERICAL')536		{537			$this->render_graph_value_ticks($this->i['left_start'], $this->i['top_start'], $this->i['graph_left_end'], $this->i['graph_top_end']);538		}539		$this->render_graph_result();540		$this->render_graph_post();541	}542	protected function render_graph_pre_init()543	{544		return;545	}546	protected function render_graph_init()547	{548		$this->update_graph_dimensions();549		$this->svg_dom = new pts_svg_dom(ceil($this->i['graph_width']), ceil($this->i['graph_height']));550		// Background Color551		if($this->i['iveland_view'] || self::$c['graph']['border'])552		{553			$this->svg_dom->add_element('rect', array('x' => 0, 'y' => 0, 'width' => $this->i['graph_width'], 'height' => $this->i['graph_height'], 'fill' => self::$c['color']['background'], 'stroke' => self::$c['color']['border'], 'stroke-width' => 2));554		}555		else556		{557			$this->svg_dom->add_element('rect', array('x' => 0, 'y' => 0, 'width' => $this->i['graph_width'], 'height' => $this->i['graph_height'], 'fill' => self::$c['color']['background']));558		}559		if($this->i['iveland_view'] == false && ($sub_title_count = count($this->graph_sub_titles)) > 1)560		{561			$this->i['top_start'] += (($sub_title_count - 1) * (self::$c['size']['sub_headers'] + 4));562		}563	}564	protected function render_graph_heading($with_version = true)565	{566		$href = null;567		if($this->test_identifier != null)568		{569			$href = 'http://openbenchmarking.org/test/' . $this->test_identifier;570		}571		// Default to NORMAL572		if($this->i['iveland_view'])573		{574			$this->svg_dom->add_element('rect', array('x' => 0, 'y' => 0, 'width' => $this->i['graph_width'], 'height' => $this->i['top_heading_height'], 'fill' => self::$c['color']['main_headers']));575			if(isset($this->graph_title[36]))576			{577				// If it's a long string make sure it won't run over the side...578				while(self::text_string_width($this->graph_title, self::$c['size']['headers']) > ($this->i['graph_left_end'] - 20))579				{580					self::$c['size']['headers'] -= 0.5;581				}582			}583			$this->svg_dom->add_text_element($this->graph_title, array('x' => 6, 'y' => (self::$c['size']['headers'] + 2), 'font-size' => self::$c['size']['headers'], 'fill' => self::$c['color']['background'], 'text-anchor' => 'start', 'xlink:show' => 'new', 'xlink:href' => $href, 'font-weight' => 'bold'));584			foreach($this->graph_sub_titles as $i => $sub_title)585			{586				$vertical_offset = 12 + self::$c['size']['headers'] + (($i + 1) * (self::$c['size']['sub_headers'] - 4));587				$sub_title_size = self::$c['size']['sub_headers'];588				if(isset($sub_title[69]))589				{590					while(self::text_string_width($sub_title, $sub_title_size) > ($this->i['graph_left_end'] - 20))591						$sub_title_size -= 0.5;592				}593				$this->svg_dom->add_text_element($sub_title, array('x' => 6, 'y' => $vertical_offset, 'font-size' => $sub_title_size, 'font-weight' => 'bold', 'fill' => self::$c['color']['background'], 'text-anchor' => 'start'));594			}595			// SVG version of PTS thanks to https://gist.github.com/xorgy/65c6d0e87757dbb56a75596			$this->svg_dom->add_element('path', array('d' => 'm74 22v9m-5-16v16m-5-28v28m-23-2h12.5c2.485281 0 4.5-2.014719 4.5-4.5s-2.014719-4.5-4.5-4.5h-8c-2.485281 0-4.5-2.014719-4.5-4.5s2.014719-4.5 4.5-4.5h12.5m-21 5h-11m11 13h-2c-4.970563 0-9-4.029437-9-9v-20m-24 40v-20c0-4.970563 4.0294373-9 9-9 4.970563 0 9 4.029437 9 9s-4.029437 9-9 9h-9', 'stroke' => '#ffffff', 'stroke-width' => 4, 'fill' => 'none', 'transform' => 'translate(' . ceil($this->i['graph_left_end'] - 77) . ',' . (ceil($this->i['top_heading_height'] / 40 + 2)) . ')'));597		}598		else599		{600			$this->svg_dom->add_text_element($this->graph_title, array('x' => round($this->i['graph_left_end'] / 2), 'y' => (self::$c['size']['headers'] + 2), 'font-size' => self::$c['size']['headers'], 'fill' => self::$c['color']['main_headers'], 'text-anchor' => 'middle', 'xlink:show' => 'new', 'xlink:href' => $href));601			foreach($this->graph_sub_titles as $i => $sub_title)602			{603				$this->svg_dom->add_text_element($sub_title, array('x' => round($this->i['graph_left_end'] / 2), 'y' => (31 + (($i + 1) * 18)), 'font-size' => self::$c['size']['sub_headers'], 'fill' => self::$c['color']['main_headers'], 'text-anchor' => 'middle'));604			}605			if($with_version)606			{607				$this->svg_dom->add_text_element($this->i['graph_version'], array('x' => $this->i['graph_left_end'] , 'y' => ($this->i['top_start'] - 3), 'font-size' => 7, 'fill' => self::$c['color']['body_light'], 'text-anchor' => 'end', 'xlink:show' => 'new', 'xlink:href' => 'http://www.phoronix-test-suite.com/'));608			}609		}610	}611	protected function render_graph_post()612	{613		if($this->i['iveland_view'])614		{615			$bottom_heading_start = $this->i['graph_top_end'] + $this->i['bottom_offset'] + 22;616			$this->svg_dom->add_element('rect', array('x' => 0, 'y' => $bottom_heading_start, 'width' => $this->i['graph_width'], 'height' => ($this->i['graph_height'] - $bottom_heading_start), 'fill' => self::$c['color']['main_headers']));617			$this->svg_dom->add_text_element($this->i['graph_version'], array('x' => $this->i['graph_left_end'], 'y' => ($bottom_heading_start + self::$c['size']['key'] + 3), 'font-size' => self::$c['size']['key'], 'fill' => self::$c['color']['background'], 'text-anchor' => 'end', 'xlink:show' => 'new', 'xlink:href' => 'http://www.phoronix-test-suite.com/'));618			if(isset($this->d['link_alternate_view']) && $this->d['link_alternate_view'])619			{620				// add SVG version: https://gist.github.com/xorgy/169a65e29a3c2cc41e7f621				$a = $this->svg_dom->make_a($this->d['link_alternate_view']);622				$g = $this->svg_dom->make_g(array('transform' => 'translate(' . 4 . ',' . ($bottom_heading_start + 1) . ')', 'width' => 10, 'height' => 16), $a);623				$this->svg_dom->add_element('path', array('d' => 'M5 0v6.5L0 11l3-3-3-3.5L5 0', 'fill' => '#038bb8'), $g);624				$this->svg_dom->add_element('path', array('d' => 'M5 0v6.5l5 4.5-3-3 3-3.5L5 0', 'fill' => '#25b3e8'), $g);625				$this->svg_dom->add_element('path', array('d' => 'M5 16V9l5-4.5V11l-5 5', 'fill' => '#e4f4fd'), $g);626				$this->svg_dom->add_element('path', array('d' => 'M5 16V9L0 4.5V11l5 5', 'fill' => '#65cbf4'), $g);627			}628			if(!empty($this->i['notes']))629			{630				$estimated_height = 0;631				foreach($this->i['notes'] as $i => $note_r)632				{633					$this->svg_dom->add_textarea_element(($i + 1) . '. ' . $note_r['note'], array('x' => 5, 'y' => ($bottom_heading_start + (self::$c['size']['key'] * 2) + 8 + $estimated_height), 'font-size' => (self::$c['size']['key'] - 1), 'fill' => self::$c['color']['background'], 'text-anchor' => 'start', 'xlink:title' => $note_r['hover-title']), $estimated_height);634				}635			}636		}637	}638	protected function render_graph_base($left_start, $top_start, $left_end, $top_end)639	{640		if($this->i['graph_orientation'] == 'HORIZONTAL' || $this->i['iveland_view'])641		{642			$g = $this->svg_dom->make_g(array('stroke' => self::$c['color']['notches'], 'stroke-width' => 1));643			$this->svg_dom->add_element('line', array('x1' => $left_start, 'y1' => $top_start, 'x2' => $left_start, 'y2' => ($top_end + 1)), $g);644			$this->svg_dom->add_element('line', array('x1' => $left_start, 'y1' => $top_end, 'x2' => ($left_end + 1), 'y2' => $top_end), $g);645			if(!empty(self::$c['text']['watermark']))646			{647				$this->svg_dom->add_text_element(self::$c['text']['watermark'], array('x' => $left_end, 'y' => ($top_start - 5), 'font-size' => 8, 'fill' => self::$c['color']['text'], 'text-anchor' => 'end', 'xlink:show' => 'new', 'xlink:href' => self::$c['text']['watermark_url']));648			}649		}650		else651		{652			$this->svg_dom->add_element('rect', array('x' => $left_start, 'y' => $top_start, 'width' => ($left_end - $left_start), 'height' => ($top_end - $top_start), 'fill' => self::$c['color']['body'], 'stroke' => self::$c['color']['notches'], 'stroke-width' => 1));653			if(self::$c['text']['watermark'] != null)654			{655				$this->svg_dom->add_text_element(self::$c['text']['watermark'], array('x' => ($left_end - 2), 'y' => ($top_start + 12), 'font-size' => 10, 'fill' => self::$c['color']['text'], 'text-anchor' => 'end', 'xlink:show' => 'new', 'xlink:href' => self::$c['text']['watermark_url']));656			}657		}658		if(!empty($this->graph_y_title) && $this->i['hide_y_title'] == false)659		{660			$str = $this->graph_y_title;661			$offset = 0;662			if($this->i['graph_proportion'] != null)663			{664				$proportion = null;665				switch($this->i['graph_proportion'])666				{667					case 'LIB':668						$proportion = 'Less Is Better';669						$offset += 12;670						if($this->i['graph_orientation'] == 'HORIZONTAL')671						{672							$this->draw_arrow($left_start, $top_start - 8, $left_start + 9, $top_start - 8, self::$c['color']['text'], self::$c['color']['body_light'], 1);673						}674						else675						{676							$this->draw_arrow($left_start + 4, $top_start - 4, $left_start + 4, $top_start - 11, self::$c['color']['text'], self::$c['color']['body_light'], 1);677						}678						break;679					case 'HIB':680						$proportion = 'More Is Better';681						$offset += 12;682						if($this->i['graph_orientation'] == 'HORIZONTAL')683						{684							$this->draw_arrow($left_start + 9, $top_start - 8, $left_start, $top_start - 8, self::$c['color']['text'], self::$c['color']['body_light'], 1);685						}686						else687						{688							$this->draw_arrow($left_start + 4, $top_start - 11, $left_start + 4, $top_start - 4, self::$c['color']['text'], self::$c['color']['body_light'], 1);689						}690						break;691				}692				if($proportion)693				{694					if($str)695					{696						$str .= ', ';697					}698					$str .= $proportion;699				}700			}701			$this->svg_dom->add_text_element($str, array('x' => ($left_start + $offset), 'y' => ($top_start - 5), 'font-size' => 8, 'fill' => self::$c['color']['text'], 'text-anchor' => 'start'));702		}703	}704	protected function render_graph_value_ticks($left_start, $top_start, $left_end, $top_end, $show_numbers = true)705	{706		$increment = round($this->i['graph_max_value'] / $this->i['mark_count'], $this->i['graph_max_value'] < 10 ? 4 : 2);707		if($this->i['graph_orientation'] == 'HORIZONTAL')708		{709			$tick_width = round(($left_end - $left_start) / $this->i['mark_count']);710			$display_value = 0;711			$g = $this->svg_dom->make_g(array('font-size' => self::$c['size']['tick_mark'], 'fill' => self::$c['color']['text'], 'text-anchor' => 'middle'));712			$g_lines = $this->svg_dom->make_g(array('stroke' => self::$c['color']['body'], 'stroke-width' => 1));713			for($i = 0; $i < $this->i['mark_count']; $i++)714			{715				$px_from_left = $left_start + ($tick_width * $i);716				if($i != 0)717				{718					$show_numbers && $this->svg_dom->add_text_element($display_value, array('x' => $px_from_left + 2, 'y' => ($top_end + 5 + self::$c['size']['tick_mark'])), $g);719					$this->svg_dom->add_element('line', array('x1' => ($px_from_left + 2), 'y1' => ($top_start), 'x2' => ($px_from_left + 2), 'y2' => ($top_end - 5), 'stroke-dasharray' => '5,5'), $g_lines);720					$this->svg_dom->add_element('line', array('x1' => ($px_from_left + 2), 'y1' => ($top_end - 4), 'x2' => ($px_from_left + 2), 'y2' => ($top_end + 5)), $g_lines);721				}722				$display_value += $increment;723			}724		}725		else726		{727			$tick_width = round(($top_end - $top_start) / $this->i['mark_count']);728			$px_from_left_start = $left_start - 5;729			$px_from_left_end = $left_start + 5;730			$display_value = 0;731			$g_lines = $this->svg_dom->make_g(array('stroke' => self::$c['color']['notches'], 'stroke-width' => 1, 'stroke-dasharray' => '5,5'));732			$g_lines_2 = $this->svg_dom->make_g(array('stroke' => self::$c['color']['notches'], 'stroke-width' => 1));733			$g_background_lines = $this->svg_dom->make_g(array('stroke' =>  self::$c['color']['body_light'], 'stroke-width' => 1, 'stroke-dasharray' => '5,5'));734			$g_text = $this->svg_dom->make_g(array('font-size' => self::$c['size']['tick_mark'], 'fill' => self::$c['color']['text'], 'text-anchor' => 'end'));735			for($i = 0; $i < $this->i['mark_count']; $i++)736			{737				$px_from_top = round($top_end - ($tick_width * $i));738				if($i != 0)739				{740					$show_numbers && $this->svg_dom->add_text_element($display_value, array('x' => ($px_from_left_start - 4), 'y' => round($px_from_top + (self::$c['size']['tick_mark'] / 2))), $g_text);741					if($this->i['show_background_lines'])742					{743						$this->svg_dom->add_element('line', array('x1' => ($px_from_left_end + 6), 'y1' => ($px_from_top + 1), 'x2' => ($this->i['graph_left_end']), 'y2' => ($px_from_top + 1)), $g_background_lines);744					}745					$this->svg_dom->add_element('line', array('x1' => ($left_start), 'y1' => ($px_from_top + 1), 'x2' => ($left_end), 'y2' => ($px_from_top + 1)), $g_lines);746					$this->svg_dom->add_element('line', array('x1' => ($left_start - 4), 'y1' => ($px_from_top + 1), 'x2' => ($left_start + 4), 'y2' => ($px_from_top + 1)), $g_lines_2);747				}748				$display_value += $increment;749			}750		}751	}752	protected function render_graph_identifiers()753	{754		return;755	}756	protected function render_graph_result()757	{758		return;759	}760	protected function graph_key_height()761	{762		if((count($this->results) < 2 || $this->i['show_graph_key'] == false) && !$this->is_multi_way_comparison) // TODO likely should be OR763		{764			return 0;765		}766		$this->i['key_line_height'] = 16;767		$ak = array_keys($this->results);768		$this->i['key_item_width'] = 20 + self::text_string_width(pts_strings::find_longest_string($ak), self::$c['size']['key']);769		$this->i['keys_per_line'] = max(1, floor(($this->i['graph_left_end'] - $this->i['left_start']) / $this->i['key_item_width']));770		return ceil(count($this->results) / $this->i['keys_per_line']) * $this->i['key_line_height'];771	}772	protected function render_graph_key()773	{774		if($this->i['key_line_height'] == 0)775		{776			return;777		}778		$y = $this->i['top_start'] - $this->graph_key_height() - 7;779		$i = 0;780		$g_rect = $this->svg_dom->make_g(array('stroke' => self::$c['color']['notches'], 'stroke-width' => 1));781		$g_text = $this->svg_dom->make_g(array('font-size' => self::$c['size']['key'], 'text-anchor' => 'start', 'font-weight' => 'bold'));782		if(!is_array($this->results))783		{784			return false;785		}786		foreach(array_keys($this->results) as $title)787		{788			if(!empty($title))789			{790				$this_color = $this->get_paint_color($title);791				if($i != 0 && $i % $this->i['keys_per_line'] == 0)792				{793					$y += $this->i['key_line_height'];794				}795				$x = $this->i['left_start'] + 13 + ($this->i['key_item_width'] * ($i % $this->i['keys_per_line']));796				$this->svg_dom->add_element('rect', array('x' => ($x - 13), 'y' => ($y - 5), 'width' => 10, 'height' => 10, 'fill' => $this_color), $g_rect);797				$this->svg_dom->add_text_element($title, array('x' => $x, 'y' => ($y + 4), 'fill' => $this_color), $g_text);798				$i++;799			}800		}801	}802	protected function draw_arrow($tip_x1, $tip_y1, $tail_x1, $tail_y1, $background_color, $border_color = null, $border_width = 0)803	{804		$is_vertical = ($tip_x1 == $tail_x1);805		if($is_vertical)806		{807			// Vertical arrow808			$arrow_length = sqrt(pow(($tail_x1 - $tip_x1), 2) + pow(($tail_y1 - $tip_y1), 2));809			$arrow_length_half = $arrow_length / 2;810			$arrow_points = array(811				$tip_x1 . ',' . $tip_y1,812				($tail_x1 + $arrow_length_half) . ',' . $tail_y1,813				($tail_x1 - $arrow_length_half) . ',' . $tail_y1814				);815		}816		else817		{818			// Horizontal arrow819			$arrow_length = sqrt(pow(($tail_x1 - $tip_x1), 2) + pow(($tail_y1 - $tip_y1), 2));820			$arrow_length_half = $arrow_length / 2;821			$arrow_points = array(822				$tip_x1 . ',' . $tip_y1,823				$tail_x1 . ',' . ($tail_y1 + $arrow_length_half),824				$tail_x1 . ',' . ($tail_y1 - $arrow_length_half)825				);826		}827		$this->svg_dom->add_element('polygon', array('points' => implode(' ', $arrow_points), 'fill' => $background_color, 'stroke' => $border_color, 'stroke-width' => $border_width));828	}829	protected static function text_string_width($string, $size)830	{831		$dimensions = pts_svg_dom::estimate_text_dimensions($string, $size);832		return $dimensions[0];833	}834	protected static function text_string_height($string, $size)835	{836		$dimensions = pts_svg_dom::estimate_text_dimensions($string, $size);837		return $dimensions[1];838	}839	protected function note_display_height()840	{841		// This basically figures out how many lines of notes there are times the size of the font key...842		// additionally, it attempts to figure out if the note will word-wrap to additional lines to accomodate that843		$note_height = 0;844		$sections = array();845		if(!empty($this->i['notes']))846		{847			foreach($this->i['notes'] as $note)848			{849				// If the note isn't at least 36 characters long, assume it's not long enough to word-wrap, so take short-cut for efficiency850				$note_height += !isset($note['note'][36]) ? (self::$c['size']['key'] + 2) : (ceil(self::text_string_width($note['note'], self::$c['size']['key']) / ($this->i['graph_width'] - 14))) * self::$c['size']['key'];...pts_svg_dom.php
Source:pts_svg_dom.php  
...229		if(!isset($attributes['width']))230		{231			$attributes['width'] = $this->width - $attributes['x'];232		}233		$queue_dimensions = self::estimate_text_dimensions($text_string, $attributes['font-size']);234		if($queue_dimensions[0] < $attributes['width'])235		{236			// No wrapping is occuring, so stuff it in a more efficient text element instead237			unset($attributes['width']);238			$this->add_text_element($text_string, $attributes);239			$estimated_height += ($attributes['font-size'] + 3);240			return;241		}242		$el = $this->dom->createElement('text');243		$word_queue = null;244		$line_count = 0;245		$words = explode(' ', $text_string);246		$word_count = count($words);247		$last_word = null;248		foreach($words as $i => $word)249		{250			$word_queue .= $word . ' ';251			$queue_dimensions = self::estimate_text_dimensions($word_queue, ($attributes['font-size'] - 0.45));252			if($queue_dimensions[0] > $attributes['width'] || $i == ($word_count - 1))253			{254				if($i != ($word_count - 1))255				{256					$last_word_pos = strrpos($word_queue, ' ', -2);257					$last_word = substr($word_queue, $last_word_pos);258					$word_queue = substr($word_queue, 0, $last_word_pos);259				}260				$tspan = $this->dom->createElement('tspan');261				$tspan->setAttribute('x', $attributes['x']);262				$tspan->setAttribute('y', $attributes['y']);263				$tspan->setAttribute('dx', ($line_count == 0 ? 0 : 5));264				$tspan->setAttribute('dy', ($line_count * ($attributes['font-size'] + 1)));265				$text_node = $this->dom->createTextNode($word_queue);266				$tspan->appendChild($text_node);267				$el->appendChild($tspan);268				$word_queue = $last_word;269				$line_count++;270			}271		}272		$estimated_height += $line_count * ($attributes['font-size'] + 2);273		unset($attributes['width']);274		if(isset($attributes['xlink:href']) && $attributes['xlink:href'] != null)275		{276			$link = $this->dom->createElement('a');277			$link->setAttribute('xlink:href', $attributes['xlink:href']);278			$link->setAttribute('target', '_blank');279			$link->appendChild($el);280			if($append_to)281			{282				$append_to->appendChild($link);283			}284			else285			{286				$this->svg->appendChild($link);287			}288			unset($attributes['xlink:href']);289		}290		else291		{292			if($append_to)293			{294				$append_to->appendChild($el);295			}296			else297			{298				$this->svg->appendChild($el);299			}300		}301		foreach($attributes as $name => $value)302		{303			$el->setAttribute($name, $value);304		}305	}306	public static function estimate_text_dimensions($text_string, $font_size)307	{308		if($text_string == null)309		{310			return array(0, 0);311		}312		$box_height = ceil(0.76 * $font_size);313		$box_width = ceil((0.76 * strlen($text_string) * $font_size) - ceil(strlen($text_string) * 1.05));314		// Width x Height315		return array($box_width, $box_height);316	}317}318?>...estimate_text_dimensions
Using AI Code Generation
1require_once('pts_svg_dom.php');2$svg = new pts_svg_dom();3$text = $svg->create_text(0,0,'my text');4$text->setAttribute('font-size', 20);5$text->setAttribute('font-family', 'Arial');6$dimensions = $svg->estimate_text_dimensions($text);7echo "Width: ".$dimensions['width'].", Height: ".$dimensions['height'];estimate_text_dimensions
Using AI Code Generation
1require_once 'pts_svg_dom.php';2$svg = new pts_svg_dom();3$svg->set_font_size(30);4$svg->set_font_family('Arial');5$svg->set_text_anchor('middle');6$svg->set_text('Hello World');7$svg->set_x(50);8$svg->set_y(50);9$svg->set_stroke('black');10$svg->set_fill('none');11$svg->set_stroke_width(1);12$svg->set_stroke_linecap('round');13$svg->set_stroke_linejoin('round');14$svg->set_stroke_dasharray('5,5');15$svg->set_stroke_dashoffset(0);16$svg->set_stroke_opacity(1);17$svg->set_fill_opacity(1);18$svg->set_font_style('normal');19$svg->set_font_weight('normal');20$svg->set_text_decoration('none');21$svg->set_text_transform('none');22$svg->set_letter_spacing(0);23$svg->set_word_spacing(0);24$svg->set_dominant_baseline('auto');25$svg->set_alignment_baseline('auto');26$svg->set_baseline_shift(0);27$svg->set_text_length(0);28$svg->set_length_adjust('spacing');29$svg->set_rotation(0);30$svg->set_text_anchor('middle');31$svg->set_text_direction('ltr');32$svg->set_text_orientation('auto');33$svg->set_text_path('none');34$svg->set_text_path_spacing('auto');35$svg->set_text_path_method('align');36$svg->set_text_path_midline('sharp');37$svg->set_text_path_side('right');38$svg->set_text_path_spacing('auto');39$svg->set_text_path_start_offset(0);40$svg->set_text_path_glyph_orientation_horizontal(0);41$svg->set_text_path_glyph_orientation_vertical(0);42$svg->set_text_path_kerning('auto');43$svg->set_text_path_textLength(0);44$svg->set_text_path_lengthAdjust('spacing');45$svg->set_text_path_rotation(0);46$svg->set_text_path_text_direction('ltr');47$svg->set_text_path_text_orientation('auto');48$svg->set_text_path_text_anchor('middle');49$svg->set_text_path_text_decoration('none');estimate_text_dimensions
Using AI Code Generation
1require_once 'pts_svg_dom.php';2require_once 'pts_svg_text.php';3$svg = new pts_svg_dom(100, 100);4$svg->add_text("Hello World", 20, 20, 0, 0, "Hello World", "Verdana");5$svg->add_text("Hello World", 20, 40, 0, 0, "Hello World", "Verdana", 12, "bold");6$svg->add_text("Hello World", 20, 60, 0, 0, "Hello World", "Verdana", 12, "bold", "italic");7$svg->add_text("Hello World", 20, 80, 0, 0, "Hello World", "Verdana", 12, "bold", "italic", "underline");8$svg->render();9require_once 'pts_svg_dom.php';10require_once 'pts_svg_text.php';11$svg = new pts_svg_dom(100, 100);12$svg->add_text("Hello World", 20, 20,estimate_text_dimensions
Using AI Code Generation
1$width = $svg->estimate_text_dimensions($text, $font_size, $font_family, $font_weight);2$height = $svg->estimate_text_dimensions($text, $font_size, $font_family, $font_weight, true);3$svg->width = $width;4$svg->height = $height;5$svg->text = $text;6$svg->font_size = $font_size;7$svg->font_family = $font_family;8$svg->font_weight = $font_weight;9$svg->x = 0;10$svg->y = 0;11$svg->fill = $color;12$svg->background = $background;13$svg->stroke = $stroke;14$svg->stroke_width = $stroke_width;15$svg->stroke_linecap = $stroke_linecap;16$svg->stroke_linejoin = $stroke_linejoin;17$svg->stroke_miterlimit = $stroke_miterlimit;18$svg->stroke_dasharray = $stroke_dasharray;19$svg->stroke_dashoffset = $stroke_dashoffset;20$svg->opacity = $opacity;21$svg->visibility = $visibility;22$svg->display = $display;23$svg->overflow = $overflow;estimate_text_dimensions
Using AI Code Generation
1require_once 'pts_svg_dom.php';2$svg = new pts_svg_dom();3$svg->load('svg.svg');4$svg->registerNodeClass('DOMElement', 'pts_svg_dom_element');5$svg->estimate_text_dimensions('This is a test');6require_once 'pts_svg_dom.php';7$svg = new pts_svg_dom();8$svg->load('svg.svg');9$svg->registerNodeClass('DOMElement', 'pts_svg_dom_element');10$svg->estimate_text_dimensions('This is a test', 20, 'Verdana');11require_once 'pts_svg_dom.php';12$svg = new pts_svg_dom();13$svg->load('svg.svg');14$svg->registerNodeClass('DOMElement', 'pts_svg_dom_element');15$svg->estimate_text_dimensions('This is a test', 20, 'Verdana', 'bold');16require_once 'pts_svg_dom.php';17$svg = new pts_svg_dom();18$svg->load('svg.svg');19$svg->registerNodeClass('DOMElement', 'pts_svg_dom_element');20$svg->estimate_text_dimensions('This is a test', 20, 'Verdana', 'bold', 'italic');21require_once 'pts_svg_dom.php';22$svg = new pts_svg_dom();estimate_text_dimensions
Using AI Code Generation
1require_once "pts_svg_dom.php";2$svg = new pts_svg_dom();3$svg->width = 400;4$svg->height = 200;5$font_size = 20;6$text = "The quick brown fox jumped over the lazy dog.";7$text_width = $svg->estimate_text_dimensions($text, $font_size);8$x = ($svg->width - $text_width) / 2;9$y = $svg->height / 2;10$fill = "black";11$svg->draw_text($x, $y, $text, $fill, $font_size);12$svg->write_file("2.svg");13$svg->display();Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with estimate_text_dimensions on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!
