Merge branch 'wip_MDL-47333_m28_fonts' of https://github.com/skodak/moodle

This commit is contained in:
Dan Poltawski 2014-09-29 10:42:50 +01:00
commit 156cd01e7b
2 changed files with 156 additions and 73 deletions

View File

@ -22,12 +22,29 @@
* *
* The default location for fonts that are included with TCPDF is * The default location for fonts that are included with TCPDF is
* lib/tcpdf/fonts/. If PDF_CUSTOM_FONT_PATH exists, this directory * lib/tcpdf/fonts/. If PDF_CUSTOM_FONT_PATH exists, this directory
* will be used instead of lib/tcpdf/fonts/. If there is only one font * will be used instead of lib/tcpdf/fonts/, the default location is
* present in PDF_CUSTOM_FONT_PATH, the font is used as the default * $CFG->dataroot.'/fonts/'.
* font.
* *
* See lib/tcpdf/fonts/README for details on how to convert fonts for use * You should always copy all fonts from lib/tcpdf/fonts/ to your
* with TCPDF. * PDF_CUSTOM_FONT_PATH and then add extra fonts. Alternatively
* you may download all TCPDF fonts from http://www.tcpdf.org/download.php
* and extract them to PDF_CUSTOM_FONT_PATH directory.
*
* You can specify your own default font family in config.php
* by defining PDF_DEFAULT_FONT constant there.
*
* If you want to add True Type fonts such as "Arial Unicode MS",
* you need to create a simple script and then execute it, it should add
* new file to your fonts directory:
* <code>
* <?php
* require('config.php');
* require_once($CFG->libdir . '/pdflib.php');
* TCPDF_FONTS::addTTFfont('/full_path_to/ARIALUNI.TTF', 'TrueTypeUnicode');
* </code>
* This script will convert the TTF file to format compatible with TCPDF.
*
* Please note you need to have appropriate license to use the font files on your server!
* *
* Example usage: * Example usage:
* <code> * <code>
@ -46,26 +63,71 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
/** defines the site-specific location of fonts */ if (!defined('PDF_CUSTOM_FONT_PATH')) {
define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/'); /** Defines the site-specific location of fonts. */
define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/');
}
/** default font to be used if there are more of them available */ if (!defined('PDF_DEFAULT_FONT')) {
define('PDF_DEFAULT_FONT', 'FreeSerif'); /** Default font to be used. */
define('PDF_DEFAULT_FONT', 'FreeSerif');
}
/** tell tcpdf it is configured here instead of in its own config file */ /** tell tcpdf it is configured here instead of in its own config file */
define('K_TCPDF_EXTERNAL_CONFIG', 1); define('K_TCPDF_EXTERNAL_CONFIG', 1);
// The configuration constants needed by tcpdf follow // The configuration constants needed by tcpdf follow
/**
* Init K_PATH_FONTS and PDF_FONT_NAME_MAIN constant.
*
* Unfortunately this hack is necessary because the constants need
* to be defined before inclusion of the tcpdf.php file.
*/
function tcpdf_init_k_font_path() {
global $CFG;
$defaultfonts = $CFG->dirroot.'/lib/tcpdf/fonts/';
if (!defined('K_PATH_FONTS')) {
if (is_dir(PDF_CUSTOM_FONT_PATH)) {
// NOTE:
// There used to be an option to have just one file and having it set as default
// but that does not make sense any more because add-ons using standard fonts
// would fail very badly, also font families consist of multiple php files for
// regular, bold, italic, etc.
// Check for some standard font files if present and if not do not use the custom path.
$somestandardfiles = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats', 'freeserif', 'freesans');
$missing = false;
foreach ($somestandardfiles as $file) {
if (!file_exists(PDF_CUSTOM_FONT_PATH . $file . '.php')) {
$missing = true;
break;
}
}
if ($missing) {
define('K_PATH_FONTS', $defaultfonts);
} else {
define('K_PATH_FONTS', PDF_CUSTOM_FONT_PATH);
}
} else {
define('K_PATH_FONTS', $defaultfonts);
}
}
if (!defined('PDF_FONT_NAME_MAIN')) {
define('PDF_FONT_NAME_MAIN', strtolower(PDF_DEFAULT_FONT));
}
}
tcpdf_init_k_font_path();
/** tcpdf installation path */ /** tcpdf installation path */
define('K_PATH_MAIN', $CFG->dirroot.'/lib/tcpdf/'); define('K_PATH_MAIN', $CFG->dirroot.'/lib/tcpdf/');
/** URL path to tcpdf installation folder */ /** URL path to tcpdf installation folder */
define('K_PATH_URL', $CFG->wwwroot . '/lib/tcpdf/'); define('K_PATH_URL', $CFG->wwwroot . '/lib/tcpdf/');
/** path for PDF fonts */
define('K_PATH_FONTS', K_PATH_MAIN . 'fonts/');
/** cache directory for temporary files (full path) */ /** cache directory for temporary files (full path) */
define('K_PATH_CACHE', $CFG->cachedir . '/tcpdf/'); define('K_PATH_CACHE', $CFG->cachedir . '/tcpdf/');
@ -106,20 +168,6 @@ class pdf extends TCPDF {
parent::__construct($orientation, $unit, $format, $unicode, $encoding); parent::__construct($orientation, $unit, $format, $unicode, $encoding);
if (is_dir(PDF_CUSTOM_FONT_PATH)) {
$fontfiles = $this->_getfontfiles(PDF_CUSTOM_FONT_PATH);
if (count($fontfiles) == 1) {
$autofontname = substr($fontfiles[0], 0, -4);
$this->AddFont($autofontname, '', $autofontname.'.php');
$this->SetFont($autofontname);
} else if (count($fontfiles == 0)) {
$this->SetFont(PDF_DEFAULT_FONT);
}
} else {
$this->SetFont(PDF_DEFAULT_FONT);
}
// theses replace the tcpdf's config/lang/ definitions // theses replace the tcpdf's config/lang/ definitions
$this->l['w_page'] = get_string('page'); $this->l['w_page'] = get_string('page');
$this->l['a_meta_language'] = current_language(); $this->l['a_meta_language'] = current_language();
@ -145,36 +193,61 @@ class pdf extends TCPDF {
} }
/** /**
* Return fonts path * Is this font family one of core fonts?
* Overriding TCPDF::_getfontpath() * @param string $fontfamily
* * @return bool
* @global object
*/ */
protected function _getfontpath() { public function is_core_font_family($fontfamily) {
global $CFG; return isset($this->CoreFonts[$fontfamily]);
if (is_dir(PDF_CUSTOM_FONT_PATH)
&& count($this->_getfontfiles(PDF_CUSTOM_FONT_PATH)) > 0) {
$fontpath = PDF_CUSTOM_FONT_PATH;
} else {
$fontpath = K_PATH_FONTS;
}
return $fontpath;
} }
/** /**
* Get the .php files for the fonts * Returns list of font families and types of fonts.
*
* @return array multidimensional array with font families as keys and B, I, BI and N as values.
*/ */
protected function _getfontfiles($fontdir) { public function get_font_families() {
$dirlist = get_directory_list($fontdir); $families = array();
$fontfiles = array(); foreach ($this->fontlist as $font) {
if (strpos($font, 'uni2cid') === 0) {
foreach ($dirlist as $file) { // This is not an font file.
if (substr($file, -4) == '.php') { continue;
array_push($fontfiles, $file); }
if (strpos($font, 'cid0') === 0) {
// These do not seem to work with utf-8, better ignore them for now.
continue;
}
if (substr($font, -2) === 'bi') {
$family = substr($font, 0, -2);
if (in_array($family, $this->fontlist)) {
$families[$family]['BI'] = 'BI';
continue;
} }
} }
return $fontfiles; if (substr($font, -1) === 'i') {
$family = substr($font, 0, -1);
if (in_array($family, $this->fontlist)) {
$families[$family]['I'] = 'I';
continue;
}
}
if (substr($font, -1) === 'b') {
$family = substr($font, 0, -1);
if (in_array($family, $this->fontlist)) {
$families[$family]['B'] = 'B';
continue;
}
}
// This must be a Family or incomplete set of fonts present.
$families[$font]['R'] = 'R';
} }
// Sort everything consistently.
ksort($families);
foreach ($families as $k => $v) {
krsort($families[$k]);
}
return $families;
}
} }

