<?php

// text
// ================================================================================================
function txtGetStyle($pcb, $txt)
{
	for ($i=0; $i < sizeof($pcb->TXT_STYLE); $i++)
	{
		$style = $pcb->TXT_STYLE[$i];
		if ( ($style['size_v'] == $txt['size_v']) )
			return $style['name'];
	}

	return false;
}
function txtAddStyle($pcb, $txt)
{
	for ($i=0; $i < sizeof($pcb->TXT_STYLE); $i++)
	{
		$style = $pcb->TXT_STYLE[$i];
		if ( ($style['size_v'] == $txt['size_v']) )
			return false;
	}

	$pcb->TXT_STYLE[$i]['size_v'] = $txt['size_v'];
	$pcb->TXT_STYLE[$i]['name'] = "txt".$i;

	return $pcb->TXT_STYLE[$i]['name'];
}

echo"<br>";
echo"generate text styles...<br>";
for($i=1; $i<=$pcb->HDR['texts_count']['v']; $i++)
{
	$T = $pcb->TXT[$i];

	$txt['size_v'] = round(1.5*$T['size_v']['v']);

	if ($txt_style = txtAddStyle($pcb, $txt))
	{
			echo"text [$i], added new text {";
			echo"size_v=".$txt['size_v']."";
			echo"}, style=[$txt_style]<br>";
	}

	// save style to text
	$pcb->TXT[$i]['style']['v'] = txtGetStyle($pcb, $txt);
}
echo"save text styles: ".sizeof($pcb->TXT_STYLE)."<br>";

	$pcad_font_stroke  = "    (font\r\n";
	$pcad_font_stroke .= "      (fontType Stroke)\r\n";
	$pcad_font_stroke .= "      (fontFamily SanSerif)\r\n";
	$pcad_font_stroke .= "      (fontFace \"Quality\")\r\n";
	$pcad_font_stroke .= "      (fontHeight 100)\r\n";
	$pcad_font_stroke .= "      (strokeWidth 8)\r\n";
	$pcad_font_stroke .= "    )\r\n";

	$pcad_font_truetype  = "    (font\r\n";
	$pcad_font_truetype .= "      (fontType TrueType)\r\n";
	$pcad_font_truetype .= "      (fontFamily Modern)\r\n";
	$pcad_font_truetype .= "      (fontFace \"Arial\")\r\n";
	$pcad_font_truetype .= "      (fontHeight 100)\r\n";
	$pcad_font_truetype .= "      (strokeWidth 10)\r\n";
	$pcad_font_truetype .= "      (fontWeight 400)\r\n";
	$pcad_font_truetype .= "      (fontCharSet 0)\r\n";
	$pcad_font_truetype .= "      (fontOutPrecision 7)\r\n";
	$pcad_font_truetype .= "      (fontClipPrecision 32)\r\n";
	$pcad_font_truetype .= "      (fontQuality 1)\r\n";
	$pcad_font_truetype .= "      (fontPitchAndFamily 6)\r\n";
	$pcad_font_truetype .= "    )\r\n";


	$pcad_lib =  "  (textStyleDef \"(Default)\"\r\n";
	$pcad_lib .= $pcad_font_stroke;
	$pcad_lib .= "  )\r\n";

	$pcad_lib .= "  (textStyleDef \"(DefaultTTF)\"\r\n";
	$pcad_lib .= $pcad_font_stroke;
	$pcad_lib .= $pcad_font_truetype;
	$pcad_lib .= "    (textStyleAllowTType True)\r\n";
	$pcad_lib .= "    (textStyleDisplayTType True)\r\n";
	$pcad_lib .= "  )\r\n";
file_put_contents($out_file, $pcad_lib, FILE_APPEND | LOCK_EX);

$pcad_lib =  "";
for ($i=0; $i<sizeof($pcb->TXT_STYLE); $i++)
{
	$pcad_lib .= "  (textStyleDef \"".$pcb->TXT_STYLE[$i]['name']."\"\r\n";
	$pcad_lib .= "    (font\r\n";
	$pcad_lib .= "      (fontType Stroke)\r\n";
	$pcad_lib .= "      (fontFamily SanSerif)\r\n";
	$pcad_lib .= "      (fontFace \"Quality\")\r\n";
	$pcad_lib .= "      (fontHeight ".$pcb->TXT_STYLE[$i]['size_v'].")\r\n";
	$pcad_lib .= "      (strokeWidth 8)\r\n";
	$pcad_lib .= "    )\r\n";
	$pcad_lib .= "    (textStyleAllowTType False)\r\n";
	$pcad_lib .= "    (textStyleDisplayTType False)\r\n";
	$pcad_lib .= "  )\r\n";

}
file_put_contents($out_file, $pcad_lib, FILE_APPEND | LOCK_EX);
