# -*- mode: Perl -*-
# /=====================================================================\ #
# |  nicefrac                                                           | #
# | 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=/ #
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;

#RequirePackage('ifthen');

# Handy for cases where macros want to use math, but track the current text font
# (examples, nicefrac, units...)
DefPrimitiveI('\nf@mathcopytextfont', undef, sub {
    if (my $textfont = LookupValue('savedfont')) {
      MergeFont(family => $textfont->getFamily,
        series => $textfont->getSeries,
        shape  => $textfont->getShape); }
    return; });

# This version generates inline /, with up-shifted numerator
# The shift is an approximation of em - script(em)
# Given current browser implementations of mfrac @bevelled,
# it more accurately captures the appearance of \nicefrac
DefConstructor('\ltx@nicefrac@inline InFractionStyle InFractionStyle',
  "<ltx:XMApp>"
    # Note: / should be kerned with -2mu, -1mu on left & right
    # Note that we're using width negative as an ADJUSTMENT of width, not total width!!!
    . "<ltx:XMTok stretchy='true' meaning='divide' role='MULOP' _font='#slashfont'"
    . " xoffset='-0.1em' width='-0.15em'>/</ltx:XMTok>"
    . "<ltx:XMArg yoffset='0.3em' rpadding='-0.5em'>#1</ltx:XMArg>"
    . "<ltx:XMArg>#2</ltx:XMArg>"
    . "</ltx:XMApp>",
  alias      => '\nicefrac',
  properties => { slashfont => sub { LookupValue('font')->specialize('/'); } });

# This version generates a MathML mfrac with bevelled='true'
DefConstructor('\ltx@nicefrac@bevelled InFractionStyle InFractionStyle',
  "<ltx:XMApp>"
    . "<ltx:XMTok meaning='divide' role='FRACOP' mathstyle='#mathstyle' class='ltx_bevelled'/>"
    . "<ltx:XMArg>#1</ltx:XMArg><ltx:XMArg>#2</ltx:XMArg>"
    . "</ltx:XMApp>",
  alias      => '\nicefrac',                                                    #);
  sizer      => sub { fracSizer($_[0]->getArg(1), $_[0]->getArg(2)); },
  properties => { mathstyle => sub { LookupValue('font')->getMathstyle; } });

# Note that we want to typeset all \nicefrac as math
# However, when \nicefrac is in text mode, it inherits the text font; NOT in math!
# OTOH, when the optional font is given, it can't be \mathtt (eg) when in text mode.
# Can we make \texttt act like \mathtt when in math? (ie. avoid wrapping in mtext!)
DefMacro('\@UnitsNiceFrac Optional {}{}',
  '\ifmmode\ltx@nicefrac@inline{#1{#2}}{#1{#3}}'
    . '\else\if.#1.$\ltx@nicefrac@inline{\nf@mathcopytextfont{#2}}{\nf@mathcopytextfont{#3}}$'
    . '\else$\ltx@nicefrac@inline{#1{#2}}{#1{#3}}$\fi\fi');
DefMacro('\@UnitsNiceFrac@bevelled Optional {}{}',
  '\ifmmode\ltx@nicefrac@bevelled{#1{#2}}{#1{#3}}'
    . '\else\if.#1.$\ltx@nicefrac@bevelled{\nf@mathcopytextfont{#2}}{\nf@mathcopytextfont{#3}}$'
    . '\else$\ltx@nicefrac@bevelled{#1{#2}}{#1{#3}}$\fi\fi');

Let('\@UnitsUglyFrac', '\@UnitsNiceFrac');

DeclareOption('nice', sub { Let('\nicefrac', '\@UnitsNiceFrac'); });
DeclareOption('ugly', sub { Let('\nicefrac', '\@UnitsUglyFrac'); });
# NON-STANDARD option; generate MathML mfrac with bevelled='true'
DeclareOption('bevelled', sub { Let('\nicefrac', '\@UnitsNiceFrac@bevelled'); });

ExecuteOptions('nice');
ProcessOptions();

#======================================================================
1;
