<?php  if (!defined('PmWiki')) exit();

/*

  Google Fonts - How to use google web fonts on a per page basis

 *****
 *
 *  Copyright 2018 Carlos A. Bonamigo <cabsec.pmwiki@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *****

 Based on Google Web Font service, that uses OpenSource Licenses .

*/


$RecipeInfo['GoogleFonts']['Version']  =  '20180319';


# font names are case sensitive
# check webfont names with quick use
# preferably numeric styles
# format is "Font+Name:numeric-style:subset"
# only one font name value per key
# only one font style per key
# only one subset value per key

SDVA($GoogleWebFonts, array(
  'AdventProNormalLatin' => 'Advent+Pro:400:latin',
  'PlasterNormalLatin' => 'Plaster:400:latin',
  'EmblemaOneNormalLatin' => 'Emblema+One:400:latin',
  'HennyPennyNormalLatin' => 'Henny+Penny:400:latin',
  'ButchermanNormalLatin' => 'Butcherman:400:latin',
  'CreepsterNormalLatin' => 'Creepster:400:latin',
  'DrSugiyamaNormalLatin' => 'Dr+Sugiyama:400:latin',
  'MonotonNormalLatin' => 'Monoton:400:latin',
  'EwertNormalLatin' => 'Ewert:400:latin',
  'EaterNormalLatin' => 'Eater:400:latin',
  'NosiferNormalLatin' => 'Nosifer:400:latin'
));

SDV($GoogleWebFontsUrl, "http://fonts.googleapis.com/css?");  

// with this var you can set fonts to be always loaded
SDVA($LoadWebFonts, (array)"dummy");

Markup('reservewebfonts','directives',
   "/\\(:loadfont\\s+?(.*):\\)/e",
   "ReserveWebFonts('$pagename',PSS('$1'))");

// will be evaled inside a function
$MarkupFrameBase['posteval']['loadwebfonts'] =
                   "\$loadwebfonts = LoadWebFonts(\$pagename);";

function ReserveWebFonts($p,$f){
global  $GoogleWebFonts,
   $LoadWebFonts;

if($p == '') return;

$f = trim($f);
  if(strtolower($f) == 'help' ){
    $h = FmtPageName("$[Webfonts]:",$p);
    $h .= '<ul class="webfonthelp">';
    foreach($GoogleWebFonts as $k => $v){
      $h .= "<li>".$k."</li>";
    }
    $h .= '</ul>';
    return $h;
  }elseif(strpos($f,',',1) !== false){
    $e = explode(',',$f);
    foreach($e as $k => $v){
      $n = trim($v);
      $l[$n] = 1;
    }
  }else{
    $l[trim($f)] = 1;
  }
$LoadWebFonts = array_merge($LoadWebFonts,$l);
}

function LoadWebFonts($p){
global  $HTMLHeaderFmt, $GoogleWebFonts,
   $LoadWebFonts,$GoogleWebFontsUrl;

if($p == '') return;
if(count($LoadWebFonts,0) == 0) return;

$x = $l = $e = $fl = $fk = '';
  foreach($LoadWebFonts as $l => $v){
    $x = trim($l);
    if(@$GoogleWebFonts[$x] != ''){
      $e = explode(':',$GoogleWebFonts[$x]);
      if(count($e) == 3){
        $fl[$e[0]]['family'] = $e[0]; // for easy access
        $fl[$e[0]]['styles'][$e[1]] = 1;
        $fl[$e[0]]['subsets'][$e[2]] = 1;
      }else $fl = ''; 
    }else $fl = '' ;
  }

if(!is_array($fl)) return;
$family = ''; $subset = ''; 
  foreach($fl as $f){
    if(is_array($f['styles'])){
      if(count($fl,0) >= 2 && $family != '') $family .= "|";
      $family .= $f['family'].":";
      foreach($f['styles'] as $st => $v ){
        $family .= $st.",";
      }
      $family = substr($family,0,-1);
    }
    if(is_array($f['subsets'])){
      if(count($fl,0) >= 2 && $subset != '') $subset .= ",";
      foreach($f['subsets'] as $sb => $v ){
        if(strpos($subset,$sb) === false){
          $subset .= $sb.",";
        }
      }
      $subset = substr($subset,0,-1);
    }
  }

$url = $GoogleWebFontsUrl."family=".$family."&amp;subset=".$subset;
$out = "<link href='".$url."' rel='stylesheet' type='text/css'>"; ;
$HTMLHeaderFmt['googlewebfonts'] = $out;