# -*- CPERL -*-
# /=====================================================================\ #
# |  pifont                                                             | #
# | Implementation for LaTeXML                                          | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
use strict;
use LaTeXML::Package;
package LaTeXML::Package::Pool;

sub dingbat_map {
  my($code)=@_;
  pack('U',$code+0x2700); }

our %PIFONT_MAP = (pzd=>\&dingbat_map);

sub pichar {
  my($pifont,$number)=@_;
  $pifont = ToString($pifont);
  $number = $number->valueOf if ref $number;
  if(my $mapper = $PIFONT_MAP{$pifont}){
    &$mapper($number); }
  else {
    Error(":undefined:$pifont Unknown pi font $pifont");
    "?"; }}

DefConstructor('\Pisymbol{}{Number}', sub {
  my($document,$pifont,$code,%props)=@_;
  $document->openText(pichar($pifont,$code),$props{font}); });

DefConstructor('\pilist@item[]',
	       "<ltx:item xml:id='#id'><ltx:tag>#refnum</ltx:tag>",
	       properties=>sub{
		 (RefStepItemCounter(),
		  refnum=>pichar(LookupValue('pilist_pifont'),
			      LookupValue('pilist_base'))); });
DefConstructor('\piautolist@item[]',
	       "<ltx:item xml:id='#id' refnum='#refnum'><ltx:tag>#refnum</ltx:tag>",
	       properties=>sub{
		 my $lvl = ToString(Tokens(roman(LookupValue('itemlevel'))));
		 (RefStepItemCounter(),
		  refnum=>pichar(LookupValue('pilist_pifont'),
			      LookupValue('pilist_base')
			      +LookupValue('\c@enum'.$lvl)->valueOf)); });
  
DefEnvironment('{Pilist}{}{Number}',
	       "<ltx:itemize xml:id='#id'>#body</ltx:itemize>",
	       beforeDigest=>sub { beginItemize('pilist'); },
	       afterDigestBegin=>sub {
		 AssignValue(pilist_pifont=>ToString($_[1]->getArg(1))); 
		 AssignValue(pilist_base  =>$_[1]->getArg(2)->valueOf); },
	       properties=>sub { RefStepCounter('@itemize'); });

DefEnvironment('{Piautolist}{}{Number}',
	       "<ltx:itemize xml:id='#id'>#body</ltx:itemize>",
	       beforeDigest=>sub { beginItemize('piautolist'); },
	       afterDigestBegin=>sub {
		 AssignValue(pilist_pifont=>ToString($_[1]->getArg(1)));
		 AssignValue(pilist_base  =>$_[1]->getArg(2)->valueOf); },
	       properties=>sub { RefStepCounter('@itemize'); });

# Don't know what to do with these.
DefPrimitive('\Piline{}{Number}',undef);
DefPrimitive('\Pifill{}{Number}',undef);

DefMacro('\ding{}','\Pisymbol{pzd}{#1}');

DefEnvironment('{dinglist}{Number}',
	       "<ltx:itemize xml:id='#id'>#body</ltx:itemize>",
	       beforeDigest=>sub { beginItemize('pilist'); },
	       afterDigestBegin=>sub {
		 AssignValue(pilist_pifont=>'pzd'); 
		 AssignValue(pilist_base  =>$_[1]->getArg(1)->valueOf); },
	       properties=>sub { RefStepCounter('@itemize'); });

DefEnvironment('{dingautolist}{Number}',
	       "<ltx:itemize xml:id='#id'>#body</ltx:itemize>",
	       beforeDigest=>sub { beginItemize('piautolist'); },
	       afterDigestBegin=>sub {
		 AssignValue(pilist_pifont=>'pzd'); 
		 AssignValue(pilist_base  =>$_[1]->getArg(1)->valueOf); },
	       properties=>sub { RefStepCounter('@itemize'); });

# Don't know what to do with these.
DefPrimitive('\dingline{Number}',undef);
DefPrimitive('\dingfill{Number}',undef);



1;
