Merge pull request #2195 from libvips/text-colour-rendering
Text colour rendering
This commit is contained in:
commit
da6fe210b4
3
.github/workflows/ci.yml
vendored
3
.github/workflows/ci.yml
vendored
@ -60,7 +60,8 @@ jobs:
|
|||||||
|
|
||||||
- name: Install macOS dependencies
|
- name: Install macOS dependencies
|
||||||
if: contains(matrix.os, 'macos')
|
if: contains(matrix.os, 'macos')
|
||||||
run:
|
run: |
|
||||||
|
brew update
|
||||||
brew install
|
brew install
|
||||||
autoconf automake libtool
|
autoconf automake libtool
|
||||||
gtk-doc gobject-introspection
|
gtk-doc gobject-introspection
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
- add vips_image_[set|get]_array_double()
|
- add vips_image_[set|get]_array_double()
|
||||||
- add GIF load with libnsgif
|
- add GIF load with libnsgif
|
||||||
- add JPEG2000 load and save
|
- add JPEG2000 load and save
|
||||||
|
- add "rgba" flag to vips_text() to enable full colour text rendering
|
||||||
|
|
||||||
22/12/20 start 8.10.6
|
22/12/20 start 8.10.6
|
||||||
- don't seek on bad file descriptors [kleisauke]
|
- don't seek on bad file descriptors [kleisauke]
|
||||||
|
@ -243,10 +243,10 @@ If you are going to be using libvips with untrusted images, perhaps in a
|
|||||||
web server, for example, you should consider the security implications of
|
web server, for example, you should consider the security implications of
|
||||||
enabling a package with such a large attack surface.
|
enabling a package with such a large attack surface.
|
||||||
|
|
||||||
### pangoft2
|
### pangocairo
|
||||||
|
|
||||||
If available, libvips adds support for text rendering. You need the
|
If available, libvips adds support for text rendering. You need the
|
||||||
package pangoft2 in `pkg-config --list-all`.
|
package pangocairo in `pkg-config --list-all`.
|
||||||
|
|
||||||
### orc-0.4
|
### orc-0.4
|
||||||
|
|
||||||
|
30
configure.ac
30
configure.ac
@ -1083,25 +1083,25 @@ fi
|
|||||||
VIPS_CFLAGS="$VIPS_CFLAGS $LIBWEBP_CFLAGS"
|
VIPS_CFLAGS="$VIPS_CFLAGS $LIBWEBP_CFLAGS"
|
||||||
VIPS_LIBS="$VIPS_LIBS $LIBWEBP_LIBS"
|
VIPS_LIBS="$VIPS_LIBS $LIBWEBP_LIBS"
|
||||||
|
|
||||||
# pangoft2
|
# pangocairo for text rendering
|
||||||
AC_ARG_WITH([pangoft2],
|
AC_ARG_WITH([pangocairo],
|
||||||
AS_HELP_STRING([--without-pangoft2],
|
AS_HELP_STRING([--without-pangocairo],
|
||||||
[build without pangoft2 (default: test)]))
|
[build without pangocairo (default: test)]))
|
||||||
|
|
||||||
if test x"$with_pangoft2" != x"no"; then
|
if test x"$with_pangocairo" != x"no"; then
|
||||||
PKG_CHECK_MODULES(PANGOFT2, pangoft2,
|
PKG_CHECK_MODULES(PANGOCAIRO, pangocairo,
|
||||||
[AC_DEFINE(HAVE_PANGOFT2,1,[define if you have pangoft2 installed.])
|
[AC_DEFINE(HAVE_PANGOCAIRO,1,[define if you have pangocairo installed.])
|
||||||
with_pangoft2=yes
|
with_pangocairo=yes
|
||||||
PACKAGES_USED="$PACKAGES_USED pangoft2"
|
PACKAGES_USED="$PACKAGES_USED pangocairo"
|
||||||
],
|
],
|
||||||
[AC_MSG_WARN([pangoft2 not found; disabling pangoft2 support])
|
[AC_MSG_WARN([pangocairo not found; disabling pangocairo support])
|
||||||
with_pangoft2=no
|
with_pangocairo=no
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
VIPS_CFLAGS="$VIPS_CFLAGS $PANGOFT2_CFLAGS"
|
VIPS_CFLAGS="$VIPS_CFLAGS $PANGOCAIRO_CFLAGS"
|
||||||
VIPS_LIBS="$VIPS_LIBS $PANGOFT2_LIBS"
|
VIPS_LIBS="$VIPS_LIBS $PANGOCAIRO_LIBS"
|
||||||
|
|
||||||
# look for TIFF with pkg-config ... fall back to our tester
|
# look for TIFF with pkg-config ... fall back to our tester
|
||||||
# pkgconfig support for libtiff starts with libtiff-4
|
# pkgconfig support for libtiff starts with libtiff-4
|
||||||
@ -1325,7 +1325,7 @@ use fftw3 for FFT: $with_fftw, \
|
|||||||
accelerate loops with orc: $with_orc, \
|
accelerate loops with orc: $with_orc, \
|
||||||
ICC profile support with lcms: $with_lcms, \
|
ICC profile support with lcms: $with_lcms, \
|
||||||
zlib: $with_zlib, \
|
zlib: $with_zlib, \
|
||||||
text rendering with pangoft2: $with_pangoft2, \
|
text rendering with pangocairo: $with_pangocairo, \
|
||||||
EXIF metadata support with libexif: $with_libexif, \
|
EXIF metadata support with libexif: $with_libexif, \
|
||||||
JPEG load/save with libjpeg: $with_jpeg, \
|
JPEG load/save with libjpeg: $with_jpeg, \
|
||||||
PNG load with libspng: $with_libspng, \
|
PNG load with libspng: $with_libspng, \
|
||||||
@ -1427,7 +1427,7 @@ accelerate loops with orc: $with_orc
|
|||||||
(requires orc-0.4.11 or later)
|
(requires orc-0.4.11 or later)
|
||||||
ICC profile support with lcms: $with_lcms
|
ICC profile support with lcms: $with_lcms
|
||||||
zlib: $with_zlib
|
zlib: $with_zlib
|
||||||
text rendering with pangoft2: $with_pangoft2
|
text rendering with pangocairo: $with_pangocairo
|
||||||
EXIF metadata support with libexif: $with_libexif
|
EXIF metadata support with libexif: $with_libexif
|
||||||
|
|
||||||
## File format support
|
## File format support
|
||||||
|
@ -116,9 +116,9 @@ vips_create_operation_init( void )
|
|||||||
extern GType vips_gaussmat_get_type( void );
|
extern GType vips_gaussmat_get_type( void );
|
||||||
extern GType vips_logmat_get_type( void );
|
extern GType vips_logmat_get_type( void );
|
||||||
extern GType vips_gaussnoise_get_type( void );
|
extern GType vips_gaussnoise_get_type( void );
|
||||||
#ifdef HAVE_PANGOFT2
|
#ifdef HAVE_PANGOCAIRO
|
||||||
extern GType vips_text_get_type( void );
|
extern GType vips_text_get_type( void );
|
||||||
#endif /*HAVE_PANGOFT2*/
|
#endif /*HAVE_PANGOCAIRO*/
|
||||||
extern GType vips_xyz_get_type( void );
|
extern GType vips_xyz_get_type( void );
|
||||||
extern GType vips_eye_get_type( void );
|
extern GType vips_eye_get_type( void );
|
||||||
extern GType vips_grey_get_type( void );
|
extern GType vips_grey_get_type( void );
|
||||||
@ -146,9 +146,9 @@ vips_create_operation_init( void )
|
|||||||
vips_gaussmat_get_type();
|
vips_gaussmat_get_type();
|
||||||
vips_logmat_get_type();
|
vips_logmat_get_type();
|
||||||
vips_gaussnoise_get_type();
|
vips_gaussnoise_get_type();
|
||||||
#ifdef HAVE_PANGOFT2
|
#ifdef HAVE_PANGOCAIRO
|
||||||
vips_text_get_type();
|
vips_text_get_type();
|
||||||
#endif /*HAVE_PANGOFT2*/
|
#endif /*HAVE_PANGOCAIRO*/
|
||||||
vips_xyz_get_type();
|
vips_xyz_get_type();
|
||||||
vips_eye_get_type();
|
vips_eye_get_type();
|
||||||
vips_grey_get_type();
|
vips_grey_get_type();
|
||||||
|
@ -32,6 +32,9 @@
|
|||||||
* - fitting could occasionally terminate early [levmorozov]
|
* - fitting could occasionally terminate early [levmorozov]
|
||||||
* 16/5/20 [keiviv]
|
* 16/5/20 [keiviv]
|
||||||
* - don't add fontfiles repeatedly
|
* - don't add fontfiles repeatedly
|
||||||
|
* 12/4/21
|
||||||
|
* - switch to cairo for text rendering
|
||||||
|
* - add rgba flag
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -74,11 +77,14 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <vips/vips.h>
|
#include <vips/vips.h>
|
||||||
|
#include <vips/internal.h>
|
||||||
|
|
||||||
#ifdef HAVE_PANGOFT2
|
#ifdef HAVE_PANGOCAIRO
|
||||||
|
|
||||||
|
#include <cairo.h>
|
||||||
#include <pango/pango.h>
|
#include <pango/pango.h>
|
||||||
#include <pango/pangoft2.h>
|
#include <pango/pangocairo.h>
|
||||||
|
#include <fontconfig/fontconfig.h>
|
||||||
|
|
||||||
#include "pcreate.h"
|
#include "pcreate.h"
|
||||||
|
|
||||||
@ -94,8 +100,8 @@ typedef struct _VipsText {
|
|||||||
gboolean justify;
|
gboolean justify;
|
||||||
int dpi;
|
int dpi;
|
||||||
char *fontfile;
|
char *fontfile;
|
||||||
|
gboolean rgba;
|
||||||
|
|
||||||
FT_Bitmap bitmap;
|
|
||||||
PangoContext *context;
|
PangoContext *context;
|
||||||
PangoLayout *layout;
|
PangoLayout *layout;
|
||||||
|
|
||||||
@ -129,7 +135,6 @@ vips_text_dispose( GObject *gobject )
|
|||||||
|
|
||||||
VIPS_UNREF( text->layout );
|
VIPS_UNREF( text->layout );
|
||||||
VIPS_UNREF( text->context );
|
VIPS_UNREF( text->context );
|
||||||
VIPS_FREE( text->bitmap.buffer );
|
|
||||||
|
|
||||||
G_OBJECT_CLASS( vips_text_parent_class )->dispose( gobject );
|
G_OBJECT_CLASS( vips_text_parent_class )->dispose( gobject );
|
||||||
}
|
}
|
||||||
@ -186,8 +191,8 @@ vips_text_get_extents( VipsText *text, VipsRect *extents )
|
|||||||
PangoRectangle ink_rect;
|
PangoRectangle ink_rect;
|
||||||
PangoRectangle logical_rect;
|
PangoRectangle logical_rect;
|
||||||
|
|
||||||
pango_ft2_font_map_set_resolution(
|
pango_cairo_font_map_set_resolution(
|
||||||
PANGO_FT2_FONT_MAP( vips_text_fontmap ), text->dpi, text->dpi );
|
PANGO_CAIRO_FONT_MAP( vips_text_fontmap ), text->dpi );
|
||||||
|
|
||||||
VIPS_UNREF( text->layout );
|
VIPS_UNREF( text->layout );
|
||||||
if( !(text->layout = text_layout_new( text->context,
|
if( !(text->layout = text_layout_new( text->context,
|
||||||
@ -340,9 +345,12 @@ vips_text_build( VipsObject *object )
|
|||||||
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
|
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
|
||||||
VipsCreate *create = VIPS_CREATE( object );
|
VipsCreate *create = VIPS_CREATE( object );
|
||||||
VipsText *text = (VipsText *) object;
|
VipsText *text = (VipsText *) object;
|
||||||
|
VipsImage **t = (VipsImage **) vips_object_local_array( object, 3 );
|
||||||
|
|
||||||
VipsRect extents;
|
VipsRect extents;
|
||||||
int y;
|
VipsImage *image;
|
||||||
|
cairo_surface_t *surface;
|
||||||
|
cairo_t *cr;
|
||||||
|
|
||||||
if( VIPS_OBJECT_CLASS( vips_text_parent_class )->build( object ) )
|
if( VIPS_OBJECT_CLASS( vips_text_parent_class )->build( object ) )
|
||||||
return( -1 );
|
return( -1 );
|
||||||
@ -356,7 +364,7 @@ vips_text_build( VipsObject *object )
|
|||||||
g_mutex_lock( vips_text_lock );
|
g_mutex_lock( vips_text_lock );
|
||||||
|
|
||||||
if( !vips_text_fontmap )
|
if( !vips_text_fontmap )
|
||||||
vips_text_fontmap = pango_ft2_font_map_new();
|
vips_text_fontmap = pango_cairo_font_map_new();
|
||||||
if( !vips_text_fontfiles )
|
if( !vips_text_fontfiles )
|
||||||
vips_text_fontfiles =
|
vips_text_fontfiles =
|
||||||
g_hash_table_new( g_str_hash, g_str_equal );
|
g_hash_table_new( g_str_hash, g_str_equal );
|
||||||
@ -399,39 +407,64 @@ vips_text_build( VipsObject *object )
|
|||||||
return( -1 );
|
return( -1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
text->bitmap.width = extents.width;
|
/* Set DPI as pixels/mm.
|
||||||
text->bitmap.pitch = (text->bitmap.width + 3) & ~3;
|
*/
|
||||||
text->bitmap.rows = extents.height;
|
image = t[0] = vips_image_new_memory();
|
||||||
if( !(text->bitmap.buffer =
|
vips_image_init_fields( image,
|
||||||
VIPS_ARRAY( NULL,
|
extents.width, extents.height, 4,
|
||||||
text->bitmap.pitch * text->bitmap.rows, VipsPel )) ) {
|
VIPS_FORMAT_UCHAR, VIPS_CODING_NONE,
|
||||||
|
VIPS_INTERPRETATION_sRGB,
|
||||||
|
text->dpi / 25.4, text->dpi / 25.4 );
|
||||||
|
|
||||||
|
vips_image_pipelinev( image, VIPS_DEMAND_STYLE_ANY, NULL );
|
||||||
|
image->Xoffset = extents.left;
|
||||||
|
image->Yoffset = extents.top;
|
||||||
|
|
||||||
|
if( vips_image_write_prepare( image ) ) {
|
||||||
g_mutex_unlock( vips_text_lock );
|
g_mutex_unlock( vips_text_lock );
|
||||||
return( -1 );
|
return( -1 );
|
||||||
}
|
}
|
||||||
text->bitmap.num_grays = 256;
|
|
||||||
text->bitmap.pixel_mode = ft_pixel_mode_grays;
|
|
||||||
memset( text->bitmap.buffer, 0x00,
|
|
||||||
text->bitmap.pitch * text->bitmap.rows );
|
|
||||||
|
|
||||||
pango_ft2_render_layout( &text->bitmap, text->layout,
|
surface = cairo_image_surface_create_for_data(
|
||||||
-extents.left, -extents.top );
|
VIPS_IMAGE_ADDR( image, 0, 0 ),
|
||||||
|
CAIRO_FORMAT_ARGB32,
|
||||||
|
image->Xsize, image->Ysize,
|
||||||
|
VIPS_IMAGE_SIZEOF_LINE( image ) );
|
||||||
|
cr = cairo_create( surface );
|
||||||
|
cairo_surface_destroy( surface );
|
||||||
|
|
||||||
|
cairo_translate( cr, -extents.left, -extents.top );
|
||||||
|
|
||||||
|
pango_cairo_show_layout( cr, text->layout );
|
||||||
|
|
||||||
|
cairo_destroy( cr );
|
||||||
|
|
||||||
g_mutex_unlock( vips_text_lock );
|
g_mutex_unlock( vips_text_lock );
|
||||||
|
|
||||||
vips_image_init_fields( create->out,
|
if( text->rgba ) {
|
||||||
text->bitmap.width, text->bitmap.rows, 1,
|
int y;
|
||||||
VIPS_FORMAT_UCHAR, VIPS_CODING_NONE,
|
|
||||||
VIPS_INTERPRETATION_MULTIBAND,
|
|
||||||
1.0, 1.0 );
|
|
||||||
vips_image_pipelinev( create->out,
|
|
||||||
VIPS_DEMAND_STYLE_ANY, NULL );
|
|
||||||
create->out->Xoffset = extents.left;
|
|
||||||
create->out->Yoffset = extents.top;
|
|
||||||
|
|
||||||
for( y = 0; y < text->bitmap.rows; y++ )
|
/* Cairo makes pre-multipled BRGA -- we must byteswap and
|
||||||
if( vips_image_write_line( create->out, y,
|
* unpremultiply.
|
||||||
(VipsPel *) text->bitmap.buffer +
|
*/
|
||||||
y * text->bitmap.pitch ) )
|
for( y = 0; y < image->Ysize; y++ )
|
||||||
|
vips__premultiplied_bgra2rgba(
|
||||||
|
(guint32 *)
|
||||||
|
VIPS_IMAGE_ADDR( image, 0, y ),
|
||||||
|
image->Xsize );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* We just want the alpha channel.
|
||||||
|
*/
|
||||||
|
if( vips_extract_band( image, &t[1], 3, NULL ) ||
|
||||||
|
vips_copy( t[1], &t[2],
|
||||||
|
"interpretation", VIPS_INTERPRETATION_MULTIBAND,
|
||||||
|
NULL ) )
|
||||||
|
return( -1 );
|
||||||
|
image = t[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
if( vips_image_write( image, create->out ) )
|
||||||
return( -1 );
|
return( -1 );
|
||||||
|
|
||||||
return( 0 );
|
return( 0 );
|
||||||
@ -534,6 +567,13 @@ vips_text_class_init( VipsTextClass *class )
|
|||||||
G_STRUCT_OFFSET( VipsText, fontfile ),
|
G_STRUCT_OFFSET( VipsText, fontfile ),
|
||||||
NULL );
|
NULL );
|
||||||
|
|
||||||
|
VIPS_ARG_BOOL( class, "rgba", 9,
|
||||||
|
_( "RGBA" ),
|
||||||
|
_( "Enable RGBA output" ),
|
||||||
|
VIPS_ARGUMENT_OPTIONAL_INPUT,
|
||||||
|
G_STRUCT_OFFSET( VipsText, rgba ),
|
||||||
|
FALSE );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -541,11 +581,10 @@ vips_text_init( VipsText *text )
|
|||||||
{
|
{
|
||||||
text->align = VIPS_ALIGN_LOW;
|
text->align = VIPS_ALIGN_LOW;
|
||||||
text->dpi = 72;
|
text->dpi = 72;
|
||||||
text->bitmap.buffer = NULL;
|
|
||||||
VIPS_SETSTR( text->font, "sans 12" );
|
VIPS_SETSTR( text->font, "sans 12" );
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /*HAVE_PANGOFT2*/
|
#endif /*HAVE_PANGOCAIRO*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* vips_text:
|
* vips_text:
|
||||||
@ -563,17 +602,22 @@ vips_text_init( VipsText *text )
|
|||||||
* * @justify: %gboolean, justify lines
|
* * @justify: %gboolean, justify lines
|
||||||
* * @dpi: %gint, render at this resolution
|
* * @dpi: %gint, render at this resolution
|
||||||
* * @autofit_dpi: %gint, read out auto-fitted DPI
|
* * @autofit_dpi: %gint, read out auto-fitted DPI
|
||||||
|
* * @rgba: %gboolean, enable RGBA output
|
||||||
* * @spacing: %gint, space lines by this in points
|
* * @spacing: %gint, space lines by this in points
|
||||||
*
|
*
|
||||||
* Draw the string @text to an image. @out is a one-band 8-bit
|
* Draw the string @text to an image. @out is normally a one-band 8-bit
|
||||||
* unsigned char image, with 0 for no text and 255 for text. Values between
|
* unsigned char image, with 0 for no text and 255 for text. Values between
|
||||||
* are used for anti-aliasing.
|
* are used for anti-aliasing.
|
||||||
*
|
*
|
||||||
|
* Set @rgba to enable RGBA output. This is useful for colour emoji rendering,
|
||||||
|
* or support for pango markup features like `<span
|
||||||
|
* foreground="red">Red!</span>`.
|
||||||
|
*
|
||||||
* @text is the text to render as a UTF-8 string. It can contain Pango markup,
|
* @text is the text to render as a UTF-8 string. It can contain Pango markup,
|
||||||
* for example "<i>The</i>Guardian".
|
* for example `<i>The</i>Guardian`.
|
||||||
*
|
*
|
||||||
* @font is the font to render with, as a fontconfig name. Examples might be
|
* @font is the font to render with, as a fontconfig name. Examples might be
|
||||||
* "sans 12" or perhaps "bitstream charter bold 10".
|
* `sans 12` or perhaps `bitstream charter bold 10`.
|
||||||
*
|
*
|
||||||
* You can specify a font to load with @fontfile. You'll need to also set the
|
* You can specify a font to load with @fontfile. You'll need to also set the
|
||||||
* name of the font with @font.
|
* name of the font with @font.
|
||||||
|
Loading…
Reference in New Issue
Block a user