There is an update, see below
I'm designing mosfet symbols, which should be able to be flipped. The important thing about flipping is that the relative positioning with the positioning library should stay the same, e.g. placing a mirrored node right of a mirrored node should keep the same distance as a 'normal' node placed right of a 'normal' node.
For this, i thought about using the .is if handler and replace the anchors and the background path accordingly. While trying this with the original code, i could change the path, but not the anchors. While creating a MWE for a question here i did something wrong, so now the .is if handler does nothing. Now i'm very confused. Can somebody point me in the right direction?
Note: i know of the xscale=-1 solution for mirroring nodes, but this requires a manual overwrite of the involved anchors of the nodes. I want to avoid this, and i'm open for all kinds of (elegant/easy-to-use) solutions.
Here is the MWE:
\documentclass[parskip=half]{scrartcl}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newif\ifmirrorshape
\pgfkeys{
/tikz/mirror/.is if=mirrorshape
}
\pgfdeclareshape{testshape}
{
\ifmirrorshape
\savedanchor{\center}{
\pgfpointorigin
}
\savedanchor{\left}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{0.5cm}{0cm}}
}
\savedanchor{\upperright}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{-0.5cm}{0.5cm}}
}
\savedanchor{\lowerright}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{-0.5cm}{-0.5cm}}
}
\anchor{center}{\center}
\anchor{north}{\pgfpointadd{\pgfpointorigin}{\pgfpoint{0cm}{0.25cm}}}
\anchor{south}{\pgfpointadd{\pgfpointorigin}{\pgfpoint{0cm}{-0.25cm}}}
\anchor{west}{\left}
\anchor{east}{\pgfpointadd{\pgfpointorigin}{\pgfpoint{-0.5cm}{0cm}}}
\else
\savedanchor{\center}{
\pgfpointorigin
}
\savedanchor{\left}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{-0.5cm}{0cm}}
}
\savedanchor{\upperright}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{0.5cm}{0.5cm}}
}
\savedanchor{\lowerright}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{0.5cm}{-0.5cm}}
}
\anchor{center}{\center}
\anchor{north}{\pgfpointadd{\pgfpointorigin}{\pgfpoint{0cm}{0.25cm}}}
\anchor{south}{\pgfpointadd{\pgfpointorigin}{\pgfpoint{0cm}{-0.25cm}}}
\anchor{west}{\left}
\anchor{east}{\pgfpointadd{\pgfpointorigin}{\pgfpoint{0.5cm}{0cm}}}
\fi
\backgroundpath{
\pgfpathmoveto{\left}
\pgfpathlineto{\upperright}
\pgfpathlineto{\lowerright}
\pgfpathclose
\pgfusepath{stroke}
}
}
\begin{document}
\begin{tikzpicture}[every node/.style={align=center}]
\node[testshape] (s1) at(0, 0) { };
\node[red, testshape, right=of s1, mirror] { };
\end{tikzpicture}
\end{document}
Update:
I've come further with @cfr's answer, but its still not quite right. What i need is to place (outside visible, i don't care about the internals) anchors depending on the 'mirror state'.
Here is an example:
\newif\ifmirrorshape
\pgfkeys{
/tikz/mirror/.is if=mirrorshape
}
\pgfdeclareshape{testshape}
{
\savedanchor{\center}{
\pgfpointorigin
}
\savedanchor{\left}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{-0.5cm}{0cm}}
}
\savedanchor{\right}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{0.5cm}{0cm}}
}
\savedanchor{\upperright}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{0.5cm}{0.5cm}}
}
\savedanchor{\lowerright}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{0.5cm}{-0.5cm}}
}
\savedanchor{\upperleft}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{-0.5cm}{0.5cm}}
}
\savedanchor{\lowerleft}{
\pgfpointadd{\pgfpointorigin}{\pgfpoint{-0.5cm}{-0.5cm}}
}
\anchor{center}{\center}
\anchor{north}{\pgfpointadd{\pgfpointorigin}{\pgfpoint{0cm}{0.25cm}}}
\anchor{south}{\pgfpointadd{\pgfpointorigin}{\pgfpoint{0cm}{-0.25cm}}}
\anchor{right}{\right}
\anchor{left}{\left}
\anchor{west}{\left}
\anchor{east}{\right}
\backgroundpath{
\ifmosfetmirror
\pgfpathmoveto{\right}
\pgfpathlineto{\upperleft}
\pgfpathlineto{\lowerleft}
\else
\pgfpathmoveto{\left}
\pgfpathlineto{\upperright}
\pgfpathlineto{\lowerright}
\fi
\pgfpathclose
\pgfusepath{stroke}
}
}
Testcase:
\begin{tikzpicture}[every node/.style={align=center}]
\node[testshape] (s1) { };
\node[testshape, mirror, right=of s1] (s2) { };
\end{tikzpicture}
I've tried to place the \savedanchor's differently (doesn't work):
\ifmirrorshape
\savedanchor...
\else
\savedanchor...
\fi
I've tried to change anchor within the \deferredanchor (doesn't work):
\deferredanchor{...}{\ifmirrorshape ... \else ... \fi}
I have no idea how to solve this, any help is appreciated.


right=of s1successfully to places2. So I'm not really sure what the problem is or what you expectright=ofto do, if not what it does with my code. – cfr Aug 22 '16 at 00:11eastto sometimes be 5mm to the right of the node andwestsometimes 5mm to the left? Because that is what you've told TikZ to give you, I think. You shouldn't stroke thebackground path. See the TikZ manual for details of what to do if you really want to always draw something. – cfr Aug 22 '16 at 00:23