Sign in to follow this  
FireInDark

Show me a way to render text than FreeType..

Recommended Posts

FireInDark    148
As shown in the topic , I've take a look at FreeType for text rendering in my project.
But it is not so easy to use thus some one has explained some thing of it for me at gamedev..

And just right now I searched out pango and cairo ..
I know nothing about them two except pango can be used to render texts in cairo (ofcuse FT)

Now I just need to rendering texts, get the pixel data and some info about the text's image (such as the position,the width,and the height of every text's bounding box)

I need to render the texts and need select the text by mouse ..

Question :
1.will the pango lib show me the pixels data poingter in memory and tell me the infos I need
2.any other lib for text renering rather than Pango and FreeType..

Any info you can tell is welcom ..thanks.

Share this post


Link to post
Share on other sites
dilyan_rusev    1360
So far as I know, most things are actually based on FreeType. I've personally used [url="http://www.libsdl.org/projects/SDL_ttf/"]SDL_ttf[/url], and it is [url="http://lazyfoo.net/SDL_tutorials/lesson07/index.php"]as easy as it gets[/url]. The result of rendering is an SDL_Surface, which has got exactly what you need. Now, to access individual pixels, you have to make certain the surface is in RAM and not VRAM. I think that is the default on all platforms with SDL 1.2 now, but I am not sure. Then, there is the problem with accessing individual pixels - that is depth and stuff. There is a very old [url="http://www.libsdl.org/docs/html/guidevideo.html"]example on the aging SDL tutorial[/url] (look up getpixel), and there is some stuff again at lazy foo about [url="http://lazyfoo.net/SDL_tutorials/lesson31/index.php"]pixels and SDL_Surface[/url].

On Windows, you can use [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd371554%28v=vs.85%29.aspx"]DirectWrite[/url]to[url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd368165%28v=vs.85%29.aspx"] render to a bitmap[/url]. From what I've seen, DirectWrite is fairly complicated and requires you to use Direct2D for the actual rendering, which is yet another pain in the ass. But it does seem to produce high-quality text.

Universally, you can simply use a spritesheet that contains pre-rendered glyphs. If the original font were a monospace font, and the ordering of the glyphs predefined, it is trivial to selectively render the desired glyphs by copying them to another destination. There are a lot of bitmap font generators, such as the [url="http://www.angelcode.com/products/bmfont/"]AngelCode BMFont[/url], [url="http://www.ironstarmedia.co.uk/2010/01/free-game-dev-utility-fancy-bitmap-font-generator/"]Fancy Bitmap Font Generator[/url], [url="http://code.google.com/p/fontbuilder/"]fontbuilder[/url], and [url="http://www.codehead.co.uk/cbfg/"]Codehead's Bitmap Font Generator[/url]. Since the font is monospace, glyphs are guaranteed to have the same dimentions. You know how many characters are located per column and how many rows you have, so getting a clip rectangle is just multiplication (that is, if you logically view the spritesheet as a matrix, you can get any cell via simple math). Since you know where each letter is, you can calculate to which cell a glyph belongs. Once you've calculated the clip rectangle, it is simply a matter of copying it over to where you want to render the glyph on your scene. Also, since all characters have predefined width and height, you can determine the size of a string you want to render.

I don't know what libraries you use, so it really boils down to your preferences. The thing that will literally draw 0 dependencies is the bitmap font approach, but it is also the least flexible.

To directly answer the question about pango. I have never used the API, but it seems they have [url="http://developer.gnome.org/pango/stable/"]documentation[/url]. I urge you to consult it. Two minutes spent with the documentation partially answer the question: you need to obtain font description, and then use either [url="http://pango_font_metrics_get_approximate_digit_width"]pango_font_metrics_get_approximate_char_width[/url], or [url="http://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-metrics-get-approximate-digit-width"]pango_font_metrics_get_approximate_digit_width[/url]. Honestly, I'd rather poke my eye out than working with this API, as it might as well depend on some gtk+ library, plus it is [url="https://mail.gnome.org/archives/gtk-list/2009-September/msg00081.html"]freakishly ugly.[/url] As far as accessing the pixel data of rendered strings - that depends on the renderer. I do not think it is worth to use this, as it pulls in a ton of dependencies.

Finally, without any real information as to target platforms, libraries you use, etc, there is no way on Earth I can be more helpful than that. Finally, it would help you greatly if you learn some of the typography terminology. Regardless of API, the theory is always the same, just getting the metrics is slightly different. I learned about it from Charlez Petzold's book on Windows programming, and [url="http://www.catch22.net/tuts/enhanced-drawing-painting"]the best free resource I could find is this article[/url]. Skip over the API stuff, and concentrate on the theory.

Share this post


Link to post
Share on other sites
lawnjelly    1246
You can also use non-monospace fonts with some of those exporters. I've currently been using [url="http://www.angelcode.com/products/bmfont/"]BMFont[/url], which allows you to export the width / height / kerning info for each character, as well as a few more things.

Writing a routine to use this info to blat out strings once you have your font 'texture atlas' is not-super complicated. However, once you start doing line wrapping and justification, and adding more than just the basics it gets a bit more tricky.

If you can bear it though, the effort integrating / learning freetype or similar is probably for the best, unless you want to do it for learning's sake.

On windows, using the latest MS api should give you the best quality, using their '[url="http://en.wikipedia.org/wiki/ClearType"]cleartype[/url]' system, which uses subpixel rendering. Freetype has subpixel rendering turned off by default I believe, to avoid the risks of MS patents.

Share this post


Link to post
Share on other sites
FireInDark    148
OK?
Thanks for all of you two..

If just for using in this project ,I can render text with GDI and FT simply ..
And I can make font bitmap myself.
But for a long time I used to find a way to render text ,it is so important - than any thing in all softwares needing GUI- I must have a right way ..

I've tried to use bitmap fonts ,and also to load TTF and other font files but I can't get how to use the data in those file to render it to text image..
Any help?

Share this post


Link to post
Share on other sites
dilyan_rusev    1360
I think both of us replied to this. Like I said, if you read a bit more about text and typography, it will be very helpful for you.

People coming from command-line background think that text is easy (e.g. printf/puts). In fact, it is quite the opposite. Dealing with text can be one of the more complicated things you will have to do. There is the multitude of ways to encode text, and the whole confusion that is called unicode (at least three ways to encode the same thing, really?).

Anyways, what concerns you is how to render text on the screen. You didn't mention neither language, nor graphics API except for GDI. Therefore, I will simply assume you use GDI and C++.

OK, so you can "load" a font. What is a font? Just a bunch of statistics (height, width, etc), mappings, and, overall, describes *how* and *what* to draw. It would be helpful for you to [url="http://scripts.sil.org/cms/scripts/page.php?item_id=IWS-Chapter08"]understand what True Type fonts are[/url]. On its own, loading a font doesn't enable you to render them.

Now, what you see on the screen is created by a renderer. Roughly speaking, it takes a font description, a sequence of characters, and draws them onto a surface. That surface may be an image. You can specify what is the format of that image. And therefore, you can obtain its pixels.

Since you said you have managed to load a font with GDI (with CreateFont* and SelectObject) rendering it is very simple. All you need to do is call one of the [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162499%28v=vs.85%29.aspx"]text drawing functions[/url]:[list]
[*][url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498%28v=vs.85%29.aspx"]DrawText[/url] or [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162499%28v=vs.85%29.aspx"]DrawTextOut[/url]
[*][url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd145133%28v=vs.85%29.aspx"]TextOut[/url] or [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162713%28v=vs.85%29.aspx"]ExtTextOut[/url]
[/list]
So far as metrics go, you can obtain [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd145132%28v=vs.85%29.aspx"]TEXTMETRIC[/url] via [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd144941%28v=vs.85%29.aspx"]GetTextMetrics[/url]for the currently [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162956%28v=vs.85%29.aspx"]selected font[/url]. The line height is roughly tmHeight + tmExternalLeading. The average character width would be tmAveCharWidth. For a monospace font, it will be OK to just use that metric. If you used proportional font, you will need to get [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162755%28v=vs.85%29.aspx"]OUTLINETEXTMETRIC[/url]via [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd144906%28v=vs.85%29.aspx"]GetOutlineTextMetrics[/url].

In order to get the "pixels" (what for?), you will need to render in memory on a bitmap. You do this by rendering on a [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd183489%28v=vs.85%29.aspx"]compatible[/url]DC, which has [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162957%28v=vs.85%29.aspx"]selected[/url] a [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd183488%28v=vs.85%29.aspx"]compatible bitmap[/url]. When you are done, you should be able to access to the pixels via GetPixel/SetPixel for individual pixels, and to the whole data via [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd144879%28v=vs.85%29.aspx"]GetDIBits[/url].

And I don't know why you need this at all. If you said what you wanted to do in the first place, it would have been much easier to help.

Share this post


Link to post
Share on other sites
FireInDark    148
[quote name='dilyan_rusev' timestamp='1343492978' post='4964006']
And I don't know why you need this at all. If you said what you wanted to do in the first place, it would have been much easier to help.
[/quote]

It's sunday you know,I gone to fishing ,so I get your reply till now..

So what I meaned by "load font files" is :
We know that there are datas expressing Bezier curve and lines _maybe we can call them out lines ..OK don't consider about hint now.but how should I got the info about Bezier curve and lines ..and ofcause ,there are datas for layouting ....

Yes I want to render text by the font files myself instead of the work by FT..
I know it's a hard work .but I really want to do it my self..
To be honest I can't express my mind accurately in english.But I think you would know what I want to do by words above..

Thanks for your help... Edited by FireInDark

Share this post


Link to post
Share on other sites
Krohm    5030
[quote name='FireInDark' timestamp='1343572044' post='4964254']
Yes I want to render text by the font files myself instead of the work by FT..
I know it's a hard work .but I really want to do it my self..
To be honest I can't express my mind accurately in english.But I think you would know what I want to do by words above..[/quote]Are you really sure? Ok, perhaps you're sure. I hope you know what you're going to do and why. My current approach involves[list=1]
[*][url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd374091(v=vs.85).aspx"]Uniscribe[/url]. Maps char/wchar_t to glyphs and positions.
[*][url="http://www.cs.cmu.edu/~quake/triangle.html"]Triangle library[/url]. Produce triangles out of contours.
[*][url="http://research.microsoft.com/en-us/um/people/cloop/loopblinn05.pdf"]Resolution Independant Curve Rendering using Programmable Graphics Hardware[/url].
[/list]
This gives me a nice "fire and forget" text which, differently from freetype, can be zoomed and transformed without loosing too much detail. It required quite some work (warning) and quality isn't much of a big deal for the time being. Small character text is often a blurry mess. Think about it.

Share this post


Link to post
Share on other sites
Bregma    9199
Using bitmaps for rendering text is the easiest route, as long as you're limiting yourself to the kind of US English used by Jesus when he wrote the bible. Anything else, and you better brush up on typography. Trying to create a bitmap to support every single Unicode codepoint and being able to handle the metrics of combined characters and right-to-left or vertical scripts will reduce your productivity a bit. Add in multiple typefaces, pointsizes, and slant for added kerning fun.

The only library I've ever seen that comes close to doing proper typography is pango.

Share this post


Link to post
Share on other sites
FireInDark    148
Thank you very much ..
Replies above are helpful for me .
Text rendering is a hard work,I need to study about it for a long time .Then I can decide.

[quote name='Krohm' timestamp='1343634093' post='4964411']
This gives me a nice "fire and forget" text which, differently from freetype, can be zoomed and transformed without loosing too much detail. It required quite some work (warning) and quality isn't much of a big deal for the time being. Small character text is often a blurry mess. Think about it.
[/quote]

Infact ,I can draw images no matter with CPU or GPU ,no matter GLSL shaders or OpenCL .
If I know the path to draw pixels I can do it..
But now,the hardest for me is how to get the "path data" in font files..
Yes I know the inofmation in fonts files expresses where to draw pixels ,so want I want is getting the right infos ..

Such a charactor "A" there're infomation express the out lines if it in a font file ..

typedef struct _CharRenderingInfo
{
//what we need ?
}CharReneringInfo;

typedef unsigned int uint;
int getTextImage(wchar_t charactor,unsigned char font_size,char* font_file_path,char* out_bit_buffer,uint out_width,uint out_height)
{
fstream file(font_file_path);//First we open the font files .
//OK now we should find the right info data from the file.But :HOW
//We need a struct to express the need info for this our "A"
CharRenderingInfo CRINFO = findInfoFromFontFile(file);//how?
//Here we can render it by the CRINFO

}
If you know any links can explain the data structs in TTF files ,that'll be kind.. Edited by FireInDark

Share this post


Link to post
Share on other sites
FireInDark    148
[quote name='Bregma' timestamp='1343635287' post='4964415']
The only library I've ever seen that comes close to doing proper typography is pango.
[/quote]

I do have took a look at pango,and tried to use it but there are some problems when compile it.
AND that
"Client side fonts using the FreeType and fontconfig libraries. Rendering can be with with Cairo or Xft libraries, or directly to an in-memory buffer with no additional libraries.
Native fonts on Microsoft Windows using Uniscribe for complex-text handling. Rendering can be done via Cairo or directly using the native Win32 API.
Native fonts on MacOS X using ATSUI for complex-text handling, rendering via Cairo.
"

Cause words above I think it's not the right what I want..

Thank you ..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this