Automatic font size with character styles

Hello all

I describe my problem bellow.

In a paragraph that has the “Text Body” style I want to apply some formatting to certain words (e.g. a monospace font). In order to do so, I create my character style by right-clicking the character style “Default style” and setting the desired font (only the font family and not the size; the size is left as it is). Let’s call it “Monospace Style”. Applying it to the desired words it works.

Now comes the interesting part: I want to modify the “Text Body” style and change the font size. It works very well: It changes the font size for the entire paragraph and also for the words having my just created style “Monospace Style”. This is the expected behavior. But it works only when default font size (that proposed by Writer) of the style “Monospace Style” is not modified.

Because if I change the font size for my “Monospace Style” to 80%, then the when changing the font size for “Text body” the words having the style “Monospace Style” do not change their size.

(Here is my reasoning: probably Writer computes the exact size of the font when the font size is changed. However if the font size is not changed then it is able to look at the paragraph font and apply a size provided by it.).

Is it possible to create a character style with a font size expressed in percent relative to the paragraph font? How?

Thank you.
quilx

Thank you very mush for your answer.

I cannot understand what prevents what I need to happen. Because it already happens. When the size of the font used in the “Monospace style” is the default proposed by Write all works well: when I change the size of the paragraph font the size of the word in “Monospace Style” changes. What would prevent Writer to apply the same algorithm but for a percentage percentage?

Do you suggest that the solution to my problem would be to create a “Monospace Style” whose size will be set after each change to the font size of the Text Body style?

1 Like

Do you suggest that the solution to my problem would be to create a “Monospace Style” whose size will be set after each change to the font size of the Text Body style?

Yes, that is one possibility.


I recommend you not to use the character style “Standard”.
By the way, in newer versions it is called “No Character Style” and cannot be changed.

So another possibility would be to create several character styles derived from “Standard” with different font sizes and assign them to the desired text as needed.


What would prevent Writer to apply the same algorithm but for a percentage percentage?

It is best to ask the developers.


You can make a feature request on Bugzilla.
See the Comment from @mikekaganski

The relative value of the font size property in a style does not relate to the place at which the style is applied; it refers to the font of the parent style in the hierarchy of inheritance (i.e., when your style has an “inherit from”). When there’s no size defined at parent (either it’s not explicitly set in all parents, or there’s no parents), a global default is used.

Consider a style with a parent, the parent defining the font size to 14, and the child to 150%. You apply child to a text, where the paragraph size is 10. What the 150% should refer to?

It may still be reasonable to consider the text at the place of application when there’s no size set in parent; I suppose it could be a valid enhancement request (but @Regina may know better if that interpretation is potentially allowed by the standard).

1 Like

Relative to the default font-size given in the paragraph style is not possible. The relationship to the parent character style is explicit:

In contrast to XSL, percentage values can be used within common styles only and are based on the font height of the parent style rather than to the font height of the attributes neighborhood.

2 Likes

Is it possible to create a character style with a font size expressed in percent relative to the paragraph font?

No.


This is due to the fact that a change of the font size (i.e. also a percentage) in the character style is a change. A change has the consequence that the inheritance is interrupted.

relativeFontSizeInCharacterStyle.odt (39.0 kB)

It is wild solution, but you can use Superscript with your %, and the macro will set Raise by 0% to your Character Style.

Sub setFontSizePCTtoCharStyle
	const c="Monospace Style" 'name of your Character Style
	dim oDoc as object, oStyles as object, oStyle as object, s$
	oDoc=ThisComponent
	s=inputbox("Character Style: ", , c)
	if s="" then exit sub
	oStyles=oDoc.StyleFamilies.getByName("CharacterStyles")
	if oStyles.hasByName(s) then
		oStyle=oStyles.getByName(s)
		oStyle.CharEscapement=0
	else
		msgbox("Character Style doesn't exist:" & chr(13) & s)
	end if
End Sub

But remember it is good to set Text-to-text Alignment to Base line for a Paragraph Style.



It is also possible to use a macro for direct formatting with same efect.

Sub fontSizeXpct 'set Superscript for selection
	const c=80 'default % size
	dim oDoc as object, oSel as object, o as object, i&, a&
	oDoc=ThisComponent
	oSel=oDoc.CurrentSelection
	a=inputBox("%", "Font Size %", c)
	if a=0 then exit sub 'Esc pressed
	for i=0 to oSel.Count-1
		o=oDoc.Text.createTextCursorByRange(oSel.getByIndex(i))
		o.CharEscapement=0
		o.CharEscapementHeight=a
	next i
End Sub


And small explanation: It is not possible to set manually 0% for Raise/lower by, but the macro can it.
superscript