Issue 85105

Summary: ActualSize of com.sun.star.text.TextGraphicObject is not initialized if pic inserted programmatically.
Product: Writer Reporter: villeroy <villeroy>
Component: codeAssignee: michael.ruess
Status: CLOSED FIXED QA Contact: issues@sw <issues>
Severity: Trivial    
Priority: P3 CC: issues, kpalagin, Mathias_Bauer, os_ooo, samuel.hartmann.ch
Version: OOo 2.3.1Keywords: regression
Target Milestone: 3.4.0   
Hardware: All   
OS: All   
Issue Type: DEFECT Latest Confirmation in: ---
Developer Difficulty: ---

Description villeroy 2008-01-09 12:52:48 UTC
After manual insertion of a graphic (Insert>Picture>From file...) I have a
TextGraphicObject with a read-only property "ActualSize".
This propertie's struct is unset (Height=0, Width=0) if I do the same thing via API.
Basic code from http://www.oooforum.org/forum/viewtopic.phtml?t=67567
Edit the graphicUrl and run with a Writer document as thisComponent.

Sub Main
graphicUrl = "file:///home/andreas/Documents/Bilder/sott_religion1024x768.jpg"
graphic = thisComponent.createInstance("com.sun.star.text.TextGraphicObject")
graphic.GraphicURL = graphicUrl
xTextRange = thisComponent.CurrentSelection.getByIndex(0)
xTextRange.getText().insertTextContent(xTextRange.getEnd(), graphic, false)

sz = graphic.ActualSize
print sz.Width, sz.Height ' prints 0, 0
End Sub
Comment 1 jsc 2008-01-14 08:44:43 UTC
jsc -> tl:seems to be one for you
Comment 2 thomas.lange 2008-03-14 13:14:34 UTC
.
Comment 3 andrew 2008-12-16 00:05:26 UTC
Although issue 97291 is different, they may have related solutions:

http://www.openoffice.org/issues/show_bug.cgi?id=97291
Comment 4 kpalagin 2008-12-16 04:45:20 UTC
*** Issue 95730 has been marked as a duplicate of this issue. ***
Comment 5 kpalagin 2008-12-16 04:48:32 UTC
added assignee from 95730 to CC
Comment 6 Oliver Specht 2008-12-16 06:31:42 UTC
Reassigned to od
->od: The property is queried from SwGrfNode::GetTwipSize().
Comment 7 kpalagin 2008-12-16 11:54:28 UTC
Oliver,
thanks a lot!

As 2.1 does not have this bug I set keyword regression.
Comment 8 kpalagin 2009-02-16 07:41:02 UTC
Oliver,
any chance to fix this issue for 3.2?

Thanks a lot for your attention.
WBR,
KP.
Comment 9 Oliver-Rainer Wittmann 2009-02-26 11:17:48 UTC
Investigation reveals the following:
The graphic is inserted as a link. Thus, the intrinsic graphic is loaded
asynchronous in a background thread - issue 73788. Until the graphic is
completely loaded its "ActualSize" attribute is not initialized.
Unfortunately, the "ActualSize" attribute is requested before the graphic is
completely loaded.

Possible solutions:
A: Perform something like "busy-waiting" on accessing the "ActualSize" attribute
- loop of access; check, if set; if set then go on else wait a moment and
perform loop-body again.
B: Trigger a synchronous load of a linked graphic on request its "ActualSize"
attribute.

I vote for solution A. Otherwise the advantages of the asynchronous load of
linked graphics are partly lost - see issue 73788

