Disclaimer: I am assuming you are working in 2D, as rectangles and viewports usually are express in 2D coordinates.
Let the four corners of the viewport be $v,v+w,v+w+h$ and $v+h$, where $v$ is the top left corner of the viewport, and $h$ and $w$ are the height and length respectively of the viewport.
Let the four corners of the rectangle be $r_0, r_1, r_2$ and $r_3$, where $r_0$ has the highest `$y$' coordinate and proceeding clockwise. The height $h_r$ and width $w_r$ of the rectangle can be calculated using Pythagoras's formula.
$$
\begin{align*}
h_r&=\sqrt{\left(r_3^{(x)}-r_0^{(x)}\right)^2+\left(r_3^{(y)}-r_0^{(y)}\right)^2}\\
w_r&=\sqrt{\left(r_1^{(x)}-r_0^{(x)}\right)^2+\left(r_1^{(y)}-r_0^{(y)}\right)^2}
\end{align*}
$$
You also need to check the aspect ratio to ensure that the rectangle isn't too wide. This will be used to determine the scale factor $s$.
if (h/w >= hr/wr) /* rectangle will use full width of the viewport with
with space at the top and bottom */
s = w/wr;
else /* space will be at the left and right */
s = h/hr;
Now, to rotate the rectangle, scale it appropriately and align it within the viewport, a fourfold transformation is required;
- Translate the centre of the rectangle to the origin.
- Rotate the rectangle.
- Scale the rectangle
- Translate the centre of the rectangle to the centre of the viewport.
Assuming that $r_0$ will be rotated into the top left corner, the following identities are true for the rotation by an angle of $\theta$ in a counter-clockwise direction.
$$
\begin{align*}
\cos\theta&=\frac{r_1^{(x)}-r_0^{(x)}}{w_r}\\
\sin\theta&=\frac{r_0^{(y)}-r_1^{(y)}}{w_r}
\end{align*}
$$
The rotation matrix $R$ is then
$$
R=\begin{bmatrix}
\cos\theta&-\sin\theta\\\sin\theta&\cos\theta
\end{bmatrix}.
$$
The centre of the rectangle $c_r$ is able to be expressed as
$$
\begin{align*}
c_r^{(x)}&=\frac14\sum\limits_{i=1}^4r_i^{(x)},\\
c_r^{(y)}&=\frac14\sum\limits_{i=1}^4r_i^{(y)},
\end{align*}
$$
and the centre of the viewport $c$ as
$$c^{(x)}=v^{(x)}+w/2 \qquad c^{(y)}=v^{(y)}+h/2.$$
The combined transformation for each point is then
$$
r_k^{new}=sR(r_k-c_r)+c,
$$
where each of $r_k$, $c_r$ and $c$ are expressed as a $2\times1$ vector.
If you begin with a horizontal object whose initial dimensions are $L_x$ and $L_y$ (before scaling), then the offset between blue and red text should be
$$
\begin{align}
&\Delta x={K_x-1\over2}L_x(1-\cos\alpha)+{K_y-1\over2}L_y\sin\alpha,\cr
&\Delta y=-{K_y-1\over2}L_y(1-\cos\alpha)+{K_x-1\over2}L_x\sin\alpha,\cr
\end{align}
$$
where $\alpha$ is the rotation angle with respect to the horizontal and $K_x$, $K_y$ are the scaling factors.
The sign of $\Delta x$ and $\Delta y$ depends on how you measure $\alpha$, so you may need to find the correct sign by trial and error. Hope that helps.
EDIT
To show how those formulas can be derived, consider the black rectangle in the diagram below. Scaling it (with respect to up-left vertex $P$) and then rotating around the center $O'$ of the scaled rectangle yields a final brown rectangle, whose up-left vertex is $P'$.
If the black rectangle is first rotated around its center $O$ and then scaled with respect to $P''$, then we get instead the blue rectangle below. We are interested in finding the "offset" vector $\vec{P'P''}$.
If we denote by $R$ the rotation operator
($R(x,y)=(\cos\alpha x-\sin\alpha y, \cos\alpha y+\sin\alpha x)$, if positive angles represent a counterclockwise rotation) and by $S$ the scaling operator
($S(x,y)=(K_x x,K_y y)$), then we have:
$$
\vec{P'P''}=\vec{P'O'}+\vec{O'O}+\vec{OP''}=
R\vec{PO'}+(S-1)\vec{OP}+R\vec{OP}=
-R(S\vec{OP})+(S-1)\vec{OP}+R\vec{OP},
$$
that is:
$$
\vec{P'P''}=
(S-1)\vec{OP}-R(S-1)\vec{OP}.
$$
Inserting here $\vec{OP}=(-L_x/2,L_y/2)$ one gets the desired result.
In comparing with my old formulas above, I see that
$\vec{P'P''}=(-\Delta x,-\Delta y)$.
In addition, I used there the opposite convention for the sign of the angle, but I hope this explanation is clear enough.
Best Answer
One way for you to do this would be to center in on the fact that the center point doesn't change in a rotation. So find the center of the rectangle. Let's call it $(x,y)$ (although it's very easy to find).
Then, make it so that you can pretend your rectangle is at the origin. That is, subtract the center coordinates from each of the corners, so that you get for instance $(625.49939 - x,632.40015 - y) $ as one of the corners.
Then you can apply the standard rotation matrices to these coordinates. Since you have the coordinates after the rotation, you would use the rotation matrix that rotates by $-90^\circ$. This will rotate the rectangle.
All that's left is to translate the rectangle back to where it should be. You do this by adding the center back to each of the coordinates.
Does that make sense? I can furnish more details if this isn't clear.