Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / docs / html / a-simple-shaping-example.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>A simple shaping example: HarfBuzz Manual</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="index.html" title="HarfBuzz Manual">
8 <link rel="up" href="getting-started.html" title="Getting started with HarfBuzz">
9 <link rel="prev" href="terminology.html" title="Terminology">
10 <link rel="next" href="shaping-concepts.html" title="Shaping concepts">
11 <meta name="generator" content="GTK-Doc V1.32 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 </head>
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
16 <td width="100%" align="left" class="shortcuts"></td>
17 <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
18 <td><a accesskey="u" href="getting-started.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
19 <td><a accesskey="p" href="terminology.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
20 <td><a accesskey="n" href="shaping-concepts.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
21 </tr></table>
22 <div class="section">
23 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
24 <a name="a-simple-shaping-example"></a>A simple shaping example</h2></div></div></div>
25 <p>
26       Below is the simplest HarfBuzz shaping example possible.
27     </p>
28 <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>
29           Create a buffer and put your text in it.
30         </p></li></ol></div>
31 <pre class="programlisting">
32       #include &lt;hb.h&gt;
33
34       hb_buffer_t *buf;
35       buf = hb_buffer_create();
36       hb_buffer_add_utf8(buf, text, -1, 0, -1);
37     </pre>
38 <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="2"><p>
39           Set the script, language and direction of the buffer.
40         </p></li></ol></div>
41 <pre class="programlisting">
42       hb_buffer_set_direction(buf, HB_DIRECTION_LTR);
43       hb_buffer_set_script(buf, HB_SCRIPT_LATIN);
44       hb_buffer_set_language(buf, hb_language_from_string("en", -1));
45     </pre>
46 <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="3"><p>
47           Create a face and a font from a font file.
48         </p></li></ol></div>
49 <pre class="programlisting">
50       hb_blob_t *blob = hb_blob_create_from_file(filename); /* or hb_blob_create_from_file_or_fail() */
51       hb_face_t *face = hb_face_create(blob, 0);
52       hb_font_t *font = hb_font_create(face);
53     </pre>
54 <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="4"><p>
55           Shape!
56         </p></li></ol></div>
57 <pre class="programlisting">
58       hb_shape(font, buf, NULL, 0);
59     </pre>
60 <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="5"><p>
61           Get the glyph and position information.
62         </p></li></ol></div>
63 <pre class="programlisting">
64       unsigned int glyph_count;
65       hb_glyph_info_t *glyph_info    = hb_buffer_get_glyph_infos(buf, &amp;glyph_count);
66       hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buf, &amp;glyph_count);
67     </pre>
68 <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="6"><p>
69           Iterate over each glyph.
70         </p></li></ol></div>
71 <pre class="programlisting">
72       hb_position_t cursor_x = 0;
73       hb_position_t cursor_y = 0;
74       for (unsigned int i = 0; i &lt; glyph_count; i++) {
75           hb_codepoint_t glyphid  = glyph_info[i].codepoint;
76           hb_position_t x_offset  = glyph_pos[i].x_offset;
77           hb_position_t y_offset  = glyph_pos[i].y_offset;
78           hb_position_t x_advance = glyph_pos[i].x_advance;
79           hb_position_t y_advance = glyph_pos[i].y_advance;
80        /* draw_glyph(glyphid, cursor_x + x_offset, cursor_y + y_offset); */
81           cursor_x += x_advance;
82           cursor_y += y_advance;
83       }
84     </pre>
85 <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="7"><p>
86           Tidy up.
87         </p></li></ol></div>
88 <pre class="programlisting">
89       hb_buffer_destroy(buf);
90       hb_font_destroy(font);
91       hb_face_destroy(face);
92       hb_blob_destroy(blob);
93     </pre>
94 <p>
95       This example shows enough to get us started using HarfBuzz. In
96       the sections that follow, we will use the remainder of
97       HarfBuzz's API to refine and extend the example and improve its
98       text-shaping capabilities.
99     </p>
100 </div>
101 <div class="footer">
102 <hr>Generated by GTK-Doc V1.32</div>
103 </body>
104 </html>