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
|
/*
* 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 <string.h>
#include "file.h"
#include "datafile.h"
#include "dataarea.h"
#include "atb.h"
#include "atb_internal.h"
typedef struct {
unsigned char attribute;
unsigned char operation;
} DotData;
typedef struct {
DataArea *area;
DotData dots[8];
} AttributesTableData;
static inline AttributesTableHeader *
getAttributesTableHeader (AttributesTableData *atd) {
return getDataItem(atd->area, 0);
}
static int
makeAttributesToDots (AttributesTableData *atd) {
unsigned char *table = getAttributesTableHeader(atd)->attributesToDots;
unsigned char bits = 0;
do {
unsigned char *cell = &table[bits];
unsigned char dotIndex;
*cell = 0;
for (dotIndex=0; dotIndex<BRL_DOT_COUNT; dotIndex+=1) {
const DotData *dot = &atd->dots[dotIndex];
int isSet = bits & dot->attribute;
if (!isSet == !dot->operation) *cell |= brlDotBits[dotIndex];
}
} while ((bits += 1));
return 1;
}
static int
parseAttributeOperand (DataFile *file, unsigned char *bit, unsigned char *operation, const wchar_t *characters, int length) {
if (length > 1) {
static const wchar_t operators[] = {'~', '='};
const wchar_t *operator = wmemchr(operators, characters[0], ARRAY_COUNT(operators));
if (operator) {
typedef struct {
const wchar_t *name;
unsigned char bit;
} AttributeEntry;
static const AttributeEntry attributeTable[] = {
{WS_C("fg-blue") , 0X01},
{WS_C("fg-green") , 0X02},
{WS_C("fg-red") , 0X04},
{WS_C("fg-bright"), 0X08},
{WS_C("bg-blue") , 0X10},
{WS_C("bg-green") , 0X20},
{WS_C("bg-red") , 0X40},
{WS_C("blink") , 0X80},
{WS_C("bit0"), 0X01},
{WS_C("bit1"), 0X02},
{WS_C("bit2"), 0X04},
{WS_C("bit3"), 0X08},
{WS_C("bit4"), 0X10},
{WS_C("bit5"), 0X20},
{WS_C("bit6"), 0X40},
{WS_C("bit7"), 0X80},
{WS_C("bit01"), 0X01},
{WS_C("bit02"), 0X02},
{WS_C("bit04"), 0X04},
{WS_C("bit08"), 0X08},
{WS_C("bit10"), 0X10},
{WS_C("bit20"), 0X20},
{WS_C("bit40"), 0X40},
{WS_C("bit80"), 0X80},
{NULL, 0X00}
};
const AttributeEntry *attribute = attributeTable;
characters += 1, length -= 1;
while (attribute->name) {
if ((length == wcslen(attribute->name)) &&
(wmemcmp(characters, attribute->name, length) == 0)) {
*bit = attribute->bit;
*operation = operator - operators;
return 1;
}
attribute += 1;
}
reportDataError(file, "invalid attribute name: %.*" PRIws, length, characters);
} else {
reportDataError(file, "invalid attribute operator: %.1" PRIws, &characters[0]);
}
}
return 0;
}
static int
getAttributeOperand (DataFile *file, unsigned char *bit, unsigned char *operation) {
DataOperand attribute;
if (getDataOperand(file, &attribute, "attribute"))
if (parseAttributeOperand(file, bit, operation, attribute.characters, attribute.length))
return 1;
return 0;
}
static DATA_OPERANDS_PROCESSOR(processDotOperands) {
AttributesTableData *atd = data;
int dotIndex;
if (getDotOperand(file, &dotIndex)) {
DotData *dot = &atd->dots[dotIndex];
if (getAttributeOperand(file, &dot->attribute, &dot->operation)) return 1;
}
return 1;
}
static DATA_OPERANDS_PROCESSOR(processAttributesTableOperands) {
BEGIN_DATA_DIRECTIVE_TABLE
DATA_NESTING_DIRECTIVES,
{.name=WS_C("dot"), .processor=processDotOperands},
END_DATA_DIRECTIVE_TABLE
return processDirectiveOperand(file, &directives, "attributes table directive", data);
}
AttributesTable *
compileAttributesTable (const char *name) {
AttributesTable *table = NULL;
if (setTableDataVariables(ATTRIBUTES_TABLE_EXTENSION, ATTRIBUTES_SUBTABLE_EXTENSION)) {
AttributesTableData atd;
memset(&atd, 0, sizeof(atd));
if ((atd.area = newDataArea())) {
if (allocateDataItem(atd.area, NULL, sizeof(AttributesTableHeader), __alignof__(AttributesTableHeader))) {
const DataFileParameters parameters = {
.processOperands = processAttributesTableOperands,
.data = &atd
};
if (processDataFile(name, ¶meters)) {
if (makeAttributesToDots(&atd)) {
if ((table = malloc(sizeof(*table)))) {
table->header.fields = getAttributesTableHeader(&atd);
table->size = getDataSize(atd.area);
resetDataArea(atd.area);
}
}
}
}
destroyDataArea(atd.area);
}
}
return table;
}
void
destroyAttributesTable (AttributesTable *table) {
if (table->size) {
free(table->header.fields);
free(table);
}
}
char *
ensureAttributesTableExtension (const char *path) {
return ensureFileExtension(path, ATTRIBUTES_TABLE_EXTENSION);
}
char *
makeAttributesTablePath (const char *directory, const char *name) {
char *subdirectory = makePath(directory, ATTRIBUTES_TABLES_SUBDIRECTORY);
if (subdirectory) {
char *file = makeFilePath(subdirectory, name, ATTRIBUTES_TABLE_EXTENSION);
free(subdirectory);
if (file) return file;
}
return NULL;
}
|