| 1 |
/* Pango |
| 2 |
* pango-segmentation.c: Test Pango line breaking |
| 3 |
* |
| 4 |
* Copyright (C) 2021 Red Hat, Inc |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Library General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Library General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Library General Public |
| 17 |
* License along with this library; if not, write to the |
| 18 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 |
* Boston, MA 02111-1307, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <glib.h> |
| 23 |
#include <pango/pangocairo.h> |
| 24 |
#include <string.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <locale.h> |
| 27 |
|
| 28 |
#ifndef G_OS_WIN32 |
| 29 |
#include <unistd.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
typedef enum { |
| 33 |
GRAPHEME, |
| 34 |
WORD, |
| 35 |
WORD_START_END, |
| 36 |
LINE, |
| 37 |
SENTENCE, |
| 38 |
SENTENCE_START_END, |
| 39 |
} BreakKind; |
| 40 |
|
| 41 |
static BreakKind |
| 42 |
kind_from_string (const char *str) |
| 43 |
{ |
| 44 |
if (strcmp (str, "grapheme") == 0) |
| 45 |
return GRAPHEME; |
| 46 |
else if (strcmp (str, "word") == 0) |
| 47 |
return WORD; |
| 48 |
else if (strcmp (str, "words") == 0) |
| 49 |
return WORD_START_END; |
| 50 |
else if (strcmp (str, "line") == 0) |
| 51 |
return LINE; |
| 52 |
else if (strcmp (str, "sentence") == 0) |
| 53 |
return SENTENCE; |
| 54 |
else if (strcmp (str, "sentences") == 0) |
| 55 |
return SENTENCE_START_END; |
| 56 |
else |
| 57 |
{ |
| 58 |
g_printerr ("Not a segmentation: %s", str); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
enum { |
| 64 |
NONE, |
| 65 |
BREAK, |
| 66 |
START, |
| 67 |
END |
| 68 |
}; |
| 69 |
|
| 70 |
static gboolean |
| 71 |
show_segmentation (const char *input, |
| 72 |
BreakKind kind) |
| 73 |
{ |
| 74 |
GString *string; |
| 75 |
PangoContext *context; |
| 76 |
gsize length; |
| 77 |
GError *error = NULL; |
| 78 |
PangoLogAttr *attrs; |
| 79 |
int len; |
| 80 |
char *p; |
| 81 |
int i; |
| 82 |
char *text; |
| 83 |
PangoAttrList *attributes; |
| 84 |
PangoLayout *layout; |
| 85 |
|
| 86 |
context = pango_font_map_create_context (pango_cairo_font_map_get_default ()); |
| 87 |
|
| 88 |
string = g_string_new (""); |
| 89 |
|
| 90 |
length = strlen (input); |
| 91 |
len = g_utf8_strlen (input, -1) + 1; |
| 92 |
|
| 93 |
pango_parse_markup (input, -1, 0, &attributes, &text, NULL, &error); |
| 94 |
g_assert_no_error (error); |
| 95 |
|
| 96 |
layout = pango_layout_new (context); |
| 97 |
pango_layout_set_text (layout, text, length); |
| 98 |
pango_layout_set_attributes (layout, attributes); |
| 99 |
|
| 100 |
pango_layout_get_log_attrs (layout, &attrs, &len); |
| 101 |
|
| 102 |
for (i = 0, p = text; i < len; i++, p = g_utf8_next_char (p)) |
| 103 |
{ |
| 104 |
PangoLogAttr log = attrs[i]; |
| 105 |
int indicator = NONE; |
| 106 |
|
| 107 |
switch (kind) |
| 108 |
{ |
| 109 |
case GRAPHEME: |
| 110 |
if (log.is_cursor_position) |
| 111 |
indicator = BREAK; |
| 112 |
break; |
| 113 |
case WORD: |
| 114 |
if (log.is_word_boundary) |
| 115 |
indicator = BREAK; |
| 116 |
break; |
| 117 |
case WORD_START_END: |
| 118 |
if (log.is_word_start && log.is_word_end) |
| 119 |
indicator = BREAK; |
| 120 |
else if (log.is_word_start) |
| 121 |
indicator = START; |
| 122 |
else if (log.is_word_end) |
| 123 |
indicator = END; |
| 124 |
break; |
| 125 |
case LINE: |
| 126 |
if (log.is_line_break) |
| 127 |
indicator = BREAK; |
| 128 |
break; |
| 129 |
case SENTENCE: |
| 130 |
if (log.is_sentence_boundary) |
| 131 |
indicator = BREAK; |
| 132 |
break; |
| 133 |
case SENTENCE_START_END: |
| 134 |
if (log.is_sentence_start && log.is_sentence_end) |
| 135 |
indicator = BREAK; |
| 136 |
else if (log.is_sentence_start) |
| 137 |
indicator = START; |
| 138 |
else if (log.is_sentence_end) |
| 139 |
indicator = END; |
| 140 |
break; |
| 141 |
default: |
| 142 |
g_assert_not_reached (); |
| 143 |
} |
| 144 |
|
| 145 |
switch (indicator) |
| 146 |
{ |
| 147 |
case BREAK: |
| 148 |
g_string_append (string, "|"); |
| 149 |
break; |
| 150 |
case START: |
| 151 |
g_string_append (string, "⌊"); |
| 152 |
break; |
| 153 |
case END: |
| 154 |
g_string_append (string, "⌋"); |
| 155 |
break; |
| 156 |
case NONE: |
| 157 |
default: |
| 158 |
break; |
| 159 |
} |
| 160 |
|
| 161 |
if (i < len - 1) |
| 162 |
{ |
| 163 |
gunichar ch = g_utf8_get_char (p); |
| 164 |
if (ch == 0x20) |
| 165 |
g_string_append (string, " "); |
| 166 |
else if (g_unichar_isgraph (ch) || |
| 167 |
ch == '\n' || |
| 168 |
g_unichar_type (ch) == G_UNICODE_LINE_SEPARATOR || |
| 169 |
g_unichar_type (ch) == G_UNICODE_PARAGRAPH_SEPARATOR) |
| 170 |
g_string_append_unichar (string, ch); |
| 171 |
else |
| 172 |
g_string_append_printf (string, "[%#04x]", ch); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
g_object_unref (layout); |
| 177 |
g_free (attrs); |
| 178 |
g_free (text); |
| 179 |
pango_attr_list_unref (attributes); |
| 180 |
|
| 181 |
g_print ("%s\n", string->str); |
| 182 |
|
| 183 |
g_string_free (string, TRUE); |
| 184 |
|
| 185 |
return TRUE; |
| 186 |
} |
| 187 |
|
| 188 |
int |
| 189 |
main (int argc, char *argv[]) |
| 190 |
{ |
| 191 |
const char *opt_kind = "grapheme"; |
| 192 |
const char *opt_text = NULL; |
| 193 |
gboolean opt_version = FALSE; |
| 194 |
GOptionEntry entries[] = { |
| 195 |
{ "kind", 0, 0, G_OPTION_ARG_STRING, &opt_kind, "Boundary (grapheme/word/words/line/sentence/sentences)", "KIND" }, |
| 196 |
{ "text", 0, 0, G_OPTION_ARG_STRING, &opt_text, "Text to display", "STRING" }, |
| 197 |
{ "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Show version" }, |
| 198 |
{ NULL, }, |
| 199 |
}; |
| 200 |
GOptionContext *context; |
| 201 |
GError *error = NULL; |
| 202 |
char *text; |
| 203 |
gsize len; |
| 204 |
|
| 205 |
g_set_prgname ("pango-segmentation"); |
| 206 |
setlocale (LC_ALL, ""); |
| 207 |
|
| 208 |
context = g_option_context_new ("[FILE]"); |
| 209 |
g_option_context_add_main_entries (context, entries, NULL); |
| 210 |
g_option_context_set_description (context, |
| 211 |
"Show text segmentation as determined by Pango:\n" |
| 212 |
" grapheme - Cursor positions\n" |
| 213 |
" word - Word boundaries\n" |
| 214 |
" words - Word starts and ends\n" |
| 215 |
" line - Possible line breaks\n" |
| 216 |
" sentence - Sentence boundaries\n" |
| 217 |
" sentences - Sentence starts and ends"); |
| 218 |
if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 219 |
{ |
| 220 |
g_printerr ("%s\n", error->message); |
| 221 |
exit (1); |
| 222 |
} |
| 223 |
|
| 224 |
if (opt_version) |
| 225 |
{ |
| 226 |
g_print ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION); |
| 227 |
exit (0); |
| 228 |
} |
| 229 |
|
| 230 |
if (opt_text) |
| 231 |
{ |
| 232 |
text = (char *)opt_text; |
| 233 |
} |
| 234 |
else if (argc > 1) |
| 235 |
{ |
| 236 |
if (!g_file_get_contents (argv[1], &text, &len, &error)) |
| 237 |
{ |
| 238 |
g_printerr ("%s\n", error->message); |
| 239 |
exit (1); |
| 240 |
} |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
g_printerr ("Usage: pango-segmentation [OPTIONS…] FILE\n"); |
| 245 |
exit (1); |
| 246 |
} |
| 247 |
|
| 248 |
show_segmentation (text, kind_from_string (opt_kind)); |
| 249 |
|
| 250 |
return 0; |
| 251 |
} |