ILIAS  release_8 Revision v8.24
ilMathJax Class Reference

Class for processing of latex formulas This class uses a sigleton pattern to store the rendering purpose during a request. More...

+ Collaboration diagram for ilMathJax:

Public Member Functions

 init (string $a_purpose=self::PURPOSE_BROWSER)
 Initialize the usage for a certain purpose This must be done before any rendering call. More...
 
 setRendering (string $a_rendering)
 Set the image type rendered by the server. More...
 
 setDpi (int $a_dpi)
 Set the dpi of the rendered images. More...
 
 setZoomFactor (float $a_factor)
 Set the zoom factor of the rendered images. More...
 
 includeMathJax (ilGlobalTemplateInterface $a_tpl=null)
 Include the Mathjax javascript(s) in the page template. More...
 
 insertLatexImages (string $a_text, ?string $a_start='[tex]', ?string $a_end='[/tex]')
 Replace all tex code within given start and end delimiters in a text If client-side rendering is enabled, change the start end end delimiters to what Mathjax expects If Server-side rendering is used, replace the whole expression with delimiters by svg or image. More...
 
 getCacheSize ()
 Get the size of the image cache. More...
 
 clearCache ()
 Clear the cache of rendered graphics. More...
 

Static Public Member Functions

static getInstance ()
 Singleton: get instance for use in ILIAS requests with a config loaded from the settings. More...
 
static getIndependent (ilMathJaxConfig $config, ilMathJaxFactory $factory)
 Get an independent instance with a specific config for use in unit tests or on the mathjax settings page Don't use in standard cases! More...
 

Data Fields

const PURPOSE_BROWSER = 'browser'
 
const PURPOSE_EXPORT = 'export'
 
const PURPOSE_PDF = 'pdf'
 
const PURPOSE_DEFERRED_PDF = 'deferred_pdf'
 
const ENGINE_SERVER = 'server'
 
const ENGINE_CLIENT = 'client'
 
const ENGINE_DEFERRED = 'deferred'
 
const ENGINE_NONE = 'none'
 
const RENDER_SVG_AS_XML_EMBED = 'svg_as_xml_embed'
 
const RENDER_SVG_AS_IMG_EMBED = 'svg_as_img_embed'
 
const RENDER_PNG_AS_IMG_EMBED = 'png_as_img_embed'
 
const RENDER_PNG_AS_FO_FILE = 'png_as_fo_file'
 

Protected Member Functions

 __construct (ilMathJaxConfig $config, ilMathJaxFactory $factory)
 Protected constructor to force the use of an initialized instance. More...
 
 setEngine (string $a_engine)
 Set the Rendering engine. More...
 
 renderMathJax (string $a_tex)
 Render image from tex code using the MathJax server. More...
 

Protected Attributes

const OUTPUT_SVG = 'svg'
 
const OUTPUT_PNG = 'png'
 
const DEFAULT_DPI = 150
 
const DEFAULT_ZOOM = 1.0
 
ilMathJaxConfig $config
 
ilMathJaxFactory $factory
 
string $engine
 
string $rendering = self::RENDER_SVG_AS_XML_EMBED
 
string $output = self::OUTPUT_SVG
 
int $dpi
 
float $zoom_factor
 
array $default_server_options
 

Static Protected Attributes

static self $_instance
 

Detailed Description

Class for processing of latex formulas This class uses a sigleton pattern to store the rendering purpose during a request.

The rendering purpose for export or deferred PDF generation must be determined at the beginning of a request. All following calls to convert latex code must use this purpose. Use init() to reset the purpose and all related settings.

Definition at line 29 of file class.ilMathJax.php.

Constructor & Destructor Documentation

◆ __construct()

ilMathJax::__construct ( ilMathJaxConfig  $config,
ilMathJaxFactory  $factory 
)
protected

Protected constructor to force the use of an initialized instance.

Definition at line 113 of file class.ilMathJax.php.

114 {
115 $this->config = $config;
116 $this->factory = $factory;
117 $this->init(self::PURPOSE_BROWSER);
118 }
init(string $a_purpose=self::PURPOSE_BROWSER)
Initialize the usage for a certain purpose This must be done before any rendering call.
ilMathJaxConfig $config
ilMathJaxFactory $factory

