From d5c4ddf4e43797503e5a5629cdbab74e834bc549 Mon Sep 17 00:00:00 2001 From: JucaBlues Date: Sun, 25 May 2008 15:35:28 +0000 Subject: [PATCH] * add rule in configure.ac so that only who has cairo > 1.6.4 (currently cairo git master branch) will compile SVGFonts (experimental) support. * First code for SVGFonts rendering. Renders in a separate window because we still dont have pango integration. --- configure.ac | 9 ++ share/examples/svgfont.svg | 29 +++--- src/display/Makefile_insert | 2 + src/display/nr-arena-glyphs.cpp | 2 + src/display/nr-svgfonts.cpp | 156 ++++++++++++++++++++++++++++++++ src/display/nr-svgfonts.h | 25 +++++ src/sp-font-face.cpp | 4 +- src/sp-font-face.h | 3 + src/sp-font.cpp | 8 +- src/sp-font.h | 3 + src/sp-glyph-kerning.cpp | 4 +- src/sp-glyph-kerning.h | 3 + src/sp-glyph.cpp | 4 +- src/sp-glyph.h | 3 + src/sp-missing-glyph.cpp | 4 +- src/sp-missing-glyph.h | 3 + src/sp-object-repr.cpp | 17 +++- 17 files changed, 252 insertions(+), 27 deletions(-) create mode 100644 src/display/nr-svgfonts.cpp create mode 100644 src/display/nr-svgfonts.h diff --git a/configure.ac b/configure.ac index 00cffd4a7..852970ba8 100644 --- a/configure.ac +++ b/configure.ac @@ -610,6 +610,15 @@ AM_CONDITIONAL(USE_IMAGE_MAGICK, test "x$magick_ok" = "xyes") AC_SUBST(IMAGEMAGICK_LIBS) AC_SUBST(IMAGEMAGICK_CFLAGS) +dnl *********************************************************************************************************** +dnl Check for a Cairo version that implements user-fonts feature, so that we conditionally add SVGFonts support +dnl *********************************************************************************************************** + +PKG_CHECK_MODULES(CAIRO_USER_FONTS, cairo > 1.6.4, cairouserfonts=yes, cairouserfonts=no) +if test "x$cairouserfonts" = "xyes"; then + AC_DEFINE(ENABLE_SVG_FONTS, 1, [SVG Fonts should be used]) +fi + dnl ****************************** dnl Unconditional dependencies dnl ****************************** diff --git a/share/examples/svgfont.svg b/share/examples/svgfont.svg index 4e9a06eab..10bac6e91 100644 --- a/share/examples/svgfont.svg +++ b/share/examples/svgfont.svg @@ -1,24 +1,19 @@ - - - - - - - - - - - + + + + + + + + + + - - A!@A + + This text is not used yet. Currently test strings are hardcoded... diff --git a/src/display/Makefile_insert b/src/display/Makefile_insert index 2e27cc188..5c7fd892e 100644 --- a/src/display/Makefile_insert +++ b/src/display/Makefile_insert @@ -55,6 +55,8 @@ display_libspdisplay_a_SOURCES = \ display/nr-plain-stuff-gdk.h \ display/nr-plain-stuff.cpp \ display/nr-plain-stuff.h \ + display/nr-svgfonts.cpp \ + display/nr-svgfonts.h \ display/rendermode.h \ display/snap-indicator.cpp \ display/snap-indicator.h \ diff --git a/src/display/nr-arena-glyphs.cpp b/src/display/nr-arena-glyphs.cpp index 6ee1be796..63af4787a 100644 --- a/src/display/nr-arena-glyphs.cpp +++ b/src/display/nr-arena-glyphs.cpp @@ -37,6 +37,8 @@ void nr_pixblock_render_shape_mask_or(NRPixBlock &m, Shape *theS); #endif +#include "nr-svgfonts.h" + static void nr_arena_glyphs_class_init(NRArenaGlyphsClass *klass); static void nr_arena_glyphs_init(NRArenaGlyphs *glyphs); static void nr_arena_glyphs_finalize(NRObject *object); diff --git a/src/display/nr-svgfonts.cpp b/src/display/nr-svgfonts.cpp new file mode 100644 index 000000000..472d232cf --- /dev/null +++ b/src/display/nr-svgfonts.cpp @@ -0,0 +1,156 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS +/* + * SVGFonts rendering implementation + * + * Authors: + * Felipe C. da S. Sanches + * + * Copyright (C) 2008 Felipe C. da S. Sanches + * + * Released under GNU GPL version 2 or later. + * Read the file 'COPYING' for more information. + */ + +#include +#include "../style.h" +#include "../sp-glyph.h" +#include "../sp-missing-glyph.h" +#include "../sp-font.h" +#include +#include +#include "svg/svg.h" +#include "inkscape-cairo.h" +#include + +static std::vector glyphs; +static SPMissingGlyph* missingglyph = NULL; +static std::vector svgfonts; + +cairo_font_face_t* nr_svgfonts_get_user_font_face(); +void nr_svgfonts_append_spfont(SPFont* font); +static cairo_status_t scaled_font_init (cairo_scaled_font_t *scaled_font, cairo_font_extents_t *metrics); +static cairo_status_t scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font, unsigned long unicode, unsigned long *glyph); +static cairo_status_t scaled_font_render_glyph (cairo_scaled_font_t *scaled_font, unsigned long glyph, cairo_t *cr, cairo_text_extents_t *metrics); + +void nr_svgfonts_append_spfont(SPFont* font){ + svgfonts.push_back(font); + nr_svgfonts_get_user_font_face(); +} + +static cairo_status_t +scaled_font_init (cairo_scaled_font_t *scaled_font, + cairo_font_extents_t *metrics) +{ +//TODO +// metrics->ascent = .75; +// metrics->descent = .25; + return CAIRO_STATUS_SUCCESS; +} + +static cairo_status_t +scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font, + unsigned long unicode, + unsigned long *glyph) +{ +//g_warning("scaled_font_unicode_to_glyph CALL. unicode=%c", (char)unicode); + + unsigned long i; + for (i=0; i < (unsigned long) glyphs.size(); i++){ + //TODO: compare unicode strings for ligadure glyphs (i.e. scaled_font_text_to_glyph) + if ( ((unsigned long) glyphs[i]->unicode[0]) == unicode){ + *glyph = i; + return CAIRO_STATUS_SUCCESS; + } + } + *glyph = i; + return CAIRO_STATUS_SUCCESS; +} + +static cairo_status_t +scaled_font_render_glyph (cairo_scaled_font_t *scaled_font, + unsigned long glyph, + cairo_t *cr, + cairo_text_extents_t *metrics) +{ +//g_warning("scaled_font_render_glyph CALL. glyph=%d", (int)glyph); + + if (glyph > glyphs.size()) return CAIRO_STATUS_SUCCESS; + + SPObject* node; + if (glyph == glyphs.size()){ + if (!missingglyph) return CAIRO_STATUS_SUCCESS; + node = (SPObject*) missingglyph; + g_warning("RENDER MISSING-GLYPH"); + } else { + node = (SPObject*) glyphs[glyph]; + g_warning("RENDER %c", glyphs[glyph]->unicode[0]); + } + + NArtBpath *bpath = NULL; + if (SP_IS_GLYPH(node)) bpath = sp_svg_read_path(((SPGlyph*)node)->d); + if (SP_IS_MISSING_GLYPH(node)) bpath = sp_svg_read_path(((SPMissingGlyph*)node)->d); + + cairo_new_path(cr); + + NR::scale s(1.0/((SPFont*) node->parent)->horiz_adv_x); + NR::Matrix t(s); + NRRect area(0,0,1,1); //I need help here! + feed_curve_to_cairo (cr, bpath, t, area.upgrade(), false, 0); + cairo_fill(cr); + + return CAIRO_STATUS_SUCCESS; +} + + +bool drawing_expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data) +{ + cairo_t *cr = gdk_cairo_create (widget->window); + + cairo_set_font_face (cr, nr_svgfonts_get_user_font_face()); + cairo_set_font_size (cr, 200); + cairo_move_to (cr, 200, 200); + cairo_show_text (cr, "A@!A"); + + cairo_move_to (cr, 200, 400); + cairo_show_text (cr, "A!@A"); + + cairo_destroy (cr); + + return TRUE; +} + + +cairo_font_face_t * nr_svgfonts_get_user_font_face(){ + static cairo_font_face_t *user_font_face = NULL; + + if (!user_font_face) { + for(SPObject* node = svgfonts[0]->children;node;node=node->next){ + if (SP_IS_GLYPH(node)){ + g_warning("glyphs.push_back((SPGlyph*)node); (node->unicode[0]='%c')", ((SPGlyph*)node)->unicode[0]); + glyphs.push_back((SPGlyph*)node); + } + if (SP_IS_MISSING_GLYPH(node)){ + g_warning("missingglyph=(SPMissingGlyph*)node;"); + missingglyph=(SPMissingGlyph*)node; + } + } + user_font_face = cairo_user_font_face_create (); + cairo_user_font_face_set_init_func (user_font_face, scaled_font_init); + cairo_user_font_face_set_render_glyph_func (user_font_face, scaled_font_render_glyph); + cairo_user_font_face_set_unicode_to_glyph_func (user_font_face, scaled_font_unicode_to_glyph); + + GtkWidget *window; + GtkWidget *drawing; + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size (GTK_WINDOW(window), 1200, 850); + drawing = gtk_drawing_area_new (); + gtk_container_add (GTK_CONTAINER (window), drawing); + gtk_widget_show_all (window); + g_signal_connect (drawing, "expose-event", G_CALLBACK (drawing_expose_cb), NULL); + } + return user_font_face; +} +#endif //#ifdef ENABLE_SVG_FONTS + diff --git a/src/display/nr-svgfonts.h b/src/display/nr-svgfonts.h new file mode 100644 index 000000000..f1671cb7d --- /dev/null +++ b/src/display/nr-svgfonts.h @@ -0,0 +1,25 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS +#ifndef __SVGFONTS_H__ +#define __SVGFONTS_H__ +/* + * SVGFonts rendering headear + * + * Authors: + * Felipe C. da S. Sanches + * + * Copyright (C) 2008 Felipe C. da S. Sanches + * + * Released under GNU GPL version 2 or later. + * Read the file 'COPYING' for more information. + */ + +#include "../sp-font.h" +#include "cairo.h" + +void nr_svgfonts_append_spfont(SPFont* font); +cairo_font_face_t* nr_svgfonts_get_user_font_face(); + +#endif //#ifndef __SVGFONTS_H__ +#endif //#ifdef ENABLE_SVG_FONTS + diff --git a/src/sp-font-face.cpp b/src/sp-font-face.cpp index 78fc51a6b..ad4d586fc 100644 --- a/src/sp-font-face.cpp +++ b/src/sp-font-face.cpp @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #define __SP_FONTFACE_C__ /* @@ -531,7 +533,7 @@ static Inkscape::XML::Node *sp_fontface_write(SPObject *object, Inkscape::XML::N return repr; } - +#endif //#ifdef ENABLE_SVG_FONTS /* Local Variables: mode:c++ diff --git a/src/sp-font-face.h b/src/sp-font-face.h index 754125831..d4ac8790a 100644 --- a/src/sp-font-face.h +++ b/src/sp-font-face.h @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #ifndef __SP_FONTFACE_H__ #define __SP_FONTFACE_H__ @@ -65,3 +67,4 @@ struct SPFontFaceClass { GType sp_fontface_get_type (void); #endif //#ifndef __SP_FONTFACE_H__ +#endif //#ifdef ENABLE_SVG_FONTS diff --git a/src/sp-font.cpp b/src/sp-font.cpp index a78f92cc0..5f8d3440a 100644 --- a/src/sp-font.cpp +++ b/src/sp-font.cpp @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #define __SP_FONT_C__ /* @@ -19,6 +21,8 @@ #include "document.h" #include "helper-fns.h" +#include "display/nr-svgfonts.h" + static void sp_font_class_init(SPFontClass *fc); static void sp_font_init(SPFont *font); @@ -96,6 +100,8 @@ static void sp_font_build(SPObject *object, SPDocument *document, Inkscape::XML: sp_object_read_attr(object, "vert-origin-x"); sp_object_read_attr(object, "vert-origin-y"); sp_object_read_attr(object, "vert-adv-y"); + + nr_svgfonts_append_spfont(SP_FONT(object)); } @@ -270,7 +276,7 @@ static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Node return repr; } - +#endif //#ifdef ENABLE_SVG_FONTS /* Local Variables: mode:c++ diff --git a/src/sp-font.h b/src/sp-font.h index 3ec866ccc..d8dc37189 100644 --- a/src/sp-font.h +++ b/src/sp-font.h @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #ifndef __SP_FONT_H__ #define __SP_FONT_H__ @@ -36,3 +38,4 @@ struct SPFontClass { GType sp_font_get_type (void); #endif //#ifndef __SP_FONT_H__ +#endif //#ifdef ENABLE_SVG_FONTS diff --git a/src/sp-glyph-kerning.cpp b/src/sp-glyph-kerning.cpp index 6d7006f57..b8a2bb1ed 100644 --- a/src/sp-glyph-kerning.cpp +++ b/src/sp-glyph-kerning.cpp @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #define __SP_ANCHOR_C__ /* @@ -229,7 +231,7 @@ static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::X return repr; } - +#endif //#ifdef ENABLE_SVG_FONTS /* Local Variables: mode:c++ diff --git a/src/sp-glyph-kerning.h b/src/sp-glyph-kerning.h index 2933a8abe..806ec0986 100644 --- a/src/sp-glyph-kerning.h +++ b/src/sp-glyph-kerning.h @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #ifndef __SP_GLYPH_KERNING_H__ #define __SP_GLYPH_KERNING_H__ @@ -45,3 +47,4 @@ GType sp_glyph_kerning_h_get_type (void); GType sp_glyph_kerning_v_get_type (void); #endif //#ifndef __SP_GLYPH_KERNING_H__ +#endif //#ifdef ENABLE_SVG_FONTS diff --git a/src/sp-glyph.cpp b/src/sp-glyph.cpp index 0c6d3f1e0..4860b42ed 100644 --- a/src/sp-glyph.cpp +++ b/src/sp-glyph.cpp @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #define __SP_GLYPH_C__ /* @@ -295,7 +297,7 @@ static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node return repr; } - +#endif //#ifdef ENABLE_SVG_FONTS /* Local Variables: mode:c++ diff --git a/src/sp-glyph.h b/src/sp-glyph.h index 40513a259..a1657dd43 100644 --- a/src/sp-glyph.h +++ b/src/sp-glyph.h @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #ifndef __SP_GLYPH_H__ #define __SP_GLYPH_H__ @@ -53,3 +55,4 @@ struct SPGlyphClass { GType sp_glyph_get_type (void); #endif //#ifndef __SP_GLYPH_H__ +#endif //#ifdef ENABLE_SVG_FONTS diff --git a/src/sp-missing-glyph.cpp b/src/sp-missing-glyph.cpp index d4cd16360..7cd75b3b8 100644 --- a/src/sp-missing-glyph.cpp +++ b/src/sp-missing-glyph.cpp @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #define __SP_MISSING_GLYPH_C__ /* @@ -178,7 +180,7 @@ static Inkscape::XML::Node *sp_missing_glyph_write(SPObject *object, Inkscape::X return repr; } - +#endif //#ifdef ENABLE_SVG_FONTS /* Local Variables: mode:c++ diff --git a/src/sp-missing-glyph.h b/src/sp-missing-glyph.h index 41b988ab1..4983dce69 100644 --- a/src/sp-missing-glyph.h +++ b/src/sp-missing-glyph.h @@ -1,3 +1,5 @@ +#include "config.h" +#ifdef ENABLE_SVG_FONTS #ifndef __SP_MISSING_GLYPH_H__ #define __SP_MISSING_GLYPH_H__ @@ -35,3 +37,4 @@ struct SPMissingGlyphClass { GType sp_missing_glyph_get_type (void); #endif //#ifndef __SP_MISSING_GLYPH_H__ +#endif //#ifdef ENABLE_SVG_FONTS diff --git a/src/sp-object-repr.cpp b/src/sp-object-repr.cpp index 6ed2f9ada..7eb91d281 100644 --- a/src/sp-object-repr.cpp +++ b/src/sp-object-repr.cpp @@ -43,11 +43,16 @@ #include "sp-flowdiv.h" #include "sp-flowregion.h" #include "sp-flowtext.h" -#include "sp-font.h" -#include "sp-font-face.h" -#include "sp-glyph.h" -#include "sp-missing-glyph.h" -#include "sp-glyph-kerning.h" +#include "config.h" + +#ifdef ENABLE_SVG_FONTS + #include "sp-font.h" + #include "sp-font-face.h" + #include "sp-glyph.h" + #include "sp-missing-glyph.h" + #include "sp-glyph-kerning.h" +#endif + #include "sp-style-elem.h" #include "sp-switch.h" #include "color-profile-fns.h" @@ -144,12 +149,14 @@ populate_dtables() { "svg:flowRegionExclude", SP_TYPE_FLOWREGIONEXCLUDE }, { "svg:flowRoot", SP_TYPE_FLOWTEXT }, { "svg:flowSpan", SP_TYPE_FLOWTSPAN }, +#ifdef ENABLE_SVG_FONTS { "svg:font", SP_TYPE_FONT }, { "svg:font-face", SP_TYPE_FONTFACE }, { "svg:glyph", SP_TYPE_GLYPH }, { "svg:missing-glyph", SP_TYPE_MISSING_GLYPH }, { "svg:hkern", SP_TYPE_HKERN }, { "svg:vkern", SP_TYPE_VKERN }, +#endif { "svg:g", SP_TYPE_GROUP }, { "svg:feBlend", SP_TYPE_FEBLEND }, { "svg:feColorMatrix", SP_TYPE_FECOLORMATRIX }, -- 2.30.2