Controlling Text Rotation

You can control the appearance of rotated text so that users don't have to read upside-down text. By default, when a shape is rotated, the text block rotates, too—which can cause readability problems for shapes rotated from 90 to 270 degrees. If you are designing shapes for use in drawings where readability is an issue, you can customize text rotation behavior using one of the following methods:

  • To prevent upside-down text as a shape is rotated, use the gravity function, which orients the letter baseline toward the bottom or right edge of the page.
  • To prevent text from rotating under any circumstances, use a counterrotation formula to keep the text block level with respect to the bottom of the page as a shape is rotated.

In this section…

Writing Formulas to Control Text Rotation

Gravity Formulas

Counterrotation Formulas for Level Text

Constraining Text Block size

Writing Formulas to Control Text Rotation

You can use the gravity function to prevent upside-down text or to ensure that text blocks are always level with respect to the bottom of the page with a counterrotation formula.

In addition, you can choose whether the gravity or level text block is centered over the shape or offset from it. For example, a text pointer like those shown in the following figures is formatted with a solid color background and remains centered on the shape. By contrast, if you were designing street shapes for a map, you might want to offset the street names from the lines that represent the streets.

Text can rotate with a shape, or not. Default behavior can result in upside-down text (A). Gravity formulas adjust text block orientation for readability (B), while Level text formulas counterrotate the text block to keep it upright (C).

Text can rotate with a shape, or not. Default behavior can result in upside-down text (A). Gravity formulas adjust text block orientation for readability (B), while level text formulas counterrotate the text block to keep it upright (C).

Rotation is cumulative. If you rotate a shape's text block, and then rotate the shape, the amount of the shape's rotation is added to the amount of the text block's rotation.

TOP

Gravity Formulas

The gravity formula calculates the text block rotation for the indicated shape rotation. Angle is a reference to the Angle cell of the shape and represents the shape's current rotation. The syntax for the GRAVITY formula is:

  • GRAVITY(
  • Angle
  • ,
  • limit1
  • ,
  • limit2
  • )

To specify gravity behavior for a shape's text, you add the GRAVITY formula to the Text Transform section in the shape's ShapeSheet spreadsheet. For example:

If the value of Angle falls within the range defined by the following two angles, in this case �60 deg. and 120 deg., the formula returns a value of 180 degrees and the text block is rotated 180 degrees to read correctly. If the Angle doesn't fall within the range, the formula returns a value of zero (0) degrees, and the text block is not rotated. Using this formula, the text is upright for most angles of rotation. For details about gravity formula syntax, see the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference).

Without GRAVITY behavior, text rotated between 120 and 300 (or -60) degrees (A) appears upside down.

Without GRAVITY behavior, text rotated from 120 to 300 (or �60) degrees (A) appears upside down.

TOP

Counterrotation Formulas for Level Text

To counterrotate a text block as a shape is rotated, add the following formula to the Text Transform section in the shape's ShapeSheet:

This formula checks to see if the shape has been flipped, and determines the angle to use based on that information. If the shape has been flipped in both dimensions or has not been flipped at all (if FlipX and FlipY are either both TRUE or both FALSE), the bitxor formula returns a value of false and the if formula returns the value �Angle. The original angle is maintained if the shape has been flipped in only one dimension (if either FlipX or FlipY is TRUE). The Microsoft® Visio® engine writes only the values FALSE (0) or true (1) into the FlipX and FlipY cells, so you can safely assume these are the only values present.

If the shape will never be flipped, you can use a simpler formula to counterrotate the text block:

If you rotate a shape's text block (such that TxtAngle > 0 degrees), and then rotate the shape, the apparent text angle is the sum of the value of TxtAngle and Angle.

TOP

Constraining Text Block Size

When you're designing level text for a small shape, the shape can become obscured by the text if a user types a lot of text or rotates the shape to certain angles. You can constrain the width of the text block to accommodate shapes using the formulas described in this section.

When you design a shape's text behavior, you want to prevent the text from obscuring the shape.

When you design a shape's text behavior, you want to prevent the text from obscuring the shape.

  1. Centered, level text can obscure the shape when rotated, and by default constrains the text block width.
  1. You can write formulas to widen the text block if the shape is rotated out of the way.

Constraining the width of a level text block

