Repository: nvaccess/nvda

Issue:
NVDA 2018.4 beta 2 giving incorrect position in indented MS Word paragraphs
With NVDA 2018.4 beta 2 the NVDA-delete command incorrectly reports the horizontal position in MS Word files if the paragraph has been indented.  Regardless of the level of indentation, it uses the default margin setting to calculate the position.  To reproduce:
1.  Open MS Word 365
2.  Write a paragraph of text and check what NVDA reports when on the left margin.
3.  Create another paragraph and set the left margin at - say - 2cm.
4.  Press NVDA-delete and NVDA will report the same value as previously.
A file is attached which demonstrates the issue.  I previously added shift-control-tab to the first item in Miscellaneous under Input Gestures.  That causes NVDA to report the current horizontal position without moving the cursor and it reports accurately when a paragraph is indented.
Andrew
[indented-paragraph-issue.docx](https://github.com/nvaccess/nvda/files/2631668/indented-paragraph-issue.docx)



Discussion:
Cc @michaeldcurran 
It is debatable whether users expect the value to be relative to the page or the margin. I don't really see how useful taking the margin into account would be as the margin will be the same all the way down the page. I think it would be better in future to have the ability to simply report the size of the margin separately.
@andrew-l-d I did not fully understand what you meant about assigning an input gesture. Are you saying you previously wrote a script that works correctly in this situation? If so, I'd love to know how you did it. Currently I am getting the distance from the lft edge with the MS Word range object's information property with a constant of wdHorizontalPositionRelativeToPage.
Michael, I am fairly certain I disagree with your comment, "I don't really see how useful taking the margin into account would be..."  In the example I attached, the margin is not the same all the way down the page.  On the indented paragraph I brought the left margin in by 3cm.  Indenting a paragraph is not an uncommon practice, especially for quotations.  Another case where getting the horizontal position correct is important is when hanging indents are used.
You asked, "Are you saying you previously wrote a script that works correctly in this situation?"  No, I suspect you folk did.  I simply assigned a key combination to the first item under Miscellaneous in Input Gestures.  That is what NVDA uses to announce a table cell or, if not in a table, position from the left edge of the document.  I assigned control-shift-tab so the position does not alter and the correct horizontal position is reported.  Let me know if I have done more to confuse than clarify.
Andrew

Thanks for your clarification.

I think I better understand the issue now.  It looks like when a 
paragraph is indented, NVDA is not able to take this in to account when 
reporting the position of the caret.

I think it best not to use the word "margin" in this discussion.

Clearly Microsoft Word is giving back the distance from the left edge of 
the paragraph, not the page. This is not what I expected.

We already support getting the paragraph indent when pressing tab at the 
beginning of a paragraph, so it should be easy enough for us to just add 
these two values together. I shall investigate this.


Technical:

When Information(wdHorizontalPositionRelativeToPage) is called on a 
range object, MS Word does not take the paragraph indent into account, 
and therefore gives back the distance from the left edge of the paragraph.

However, when information(wdHorizontalPositionRelativeToPage) is called 
on the actual selection object, it does take it into account, I.e. 
distance from the left edge of the page.

This is most certainly a bug in MS Word.

To work around this in NVDA, Our locationText property on NVDA TextInfos 
is currently too abstract to correctly handle this. We will need to 
specifically override the locationText script in MS Word to handle the 
caret (selection) specifically.

I will try to implement something and provide a try build here soon.

Thanks for reporting this.



Resolving patch:
diff --git a/source/NVDAObjects/window/winword.py b/source/NVDAObjects/window/winword.py
--- a/source/NVDAObjects/window/winword.py
+++ b/source/NVDAObjects/window/winword.py
@@ -565,18 +565,23 @@ def _get_unit_mouseChunk(self):
 
 	def _get_locationText(self):
 		textList=[]
-		offset=self._rangeObj.information(wdHorizontalPositionRelativeToPage)
+		# #8994: MS Word can only give accurate distances (taking paragraph indenting into account) when directly querying the selection. 
+		r=self._rangeObj
+		s=self.obj.WinwordSelectionObject
+		if s.isEqual(r):
+			r=s
+		else:
+			return super(WordDocumentTextInfo,self).locationText
+		offset=r.information(wdHorizontalPositionRelativeToPage)
 		distance=self.obj.getLocalizedMeasurementTextForPointSize(offset)
 		# Translators: a distance from the left edge of the page in Microsoft Word
 		textList.append(_("{distance} from left edge of page").format(distance=distance))
-		offset=self._rangeObj.information(wdVerticalPositionRelativeToPage)
+		offset=r.information(wdVerticalPositionRelativeToPage)
 		distance=self.obj.getLocalizedMeasurementTextForPointSize(offset)
 		# Translators: a distance from the left edge of the page in Microsoft Word
 		textList.append(_("{distance} from top edge of page").format(distance=distance))
 		return ", ".join(textList)
 
-
-
 	def copyToClipboard(self):
 		self._rangeObj.copy()
 		return True


Test patch: