Phoca Font - Here the code to add rules of font assigment
Posted: 13 Sep 2009, 17:48
Phoca Font modifies the font used for a group of tag/id/class, but only a group.
I needed to modify more groups with different fonts (pages general font and another font for articles title)
I modified php code to have more rules of font assignment.
These are the modifications of the code:
New file [...]/administrator/components/com_phocafontconfig.xml
New file [...]/plugins/system/phocafont.php
These modifications implement four rules of font assignment but it is possible to add more rules in the same way.
It works great. I like seeing my pages with my fonts with the same aspect in all browsers.
Phoca Font is really a great component
I Hope it could be useful for anybody, Enjoy
I needed to modify more groups with different fonts (pages general font and another font for articles title)
I modified php code to have more rules of font assignment.
These are the modifications of the code:
- Modified component admin side to request further parameters (file [...]/administrator/components/com_phocafontconfig.xml)
- Modified plugin site side (file [...]/plugins/system/phocafont.php - rows 57-73 removed and changed with new code)
New file [...]/administrator/components/com_phocafontconfig.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<config>
<params addpath="/administrator/components/com_phocafont/elements">
<param name="@phocahead" type="phocahead" default="GENERAL SETTINGS LABEL" />
<param type="spacer" default="<b>Base Rules - Use Predefinited Font</b>" />
<param name="tag_id_class" default="body" size="30" type="phocatext" label="Tag, Id or Class" description="Tag, Id or Class Desc" />
<param name="menu_selection" default="" size="30" type="phocatext" label="Menu Selection" description="Menu Selection Desc" />
<param name="font_size" default="" size="30" type="phocatext" label="Font Size" description="Font Size Desc" />
<param type="spacer" default="<b>New Rules - Use Selected Font</b>" />
<param name="fonts_02" type="sql" default="1" label="Fonts 02" query="SELECT id AS value, title AS fonts_02 FROM #__phocafont_font" />
<param name="tag_id_class_02" default="" size="30" type="phocatext" label="Tag, Id or Class 02" description="Tag, Id or Class Rule 02" />
<param name="menu_selection_02" default="" size="30" type="phocatext" label="Menu Selection 02" description="Menu Selection Rule 02" />
<param name="font_size_02" default="" size="30" type="phocatext" label="Font Size 02" description="Font Size Rule 02" />
<param type="spacer" default="<b>New Rules - Use Selected Font</b>" />
<param name="fonts_03" type="sql" default="1" label="Fonts 03" query="SELECT id AS value, title AS fonts_03 FROM #__phocafont_font" />
<param name="tag_id_class_03" default="" size="30" type="phocatext" label="Tag, Id or Class 03" description="Tag, Id or Class Rule 03" />
<param name="menu_selection_03" default="" size="30" type="phocatext" label="Menu Selection 03" description="Menu Selection Rule 03" />
<param name="font_size_03" default="" size="30" type="phocatext" label="Font Size 03" description="Font Size Rule 03" />
<param type="spacer" default="<b>New Rules - Use Selected Font</b>" />
<param name="fonts_04" type="sql" default="1" label="Fonts 04" query="SELECT id AS value, title AS fonts_04 FROM #__phocafont_font" />
<param name="tag_id_class_04" default="" size="30" type="phocatext" label="Tag, Id or Class 04" description="Tag, Id or Class Rule 04" />
<param name="menu_selection_04" default="" size="30" type="phocatext" label="Menu Selection 04" description="Menu Selection Rule 04" />
<param name="font_size_04" default="" size="30" type="phocatext" label="Font Size 04" description="Font Size Rule 04" />
</params>
</config>
Code: Select all
<?php
/*
* @package Joomla 1.5
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*
* @plugin Phoca Plugin
* @copyright Copyright (C) Jan Pavelka www.phoca.cz
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin' );
jimport( 'joomla.application.component.helper' );
class plgSystemPhocaFont extends JPlugin
{
function plgSystemPhocaFont(& $subject, $config) {
parent::__construct($subject, $config);
}
function onAfterRender() {
global $mainframe, $database;
$document =& JFactory::getDocument();
$doctype = $document->getType();
$db = &JFactory::getDBO();
$app =& JFactory::getApplication();
$component = 'com_phocafont';
$tmpl = array();
if (!JComponentHelper::isEnabled($component, true)) {
JText::_('Phoca Font Plugin requires Phoca Font Component');
return true;
}
if($app->getName() != 'site') {
return true;
}
if ( $doctype !== 'html' ){
return true;
}
$table =& JTable::getInstance('component');
$table->loadByOption( $component );
$paramsC = new JParameter( $table->params );
// Parameters Component
$tmpl['tagidclass'] = $paramsC->get('tag_id_class', 'body');
$tmpl['fontsize'] = $paramsC->get('font_size', '');
$tmpl['menuselection'] = $paramsC->get('menu_selection', '');
// ***** START UPGRADE by Tardis
//
// Scope Upgrade: add the possibility to set more rules of Font Assignment, by one to four
/* Old Code Removed
// Itemid check - - - - -
$ItemId = JRequest::getVar('Itemid', 1, 'get', 'int');
if ($tmpl['menuselection'] != '') {
$tmpl['menuselectionarray'] = explode(',', $tmpl['menuselection']);
}
if (!empty($tmpl['menuselectionarray'])) {
$key = array_search($ItemId, $tmpl['menuselectionarray']);
if ($key === false) {
return true;
}
}
// - - - - - - - - - - -
$where = array();
$where[] = 'a.published = 1';
$where[] = 'a.defaultfont = 1';
*/
/* New Code
1) Create the new function "onAfterRenderCssRulesWrite" with old css string creating code - Parameters: $db and $tmpl, needed by code, and $idAtt, if $idAtt is empty the function uses the Predefined Font, otherwise the function uses $idAtt in Sql Select to locate the Font in the Db Table
2) Add a call to "onAfterRenderCssRulesWrite" to realize the original work
3) Add three new calls to "onAfterRenderCssRulesWrite" for the new three Rules
4) Add, to exising check, a bypass if tagidclass is empty in "onAfterRenderCssRulesWrite"
*/
// Base Rule
$this->onAfterRenderCssRulesWrite($db , $tmpl , '') ;
// Rule 02
// Parameters Component
$tmpl['idfont'] = $paramsC->get('fonts_02', '');
$tmpl['tagidclass'] = $paramsC->get('tag_id_class_02', '');
$tmpl['fontsize'] = $paramsC->get('font_size_02', '');
$tmpl['menuselection'] = $paramsC->get('menu_selection_02', '');
$this->onAfterRenderCssRulesWrite($db , $tmpl , $tmpl['idfont']) ;
// Rule 03
// Parameters Component
$tmpl['idfont'] = $paramsC->get('fonts_03', '');
$tmpl['tagidclass'] = $paramsC->get('tag_id_class_03', '');
$tmpl['fontsize'] = $paramsC->get('font_size_03', '');
$tmpl['menuselection'] = $paramsC->get('menu_selection_03', '');
$this->onAfterRenderCssRulesWrite($db , $tmpl , $tmpl['idfont']) ;
// Rule 04
// Parameters Component
$tmpl['idfont'] = $paramsC->get('fonts_04', '');
$tmpl['tagidclass'] = $paramsC->get('tag_id_class_04', '');
$tmpl['fontsize'] = $paramsC->get('font_size_04', '');
$tmpl['menuselection'] = $paramsC->get('menu_selection_04', '');
$this->onAfterRenderCssRulesWrite($db , $tmpl , $tmpl['idfont']) ;
return true ;
}
function onAfterRenderCssRulesWrite($db , $tmpl , $idAtt) {
if (empty($tmpl['tagidclass'])) {
return true ;
}
// Itemid check - - - - -
$ItemId = JRequest::getVar('Itemid', 1, 'get', 'int');
if ($tmpl['menuselection'] != '') {
$tmpl['menuselectionarray'] = explode(',', $tmpl['menuselection']);
}
if (!empty($tmpl['menuselectionarray'])) {
$key = array_search($ItemId, $tmpl['menuselectionarray']);
if ($key === false) {
return true;
}
}
$where = array();
$where[] = 'a.published = 1';
if (empty($idAtt)) {
$where[] = 'a.defaultfont = 1';
}
else
{
$where[] = 'a.id = '.$idAtt;
}
// ***** END UPDATE by Tardis
$where = ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );
$query = 'SELECT a.*'
.' FROM #__phocafont_font AS a '
. $where;
$db->setQuery( $query );
$fontData = $db->loadObject();
$css = '';
$cssIe = '';
if (isset($fontData) && !empty($fontData)) {
if(isset($fontData->xmlfile) && $fontData->xmlfile !=''
&& isset($fontData->title) && $fontData->title !='') {
$linkFont = JURI::base(true).'/components/com_phocafont/fonts/';
$linkFontAbs = JPATH_ROOT . DS . 'components' . DS .'com_phocafont'. DS .'fonts' . DS ;
jimport( 'joomla.filesystem.file' );
// Format
$format = '';
if (isset($fontData->format) && $fontData->format !='') {
$format = 'format("'.$fontData->format.'")';
}
// Regular
if(isset($fontData->regular) && $fontData->regular !='') {
$cssR='@font-face {'."\n";
$cssR.=' font-family: '.$fontData->title.';'."\n";
$cssR.=' font-style: normal;'."\n";
$cssR.=' font-weight: normal;'."\n";
$cssR.=' font-stretch: normal;'."\n";
$cssR.=' font-stretch: normal;'."\n";
$cssR.=' src: url("'.$linkFont.$fontData->regular.'") '.$format.';'."\n";
$cssR.=' }';
$css .= $cssR;
// IE - - - - -
$fontData->regular_eot = str_replace ('ttf', 'eot', $fontData->regular);
$fontData->regular_eot = str_replace ('otf', 'eot', $fontData->regular_eot);
if (JFile::exists($linkFontAbs. $fontData->regular_eot)) {
$cssIe .= $cssR;
}
// - - - - - -
}
// Bold
if(isset($fontData->bold) && $fontData->bold !='') {
$cssB='@font-face {'."\n";
$cssB.=' font-family: '.$fontData->title.';'."\n";
$cssB.=' font-style: normal;'."\n";
$cssB.=' font-weight: bold;'."\n";
$cssB.=' font-stretch: normal;'."\n";
$cssB.=' src: url("'.$linkFont.$fontData->bold.'") '.$format.';'."\n";
$cssB.=' }';
$css .= $cssB;
// IE - - - - -
$fontData->bold_eot = str_replace ('ttf', 'eot', $fontData->bold);
$fontData->bold_eot = str_replace ('otf', 'eot', $fontData->bold_eot);
if (JFile::exists($linkFontAbs. $fontData->bold_eot)) {
$cssIe .= $cssB;
}
// - - - - - -
}
// Italic
if(isset($fontData->italic) && $fontData->italic !='') {
$cssI='@font-face {'."\n";
$cssI.=' font-family: '.$fontData->title.';'."\n";
$cssI.=' font-style: italic;'."\n";
$cssI.=' font-weight: normal;'."\n";
$cssI.=' font-stretch: normal;'."\n";
$cssI.=' src: url("'.$linkFont.$fontData->italic.'") '.$format.';'."\n";
$cssI.=' }';
$css .= $cssI;
// IE - - - - -
$fontData->italic_eot = str_replace ('ttf', 'eot', $fontData->italic);
$fontData->italic_eot = str_replace ('otf', 'eot', $fontData->italic_eot);
if (JFile::exists($linkFontAbs. $fontData->italic_eot)) {
$cssIe .= $cssI;
}
// - - - - - -
}
// Bold Italic
if(isset($fontData->bolditalic) && $fontData->bolditalic !='') {
$cssBI='@font-face {'."\n";
$cssBI.=' font-family: '.$fontData->title.';'."\n";
$cssBI.=' font-style: italic;'."\n";
$cssBI.=' font-weight: bold;'."\n";
$cssBI.=' font-stretch: normal;'."\n";
$cssBI.=' src: url("'.$linkFont.$fontData->italic.'") '.$format.';'."\n";
$cssBI.=' }';
$css .= $cssBI;
// IE - - - - -
$fontData->bolditalic_eot = str_replace ('ttf', 'eot', $fontData->bolditalic);
$fontData->bolditalic_eot = str_replace ('otf', 'eot', $fontData->bolditalic_eot);
if (JFile::exists($linkFontAbs. $fontData->bolditalic_eot)) {
$cssIe .= $cssBI;
}
// - - - - - -
}
// Condensed
if(isset($fontData->condensed) && $fontData->condensed !='') {
$cssC='@font-face {'."\n";
$cssC.=' font-family: '.$fontData->title.';'."\n";
$cssC.=' font-style: normal;'."\n";
$cssC.=' font-weight: normal;'."\n";
$cssC.=' font-stretch: condensed;'."\n";
$cssC.=' src: url("'.$linkFont.$fontData->condensed.'") '.$format.';'."\n";
$cssC.=' }';
$css .= $cssC;
// IE - - - - -
$fontData->condensed_eot = str_replace ('ttf', 'eot', $fontData->condensed);
$fontData->condensed_eot = str_replace ('otf', 'eot', $fontData->condensed_eot);
if (JFile::exists($linkFontAbs. $fontData->condensed_eot)) {
$cssIe .= $cssC;
}
// - - - - - -
}
// Condensed Bold
if(isset($fontData->condensedbold) && $fontData->condensedbold !='') {
$cssCB='@font-face {'."\n";
$cssCB.=' font-family: '.$fontData->title.';'."\n";
$cssCB.=' font-style: normal;'."\n";
$cssCB.=' font-weight: bold;'."\n";
$cssCB.=' font-stretch: condensed;'."\n";
$cssCB.=' src: url("'.$linkFont.$fontData->condensedbold.'") '.$format.';'."\n";
$cssCB.=' }';
$css .= $cssCB;
// IE - - - - -
$fontData->condensedbold_eot = str_replace ('ttf', 'eot', $fontData->condensedbold);
$fontData->condensedbold_eot = str_replace ('otf', 'eot', $fontData->condensedbold_eot);
if (JFile::exists($linkFontAbs. $fontData->condensedbold_eot)) {
$cssIe .= $cssCB;
}
// - - - - - -
}
// Condensed Italic
if(isset($fontData->condenseditalic) && $fontData->condenseditalic !='') {
$cssCI='@font-face {'."\n";
$cssCI.=' font-family: '.$fontData->title.';'."\n";
$cssCI.=' font-style: italic;'."\n";
$cssCI.=' font-weight: normal;'."\n";
$cssCI.=' font-stretch: condensed;'."\n";
$cssCI.=' src: url("'.$linkFont.$fontData->condenseditalic.'") '.$format.';'."\n";
$cssCI.=' }';
$css .= $cssCI;
// IE - - - - -
$fontData->condenseditalic_eot = str_replace ('ttf', 'eot', $fontData->condenseditalic);
$fontData->condenseditalic_eot = str_replace ('otf', 'eot', $fontData->condenseditalic_eot);
if (JFile::exists($linkFontAbs. $fontData->condenseditalic_eot)) {
$cssIe .= $cssCI;
}
// - - - - - -
}
// Condensed Bold Italic
if(isset($fontData->condensedbolditalic) && $fontData->condensedbolditalic !='') {
$cssCBI='@font-face {'."\n";
$cssCBI.=' font-family: '.$fontData->title.';'."\n";
$cssCBI.=' font-style: italic;'."\n";
$cssCBI.=' font-weight: bold;'."\n";
$cssCBI.=' font-stretch: condensed;'."\n";
$cssCBI.=' src: url("'.$linkFont.$fontData->condensedbolditalic.'") '.$format.';'."\n";
$cssCBI.=' }';
$css .= $cssCBI;
// IE - - - - -
$fontData->condensedbolditalic_eot = str_replace ('ttf', 'eot', $fontData->condensedbolditalic);
$fontData->condensedbolditalic_eot = str_replace ('otf', 'eot', $fontData->condensedbolditalic_eot);
if (JFile::exists($linkFontAbs. $fontData->condensedbolditalic_eot)) {
$cssIe .= $cssCBI;
}
// - - - - - -
}
$alternative = '';
if (isset($fontData->alternative) && $fontData->alternative !='') {
$alternative = ', '.$fontData->alternative;
}
$cssFontSize = '';
if($tmpl['fontsize'] !='') {
$cssFontSize = ' font-size: '.$tmpl['fontsize'].';'."\n";
}
$css .= "\n" . $tmpl['tagidclass'] .' { font-family: "'.$fontData->title.'"'.$alternative.';';
$css .= "\n" . $cssFontSize;
$css .= '}';
if ($cssIe != '') {
$cssIe .= "\n" . $tmpl['tagidclass'] .' { font-family: "'.$fontData->title.'"'.$alternative.';';
$cssIe .= "\n" . $cssFontSize;
$cssIe .= '}';
}
//$document->addStyleDeclaration($css);
if ($cssIe != '') {
$cssIe = str_replace('format("truetype")', '', $cssIe);
$cssIe = str_replace('format("opentype")', '', $cssIe);
$cssIe = str_replace ('ttf', 'eot', $cssIe);
$cssIe = str_replace ('otf', 'eot', $cssIe);
$cssOutput = "\n\n" . '<!--[if IE]>' . "\n"
.'<style type="text/css">' . "\n" . $cssIe . "\n" . '</style>'
.'<![endif]-->'. "\n"
//.'<![if !IE]>'
.'<!--[if !IE]>-->' . "\n"
.'<style type="text/css">' . "\n" . $css . "\n" . '</style>'
//.'<![endif]>';
.'<!--<![endif]-->'."\n";
} else {
$cssOutput = '<style type="text/css">' . "\n" . $css . "\n" . '</style>';
}
$bodySite = JResponse::getBody();
$bodySite = str_replace('</head>', $cssOutput .'</head>', $bodySite);
JResponse::setBody($bodySite);
}
}
return true;
}
}
It works great. I like seeing my pages with my fonts with the same aspect in all browsers.
Phoca Font is really a great component
I Hope it could be useful for anybody, Enjoy