Changing fonts in multiple embedded formulas, 24.8.1.2

I need to change all fonts in embedded formulas on a Draw document (would be great if it worked on Impress as well), including formulas that are grouped with other objects.

Math fonts: OpenSymbol, Bold
Variables: Liberation Serif, Italic, Bold
Functions: Liberation Serif, Bold
Numbers: Liberation Serif, Bold
Text: Liberation Serif, Bold
Serif: Liberation Serif, Bold
Sans-serif: Liberation Sans, Bold
Fixed-width: Liberation Mono, Bold

I tried some AI assistance, however their input amounted to less than nothing, unfortunately. I just cannot get the LO Basic script right…

Any suggestions are welcome.

Draw and Impress are internally the same component (with slightly different UIs). What you can do in one can be done in the other.

Formulas are built in Math which, unfortunately, has a development lagging light-years behind the others. This explains why Math is not style-driven. You can’t change fonts or other attributes simply through a customisation of “styles”. What you configure in its dialogs is effective for new formulas and has no feed back on existing ones.

This means you are doomed to review your existing formulas manually one bu one.

Are you sure? I still have a script that modifies all embedded formulas to make them bold, or change their font size. Can’t this be modified to change font types as well? Also, I understand that Math is behind other modules, but does it not get any updates at all or it just has lower priority?

Here’s the script, by the way.

REM  *****  BASIC  *****

Sub changeFormulas_Bold_10pt()
	doc = ThisComponent
	sNames = Array("BaseFontHeight", "FontMathIsBold", "FontFixedIsBold", "FontFunctionsIsBold", "FontNumbersIsBold", "FontSansIsBold", "FontSerifIsBold", "FontTextIsBold", "FontVariablesIsBold")
	values = Array(10, True, True, True, True, True, True, True, True)
	changeFormulaProperties doc, sNames, values
End Sub

Sub changeFormulas_Regular_12pt()
	doc = ThisComponent
	sNames = Array("BaseFontHeight", "FontMathIsBold", "FontFixedIsBold", "FontFunctionsIsBold", "FontNumbersIsBold", "FontSansIsBold", "FontSerifIsBold", "FontTextIsBold", "FontVariablesIsBold")
	values = Array(12, False, False, False, False, False, False, False, False)
	changeFormulaProperties doc, sNames, values
End Sub

Sub changeFormulaProperties(doc, sNames, values)
	if doc.supportsService("com.sun.star.formula.FormulaProperties") Then  
		doc.setPropertyValues(sNames, values)
	elseif HasUnoInterfaces(doc, "com.sun.star.drawing.XDrawPageSupplier") Then  
		obj = doc.getDrawPage()
		loopDrawPage obj, sNames, values
	elseif HasUnoInterfaces(doc, "com.sun.star.drawing.XDrawPagesSupplier") Then  
		REM Draw, Impress, Calc
		obj = doc.getDrawPages()
		loopDrawPages obj, sNames, values
	endif
End Sub

Sub loopDrawPages(oDrawPages, sNames, values)
	for nDP = 0 to oDrawPages.getCount()-1
		oDP = oDrawPages.getByIndex(nDP)
		loopDrawPage oDP, sNames, values
	next nDP
End Sub

Sub loopDrawPage(oDP, sNames, values)
	for nShape = 0 to oDP.getCount()-1
		oShape = oDP.getByIndex(nShape)
		REM recurse grouped controls:
		if oShape.supportsService("com.sun.star.drawing.GroupShape") then loopDrawPage oShape, sNames, values
		on error resume next
			setShapeModelProperties oShape, sNames, values
	next nShape
End Sub

Sub setShapeModelProperties(oShape, sNames, values)
	oModel = oShape.Model
	sImplementationName = oModel.ImplementationName
	if sImplementationName = "com.sun.star.comp.Math.FormulaDocument" then
		oModel.setPropertyValues(sNames, values)
	endif
End Sub

@ExtremeConfusion: macros allow you to tweak the ODF any way you like. My comment is valid for the standard UI. I was thinking also of patching the XML encoding but this is unsafe as an error can easily be made.

I am going to test it afterwards, of course. So, I am willing to accept the risk if it breaks anything. This script specifically, worked amazingly well. Any way to make this change fonts in a way I described earlier?

I have absolutely no experience with LO macros. Sorry.

It’s alright. I hope those with experience will contribute to it.
Regarding LO Math, is it abandoned? Or does it receive updates, albeit with lower priority?

Alas, few updates because the component is not considered to its true value.

I do hope the developers reconsider their position. It is not an Office suite without math tools.

A document with some formulas can help to study the macro and see if it is possible to change the font.

It is definitely possible. It just might take significantly longer for someone that knows no basic. Otherwise, it would take minutes to adjust this script.

