dynamic font size for hbox

You can just scale each line to the appropriate length. For example, in ConTeXt I would use:

\starttext

\startframedtext
    [frame=off,width=8cm]
\setupscale[width=\hsize]
\scale{Same}\\
\scale{Word length}\\
\scale{Even though lines are different}
\stopframedtext

\stoptext

which gives

enter image description here

I am sure there is a similar scaling macro in LaTeX.

It is also possible to define an environment where each line is automatically scaled to an appropriate width. If you want that, I can provide the code.


Here’s a LaTeX version of what Aditya did:

\documentclass[12pt]{article}
\usepackage{calc,fontspec,graphicx}
\setmainfont[Ligatures=Rare]{Didot LT Pro}
\begin{document}
\centering
% scale to 3 inches
\resizebox{3in}{!}{Same}\\[.75em]% adjust vertical spacing to taste
\resizebox{3in}{!}{Word length}\\[.5em]
\resizebox{3in}{!}{Even though lines are different}\\

\vspace{5ex}

% scale to length of second line
\Huge
\resizebox{\widthof{Word length}}{!}{Same}\\
Word length\\[-.25em]
\resizebox{\widthof{Word length}}{!}{Even though lines are different}
\end{document}

Didot scaled in two ways

I’m not competent to automate this, and I doubt one should try: for best results, one needs to adjust both vertical spacing and width manually, taking into account the shapes of the glyphs and the effect desired. E.g., to my eye, the width of first line looks more like the others if I scale it to 3.12 inches. And there may be need to add or substract some \hspace at the beginning of one or another line, because what’s exactly centered may look off-center to the eye, again depending on the shapes of the glyphs.

This approach takes time, but if a piece of text is worth setting in this style, it’s worth the effort to make manual adjustments.