summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathon Jongsma <jjongsma@gnome.org>2008-12-07 22:21:54 -0600
committerJonathon Jongsma <jjongsma@gnome.org>2008-12-08 22:34:55 -0600
commit62d59edc9323b6785f879466493d2227f0ca61a4 (patch)
treeea75e601b43007763e79074e50f6741d83280289
parent7924bd955ce2072539f83cea9b070a4897d0fb22 (diff)
Added a very simple UserFontFace example
* examples/text/Makefile.am: * examples/text/user-font.cc: Added a very simple example of using a UserFontFace to draw text
-rw-r--r--ChangeLog6
-rw-r--r--examples/text/Makefile.am5
-rw-r--r--examples/text/user-font.cc66
3 files changed, 75 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 69dc6a7..cca5e30 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2008-12-07 Jonathon Jongsma <jonathon@quotidian.org>
+ * examples/text/Makefile.am:
+ * examples/text/user-font.cc: Added a very simple example of using a
+ UserFontFace to draw text
+
+2008-12-07 Jonathon Jongsma <jonathon@quotidian.org>
+
* cairomm/fontface.cc: Fix the default implementation of UserFontFace::init()
to set up the font extents parameter correctly according to the documentation
diff --git a/examples/text/Makefile.am b/examples/text/Makefile.am
index adb68b0..19f6e70 100644
--- a/examples/text/Makefile.am
+++ b/examples/text/Makefile.am
@@ -1,9 +1,10 @@
include $(top_srcdir)/examples/Makefile.am_fragment
# build the executable but don't install it
-noinst_PROGRAMS = text_rotate toy-text
+noinst_PROGRAMS = text_rotate toy-text user-font
text_rotate_SOURCES = text-rotate.cc
toy_text_SOURCES=toy-text.cc
+user_font_SOURCES=user-font.cc
-CLEANFILES = text-rotate.png toy-text.png
+CLEANFILES = text-rotate.png toy-text.png user-font.png
diff --git a/examples/text/user-font.cc b/examples/text/user-font.cc
new file mode 100644
index 0000000..8e5dae9
--- /dev/null
+++ b/examples/text/user-font.cc
@@ -0,0 +1,66 @@
+#include <cairomm/cairomm.h>
+#include <iostream>
+
+const double HEIGHT = 200.0;
+const double WIDTH = 400.0;
+const double FONT_SIZE = 64.0;
+const double TEXT_ORIGIN_Y = (HEIGHT / 2.0) + (FONT_SIZE / 2.0);
+const double TEXT_ORIGIN_X = 50.0; // arbitrary
+
+// A *very* simple font that just draws a box for every glyph
+class BoxFontFace : public Cairo::UserFontFace
+{
+public:
+ static Cairo::RefPtr<BoxFontFace> create()
+ {
+ return Cairo::RefPtr<BoxFontFace>(new BoxFontFace());
+ }
+
+ virtual Cairo::ErrorStatus
+ render_glyph(const Cairo::RefPtr<Cairo::ScaledFont>& /*scaled_font*/,
+ unsigned long glyph,
+ const Cairo::RefPtr<Cairo::Context>& cr,
+ Cairo::TextExtents& /*metrics*/)
+ {
+ std::cout << "Rendering glyph " << glyph << std::endl;
+ cr->set_line_width(0.05);
+ // FIXME: is the negative Y value correct?
+ cr->rectangle(0.0, 0.0, 1.0, -1.0);
+ cr->stroke();
+ return CAIRO_STATUS_SUCCESS;
+ }
+
+protected:
+ BoxFontFace() : UserFontFace() { }
+};
+
+int main(int, char**)
+{
+ Cairo::RefPtr<Cairo::ImageSurface> surface =
+ Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, WIDTH, HEIGHT);
+ Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface);
+ // fill background in white
+ cr->set_source_rgb(1.0, 1.0, 1.0);
+ cr->paint();
+
+ // draw a little dot at the point where text will be drawn
+ cr->arc(TEXT_ORIGIN_X, TEXT_ORIGIN_Y, FONT_SIZE / 4.0, 0, 2*M_PI);
+ cr->set_source_rgba(0.0, 1.0, 0.0, 0.5);
+ cr->fill();
+
+ // draw the text
+ cr->move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
+ cr->set_source_rgb(0.8, 0.2, 0.2);
+
+ // this scope block is simply to test that the user font can still be
+ // drawn even after the UserFont wrapper object has been de-referenced
+ {
+ Cairo::RefPtr<BoxFontFace> font = BoxFontFace::create();
+ cr->set_font_face(font);
+ }
+
+ cr->set_font_size(FONT_SIZE);
+ cr->show_text("cairomm!");
+ surface->write_to_png("user-font.png");
+ return 0;
+}