With some shapes, such as one-dimensional (1-D) arrows or short shapes, counterrotating text to keep it level isn't enough. As the shape rotates, the level text can obscure portions of the shape, as in the preceding figure. This is especially likely to happen when the text block is centered horizontally and vertically on the shape and has an opaque background. You can write formulas that keep the text block level and adjust its width as necessary when a user rotates the shape or adds text.

When you use the counterrotation formula described in Counterrotation Formulas for Level Text earlier in this section, the text block stays level as the shape rotates. The default Text Transform formulas constrain text block width to shape width, which might not be useful or attractive if the shape is rotated and stretched. To constrain the text block width to the shape width only if the shape is within 15 degrees of horizontal, use the following formulas in the Text Transform and Scratch sections:

Scratch.A1     = DEG(MODULUS(Angle, 180 deg.))
Scratch.B1     = AND(Scratch.A1 >= 15 deg., Scratch.A1 <= 165 deg.)
TxtWidth     = MAX(0.5 in., IF(Scratch.B1, 2.5 in., Width � 0.25 in.))
TxtHeight     = 0.25 in.
TxtAngle     = IF(BITXOR(FlipX, FlipY), Angle, �Angle)

Note To add the Scratch section to a ShapeSheet spreadsheet, right-click the ShapeSheet window, click Insert Section, select Scratch, and then click OK.

The formula in the TxtWidth cell mentioned previously keeps the text block at least 0.5 inch wide for readability. If the shape is rotated beyond the limit defined in the B1 cell of the Scratch section, text block width is set to 2.5 inches; otherwise, it is set to the shape's width minus 0.25 inch to prevent the text from obscuring the shape. The formula in the Scratch.B1 cell performs the rotation test, returning false if the text block width is constrained by the shape width, or true if the text width is unconstrained. The formula in the A1 cell yields a shape angle normalized to a value from 0 degrees to 180 degrees to determine deflection from horizontal.

These formulas work most of the time, but they fail for short shapes that are close to the horizontal limit and have wide text. A more sophisticated solution would take the width of the shape and the composed width and depth of the text into account. However, you should balance the advantage of improved behavior with the adverse effect it could have on your solution's performance.

Controlling the width of an offset level text block

Depending on the alignment of a text block, the shape's rotation, and the amount of text, the text block can obscure the shape. For cases where you want to customize a shape's text block so that it remains level and is also offset from the shape, you can write formulas so that the text block always stays offset from an imaginary boundary defining the shape's sides.

For example, in a space plan, you might want to move and rotate furniture but keep the labels right-side up as viewed on the page, as the following figure shows.

Level text offset from a shape

Level text offset from a shape

  1. The nearest corner of the text block is offset from the shape's side.
  1. As text is added, the offset shifts so the text block won't overwrite the shape.

Add the following formulas to the Text Transform section of a shape to create this behavior:

  • TxtWidth     = MAX(8 * Char.Size, TEXTWIDTH(theText))
  • TxtHeight     = TEXTHEIGHT(theText, TxtWidth)
  • TxtAngle     = IF(BITXOR(FlipX, FlipY), Angle, �Angle)
  • TxtPinX     = Width + (TxtWidth/2 * ABS(COS(Angle)) + TxtHeight/2 * ABS(SIN(Angle)))
  • TxtPinY     = Height/2

The TxtWidth and TxtHeight cells allow the text block to grow as text is added. In the TxtAngle cell, the counterrotation formula levels the text. The text block's pin (TxtPinX) offset is calculated by requiring that, in the shape's local coordinate system, the side of the text block be to the outside of the edge of the shape. The following figure shows that the offset is the sum of line 1 and line 2.

Calculating the text block offset

Calculating the text block offset

  1. Edge of shape
  1. TxtWidth/2
  1. TxtHeight/2
  1. TxtPinX offset = 1 + 2

Line 1 is the leg of a right triangle whose hypotenuse equals TxtHeight/2, so its length is calculated using the Angle cell in the Shape Transform section in this formula:

Line 2 is a leg of a right triangle whose hypotenuse equals TxtWidth/2, so its length is calculated using this formula:

The offset is always a positive value, even when the shape is rotated at a negative angle, because we use the abs function to return the absolute value for lines 1 and 2. Calculating the offset this way means additional formulas are not needed to keep the text block from overwriting the shape as it rotates.