From 18781a787eb8aca7dc19d53189a7ef65856bb746 Mon Sep 17 00:00:00 2001 From: James Bursa Date: Sun, 1 May 2005 10:19:59 +0000 Subject: [project @ 2005-05-01 10:19:59 by bursa] Add Python module. Add support for compiling with Norcroft (mainly required for the Python module). svn path=/import/rufl/; revision=2463 --- makefile | 44 ++++++++++-- rufl_init.c | 2 +- rufl_internal.h | 6 ++ ruflmodule.c | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ strfuncs.c | 35 ++++++++++ strfuncs.h | 5 ++ 6 files changed, 290 insertions(+), 6 deletions(-) create mode 100644 ruflmodule.c create mode 100644 strfuncs.c create mode 100644 strfuncs.h diff --git a/makefile b/makefile index d15786e..d058f7e 100644 --- a/makefile +++ b/makefile @@ -5,6 +5,18 @@ # Copyright 2005 James Bursa # +# choose one of the two below +COMPILER = gcc +#COMPILER = norcroft + + +SOURCE = rufl_init.c rufl_quit.c rufl_dump_state.c \ + rufl_character_set_test.c \ + rufl_paint.c rufl_glyph_map.c rufl_invalidate_cache.c + + +ifeq ($(COMPILER), gcc) +# cross-compiling using gccsdk CC = /home/riscos/cross/bin/gcc CFLAGS = -std=c99 -O3 -W -Wall -Wundef -Wpointer-arith -Wcast-qual \ -Wcast-align -Wwrite-strings -Wstrict-prototypes \ @@ -13,15 +25,37 @@ CFLAGS = -std=c99 -O3 -W -Wall -Wundef -Wpointer-arith -Wcast-qual \ -mpoke-function-name -I/home/riscos/env/include LIBS = -L/home/riscos/env/lib -loslib -SOURCE = rufl_init.c rufl_quit.c rufl_dump_state.c \ - rufl_character_set_test.c \ - rufl_paint.c rufl_glyph_map.c rufl_invalidate_cache.c - all: rufl.o rufl_test,ff8 rufl_chars,ff8 - rufl.o: $(SOURCE) rufl.h rufl_internal.h Glyphs $(CC) $(CFLAGS) -c -o $@ $(SOURCE) +else +# compiling on RISC OS using Norcroft +CC = cc +CFLAGS = -fn -ecz -wap -IOSLib: -DNDEBUG +LD = link +LDFLAGS = -aof +LIBS = OSLib:o.oslib32 +MKDLK = makedlk +SOURCE += strfuncs.c + +all: rufl.o rufl/pyd rufl_test,ff8 rufl_chars,ff8 +rufl.o: o.rufl +o.rufl: $(OBJS) rufl.h rufl_internal.h Glyphs + $(LD) $(LDFLAGS) -o $@ $(OBJS) +o.ruflmodule: ruflmodule.o rufl.o + $(LD) $(LDFLAGS) -o $@ $^ $(LIBS) +ruflmodule.o: ruflmodule.c + $(CC) -fn -wp -IPyInc:Include,PyInc:RISCOS,TCPIPLibs:,OSLib: -c $@ $< +rufl/pyd: o.ruflmodule + $(MKDLK) -s .RISCOS.s.linktab -o $< -d $@ -e initrufl +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +endif + + +# common rules rufl_glyph_map.c: Glyphs makeglyphs ./makeglyphs < Glyphs > $@ diff --git a/rufl_init.c b/rufl_init.c index 00ff146..5668a73 100644 --- a/rufl_init.c +++ b/rufl_init.c @@ -546,7 +546,7 @@ rufl_code rufl_init_read_encoding(font_f font, } fp = fopen(filename, "r"); - if (!fp && errno == ENOENT) + if (!fp) /* many "symbol" fonts have no encoding file: assume Latin 1 */ fp = fopen("Resources:$.Fonts.Encodings.Latin1", "r"); if (!fp) diff --git a/rufl_internal.h b/rufl_internal.h index d0c9ed6..8cd1759 100644 --- a/rufl_internal.h +++ b/rufl_internal.h @@ -8,6 +8,9 @@ #include #include "oslib/font.h" #include "rufl.h" +#ifdef __CC_NORCROFT +#include "strfuncs.h" +#endif /** The available characters in a font. The range which can be represented is @@ -154,6 +157,9 @@ extern const size_t rufl_glyph_map_size; #ifndef NDEBUG +#ifdef __CC_NORCROFT +#define __PRETTY_FUNCTION__ __func__ +#endif #define LOG(format, ...) (fprintf(stderr, __FILE__ " %s %i: ", \ __PRETTY_FUNCTION__, __LINE__), fprintf(stderr, format, \ __VA_ARGS__), fprintf(stderr, "\n")) diff --git a/ruflmodule.c b/ruflmodule.c new file mode 100644 index 0000000..66fb689 --- /dev/null +++ b/ruflmodule.c @@ -0,0 +1,204 @@ +/* + * This file is part of RUfl + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license + * Copyright 2005 James Bursa + */ + +/* Python module for RUfl. */ + +#include "Python.h" +#include "rufl.h" + + +static char pyrufl_paint__doc__[] = +"paint(font_family, font_style, font_size, string, x, y, flags)\n\n" +"Render Unicode text." +; + +static PyObject * +pyrufl_paint(PyObject *self /* Not used */, PyObject *args) +{ + const char *font_family; + rufl_style font_style; + unsigned int font_size; + const char *string; + int length; + int x; + int y; + unsigned int flags; + + if (!PyArg_ParseTuple(args, "siIs#iiI", + &font_family, &font_style, &font_size, + &string, &length, &x, &y, &flags)) + return NULL; + + rufl_paint(font_family, font_style, font_size, string, length, + x, y, flags); + + Py_INCREF(Py_None); + return Py_None; +} + + +static char pyrufl_width__doc__[] = +"width(font_family, font_style, font_size, string, flags)\n\n" +"Return the width of Unicode text." +; + +static PyObject * +pyrufl_width(PyObject *self /* Not used */, PyObject *args) +{ + const char *font_family; + rufl_style font_style; + unsigned int font_size; + const char *string; + int length; + unsigned int flags; + int width = 0; + + if (!PyArg_ParseTuple(args, "siIs#I", + &font_family, &font_style, &font_size, + &string, &length, &flags)) + return NULL; + + rufl_width(font_family, font_style, font_size, string, length, + &width); + + return PyInt_FromLong(width); +} + + +static char pyrufl_x_to_offset__doc__[] = +"x_to_offset(font_family, font_style, font_size, string, click_x)\n\n" +"Return a pair of the character offset in string that click_x falls,\n" +"and the actual x coordinate for that character offset." +; + +static PyObject * +pyrufl_x_to_offset(PyObject *self /* Not used */, PyObject *args) +{ + const char *font_family; + rufl_style font_style; + unsigned int font_size; + const char *string; + int length; + int click_x; + size_t char_offset = 0; + int actual_x = 0; + + if (!PyArg_ParseTuple(args, "siIs#i", + &font_family, &font_style, &font_size, + &string, &length, &click_x)) + return NULL; + + rufl_x_to_offset(font_family, font_style, font_size, string, length, + click_x, + &char_offset, &actual_x); + + return Py_BuildValue("ii", (int) char_offset, actual_x); +} + + +static char pyrufl_split__doc__[] = +"split(font_family, font_style, font_size, string, width)\n\n" +"Return a pair of the character offset in string that fits in width,\n" +"and the actual x coordinate for that character offset." +; + +static PyObject * +pyrufl_split(PyObject *self /* Not used */, PyObject *args) +{ + const char *font_family; + rufl_style font_style; + unsigned int font_size; + const char *string; + int length; + int width; + size_t char_offset = 0; + int actual_x = 0; + + if (!PyArg_ParseTuple(args, "siIs#i", + &font_family, &font_style, &font_size, + &string, &length, &width)) + return NULL; + + rufl_split(font_family, font_style, font_size, string, length, + width, + &char_offset, &actual_x); + + return Py_BuildValue("ii", (int) char_offset, actual_x); +} + + +static char pyrufl_invalidate_cache__doc__[] = +"invalidate_cache()\n\n" +"Clear the internal font handle cache.\n" +"Call this function on mode changes or output redirection changes." +; + +static PyObject * +pyrufl_invalidate_cache(PyObject *self /* Not used */, PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + + rufl_invalidate_cache(); + + Py_INCREF(Py_None); + return Py_None; +} + + +/* List of methods defined in the module */ + +static struct PyMethodDef pyrufl_methods[] = { + {"paint", (PyCFunction)pyrufl_paint, METH_VARARGS, pyrufl_paint__doc__}, + {"width", (PyCFunction)pyrufl_width, METH_VARARGS, pyrufl_width__doc__}, + {"x_to_offset", (PyCFunction)pyrufl_x_to_offset, METH_VARARGS, + pyrufl_x_to_offset__doc__}, + {"split", (PyCFunction)pyrufl_split, METH_VARARGS, pyrufl_split__doc__}, + {"invalidate_cache", (PyCFunction)pyrufl_invalidate_cache, METH_VARARGS, + pyrufl_invalidate_cache__doc__}, + + {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */ +}; + + +/* Initialization function for the module (*must* be called initrufl) */ + +static char pyrufl_module_documentation[] = +"This module provides access to the RISC OS Unicode font library\n" +"All string parameters must be in UTF-8." +; + +void +initrufl() +{ + PyObject *module; + rufl_code code; + + code = rufl_init(); + if (code != rufl_OK) + Py_FatalError("rufl_init() failed"); + + Py_AtExit(rufl_quit); + + /* Create the module and add the functions */ + module = Py_InitModule4("rufl", pyrufl_methods, + pyrufl_module_documentation, + (PyObject *) NULL, PYTHON_API_VERSION); + + /* Add some symbolic constants to the module */ + PyModule_AddIntConstant(module, "regular", rufl_REGULAR); + PyModule_AddIntConstant(module, "slanted", rufl_SLANTED); + PyModule_AddIntConstant(module, "bold", rufl_BOLD); + PyModule_AddIntConstant(module, "bold_slanted", rufl_BOLD_SLANTED); + + PyModule_AddIntConstant(module, "blend", rufl_BLEND_FONT); + + /* Check for errors */ + if (PyErr_Occurred()) + Py_FatalError("can't initialize module rufl"); +} + diff --git a/strfuncs.c b/strfuncs.c new file mode 100644 index 0000000..e6c6347 --- /dev/null +++ b/strfuncs.c @@ -0,0 +1,35 @@ +#include +#include +#include + +char *strdup(const char *s) +{ + size_t len = strlen(s); + char *new = malloc(len + 1); + if (!new) + return 0; + memcpy(new, s, len); + new[len] = '\0'; + return new; +} + +char *strndup(const char *s, size_t n) +{ + size_t len = strlen(s); + if (n < len) + len = n; + char *new = malloc(len + 1); + if (!new) + return 0; + memcpy(new, s, len); + new[len] = '\0'; + return new; +} + +int strcasecmp(const char *s1, const char *s2) +{ + int i; + while ((i = tolower(*s1)) && i == tolower(*s2)) + s1++, s2++; + return ((unsigned char) tolower(*s1) - (unsigned char) tolower(*s2)); +} diff --git a/strfuncs.h b/strfuncs.h new file mode 100644 index 0000000..495ea98 --- /dev/null +++ b/strfuncs.h @@ -0,0 +1,5 @@ +#include + +char *strdup(const char *s); +char *strndup(const char *s, size_t n); +int strcasecmp(const char *s1, const char *s2); -- cgit v1.2.3