|
25 | 25 | <p> |
26 | 26 | Below is the simplest HarfBuzz shaping example possible. |
27 | 27 | </p> |
28 | | -<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p> |
| 28 | +<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> |
| 29 | +<p> |
29 | 30 | Create a buffer and put your text in it. |
30 | | - </p></li></ol></div> |
| 31 | + </p> |
31 | 32 | <pre class="programlisting"> |
32 | | - #include <hb.h> |
| 33 | +#include <hb.h> |
33 | 34 |
|
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> |
| 35 | +hb_buffer_t *buf; |
| 36 | +buf = hb_buffer_create(); |
| 37 | +hb_buffer_add_utf8(buf, text, -1, 0, -1); |
| 38 | +</pre> |
| 39 | +</li></ol></div> |
| 40 | +<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="2"> |
| 41 | +<p> |
39 | 42 | Set the script, language and direction of the buffer. |
40 | | - </p></li></ol></div> |
| 43 | + </p> |
41 | 44 | <pre class="programlisting"> |
42 | | - // If you know the direction, script, and language |
43 | | - hb_buffer_set_direction(buf, HB_DIRECTION_LTR); |
44 | | - hb_buffer_set_script(buf, HB_SCRIPT_LATIN); |
45 | | - hb_buffer_set_language(buf, hb_language_from_string("en", -1)); |
| 45 | +// If you know the direction, script, and language |
| 46 | +hb_buffer_set_direction(buf, HB_DIRECTION_LTR); |
| 47 | +hb_buffer_set_script(buf, HB_SCRIPT_LATIN); |
| 48 | +hb_buffer_set_language(buf, hb_language_from_string("en", -1)); |
46 | 49 |
|
47 | | - // If you don't know the direction, script, and language |
48 | | - hb_buffer_guess_segment_properties(buf); |
49 | | - </pre> |
50 | | -<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="3"><p> |
| 50 | +// If you don't know the direction, script, and language |
| 51 | +hb_buffer_guess_segment_properties(buf); |
| 52 | +</pre> |
| 53 | +</li></ol></div> |
| 54 | +<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="3"> |
| 55 | +<p> |
51 | 56 | Create a face and a font from a font file. |
52 | | - </p></li></ol></div> |
| 57 | + </p> |
53 | 58 | <pre class="programlisting"> |
54 | | - hb_blob_t *blob = hb_blob_create_from_file(filename); /* or hb_blob_create_from_file_or_fail() */ |
55 | | - hb_face_t *face = hb_face_create(blob, 0); |
56 | | - hb_font_t *font = hb_font_create(face); |
57 | | - </pre> |
58 | | -<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="4"><p> |
| 59 | +hb_blob_t *blob = hb_blob_create_from_file(filename); /* or hb_blob_create_from_file_or_fail() */ |
| 60 | +hb_face_t *face = hb_face_create(blob, 0); |
| 61 | +hb_font_t *font = hb_font_create(face); |
| 62 | +</pre> |
| 63 | +</li></ol></div> |
| 64 | +<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="4"> |
| 65 | +<p> |
59 | 66 | Shape! |
60 | | - </p></li></ol></div> |
| 67 | + </p> |
61 | 68 | <pre class="programlisting"> |
62 | | - hb_shape(font, buf, NULL, 0); |
63 | | - </pre> |
64 | | -<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="5"><p> |
| 69 | +hb_shape(font, buf, NULL, 0); |
| 70 | +</pre> |
| 71 | +</li></ol></div> |
| 72 | +<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="5"> |
| 73 | +<p> |
65 | 74 | Get the glyph and position information. |
66 | | - </p></li></ol></div> |
| 75 | + </p> |
67 | 76 | <pre class="programlisting"> |
68 | | - unsigned int glyph_count; |
69 | | - hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count); |
70 | | - hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count); |
71 | | - </pre> |
72 | | -<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="6"><p> |
| 77 | +unsigned int glyph_count; |
| 78 | +hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count); |
| 79 | +hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count); |
| 80 | +</pre> |
| 81 | +</li></ol></div> |
| 82 | +<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="6"> |
| 83 | +<p> |
73 | 84 | Iterate over each glyph. |
74 | | - </p></li></ol></div> |
| 85 | + </p> |
75 | 86 | <pre class="programlisting"> |
76 | | - hb_position_t cursor_x = 0; |
77 | | - hb_position_t cursor_y = 0; |
78 | | - for (unsigned int i = 0; i < glyph_count; i++) { |
79 | | - hb_codepoint_t glyphid = glyph_info[i].codepoint; |
80 | | - hb_position_t x_offset = glyph_pos[i].x_offset; |
81 | | - hb_position_t y_offset = glyph_pos[i].y_offset; |
82 | | - hb_position_t x_advance = glyph_pos[i].x_advance; |
83 | | - hb_position_t y_advance = glyph_pos[i].y_advance; |
84 | | - /* draw_glyph(glyphid, cursor_x + x_offset, cursor_y + y_offset); */ |
85 | | - cursor_x += x_advance; |
86 | | - cursor_y += y_advance; |
87 | | - } |
88 | | - </pre> |
89 | | -<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="7"><p> |
| 87 | +hb_position_t cursor_x = 0; |
| 88 | +hb_position_t cursor_y = 0; |
| 89 | +for (unsigned int i = 0; i < glyph_count; i++) { |
| 90 | + hb_codepoint_t glyphid = glyph_info[i].codepoint; |
| 91 | + hb_position_t x_offset = glyph_pos[i].x_offset; |
| 92 | + hb_position_t y_offset = glyph_pos[i].y_offset; |
| 93 | + hb_position_t x_advance = glyph_pos[i].x_advance; |
| 94 | + hb_position_t y_advance = glyph_pos[i].y_advance; |
| 95 | + /* draw_glyph(glyphid, cursor_x + x_offset, cursor_y + y_offset); */ |
| 96 | + cursor_x += x_advance; |
| 97 | + cursor_y += y_advance; |
| 98 | +} |
| 99 | +</pre> |
| 100 | +</li></ol></div> |
| 101 | +<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem" value="7"> |
| 102 | +<p> |
90 | 103 | Tidy up. |
91 | | - </p></li></ol></div> |
| 104 | + </p> |
92 | 105 | <pre class="programlisting"> |
93 | | - hb_buffer_destroy(buf); |
94 | | - hb_font_destroy(font); |
95 | | - hb_face_destroy(face); |
96 | | - hb_blob_destroy(blob); |
97 | | - </pre> |
| 106 | +hb_buffer_destroy(buf); |
| 107 | +hb_font_destroy(font); |
| 108 | +hb_face_destroy(face); |
| 109 | +hb_blob_destroy(blob); |
| 110 | +</pre> |
| 111 | +</li></ol></div> |
98 | 112 | <p> |
99 | 113 | This example shows enough to get us started using HarfBuzz. In |
100 | 114 | the sections that follow, we will use the remainder of |
|
0 commit comments