References $config, $factory, and init().

+ Here is the call graph for this function:

Member Function Documentation

◆ clearCache()

ilMathJax::clearCache ( )

Clear the cache of rendered graphics.

Definition at line 458 of file class.ilMathJax.php.

458 : void
459 {
460 $image = $this->factory->image('', $this->output, $this->dpi);
461 $image->clearCache();
462 }

◆ getCacheSize()

ilMathJax::getCacheSize ( )

Get the size of the image cache.

Definition at line 450 of file class.ilMathJax.php.

450 : string
451 {
452 return $this->factory->image('', $this->output, $this->dpi)->getCacheSize();
453 }

◆ getIndependent()

static ilMathJax::getIndependent ( ilMathJaxConfig  $config,
ilMathJaxFactory  $factory 
)
static

Get an independent instance with a specific config for use in unit tests or on the mathjax settings page Don't use in standard cases!

Definition at line 138 of file class.ilMathJax.php.

138 : ilMathJax
139 {
140 return new self($config, $factory);
141 }
Class for processing of latex formulas This class uses a sigleton pattern to store the rendering purp...

References $config, and $factory.

Referenced by ilMathJaxSettingsGUI\editSettings(), ilMathJaxTest\testClientSideRendering(), ilMathJaxTest\testInstanceCanBeCreated(), and ilMathJaxTest\testServerSideRendering().

+ Here is the caller graph for this function:

◆ getInstance()

static ilMathJax::getInstance ( )
static

Singleton: get instance for use in ILIAS requests with a config loaded from the settings.

Definition at line 123 of file class.ilMathJax.php.

123 : ilMathJax
124 {
125 if (!isset(self::$_instance)) {
126 // #37803: here we can't use ilSettingsFactory because of race conditions in ilSettingsFactory::settingsFor()
127 $repo = new ilMathJaxConfigSettingsRepository(new ilSetting('MathJax'));
128 self::$_instance = new self($repo->getConfig(), new ilMathJaxFactory());
129 }
130 return self::$_instance;
131 }
Repository for storing and loading the MathJax configuration.
Factory for objects used by ilMathJax.
static self $_instance
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...

References $_instance.

Referenced by ILIAS\Wiki\Export\WikiHtmlExport\buildExportFile(), ilMathJaxSettingsGUI\clearCache(), ilMathJaxSettingsGUI\editSettings(), ILIAS\Services\Export\HTML\Util\exportCOPageFiles(), ilPresentationListTableGUI\fillRow(), ilTermListTableGUI\fillRow(), ilCertificateMathJaxHelper\fillXlsFoContent(), ilAssSelfAssessmentQuestionFormatter\format(), ilForumExportGUI\prepare(), ilTCPDFRenderer\prepareGenerationRequest(), ilWkhtmlToPdfRenderer\prepareGenerationRequest(), ilForum\prepareText(), assQuestionGUI\prepareTextareaOutput(), ilLegacyFormElementsUtil\prepareTextareaOutput(), ilForumExportGUI\printPost(), ilForumExportGUI\printThread(), and ilPageObjectGUI\showPage().

+ Here is the caller graph for this function:

◆ includeMathJax()

ilMathJax::includeMathJax ( ilGlobalTemplateInterface  $a_tpl = null)

Include the Mathjax javascript(s) in the page template.

Definition at line 259 of file class.ilMathJax.php.

259 : ilMathJax
260 {
261 if ($this->config->isClientEnabled()) {
262 $tpl = $a_tpl ?? $this->factory->template();
263
264 if (!empty($this->config->getClintPolyfillUrl())) {
265 $tpl->addJavaScript($this->config->getClintPolyfillUrl());
266 }
267 if (!empty($this->config->getClientScriptUrl())) {
268 $tpl->addJavaScript($this->config->getClientScriptUrl());
269 }
270 }
271
272 return $this;
273 }
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41

References $tpl.

Referenced by init().

+ Here is the caller graph for this function:

◆ init()

ilMathJax::init ( string  $a_purpose = self::PURPOSE_BROWSER)

Initialize the usage for a certain purpose This must be done before any rendering call.

Definition at line 147 of file class.ilMathJax.php.

