[gjs/cairo: 17/18] [cairo] Add examples
- From: Johan Dahlin <johan src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/cairo: 17/18] [cairo] Add examples
- Date: Mon, 22 Feb 2010 22:13:53 +0000 (UTC)
commit cd71789caea31d35b8a2c6b8ceb9800d25af7fbf
Author: Johan Dahlin <johan gnome org>
Date: Sun Feb 21 16:00:11 2010 -0300
[cairo] Add examples
Makefile-examples.am | 9 +++++
examples/cairo/fill.js | 11 ++++++
examples/cairo/mask.js | 19 +++++++++++
examples/cairo/paint.js | 10 ++++++
examples/cairo/path-close.js | 20 ++++++++++++
examples/cairo/poppler-gdk.js | 58 +++++++++++++++++++++++++++++++++++
examples/cairo/setsourcegradient.js | 32 +++++++++++++++++++
examples/cairo/setsourcergba.js | 28 +++++++++++++++++
examples/cairo/showtext.js | 17 ++++++++++
examples/cairo/stroke.js | 13 ++++++++
10 files changed, 217 insertions(+), 0 deletions(-)
---
diff --git a/Makefile-examples.am b/Makefile-examples.am
index 70d59a0..7f2291f 100644
--- a/Makefile-examples.am
+++ b/Makefile-examples.am
@@ -1,4 +1,13 @@
EXTRA_DIST += \
+ examples/cairo/fill.js \
+ examples/cairo/mask.js \
+ examples/cairo/paint.js \
+ examples/cairo/path-close.js \
+ examples/cairo/poppler-gdk.js \
+ examples/cairo/setsourcegradient.js \
+ examples/cairo/setsourcergba.js \
+ examples/cairo/showtext.js \
+ examples/cairo/stroke.js \
examples/clutter.js \
examples/gio-cat.js \
examples/gtk.js \
diff --git a/examples/cairo/fill.js b/examples/cairo/fill.js
new file mode 100644
index 0000000..cc2a036
--- /dev/null
+++ b/examples/cairo/fill.js
@@ -0,0 +1,11 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+
+cr.setSourceRGB(0, 0, 0);
+cr.rectangle(0.25, 0.25, 0.5, 0.5);
+cr.fill();
+
+surface.writeToPNG("fill.png");
diff --git a/examples/cairo/mask.js b/examples/cairo/mask.js
new file mode 100644
index 0000000..a3dd341
--- /dev/null
+++ b/examples/cairo/mask.js
@@ -0,0 +1,19 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+
+let linpat = new Cairo.LinearGradient(0, 0, 1, 1);
+linpat.addColorStopRGB(0, 0, 0.3, 0.8);
+linpat.addColorStopRGB(1, 0, 0.8, 0.3);
+
+let radpat = new Cairo.RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.75);
+radpat.addColorStopRGBA(0, 0, 0, 0, 1);
+radpat.addColorStopRGBA(0.5, 0, 0, 0, 0);
+
+cr.setSource(linpat);
+
+cr.mask(radpat);
+
+surface.writeToPNG("mask.png");
diff --git a/examples/cairo/paint.js b/examples/cairo/paint.js
new file mode 100644
index 0000000..4c53280
--- /dev/null
+++ b/examples/cairo/paint.js
@@ -0,0 +1,10 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+
+cr.setSourceRGB(0.0, 0.0, 0.0);
+cr.paintWithAlpha(0.5);
+
+surface.writeToPNG("paint.png");
diff --git a/examples/cairo/path-close.js b/examples/cairo/path-close.js
new file mode 100644
index 0000000..62c391a
--- /dev/null
+++ b/examples/cairo/path-close.js
@@ -0,0 +1,20 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+
+cr.setLineWidth(0.1);
+cr.setSourceRGB(0, 0, 0);
+
+cr.moveTo(0.25, 0.25);
+cr.lineTo(0.5, 0.375);
+cr.relLineTo(0.25, -0.125);
+cr.arc(0.5, 0.5, 0.25 * Math.sqrt(2), -0.25 * Math.PI, 0.25 * Math.PI);
+cr.relCurveTo(-0.25, -0.125, -0.25, 0.125, -0.5, 0);
+cr.closePath();
+
+cr.stroke();
+
+surface.writeToPNG("path-close.png");
+
diff --git a/examples/cairo/poppler-gdk.js b/examples/cairo/poppler-gdk.js
new file mode 100644
index 0000000..14ef578
--- /dev/null
+++ b/examples/cairo/poppler-gdk.js
@@ -0,0 +1,58 @@
+const Cairo = imports.cairo;
+const Lang = imports.lang;
+
+const Gdk = imports.gi.Gdk;
+const Gtk = imports.gi.Gtk;
+const Poppler = imports.gi.Poppler;
+
+function PopplerApp(filename) {
+ this._init(filename);
+}
+
+PopplerApp.prototype = {
+ _init : function(filename) {
+ this._uri = "file://" + filename;
+ this._document = Poppler.Document.new_from_file(this._uri, "");
+ this._buildUI();
+ },
+
+ _buildUI : function() {
+ this._window = new Gtk.Window({ type: Gtk.WindowType.TOPLEVEL });
+ this._window.connect("destroy", Lang.bind(this, this._onWindowDestroy));
+
+ let da = new Gtk.DrawingArea();
+ da.connect("expose-event", Lang.bind(this, this._onDrawingAreaExpose));
+ this._window.add(da);
+ },
+
+ _onDrawingAreaExpose : function(window, event) {
+ let cr = Gdk.cairo_create(window.window);
+ //let page = this._document.get_page(0);
+ //page.render(cr);
+ },
+
+ _onWindowDestroy : function(window) {
+ Gtk.main_quit();
+ },
+
+ run : function() {
+ this._window.show_all();
+ Gtk.main();
+ }
+
+}
+
+function main() {
+ if (ARGV.length == 0) {
+ printerr("Need a filename");
+ return 1;
+ }
+
+ Gtk.init(0, null);
+
+ let app = new PopplerApp(ARGV[0]);
+ app.run();
+ return 0;
+}
+
+main();
diff --git a/examples/cairo/setsourcegradient.js b/examples/cairo/setsourcegradient.js
new file mode 100644
index 0000000..8981213
--- /dev/null
+++ b/examples/cairo/setsourcegradient.js
@@ -0,0 +1,32 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+
+let radpat = new Cairo.RadialGradient(0.25, 0.25, 0.1, 0.5, 0.5, 0.5);
+radpat.addColorStopRGB(0, 1.0, 0.8, 0.8);
+radpat.addColorStopRGB(1, 0.9, 0.0, 0.0);
+
+for (let i = 1; i < 10; ++i) {
+ for (let j = 1; j < 10; ++j) {
+ cr.rectangle(i/10.0 - 0.04, j/10.0 -0.04, 0.08, 0.08);
+ }
+}
+
+cr.setSource(radpat);
+cr.fill();
+
+let linpat = new Cairo.LinearGradient(0.25, 0.35, 0.75, 0.65);
+linpat.addColorStopRGBA(0.00, 1, 1, 1, 0);
+linpat.addColorStopRGBA(0.25, 0, 1, 0, 0.5);
+linpat.addColorStopRGBA(0.50, 1, 1, 1, 0);
+linpat.addColorStopRGBA(0.75, 0, 0, 1, 0.5);
+linpat.addColorStopRGBA(1.00, 1, 1, 1, 0);
+
+cr.rectangle(0.0, 0.0, 1, 1);
+cr.setSource(linpat);
+cr.fill();
+
+surface.writeToPNG("setsourcegradient.png");
+
diff --git a/examples/cairo/setsourcergba.js b/examples/cairo/setsourcergba.js
new file mode 100644
index 0000000..99a064d
--- /dev/null
+++ b/examples/cairo/setsourcergba.js
@@ -0,0 +1,28 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+
+cr.setSourceRGB(0, 0, 0);
+cr.moveTo(0, 0);
+cr.lineTo(1, 1);
+cr.moveTo(1, 0);
+cr.lineTo(0, 1);
+cr.setLineWidth(0.2);
+cr.stroke();
+
+cr.rectangle(0, 0, 0.5, 0.5);
+cr.setSourceRGBA(1, 0, 0, 0.80);
+cr.fill();
+
+cr.rectangle(0, 0.5, 0.5, 0.5);
+cr.setSourceRGBA(0, 1, 0, 0.60);
+cr.fill();
+
+cr.rectangle(0.5, 0, 0.5, 0.5);
+cr.setSourceRGBA(0, 0, 1, 0.40);
+cr.fill();
+
+surface.writeToPNG("setsourcergba.png");
+
diff --git a/examples/cairo/showtext.js b/examples/cairo/showtext.js
new file mode 100644
index 0000000..6fb364c
--- /dev/null
+++ b/examples/cairo/showtext.js
@@ -0,0 +1,17 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+
+cr.setSourceRGB(0, 0, 0);
+cr.selectFontFace("Georgia", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
+cr.setFontSize(1.2);
+
+//let te = cr.text_extents("a");
+//cr.moveTo(0.5 - te.width / 2 - te.xBearing,
+// 0.5 - te.height / 2 - te.yBearing);
+cr.showText("a");
+
+surface.writeToPNG("showtext.png");
+
diff --git a/examples/cairo/stroke.js b/examples/cairo/stroke.js
new file mode 100644
index 0000000..821d55a
--- /dev/null
+++ b/examples/cairo/stroke.js
@@ -0,0 +1,13 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+
+cr.setLineWidth(0.1);
+cr.setSourceRGB(0, 0, 0);
+cr.rectangle(0.25, 0.25, 0.5, 0.5);
+cr.stroke();
+
+surface.writeToPNG("stroke.png");
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]