HarfBuzz text shaping engine
http://harfbuzz.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.8 KiB
99 lines
2.8 KiB
<chapter id="hello-harfbuzz"> |
|
<title>Hello, HarfBuzz</title> |
|
<para> |
|
Here's the simplest HarfBuzz that can possibly work. We will improve |
|
it later. |
|
</para> |
|
<orderedlist numeration="arabic"> |
|
<listitem> |
|
<para> |
|
Create a buffer and put your text in it. |
|
</para> |
|
</listitem> |
|
</orderedlist> |
|
<programlisting language="C"> |
|
#include <hb.h> |
|
hb_buffer_t *buf; |
|
buf = hb_buffer_create(); |
|
hb_buffer_add_utf8(buf, text, strlen(text), 0, strlen(text)); |
|
</programlisting> |
|
<orderedlist numeration="arabic"> |
|
<listitem override="2"> |
|
<para> |
|
Guess the script, language and direction of the buffer. |
|
</para> |
|
</listitem> |
|
</orderedlist> |
|
<programlisting language="C"> |
|
hb_buffer_guess_segment_properties(buf); |
|
</programlisting> |
|
<orderedlist numeration="arabic"> |
|
<listitem override="3"> |
|
<para> |
|
Create a face and a font, using FreeType for now. |
|
</para> |
|
</listitem> |
|
</orderedlist> |
|
<programlisting language="C"> |
|
#include <hb-ft.h> |
|
FT_New_Face(ft_library, font_path, index, &face) |
|
hb_font_t *font = hb_ft_font_create(face); |
|
</programlisting> |
|
<orderedlist numeration="arabic"> |
|
<listitem override="4"> |
|
<para> |
|
Shape! |
|
</para> |
|
</listitem> |
|
</orderedlist> |
|
<programlisting> |
|
hb_shape(font, buf, NULL, 0); |
|
</programlisting> |
|
<orderedlist numeration="arabic"> |
|
<listitem override="5"> |
|
<para> |
|
Get the glyph and position information. |
|
</para> |
|
</listitem> |
|
</orderedlist> |
|
<programlisting language="C"> |
|
hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count); |
|
hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count); |
|
</programlisting> |
|
<orderedlist numeration="arabic"> |
|
<listitem override="6"> |
|
<para> |
|
Iterate over each glyph. |
|
</para> |
|
</listitem> |
|
</orderedlist> |
|
<programlisting language="C"> |
|
for (i = 0; i < glyph_count; ++i) { |
|
glyphid = glyph_info[i].codepoint; |
|
x_offset = glyph_pos[i].x_offset / 64.0; |
|
y_offset = glyph_pos[i].y_offset / 64.0; |
|
x_advance = glyph_pos[i].x_advance / 64.0; |
|
y_advance = glyph_pos[i].y_advance / 64.0; |
|
draw_glyph(glyphid, cursor_x + x_offset, cursor_y + y_offset); |
|
cursor_x += x_advance; |
|
cursor_y += y_advance; |
|
} |
|
</programlisting> |
|
<orderedlist numeration="arabic"> |
|
<listitem override="7"> |
|
<para> |
|
Tidy up. |
|
</para> |
|
</listitem> |
|
</orderedlist> |
|
<programlisting language="C"> |
|
hb_buffer_destroy(buf); |
|
hb_font_destroy(hb_ft_font); |
|
</programlisting> |
|
|
|
<para> |
|
This example shows enough to get us started using HarfBuzz. Now we |
|
are going to use the remainder of HarfBuzz's API to refine that |
|
example and improve our text shaping capabilities. |
|
</para> |
|
</chapter>
|
|
|