147 : ilMathJax
148 {
149 // reset the class variables
150 $this->engine = null;
151 $this->rendering = self::RENDER_SVG_AS_XML_EMBED;
152 $this->output = self::OUTPUT_SVG;
153 $this->dpi = self::DEFAULT_DPI;
154 $this->zoom_factor = self::DEFAULT_ZOOM;
155
156 // try the server-side rendering first, set this engine, if possible
157 if ($this->config->isServerEnabled()) {
158 if ($a_purpose === self::PURPOSE_BROWSER && $this->config->isServerForBrowser()) {
159 // delivering svg directly in page may be faster than loading image files
160 $this->setEngine(self::ENGINE_SERVER);
161 $this->setRendering(self::RENDER_SVG_AS_XML_EMBED);
162 } elseif ($a_purpose === self::PURPOSE_EXPORT && $this->config->isServerForExport()) {
163 // offline pages must always embed the svg as image tags
164 // otherwise the html base tag may conflict with references in svg
165 $this->setEngine(self::ENGINE_SERVER);
166 $this->setRendering(self::RENDER_SVG_AS_IMG_EMBED);
167 } elseif ($a_purpose === self::PURPOSE_PDF && $this->config->isServerForPdf()) {
168 // embedded png should work in most pdf engines
169 // details can be set by the rendering engine
170 $this->setEngine(self::ENGINE_SERVER);
171 $this->setRendering(self::RENDER_PNG_AS_IMG_EMBED);
172 } elseif ($a_purpose === self::PURPOSE_DEFERRED_PDF && $this->config->isServerForPdf()) {
173 // final engine and rendering is set before the pdf is created
174 $this->setEngine(self::ENGINE_DEFERRED);
175 }
176 }
177
178 // support client-side rendering if enabled
179 if ($this->config->isClientEnabled()) {
180
181 // included mathjax script may render code which is not found by the server-side rendering
182 // see https://docu.ilias.de/goto_docu_wiki_wpage_5614_1357.html
183 $this->includeMathJax();
184
185 // set engine for client-side rendering, if server is not used for the purpose
186 if (!isset($this->engine)) {
187 $this->setEngine(self::ENGINE_CLIENT);
188 }
189 }
190
191 // no engine available or configured
192 if (!isset($this->engine)) {
193 $this->engine = self::ENGINE_NONE;
194 }
195
196 return $this;
197 }
const OUTPUT_SVG
const ENGINE_NONE
const DEFAULT_DPI
const RENDER_SVG_AS_XML_EMBED
includeMathJax(ilGlobalTemplateInterface $a_tpl=null)
Include the Mathjax javascript(s) in the page template.
setRendering(string $a_rendering)
Set the image type rendered by the server.
const DEFAULT_ZOOM
setEngine(string $a_engine)
Set the Rendering engine.

References DEFAULT_DPI, DEFAULT_ZOOM, ENGINE_NONE, includeMathJax(), OUTPUT_SVG, RENDER_SVG_AS_XML_EMBED, setEngine(), and setRendering().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertLatexImages()

ilMathJax::insertLatexImages ( string  $a_text,
?string  $a_start = '[tex]',
?string  $a_end = '[/tex]' 
)

Replace all tex code within given start and end delimiters in a text If client-side rendering is enabled, change the start end end delimiters to what Mathjax expects If Server-side rendering is used, replace the whole expression with delimiters by svg or image.

Parameters
string$a_texttext to be converted
string | null$a_startstart delimiter to be searched for
string | null$a_endend delimiter to be converted
Returns
string replaced text

Definition at line 284 of file class.ilMathJax.php.

