How to check if image on DrawPage and TextPortion is same?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

How to check if image on DrawPage and TextPortion is same?

Post by _savage »

I’ve got a document with five (bitmap) images.

When I iterate over document.DrawPage.createEnumeration() then I get five references to these images, e.g.

Code: Select all

pyuno object (com.sun.star.drawing.XShape)0x7fe063487c28{implementationName=SwXTextGraphicObject, supportedServices={…}, supportedInterfaces={…}}
When I iterate over the paragraphs (document.Text.createEnumeration()) then I get five text portions whose type is Frame, and upon iterating over the content of these frames I get TextContent like so:

Code: Select all

pyuno object (com.sun.star.text.XTextContent)0x7fe063484dc8{implementationName=SwXTextGraphicObject, supportedServices={…}, supportedInterfaces={…}}
Notice how both have the same implementationName “SwXTextGraphicObject”, because these are essentially the same images. But how can I test that at runtime? They seem to be different objects of different types, so how do I check that an image/shape on a DrawPage is the same as one inside of a Frame?

Interestingly, given the same object on a DrawPage, obj.Graphics returns a different value every time it’s accessed. Given that, I suspect that comparing images is not realistic?
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: How to check if image on DrawPage and TextPortion is sam

Post by hubert lambert »

Hello,

What about comparing the GraphicURL property?
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: How to check if image on DrawPage and TextPortion is sam

Post by _savage »

hubert lambert wrote:What about comparing the GraphicURL property?
The GraphicURL is a property of the TextGraphicService and would’t work for objects with Shape service support.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: How to check if image on DrawPage and TextPortion is sam

Post by _savage »

I ended up md5-hashing the bitmap data of images, and comparing the hashes. Shapes are converted to png first (see this thread).

Not pretty, resource-hungry, but it works.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: How to check if image on DrawPage and TextPortion is sam

Post by hubert lambert »

_savage wrote:The GraphicURL is a property of the TextGraphicService and would’t work for objects with Shape service support.
No. XShape objects of type SwXTextGraphicObject support the TextGraphicObject service. Thus you can easily check if the source image (return by the GraphicURL property) is the same.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: How to check if image on DrawPage and TextPortion is sam

Post by _savage »

hubert lambert wrote:No. XShape objects of type SwXTextGraphicObject support the TextGraphicObject service. Thus you can easily check if the source image (return by the GraphicURL property) is the same.
I guess you’re correct. However, I just had a document where the following code:

Code: Select all

drawobjenum = document.DrawPage.createEnumeration()                                                       
while drawobjenum.hasMoreElements():
    drawobj = drawobjenum.nextElement()
Results in a XShape:

Code: Select all

pyuno object (com.sun.star.drawing.XShape)0x7feee6c24c98{implementationName=SwXShape, supportedServices={…}, supportedInterfaces={…}}
Undortunately… :(
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: How to check if image on DrawPage and TextPortion is sam

Post by hubert lambert »

Check whether the object supports the right service and you should be done.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: How to check if image on DrawPage and TextPortion is sam

Post by _savage »

hubert lambert wrote:Check whether the object supports the right service and you should be done.
I did ;) It doesn’t :(

Code: Select all

pyuno object (com.sun.star.drawing.XShape)0x7feee6c24c98{implementationName=SwXShape, supportedServices={com.sun.star.drawing.CustomShape,com.sun.star.drawing.Shape,com.sun.star.drawing.CustomShapeProperties,com.sun.star.drawing.FillProperties,com.sun.star.drawing.LineProperties,com.sun.star.drawing.Text,com.sun.star.drawing.TextProperties,com.sun.star.style.ParagraphProperties,com.sun.star.style.ParagraphPropertiesComplex,com.sun.star.style.ParagraphPropertiesAsian,com.sun.star.style.CharacterProperties,com.sun.star.style.CharacterPropertiesComplex,com.sun.star.style.CharacterPropertiesAsian,com.sun.star.drawing.ShadowProperties,com.sun.star.drawing.RotationDescriptor,com.sun.star.drawing.Shape}, supportedInterfaces={com.sun.star.beans.XPropertySet,com.sun.star.beans.XPropertyState,com.sun.star.text.XTextContent,com.sun.star.lang.XServiceInfo,com.sun.star.lang.XUnoTunnel,com.sun.star.drawing.XShape,com.sun.star.lang.XTypeProvider,com.sun.star.uno.XWeak,com.sun.star.uno.XAggregation,com.sun.star.drawing.XShape,com.sun.star.lang.XComponent,com.sun.star.beans.XPropertySet,com.sun.star.beans.XMultiPropertySet,com.sun.star.beans.XPropertyState,com.sun.star.beans.XMultiPropertyStates,com.sun.star.drawing.XGluePointsSupplier,com.sun.star.container.XChild,com.sun.star.lang.XServiceInfo,com.sun.star.lang.XTypeProvider,com.sun.star.lang.XUnoTunnel,com.sun.star.container.XNamed,com.sun.star.text.XText,com.sun.star.container.XEnumerationAccess,com.sun.star.text.XTextRangeMover,com.sun.star.drawing.XEnhancedCustomShapeDefaulter}}
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: How to check if image on DrawPage and TextPortion is sam

Post by hubert lambert »

I mean: if it does not, pass... :) .
Or do you want also to compare shapes that are not images?

And after checking, you can simply rely on the equal operator. I.e., if the "XShape" object and the "XTextContent" object represent the same image, then

Code: Select all

objXShape == objXTextContent
will return True.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Post Reply