\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xdvipskmaps}[2026/04/10 v1.1 luatex opentype map (ST)]

\chardef\otf@dvi@code=1
\DeclareOption{otfchar}{\global\chardef\otf@dvi@code=0}
\DeclareOption{otfgid}{\global\chardef\otf@dvi@code=1}
\ProcessOptions

\RequirePackage{luatexbase}
\RequireLuaModule{xdvipskmaps}

\directlua{%
    local standard_luatex = callback.list()['dvi_output'] == nil
    if standard_luatex then
       if tonumber('\number\otf@dvi@code') > 0 then
          luatexbase.module_warning('xdvipskmaps', "Only the `otfchar` option is available in current `LuaTeX` version!")
          token.set_char("otf@dvi@code", 0, "global")
       end
    end     
    }

\ifcase\number\otf@dvi@code\relax %% \relax as guard
    \directlua{%
        xdvipskmaps.otfgid=false;
        luatexbase.add_to_callback('buildpage_filter', xdvipskmaps.buildpage_filter_callback, 'xdvipskmaps buildpage filter')%
        }%
\else\relax %% \relax just in case
    %% Works with the standard LuaTeX engine from Version 1.25.6!
    \directlua{%
        xdvipskmaps.otfgid=true;
        %% mapline DVI special sample in DVI for OpenType fonts
        %% xxx [lmroman10-regular]:+tlig;  LMRoman10-Regular "LMRoman10-Regular" >$SELFAUTOPARENT/texmf-dist/fonts/opentype/public/lm/lmroman10-regular.otf
        luatexbase.callbacktypes['dvi_output'] = 1
        luatexbase.add_to_callback('dvi_output', xdvipskmaps.dvi_output, 'xdvipskmaps_dvi_output')
        %% switch to using GID encoding in DVI
        %% DVI special to set GID encoding flag for xdvipsk
        %% xxx vtex:settings.xdvipsk.opentype={enc=gid}
        if pdf.setdviencoding ~= nil then
           pdf.setdviencoding(1)
        else
           luatexbase.module_warning('xdvipskmaps', "`GID` encoding not available in current `LuaTeX` version!")
        end
        }%
\fi
%% set OpenType font marker in DVI file
\AtBeginDvi{\special{vtex:info.fonts.otf=1}}
\endinput
