4

Background

Looking to devise a scaling factor for two fonts such that the text, when added to the final document, fits on one line.

Example

In the images below, the two fonts are the first character (e.g., "M") and the title text ("My Extraordinary Title"). In the first image, the text is split across two lines:

Large Font

While the second image, which is desired, has the font scaled to fit on one line:

Rescaled Font

The large M in the second image should be scaled down at the same ratio as the rest of the text, hence the desire for a scale factor that can be used against both the title text and its first character.

Problem

I'd like to define a macro along the lines of the following recursive function:

Integer fitText( String text, Integer fontSize, Integer minWidth, Integer maxWidth ) {
  Integer currentWidth = scratchBox( text, fontSize );

  if( currentWidth > maxWidth && currentWidth > minWidth ) {
    return fitText( text, fontSize - 1pt, minWidth, maxWidth );
  }

  return fontSize;
}

Maybe I'm going about this the wrong way, though, and am open to other approaches.

For example, given a \frame is it possible to scale the frame such that it fits the \textwidth? That is:

\framed[
  frame=off,
  width=\textwidth * 4,
]{%
  \BookStylizedChapter{\BookTitleText}
}

Then somehow scale the framed down if its content width exceeds the \textwidth?

Question

How would you scale a line of text (or a framed element) in ConTeXt such that line breaks do not occur, but the font scales down instead?

  • 1
    Why not just scale the box rather than playing with fo tsize? – Aditya Jan 6 '14 at 11:48
  • 1
    Search the list, this comes up every once in a while. For example, the undocumented \FittingText macro was revealed some time ago. I agree with @Aditya though that if you wish to fit only a single line, scaling the box itself is worth a try. – Philipp Gesang Jan 6 '14 at 13:46
4

For a single line of text, a simple solution is to use \scale with only maxwidth specified. For example:

\starttext

\startlines
\scale[maxwidth=4cm]{Hello}
\scale[maxwidth=4cm]{Hello World}
\scale[maxwidth=4cm]{Hello World! How}
\scale[maxwidth=4cm]{Hello World! How are}
\scale[maxwidth=4cm]{Hello World! How are you}
\scale[maxwidth=4cm]{Hello World! How are you doing?}
\stoplines

\stoptext

which gives

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.