ManimCommunity / ManimPango

Binding for Pango, to use with Manim.
https://manimpango.manim.community
MIT License
43 stars 13 forks source link

MarkupText creating invisible SVG files #44

Closed naveen521kk closed 3 years ago

naveen521kk commented 3 years ago

I really don't know why this happens but running MarkupText creates an SVG file which when viewed in browser is invisible. The file size isn't less but I could see it has

[...]
<g id="surface26">
<g style="fill:rgb(100%,100%,100%);fill-opacity:1;">
  <use xlink:href="#glyph0-1" x="30" y="32"/>
[...]

where the fill:rgb(100%,100%,100%) means white and this makes the text invisible.

Example Code

>>> from manim import MarkupText
>>> MarkupText("Hello")
MarkupText("Hello")

And the file media/texts/87d6c8ec25c88f0b.svg should be created, which when opened on browser is invisible.

My Pango and Cairo version are

>>> import manimpango
>>> manimpango.cairo_version()
'1.17.4'
>>> manimpango.pango_version()
'1.48.1'

Is this expected @PhilippImhof ?

naveen521kk commented 3 years ago

While trying out the same with Text it shows up, the text's are black in colour rather than white.

PhilippImhof commented 3 years ago

I'm not sure I get what you mean, but having white as the default color is expected, see

https://github.com/ManimCommunity/manim/issues/1090 https://github.com/ManimCommunity/manim/issues/1039

naveen521kk commented 3 years ago

I thought black was the default color? Is there any problems on changing the default text color to black? Or is it a bug in Pango?

PhilippImhof commented 3 years ago

Default color for MarkupText is WHITE to avoid text "not showing up" in the video.

https://github.com/ManimCommunity/manim/blob/71a05e82b954e3046e89100d8116412156495e8d/manim/mobject/svg/text_mobject.py#L1187-L1206

naveen521kk commented 3 years ago

Default color for MarkupText is WHITE to avoid text "not showing up" in the video.

That's fine. I am considering the SVG file here, would there be any problems? Also, I can't reproduce this in C.

#include <cairo.h>
#include <cairo-svg.h>
#include <math.h>
#include <pango/pangocairo.h>

#define WIDTH 600
#define HEIGHT 400
#define TEXT "Hello"

int
main (void)
{
    cairo_surface_t* surface;
    cairo_t* cr;
    PangoFontDescription* font_desc;
    PangoLayout* layout;
    double width_layout = WIDTH;
    double font_size_c= 1;
    const char * filename="test.svg";

    surface = cairo_svg_surface_create (filename,WIDTH, HEIGHT);
    cr = cairo_create (surface);

    cairo_move_to(cr,20,20);

    layout = pango_cairo_create_layout(cr);

    pango_layout_set_width(layout, pango_units_from_double(width_layout));
    font_desc = pango_font_description_new();
    pango_font_description_set_size(font_desc, pango_units_from_double(font_size_c));
    pango_font_description_set_style(font_desc, PANGO_STYLE_ITALIC);
    pango_font_description_set_weight(font_desc, PANGO_WEIGHT_BOLD);
    pango_layout_set_font_description(layout, font_desc);
    pango_font_description_free(font_desc);

    cairo_move_to(cr,30,20);
    pango_cairo_update_layout(cr,layout);

    pango_layout_set_markup(layout, TEXT, -1);
    pango_cairo_show_layout(cr, layout);

    cairo_destroy (cr);
    cairo_surface_destroy (surface);
    g_object_unref(layout);
    return 0;
}

I think we are doing something wrong and because of that we get white coloured texts.

PhilippImhof commented 3 years ago

I cannot currently compile the above code on my machine due to some linking error / library path issue. But IIRC it will yield black text, no?

I don't know whether we are doing something wrong, but white seems to be a sensible choice for the default color over at Manim. The white color in the SVG created by instantiating a MarkupText object comes from this code:

https://github.com/ManimCommunity/manim/blob/71a05e82b954e3046e89100d8116412156495e8d/manim/mobject/svg/text_mobject.py#L1324-L1337

naveen521kk commented 3 years ago

I cannot currently compile the above code on my machine due to some linking error / library path issue.

