I need to prevent my TikZ overlay's font settings from inheriting the font settings at ship out. I know this is a grouping issue because the closing } for the localized font changes has not yet been reached at shipout.
So maybe another way to ask this is to say, "How can I make my TikZ overlay part of the global scope"?
Problem Clarified
In the example, you will see that the font size of the overlay on page 2 has been undesirably affected by the settings of shipout at page 2.
Reasoning
Let's say I want to create a draft mode called "Developer Mode" that I can turn on and off. This mode uses an overlay created by TikZ and placed on each page (at shipout) using atbegshi. Unfortunately, the font settings in effect at the time of shipout are inherited by the TikZ overlay. I could explicitly set every font setting, but I think the smarter way would be to isolate the scope somehow.
Code
\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{tikz}
\usetikzlibrary{calc}% for calculating distance between points
\usepackage{tikzpagenodes}% get area nodes
\usepackage{atbegshi}% for shipout access
\usepackage{lipsum}% for dummy text
\definecolor{blueprint}{cmyk}{.85,.51,0,0}% For visual effect
\makeatletter% for \f@size
\def\printfontsize{\f@size pt}
\makeatother
\def\false{false}
\def\officialbuild{false}
\ifx\officialbuild\false
\AtBeginShipout{\AtBeginShipoutUpperLeftForeground{\draftpage}}% use upperleftforeground to make (0,0) at north west corner as TikZ expects
\fi
\def\draftpage{
\begin{tikzpicture}[overlay,remember picture]
\tikzset{blueprintmeasurement/.style={draw,dashed,color=blueprint,<->,opacity=0.5}}
\tikzset{blueprintborder/.style={draw,color=blueprint,opacity=0.5}}
\tikzset{blueprinttext/.style={color=blueprint,opacity=0.5}}
\node [blueprinttext,anchor=north east,font=\sffamily\bfseries] at ($(current page.north east)+(-2mm,-2mm)$) {{Developer Mode \printfontsize}};
% Draw Current Page Text Area Frame
\draw [blueprintborder]% text area west
let \p{pathID1} = ($(current page text area.south west)-(current page text area.north west)$)
in
(current page text area.north west)
node [left,yshift=-1cm] {\pgfmathparse{veclen(\x{pathID1},\y{pathID1})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm}
to
(current page text area.south west);
\draw [blueprintborder]% text area north
let \p{pathID} = ($(current page text area.north east)-(current page text area.north west)$)
in
(current page text area.north west)
node [above,xshift=1cm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm}
to (current page text area.north east);
\draw [blueprintborder] % text area east
(current page text area.north east)
to (current page text area.south east);
\draw [blueprintborder] % text area south
(current page text area.south east)
to (current page text area.south west);
% Margins
\draw [blueprintmeasurement]% left margin
let \p{pathID} = ($(current page text area.west)-(current page.west)$)
in
(current page.west)
node [above,xshift=10mm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm}
to (current page.west -| current page text area.west);
\draw [blueprintmeasurement]% right margin
let \p{pathID} = ($(current page text area.east)-(current page.east)$)
in
(current page.east)
node [above,xshift=-10mm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm}
to (current page.west -| current page text area.east);
\draw [blueprintmeasurement]% top margin
let \p{pathID} = ($(current page text area.north)-(current page.north)$)
in
(current page.north)
node [left,yshift=-10mm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm}
to (current page text area.north -| current page.north);
\draw [blueprintmeasurement]% top margin
let \p{pathID} = ($(current page text area.south)-(current page.south)$)
in
(current page.south)
node [left,yshift=20mm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm}
to (current page text area.south -| current page.south);
\end{tikzpicture}
}
\begin{document}
\section{Section of Lipsum in \textbackslash normalfont}
\printfontsize\ \lipsum
\section{Section of Lipsum in \textbackslash tiny}
{\tiny\printfontsize\ \lipsum
\lipsum}
\section{Following tiny font}
\lipsum[1]
\end{document}


\newcommand\draftpage{\begingroup\normalfont\normalsize<picture>\endgroup}? – egreg Feb 22 '17 at 14:10\draftpage\bgroup\injectglobalfontsettingshere<picture>\egroup. You just gave me an idea that I am not sure whether I can implement. – Jonathan Komar Feb 22 '17 at 14:29\begin{document}might suffice as the base setting (it is not global, but it might do the trick). Thanks for the optimization tip. I need to investigate that. Do you mean using\savebox? – Jonathan Komar Feb 22 '17 at 14:43\AtBeginShipout{\AtBeginShipoutUpperLeftForeground{\usebox{\draftpageoverlaybox}}}after setting up the box, but it is not working :/ Nevermind. Just found out that TikZ's overlay option and\saveboxare incompatible: http://tex.stackexchange.com/questions/47998/repeat-scaled-version-of-slide-savebox-and-usebox-in-multi-slide-frame – Jonathan Komar Feb 22 '17 at 15:39