Text - Fit text to shape

An AutoShape has a text frame, referred to in the PowerPoint UI as the shape’s Text Box. One of the settings provided is Autofit, which can be one of “Do not autofit”, “Resize text to fit shape”, or “Resize shape to fit text”. The scope of this analysis is how best to provide an alternative to the “Resize text to fit shape” behavior that simply resizes the text to the largest point size that will fit entirely within the shape extents.

This produces a similar visual effect, but the “auto-size” behavior does not persist to later changes to the text or shape. It is just as though the user had reduced the font size just until all the text fit within the shape.

Candidate Protocol

Shape size and text are set before calling TextFrame.fit_text():

>>> shape.width, shape.height = cx, cy
>>> shape.text = 'Lorem ipsum .. post facto.'
>>> text_frame = shape.text_frame
>>> text_frame.auto_size, text_frame.word_wrap
(None, False)

Calling TextFrame.fit_text() sets auto-size to MSO_AUTO_SIZE.NONE, turns word-wrap on, and sets the font size of all text in the shape to the maximum size that will fit entirely within the shape, not to exceed the optional max_size. The default max_size is 18pt. The font size is applied directly on each run-level element in the shape. The path to the matching font file must be specified:

>>> text_frame.fit_text('calibriz.ttf', max_size=18)
>>> text_frame.auto_size, text_frame.word_wrap
(MSO_AUTO_SIZE.NONE (0), True)
>>> text_frame.paragraphs[0].runs[0].font.size.pt
10

Current constraints

TextFrame.fit_text()

  • Path to font file must be provided manually.

  • Bold and italic variant is selected unconditionally.

  • Calibri typeface name is selected unconditionally.

  • User must manually set the font typeface of all shape text to match the provided font file.

  • Font typeface and variant is assumed to be uniform.

  • Font size is made uniform, without regard to existing differences in font size in the shape text.

  • Line breaks are replaced with a single space.

Incremental enhancements

  • Allow font file to be specified. This allows TextFrame.fit_text() to be run from pip installed version. OS-specific file locations and names can be determined by client.

  • Allow bold and italic to be specified. This allows the client to determine whether to apply the bold and/or italic typeface variant to the text.

  • Allow typeface name to be specified. This allows the client to determine which base typeface is applied to the text.

  • Auto-locate font file based on typeface and variants. Relax requirement for client to specify font file, looking it up in current system based on system type, typeface name, and variants.

PowerPoint behavior

  • PowerPoint shrinks text in whole-number font sizes.

  • When assigning a font size to a shape, PowerPoint applies that font size at the run level, adding a sz attribute to the <a:rPr> element for every content child of every <a:p> element in the shape. The <a:endParaRPr> element in each paragraph also gets a sz attribute set to that size.

XML specimens

<p:txBody> for default new textbox:

<p:txBody>
  <a:bodyPr wrap="none">
    <a:spAutoFit/>  <!-- fit shape to text -->
  </a:bodyPr>
  <a:lstStyle/>
  <a:p/>
</p:txBody>

8” x 0.5” text box, default margins, defaulting to 18pt “full-size” text, auto-reduced to 12pt. <a:t> element text wrapped for compact display:

<p:txBody>
  <a:bodyPr wrap="square" rtlCol="0">
    <a:noAutofit/>
  </a:bodyPr>
  <a:lstStyle/>
  <a:p>
    <a:r>
      <a:rPr lang="en-US" sz=1200 dirty="0" smtClean="0"/>
      <a:t>The art and craft of designing typefaces is called type design.
           Designers of typefaces are called type designers and are often
           employed by type foundries. In digital typography, type
           designers are sometimes also called font developers or font
           designers.</a:t>
    </a:r>
    <a:endParaRPr lang="en-US" sz=1200 dirty="0"/>
  </a:p>
</p:txBody>