You can compile it with gcc <file-name> $(pkg-config --libs --cflags pango).

But IIRC it will yield black text, no?

Yes, it did, I attach the file, test.svg

The white color in the SVG created by instantiating a MarkupText object comes from this code:

https://github.com/ManimCommunity/manim/blob/71a05e82b954e3046e89100d8116412156495e8d/manim/mobject/svg/text_mobject.py#L1324-L1337

Oh!, you are forcing white on it, I see. https://github.com/ManimCommunity/manim/blob/71a05e82b954e3046e89100d8116412156495e8d/manim/mobject/svg/text_mobject.py#L1325 Now I understand what is happening. Thanks.

PhilippImhof commented 3 years ago

You can compile it with gcc <file-name> $(pkg-config --libs --cflags pango).

No luck. Must be some other problem, probably related to gcc/clang on OS X.

Oh!, you are forcing white on it, I see.

Actually, forcing it to whatever the user chose as their preferred color. This is because we do not set the color to the SVGMobject when MarkupText is used; that is a difference compared to Text. With MarkupText, the color is in the SVG and must not be changed from within Manim anymore.

naveen521kk commented 3 years ago

No luck. Must be some other problem, probably related to gcc/clang on OS X.

If you tell me what are the error I can help, or you can use meson here :wink:, I uploaded a zip file with meson configuration

PangoTests.zip

to compile run:

pip install meson ninja
meson builddir --prefix='$PWD/prefix'
meson compile -C builddir

this should create an executable in prefix which will create an .svg file.

PhilippImhof commented 3 years ago

This is the error message:


$ gcc -v test.c $(pkg-config --libs --cflags pango) -o test

Undefined symbols for architecture x86_64:
  "_cairo_create", referenced from:
      _main in test-7a8efa.o
  "_cairo_destroy", referenced from:
      _main in test-7a8efa.o
  "_cairo_move_to", referenced from:
      _main in test-7a8efa.o
  "_cairo_surface_destroy", referenced from:
      _main in test-7a8efa.o
  "_cairo_svg_surface_create", referenced from:
      _main in test-7a8efa.o
  "_pango_cairo_create_layout", referenced from:
      _main in test-7a8efa.o
  "_pango_cairo_show_layout", referenced from:
      _main in test-7a8efa.o
  "_pango_cairo_update_layout", referenced from:
      _main in test-7a8efa.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I still think it is related to some Xcode / clang / gcc issue on OS X. It might work with Homebrew's gcc and build system.

naveen521kk commented 3 years ago

I think I got no idea on how to, sorry.

PhilippImhof commented 3 years ago

FWIW, I was able to build it with gcc -o test `pkg-config --cflags --libs pangocairo` test.c using either the default GCC from OS X Catalina or the one from Homebrew. Usingpkg-config pango or pkg-config pango cairo is not sufficient, at least not on my Mac, no matter which gcc I used.

naveen521kk commented 3 years ago

Oh, this is because of a different shell and isn't about GCC. Essentially, $() in bash means it will execute the first and then the value gets substituted in the next statement. Maybe, you don't have bash shell?

PhilippImhof commented 3 years ago

No, it's not about the shell. Backticks or $() are basically the same thing. It's about pango vs. pangocairo. They do not yield the same libs and includes:

$ pkg-config --libs-only-l pango      
-lpango-1.0 -lgobject-2.0 -lglib-2.0 -lintl -lharfbuzz

vs.

pkg-config --libs-only-l pango cairo
-lpango-1.0 -lgobject-2.0 -lglib-2.0 -lintl -lharfbuzz -lcairo

vs.

$ pkg-config --libs-only-l pangocairo 
-lpangocairo-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lintl -lharfbuzz -lcairo

The missing symbols were namely

"_pango_cairo_create_layout", referenced from:
      _main in test-c1d4ee.o
"_pango_cairo_show_layout", referenced from:
      _main in test-c1d4ee.o
"_pango_cairo_update_layout", referenced from:
      _main in test-c1d4ee.o

which must have been due to the missing -lpangocairo-1.0.

naveen521kk commented 3 years ago

Looks like my fault sorry. It needs to be pangocairo only then it will work.