284 : string
285 {
286 // don't change anything if mathjax is not configured
287 if ($this->engine === self::ENGINE_NONE) {
288 return $a_text;
289 }
290
291 // this is a fix for bug5362
292 $a_start = str_replace("\\", "", $a_start ?? '[tex]');
293 $a_end = str_replace("\\", "", $a_end ?? '[/tex]');
294
295 // current position to start the search for delimiters
296 $cpos = 0;
297 // find position of start delimiter
298 while (is_int($spos = ilStr::strIPos($a_text, $a_start, $cpos))) {
299 // find position of end delimiter
300 if (is_int($epos = ilStr::strIPos($a_text, $a_end, $spos + ilStr::strLen($a_start)))) {
301 // extract the tex code inside the delimiters
302 $tex = ilStr::subStr($a_text, $spos + ilStr::strLen($a_start), $epos - $spos - ilStr::strLen($a_start));
303
304 // undo a code protection done by the deferred engine before
305 if (ilStr::subStr($tex, 0, 7) === 'base64:') {
306 $tex = base64_decode(substr($tex, 7));
307 }
308
309 // omit the html newlines added by the ILIAS page editor
310 $tex = str_replace(array('<br>', '<br/>', '<br />'), '', $tex);
311
312 // tex specific replacements
313 $tex = preg_replace("/\\\\([RZN])([^a-zA-Z])/", "\\mathbb{" . "$1" . "}" . "$2", $tex);
314
315 // check, if tags go across div borders
316 if (is_int(ilStr::strIPos($tex, '<div>')) || is_int(ilStr::strIPos($tex, '</div>'))) {
317 // keep the original code including delimiters, continue search behind
318 $cpos = $epos + ilStr::strLen($a_end);
319 } else {
320 switch ($this->engine) {
322 // prepare code for processing in the browser
323 // add necessary html encodings
324 // use the configured mathjax delimiters
325 $tex = str_replace('<', '&lt;', $tex);
326 $replacement = $this->config->getClientLimiterStart() . $tex
327 . $this->config->getClientLimiterEnd();
328 break;
329
331 // apply server-side processing
332 // mathjax-node expects pure tex code
333 // so revert any applied html encoding
334 $tex = html_entity_decode($tex, ENT_QUOTES, 'UTF-8');
335 $replacement = $this->renderMathJax($tex);
336 break;
337
339 // protect code to save it for post production
340 $replacement = '[tex]' . 'base64:' . base64_encode($tex) . '[/tex]';
341 break;
342
343 default:
344 // keep the original
345 $replacement = $tex;
346 break;
347 }
348
349 // replace delimiters and tex code with prepared code or generated image
350 $a_text = ilStr::subStr($a_text, 0, $spos) . $replacement
351 . ilStr::subStr($a_text, $epos + ilStr::strLen($a_end));
352
353 // continue search behind replacement
354 $cpos = $spos + ilStr::strLen($replacement);
355 }
356 } else {
357 // end delimiter position not found => stop search
358 break;
359 }
360
361 if ($cpos >= ilStr::strlen($a_text)) {
362 // current position at the end => stop search
363 break;
364 }
365 }
366 return $a_text;
367 }
renderMathJax(string $a_tex)
Render image from tex code using the MathJax server.
const ENGINE_SERVER
const ENGINE_CLIENT
const ENGINE_DEFERRED
static strIPos(string $a_haystack, string $a_needle, ?int $a_offset=null)
Definition: class.ilStr.php:54
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:24
static strLen(string $a_string)
Definition: class.ilStr.php:63

References ENGINE_CLIENT, ENGINE_DEFERRED, ENGINE_SERVER, renderMathJax(), ilStr\strIPos(), ilStr\strLen(), and ilStr\subStr().

+ Here is the call graph for this function:

◆ renderMathJax()

ilMathJax::renderMathJax ( string  $a_tex)
protected

Render image from tex code using the MathJax server.

Definition at line 372 of file class.ilMathJax.php.

