Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C-API resvg_options_set_font_family doesn't work in release v0.28.0 #566

Closed
ANSH3LL opened this issue Dec 26, 2022 · 26 comments
Closed

C-API resvg_options_set_font_family doesn't work in release v0.28.0 #566

ANSH3LL opened this issue Dec 26, 2022 · 26 comments

Comments

@ANSH3LL
Copy link

ANSH3LL commented Dec 26, 2022

Hello, I tried to use the function in my C++ project as I've done before, but when rendering the svg, text is not displayed after providing a font and calling the function with the font family. This used to work in releases before v0.28.0.

I see in the commit history that this function was removed then added back, so maybe something was missed when reverting that change?

The C++ project I'm referring to is: https://github.com/ANSH3LL/Graphics-Extensions-for-Solar2D and the line where I'm using the function is: https://github.com/ANSH3LL/Graphics-Extensions-for-Solar2D/blob/23151db1257915cf0aaa47df7728f6c0c4291b30/shared/PluginGfxe.cpp#L772

@RazrFalcon
Copy link
Collaborator

Do you have the text build feature enabled?

While there were indeed some changes to resvg_options_set_font_family, it should not affect text rendering.

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 26, 2022

Yes, the text feature is enabled. Text rendering works as usual, when using system fonts for example, text displays as expected.

Using resvg_options_set_serif_family instead of resvg_options_set_font_family also works.

@RazrFalcon
Copy link
Collaborator

The only thing that have changed is the missing font resolving. If font-family attribute was not set then resvg_options_set_font_family value will be used.
If it was set, but the font is still missing - then resvg_options_set_serif_family value will be used.

It seems like the issue is with your SVG. Does your SVG has a missing font?

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 26, 2022

The SVG I'm using does not have a font-family attribute. However, I'm using resvg_options_set_font_family to set the default font family. This used to work fine before I updated resvg to v0.28.0, at which point it doesn't work anymore, and no text is rendered.

If I use resvg_options_set_serif_family instead of resvg_options_set_font_family, then the text renders as it should.

I am using resvg_options_load_font_data to load the font. I've confirmed there's no issue with this function or the font I'm using.

@RazrFalcon
Copy link
Collaborator

This is very strange. resvg_options_set_serif_family is used only when family-name have not been matched. Do you call both functions with the same value? It should work just fine.

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 26, 2022

I've tried calling both with the same value, and resvg_options_set_serif_family works while resvg_options_set_font_family doesn't. Maybe it's a problem with my build, does resvg_options_set_font_family work for you?

@RazrFalcon
Copy link
Collaborator

RazrFalcon commented Dec 26, 2022

I found the bug. I was handling missing font-family wrong.

@RazrFalcon
Copy link
Collaborator

You can try master branch. I will publish a new release later this week.

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 26, 2022

I've tried it and it still doesn't work unfortunately.

@RazrFalcon
Copy link
Collaborator

Can you add dbg!(&name_list); to usvg-text-layout/src/lib.rs:674?

For me, when rendering an SVG without family-name and resvg_options_set_font_family(opt, "Arial"); it prints:

[usvg-text-layout/src/lib.rs:675] &name_list = [
    Name(
        "Arial",
    ),
    Serif,
]

And it matches Arial.

@RazrFalcon RazrFalcon reopened this Dec 26, 2022
@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 26, 2022

It doesn't print anything. I'm using resvg as a static library, and don't really know rust.
Maybe you could try using resvg_options_set_font_family through the C-API and see if it renders the text using the given font family.

@RazrFalcon
Copy link
Collaborator

I did tried using resvg via C API and it works just fine. Can you provide a sample of your SVG?

@RazrFalcon
Copy link
Collaborator

Also, if dbg! doesn't print anything then you SVG doesn't have text elements. I don't see any other reasons.

@RazrFalcon
Copy link
Collaborator

Can you try emulating your use case using c-api/examples/cairo/example.c? Do you load system fonts at all?

@RazrFalcon
Copy link
Collaborator

I'm using resvg as a static library,

Dumb question, but have you rebuild your project from scratch after updating resvg? Otherwise it will still be using non-fixed version.

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 27, 2022

I think I've solved the problem.

In v0.27.0, the font family set using resvg_options_set_font_family was always added to the list of font names as a fallback, as you commented here.

In v0.28.0, the font family set using resvg_options_set_font_family is only added to the list of font names if the list is empty to begin with. The list seems to contain default values however, and so the font family provided is never added to the list, as you can see here

The default values I got when I added dbg!(&name_list); as you'd said before:

[usvg-text-layout\src\lib.rs:674] &name_list = [
    SansSerif,
    Serif,
]

All I did was remove the if families.is_empty() condition and now it works as it did before v0.28.0. I've created a pull request: #567

@RazrFalcon
Copy link
Collaborator

So your SVG does have font-family after all? Otherwise the condition if families.is_empty() will be true and everything will works as it should.

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 27, 2022

The SVG I'm using doesn't have font-family, only font-size, so I'm not sure what was happening exactly.

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 27, 2022

Maybe this condition wasn't actually needed then?

@RazrFalcon
Copy link
Collaborator

Can you send me a sample SVG?

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 27, 2022

<svg width="330px" height="72px" version="1.1" xmlns="http://www.w3.org/2000/svg">

<g xml:space="preserve" fill="#000000" font-size="30px" text-anchor="start">
<text x="0" y="35">£20.00    hello</text>
<text x="0" y="70">€30.00    test</text>
</g>

</svg>

@RazrFalcon
Copy link
Collaborator

The current logic is correct and works as expected. I have no idea why it's broken for you. I'm missing some info.

Can you add:

dbg!(&font_family);
dbg!(&families);

to usvg/src/text.rs:685 and show me the output? Right before if families.is_empty() {.

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 27, 2022

[usvg\src\text.rs:685] &font_family = ""
[usvg\src\text.rs:686] &families = []

It seems you're correct.

@RazrFalcon
Copy link
Collaborator

Try adding dbg!(&state.opt.font_family); at the same place to see if resvg_options_set_font_family was actually called and handled correctly.

And then dbg!(&span.font.families, resolve_font(&span.font, fontdb)); to usvg-text-layout/src/lib.rs:517, right after if !fonts_cache.contains_key(&span.font) {.

Somehow, a font resolving is either skipped or failed.

@ANSH3LL
Copy link
Author

ANSH3LL commented Dec 27, 2022

I've realized that the problem I was facing was caused by an SVG that had font-family="sans-serif". However, I was providing the Inconsolata font and so sans-serif was not available and text wasn't being rendered.

As the list of font names wasn't empty, the font family I was providing was being ignored. That's why the change I made in #567 made text render as it did in v0.27.0 when the fallback font was always being added to the list.

Sorry I got my SVGs mixed up. I've attached the SVG I'm talking about.
h350.zip

@RazrFalcon
Copy link
Collaborator

Great! No problem. There were indeed a bug with missing font-family, but otherwise everything should work as expected.

Just to clarify, the value set by resvg_options_set_font_family will be used when font-family is missing.
While the value set by resvg_options_set_serif_family will be used when none of the fonts from font-family where matched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants