Skip to content

Commit

Permalink
Change handling of languages more generic with additional fonts.
Browse files Browse the repository at this point in the history
  • Loading branch information
srobotta committed Dec 19, 2024
1 parent 71b6f24 commit 902d39b
Show file tree
Hide file tree
Showing 54 changed files with 717 additions and 149 deletions.
11 changes: 10 additions & 1 deletion classes/output/report_download.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use mod_verbalfeedback\api;
use mod_verbalfeedback\model\report as ModelReport;
use mod_verbalfeedback\output\model\report_view_model;
use mod_verbalfeedback\utils\font;
use renderable;
use renderer_base;
use stdClass;
Expand Down Expand Up @@ -60,6 +61,9 @@ class report_download implements renderable, templatable {
/** @var object Moodle user object of the assessed user. */
protected $touser;

/** @var font The font object. */
protected $font;

/**
* The report constructor.
*
Expand All @@ -68,15 +72,17 @@ class report_download implements renderable, templatable {
* @param int $coursestart The course start date
* @param int $courseend The course end date
* @param string $instancename The verbal feedback instance name.
* @param font $font The font object.
* @param int $touser The user this report is being generated for.
*/
public function __construct(ModelReport $report, $coursename, $coursestart, $courseend, $instancename, $touser) {
public function __construct(ModelReport $report, $coursename, $coursestart, $courseend, $instancename, $touser, font $font) {
$this->report = $report;
$this->coursename = $coursename;
$this->coursestart = $coursestart;
$this->courseend = $courseend;
$this->instancename = $instancename;
$this->touser = $touser;
$this->font = $font;
}

/**
Expand All @@ -94,6 +100,9 @@ public function export_for_template(renderer_base $output) {
$data->scales = api::get_scales();
$data->report = new report_view_model($this->report);
$data->lang = current_language();
$data->font_base = $this->font->get_font_base();
$data->font_student = $this->font->get_font_student();
$data->font_teacher = $this->font->get_font_teacher();

// Iterate and drop criteria with weight 0.
// First, let's filter our set of criteria inside the categories.
Expand Down
200 changes: 200 additions & 0 deletions classes/utils/font.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Class for creating graphs. For example, to display on the report.
*
* @package mod_verbalfeedback
* @copyright 2020 Kevin Tippenhauer <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_verbalfeedback\utils;



/**
* Class for handling the font in the PDF. That may vary depending on the teacher,
* student name and the content of the course.
*
* @package mod_verbalfeedback
* @copyright 2024 Stephan Robotta <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class font {

/** @var string Base font name. */
public const FONT_BASE = 'Noto_Sans';
/** @var string Font name for Arabic. */
public const FONT_ARABIC = 'Noto_Naskh_Arabic';
/** @var string Font name for Hebrew. */
public const FONT_HEBREW = 'Noto_Sans_Hebrew';
/** @var string Font name for Japanese. */
public const FONT_JAPANESE = 'Noto_Sans_JP';
/** @var string Font name for Chinese. */
public const FONT_CHINESE = 'Noto_Sans_TC';

/** @var object The report object. */
protected $report;
/** @var object The user object where the report is printed for. */
protected $touser;

/** @var string The selected font for the entire document. */
protected $font_base;
/** @var string The selected font for the students name. */
protected $font_student;
/** @var string The selected font for the teachers name. */
protected $font_teacher;

/**
* Constructor.
*
* @param object $course The report object.
* @param object $touser The user object where the report is printed for.
*/
public function __construct($report, $touser) {
$this->report = $report;
$this->touser = $touser;
}

/**
* Get the base font for the PDF.
*/
public function get_font_base() {
if (!$this->font_base) {
$lang = substr(current_language(), 0, 2);
if ($lang === 'ar') {
$this->font_base = static::FONT_ARABIC;
} else if ($lang === 'he') {
$this->font_base = static::FONT_HEBREW;
} else if ($lang === 'ja') {
$this->font_base = static::FONT_JAPANESE;
} else if ($lang === 'zh') {
$this->font_base = static::FONT_CHINESE;
} else {
$this->font_base = static::FONT_BASE;
}
}
return $this->font_base;
}

/**
* Get the font for the student name.
*
* @return string The font name (one of the class constants)
*/
public function get_font_student() {
if (!$this->font_student) {
$font = $this->eval_string(fullname($this->touser));
$this->font_student = $font === static::FONT_BASE ? 'inherit' : $font;
}
return $this->font_student;
}

/**
* Get the font for the teachers name. Check the first teacher only.
*
* @return string The font name (one of the class constants)
*/
public function get_font_teacher() {
if (!$this->font_teacher) {
$this->font_teacher = 'inherit';
foreach ($this->report->get_from_user_ids() as $fromuserid) {
$fromuser = \core_user::get_user($fromuserid);
$font = $this->eval_string(fullname($fromuser));
$this->font_teacher = $font === static::FONT_BASE ? 'inherit' : $font;
break;
}
}
return $this->font_teacher;
}

/**
* Evaluate the font based on the input string.
*
* @param string $input The input string.
* @return string The font name (one of the class constants)
*/
protected function eval_string(string $input): string {

$n = mb_ord(mb_substr($input, 0, 1));
if ($n >= 0x600 && $n <= 0x6ff) {
return static::FONT_ARABIC;
}
if ($n >= 0x0590 && $n <= 0x05ff) {
return static::FONT_HEBREW;
}
if ($n >= 0x3040 && $n <= 0x30ff) {
return static::FONT_JAPANESE;
}
if ($n >= 0x4e00 && $n <= 0x9fff) {
return static::FONT_CHINESE;
}
return static::FONT_BASE;
}

/**
* Set the required fonts for the PDF.
*
* @param \pdf $pdf The pdf object.
*/
public function set_font_for_pdf(\pdf $pdf) {
global $CFG;

$toload = [$this->get_font_base()];
if ($this->get_font_student() !== 'inherit' && $this->get_font_student() !== $this->get_font_base()) {
$toload[] = $this->get_font_student();
}
if ($this->get_font_teacher() !== 'inherit' && $this->get_font_teacher() !== $this->get_font_base()) {
$toload[] = $this->get_font_teacher();
}
foreach ($toload as $font) {
if ($font === static::FONT_BASE) {
$file = $CFG->dirroot . '/mod/verbalfeedback/fonts/Noto_Sans/notosans';
$pdf->AddFont($font, '', $file . '.php');
$pdf->AddFont($font, 'B', $file . 'b.php');
$pdf->AddFont($font, 'I', $file . 'i.php');
$pdf->AddFont($font, 'BI', $file . 'bi.php');
$pdf->SetFont($font, '', 12);
} elseif ($font === static::FONT_ARABIC) {
$this->set_font_two($pdf, $font, 'notonaskharabic');
} else if ($font === static::FONT_HEBREW) {
$this->set_font_two($pdf, $font, 'notosanshebrew');
} else if ($font === static::FONT_JAPANESE) {
$this->set_font_two($pdf, $font, 'notosansjp');
} else if ($font === static::FONT_CHINESE) {
$this->set_font_two($pdf, $font, 'notosanstc');
}
}
}

/**
* Helper function to set the font for the PDF that only has regular and bold.
*
* @param \pdf $pdf The pdf object.
* @param string $name The name of the font.
* @param string $file The file name prefix of the font.
*/
protected function set_font_two(\pdf $pdf, $name, $file) {
global $CFG;
$fontdir = "{$CFG->dirroot}/mod/verbalfeedback/fonts/{$name}/{$file}";
$pdf->AddFont($name, '', $fontdir . '.php');
$pdf->AddFont($name, 'B', $fontdir . 'b.php');
$pdf->AddFont($name, 'I', $fontdir . '.php');
$pdf->AddFont($name, 'BI', $fontdir . 'b.php');
$pdf->SetFont($name, '', 12);
}
}
93 changes: 93 additions & 0 deletions fonts/Noto_Naskh_Arabic/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/arabic)

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
https://openfontlicense.org


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file added fonts/Noto_Naskh_Arabic/NotoNaskhArabic-Bold.ttf
Binary file not shown.
Binary file not shown.
Binary file added fonts/Noto_Naskh_Arabic/notonaskharabic.ctg.z
Binary file not shown.
15 changes: 15 additions & 0 deletions fonts/Noto_Naskh_Arabic/notonaskharabic.php

Large diffs are not rendered by default.

Binary file added fonts/Noto_Naskh_Arabic/notonaskharabic.z
Binary file not shown.
Binary file added fonts/Noto_Naskh_Arabic/notonaskharabicb.ctg.z
Binary file not shown.
15 changes: 15 additions & 0 deletions fonts/Noto_Naskh_Arabic/notonaskharabicb.php

Large diffs are not rendered by default.

Binary file added fonts/Noto_Naskh_Arabic/notonaskharabicb.z
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 902d39b

Please sign in to comment.