372 : string
373 {
375 $options['math'] = $a_tex;
376 $options['dpi'] = $this->dpi;
377
378 switch ($this->output) {
379 case self::OUTPUT_PNG:
380 $options['svg'] = false;
381 $options['png'] = true;
382 $suffix = ".png";
383 break;
384
385 case self::OUTPUT_SVG:
386 default:
387 $options['svg'] = true;
388 $options['png'] = false;
389 $suffix = ".svg";
390 break;
391 }
392
393 $image = $this->factory->image($a_tex, $this->output, $this->dpi);
394
395 try {
396 if (!$image->exists()) {
397 $server = $this->factory->server($this->config);
398 $image->write($server->call($options));
399 }
400
401 // get the image properties
402 switch ($this->output) {
403 case self::OUTPUT_PNG:
404 [$width, $height] = getimagesize($image->absolutePath());
405 $width = round($width * $this->zoom_factor);
406 $height = round($height * $this->zoom_factor);
407 $mime = 'image/png';
408 break;
409
410 case self::OUTPUT_SVG:
411 default:
412 $svg = simplexml_load_string(file_get_contents($image->absolutePath()));
413 $width = round($svg['width'] * $this->zoom_factor);
414 $height = round($svg['height'] * $this->zoom_factor);
415 $mime = 'image/svg+xml';
416 break;
417 }
418
419 // generate the html code
420 switch ($this->rendering) {
422 $html = $image->read();
423 break;
424
427 $html = '<img src="data:' . $mime . ';base64,'
428 . base64_encode($image->read())
429 . '" style="width:' . $width . '; height:' . $height . ';" />';
430 break;
431
433 $html = '<fo:external-graphic src="' . $image->absolutePath() . '"'
434 . ' content-height="' . $height . 'px" content-width="' . $width . 'px"></fo:external-graphic>';
435 break;
436
437 default:
438 $html = htmlspecialchars($a_tex);
439 break;
440 }
441 return $html;
442 } catch (Exception $e) {
443 return "[TeX rendering failed: " . $e->getMessage() . htmlentities($a_tex) . "]";
444 }
445 }
const RENDER_SVG_AS_IMG_EMBED
const RENDER_PNG_AS_IMG_EMBED
array $default_server_options
const RENDER_PNG_AS_FO_FILE
const OUTPUT_PNG
$server

References $default_server_options, $dpi, Vendor\Package\$e, $server, OUTPUT_PNG, OUTPUT_SVG, RENDER_PNG_AS_FO_FILE, RENDER_PNG_AS_IMG_EMBED, RENDER_SVG_AS_IMG_EMBED, and RENDER_SVG_AS_XML_EMBED.

Referenced by insertLatexImages().

+ Here is the caller graph for this function:

◆ setDpi()

ilMathJax::setDpi ( int  $a_dpi)

Set the dpi of the rendered images.

Definition at line 241 of file class.ilMathJax.php.

241 : ilMathJax
242 {
243 $this->dpi = $a_dpi;
244 return $this;
245 }

◆ setEngine()

ilMathJax::setEngine ( string  $a_engine)
protected

Set the Rendering engine.

Definition at line 202 of file class.ilMathJax.php.

202 : ilMathJax
203 {
204 switch ($a_engine) {
208 $this->engine = $a_engine;
209 break;
210 default:
211 $this->engine = self::ENGINE_NONE;
212 }
213
214 return $this;
215 }

References ENGINE_CLIENT, ENGINE_DEFERRED, ENGINE_NONE, and ENGINE_SERVER.

Referenced by init().

+ Here is the caller graph for this function:

◆ setRendering()

ilMathJax::setRendering ( string  $a_rendering)

Set the image type rendered by the server.

Definition at line 220 of file class.ilMathJax.php.

220 : ilMathJax
221 {
222 switch ($a_rendering) {
225 $this->rendering = $a_rendering;
226 $this->output = self::OUTPUT_SVG;
227 break;
228
231 $this->rendering = $a_rendering;
232 $this->output = self::OUTPUT_PNG;
233 break;
234 }
235 return $this;
236 }

References OUTPUT_PNG, OUTPUT_SVG, RENDER_PNG_AS_FO_FILE, RENDER_PNG_AS_IMG_EMBED, RENDER_SVG_AS_IMG_EMBED, and RENDER_SVG_AS_XML_EMBED.

Referenced by init().

+ Here is the caller graph for this function:

◆ setZoomFactor()

ilMathJax::setZoomFactor ( float  $a_factor)

Set the zoom factor of the rendered images.

Definition at line 250 of file class.ilMathJax.php.

250 : ilMathJax
251 {
252 $this->zoom_factor = $a_factor;
253 return $this;
254 }

Field Documentation

◆ $_instance

self ilMathJax::$_instance
staticprotected

Definition at line 56 of file class.ilMathJax.php.

Referenced by getInstance().

◆ $config

ilMathJaxConfig ilMathJax::$config
protected

Definition at line 61 of file class.ilMathJax.php.

Referenced by __construct(), and getIndependent().

◆ $default_server_options

