125 "speakText" =>
false,
126 "speakRuleset" =>
"mathspeak",
127 "speakStyle"=>
"default",
130 "linebreaks"=>
false,
140 include_once
"./Services/Administration/classes/class.ilSetting.php";
142 $this->
init(self::PURPOSE_BROWSER);
145 $this->use_curl = extension_loaded(
'cURL');
157 if (self::$_instance === NULL) {
158 self::$_instance =
new self;
160 return self::$_instance;
169 public function init($a_purpose = self::PURPOSE_BROWSER)
172 unset($this->engine);
175 if ($this->
settings->get(
'enable_server'))
177 $this->server_address = $this->
settings->get(
'server_address');
178 $this->server_timeout = $this->
settings->get(
'server_timeout');
180 if($a_purpose == self::PURPOSE_BROWSER && $this->
settings->get(
'server_for_browser'))
182 $this->engine = self::ENGINE_SERVER;
186 elseif($a_purpose == self::PURPOSE_EXPORT && $this->
settings->get(
'server_for_export'))
188 $this->engine = self::ENGINE_SERVER;
193 elseif($a_purpose == self::PURPOSE_PDF && $this->
settings->get(
'server_for_pdf'))
195 $this->engine = self::ENGINE_SERVER;
202 elseif ($a_purpose == self::PURPOSE_DEFERRED_PDF && $this->
settings->get(
'server_for_pdf'))
204 $this->engine = self::ENGINE_DEFERRED;
210 if (!isset($this->engine) && $this->
settings->get(
'enable'))
212 $this->engine = self::ENGINE_CLIENT;
213 $this->mathjax_url = $this->
settings->get(
'path_to_mathjax');
216 switch ((
int) $this->
settings->get(
"limiter"))
219 $this->start_limiter =
"[tex]";
220 $this->end_limiter =
"[/tex]";
224 $this->start_limiter =
'<span class="math">';
225 $this->end_limiter =
'</span>';
229 $this->start_limiter =
"\(";
230 $this->end_limiter =
"\)";
237 if (!isset($this->engine) && !empty($this->mimetex_url))
239 $this->engine = self::ENGINE_MIMETEX;
243 if (!isset($this->engine))
245 $this->engine = self::ENGINE_NONE;
261 case self::RENDER_SVG_AS_XML_EMBED:
262 case self::RENDER_SVG_AS_IMG_EMBED:
263 case self::RENDER_SVG_AS_IMG_FILE:
264 $this->rendering = $a_rendering;
265 $this->output =
'svg';
268 case self::RENDER_PNG_AS_IMG_EMBED:
269 case self::RENDER_PNG_AS_IMG_FILE:
270 case self::RENDER_PNG_AS_FO_FILE:
271 $this->rendering = $a_rendering;
272 $this->output =
'png';
285 $this->dpi = (float) $a_dpi;
296 $this->zoom_factor = (float) $a_factor;
314 if ($this->engine == self::ENGINE_CLIENT)
316 $a_tpl->addJavaScript($this->mathjax_url);
333 public function insertLatexImages($a_text, $a_start =
'[tex]', $a_end =
'[/tex]', $a_dir = null, $a_path = null)
338 if ($this->engine != self::ENGINE_MIMETEX)
340 $a_text = preg_replace(
"/\\\\([RZN])([^a-zA-Z]|<\/span>)/",
"\\mathbb{".
"$1".
"}".
"$2", $a_text);
344 $a_start = str_replace(
"\\",
"", $a_start);
345 $a_end = str_replace(
"\\",
"", $a_end);
348 while (is_int($spos = stripos($a_text, $a_start, $cpos)))
350 if (is_int($epos = stripos($a_text, $a_end, $spos + strlen($a_start))))
353 $tex = substr($a_text, $spos + strlen($a_start), $epos - $spos - strlen($a_start));
356 if (substr($tex, 0, 7) ==
'base64:')
358 $tex = base64_decode(substr($tex, 7));
363 $tex = str_replace(
'<br>',
'', $tex);
364 $tex = str_replace(
'<br/>',
'', $tex);
365 $tex = str_replace(
'<br />',
'', $tex);
366 $tex = str_replace(
'\\\\' ,
'\\cr', $tex);
369 if (!is_int(strpos($tex,
'</div>')))
371 switch ($this->engine)
373 case self::ENGINE_CLIENT:
377 $tex = str_replace(
'<',
'<', $tex);
381 case self::ENGINE_SERVER:
385 $tex = html_entity_decode($tex, ENT_QUOTES,
'UTF-8');
389 case self::ENGINE_MIMETEX:
394 case self::ENGINE_DEFERRED:
396 $replacement =
'[tex]' .
'base64:' . base64_encode($tex) .
'[/tex]';
399 case self::ENGINE_NONE:
401 $replacement = htmlspecialchars($tex);
406 $a_text = substr($a_text, 0, $spos) . $replacement . substr($a_text, $epos + strlen($a_end));
423 protected function renderMathJax($a_tex, $a_output_dir = null, $a_image_path = null)
429 switch ($this->output)
446 $hash = md5($a_tex .
'#' . $this->dpi);
447 $file = $this->cache_dir .
'/' . substr($hash, 0, 4) .
'/' . substr($hash, 4, 4) .
'/' . $hash . $suffix;
456 $curl = curl_init($this->server_address);
457 curl_setopt($curl, CURLOPT_HEADER,
false);
458 curl_setopt($curl, CURLOPT_RETURNTRANSFER,
true);
459 curl_setopt($curl, CURLOPT_HTTPHEADER,
array(
"Content-type: application/json"));
460 curl_setopt($curl, CURLOPT_POST,
true);
461 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(
$options));
462 curl_setopt($curl, CURLOPT_TIMEOUT, $this->server_timeout);
464 $response = curl_exec($curl);
465 $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
470 $lines = explode(
"\n", $response);
471 return "[TeX rendering failed: " . $lines[1] .
" " . htmlspecialchars($a_tex) .
"]";
476 $context = stream_context_create(
481 'header' =>
"Content-Type: application/json\r\n",
482 'timeout' => $this->server_timeout,
483 'ignore_errors' =>
true 486 $response = @file_get_contents($this->server_address,
false, $context);
487 if (empty($response))
489 return "[TeX rendering failed: " . htmlspecialchars($a_tex) .
"]";
494 @mkdir(dirname(
$file), 0777,
true);
497 file_put_contents(
$file, $response);
501 if (isset($a_output_dir) && is_dir($a_output_dir))
503 @copy(
$file, $a_output_dir .
'/' . $hash . $suffix);
504 $src = $a_image_path .
'/' . $hash . $suffix;
508 $src = ILIAS_HTTP_PATH .
'/' .
$file;
512 switch ($this->output)
515 list($width, $height) = getimagesize(
$file);
516 $width = round($width * $this->zoom_factor);
517 $height = round($height * $this->zoom_factor);
523 $svg = simplexml_load_file(
$file);
524 $width = round($svg[
'width'] * $this->zoom_factor);
525 $height = round($svg[
'height'] * $this->zoom_factor);
526 $mime =
'image/svg+xml';
532 switch ($this->rendering)
534 case self::RENDER_SVG_AS_XML_EMBED:
535 $html = empty($response) ? file_get_contents(
$file) : $response;
538 case self::RENDER_SVG_AS_IMG_EMBED:
539 case self::RENDER_PNG_AS_IMG_EMBED:
540 $html =
'<img src="data:' . $mime .
';base64,' 541 . base64_encode(empty($response) ? file_get_contents(
$file) : $response)
542 .
'" style="width:' . $width .
'; height:' . $height .
';" />';
545 case self::RENDER_SVG_AS_IMG_FILE:
546 case self::RENDER_PNG_AS_IMG_FILE:
547 $html =
'<img src="' . $src .
'" style="width:' . $width .
'; height:' . $height .
';" />';
550 case self::RENDER_PNG_AS_FO_FILE:
551 $html =
'<fo:external-graphic src="url(' . realpath(
$file) .
')"' 552 .
' content-height="' . $height .
'px" content-width="' . $width .
'px"></fo:external-graphic>';
556 $html = htmlspecialchars($a_tex);
564 return "[TeX rendering failed: " . $e->getMessage() .
"]";
577 protected function renderMimetex($a_tex, $a_output_dir = null, $a_image_path = null)
579 $call = $this->mimetex_url.
'?' 580 .rawurlencode(str_replace(
'&',
'&', str_replace(
'>',
'>', str_replace(
'<',
'<', $a_tex))));
582 if (empty($a_output_dir))
584 $html =
'<img alt="'.htmlentities($a_tex).
'" src="'.$call.
'" />';
588 $cnt = $this->mimetex_count++;
591 $fpr = @fopen($call,
"r");
597 $buf = fread($fpr, 1024);
600 if (is_int(strpos(strtoupper(substr($buf, 0, 5)),
"GIF")))
608 $fpw = fopen($a_output_dir.
"/img".$cnt.
".".$suffix,
"w");
617 $html =
'<img alt="'.htmlentities($a_tex).
'" src='.$a_image_path.
'/img"'.$cnt.
'.'.$suffix.
'/'.
'" />';
640 $type =
array(
"k",
"M",
"G",
"T");
renderMimetex($a_tex, $a_output_dir=null, $a_image_path=null)
Render image from tex code using mimetex.
Class for Server-side generation of latex formulas.
insertLatexImages($a_text, $a_start='[tex]', $a_end='[/tex]', $a_dir=null, $a_path=null)
Replace tex tags with formula image code New version of ilUtil::insertLatexImages.
includeMathJax($a_tpl=null)
Include Mathjax javascript in a template.
const RENDER_PNG_AS_IMG_EMBED
renderMathJax($a_tex, $a_output_dir=null, $a_image_path=null)
Render image from tex code using the MathJax server.
init($a_purpose=self::PURPOSE_BROWSER)
Initialize the usage This must be done before any rendering call.
const RENDER_SVG_AS_IMG_FILE
setRendering($a_rendering)
Set the image type rendered by the server.
setDpi($a_dpi)
Set the dpi of the rendered images.
__construct()
Singleton: protected constructor.
const RENDER_PNG_AS_IMG_FILE
if(!is_array($argv)) $options
setZoomFactor($a_factor)
Set the zoom factor for images.
getCacheSize()
Get the size of the image cache.
Create styles array
The data for the language used.
const RENDER_PNG_AS_FO_FILE
static dirsize($directory)
get size of a directory or a file.
clearCache()
Clear the cache of rendered graphics.
static getInstance()
Singleton: get instance.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
const RENDER_SVG_AS_IMG_EMBED
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getWebspaceDir($mode="filesystem")
get webspace directory
const RENDER_SVG_AS_XML_EMBED
const PURPOSE_DEFERRED_PDF