Skip to content

Commit

Permalink
Wrapper for deprecated utf8_encode function in PdfWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
ruff committed Feb 17, 2023
1 parent 2c2e360 commit c7712a4
Showing 1 changed file with 2 additions and 32 deletions.
34 changes: 2 additions & 32 deletions src/ZugferdPdfWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function attach($file, $name = '', $desc = '', $relationship = 'Unspecifi
}
}
if (!$isUTF8) {
$desc = self::utf8_encode($desc);
$desc = mb_convert_encoding($desc, 'UTF-8', mb_list_encodings());
}
if ('' == $mimetype) {
$mimetype = mime_content_type($file);
Expand Down Expand Up @@ -213,7 +213,7 @@ protected function putFileSpecification(array $file_info): void
$this->_put('<<');
$this->_put('/F (' . $this->_escape($file_info['name']) . ')');
$this->_put('/Type /Filespec');
$this->_put('/UF ' . $this->_textstring(self::utf8_encode($file_info['name'])));
$this->_put('/UF ' . $this->_textstring(mb_convert_encoding($file_info['name'], 'UTF-8', mb_list_encodings())));
if ($file_info['relationship']) {
$this->_put('/AFRelationship /' . $file_info['relationship']);
}
Expand Down Expand Up @@ -451,34 +451,4 @@ protected function generateMetadataString($date_type = 'created')

return $metadata_string;
}

/**
* Internal wraooer for deprecated utf8_encode function
*
* @param string $string
* @return string
*/
private static function utf8_encode(string $string): string
{
$string .= $string;
$len = \strlen($string);

for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
switch (true) {
case $string[$i] < "\x80":
$string[$j] = $string[$i];
break;
case $string[$i] < "\xC0":
$string[$j] = "\xC2";
$string[++$j] = $string[$i];
break;
default:
$string[$j] = "\xC3";
$string[++$j] = \chr(\ord($string[$i]) - 64);
break;
}
}

return substr($string, 0, $j);
}
}

0 comments on commit c7712a4

Please sign in to comment.