aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2020-04-29 20:43:52 +0200
committerMattias Andrée <maandree@kth.se>2020-04-29 20:43:52 +0200
commit801ef2b33b102051d8ac4bbc646720edfeec8df7 (patch)
tree7a0083ef515ca67479f96749ece217c93d666378
parentm (diff)
downloadlibskrift-801ef2b33b102051d8ac4bbc646720edfeec8df7.tar.gz
libskrift-801ef2b33b102051d8ac4bbc646720edfeec8df7.tar.bz2
libskrift-801ef2b33b102051d8ac4bbc646720edfeec8df7.tar.xz
Add support for mirrored text (mirror both text and characters for right-to-left)
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--common.h1
-rw-r--r--demo.c4
-rw-r--r--libskrift_create_context.c1
-rw-r--r--libskrift_draw_text.c37
4 files changed, 39 insertions, 4 deletions
diff --git a/common.h b/common.h
index 5caf373..79f5507 100644
--- a/common.h
+++ b/common.h
@@ -2,6 +2,7 @@
#include <sys/mman.h>
#include <sys/stat.h>
+#include <alloca.h>
#include <endian.h>
#include <errno.h>
#include <math.h>
diff --git a/demo.c b/demo.c
index 0e9a6d4..dbc20c7 100644
--- a/demo.c
+++ b/demo.c
@@ -17,8 +17,8 @@ main(void)
rendering.smoothing = LIBSKRIFT_SUBPIXEL;
rendering.subpixel_order = LIBSKRIFT_NONE;
- rendering.flags = LIBSKRIFT_MIRROR_CHARS * 0;
- rendering.interletter_spacing = -5;
+ rendering.flags = LIBSKRIFT_MIRROR_CHARS | LIBSKRIFT_MIRROR_TEXT;
+ rendering.interletter_spacing = 2;
if (libskrift_open_font_file(&font, DEMO_FONT)) {
perror("libskrift_open_font_file");
diff --git a/libskrift_create_context.c b/libskrift_create_context.c
index c1732c0..18cf961 100644
--- a/libskrift_create_context.c
+++ b/libskrift_create_context.c
@@ -6,6 +6,7 @@
LIBSKRIFT_NO_AUTOKERNING)
#define IMPLEMENTED_FLAGS (LIBSKRIFT_REMOVE_GAMMA |\
+ LIBSKRIFT_MIRROR_TEXT |\
LIBSKRIFT_MIRROR_CHARS |\
FORCED_FLAGS) /* libschrift does not add gamma, so not handling is required */
diff --git a/libskrift_draw_text.c b/libskrift_draw_text.c
index 05f278c..cce2991 100644
--- a/libskrift_draw_text.c
+++ b/libskrift_draw_text.c
@@ -1,28 +1,61 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
+static void
+reverse_text(const char *text, char *s, size_t off)
+{
+ size_t n;
+ s[off] = '\0';
+ for (; *text; text += n) {
+ off -= n = grapheme_len(text);
+ memcpy(&s[off], text, n);
+ }
+}
+
int
libskrift_draw_text(LIBSKRIFT_CONTEXT *ctx, const char *text, const struct libskrift_colour *colour,
int16_t x, int16_t y, struct libskrift_image *image)
{
struct libskrift_saved_grapheme saved = LIBSKRIFT_NO_SAVED_GRAPHEME;
struct libskrift_glyph *glyph;
+ char *buffer = NULL;
double xpos = 0, ypos = 0;
ssize_t len;
+ size_t n;
int r;
+ if (ctx->rendering.flags & LIBSKRIFT_MIRROR_TEXT) {
+ n = strlen(text);
+ if (n < 1024) {
+ buffer = alloca(n + 1);
+ } else {
+ buffer = malloc(n + 1);
+ if (!buffer)
+ return -1;
+ }
+ reverse_text(text, buffer, n);
+ text = buffer;
+ if (n < 1024)
+ buffer = NULL;
+ }
+
for (; *text; text += len) {
len = libskrift_get_cluster_glyph(ctx, text, &saved, xpos, ypos, &glyph);
- if (len < 0)
+ if (len < 0) {
+ free(buffer);
return -1;
+ }
r = libskrift_apply_glyph(ctx, glyph, colour, x, y, image);
xpos += (glyph->advance + ctx->rendering.interletter_spacing) * ctx->x_advancement;
ypos += (glyph->advance + ctx->rendering.interletter_spacing) * ctx->y_advancement;
free(glyph);
- if (r)
+ if (r) {
+ free(buffer);
return -1;
+ }
}
+ free(buffer);
return 0;
}