2006-07-14 12:45:48 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2006 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software
|
|
|
|
|
* and its documentation for any purpose is hereby granted without
|
|
|
|
|
* fee, provided that the above copyright notice appear in all copies
|
|
|
|
|
* and that both that copyright notice and this permission notice
|
|
|
|
|
* appear in supporting documentation, and that the name of
|
|
|
|
|
* Red Hat, Inc. not be used in advertising or publicity pertaining to
|
|
|
|
|
* distribution of the software without specific, written prior
|
|
|
|
|
* permission. Red Hat, Inc. makes no representations about the
|
|
|
|
|
* suitability of this software for any purpose. It is provided "as
|
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
|
*
|
|
|
|
|
* RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
|
|
|
|
|
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
|
* FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
|
|
|
|
|
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
|
|
|
|
|
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
|
|
|
|
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
* Author: Carl D. Worth <cworth@cworth.org>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "cairo-test.h"
|
|
|
|
|
|
|
|
|
|
#define TEXT_SIZE 12
|
|
|
|
|
#define PAD 4
|
|
|
|
|
#define TEXT "text"
|
|
|
|
|
|
2008-01-25 07:02:25 -05:00
|
|
|
static cairo_bool_t
|
|
|
|
|
text_extents_equal (const cairo_text_extents_t *A,
|
|
|
|
|
const cairo_text_extents_t *B)
|
|
|
|
|
{
|
|
|
|
|
return A->x_bearing == B->x_bearing &&
|
|
|
|
|
A->y_bearing == B->y_bearing &&
|
|
|
|
|
A->width == B->width &&
|
|
|
|
|
A->height == B->height &&
|
|
|
|
|
A->x_advance == B->x_advance &&
|
|
|
|
|
A->y_advance == B->y_advance;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 02:33:58 -05:00
|
|
|
static cairo_test_status_t
|
2008-08-11 21:12:45 +01:00
|
|
|
box_text (const cairo_test_context_t *ctx, cairo_t *cr,
|
|
|
|
|
const char *utf8,
|
|
|
|
|
double x, double y)
|
2006-07-14 12:45:48 -07:00
|
|
|
{
|
|
|
|
|
double line_width;
|
2008-02-01 18:45:59 -08:00
|
|
|
cairo_text_extents_t extents = {0}, scaled_extents = {0};
|
2008-01-25 07:02:25 -05:00
|
|
|
cairo_scaled_font_t *scaled_font;
|
2008-10-16 14:26:15 +01:00
|
|
|
cairo_status_t status;
|
2006-07-14 12:45:48 -07:00
|
|
|
|
|
|
|
|
cairo_save (cr);
|
|
|
|
|
|
|
|
|
|
cairo_text_extents (cr, utf8, &extents);
|
2008-01-25 07:02:25 -05:00
|
|
|
|
|
|
|
|
scaled_font = cairo_get_scaled_font (cr);
|
|
|
|
|
cairo_scaled_font_text_extents (scaled_font, TEXT, &scaled_extents);
|
2008-10-16 14:26:15 +01:00
|
|
|
status = cairo_scaled_font_status (scaled_font);
|
|
|
|
|
if (status)
|
|
|
|
|
return cairo_test_status_from_status (ctx, status);
|
|
|
|
|
|
2008-01-25 07:02:25 -05:00
|
|
|
if (! text_extents_equal (&extents, &scaled_extents)) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx,
|
|
|
|
|
"Error: extents differ when they shouldn't:\n"
|
2008-01-29 09:01:47 -08:00
|
|
|
"cairo_text_extents(); extents (%g, %g, %g, %g, %g, %g)\n"
|
2008-01-25 07:02:25 -05:00
|
|
|
"cairo_scaled_font_text_extents(); extents (%g, %g, %g, %g, %g, %g)\n",
|
|
|
|
|
extents.x_bearing, extents.y_bearing,
|
|
|
|
|
extents.width, extents.height,
|
|
|
|
|
extents.x_advance, extents.y_advance,
|
|
|
|
|
scaled_extents.x_bearing, scaled_extents.y_bearing,
|
|
|
|
|
scaled_extents.width, scaled_extents.height,
|
|
|
|
|
scaled_extents.x_advance, scaled_extents.y_advance);
|
|
|
|
|
return CAIRO_TEST_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-14 12:45:48 -07:00
|
|
|
line_width = cairo_get_line_width (cr);
|
|
|
|
|
cairo_rectangle (cr,
|
|
|
|
|
x + extents.x_bearing - line_width / 2,
|
|
|
|
|
y + extents.y_bearing - line_width / 2,
|
|
|
|
|
extents.width + line_width,
|
|
|
|
|
extents.height + line_width);
|
|
|
|
|
cairo_stroke (cr);
|
|
|
|
|
|
|
|
|
|
cairo_move_to (cr, x, y);
|
|
|
|
|
cairo_show_text (cr, utf8);
|
|
|
|
|
|
|
|
|
|
cairo_restore (cr);
|
2008-01-28 02:33:58 -05:00
|
|
|
|
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
2006-07-14 12:45:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static cairo_test_status_t
|
|
|
|
|
draw (cairo_t *cr, int width, int height)
|
|
|
|
|
{
|
2008-08-11 21:12:45 +01:00
|
|
|
const cairo_test_context_t *ctx = cairo_test_get_context (cr);
|
2008-01-28 02:33:58 -05:00
|
|
|
cairo_test_status_t status;
|
2006-07-14 12:45:48 -07:00
|
|
|
cairo_text_extents_t extents;
|
|
|
|
|
cairo_matrix_t matrix;
|
|
|
|
|
|
|
|
|
|
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
|
|
|
|
|
cairo_paint (cr);
|
|
|
|
|
|
2010-06-12 16:20:37 +02:00
|
|
|
cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
|
2006-07-14 12:45:48 -07:00
|
|
|
CAIRO_FONT_SLANT_NORMAL,
|
|
|
|
|
CAIRO_FONT_WEIGHT_NORMAL);
|
|
|
|
|
cairo_set_font_size (cr, TEXT_SIZE);
|
|
|
|
|
|
|
|
|
|
cairo_translate (cr, PAD, PAD);
|
|
|
|
|
cairo_set_line_width (cr, 1.0);
|
|
|
|
|
|
|
|
|
|
cairo_text_extents (cr, TEXT, &extents);
|
|
|
|
|
|
|
|
|
|
/* Draw text and bounding box */
|
|
|
|
|
cairo_set_source_rgb (cr, 0, 0, 0); /* black */
|
2008-08-11 21:12:45 +01:00
|
|
|
status = box_text (ctx, cr, TEXT, 0, - extents.y_bearing);
|
2008-01-28 02:33:58 -05:00
|
|
|
if (status)
|
|
|
|
|
return status;
|
2006-07-14 12:45:48 -07:00
|
|
|
|
|
|
|
|
/* Then draw again with the same coordinates, but with a font
|
|
|
|
|
* matrix to position the text below and shifted a bit to the
|
|
|
|
|
* right. */
|
|
|
|
|
cairo_matrix_init_translate (&matrix, TEXT_SIZE / 2, TEXT_SIZE + PAD);
|
|
|
|
|
cairo_matrix_scale (&matrix, TEXT_SIZE, TEXT_SIZE);
|
|
|
|
|
cairo_set_font_matrix (cr, &matrix);
|
|
|
|
|
|
|
|
|
|
cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
|
2008-08-11 21:12:45 +01:00
|
|
|
status = box_text (ctx, cr, TEXT, 0, - extents.y_bearing);
|
2008-01-28 02:33:58 -05:00
|
|
|
if (status)
|
|
|
|
|
return status;
|
2006-07-14 12:45:48 -07:00
|
|
|
|
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-03 16:38:03 +01:00
|
|
|
CAIRO_TEST (font_matrix_translation,
|
|
|
|
|
"Test that translation in a font matrix can be used to offset a string",
|
|
|
|
|
"font", /* keywords */
|
|
|
|
|
NULL, /* requirements */
|
|
|
|
|
38, 34,
|
|
|
|
|
NULL, draw)
|