From fdda3cf2c56d8b06a89a4233166dd27c8ec9fa2e Mon Sep 17 00:00:00 2001 From: Jonathon Jongsma Date: Thu, 14 Aug 2008 09:59:13 -0500 Subject: Implement the ToyFontFace class --- examples/Makefile.am | 2 +- examples/text-rotate/.cvsignore | 6 --- examples/text-rotate/Makefile.am | 7 --- examples/text-rotate/text-rotate.cc | 103 ------------------------------------ examples/text/Makefile.am | 9 ++++ examples/text/text-rotate.cc | 103 ++++++++++++++++++++++++++++++++++++ examples/text/toy-text.cc | 35 ++++++++++++ 7 files changed, 148 insertions(+), 117 deletions(-) delete mode 100644 examples/text-rotate/.cvsignore delete mode 100644 examples/text-rotate/Makefile.am delete mode 100644 examples/text-rotate/text-rotate.cc create mode 100644 examples/text/Makefile.am create mode 100644 examples/text/text-rotate.cc create mode 100644 examples/text/toy-text.cc (limited to 'examples') diff --git a/examples/Makefile.am b/examples/Makefile.am index 0c42e94..b361432 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,3 +1,3 @@ -SUBDIRS = png_file pdf-surface ps-surface svg-surface text-rotate +SUBDIRS = png_file pdf-surface ps-surface svg-surface text EXTRA_DIST = README Makefile.am_fragment diff --git a/examples/text-rotate/.cvsignore b/examples/text-rotate/.cvsignore deleted file mode 100644 index c64b95d..0000000 --- a/examples/text-rotate/.cvsignore +++ /dev/null @@ -1,6 +0,0 @@ -.deps -.libs -Makefile -Makefile.in -text_rotate -text-rotate.png diff --git a/examples/text-rotate/Makefile.am b/examples/text-rotate/Makefile.am deleted file mode 100644 index 9809f5f..0000000 --- a/examples/text-rotate/Makefile.am +++ /dev/null @@ -1,7 +0,0 @@ -include $(top_srcdir)/examples/Makefile.am_fragment - -# build the executable but don't install it -noinst_PROGRAMS = text_rotate -text_rotate_SOURCES = text-rotate.cc - -CLEANFILES = text-rotate.png diff --git a/examples/text-rotate/text-rotate.cc b/examples/text-rotate/text-rotate.cc deleted file mode 100644 index 4a78829..0000000 --- a/examples/text-rotate/text-rotate.cc +++ /dev/null @@ -1,103 +0,0 @@ -/* Copyright (C) 2005 The cairomm Development Team - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - */ - -#include -#include -#include -#include - -/* M_PI is defined in math.h in the case of Microsoft Visual C++, and - * Solaris needs math.h for M_PI and floor() - */ -#if defined(_MSC_VER) -#define _USE_MATH_DEFINES -#endif - -#ifdef HAVE_MATH_H -# include -#endif - -// This example is based on the C cairo example of the same name - -const int WIDTH = 150; -const int HEIGHT = 150; -const int NUM_TEXT = 20; -const int TEXT_SIZE = 12; - -/* Draw the word cairo at NUM_TEXT different angles */ -void draw(Cairo::RefPtr cr, int width, int height) -{ - int i, x_off, y_off; - Cairo::TextExtents extents; - std::string text("cairo"); - - cr->select_font_face("Bitstream Vera Sans", Cairo::FONT_SLANT_NORMAL, - Cairo::FONT_WEIGHT_NORMAL); - cr->set_font_size(TEXT_SIZE); - - Cairo::FontOptions font_options; - - font_options.set_hint_style(Cairo::HINT_STYLE_NONE); - font_options.set_hint_metrics(Cairo::HINT_METRICS_OFF); - font_options.set_antialias(Cairo::ANTIALIAS_GRAY); - - cr->set_font_options(font_options); - - cr->set_source_rgb(0.0, 0.0, 0.0); - - cr->translate(width / 2.0, height / 2.0); - - cr->get_text_extents(text, extents); - - if (NUM_TEXT == 1) - { - x_off = y_off = 0; - } - else - { - y_off = (int) - floor(0.5 + extents.height / 2.0); - x_off = (int) floor(0.5 + (extents.height + 1.0) / (2.0 * tan (M_PI / NUM_TEXT))); - } - - for (i=0; i < NUM_TEXT; i++) - { - cr->save(); - cr->rotate(2 * M_PI * i / NUM_TEXT); - cr->set_line_width(1.0); - cr->rectangle(x_off - 0.5, y_off - 0.5, extents.width + 1, - extents.height + 1); - cr->set_source_rgb(1, 0, 0); - cr->stroke(); - cr->move_to(x_off - extents.x_bearing, y_off - extents.y_bearing); - cr->set_source_rgb(0, 0, 0); - cr->show_text("cairo"); - cr->restore(); - } -} - -int main (void) -{ - Cairo::RefPtr surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, WIDTH, HEIGHT); - Cairo::RefPtr cr = Cairo::Context::create(surface); - draw(cr, WIDTH, HEIGHT); -#ifdef CAIRO_HAS_PNG_FUNCTIONS - surface->write_to_png("text-rotate.png"); -#else - std::cout << "You must compile cairo with PNG support for this example to work" << std::endl; -#endif -} diff --git a/examples/text/Makefile.am b/examples/text/Makefile.am new file mode 100644 index 0000000..adb68b0 --- /dev/null +++ b/examples/text/Makefile.am @@ -0,0 +1,9 @@ +include $(top_srcdir)/examples/Makefile.am_fragment + +# build the executable but don't install it +noinst_PROGRAMS = text_rotate toy-text + +text_rotate_SOURCES = text-rotate.cc +toy_text_SOURCES=toy-text.cc + +CLEANFILES = text-rotate.png toy-text.png diff --git a/examples/text/text-rotate.cc b/examples/text/text-rotate.cc new file mode 100644 index 0000000..4a78829 --- /dev/null +++ b/examples/text/text-rotate.cc @@ -0,0 +1,103 @@ +/* Copyright (C) 2005 The cairomm Development Team + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include +#include +#include +#include + +/* M_PI is defined in math.h in the case of Microsoft Visual C++, and + * Solaris needs math.h for M_PI and floor() + */ +#if defined(_MSC_VER) +#define _USE_MATH_DEFINES +#endif + +#ifdef HAVE_MATH_H +# include +#endif + +// This example is based on the C cairo example of the same name + +const int WIDTH = 150; +const int HEIGHT = 150; +const int NUM_TEXT = 20; +const int TEXT_SIZE = 12; + +/* Draw the word cairo at NUM_TEXT different angles */ +void draw(Cairo::RefPtr cr, int width, int height) +{ + int i, x_off, y_off; + Cairo::TextExtents extents; + std::string text("cairo"); + + cr->select_font_face("Bitstream Vera Sans", Cairo::FONT_SLANT_NORMAL, + Cairo::FONT_WEIGHT_NORMAL); + cr->set_font_size(TEXT_SIZE); + + Cairo::FontOptions font_options; + + font_options.set_hint_style(Cairo::HINT_STYLE_NONE); + font_options.set_hint_metrics(Cairo::HINT_METRICS_OFF); + font_options.set_antialias(Cairo::ANTIALIAS_GRAY); + + cr->set_font_options(font_options); + + cr->set_source_rgb(0.0, 0.0, 0.0); + + cr->translate(width / 2.0, height / 2.0); + + cr->get_text_extents(text, extents); + + if (NUM_TEXT == 1) + { + x_off = y_off = 0; + } + else + { + y_off = (int) - floor(0.5 + extents.height / 2.0); + x_off = (int) floor(0.5 + (extents.height + 1.0) / (2.0 * tan (M_PI / NUM_TEXT))); + } + + for (i=0; i < NUM_TEXT; i++) + { + cr->save(); + cr->rotate(2 * M_PI * i / NUM_TEXT); + cr->set_line_width(1.0); + cr->rectangle(x_off - 0.5, y_off - 0.5, extents.width + 1, + extents.height + 1); + cr->set_source_rgb(1, 0, 0); + cr->stroke(); + cr->move_to(x_off - extents.x_bearing, y_off - extents.y_bearing); + cr->set_source_rgb(0, 0, 0); + cr->show_text("cairo"); + cr->restore(); + } +} + +int main (void) +{ + Cairo::RefPtr surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, WIDTH, HEIGHT); + Cairo::RefPtr cr = Cairo::Context::create(surface); + draw(cr, WIDTH, HEIGHT); +#ifdef CAIRO_HAS_PNG_FUNCTIONS + surface->write_to_png("text-rotate.png"); +#else + std::cout << "You must compile cairo with PNG support for this example to work" << std::endl; +#endif +} diff --git a/examples/text/toy-text.cc b/examples/text/toy-text.cc new file mode 100644 index 0000000..1f22521 --- /dev/null +++ b/examples/text/toy-text.cc @@ -0,0 +1,35 @@ +#include + +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 + +int main(int, char**) +{ + Cairo::RefPtr surface = + Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, WIDTH, HEIGHT); + Cairo::RefPtr 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); + Cairo::RefPtr font = + Cairo::ToyFontFace::create("Bitstream Charter", + Cairo::FONT_SLANT_ITALIC, + Cairo::FONT_WEIGHT_BOLD); + cr->set_font_face(font); + cr->set_font_size(FONT_SIZE); + cr->show_text("cairomm!"); + surface->write_to_png("toy-text.png"); + return 0; +} -- cgit v1.2.3