array ilMathJax::$default_server_options
protected
Initial value:
= array(
"format" => "TeX",
"math" => '',
"svg" => true,
"mml" => false,
"png" => false,
"speakText" => false,
"speakRuleset" => "mathspeak",
"speakStyle" => "default",
"ex" => 6,
"width" => 1000000,
"linebreaks" => false,
)

Definition at line 96 of file class.ilMathJax.php.

Referenced by renderMathJax().

◆ $dpi

int ilMathJax::$dpi
protected

Definition at line 86 of file class.ilMathJax.php.

Referenced by renderMathJax().

◆ $engine

string ilMathJax::$engine
protected

Definition at line 71 of file class.ilMathJax.php.

◆ $factory

ilMathJaxFactory ilMathJax::$factory
protected

Definition at line 66 of file class.ilMathJax.php.

Referenced by __construct(), and getIndependent().

◆ $output

string ilMathJax::$output = self::OUTPUT_SVG
protected

Definition at line 81 of file class.ilMathJax.php.

◆ $rendering

string ilMathJax::$rendering = self::RENDER_SVG_AS_XML_EMBED
protected

Definition at line 76 of file class.ilMathJax.php.

◆ $zoom_factor

float ilMathJax::$zoom_factor
protected

Definition at line 91 of file class.ilMathJax.php.

◆ DEFAULT_DPI

const ilMathJax::DEFAULT_DPI = 150
protected

Definition at line 50 of file class.ilMathJax.php.

Referenced by init().

◆ DEFAULT_ZOOM

const ilMathJax::DEFAULT_ZOOM = 1.0
protected

Definition at line 51 of file class.ilMathJax.php.

Referenced by init().

◆ ENGINE_CLIENT

const ilMathJax::ENGINE_CLIENT = 'client'

Definition at line 38 of file class.ilMathJax.php.

Referenced by insertLatexImages(), and setEngine().

◆ ENGINE_DEFERRED

const ilMathJax::ENGINE_DEFERRED = 'deferred'

Definition at line 39 of file class.ilMathJax.php.

Referenced by insertLatexImages(), and setEngine().

◆ ENGINE_NONE

const ilMathJax::ENGINE_NONE = 'none'

Definition at line 40 of file class.ilMathJax.php.

Referenced by init(), and setEngine().

◆ ENGINE_SERVER

const ilMathJax::ENGINE_SERVER = 'server'

Definition at line 37 of file class.ilMathJax.php.

Referenced by insertLatexImages(), and setEngine().

◆ OUTPUT_PNG

const ilMathJax::OUTPUT_PNG = 'png'
protected

Definition at line 48 of file class.ilMathJax.php.

Referenced by renderMathJax(), and setRendering().

◆ OUTPUT_SVG

const ilMathJax::OUTPUT_SVG = 'svg'
protected

Definition at line 47 of file class.ilMathJax.php.

Referenced by init(), renderMathJax(), and setRendering().

◆ PURPOSE_BROWSER

const ilMathJax::PURPOSE_BROWSER = 'browser'

Definition at line 31 of file class.ilMathJax.php.

Referenced by ilMathJaxSettingsGUI\editSettings().

◆ PURPOSE_DEFERRED_PDF

const ilMathJax::PURPOSE_DEFERRED_PDF = 'deferred_pdf'

Definition at line 34 of file class.ilMathJax.php.

◆ PURPOSE_EXPORT

◆ PURPOSE_PDF

◆ RENDER_PNG_AS_FO_FILE

const ilMathJax::RENDER_PNG_AS_FO_FILE = 'png_as_fo_file'

◆ RENDER_PNG_AS_IMG_EMBED

const ilMathJax::RENDER_PNG_AS_IMG_EMBED = 'png_as_img_embed'

◆ RENDER_SVG_AS_IMG_EMBED

const ilMathJax::RENDER_SVG_AS_IMG_EMBED = 'svg_as_img_embed'

Definition at line 43 of file class.ilMathJax.php.

Referenced by renderMathJax(), and setRendering().

◆ RENDER_SVG_AS_XML_EMBED

const ilMathJax::RENDER_SVG_AS_XML_EMBED = 'svg_as_xml_embed'

The documentation for this class was generated from the following file: