Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Last active August 29, 2015 14:02
Show Gist options
  • Save zr-tex8r/defe33d8bbee8af447ad to your computer and use it in GitHub Desktop.
Save zr-tex8r/defe33d8bbee8af447ad to your computer and use it in GitHub Desktop.
LaTeX: to create xbb files (for use with dvipdfmx) only using pdfLaTeX
% bxxbb.sty
%
% package declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bxxbb}[2014/06/15 v0.1]
% preparationd
\def\bxxb@pkgname{bxxbb}
% environment check
\ifx\pdfoutput\@undefined\else \ifnum\pdfoutput>\z@
\ifx\pdfximagebbox\@undefined\else
\let\bxxb@work=t
\fi
\fi\fi
%% options
\DeclareOption{autostop}{%
\let\bxxb@autostop=t%
}
\DeclareOption{noautostop}{%
\let\bxxb@autostop=f%
}
\ProcessOptions*
%--------------------------------------- public interface
%%<*>\extractbb[<page>]{<image_file>}
\newcommand*{\extractbb}{%
\kernel@ifnextchar[%]
{\bxxb@extractbb@a}{\bxxb@extractbb@a[]}%
}
% (now defined as no-op)
\def\bxxb@extractbb@a[#1]#2{}
%%<*> \xbbquit
% Quit the TeX process now.
% (now defined as no-op)
\newcommand*{\xbbquit}{}
% If not running on pdfTeX, stop loading the package.
\ifx t\bxxb@work\else
\expandafter\endinput\fi\relax
%--------------------------------------- main
% (e-TeX is assumed afterwards)
%% variables
\newwrite\bxxb@xbb
%% \bxxb@pp : the string '%%'
\begingroup \escapechar\m@ne
\xdef\bxxb@pp{\string\%\string\%}
\endgroup
%% redefinition
\def\bxxb@extractbb@a[#1]#2{%
\bxxb@write@xbb{#1}{#2}%
\bxxb@maybe@quit
}
\def\xbbquit{%
\PackageWarning\bxxb@pkgname{Quit now}%
\@@end
}
%% \bxxb@maybe@quit
\ifx f\bxxb@autostop
\let\bxxb@maybe@quit\relax
\else
\def\bxxb@maybe@quit{%
\kernel@ifnextchar\extractbb
{\relax}{\xbbquit}%
}
\fi
%% \bxxb@write@xbb{<page>}{<image_file>}
\def\bxxb@write@xbb#1#2{%
% obtain bbox value
\edef\bxxb@x{#1}%
\edef\bxxb@x{\ifx\bxxb@x\@empty\else page \bxxb@x\fi}%
\pdfximage\bxxb@x{#2}%
\edef\bxxb@bbox{%
\pdfximagebbox\pdflastximage 1 \space
\pdfximagebbox\pdflastximage 2 \space
\pdfximagebbox\pdflastximage 3 \space
\pdfximagebbox\pdflastximage 4}%
\edef\bxxb@x{%
\the\z@\space\the\z@\space\the\z@\space\the\z@}%
\ifx\bxxb@bbox\bxxb@x
\setbox\z@\hbox{\pdfrefximage\pdflastximage}%
\edef\bxxb@bbox{\the\z@\space\the\z@\space
\the\wd\z@\space\the\ht\z@}%
\fi
% create xbb file
\filename@parse{#2}%
\immediate\openout\bxxb@xbb=%
\filename@area\filename@base.xbb\relax
\immediate\write\bxxb@xbb{%
\bxxb@pp Title: #2^^J%
\bxxb@pp BoundingBox: \bxxb@bbox^^J%
\bxxb@pp HiResBoundingBox: \bxxb@bbox^^J%
}%
\immediate\closeout\bxxb@xbb
\typeout{>> bbox of image '#2' = [\bxxb@bbox]}%
}
%--------------------------------------- done
\endinput
%% EOF
% Write instructions for creating xbb files before \documentclass
% command. When this source file is compiled with pdfLaTeX, only
% this "pre-preamble" part will be executed.
\RequirePackage{bxxbb}
\extractbb{tiger.pdf}
\extractbb{tigra.png}
% When the source file is compiled with a non-pdfLaTeX engine,
% the "pre-preamble" part will be ignored and the normal source
% lines thereafter will be processed.
\documentclass[a4paper,dvipdfmx]{jsarticle}
\usepackage{graphicx}
\begin{document}
\begin{center}
\includegraphics[width=100pt]{tiger.pdf}
\end{center}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment