The standard scaling and rotation matrices pivot point is $(0,0)$. To adjust it, compose your transformation with appropriate translation before and after the main matrix.
Suppose you want to rotate your picture with pivot at $(p_x,p_y)$, then first translate $(-p_x,-p_y)$, then rotate and finally translate back $(p_x,p_y)$
$$
\begin{bmatrix}x'\\y'\\1\end{bmatrix}=
\begin{bmatrix}
1 & 0 & p_x \\
0 & 1 & p_y \\
0 & 0 & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
cos(\theta) & -sin(\theta) & 0 \\
sin(\theta) & cos(\theta) & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 0 & -p_y \\
0 & 1 & -p_x \\
0 & 0 & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}x\\y\\1\end{bmatrix}
$$
or for row-vector representation of points (which seems you are using)
$$
\begin{bmatrix}x,y,1\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
-p_x & -p_y & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
cos(\theta) & sin(\theta) & 0 \\
-sin(\theta) & cos(\theta) & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
p_x & p_y & 1 \\
\end{bmatrix}
=
\begin{bmatrix}x',y',1\end{bmatrix}
$$
The same will work with scaling. Also, if both your scaling and rotation use the same pivot point, then you can get rid of the inner translation as these cancel out, i.e.
$$
\begin{bmatrix}
1 & 0 & -p_y \\
0 & 1 & -p_x \\
0 & 0 & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 0 & p_y \\
0 & 1 & p_x \\
0 & 0 & 1 \\
\end{bmatrix}
=
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
$$
I hope this helps $\ddot\smile$
Let $X=(0,0)$ and let $W=(a,b)$. Call $S_X, S_W$ the respective squares, $S_X', S_W'$ after rotation.
$$S_X : (a\pm EF/2, b\pm EF/2)$$
$$S_W : (\pm AB/2, \pm AB/2)$$
After rotation:
$$S_X' : (a, b\pm \sqrt{2}/2EF),(a\pm \sqrt{2}/2EF, b)$$
$$S_W' : (0, \pm \sqrt{2}/2AB), (\pm \sqrt{2}/2AB,0)$$
(you can get this by multiplying the coordinates by the rotation matrix.)
If you want to line up $B$ and $F$ then you want real numbers $s,t$ such that
$$(s, \sqrt{2}/2AB+t)=(a, b+ \sqrt{2}/2EF)$$
So $$(s,t)= (a,b+\sqrt{2}/2(EF-AB))$$
Edit
Here is an interactive plot I made for you. First both squares of specified length are rotated by 45 degrees. Then the square centered at the origin is translated by the amount specified above.
https://www.desmos.com/calculator/psp0oquhfs
For rectangles, rather than go through a whole 'nother derivation, the value of $s,t$ you seek is:
$$\left[\begin{matrix}s\\ t\end{matrix}\right]=\left[\begin{matrix}a\\ b\end{matrix}\right]+\left[\begin{matrix}\cos\theta & -\sin\theta\\ \sin\theta & \cos\theta\end{matrix}\right]\left[\begin{matrix}F_x-B_x-a\\ F_y-B_y-b\end{matrix}\right]$$
Another example for rectangles, rotating $270^o$
https://www.desmos.com/calculator/qccjkp32ha
Best Answer
Turns out the solution lies in knowing the correct terms to google for.
Put exact implementation here into a function for individual points, using same rotation matrix returned by the image rotate function solved the problem: