Text/font rendering in OpenGLES 2 (iOS - CoreText?) - options and best practice?

前端 未结 3 576
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-25 14:12

There are many questions on OpenGL font rendering, many of them are satisfied by texture atlases (fast, but wrong), or string-textures (fixed-text only).

However, th

3条回答
  •  长情又很酷
    2020-12-25 15:13

    I did some more experimenting, and it seems that CoreText might make for a perfect solution when combined with a texture atlas and Valve's signed-difference textures (which can turn a bitmap glyph into a resolution-independent hi-res texture).

    ...but I don't have it working yet, still experimenting.


    UPDATE: Apple's docs say they give you access to everything except the final detail: which glyph + glyph layout to render (you can get the line layout, and the number of glyphs, but not the glyph itself, according to docs). For no apparent reason, this core piece of info is apparently missing from CoreText (if so, that makes CT almost worthless. I'm still hunting to see if I can find a way to get the actual glpyhs + per-glyph data)


    UPDATE2: I now have this working properly with Apple's CT (but no different-textures), but it ends up as 3 class files, 10 data structures, about 300 lines of code, plus the OpenGL code to render it. Too much for an SO answer :(.

    The short answer is: yes, you can do it, and it works, if you:

    1. Create CTFrameSetter
    2. Create CTFrame for a theoretical 2D frame
    3. Create a CGContext that you'll convert to a GL texture
    4. Go through glyph-by-glyph, allowing Apple to render to the CGContext
    5. Each time Apple renders a glyph, calculate the boundingbox (this is HARD), and save it somewhere
    6. And save the unique glyph-ID (this will be different for e.g. "o", "f", and "of" (one glyph!))
    7. Finally, send your CGContext up to GL as a texture

    When you render, use the list of glyph-IDs that Apple created, and for each one use the saved info, and the texture, to render quads with texture-co-ords that pull individual glyphs out of the texture you uploaded.

    This works, it's fast, it works with all fonts, it gets all font layout and kerning correct, etc.

提交回复
热议问题