2.4.4 Determining Cell Boundaries
This section describes an algorithm to find the boundaries of the innermost table cell containing a given character position or to determine that the given character position is not in a table cell. Every valid character position in a document belongs to a paragraph, so table depth can be computed for each paragraph. If a paragraph is found to be at depth zero, that paragraph is not in a table cell.
Given character position cp, use the following algorithm to determine if cp is in a table cell.
Follow the procedure from Direct Paragraph Formatting to find the paragraph properties for the paragraph that contains cp. Apply the properties, and determine the table depth as specified in Overview of Tables. Call this itapOrig.
If itapOrig is 0, then this paragraph is not in a table cell, so the following algorithms do not apply. Leave this algorithm. Otherwise, cp is in a table.
If the character at character position cp is not a TTP mark as specified in Overview of Tables, then leave this algorithm.
If itapOrig is 1, then the cp is not in a table cell. Leave this algorithm. Otherwise this TTP mark is in a cell itself, to determine the boundaries of the containing cell set itapOrig to itapOrig – 1 in the following algorithms.
Given a character position cp known to be at table depth itapOrig, follow this procedure to determine the character position of the last character in the innermost table cell that contains cp.
Set itap to itapOrig.
Determine the character position of the last character in the paragraph that contains cp, as specified in Determining Paragraph Boundaries. Let this position be called cpLast.
Follow the procedure from Direct Paragraph Formatting to find the paragraph properties for the paragraph that contains cpLast. Apply the properties, and determine the table depth as specified in Overview of Tables. Call this itap'. It is invalid for itap' to be less than itap. If itap' is less than itap, leave the algorithm.
If itap' is equal to itap, determine the text at character position cpLast, as specified in Retrieving Text. If this character is a cell mark, as specified in Overview of Tables, then cpLast is the desired output. Leave the algorithm.
Let cp be cpLast + 1, and go to step 2.
Given a character position cp that is known to be at table depth itapOrig, follow this procedure to determine the character position of the first character in the innermost table cell that contains cp.
Set itap to itapOrig.
Determine the character position of the first character in the paragraph that contains cp, as specified in Determining Paragraph Boundaries. Let this character position be called cpFirst.
If cpFirst is zero, then this is the desired output. Leave the algorithm. Negative values for cpFirst are invalid. If cpFirst is negative, leave the algorithm.
Let cpPrev be cpFirst – 1. Follow the procedure from Direct Paragraph Formatting to find the paragraph properties for the paragraph that contains cpPrev. Apply the properties, and determine the table depth as specified in Overview of Tables. Call this itapPrev.
If itapPrev is less than itap, then cpFirst is the desired output. Leave the algorithm.
If itapPrev is equal to itap, determine the text at character position cpPrev, as specified in Retrieving Text. If this character is a cell mark or a TTP mark, then cpFirst is the desired output. Leave the algorithm.
7. Set cp to cpPrev. Go to step 2.