File: ttb_translate.c

package info (click to toggle)
brltty 6.7-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,240 kB
  • sloc: ansic: 148,859; java: 13,418; sh: 9,623; xml: 5,699; tcl: 2,634; makefile: 2,333; awk: 713; lisp: 366; python: 321; ml: 301
file content (371 lines) | stat: -rw-r--r-- 9,760 bytes parent folder | download | duplicates (3)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2024 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

#include "prologue.h"

#include <stdio.h>
#include <string.h>

#include "log.h"
#include "lock.h"
#include "file.h"
#include "charset.h"
#include "ttb.h"
#include "ttb_internal.h"
#include "brl_dots.h"
#include "brl_types.h"
#include "prefs.h"

static const unsigned char internalTextTableBytes[] = {
#include "ttb.auto.h"
};

static TextTable internalTextTable = {
  .header.bytes = internalTextTableBytes,
  .size = 0
};

TextTable *textTable = &internalTextTable;

static LockDescriptor *
getTextTableLock (void) {
  static LockDescriptor *lock = NULL;
  return getLockDescriptor(&lock, "text-table");
}

void
lockTextTable (void) {
  obtainExclusiveLock(getTextTableLock());
}

void
unlockTextTable (void) {
  releaseLock(getTextTableLock());
}

static inline const void *
getTextTableItem (TextTable *table, TextTableOffset offset) {
  return &table->header.bytes[offset];
}

static inline const UnicodeGroupEntry *
getUnicodeGroupEntry (TextTable *table, wchar_t character) {
  TextTableOffset offset = table->header.fields->unicodeGroups[UNICODE_GROUP_NUMBER(character)];
  if (offset) return getTextTableItem(table, offset);
  return NULL;
}

static inline const UnicodePlaneEntry *
getUnicodePlaneEntry (TextTable *table, wchar_t character) {
  const UnicodeGroupEntry *group = getUnicodeGroupEntry(table, character);

  if (group) {
    TextTableOffset offset = group->planes[UNICODE_PLANE_NUMBER(character)];
    if (offset) return getTextTableItem(table, offset);
  }

  return NULL;
}

static inline const UnicodeRowEntry *
getUnicodeRowEntry (TextTable *table, wchar_t character) {
  const UnicodePlaneEntry *plane = getUnicodePlaneEntry(table, character);

  if (plane) {
    TextTableOffset offset = plane->rows[UNICODE_ROW_NUMBER(character)];
    if (offset) return getTextTableItem(table, offset);
  }

  return NULL;
}

static inline const unsigned char *
getUnicodeCell (TextTable *table, wchar_t character) {
  const UnicodeRowEntry *row = getUnicodeRowEntry(table, character);

  if (row) {
    unsigned int cellNumber = UNICODE_CELL_NUMBER(character);
    if (BITMASK_TEST(row->cellDefined, cellNumber)) return &row->cells[cellNumber];
  }

  return NULL;
}

void
setTryBaseCharacter (TextTable *table, unsigned char yes) {
  table->options.tryBaseCharacter = yes;
}

static int
searchTextTableAlias (const void *target, const void *element) {
  const wchar_t *reference = target;
  const TextTableAliasEntry *alias = element;

  if (*reference < alias->from) return -1;
  if (*reference > alias->from) return 1;
  return 0;
}

const TextTableAliasEntry *
locateTextTableAlias (wchar_t character, const TextTableAliasEntry *array, size_t count) {
  const TextTableAliasEntry *alias = bsearch(
    &character, array, count, sizeof(*array), searchTextTableAlias
  );

  if (alias) return alias;
  return NULL;
}

static const TextTableAliasEntry *
findTextTableAlias (TextTable *table, wchar_t character) {
  const TextTableHeader *header = table->header.fields;

  return locateTextTableAlias(character, getTextTableItem(table, header->aliasArray), header->aliasCount);
}

static int
getDotsForAliasedCharacter (TextTable *table, wchar_t *character, unsigned char *dots) {
  unsigned int iterationLimit = 0X10;
  wchar_t characterEncountered[iterationLimit];
  unsigned int iterationNumber = 0;

  while (iterationNumber < iterationLimit) {
    if (wmemchr(characterEncountered, *character, iterationNumber)) break;
    characterEncountered[iterationNumber++] = *character;
    const UnicodeRowEntry *row = getUnicodeRowEntry(table, *character);

    if (row) {
      unsigned int cellNumber = UNICODE_CELL_NUMBER(*character);

      if (BITMASK_TEST(row->cellDefined, cellNumber)) {
        *dots = row->cells[cellNumber];
        return 1;
      }

      if (BITMASK_TEST(row->cellAliased, cellNumber)) {
        const TextTableAliasEntry *alias = findTextTableAlias(table, *character);

        if (alias) {
          *character = alias->to;
          continue;
        }
      }
    }

    break;
  }

  return 0;
}

typedef struct {
  TextTable *const table;
  unsigned char dots;
} SetBrailleRepresentationData;

static int
setBrailleRepresentation (wchar_t character, void *data) {
  SetBrailleRepresentationData *sbr = data;
  const unsigned char *cell = getUnicodeCell(sbr->table, character);

  if (cell) {
    sbr->dots = *cell;
    return 1;
  }

  return 0;
}

