johnnyjob
johnnyjob

Reputation: 390

Expected behavior of the renderer for superscripts and subscripts in Word Open XML

It looks like ECMA specification for Word Open XML doesn't specify how to render "runs" with vertAlign attribute. Is there a document describing the expected behavior:

  1. What font size to use for superscripts and subscripts?
  2. For how much to shift the superscript/subscript text relatively to the baseline?

Just for reference, here's a document.xml generated by MS Word for a trivial document containing text "X²" (XML namespaces are omitted for brevity):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document>
  <w:body>
    <w:p w14:paraId="4B8ED8F1" w14:textId="3891D3E1" 
         w:rsidR="00CE1223" w:rsidRDefault="00886D56">
      <w:r>
        <w:t>X</w:t>
      </w:r>
      <w:r w:rsidRPr="00886D56">
        <w:rPr>
          <w:vertAlign w:val="superscript"/>
        </w:rPr>
        <w:t>2</w:t>
      </w:r>
      <w:bookmarkStart w:id="0" w:name="_GoBack"/>
      <w:bookmarkEnd w:id="0"/>
    </w:p>
    <w:sectPr w:rsidR="00CE1223">
      <w:pgSz w:w="12240" w:h="15840"/>
      <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" 
               w:header="720" w:footer="720" w:gutter="0"/>
      <w:cols w:space="720"/>
      <w:docGrid w:linePitch="360"/>
    </w:sectPr>
  </w:body>
</w:document>

Upvotes: 2

Views: 552

Answers (1)

Discobarry
Discobarry

Reputation: 64

The RunProperties elements has following two child elements which can be used to set the position and the font of a Run which has a VerticalTextAlignment:

  • The RunFonts element which can be used to set the type of font
  • The Position element which can be used to lower or raise the the run in relation to its default baseline.

Using these elements you can create a run which is in superscript and has an adjusted font:

    // Creates an RunProperties instance and adds its children.
    public RunProperties GenerateRunProperties()
    {
        RunProperties runProperties1 = new RunProperties();
        RunFonts runFonts1 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" };
        FontSize fontSize1 = new FontSize(){ Val = "48" };
        VerticalTextAlignment verticalTextAlignment1 = new VerticalTextAlignment(){ Val = VerticalPositionValues.Superscript };

        runProperties1.Append(runFonts1);
        runProperties1.Append(fontSize1);
        runProperties1.Append(verticalTextAlignment1);
        return runProperties1;
    }

This will output the follwing openxml:

<w:rPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
   <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman" />
  <w:sz w:val="48" />
  <w:vertAlign w:val="superscript" />
</w:rPr>

And you can shift the vertical aligned run using these elements:

    // Creates an RunProperties instance and adds its children.
    public RunProperties GenerateRunProperties()
    {
        RunProperties runProperties1 = new RunProperties();
        RunFonts runFonts1 = new RunFonts(){ Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" };
        Position position1 = new Position(){ Val = "-10" };
        FontSize fontSize1 = new FontSize(){ Val = "48" };
        VerticalTextAlignment verticalTextAlignment1 = new VerticalTextAlignment(){ Val = VerticalPositionValues.Superscript };

        runProperties1.Append(runFonts1);
        runProperties1.Append(position1);
        runProperties1.Append(fontSize1);
        runProperties1.Append(verticalTextAlignment1);
        return runProperties1;
    }

Generating the follwing openXML:

<w:rPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman" />
  <w:position w:val="-10" />
  <w:sz w:val="48" />
  <w:vertAlign w:val="superscript" />
</w:rPr>

Depending on the value that you assign to the position element the run will be raised or lowered in relation to its baseline:

  • Positve => Raised
  • Negative => Lowered

Upvotes: 0

Related Questions