aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-01-25 09:57:16 +0100
committerMattias Andrée <m@maandree.se>2026-01-25 09:57:16 +0100
commite9b19895c2322f1b9753f67fbe3124eb99bcfe16 (patch)
tree62b61c7c0ca6f4565097dede120da5c04423b6cd
parentAdd crop marks (diff)
downloadcharconv-e9b19895c2322f1b9753f67fbe3124eb99bcfe16.tar.gz
charconv-e9b19895c2322f1b9753f67fbe3124eb99bcfe16.tar.bz2
charconv-e9b19895c2322f1b9753f67fbe3124eb99bcfe16.tar.xz
Add braille
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to '')
-rw-r--r--Makefile6
-rw-r--r--convert-to-braille.c18
-rw-r--r--libcharconv.h35
-rw-r--r--libcharconv_braille.c62
-rw-r--r--libcharconv_latin.c29
5 files changed, 147 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index f2e0bb9..c1b5690 100644
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,8 @@ BIN =\
convert-to-domino-tiles\
convert-to-clock-faces\
convert-to-ocr\
- convert-to-crop-marks
+ convert-to-crop-marks\
+ convert-to-braille
LIBOBJ =\
libcharconv_decode_utf8_.o\
@@ -93,7 +94,8 @@ LIBOBJ =\
libcharconv_domino_tiles_vertical.o\
libcharconv_clock_faces.o\
libcharconv_ocr.o\
- libcharconv_crop_marks.o
+ libcharconv_crop_marks.o\
+ libcharconv_braille.o
LOBJ = $(LIBOBJ:.o=.lo)
diff --git a/convert-to-braille.c b/convert-to-braille.c
new file mode 100644
index 0000000..c5513c9
--- /dev/null
+++ b/convert-to-braille.c
@@ -0,0 +1,18 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+USAGE("");
+
+
+int
+main(int argc, char *argv[])
+{
+ ARGBEGIN {
+ default:
+ usage();
+ } ARGEND;
+ if (argc)
+ usage();
+
+ return convert(&libcharconv_braille);
+}
diff --git a/libcharconv.h b/libcharconv.h
index 5b165b6..613ef1b 100644
--- a/libcharconv.h
+++ b/libcharconv.h
@@ -1278,4 +1278,39 @@ enum libcharconv_result libcharconv_ocr(const char *s, size_t slen, size_t *n, u
enum libcharconv_result libcharconv_crop_marks(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp);
+/**
+ * Convert sequences of unique [1, 8]-digits to BRAILLE PATTERNs,
+ * a sequence can be empty (for the blank pattern), each
+ * sequence can be (and the empty sequence must be) termianted
+ * by a DIGIT ZERO
+ *
+ * @param s Text to convert
+ * @param slen The number of bytes available in `s`
+ * @param n Output parameter for the number of consumed bytes
+ * @param cp Output buffer for the codepoints
+ * @param ncp Input parameter for the number of codepoints that
+ * fit in `cp`, and output parameter for the number
+ * of output codepoints (if it exceeds the original
+ * value of `ncp`, a larger buffer is needed)
+ * @return LIBCHARCONV_NO_CONVERT:
+ * `*n` is the number of bytes from the beginning
+ * of `s` that cannot be converted
+ * LIBCHARCONV_CONVERTED:
+ * `*n` is the number of bytes from the beginning
+ * of `s` that was converted to a codepoint which
+ * is stored in `*cp`
+ * LIBCHARCONV_INDETERMINATE:
+ * If all text has been input, no more can be
+ * converted, otherwise more of the text most
+ * be made available before the function can
+ * determine whether the beginning of `s` can be
+ * converted or what it should be converted to
+ * LIBCHARCONV_CONVERT_IF_END:
+ * As LIBCHARCONV_CONVERTED the entire text has
+ * been input, as LIBCHARCONV_INDETERMINATE
+ * otherwise
+ */
+enum libcharconv_result libcharconv_braille(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp);
+
+
#endif
diff --git a/libcharconv_braille.c b/libcharconv_braille.c
new file mode 100644
index 0000000..3a9807e
--- /dev/null
+++ b/libcharconv_braille.c
@@ -0,0 +1,62 @@
+/* See LICENSE file for copyright and license details. */
+#include "lib-common.h"
+
+
+enum libcharconv_result
+libcharconv_braille(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp)
+{
+ unsigned pattern, dot;
+ uint_least32_t c;
+ *n = 0;
+ for (; slen--; s++) {
+ if (s[0] == '0') {
+ if (*n)
+ goto no_conv;
+ c = UINT32_C(0x2800);
+ *n = 1u;
+ goto conv;
+ } else if ('1' <= s[0] && s[0] <= '8') {
+ if (*n)
+ goto no_conv;
+ pattern = 1u << (s[0] - '1');
+ *n = 1u;
+ for (;;) {
+ if (!slen--)
+ goto indeterminate;
+ if ('1' <= s[*n] && s[*n] <= '8') {
+ dot = 1u << (s[*n] - '1');
+ if (pattern & dot)
+ break;
+ pattern |= dot;
+ *n += 1u;
+ } else if (s[*n] == '0') {
+ *n += 1u;
+ break;
+ } else {
+ break;
+ }
+ }
+ c = UINT32_C(0x2800) | pattern;
+ goto conv;
+ } else {
+ *n += 1u;
+ }
+ }
+ return LIBCHARCONV_NO_CONVERT;
+
+no_conv:
+ if (!*n)
+ *n = 1u;
+ return LIBCHARCONV_NO_CONVERT;
+
+indeterminate:
+ if (*n)
+ goto no_conv;
+ return LIBCHARCONV_INDETERMINATE;
+
+conv:
+ if (*ncp)
+ *cp = c;
+ *ncp = 1u;
+ return LIBCHARCONV_CONVERTED;
+}
diff --git a/libcharconv_latin.c b/libcharconv_latin.c
index de5b618..5124c53 100644
--- a/libcharconv_latin.c
+++ b/libcharconv_latin.c
@@ -8,7 +8,7 @@ libcharconv_latin(const char *s, size_t slen, size_t *n, uint_least32_t *cp, siz
enum libcharconv_result ret = LIBCHARCONV_CONVERTED;
uint_least32_t c;
char c1, c2, c3, c4, c5;
- size_t clen;
+ size_t i, clen;
unsigned num;
*n = 0;
@@ -355,6 +355,33 @@ libcharconv_latin(const char *s, size_t slen, size_t *n, uint_least32_t *cp, siz
c = (uint_least32_t)"SdYAIXC/P_\\"[c - UINT32_C(0x2440)];
goto conv;
+ } else if (UINT32_C(0x2800) <= c && c <= UINT32_C(0x28FF)) {
+ /* braille */
+ if (*n)
+ goto no_conv;
+ *n += clen;
+ i = 0u;
+ if ((c & 0x01u) && *ncp > i)
+ cp[i++] = '1';
+ if ((c & 0x02u) && *ncp > i)
+ cp[i++] = '2';
+ if ((c & 0x04u) && *ncp > i)
+ cp[i++] = '3';
+ if ((c & 0x08u) && *ncp > i)
+ cp[i++] = '4';
+ if ((c & 0x10u) && *ncp > i)
+ cp[i++] = '5';
+ if ((c & 0x20u) && *ncp > i)
+ cp[i++] = '6';
+ if ((c & 0x40u) && *ncp > i)
+ cp[i++] = '7';
+ if ((c & 0x80u) && *ncp > i)
+ cp[i++] = '8';
+ if (*ncp > i)
+ cp[i++] = '0';
+ *ncp = i;
+ return ret;
+
} else {
switch (c) {
/* shogi */