OD->villeroy, kpalagin:
What is your opinion?
Comment 10 villeroy 2009-02-28 10:44:59 UTC
Option C could be a remark in the documentation that certain properties, such as
ActualSize, are availlable after the picture has been loaded completely.
I vote for option A.
Comment 11 michael.ruess 2009-03-13 16:48:52 UTC
*** Issue 100157 has been marked as a duplicate of this issue. ***
Comment 12 samuha76 2009-06-18 23:08:53 UTC
I also experienced problems with the property "ActualSize" of a
TextGraphicObject. And I am sure that the bug it is not just due to the
asynchronous loading of the image in the background. The proposed solution A
does not work for me. My basic macro looped forever if I did not trigger by some
other means the update of the ActualSize property. I have found that watching
the contents of the TextGraphic object in the OOo Basic IDE updates the field
ActualSize. Also using XRay (http://ooomacros.org/dev.php#101416) to inspect the
object updates the ActualSize field. I then identified the line in the XRay
macro which triggers the update. The field is updated when the property
"IsPixelContour" is read:
oTextGraphic.getPropertyValue("IsPixelContour")

I am now using this in my routine to update an existing graphic or create a new
graphic. The code is the following:

Function ImportBitmapIntoWriter(sFile As String, Optional oTextGraphic) As Object
	' Imports a bitmap into Writer
	
	Dim oDoc As Object
	Dim oBitmaps As Object
	Dim sNewUrl As String
	Dim oCursor As Object
	Dim oText As Object
	Const INCH = 2540

	oDoc = ThisComponent

	' Load the image into the internal bitmap table
	oBitmaps = oDoc.createInstance("com.sun.star.drawing.BitmapTable")
	oBitmaps.insertByName( "OOoLilyPond", ConvertToURL(sFile) )
	sNewURL = oBitmaps.getByName( "OOoLilyPond" )
	
	' Create a new TextGraphic Object, if needed
	If IsMissing(oTextGraphic) Then
		oCursor=oDoc.getCurrentController().getViewCursor()
		oText = oCursor.getText()
		oTextGraphic = oDoc.createInstance("com.sun.star.text.GraphicObject")
		oTextGraphic.GraphicURL = sNewURL
		oTextGraphic.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
		oText.insertTextContent( oCursor, oTextGraphic, False )
	End If

	' remove the Bitmap from the table
	' (otherwise the Bitmaps of deleted Objects will still be referenced and saved)
	oBitmaps.removeByName( "OOoLilyPond" )

	' this triggers the updating of the property "ActualSize"
	' see http://www.openoffice.org/issues/show_bug.cgi?id=85105
	oTextGraphic.getPropertyValue("IsPixelContour")
	' set the original graphic size
	oTextGraphic.setSize(oTextGraphic.ActualSize)

'	oTextGraphic	
	' select the object
	oDoc.getCurrentController().select(oTextGraphic)

	'return the TextGraphic object
	ImportBitmapIntoWriter = oTextGraphic	
End Function


If reading "IsPixelContour" triggers the update of "ActualSize". It should be
implemented that reading "ActualSize" also triggers the same action. Or
alternatively if there is really a time delay due to the asynchronous loading of
the image there should be a property "GraphicIsLoaded" and a method
"WaitForGraphicLoaded".

Samuel
Comment 13 Mathias_Bauer 2011-02-09 11:03:09 UTC
Please don't implement option "A" - this is a can of worms. Switching to
synchronous loading is better. 
Comment 14 villeroy 2011-02-09 15:42:40 UTC
I revoke my vote for option A and vote for B now. I believe that mba is more
knowledgeable than me.
Comment 15 Oliver-Rainer Wittmann 2011-03-11 14:19:20 UTC
fixed in cws sw34bf05 - changed files:
/sw/source/core/unocore/unoframe.cxx, 
/sw/source/core/graphic/ndgrf.cxx,
change set http://hg.services.openoffice.org/cws/sw34bf05/rev/e051950c25cd
Comment 16 Oliver-Rainer Wittmann 2011-03-11 14:20:46 UTC
forgot to adjust status
Comment 17 Oliver-Rainer Wittmann 2011-03-14 09:53:41 UTC
reviewed code to fix this issue with OS
Comment 18 Oliver-Rainer Wittmann 2011-03-21 09:26:16 UTC
od->mru: Checked in internal installation set of cws sw34bf05 - please verify.
Comment 19 michael.ruess 2011-03-22 13:04:00 UTC
Verified fix in CWS sw34bf05.