Skip to content

Commit

Permalink
Update Custom Local Font
Browse files Browse the repository at this point in the history
  • Loading branch information
sonvnn committed Oct 8, 2022
1 parent 2cd14d0 commit d572854
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions framework/library/astroid/Helper/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ public static function getUploadedFonts($template)
return [];
}
require_once JPATH_LIBRARIES . '/' . 'astroid' . '/' . 'framework' . '/' . 'library' . '/' . 'FontLib' . '/' . 'Autoloader.php';
$template_fonts_path = JPATH_SITE . "/templates/{$template}/fonts";
$template_media_fonts_path = JPATH_SITE . "/media/templates/site/{$template}/fonts";
$template_fonts_path = JPATH_SITE . "/templates/{$template}/fonts";
$template_media_fonts_path = JPATH_SITE . "/media/templates/site/{$template}/fonts";
$template_custom_fonts_path = JPATH_SITE . "/images/{$template}/fonts";
if (!file_exists($template_fonts_path) && !file_exists($template_media_fonts_path)) {
return [];
}
Expand All @@ -133,6 +134,26 @@ public static function getUploadedFonts($template)
}
}
}
if (file_exists($template_custom_fonts_path)) {
foreach (scandir($template_custom_fonts_path) as $font_path) {
if (is_file($template_custom_fonts_path . '/' . $font_path)) {
$pathinfo = pathinfo($template_custom_fonts_path . '/' . $font_path);
if (in_array($pathinfo['extension'], $font_extensions)) {
$font = \FontLib\Font::load($template_custom_fonts_path . '/' . $font_path);
$font->parse();
$fontname = $font->getFontFullName();
$fontid = 'library-font-' . Helper::slugify($fontname);
if (!isset($fonts[$fontid])) {
$fonts[$fontid] = [];
$fonts[$fontid]['id'] = $fontid;
$fonts[$fontid]['name'] = $fontname;
$fonts[$fontid]['files'] = [];
}
$fonts[$fontid]['files'][] = $font_path;
}
}
}
}
Framework::getDebugger()->stop('local-fonts');
return $fonts;
}
Expand Down Expand Up @@ -242,17 +263,23 @@ public static function loadLocalFont($value)
$template = Framework::getTemplate();
$document = Framework::getDocument();
$uploaded_fonts = $template->getFonts();
$template_media_fonts_path = JPATH_SITE . "/media/templates/site/{$template->template}/fonts";
$template_media_fonts_path = JPATH_SITE . "/media/templates/site/{$template->template}/fonts";
$template_custom_fonts_path = JPATH_SITE . "/images/{$template->template}/fonts";
$font_custom_path = \JURI::root() . "images/{$template->template}/fonts/";
if (file_exists($template_media_fonts_path)) {
$font_path = \JURI::root() . "media/templates/site/{$template->template}/fonts/";
$font_path = \JURI::root() . "media/templates/site/{$template->template}/fonts/";
} else {
$font_path = \JURI::root() . "templates/{$template->template}/fonts/";
$font_path = \JURI::root() . "templates/{$template->template}/fonts/";
}
if (isset($uploaded_fonts[$value])) {
$files = $uploaded_fonts[$value]['files'];
$value = $uploaded_fonts[$value]['name'];
foreach ($files as $file) {
$document->addStyleDeclaration('@font-face { font-family: "' . $value . '"; src: url("' . $font_path . $file . '");}');
if (file_exists($template_custom_fonts_path . '/' . $file)) {
$document->addStyleDeclaration('@font-face { font-family: "' . $value . '"; src: url("' . $font_custom_path . $file . '");}');
} else {
$document->addStyleDeclaration('@font-face { font-family: "' . $value . '"; src: url("' . $font_path . $file . '");}');
}
}
}
return $value;
Expand Down

0 comments on commit d572854

Please sign in to comment.