locked
Measuring whitespace in text runs RRS feed

  • Question

  • Hi,

     

    I have a project which extracts text from a rectangle (which represents an envelope window) on an XPS page. This page is created from Microsoft Word 2007 and a problem occurs when the user presses tab to move to the right of the envelope window and types some text outside of the region I want to extract the text from.

     

    Visually the text is to the right of the window and should not be detected by my code but in the source XPS the tabs are represented as spaces on the front of the text and those spaces start in the envelope window. Therefore the text gets detected as overlapping the window.

     

    In trying to avoid this I am stripping the whitespace off the front of the string (the UnicodeString property) and trying to measure it using .Net so that I can detect where non-whitespace text starts. I cannot find an API that will measure whitespace though.

     

    So far I have tried;

    • FormattedText (does not work because it does not allow for the use of the Indices property which defines how big each space is).
    • Measure method on the Gyphs object, comes back with very large numbers and has very little documentation, none that I can find on how to interpret those numbers.
    • TextFormatter (overkill, but discarded for the same reason as FormattedText).
    • BuildGeometry (Glyphs.ToGlyphRun.BuildGeometry.Bounds). This correctly sizes text that is not whitespace but returns -1 (infinite) on sizes and Bounds.IsEmpty is True when whitespace is being measured.

    The only way I can see of doing this is to manually parse the Indices property and calculate the glyph sizes myself for each space character using the AdvanceWidth property of each glyph indices.

     

    There must be an easier way, does anyone know how to interpret the results from Measure above?

     

    Regards

     

    Ryan

    Tuesday, May 6, 2008 11:30 AM