View File

@ -30,7 +30,11 @@ $context = context_system::instance();
require_capability('moodle/site:config', $context); require_capability('moodle/site:config', $context);
$getpdf = optional_param('getpdf', 0, PARAM_INT); $getpdf = optional_param('getpdf', 0, PARAM_INT);
$fontfamily = optional_param('fontfamily', PDF_DEFAULT_FONT, PARAM_ALPHA); // to be configurable $fontfamily = optional_param('fontfamily', PDF_FONT_NAME_MAIN, PARAM_ALPHA); // to be configurable
if (!$fontfamily) {
$fontfamily = PDF_FONT_NAME_MAIN;
}
/** /**
* Extend the standard PDF class to get access to some protected values we want to display * Extend the standard PDF class to get access to some protected values we want to display
@ -39,17 +43,8 @@ $fontfamily = optional_param('fontfamily', PDF_DEFAULT_FONT, PARAM_ALPHA); // t
* @copyright 2009 David Mudrak <david.mudrak@gmail.com> * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class testable_pdf extends pdf {
public function returnFontsList() {
return $this->fontlist;
}
public function _getfontpath() {
return parent::_getfontpath();
}
}
if ($getpdf) { if ($getpdf) {
$doc = new testable_pdf(); $doc = new pdf();
$doc->SetTitle('Moodle PDF library test'); $doc->SetTitle('Moodle PDF library test');
$doc->SetAuthor('Moodle ' . $CFG->release); $doc->SetAuthor('Moodle ' . $CFG->release);
@ -81,48 +76,63 @@ if ($getpdf) {
$c = '<h3>General information</h3>'; $c = '<h3>General information</h3>';
$c .= 'Moodle release: ' . $CFG->release . '<br />'; $c .= 'Moodle release: ' . $CFG->release . '<br />';
$c .= 'PDF producer: TCPDF ' . TCPDF_STATIC::getTCPDFVersion() . ' (http://www.tcpdf.org) <br />'; $c .= 'PDF producer: TCPDF ' . TCPDF_STATIC::getTCPDFVersion() . ' (http://www.tcpdf.org) <br />';
$c .= 'Font of this test page: ' . $fontfamily . '<br />'; $c .= 'Font family used: ' . $fontfamily . '<br />';
$c .= '<h3>Current settings</h3>'; $c .= '<h3>Current settings</h3>';
$c .= '<table border="1" cellspacing="0" cellpadding="1">'; $c .= '<table border="1" cellspacing="0" cellpadding="1">';
foreach (array('K_PATH_MAIN', 'K_PATH_URL', 'K_PATH_FONTS', 'K_PATH_CACHE', 'K_PATH_IMAGES', 'K_BLANK_IMAGE', foreach (array('K_PATH_MAIN', 'K_PATH_URL', 'K_PATH_FONTS', 'PDF_FONT_NAME_MAIN', 'K_PATH_CACHE', 'K_PATH_IMAGES', 'K_BLANK_IMAGE',
'K_CELL_HEIGHT_RATIO', 'K_SMALL_RATIO', 'PDF_CUSTOM_FONT_PATH', 'PDF_DEFAULT_FONT') as $setting) { 'K_CELL_HEIGHT_RATIO', 'K_SMALL_RATIO', 'PDF_CUSTOM_FONT_PATH', 'PDF_DEFAULT_FONT') as $setting) {
if (defined($setting)) { if (defined($setting)) {
$c .= '<tr style="font-size: x-small;"><td>' . $setting . '</td><td>' . constant($setting) . '</td></tr>'; $c .= '<tr style="font-size: x-small;"><td>' . $setting . '</td><td>' . constant($setting) . '</td></tr>';
} }
} }
$c .= '<tr style="font-size: x-small;"><td>Effective font path</td><td>' . $doc->_getfontpath() . '</td></tr>';
$c .= '</table><br />'; $c .= '</table><br />';
$c .= '<h3>Available font files</h3>'; $c .= '<h3>Available font families</h3>';
$fontfiles = $doc->returnFontsList(); $fontfamilies = $doc->get_font_families();
sort($fontfiles); $list = array();
$c .= implode(', ', $fontfiles); foreach ($fontfamilies as $family => $fonts) {
$c .= '<br />'; $f = $family;
$spacer = '';
if ($doc->is_core_font_family($family)) {
$f .= '<sup>*</sup>';
} else {
$spacer = ' ';
}
if (count($fonts) > 1) {
$f .= $spacer . '<i>(' . implode(', ', $fonts) . ')</i>';
}
$list[] = $f;
}
$c .= implode($list, ', ');
$c .= '<p><i><small>Note: * Standard core fonts are not embedded in PDF files, PDF viewers are using local fonts.</small></i></p>';
$c .= '<h3>Installed languages and their alphabets</h3>'; $c .= '<h3>Installed languages and their alphabets</h3>';
$languages = array(); $languages = array();
$langdirs = get_list_of_plugins('lang', '', $CFG->dataroot); $langdirs = get_list_of_plugins('lang', '', $CFG->dataroot);
array_unshift($langdirs, 'en'); array_unshift($langdirs, 'en');
foreach ($langdirs as $langdir) { foreach ($langdirs as $langdir) {
$enlangconfig = $CFG->dirroot . '/lang/en/langconfig.php';
if ('en' == $langdir) { if ('en' == $langdir) {
$langconfig = $CFG->dirroot . '/lang/en/langconfig.php'; $langconfig = $enlangconfig;
} else { } else {
$langconfig = $CFG->dataroot . '/lang/' . $langdir . '/langconfig.php'; $langconfig = $CFG->dataroot . '/lang/' . $langdir . '/langconfig.php';
} }
// Ignore parents here for now.
$string = array();
if (is_readable($langconfig)) { if (is_readable($langconfig)) {
include($langconfig); include($langconfig);
if (is_array($string)) { if (is_array($string)) {
$languages[$langdir] = new stdClass(); $languages[$langdir] = new stdClass();
$languages[$langdir]->langname = isset($string['thislanguage']) ? $string['thislanguage'] : '(unknown)'; $languages[$langdir]->langname = isset($string['thislanguage']) ? $string['thislanguage'] : '(unknown)';
$languages[$langdir]->alphabet = isset($string['alphabet']) ? $string['alphabet'] : '(no alphabet defined)'; $languages[$langdir]->alphabet = isset($string['alphabet']) ? '"' . $string['alphabet'] . '"': '(no alphabet defined)';
} }
} }
} }
$c .= '<dl>'; $c .= '<dl>';
foreach ($languages as $langcode => $language) { foreach ($languages as $langcode => $language) {
$c .= '<dt>' . $language->langname . ' (' . $langcode . ')</dt>'; $c .= '<dt>' . $language->langname . ' (' . $langcode . ')</dt>';
$c .= '<dd>"' . $language->alphabet . '"</dd>'; $c .= '<dd>' . $language->alphabet . '</dd>';
} }
$c .= '</dl>'; $c .= '</dl>';
@ -139,5 +149,5 @@ $PAGE->set_heading('PDF library test');
echo $OUTPUT->header(); echo $OUTPUT->header();
echo $OUTPUT->heading('Press the button to generate test PDF', 2); echo $OUTPUT->heading('Press the button to generate test PDF', 2);
echo $OUTPUT->continue_button(new moodle_url($PAGE->url, array('getpdf' => 1))); echo $OUTPUT->continue_button(new moodle_url($PAGE->url, array('getpdf' => 1, 'fontfamily' => PDF_FONT_NAME_MAIN)));
echo $OUTPUT->footer(); echo $OUTPUT->footer();