To layout text you basically have two options:
- use the paragraph layouter
- use the XHTML layouter
Using the Paragraph Layouter and the SDL output
The paragraph layouter is a basic layouter for one or more paragraphs of text. Its input is a unicode string, some settings and a set of properties for each and every character in the string to format.
That way it is possible to change a single character to underlined, or red or make is using a different font
To use the paragraph layouter you need to use code like the following:
int main()
{
ai.set(6, attr);
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
SDL_Flip(screen);
sleep(10);
}
Using the XHTML Layouter and the OpenGL output
The XHTML layouter takes a subsection of XHTML as a string and parses it and uses the paragraph layouter to layout the text. Here you specify the format using html and css-like formatting information.
The XHTML Layouter comes with 2 entry points. One takes a string, parses that string into a DOM-tree and works with that tree.
The other choice is to directly input a DOM-tree on your own.
For the XHTML layouter the flow is slightly different:
#define USE_PUGI_XML
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main()
{
styleSheet.
addRule(
"body",
"color",
"#ffffff");
styleSheet.
addRule(
"body",
"font-size",
"20px");
styleSheet.
addRule(
"body",
"text-align",
"justify");
styleSheet.
addRule(
"body",
"padding",
"10px");
styleSheet.
addRule(
"h1",
"font-weight",
"bold");
styleSheet.
addRule(
"h1",
"font-size",
"60px");
styleSheet.
addRule(
"h1",
"text-align",
"center");
styleSheet.
addRule(
"h1",
"background-color",
"#FF8080");
std::string text = u8"<html><body><h1>Title</h1><p>Some text</p></body></html>";
if (!glfwInit()) return 1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_SRGB_CAPABLE, GL_TRUE);
GLFWwindow* screen = glfwCreateWindow(800, 600, "OpenGL example", NULL, NULL);
if(!screen) return 1;
glfwMakeContextCurrent(screen);
glewExperimental = true;
if (glewInit() != GLEW_OK) return 1;
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_FRAMEBUFFER_SRGB);
glfwSwapBuffers(screen);
while (!glfwWindowShouldClose(screen)) glfwPollEvents();
glfwDestroyWindow(screen);
glfwTerminate();
return 0;
}
Using hyphenation dictionaries
The following example shows how to use hyphenation. There are only 2 things to do:
- register the dictionaries required for your language
- provide a language information, either as an attribute in the XHTML code, or to the member variable lang in the CodepointAttributes_c of you text
#define USE_PUGI_XML
#include <stll/hyphenationdictionaries/hyph_en_US.h>
#include <stll/hyphenationdictionaries/hyph_de_DE_gz.h>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_stream.hpp>
int main()
{
styleSheet.
addRule(
"body",
"color",
"#ffffff");
styleSheet.
addRule(
"body",
"font-size",
"20px");
styleSheet.
addRule(
"body",
"text-align",
"justify");
styleSheet.
addRule(
"body",
"padding",
"10px");
styleSheet.
addRule(
"h1",
"font-weight",
"bold");
styleSheet.
addRule(
"h1",
"font-size",
"60px");
styleSheet.
addRule(
"h1",
"text-align",
"center");
styleSheet.
addRule(
"h1",
"background-color",
"#FF8080");
{
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::gzip_decompressor());
boost::iostreams::array_source src((const char*)hyph_de_DE_gz, hyph_de_DE_gz_len);
in.push(src);
}
std::string text = u8"<html><body>"
"<p lang='en'>Text in English language</p>"
"<p lang='de-de'>Text in deutscher Sprache</p></body></html>";
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
SDL_Flip(screen);
sleep(10);
return 0;
}