cursor.setPropertyToDefault( "CharPosture") removes color

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
antalk
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

cursor.setPropertyToDefault( "CharPosture") removes color

Post by antalk »

In the snippet below, cursor.setPropertyToDefault( "CharPosture") seems to
also remove font color. I observed similar interaction between other character properties as well when
using cursor.setPropertyToDefault
Runnable python3 example in the attachment.
Originally observed from java code.

LibreOffice
Version: 6.4.6.2
Build ID: 1:6.4.6-0ubuntu0.20.04.1

Code: Select all

    #
    # *** TEST ***
    #
    text.insertString( cursor, "(upright black)" , 0 )

    cursor.setPropertyValue( "CharColor", 0xff4040 )
    cursor.setPropertyValue( "CharPosture", ITALIC )
    text.insertString( cursor, "(color italic)" , 0 )

    cursor.setPropertyToDefault( "CharPosture") ## removes italic (OK), but removes CharColor as well (not OK)
    text.insertString( cursor, "(upright, should have color)" , 0 )    ## lost the color

Could you suggest a workaround?
Attachments
ex-setPropertyValue-loses-color.zip
(15.74 KiB) Downloaded 229 times
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by RoryOF »

And what is the "Default" colour property?

See https://www.openoffice.org/api/docs/com ... le-ix.html for the methods available.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
antalk
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by antalk »

> And what is the "Default" colour property?

I do not know. The code does not ask for default color, it asks for default CharPosture,
which I expected to to have the effect of removing direct formatting. Without affecting color in any way.
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by FJCC »

The following code results in the last inserted string being red and upright. I do not understand why the original code does not work and this does.

Code: Select all

from com.sun.star.awt.FontSlant  import ITALIC
def togglePosture():
  doc = XSCRIPTCONTEXT.getDocument()
  oText = doc.getText()
  oCurs = oText.createTextCursor()
  oText.insertString( oCurs, "(upright black)" , 0 )
  oCurs.setPropertyValue( "CharColor", 0xff4040 )
  oCurs.setPropertyValue( "CharPosture", ITALIC )
  oText.insertString( oCurs, "(color italic)" , 0 )
  oCurs.setString("(upright, should have color)")
  oCurs.setPropertyToDefault( "CharPosture")
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by JeJe »

The same thing happens in Basic and it appears to happen with any char property... whichever one you put in the setPropertyToDefault line, all the char properties are reset. Unless that's what you want you'll have to not use setPropertyToDefault.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
antalk
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by antalk »

Your (FJCC) example suggested the strange behaviour may be related to
chaging properties on empty cursors. The attached workaround attempts
to avoid this by adding extra characters to "hold" the new properties and keeping them
around for the following inserts. Seems to work. Maybe the case of non-empty cursor
could be handled more simply by leaving out the bracketing calls.

Thank you for the example.
Attachments
ex-setPropertyValue-loses-color-workaround.zip
(24.49 KiB) Downloaded 233 times
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
antalk
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by antalk »

> you'll have to not use setPropertyToDefault.

Tricky. I am trying to interpret text with html-like markup into a text range within a document.
The idea was that closing tags, like "</b>" should restore the state to that before "<b>",
as reported by getPropertyValue.
This latter sometimes turned out to be "not directly formatted" (which can be checked by getPropertyState),
hence the attempt to remove "direct formatting" by using setPropertyToDefault.

Which turns out to modify properties I did not want to touch: the idea was that
if I insert into red or otherwise formatted text, the insertion should only differ from the surrounding
text in properties prescribed by the markup. But now the first closing tag removes the color
and other direct formatting. I hope the workaround in ex-setPropertyValue-loses-color-workaround.zip
will solve this, but I did not get there yet.
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by JeJe »

Why can't you just use?

Code: Select all

tmp =oCurs.getPropertyValue( whichever)
oCurs.setPropertyValue( whichever, whatever)
do something
oCurs.setPropertyValue( whichever, tmp)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by Villeroy »

Or something like this?

Code: Select all

v = oCurs.getPropertyDefault("CharPosture")
oCurs.CharPosture = v
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
antalk
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by antalk »

This is how I started.
Well, almost. I casted tmp to the type documented for the given poperties.
I do not do that in the example below.

A ran into (reproduction in python):

Code: Select all

       # The code below assumes that  "CharStyleName" was not
       # set before.
        tmp = cursor.getPropertyValue( "CharStyleName" )
       # tmp = cursor.getPropertyDefault( "CharStyleName" ) gives the same result
        print(tmp) ## prints ""
        cursor.setPropertyValue( "CharStyleName", "Emphasis" )
        text.insertString( cursor, "thisEmphasis" , 0 )

        cursor.setPropertyValue( "CharStyleName", tmp )
        ## IllegalArgumentException
        text.insertString( cursor, "restoredCharStyleName" , 0 )
For "CharStyleName" restoring default or setting to default
with setPropertyValue results in IllegalArgumentException.
(setPropertyToDefault does not)


So I figured this is not the way to remove the effects,
and found
https://www.openoffice.org/api/docs/com ... State.html
and https://www.openoffice.org/api/docs/com ... ibute.html

PropertyAttribute.MAYBEDEFAULT always came out as false (in LibreOffice).
Finally setPropertyToDefault seemed to work, except for the unwanted side effects.

There is also a PropertyAttribute.REMOVEABLE ("indicates that the property can be removed (i.e., by calling XPropertyContainer::removeProperty)")
I did not try removeProperty.

I think I saw examples using setToDefault to remove direct formatting, so I went in that direction.
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by JeJe »

for the charstylename try

Code: Select all

if tmp = "" then tmp ="Default"
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
antalk
Posts: 6
Joined: Mon Mar 22, 2021 2:46 pm

Re: cursor.setPropertyToDefault( "CharPosture") removes colo

Post by antalk »

Is setting CharStyleName to "Default" in a paragraph with a style prescribing
some properties, for example "Preformatted Text" equivalent to "not changing properties"?

Yes, it seems so. I did not realize that.

Thank you.
LibreOffice Version: 6.4.6.2
x86_64 GNU/Linux
Post Reply