[I really feel annoyed. Yesterday I tried to post a comment on this question (#11 then), but first time in about 10.5 years of my contributions to this site (discourse or askbot) the post was not accepted and published. Instead it was “Waiting for approval” for more than a full day. I now deleted the draft and am starting a new attempt. I would urgently want to know what was or is going on.]

Now the new comment:
As already mentioned Math formula models have lots of properties which may afflicht the appearance.
Since the also mentioned “magic trick” usable in Writer doesn’t work in Draw we may have to resort to setting the properties one by one. However, I still prefer to base this on a template formula.
User code in Basic doing it this way will not be very efficient, but my example is working in LibO V 14.8. (under Win 10). See
harmonizeFormulaShapesInDrawingsDiving.odg (75.1 KB)
The code is using the For Each construct with shape containers, and will therefore not work witn version before (about) 7.0.
I just made a raw adaption to circumvent the limitation. See:
harmonizeFormulaShapesInDrawingsDivingLegacy.odg (63.9 KB)
This example should work in all versions of LibO, but also in any OO.o and in current AOO.

1 Like

How do you know?
I had to take some time to get what I offered above. Am I just lacking experience?
I did not try to “adjust this script”, however.

1 Like

I am sorry, I am confused about what you mean. Isn’t that what macros are for?
If it lets you mass change all font sizes, or make them bold, how can it not change the type of the font?
For the people who have some experience with basic, they probably know the name of the attribute to target font types. This isn’t something new or revolutionary. I tried some AI assistance, even the ones “supposedly” well-versed in coding, I even gave them the functioning script as an example. However, their output was not working as it gave me one “Basic runtime error” after another.

Since you don’t know Basic, you think that Basic is very simple and anyone who knows basic can make a script in seconds.

  • Basic is very simple (I encourage you to learn)

The complicated thing is LibreOffice and its ways of accessing objects and properties and the way an embedded object can be accessed.
In Writer you have the option of a macro recorder, which you don’t have in Draw due to its complexity.

  • You can try macros with AI (it’s another solution) if it’s that simple to create a script AI will solve it for you in milliseconds.

I already had some help in the past to turn and now i have a script that can turn all fonts in math formulas to bold, or change their font size to a specified value.
Here’s the sample code

REM  *****  BASIC  *****

Sub changeFormulas_Bold_10pt()
	doc = ThisComponent
	sNames = Array("BaseFontHeight", "FontMathIsBold", "FontFixedIsBold", "FontFunctionsIsBold", "FontNumbersIsBold", "FontSansIsBold", "FontSerifIsBold", "FontTextIsBold", "FontVariablesIsBold")
	values = Array(10, True, True, True, True, True, True, True, True)
	changeFormulaProperties doc, sNames, values
End Sub

Sub changeFormulas_Regular_12pt()
	doc = ThisComponent
	sNames = Array("BaseFontHeight", "FontMathIsBold", "FontFixedIsBold", "FontFunctionsIsBold", "FontNumbersIsBold", "FontSansIsBold", "FontSerifIsBold", "FontTextIsBold", "FontVariablesIsBold")
	values = Array(12, False, False, False, False, False, False, False, False)
	changeFormulaProperties doc, sNames, values
End Sub

Sub changeFormulaProperties(doc, sNames, values)
	if doc.supportsService("com.sun.star.formula.FormulaProperties") Then  
		doc.setPropertyValues(sNames, values)
	elseif HasUnoInterfaces(doc, "com.sun.star.drawing.XDrawPageSupplier") Then  
		obj = doc.getDrawPage()
		loopDrawPage obj, sNames, values
	elseif HasUnoInterfaces(doc, "com.sun.star.drawing.XDrawPagesSupplier") Then  
		REM Draw, Impress, Calc
		obj = doc.getDrawPages()
		loopDrawPages obj, sNames, values
	endif
End Sub

Sub loopDrawPages(oDrawPages, sNames, values)
	for nDP = 0 to oDrawPages.getCount()-1
		oDP = oDrawPages.getByIndex(nDP)
		loopDrawPage oDP, sNames, values
	next nDP
End Sub

Sub loopDrawPage(oDP, sNames, values)
	for nShape = 0 to oDP.getCount()-1
		oShape = oDP.getByIndex(nShape)
		REM recurse grouped controls:
		if oShape.supportsService("com.sun.star.drawing.GroupShape") then loopDrawPage oShape, sNames, values
		on error resume next
			setShapeModelProperties oShape, sNames, values
	next nShape
End Sub

Sub setShapeModelProperties(oShape, sNames, values)
	oModel = oShape.Model
	sImplementationName = oModel.ImplementationName
	if sImplementationName = "com.sun.star.comp.Math.FormulaDocument" then
		oModel.setPropertyValues(sNames, values)
	endif
End Sub

Wouldn’t it just take a few changes in this formula to make it change font types to the desired one?
As I mentioned earlier, I already had AI assistance, the script they produced amounted to being less than useless. They all had fundamental “basic runtime errors”. I could give them the error back in an attempt to fix it. The error would change a few times and ultimately stabilize at something unfixable.

I’m sorry I can’t help you, but my knowledge of basic is not much and is related to Writer and calc (they are less).
The methods to access objects in each LibreOffice component are different and my time is limited.

The Draw documents provided by @Lupp have some macros that could be a definitive solution or a good starting point

A formula must be selected that serves as a template to change all formulas.

  • I think the macros you provide are limited for a linked object.

It’s alright. There were a few people here some months ago that easily made some suggestions to get the desired result. I hope they will turn up. In the meantime, I will do my own research to come up with a solution, of course.

I think the macros you provide are limited for a linked object.

What do you mean? The script I provided does exactly what is expected. It changes all the formulas, even the ones that are grouped with other objects.

I have only tried some macros and they are effective on the formulas but not how they are represented in Draw.
@Lupp macros are effective and their representation in Draw is changed.

Highly appreciated @ajlittoz (who rarely responds to questions about Draw) wasn’t quite sure, and in fact in this one case he was exceptionally wrong.

You failed to cite the source from which you got the quoted code. That is not a good idea.