summaryrefslogtreecommitdiff
path: root/test_fribidi.c
blob: 7fc6e0fc35b081b779bc8cd29a504f13cd04c465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* FriBidi - Library of BiDi algorithm
 * Copyright (C) 1999 Dov Grobgeld
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <stdio.h>
#include "fribidi.h"

#define CASE(s) if (strcmp(S_, s) == 0)

int main(int argc, char *argv[])
{
  int argp=1;
  gchar *fn;
  guchar S_[255];
  FriBidiChar us[255], old_out_us[255];
  FILE *IN;
  int len = 0, old_len = 0;
  gint pad_width = 35;
  gboolean do_use_order = FALSE;
  gboolean do_test_vtol = FALSE;
  gboolean do_cap_as_rtl = FALSE;
  gboolean do_output_only = FALSE;
  gboolean do_no_pad = FALSE;
  gboolean do_print_embedding = FALSE;
  gboolean do_test_changes = FALSE;

  while(argp< argc && argv[argp][0] == '-')
    {
      gchar *S_ = argv[argp++];

      CASE("-help") {
	printf(
	       "test_fribidi - A program for testing the fribidi library\n"
	       "\n"
	       "Syntax:\n"
	       "    test_fribidi [-debug] [-outputonly] [-test_vtol] [-order] [-capital_rtl]\n"
	       "                 [-nopad]\n"
	       "\n"
	       "Description:\n"
	       "    A program for running the BiDi algorithm on all the lines in\n"
	       "    test file.\n"
	       "\n"
	       "Options:\n"
	       "    -debug      Output debug info about the progress of the algorithm\n"
	       "    -outputonly Don't print the original logical strings.\n"
	       "    -test_vtol  Output string is according to the ltov array.\n"
	       "    -test_ltov  Output string is according to the vtol array.\n"
	       "    -capital_rtl  Treat capital letters as RTL letters.\n"
	       "    -test_changes  Output information about changes.\n"
	       );
	exit(0);
      }

      CASE("-outputonly")  { do_output_only++; pad_width = 80; continue; };
      CASE("-test_vtol") { do_test_vtol++; continue; };
      CASE("-print_embedding") { do_print_embedding++; continue; };
      CASE("-debug") { fribidi_set_debug(TRUE); continue; };
      CASE("-order") { do_use_order++; continue; };
      CASE("-capital_rtl") { do_cap_as_rtl++; continue; }; 
      CASE("-nopad") { do_no_pad++; continue; };
      CASE("-test_changes") { do_test_changes++; continue; };

      fprintf(stderr, "Unknown option %s!\n", S_);
      exit(0);
    }
  
  if (argp >= argc) {
    IN = stdin;
  } else {
    fn = argv[argp++];
    IN = fopen(fn, "r");
  }

  while(fgets(S_, sizeof(S_), IN))
    {
      FriBidiChar out_us[255];
      guint16 positionLtoV[255], positionVtoL[255];
      guchar outstring[255];
      guint8 embedding_list[255];
      FriBidiCharType base;
      int i;

      len = strlen(S_);
      
      if (S_[0] == '#')
	continue;
      
      if (len == 1)
	continue;
      
      /* chop */
      if (S_[len-1] == '\n')
	S_[len-1] = '\0';

      len--;

      /* Output before the mapping */
      if (!do_output_only)
	printf("%-35s => ", S_);
      
      if (do_cap_as_rtl)
	for (i=0; i<len; i++)
	  if (S_[i]>='A' && S_[i]<='Z')
	    S_[i]+= 0xE0 - 'A';  /* Map to iso Hebrew */

      fribidi_iso8859_8_to_unicode(S_, us);

      /* Create a bidi string */
      base = FRIBIDI_TYPE_N;
      fribidi_log2vis(us, len, &base, 
		      /* output */
		      out_us,
		      positionLtoV,
		      positionVtoL,
		      embedding_list
		      );

      if (do_test_changes)
	{
	  int change_start, change_len;
	  fribidi_find_string_changes(old_out_us, old_len,
				      out_us, len,
				      &change_start, &change_len);
	  printf("Change start[length] = %d[%d]\n", change_start, change_len);

	  /* Keep the old output string for comparisons */
	  old_len = len;
	  memcpy(old_out_us, out_us, old_len * sizeof(FriBidiChar));
				      
	}
      
      /* Convert it to something to print */
      fribidi_unicode_to_iso8859_8(out_us, len, outstring);

      if (do_cap_as_rtl)
	for (i=0; i<len; i++)
	  if (outstring[i]>=0xE0 && outstring[i]<=0xFA)
	    outstring[i]-= 0xE0 - 'A';  /* Map to capital letters */
      
      if (base == FRIBIDI_TYPE_R && !do_no_pad)
	for (i=0; i<pad_width-len; i++)
	  printf(" ");
      
      if (do_use_order)
	{
	  int i;
	    
	  for (i=0; i<len; i++)
	    /* printf("%c", S_[positionLtoV[i]]); */
	    printf("%d ", positionLtoV[i]); 
	}
      else if (do_test_vtol)
	{
	  for (i=0; i<len; i++)
	    /* printf("%c", outstring[positionVtoL[i]]); */
	    printf("%d ", positionVtoL[i]); 
	}
      else
	{
	  printf("%s", outstring);
	}

      if (base == FRIBIDI_TYPE_L && !do_no_pad)
	for (i=0; i<pad_width-len; i++)
	  printf(" ");

      printf("\n");
      if (do_print_embedding)
	{
	  for (i=0; i<len; i++)
	    printf("%d", embedding_list[i]);
	  printf("\n");
	}
    }
  
  return 0;
}