blob: 1905b84bb4577f23b910c68455b0393e908a2f2b [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>A simple shaping example: HarfBuzz Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="HarfBuzz Manual">
<link rel="up" href="getting-started.html" title="Getting started with HarfBuzz">
<link rel="prev" href="ch03s02.html" title="Terminology">
<link rel="next" href="shaping-concepts.html" title="Shaping concepts">
<meta name="generator" content="GTK-Doc V1.25 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="getting-started.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="ch03s02.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="shaping-concepts.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id-1.2.4.4"></a>A simple shaping example</h2></div></div></div>
<p>
Below is the simplest HarfBuzz shaping example possible.
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>
Create a buffer and put your text in it.
</p></li></ol></div>
<pre class="programlisting">
#include &lt;hb.h&gt;
hb_buffer_t *buf;
buf = hb_buffer_create();
hb_buffer_add_utf8(buf, text, -1, 0, -1);
</pre>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="2"><p>
Guess the script, language and direction of the buffer.
</p></li></ol></div>
<pre class="programlisting">
hb_buffer_set_direction(buf, HB_DIRECTION_LTR);
hb_buffer_set_script(buf, HB_SCRIPT_LATIN);
hb_buffer_set_language(buf, hb_language_from_string("en", -1));
</pre>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="3"><p>
Create a face and a font, using FreeType for now.
</p></li></ol></div>
<pre class="programlisting">
#include &lt;hb-ft.h&gt;
FT_New_Face(ft_library, font_path, index, &amp;face);
FT_Set_Char_Size(face, 0, 1000, 0, 0);
hb_font_t *font = hb_ft_font_create(face);
</pre>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="4"><p>
Shape!
</p></li></ol></div>
<pre class="programlisting">
hb_shape(font, buf, NULL, 0);
</pre>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="5"><p>
Get the glyph and position information.
</p></li></ol></div>
<pre class="programlisting">
hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buf, &amp;glyph_count);
hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buf, &amp;glyph_count);
</pre>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="6"><p>
Iterate over each glyph.
</p></li></ol></div>
<pre class="programlisting">
for (i = 0; i &lt; 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;
}
</pre>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="7"><p>
Tidy up.
</p></li></ol></div>
<pre class="programlisting">
hb_buffer_destroy(buf);
hb_font_destroy(hb_ft_font);
</pre>
<p>
This example shows enough to get us started using HarfBuzz. In
the sections that follow, we will use the remainder of
HarfBuzz's API to refine and extend the example and improve its
text-shaping capabilities.
</p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.25</div>
</body>
</html>