unsigned char
convertCharacterToDots (TextTable *table, wchar_t character) {
  uint32_t row = character & ~UNICODE_CELL_MASK;

  switch (row) {
#if WCHAR_MAX >= UINT16_MAX
    case 0XF000: {
      wint_t wc = convertCharToWchar(character & UNICODE_CELL_MASK);
      if (wc == WEOF) break;
      character = wc;
    }
    /* fall through */
#endif /* WCHAR_MAX >= UINT16_MAX */

    default: {
      {
        unsigned char dots;
        if (getDotsForAliasedCharacter(table, &character, &dots)) return dots;
      }

      if (character == UNICODE_REPLACEMENT_CHARACTER) break;

      if (table->options.tryBaseCharacter) {
        SetBrailleRepresentationData sbr = {
          .table = table,
          .dots = 0
        };

        if (handleBestCharacter(character, setBrailleRepresentation, &sbr)) {
          return sbr.dots;
        }
      }

      break;
    }
  }

  if (row == UNICODE_BRAILLE_ROW) {
    return character & UNICODE_CELL_MASK;
  }

  {
    const unsigned char *cell = table->cells.replacementCharacter;
    if (cell) return *cell;
  }

  return BRL_DOT_1 | BRL_DOT_2 | BRL_DOT_3 | BRL_DOT_4 | BRL_DOT_5 | BRL_DOT_6 | BRL_DOT_7 | BRL_DOT_8;
}

wchar_t
convertDotsToCharacter (TextTable *table, unsigned char dots) {
  const TextTableHeader *header = table->header.fields;
  if (BITMASK_TEST(header->inputCharacterDefined, dots)) return header->inputCharacters[dots];
  return UNICODE_REPLACEMENT_CHARACTER;
}

wchar_t
convertInputToCharacter (unsigned char dots) {
  switch (prefs.brailleTypingMode) {
    case BRL_TYPING_TEXT:
      return convertDotsToCharacter(textTable, dots);

    default:
      logMessage(LOG_WARNING, "unknown braille typing mode: %u", prefs.brailleTypingMode);
      /* fall through */
    case BRL_TYPING_DOTS:
      return UNICODE_BRAILLE_ROW | dots;
  }
}

int
replaceTextTable (const char *directory, const char *name) {
  TextTable *newTable = NULL;

  if (*name) {
    char *path;

    if ((path = makeTextTablePath(directory, name))) {
      logMessage(LOG_DEBUG, "compiling text table: %s", path);

      if (!(newTable = compileTextTable(path))) {
        logMessage(LOG_ERR, "%s: %s", gettext("cannot compile text table"), path);
      }

      free(path);
    }
  } else {
    newTable = &internalTextTable;
  }

  if (newTable) {
    TextTable *oldTable = textTable;

    lockTextTable();
      textTable = newTable;
    unlockTextTable();

    destroyTextTable(oldTable);
    return 1;
  }

  logMessage(LOG_ERR, "%s: %s", gettext("cannot load text table"), name);
  return 0;
}

size_t
getTextTableRowsMask (TextTable *table, uint8_t *mask, size_t size) {
  size_t result = 0;
  memset(mask, 0, size);

  for (unsigned int groupNumber=0; groupNumber<UNICODE_GROUP_COUNT; groupNumber+=1) {
    TextTableOffset groupOffset = table->header.fields->unicodeGroups[groupNumber];

    if (groupOffset) {
      const UnicodeGroupEntry *group = getTextTableItem(table, groupOffset);

      for (unsigned int planeNumber=0; planeNumber<UNICODE_PLANES_PER_GROUP; planeNumber+=1) {
        TextTableOffset planeOffset = group->planes[planeNumber];

        if (planeOffset) {
          const UnicodePlaneEntry *plane = getTextTableItem(table, planeOffset);

          for (unsigned int rowNumber=0; rowNumber<UNICODE_ROWS_PER_PLANE; rowNumber+=1) {
            TextTableOffset rowOffset = plane->rows[rowNumber];

            if (rowOffset) {
              uint32_t row = UNICODE_CHARACTER(groupNumber, planeNumber, rowNumber, 0) >> UNICODE_ROW_SHIFT;
              uint32_t index = row / 8;
              if (index >= size) goto done;
              mask[index] |= 1 << (row % 8);
              result = index + 1;
            }
          }
        }
      }
    }
  }

done:
  return result;
}

int
getTextTableRowCells (TextTable *table, uint32_t rowIndex, uint8_t *cells, uint8_t *defined) {
  wchar_t character = rowIndex << UNICODE_ROW_SHIFT;
  const UnicodeRowEntry *row = getUnicodeRowEntry(table, character);
  if (!row) return 0;

  int maskIndex = -1;
  uint8_t maskBit = 0;

  for (unsigned int cellNumber=0; cellNumber<UNICODE_CELLS_PER_ROW; cellNumber+=1) {
    unsigned char *cell = &cells[cellNumber];
    *cell = 0;

    if (!maskBit) {
      defined[++maskIndex] = 0;
      maskBit = 1;
    }

    if (BITMASK_TEST(row->cellDefined, cellNumber)) {
      *cell = row->cells[cellNumber];
      defined[maskIndex] |= maskBit;
    } else if (BITMASK_TEST(row->cellAliased, cellNumber)) {
      wchar_t wc = character | (cellNumber << UNICODE_CELL_SHIFT);
      unsigned char dots;

      if (getDotsForAliasedCharacter(table, &wc, &dots)) {
        *cell = dots;
        defined[maskIndex] |= maskBit;
      }
    }

    maskBit <<= 1;
  }

  return 1;
}