CasparCG / server

CasparCG Server is a Windows and Linux software used to play out professional graphics, audio and video to multiple outputs. It has been in 24/7 broadcast production since 2006. Ready-to-use downloads are available under the Releases tab https://casparcg.com.
GNU General Public License v3.0
904 stars 269 forks source link

Asian font support for PSD/Scene #627

Closed didikunz closed 1 year ago

didikunz commented 7 years ago

It seeams that the PSD producer does not handle foreign alphabeths properly. I tried with japanese and arabic. Even if the coresponding fonts are present in the font folder, the text does not get rendered. I think as a global open source project we should be able to show the whole unicode range of characters.

drakmor commented 6 years ago

Charset limited in https://github.com/CasparCG/Server/blob/b2a17fcb83bb2aa5350f89d9d5e3803f6c7fd1a0/core/producer/text/text_producer.cpp#L159 for minimize texture generation. May be move charset list to config?

didikunz commented 6 years ago

When moving it to the config, it should not be neccessary to enable every unicode block individually, as this is too abstact for most people insted we should probably have a few groups like so:

jesperstarkar commented 6 years ago

How about enabling all glyphs by default? I don't see any reason not to do so? We do it within the templates all the times.

didikunz commented 6 years ago

Good point Jesper, I do that also all the time, the characters that are not present in the font will not be loaded anyway. There are no fonts, that contain the whole unicode range, because that is just not possible. So the "waste" of memory could be toleratable.

drakmor commented 6 years ago

CasparCG render each font character of size 72x72 in the texture. There are 136,690 characters in Unicode and it will be very long and costly to render them all. May be split ranges like this http://unicode.org/charts/#scripts ?

didikunz commented 6 years ago

You say it renders every character as a bitmap? What does it do with unicode codepoints that are empty? Render a blank bitmap of 72x72 pixels? Because, as I said earlier: Most fonts contain only a few characctes, for instance Adobes Bebas contains 455, Microsofts Arial is a bit bigger but "only" contains 2137.

drakmor commented 6 years ago

It's try in loop render every glyph in range.

for(int i = range.first; i <= range.last; ++i)
{
    FT_UInt glyph_index = FT_Get_Char_Index(face_.get(), i);
    if(!glyph_index)    //ignore codes that doesn't have a glyph for now. Might want to map these to a special glyph later.
        continue;

    FT_Error err = FT_Load_Glyph(face_.get(), glyph_index, flags);
    if(err) continue;   //igonore glyphs that fail to load

    const FT_Bitmap& bitmap = face_->glyph->bitmap; //shorthand notation

    auto region = atlas_.get_region(bitmap.width+1, bitmap.rows+1);
    if(region.x < 0)
    {
        //the glyph doesn't fit in the texture-atlas. ignore it for now.
        //we might want to restart with a bigger atlas in the future
        continue;
    }
...
}

We can try rewrite get_range to request avaible ranges from fonts metadata.

didikunz commented 6 years ago

As a programmer unfamiliar with c++ I do not full understand the code. But would it be possible to not allocate the bitmap, when the glyph does not load, what means, that it is not present in the font? And what do we do with ones that do not fit in that region, as the code ignores them, if I understand right? And what do we do, if we need fonts bigger than 72 pixel? No way to display them? In Flash we have the limitation, that a font can not have a bigger size than 72 points. But, as it is vector based, you can put it into a movieClip and scale it up, without it getting blury...

drakmor commented 6 years ago

Sorry, font size flexible. 72 - it's DPI

didikunz commented 6 years ago

I see, so there is no limit how big the characters can be. That's good.

drakmor commented 6 years ago

Rewrote the load of all characters in the font, now you can test. https://github.com/drakmor/CasparCG_Server/commit/b1eba83ab75e69fb52f5e25b7d3a32ba2dc94fcd

binaries - http://dropmefiles.com/fToZQ

didikunz commented 6 years ago

Thanks. will test tomorrow.

didikunz commented 6 years ago

Thanks. will test tomorrow.

didikunz commented 6 years ago

Ok, I have tested it now: I have a PSD template for a match statistic in football if I run it with Beta 2 (build 3440 f15ba60) I stopped 1.93 seconds from hitting play to the template appering (stopped by hand). With the test build you sent me yesterday the same takes 6.10 So we need to find a way to further optimize it. And what I found out also is, that no far east (CJK) is working. I tried with Japanese and simplified Chinese and it was not displayed. For the test of other alphabeths I used a Noto font that I merged together myself, so that it contains a lot of alphabeths like arabic, hebrew, cyrilian etc. Googles original Noto fonts only support a single of these alphabeths, You find the font here: https://www.dropbox.com/s/4rpzs16xnbnpaer/MergedNotoSans-Regular.ttf?dl=0 But if I take a normal, smaller Noto font the load time does not change.

didikunz commented 6 years ago

By the way: Is there a way to read the supported Unicode planes from the metadata of the font? Because the users will probaly not use fonts supporting alphabeths they don't use. So we could only load the planes that are insede the font, without wasting space for missing characters.

drakmor commented 6 years ago

It's simpe don't fit in texture. Need another method or more large texture. [2017-10-21 17:31:12.694] [12548] [debug] [text_producer] 2957 of 15444 glyphs of font "Kozuka Gothic Pro L" loaded, 7220 did not fit in the atlas, 0 load error. image

didikunz commented 6 years ago

And is there a way to read the supported Unicode planes from the metadata of the font? Because the users will probaly not use fonts supporting alphabeths they don't use. So we could only load the planes that are insede the font, without wasting space for missing characters.

didikunz commented 6 years ago

...or another idea: Can we write a file per font, that contains the bitmaps in an optimized form so that we do not need to run the conversion over and over again? Could also be a "font import tool" that could be run by the user (or the template creator) and that imports the font. We could give there also a selection of codepages to be converted, similar to the font embedding dialog inside Flash Pro / Animate.

drakmor commented 6 years ago

I only load existing characters. They are simply very much limited by the size of the symbol and the free space in the texture. It may be worthwhile to remove the caching of characters altogether and draw them at once.

FreeType library have cache subsystem - https://www.freetype.org/freetype2/docs/reference/ft2-cache_subsystem.html

Julusian commented 1 year ago

I am closing this to clean up the number of stale issues. If someone wants to revive the scene producer, all of the closed tasks can be found at https://github.com/orgs/CasparCG/projects/1/views/1