#211 Use custom fonts if present

This commit is contained in:
Mark Nelson 2018-07-12 12:23:17 +08:00
parent 2500fada80
commit 54212da3d2

View file

@ -102,23 +102,20 @@ class certificate {
public static function get_fonts() { public static function get_fonts() {
global $CFG; global $CFG;
// Array to store the available fonts. require_once($CFG->libdir . '/pdflib.php');
$options = array();
// Location of fonts in Moodle. $arrfonts = [];
$fontdir = "$CFG->dirroot/lib/tcpdf/fonts"; $pdf = new \pdf();
// Check that the directory exists. $fontfamilies = $pdf->get_font_families();
if (file_exists($fontdir)) { foreach ($fontfamilies as $fontfamily => $fontstyles) {
// Get directory contents. foreach ($fontstyles as $fontstyle) {
$fonts = new \DirectoryIterator($fontdir); $fontstyle = strtolower($fontstyle);
// Loop through the font folder. if ($fontstyle == 'r') {
foreach ($fonts as $font) { $filenamewoextension = $fontfamily;
// If it is not a file, or either '.' or '..', or } else {
// the extension is not php, or we can not open file, $filenamewoextension = $fontfamily . $fontstyle;
// skip it.
if (!$font->isFile() || $font->isDot() || ($font->getExtension() != 'php')) {
continue;
} }
$fullpath = \TCPDF_FONTS::_getfontpath() . $filenamewoextension;
// Set the name of the font to null, the include next should then set this // Set the name of the font to null, the include next should then set this
// value, if it is not set then the file does not include the necessary data. // value, if it is not set then the file does not include the necessary data.
$name = null; $name = null;
@ -127,25 +124,24 @@ class certificate {
$displayname = null; $displayname = null;
// Some of the TCPDF files include files that are not present, so we have to // Some of the TCPDF files include files that are not present, so we have to
// suppress warnings, this is the TCPDF libraries fault, grrr. // suppress warnings, this is the TCPDF libraries fault, grrr.
@include("$fontdir/$font"); @include($fullpath . '.php');
// If no $name variable in file, skip it. // If no $name variable in file, skip it.
if (is_null($name)) { if (is_null($name)) {
continue; continue;
} }
// Remove the extension of the ".php" file that contains the font information.
$filename = basename($font, ".php");
// Check if there is no display name to use. // Check if there is no display name to use.
if (is_null($displayname)) { if (is_null($displayname)) {
// Format the font name, so "FontName-Style" becomes "Font Name - Style". // Format the font name, so "FontName-Style" becomes "Font Name - Style".
$displayname = preg_replace("/([a-z])([A-Z])/", "$1 $2", $name); $displayname = preg_replace("/([a-z])([A-Z])/", "$1 $2", $name);
$displayname = preg_replace("/([a-zA-Z])-([a-zA-Z])/", "$1 - $2", $displayname); $displayname = preg_replace("/([a-zA-Z])-([a-zA-Z])/", "$1 - $2", $displayname);
} }
$options[$filename] = $displayname;
}
ksort($options);
}
return $options; $arrfonts[$filenamewoextension] = $displayname;
}
}
ksort($arrfonts);
return $arrfonts;
} }
/** /**