Git init
[external/pango1.0.git] / tests / dump-boundaries.c
1 /* Pango
2  * dump-boundaries.c: Dump text boundaries for a file
3  *
4  * Copyright (C) 1999-2000 Red Hat Software
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 <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <unistd.h>
26
27 #include <pango/pango.h>
28
29 #define CHFORMAT "%0#6x"
30
31 static void fail (const char *format, ...) G_GNUC_PRINTF (1, 2) G_GNUC_NORETURN;
32 static void fail (const char *format, ...)
33 {
34   char *str;
35
36   va_list args;
37
38   va_start (args, format);
39   str = g_strdup_vprintf (format, args);
40   va_end (args);
41
42   fprintf (stderr, "Error: %s\n", str);
43
44   g_free (str);
45
46   exit (1);
47 }
48
49 static void
50 dump_text (const char *text)
51 {
52   unsigned int len;
53   PangoLogAttr *attrs;
54   unsigned int i;
55   gunichar *ucs4;
56
57   if (!g_utf8_validate (text, -1, NULL))
58     fail ("Invalid UTF-8 in file");
59
60   len = g_utf8_strlen (text, -1);
61   attrs = g_new0 (PangoLogAttr, len + 1);
62
63   pango_get_log_attrs (text,
64                        -1,
65                        0,
66                        pango_language_from_string ("C"),
67                        attrs,
68                        len + 1);
69
70   ucs4 = g_utf8_to_ucs4 (text, -1, NULL, NULL, NULL);
71
72   for (i = 0; i < len + 1; i++)
73     {
74       char buf[7] = { '\0', };
75       char *loc;
76
77       g_unichar_to_utf8 (ucs4[i], buf);
78
79       if (*buf == '\n')
80         loc = g_strdup ("\\n");
81       else if (*buf == '\r')
82         loc = g_strdup ("\\r");
83       else
84         loc = g_locale_from_utf8 (buf, -1, NULL, NULL, NULL);
85
86       g_print (CHFORMAT " (%s):\t line_break = %d mandatory_break = %d char_break = %d\n"
87                "     \t\t white = %d cursor_position = %d\n"
88                "     \t\t word_start = %d word_end = %d\n"
89                "     \t\t sentence_boundary = %d sentence_start = %d sentence_end = %d\n",
90                ucs4[i], loc ? loc : "?",
91                attrs[i].is_line_break,
92                attrs[i].is_mandatory_break,
93                attrs[i].is_char_break,
94                attrs[i].is_white,
95                attrs[i].is_cursor_position,
96                attrs[i].is_word_start,
97                attrs[i].is_word_end,
98                attrs[i].is_sentence_boundary,
99                attrs[i].is_sentence_start,
100                attrs[i].is_sentence_end);
101
102       g_free (loc);
103     }
104
105   g_free (ucs4);
106   g_free (attrs);
107 }
108
109 int
110 main (int    argc,
111       char **argv)
112 {
113   gchar *text;
114
115   g_setenv ("PANGO_RC_FILE", "./pangorc", TRUE);
116
117   if (argc < 2)
118     fail ("must give a filename on the command line");
119
120   if (!g_file_get_contents (argv[1], &text, NULL, NULL))
121     fail ("Couldn't open sample text file");
122
123   dump_text (text);
124
125   g_free (text);
126
127   return 0;
128 }
129