diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index 732e6ad7a..a162b8ba6 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -30,6 +30,8 @@ from ._util import isDirectory, isPath import os import sys +LAYOUT_BASIC = 0 +LAYOUT_RAQM = 1 class _imagingft_not_installed(object): # module placeholder @@ -42,8 +44,6 @@ try: except ImportError: core = _imagingft_not_installed() -LAYOUT_BASIC = 0 -LAYOUT_RAQM = 1 # FIXME: add support for pilfont2 format (see FontFile.py) diff --git a/_imagingft.c b/_imagingft.c index 3f005f029..92642e733 100644 --- a/_imagingft.c +++ b/_imagingft.c @@ -28,6 +28,10 @@ #define KEEP_PY_UNICODE #include "py3.h" +#if !defined(_MSC_VER) +#include +#endif + #if !defined(FT_LOAD_TARGET_MONO) #define FT_LOAD_TARGET_MONO FT_LOAD_MONOCHROME #endif @@ -41,9 +45,8 @@ #define FT_ERRORDEF( e, v, s ) { e, s }, #define FT_ERROR_START_LIST { #define FT_ERROR_END_LIST { 0, 0 } }; -#ifdef HAVE_RAQM + #include -#endif #define LAYOUT_FALLBACK 0 #define LAYOUT_RAQM 1 @@ -75,6 +78,45 @@ typedef struct { static PyTypeObject Font_Type; +typedef raqm_t* (*t_raqm_create)(void); +typedef int (*t_raqm_set_text)(raqm_t *rq, + const uint32_t *text, + size_t len); +typedef bool (*t_raqm_set_text_utf8) (raqm_t *rq, + const char *text, + size_t len); +typedef bool (*t_raqm_set_par_direction) (raqm_t *rq, + raqm_direction_t dir); +typedef bool (*t_raqm_add_font_feature) (raqm_t *rq, + const char *feature, + int len); +typedef bool (*t_raqm_set_freetype_face) (raqm_t *rq, + FT_Face face); +typedef bool (*t_raqm_layout) (raqm_t *rq); +typedef raqm_glyph_t* (*t_raqm_get_glyphs) (raqm_t *rq, + size_t *length); +typedef raqm_glyph_t_01* (*t_raqm_get_glyphs_01) (raqm_t *rq, + size_t *length); +typedef void (*t_raqm_destroy) (raqm_t *rq); + +typedef struct { + void* raqm; + int version; + t_raqm_create create; + t_raqm_set_text set_text; + t_raqm_set_text_utf8 set_text_utf8; + t_raqm_set_par_direction set_par_direction; + t_raqm_add_font_feature add_font_feature; + t_raqm_set_freetype_face set_freetype_face; + t_raqm_layout layout; + t_raqm_get_glyphs get_glyphs; + t_raqm_get_glyphs_01 get_glyphs_01; + t_raqm_destroy destroy; +} p_raqm_func; + +static p_raqm_func p_raqm; + + /* round a 26.6 pixel coordinate to the nearest larger integer */ #define PIXEL(x) ((((x)+63) & -64)>>6) @@ -93,6 +135,90 @@ geterror(int code) return NULL; } +static int +setraqm(void) +{ + /* set the static function pointers for dynamic raqm linking */ + p_raqm.raqm = NULL; + + /* Microsoft needs a totally different system */ +#if !defined(_MSC_VER) + p_raqm.raqm = dlopen("libraqm.so.0", RTLD_LAZY); + if (!p_raqm.raqm) { + p_raqm.raqm = dlopen("libraqm.dylib", RTLD_LAZY); + } +#else + p_raqm.raqm = LoadLibrary("libraqm"); +#endif + + if (!p_raqm.raqm) { + return 1; + } + +#if !defined(_MSC_VER) + p_raqm.create = (t_raqm_create)dlsym(p_raqm.raqm, "raqm_create"); + p_raqm.set_text = (t_raqm_set_text)dlsym(p_raqm.raqm, "raqm_set_text"); + p_raqm.set_text_utf8 = (t_raqm_set_text_utf8)dlsym(p_raqm.raqm, "raqm_set_text_utf8"); + p_raqm.set_par_direction = (t_raqm_set_par_direction)dlsym(p_raqm.raqm, "raqm_set_par_direction"); + p_raqm.add_font_feature = (t_raqm_add_font_feature)dlsym(p_raqm.raqm, "raqm_add_font_feature"); + p_raqm.set_freetype_face = (t_raqm_set_freetype_face)dlsym(p_raqm.raqm, "raqm_set_freetype_face"); + p_raqm.layout = (t_raqm_layout)dlsym(p_raqm.raqm, "raqm_layout"); + p_raqm.destroy = (t_raqm_destroy)dlsym(p_raqm.raqm, "raqm_destroy"); + if(dlsym(p_raqm.raqm, "raqm_index_to_position")) { + p_raqm.get_glyphs = (t_raqm_get_glyphs)dlsym(p_raqm.raqm, "raqm_get_glyphs"); + p_raqm.version = 2; + } else { + p_raqm.version = 1; + p_raqm.get_glyphs_01 = (t_raqm_get_glyphs_01)dlsym(p_raqm.raqm, "raqm_get_glyphs"); + } + if (dlerror() || + !(p_raqm.create && + p_raqm.set_text && + p_raqm.set_text_utf8 && + p_raqm.set_par_direction && + p_raqm.add_font_feature && + p_raqm.set_freetype_face && + p_raqm.layout && + (p_raqm.get_glyphs || p_raqm.get_glyphs_01) && + p_raqm.destroy)) { + dlclose(p_raqm.raqm); + p_raqm.raqm = NULL; + return 2; + } +#else + p_raqm.create = (t_raqm_create)GetProcAddress(p_raqm.raqm, "raqm_create"); + p_raqm.set_text = (t_raqm_set_text)GetProcAddress(p_raqm.raqm, "raqm_set_text"); + p_raqm.set_text_utf8 = (t_raqm_set_text_utf8)GetProcAddress(p_raqm.raqm, "raqm_set_text_utf8"); + p_raqm.set_par_direction = (t_raqm_set_par_direction)GetProcAddress(p_raqm.raqm, "raqm_set_par_direction"); + p_raqm.add_font_feature = (t_raqm_add_font_feature)GetProcAddress(p_raqm.raqm, "raqm_add_font_feature"); + p_raqm.set_freetype_face = (t_raqm_set_freetype_face)GetProcAddress(p_raqm.raqm, "raqm_set_freetype_face"); + p_raqm.layout = (t_raqm_layout)GetProcAddress(p_raqm.raqm, "raqm_layout"); + p_raqm.destroy = (t_raqm_destroy)GetProcAddress(p_raqm.raqm, "raqm_destroy"); + if(GetProcAddress(p_raqm.raqm, "raqm_index_to_position")) { + p_raqm.get_glyphs = (t_raqm_get_glyphs)GetProcAddress(p_raqm.raqm, "raqm_get_glyphs"); + p_raqm.version = 2; + } else { + p_raqm.version = 1; + p_raqm.get_glyphs_01 = (t_raqm_get_glyphs_01)GetProcAddress(p_raqm.raqm, "raqm_get_glyphs"); + } + if (!(p_raqm.create && + p_raqm.set_text && + p_raqm.set_text_utf8 && + p_raqm.set_par_direction && + p_raqm.add_font_feature && + p_raqm.set_freetype_face && + p_raqm.layout && + (p_raqm.get_glyphs || p_raqm.get_glyphs_01) && + p_raqm.destroy)) { + FreeLibrary(p_raqm.raqm); + p_raqm.raqm = NULL; + return 2; + } +#endif + + return 0; +} + static PyObject* getfont(PyObject* self_, PyObject* args, PyObject* kw) { @@ -205,7 +331,6 @@ font_getchar(PyObject* string, int index, FT_ULong* char_out) return 0; } -#ifdef HAVE_RAQM static size_t text_layout_raqm(PyObject* string, FontObject* self, const char* dir, PyObject *features ,GlyphInfo **glyph_info, int mask) @@ -213,10 +338,11 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, int i = 0; raqm_t *rq; size_t count = 0; - raqm_glyph_t *glyphs; + raqm_glyph_t *glyphs = NULL; + raqm_glyph_t_01 *glyphs_01 = NULL; raqm_direction_t direction; - rq = raqm_create(); + rq = (*p_raqm.create)(); if (rq == NULL) { PyErr_SetString(PyExc_ValueError, "raqm_create() failed."); goto failed; @@ -230,7 +356,7 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, and raqm fails with empty strings */ goto failed; } - if (!raqm_set_text(rq, (const uint32_t *)(text), size)) { + if (!(*p_raqm.set_text)(rq, (const uint32_t *)(text), size)) { PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed"); goto failed; } @@ -242,7 +368,7 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, if (! size) { goto failed; } - if (!raqm_set_text_utf8(rq, text, size)) { + if (!(*p_raqm.set_text_utf8)(rq, text, size)) { PyErr_SetString(PyExc_ValueError, "raqm_set_text_utf8() failed"); goto failed; } @@ -267,7 +393,7 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, } } - if (!raqm_set_par_direction(rq, direction)) { + if (!(*p_raqm.set_par_direction)(rq, direction)) { PyErr_SetString(PyExc_ValueError, "raqm_set_par_direction() failed"); goto failed; } @@ -308,30 +434,39 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, size = PyString_GET_SIZE(item); } #endif - if (!raqm_add_font_feature(rq, feature, size)) { + if (!(*p_raqm.add_font_feature)(rq, feature, size)) { PyErr_SetString(PyExc_ValueError, "raqm_add_font_feature() failed"); goto failed; } } } - if (!raqm_set_freetype_face(rq, self->face)) { - PyErr_SetString(PyExc_RuntimeError, "raqm_set_freetype_face() failed."); - goto failed; - } - - if (!raqm_layout (rq)) { - PyErr_SetString(PyExc_RuntimeError, "raqm_layout() failed."); - goto failed; - } - - glyphs = raqm_get_glyphs(rq, &count); - if (glyphs == NULL) { - PyErr_SetString(PyExc_ValueError, "raqm_get_glyphs() failed."); - count = 0; + if (!(*p_raqm.set_freetype_face)(rq, self->face)) { + PyErr_SetString(PyExc_RuntimeError, "raqm_set_freetype_face() failed."); goto failed; } + if (!(*p_raqm.layout)(rq)) { + PyErr_SetString(PyExc_RuntimeError, "raqm_layout() failed."); + goto failed; + } + + if (p_raqm.version == 1){ + glyphs_01 = (*p_raqm.get_glyphs_01)(rq, &count); + if (glyphs_01 == NULL) { + PyErr_SetString(PyExc_ValueError, "raqm_get_glyphs() failed."); + count = 0; + goto failed; + } + } else { /* version == 2 */ + glyphs = (*p_raqm.get_glyphs)(rq, &count); + if (glyphs == NULL) { + PyErr_SetString(PyExc_ValueError, "raqm_get_glyphs() failed."); + count = 0; + goto failed; + } + } + (*glyph_info) = PyMem_New(GlyphInfo, count); if ((*glyph_info) == NULL) { PyErr_SetString(PyExc_MemoryError, "PyMem_New() failed"); @@ -339,19 +474,28 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, goto failed; } - for (i = 0; i < count; i++) { - (*glyph_info)[i].index = glyphs[i].index; - (*glyph_info)[i].x_offset = glyphs[i].x_offset; - (*glyph_info)[i].x_advance = glyphs[i].x_advance; - (*glyph_info)[i].y_offset = glyphs[i].y_offset; - (*glyph_info)[i].cluster = glyphs[i].cluster; + if (p_raqm.version == 1){ + for (i = 0; i < count; i++) { + (*glyph_info)[i].index = glyphs_01[i].index; + (*glyph_info)[i].x_offset = glyphs_01[i].x_offset; + (*glyph_info)[i].x_advance = glyphs_01[i].x_advance; + (*glyph_info)[i].y_offset = glyphs_01[i].y_offset; + (*glyph_info)[i].cluster = glyphs_01[i].cluster; + } + } else { + for (i = 0; i < count; i++) { + (*glyph_info)[i].index = glyphs[i].index; + (*glyph_info)[i].x_offset = glyphs[i].x_offset; + (*glyph_info)[i].x_advance = glyphs[i].x_advance; + (*glyph_info)[i].y_offset = glyphs[i].y_offset; + (*glyph_info)[i].cluster = glyphs[i].cluster; + } } failed: - raqm_destroy (rq); + (*p_raqm.destroy)(rq); return count; } -#endif static size_t text_layout_fallback(PyObject* string, FontObject* self, const char* dir, @@ -424,15 +568,12 @@ text_layout(PyObject* string, FontObject* self, const char* dir, PyObject *features, GlyphInfo **glyph_info, int mask) { size_t count; -#ifdef HAVE_RAQM - if (self->layout_engine == LAYOUT_RAQM) { + + if (p_raqm.raqm && self->layout_engine == LAYOUT_RAQM) { count = text_layout_raqm(string, self, dir, features, glyph_info, mask); } else { count = text_layout_fallback(string, self, dir, features, glyph_info, mask); } -#else - count = text_layout_fallback(string, self, dir, features, glyph_info, mask); -#endif return count; } @@ -853,11 +994,8 @@ setup_module(PyObject* m) { PyDict_SetItemString(d, "freetype2_version", v); -#ifdef HAVE_RAQM - v = PyBool_FromLong(1); -#else - v = PyBool_FromLong(0); -#endif + setraqm(); + v = PyBool_FromLong(!!p_raqm.raqm); PyDict_SetItemString(d, "HAVE_RAQM", v); return 0; diff --git a/docs/installation.rst b/docs/installation.rst index 5dae41045..2d0ad8764 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -39,7 +39,7 @@ Windows Installation We provide Pillow binaries for Windows compiled for the matrix of supported Pythons in both 32 and 64-bit versions in wheel, egg, and executable installers. These binaries have all of the optional -libraries included:: +libraries included except for raqm and libimagequant:: > pip install Pillow @@ -47,9 +47,10 @@ libraries included:: macOS Installation ^^^^^^^^^^^^^^^^^^ -We provide binaries for macOS for each of the supported Python versions -in the wheel format. These include support for all optional libraries -except OpenJPEG:: +We provide binaries for macOS for each of the supported Python +versions in the wheel format. These include support for all optional +libraries except libimagequant. Raqm support requires libraqm, +fribidi, and harfbuzz to be installed separately:: $ pip install Pillow @@ -58,7 +59,8 @@ Linux Installation We provide binaries for Linux for each of the supported Python versions in the manylinux wheel format. These include support for all -optional libraries except Raqm:: +optional libraries except libimagequant. Raqm support requires +libraqm, fribidi, and harfbuzz to be installed separately:: $ pip install Pillow @@ -170,6 +172,8 @@ Many of Pillow's features require external libraries: if not available as package in your system. * setting text direction or font features is not supported without libraqm. + * libraqm is dynamically loaded in Pillow 4.4.0 and above, so support + is available if all the libraries are installed. * Windows support: Raqm support is currently unsupported on Windows. Once you have installed the prerequisites, run:: @@ -204,7 +208,7 @@ Build Options ``--disable-tiff``, ``--disable-freetype``, ``--disable-tcl``, ``--disable-tk``, ``--disable-lcms``, ``--disable-webp``, ``--disable-webpmux``, ``--disable-jpeg2000``, - ``--disable-imagequant``, ``--disable-raqm``. + ``--disable-imagequant``. Disable building the corresponding feature even if the development libraries are present on the building machine. @@ -212,7 +216,7 @@ Build Options ``--enable-tiff``, ``--enable-freetype``, ``--enable-tcl``, ``--enable-tk``, ``--enable-lcms``, ``--enable-webp``, ``--enable-webpmux``, ``--enable-jpeg2000``, - ``--enable-imagequant``, ``--enable-raqm``. + ``--enable-imagequant``. Require that the corresponding feature is built. The build will raise an exception if the libraries are not found. Webpmux (WebP metadata) relies on WebP support. Tcl and Tk also must be used together. diff --git a/libImaging/raqm.h b/libImaging/raqm.h new file mode 100644 index 000000000..eae1f43b7 --- /dev/null +++ b/libImaging/raqm.h @@ -0,0 +1,180 @@ +/* + * Copyright © 2015 Information Technology Authority (ITA) + * Copyright © 2016 Khaled Hosny + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +#ifndef _RAQM_H_ +#define _RAQM_H_ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifndef bool +typedef int bool; +#endif +#ifndef uint32_t +typedef UINT32 uint32_t; +#endif +#include +#include FT_FREETYPE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * raqm_t: + * + * This is the main object holding all state of the currently processed text as + * well as its output. + * + * Since: 0.1 + */ +typedef struct _raqm raqm_t; + +/** + * raqm_direction_t: + * @RAQM_DIRECTION_DEFAULT: Detect paragraph direction automatically. + * @RAQM_DIRECTION_RTL: Paragraph is mainly right-to-left text. + * @RAQM_DIRECTION_LTR: Paragraph is mainly left-to-right text. + * @RAQM_DIRECTION_TTB: Paragraph is mainly vertical top-to-bottom text. + * + * Base paragraph direction, see raqm_set_par_direction(). + * + * Since: 0.1 + */ +typedef enum +{ + RAQM_DIRECTION_DEFAULT, + RAQM_DIRECTION_RTL, + RAQM_DIRECTION_LTR, + RAQM_DIRECTION_TTB +} raqm_direction_t; + +/** + * raqm_glyph_t: + * @index: the index of the glyph in the font file. + * @x_advance: the glyph advance width in horizontal text. + * @y_advance: the glyph advance width in vertical text. + * @x_offset: the horizontal movement of the glyph from the current point. + * @y_offset: the vertical movement of the glyph from the current point. + * @cluster: the index of original character in input text. + * @ftface: the @FT_Face of the glyph. + * + * The structure that holds information about output glyphs, returned from + * raqm_get_glyphs(). + */ +typedef struct raqm_glyph_t { + unsigned int index; + int x_advance; + int y_advance; + int x_offset; + int y_offset; + uint32_t cluster; + FT_Face ftface; +} raqm_glyph_t; + +/** + * version 0.1 of the raqm_glyph_t structure + */ +typedef struct raqm_glyph_t_01 { + unsigned int index; + int x_advance; + int y_advance; + int x_offset; + int y_offset; + uint32_t cluster; +} raqm_glyph_t_01; + + +raqm_t * +raqm_create (void); + +raqm_t * +raqm_reference (raqm_t *rq); + +void +raqm_destroy (raqm_t *rq); + +bool +raqm_set_text (raqm_t *rq, + const uint32_t *text, + size_t len); + +bool +raqm_set_text_utf8 (raqm_t *rq, + const char *text, + size_t len); + +bool +raqm_set_par_direction (raqm_t *rq, + raqm_direction_t dir); + +bool +raqm_set_language (raqm_t *rq, + const char *lang, + size_t start, + size_t len); + +bool +raqm_add_font_feature (raqm_t *rq, + const char *feature, + int len); + +bool +raqm_set_freetype_face (raqm_t *rq, + FT_Face face); + +bool +raqm_set_freetype_face_range (raqm_t *rq, + FT_Face face, + size_t start, + size_t len); + +bool +raqm_set_freetype_load_flags (raqm_t *rq, + int flags); + +bool +raqm_layout (raqm_t *rq); + +raqm_glyph_t * +raqm_get_glyphs (raqm_t *rq, + size_t *length); + +bool +raqm_index_to_position (raqm_t *rq, + size_t *index, + int *x, + int *y); + +bool +raqm_position_to_index (raqm_t *rq, + int x, + int y, + size_t *index); + +#ifdef __cplusplus +} +#endif +#endif /* _RAQM_H_ */ diff --git a/setup.py b/setup.py index a67c5d82a..9319b7372 100755 --- a/setup.py +++ b/setup.py @@ -143,7 +143,6 @@ IMAGEQUANT_ROOT = None TIFF_ROOT = None FREETYPE_ROOT = None LCMS_ROOT = None -RAQM_ROOT = None def _pkg_config(name): @@ -163,7 +162,7 @@ def _pkg_config(name): class pil_build_ext(build_ext): class feature: - features = ['zlib', 'jpeg', 'tiff', 'freetype', 'raqm', 'lcms', 'webp', + features = ['zlib', 'jpeg', 'tiff', 'freetype', 'lcms', 'webp', 'webpmux', 'jpeg2000', 'imagequant'] required = {'jpeg', 'zlib'} @@ -545,14 +544,6 @@ class pil_build_ext(build_ext): if subdir: _add_directory(self.compiler.include_dirs, subdir, 0) - if feature.want('raqm'): - _dbg('Looking for raqm') - if _find_include_file(self, "raqm.h"): - if _find_library_file(self, "raqm") and \ - _find_library_file(self, "harfbuzz") and \ - _find_library_file(self, "fribidi"): - feature.raqm = ["raqm", "harfbuzz", "fribidi"] - if feature.want('lcms'): _dbg('Looking for lcms') if _find_include_file(self, "lcms2.h"): @@ -638,9 +629,6 @@ class pil_build_ext(build_ext): if feature.freetype: libs = ["freetype"] defs = [] - if feature.raqm: - libs.extend(feature.raqm) - defs.append(('HAVE_RAQM', None)) exts.append(Extension( "PIL._imagingft", ["_imagingft.c"], libraries=libs, define_macros=defs)) @@ -705,7 +693,6 @@ class pil_build_ext(build_ext): (feature.imagequant, "LIBIMAGEQUANT"), (feature.tiff, "LIBTIFF"), (feature.freetype, "FREETYPE2"), - (feature.raqm, "RAQM"), (feature.lcms, "LITTLECMS2"), (feature.webp, "WEBP"), (feature.webpmux, "WEBPMUX"),