2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Contains class \core\output\icon_system
22 * @copyright 2016 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core\output;
32 * Class allowing different systems for mapping and rendering icons.
34 * Possible icon styles are:
35 * 1. standard - image tags are generated which point to pix icons stored in a plugin pix folder.
36 * 2. fontawesome - font awesome markup is generated with the name of the icon mapped from the moodle icon name.
37 * 3. inline - inline tags are used for svg and png so no separate page requests are made (at the expense of page size).
41 * @copyright 2016 Damyon Wiese
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 abstract class icon_system {
45 const STANDARD = 'standard';
46 const FONTAWESOME = 'fontawesome';
47 const INLINE = 'inline';
48 const SYSTEMS = [ self::STANDARD, self::FONTAWESOME, self::INLINE ];
50 private static $instance = null;
52 protected $system = '';
54 private function __construct($system) {
55 $this->system = $system;
58 public final static function instance($type = null) {
62 if (!empty(self::$instance)) {
63 return self::$instance;
65 $type = $PAGE->theme->get_icon_system();
66 $system = '\\core\\output\\icon_system_' . $type;
67 self::$instance = new $system($type);
68 // Default one is a singleton.
69 return self::$instance;
71 $system = '\\core\\output\\icon_system_' . $type;
73 return new $system($type);
78 * Validate the theme config setting.
80 * @param string $system
83 public final static function is_valid_system($system) {
84 return in_array($system, self::SYSTEMS);
88 * Render the pix icon according to the icon system.
90 * @param renderer_base $output
91 * @param pix_icon $icon
94 public abstract function render_pix_icon(renderer_base $output, pix_icon $icon);
97 * Overridable function to get a mapping of all icons.
98 * Default is to do no mapping.
100 public function get_icon_name_map() {
105 * Overridable function to map the icon name to something else.
106 * Default is to do no mapping. Map is cached in the singleton.
108 public final function remap_icon_name($iconname, $component) {
109 if ($this->map === null) {
110 $this->map = $this->get_icon_name_map();
112 if ($component == null || $component == 'moodle') {
114 } else if ($component != 'theme') {
115 $component = \core_component::normalize_componentname($component);
118 if (isset($this->map[$component . ':' . $iconname])) {
119 return $this->map[$component . ':' . $iconname];