diff options
author | Olivier Andrieu <oandrieu@gmail.com> | 2010-04-11 23:17:32 +0200 |
---|---|---|
committer | Olivier Andrieu <oandrieu@gmail.com> | 2010-04-11 23:17:32 +0200 |
commit | 4d202785ffebdc19d4564b2bc79e805f135dfc32 (patch) | |
tree | 4e756f4ebb35117a697bdd60f64ea6ba7425613e | |
parent | 9b98e9dda31ad502a10c60fba5036146a0082c9a (diff) |
pangocairo supportcairo-ocaml-1.2.0
contributed by Richard Jones
-rw-r--r-- | README | 4 | ||||
-rw-r--r-- | config.make.in | 3 | ||||
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | src/Makefile | 25 | ||||
-rw-r--r-- | src/ml_pango_cairo.c | 29 | ||||
-rw-r--r-- | src/pango_cairo.ml | 30 | ||||
-rw-r--r-- | src/pango_cairo.mli | 25 | ||||
-rw-r--r-- | test/Makefile | 8 | ||||
-rw-r--r-- | test/pangocairo.ml | 59 |
9 files changed, 185 insertions, 2 deletions
@@ -23,6 +23,7 @@ Dependencies ocaml >= 3.10 cairo >= 1.2.0 libsvg-cairo optional >= 0.1.5 + libpangocairo optional LablGTK optional, GTK+ >= 2.8 @@ -39,3 +40,6 @@ and is also available here: Author ====== Olivier Andrieu <oandrieu@gmail.com> + +Pango_cairo support started by: + Richard Jones <rich at annexia.org> or <rich at merjis.com> diff --git a/config.make.in b/config.make.in index 4d36b95..0b4af37 100644 --- a/config.make.in +++ b/config.make.in @@ -25,6 +25,9 @@ GDK_LIBS = $(filter-out $(FILT),@GDK_LIBS@) LIBSVG_CAIRO_CFLAGS = @LIBSVG_CAIRO_CFLAGS@ LIBSVG_CAIRO_LIBS = @LIBSVG_CAIRO_LIBS@ +LIBPANGOCAIRO_CFLAGS = @LIBPANGOCAIRO_CFLAGS@ +LIBPANGOCAIRO_LIBS = @LIBPANGOCAIRO_LIBS@ + cobjs = $(patsubst %.c, %.o, $(filter %.c,$(1))) mlintfs = $(patsubst %.mli, %.cmi, $(filter %.mli,$(1))) mlobjs = $(patsubst %.ml, %.cmo, $(filter %.ml,$(1))) diff --git a/configure.ac b/configure.ac index 2269dfa..6c8c4e9 100644 --- a/configure.ac +++ b/configure.ac @@ -29,9 +29,13 @@ fi # Optional libsvg-cairo support PKG_CHECK_MODULES(LIBSVG_CAIRO, libsvg-cairo, use_libsvg_cairo=yes, use_libsvg_cairo=no) +# Optional pango-cairo support +PKG_CHECK_MODULES(LIBPANGOCAIRO, pangocairo, use_libpangocairo=yes, use_libpangocairo=no) + echo echo " GTK+ support: $use_gtk" echo " libsvg-cairo support: $use_libsvg_cairo" +echo " libpangocairo support: $use_libpangocairo" echo AC_OUTPUT(config.make) diff --git a/src/Makefile b/src/Makefile index 24bf418..46a62f6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,6 +10,10 @@ endif ifdef LIBSVG_CAIRO_CFLAGS TARGETS += svgcairo endif +ifdef LIBPANGOCAIRO_CFLAGS +TARGETS += pangocairo +endif + all : $(TARGETS) $(if $(OCAMLOPT),opt) opt : $(addsuffix .opt,$(TARGETS)) @@ -21,6 +25,8 @@ gtkcairo : gtkcairo.cma libmlgtkcairo.a gtkcairo.opt : gtkcairo.cmxa dllmlgtkcairo.so svgcairo : svg_cairo.cma libmlsvgcairo.a svgcairo.opt : svg_cairo.cmxa dllmlsvgcairo.so +pangocairo : pango_cairo.cma libmlpangocairo.a +pangocairo.opt : pango_cairo.cmxa dllmlpangocairo.so cairo_SRC = cairo.mli cairo.ml \ cairo_bigarray.mli cairo_bigarray.ml \ @@ -60,12 +66,25 @@ svg_cairo.cmxa : $(call mloptobjs,$(svgcairo_SRC)) libmlsvgcairo.a dllmlsvgcairo.so : $(call cobjs,$(svgcairo_SRC)) $(OCAMLMKLIB) -o svg_cairo -oc mlsvgcairo $^ $(LIBSVG_CAIRO_LIBS) +pangocairo_SRC = pango_cairo.mli pango_cairo.ml ml_pango_cairo.c + +pango_cairo.cma : $(call mlobjs,$(pangocairo_SRC)) + $(OCAMLMKLIB) -o pango_cairo -oc mlpangocairo $^ $(LIBPANGOCAIRO_LIBS) +pango_cairo.cmxa : $(call mloptobjs,$(pangocairo_SRC)) + $(OCAMLMKLIB) -o pango_cairo -oc mlpangocairo $^ $(LIBPANGO_CAIRO_LIBS) +libmlpangocairo.a dllmlpangocairo.so : $(call cobjs,$(pangocairo_SRC)) + $(OCAMLMKLIB) -o pango_cairo -oc mlpangocairo $^ $(LIBPANGOCAIRO_LIBS) + $(call cobjs,$(cairo_SRC)) : CPPFLAGS+=$(CAIRO_CFLAGS) $(call cobjs,$(lablgtk_SRC)) : CPPFLAGS+=$(GDK_CFLAGS) -I$(C_LABLGTKDIR) $(call cobjs,$(svgcairo_SRC)) : CPPFLAGS+=$(LIBSVG_CAIRO_CFLAGS) +$(call cobjs,$(pangocairo_SRC)) : CPPFLAGS+=$(LIBPANGOCAIRO_CFLAGS) $(GDK_CFLAGS) -I$(C_LABLGTKDIR) $(call mlobjs,$(lablgtk_SRC)) : INCFLAGS=-I $(LABLGTKDIR) $(call mlintfs,$(lablgtk_SRC)) : INCFLAGS=-I $(LABLGTKDIR) $(call mloptobjs,$(lablgtk_SRC)) : INCFLAGS=-I $(LABLGTKDIR) +$(call mlobjs,$(pangocairo_SRC)) : INCFLAGS=-I $(LABLGTKDIR) +$(call mlintfs,$(pangocairo_SRC)) : INCFLAGS=-I $(LABLGTKDIR) +$(call mloptobjs,$(pangocairo_SRC)) : INCFLAGS=-I $(LABLGTKDIR) install: all mkdir -p $(DESTDIR)$(INSTALLDIR) $(DESTDIR)$(OCAMLLIB)/stublibs @@ -85,6 +104,10 @@ endif ifdef LIBSVG_CAIRO_CFLAGS DOCFILES += svg_cairo.mli endif +ifdef LIBPANGOCAIRO_CFLAGS +DOCFILES += pango_cairo.mli +endif + doc: $(DOCFILES:%.mli=%.cmi) mkdir -p ../doc/html ocamldoc -v -html -d ../doc/html -t Cairo-ocaml $(if $(LABLGTKDIR),-I $(LABLGTKDIR)) $(DOCFILES) @@ -94,7 +117,7 @@ clean : .depend : $(wildcard *.ml *.mli) @echo "making deps" - @$(OCAMLDEP) $^ > $@ + @$(OCAMLDEP) $(if $(LABLGTKDIR),-I $(LABLGTKDIR)) $^ > $@ -include .depend -include .depend_c diff --git a/src/ml_pango_cairo.c b/src/ml_pango_cairo.c new file mode 100644 index 0000000..6a2489a --- /dev/null +++ b/src/ml_pango_cairo.c @@ -0,0 +1,29 @@ +/**************************************************************************/ +/* cairo-ocaml -- Objective Caml bindings for Cairo */ +/* Copyright © 2004-2005 Olivier Andrieu */ +/* */ +/* This code is free software and is licensed under the terms of the */ +/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ +/**************************************************************************/ + +#define CAML_NAME_SPACE + +#include "ml_cairo.h" + +#include <pango/pangocairo.h> + +#include <wrappers.h> +#include <ml_glib.h> +#include <ml_gobject.h> +#include <ml_pango.h> + +#define PangoCairoFontMap_val(val) check_cast(PANGO_CAIRO_FONT_MAP, val) +#define Val_PangoCairoFontMap Val_GAnyObject + +wML_0(pango_cairo_font_map_new, Val_PangoCairoFontMap) +wML_0(pango_cairo_font_map_get_default, Val_PangoCairoFontMap) +wML_1(pango_cairo_font_map_create_context, PangoCairoFontMap_val, Val_PangoContext) +wML_2(pango_cairo_update_context, cairo_t_val, PangoContext_val, Unit) +wML_1(pango_cairo_create_layout, cairo_t_val, Val_PangoLayout) +wML_2(pango_cairo_update_layout, cairo_t_val, PangoLayout_val, Unit) +wML_2(pango_cairo_show_layout, cairo_t_val, PangoLayout_val, Unit) diff --git a/src/pango_cairo.ml b/src/pango_cairo.ml new file mode 100644 index 0000000..91a5873 --- /dev/null +++ b/src/pango_cairo.ml @@ -0,0 +1,30 @@ +(**************************************************************************) +(* cairo-ocaml -- Objective Caml bindings for Cairo *) +(* Copyright © 2004-2005 Olivier Andrieu *) +(* *) +(* This code is free software and is licensed under the terms of the *) +(* GNU Lesser General Public License version 2.1 (the "LGPL"). *) +(**************************************************************************) + +open Gaux +open Gobject + +type font_map = [`pangocairofontmap] obj + +module FontMap = struct + external new_ : unit -> font_map + = "ml_pango_cairo_font_map_new" + external get_default : unit -> font_map + = "ml_pango_cairo_font_map_get_default" + external create_context : font_map -> Pango.context + = "ml_pango_cairo_font_map_create_context" +end + +external update_context : Cairo.t -> Pango.context -> unit + = "ml_pango_cairo_update_context" +external create_layout : Cairo.t -> Pango.layout + = "ml_pango_cairo_create_layout" +external update_layout : Cairo.t -> Pango.layout -> unit + = "ml_pango_cairo_update_layout" +external show_layout : Cairo.t -> Pango.layout -> unit + = "ml_pango_cairo_show_layout" diff --git a/src/pango_cairo.mli b/src/pango_cairo.mli new file mode 100644 index 0000000..ec71cb7 --- /dev/null +++ b/src/pango_cairo.mli @@ -0,0 +1,25 @@ +(**************************************************************************) +(* cairo-ocaml -- Objective Caml bindings for Cairo *) +(* Copyright © 2004-2005 Olivier Andrieu *) +(* *) +(* This code is free software and is licensed under the terms of the *) +(* GNU Lesser General Public License version 2.1 (the "LGPL"). *) +(**************************************************************************) + +(** Cairo rendering backend for Pango. + For more details please see: + [http://developer.gnome.org/doc/API/2.0/pango/pango-Cairo-Rendering.html] + *) + +type font_map + +module FontMap : sig + val new_ : unit -> font_map + val get_default : unit -> font_map + val create_context : font_map -> Pango.context +end + +val update_context : Cairo.t -> Pango.context -> unit +val create_layout : Cairo.t -> Pango.layout +val update_layout : Cairo.t -> Pango.layout -> unit +val show_layout : Cairo.t -> Pango.layout -> unit diff --git a/test/Makefile b/test/Makefile index 67822b1..fbe3d2c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -8,12 +8,18 @@ endif ifdef LIBSVG_CAIRO_CFLAGS TARGETS += svg2png endif +ifdef LIBPANGOCAIRO_CFLAGS +TARGETS += pangocairo +endif all : $(TARGETS) svg2png : svg2png.ml $(OCAMLOPT) -o $@ -I ../src cairo.cmxa svg_cairo.cmxa $^ +pangocairo : pangocairo.ml + $(OCAMLOPT) -o $@ -I ../src -I $(LABLGTKDIR) lablgtk.cmxa cairo.cmxa pango_cairo.cmxa gtkInit.cmx $^ + kapow : kapow.ml $(OCAMLOPT) -o $@ -I ../src cairo.cmxa $^ @@ -42,6 +48,6 @@ knockout : knockout.ml $(OCAMLOPT) -o $@ -I ../src -I $(LABLGTKDIR) cairo.cmxa lablgtk.cmxa cairo_lablgtk.cmxa gtkInit.cmx $^ clean : - rm -f *.cm* *.o $(TARGETS) *.ps *.ppm *.png + rm -f *.cm* *.o $(TARGETS) *.ps *.ppm *.png *.svg *.pdf include ../Makefile.rules diff --git a/test/pangocairo.ml b/test/pangocairo.ml new file mode 100644 index 0000000..2fbf12b --- /dev/null +++ b/test/pangocairo.ml @@ -0,0 +1,59 @@ +(* This is a direct translation of the example Pango-Cairo program + * which can be found on this page: + * http://developer.gnome.org/doc/API/2.0/pango/pango-Cairo-Rendering.html + * $Id$ + * By Richard W.M. Jones <rich at annexia.org> or <rich at merjis.com> + *) + +let pi = 4. *. atan 1. (* Annoying omission from the stdlib. *) + +let radius = 150 +let n_words = 10 +let font = "Sans Bold 27" + +let radius_f = float radius +let n_words_f = float n_words +let scale_f = float Pango.scale + +let draw_text cr = + Cairo.translate cr radius_f radius_f; + let layout = Pango_cairo.create_layout cr in + Pango.Layout.set_text layout "Text"; + let desc = Pango.Font.from_string font in + Pango.Layout.set_font_description layout desc; + + (* Draw the layout n_words times in a circle. *) + for i = 0 to n_words-1; do + let angle = (360. *. float i) /. n_words_f in + Cairo.save cr; begin + (* Gradient from red at angle == 60 to blue at angle == 240 *) + let red = (1. +. cos ((angle -. 60.) *. pi /. 180.)) /. 2. in + Cairo.set_source_rgb cr red 0. (1.0 -. red); + + Cairo.rotate cr (angle *. pi /. 180.); + + (* Inform Pango to re-layout the text with the new transformation *) + Pango_cairo.update_layout cr layout; + + let width, height = Pango.Layout.get_size layout in + Cairo.move_to cr ~-.((float width/.scale_f) /. 2.) ~-.radius_f; + Pango_cairo.show_layout cr layout; + + end; Cairo.restore cr + done + +let () = + if Array.length Sys.argv <> 2 then + failwith "Usage: pangocairo OUTPUT_FILENAME.png"; + + let filename = Sys.argv.(1) in + let surface = + Cairo.image_surface_create + Cairo.FORMAT_ARGB32 (2 * radius) (2 * radius) in + let cr = Cairo.create surface in + + Cairo.set_source_rgb cr 1. 1. 1.; + Cairo.paint cr; + draw_text cr; + + Cairo_png.surface_write_to_file surface filename |