| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Font Model |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPL v2 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 0.00.01 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Model_E2pdf_Font extends Model_E2pdf_Model { |
| 17 |
|
| 18 |
protected $text; |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize functions |
| 22 |
*/ |
| 23 |
function __construct() { |
| 24 |
parent::__construct(); |
| 25 |
} |
| 26 |
|
| 27 |
public function get_font_info($font = false, $key = false, $font_path = false) { |
| 28 |
|
| 29 |
$font_tags = array(); |
| 30 |
|
| 31 |
if (!$font_path) { |
| 32 |
$font_path = $this->helper->get("fonts_dir") . $font; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
if ($font_path && file_exists($font_path) && filesize($font_path) > 0) { |
| 37 |
|
| 38 |
$fd = fopen($font_path, "r"); |
| 39 |
$this->text = fread($fd, filesize($font_path)); |
| 40 |
fclose($fd); |
| 41 |
|
| 42 |
$number_of_tables = hexdec($this->dec2ord($this->text[4]) . $this->dec2ord($this->text[5])); |
| 43 |
|
| 44 |
for ($i = 0; $i < $number_of_tables; $i++) { |
| 45 |
$tag = $this->text[12 + $i * 16] . $this->text[12 + $i * 16 + 1] . $this->text[12 + $i * 16 + 2] . $this->text[12 + $i * 16 + 3]; |
| 46 |
|
| 47 |
if ($tag == 'name') { |
| 48 |
$this->ntOffset = hexdec( |
| 49 |
$this->dec2ord($this->text[12 + $i * 16 + 8]) . $this->dec2ord($this->text[12 + $i * 16 + 8 + 1]) . |
| 50 |
$this->dec2ord($this->text[12 + $i * 16 + 8 + 2]) . $this->dec2ord($this->text[12 + $i * 16 + 8 + 3])); |
| 51 |
|
| 52 |
$offset_storage_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 4]) . $this->dec2ord($this->text[$this->ntOffset + 5])); |
| 53 |
$number_name_records_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 2]) . $this->dec2ord($this->text[$this->ntOffset + 3])); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
$storage_dec = $offset_storage_dec + $this->ntOffset; |
| 58 |
$storage_hex = strtoupper(dechex($storage_dec)); |
| 59 |
|
| 60 |
for ($j = 0; $j < $number_name_records_dec; $j++) { |
| 61 |
$platform_id_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 0]) . $this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 1])); |
| 62 |
$name_id_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 6]) . $this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 7])); |
| 63 |
$string_length_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 8]) . $this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 9])); |
| 64 |
$string_offset_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 10]) . $this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 11])); |
| 65 |
|
| 66 |
if (!empty($name_id_dec) and empty($font_tags[$name_id_dec])) { |
| 67 |
for ($l = 0; $l < $string_length_dec; $l++) { |
| 68 |
if (ord($this->text[$storage_dec + $string_offset_dec + $l]) == '0') { |
| 69 |
continue; |
| 70 |
} else { |
| 71 |
|
| 72 |
if (!isset($font_tags[$name_id_dec])) { |
| 73 |
$font_tags[$name_id_dec] = ""; |
| 74 |
} |
| 75 |
|
| 76 |
$font_tags[$name_id_dec] .= ($this->text[$storage_dec + $string_offset_dec + $l]); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if ($key) { |
| 84 |
if (!empty($font_tags) && isset($font_tags[$key])) { |
| 85 |
return $font_tags[$key]; |
| 86 |
} else { |
| 87 |
return false; |
| 88 |
} |
| 89 |
} else { |
| 90 |
return $font_tags; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public function get_font($font) { |
| 95 |
$font_path = $this->helper->get('fonts_dir') . $font; |
| 96 |
|
| 97 |
if (file_exists($font_path)) { |
| 98 |
return base64_encode(file_get_contents($font_path)); |
| 99 |
} |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
public function get_fonts() { |
| 104 |
|
| 105 |
$fonts = array(); |
| 106 |
$files = glob($this->helper->get('fonts_dir') . "*"); |
| 107 |
if ($files) { |
| 108 |
foreach ($files as $key => $value) { |
| 109 |
if (pathinfo($value, PATHINFO_EXTENSION) == 'ttf') { |
| 110 |
$font_info = $this->get_font_info(basename($value), 4); |
| 111 |
if ($font_info) { |
| 112 |
$fonts[basename($value)] = $font_info; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
return $fonts; |
| 118 |
} |
| 119 |
|
| 120 |
public function get_font_path($font) { |
| 121 |
$fonts = $this->get_fonts(); |
| 122 |
$c_font = array_search($font, $fonts); |
| 123 |
|
| 124 |
if ($c_font) { |
| 125 |
return $this->helper->get('fonts_dir') . $c_font; |
| 126 |
} |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
public function delete_font($font) { |
| 131 |
$font_path = $this->helper->get('fonts_dir') . $font; |
| 132 |
|
| 133 |
if (file_exists($font_path)) { |
| 134 |
unlink($font_path); |
| 135 |
return true; |
| 136 |
} |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
function get_element_fonts($el_value, $all_fonts) { |
| 141 |
$fonts = array(); |
| 142 |
if ($el_value['type'] == 'e2pdf-html' && isset($el_value['value']) && $el_value['value']) { |
| 143 |
preg_match_all('/font-family:[\s]+?"(.*?)";/', htmlspecialchars_decode($el_value['value']), $matches); |
| 144 |
if (isset($matches[1]) && !empty($matches[1])) { |
| 145 |
foreach ($matches[1] as $font_key => $font_value) { |
| 146 |
$exist = array_search($font_value, $fonts); |
| 147 |
if (!$exist) { |
| 148 |
$c_font = array_search($font_value, $all_fonts); |
| 149 |
if ($c_font) { |
| 150 |
$fonts[$c_font] = $font_value; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (isset($el_value['properties']['text_font']) && $el_value['properties']['text_font']) { |
| 158 |
$font_value = $el_value['properties']['text_font']; |
| 159 |
$exist = array_search($font_value, $fonts); |
| 160 |
if (!$exist) { |
| 161 |
$c_font = array_search($font_value, $all_fonts); |
| 162 |
if ($c_font) { |
| 163 |
$fonts[$c_font] = $font_value; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
//action fonts |
| 169 |
if (isset($el_value['actions']) && !empty($el_value['actions'])) { |
| 170 |
foreach ($el_value['actions'] as $action) { |
| 171 |
if (isset($action['property']) && $action['property'] == 'text_font' && isset($action['change']) && $action['change']) { |
| 172 |
$font_value = $action['change']; |
| 173 |
$exist = array_search($font_value, $fonts); |
| 174 |
if (!$exist) { |
| 175 |
$c_font = array_search($font_value, $all_fonts); |
| 176 |
if ($c_font) { |
| 177 |
$fonts[$c_font] = $font_value; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if ($el_value['type'] == 'e2pdf-html' && isset($action['property']) && $action['property'] == 'value' && isset($action['change']) && $action['change']) { |
| 183 |
preg_match_all('/font-family:[\s]+?"(.*?)";/', htmlspecialchars_decode($action['change']), $matches); |
| 184 |
if (isset($matches[1]) && !empty($matches[1])) { |
| 185 |
foreach ($matches[1] as $font_key => $font_value) { |
| 186 |
$exist = array_search($font_value, $fonts); |
| 187 |
if (!$exist) { |
| 188 |
$c_font = array_search($font_value, $all_fonts); |
| 189 |
if ($c_font) { |
| 190 |
$fonts[$c_font] = $font_value; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return $fonts; |
| 200 |
} |
| 201 |
|
| 202 |
protected function dec2ord($dec) { |
| 203 |
return $this->dec2hex(ord($dec)); |
| 204 |
} |
| 205 |
|
| 206 |
protected function dec2hex($dec) { |
| 207 |
return str_repeat('0', 2 - strlen(($hex = strtoupper(dechex($dec))))) . $hex; |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|