Author: fejj
Date: 2007-09-21 17:01:36 -0400 (Fri, 21 Sep 2007)
New Revision: 86179
Modified:
trunk/moon/src/ChangeLog
trunk/moon/src/font.cpp
trunk/moon/src/font.h
trunk/moon/src/moon-path.c
Log:
2007-09-21 Jeffrey Stedfast <[EMAIL PROTECTED]>
* font.cpp: Now handles scalable fonts by rendering their outline
paths rather than rendering bitmaps.
* moon-path.c (moon_path_move_to): Resize the path auto-magically
if we don't have enough room. Same for all of the other
line_to/curve_to/etc.
Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog 2007-09-21 19:44:10 UTC (rev 86178)
+++ trunk/moon/src/ChangeLog 2007-09-21 21:01:36 UTC (rev 86179)
@@ -1,3 +1,12 @@
+2007-09-21 Jeffrey Stedfast <[EMAIL PROTECTED]>
+
+ * font.cpp: Now handles scalable fonts by rendering their outline
+ paths rather than rendering bitmaps.
+
+ * moon-path.c (moon_path_move_to): Resize the path auto-magically
+ if we don't have enough room. Same for all of the other
+ line_to/curve_to/etc.
+
2007-09-21 Jeffrey Stedfast <[EMAIL PROTECTED]>
* font.cpp (SetMaxWidth): Oops, set max_width = max (rather than
Modified: trunk/moon/src/font.cpp
===================================================================
--- trunk/moon/src/font.cpp 2007-09-21 19:44:10 UTC (rev 86178)
+++ trunk/moon/src/font.cpp 2007-09-21 21:01:36 UTC (rev 86179)
@@ -15,9 +15,12 @@
#include <glib.h>
#include <string.h>
+#include "moon-path.h"
#include "font.h"
+#include FT_OUTLINE_H
+
static GHashTable *font_cache = NULL;
static bool initialized = false;
static FT_Library libft2;
@@ -122,13 +125,19 @@
FT_Glyph_Metrics metrics;
uint32_t unichar;
uint32_t index;
+ moon_path *path;
cairo_surface_t *surface;
FT_Bitmap bitmap;
int bitmap_left;
int bitmap_top;
};
+static const FT_Matrix invert_y = {
+ 65535, 0,
+ 0, -65535,
+};
+
#define LOAD_FLAGS (FT_LOAD_NO_BITMAP | FT_LOAD_TARGET_NORMAL)
TextFont::TextFont (FcPattern *pattern)
@@ -242,6 +251,94 @@
return face->size->metrics.height / 64;
}
+static int
+font_move_to (FT_Vector *to, void *user_data)
+{
+ moon_path *path = (moon_path *) user_data;
+ double x, y;
+
+ x = to->x / 64.0;
+ y = to->y / 64.0;
+
+ moon_move_to (path, x, y);
+
+ return 0;
+}
+
+static int
+font_line_to (FT_Vector *to, void *user_data)
+{
+ moon_path *path = (moon_path *) user_data;
+ double x, y;
+
+ x = to->x / 64.0;
+ y = to->y / 64.0;
+
+ moon_line_to (path, x, y);
+
+ return 0;
+}
+
+static int
+font_conic_to (FT_Vector *control, FT_Vector *to, void *user_data)
+{
+ moon_path *path = (moon_path *) user_data;
+ double x0, y0;
+ double x1, y1;
+ double x2, y2;
+ double x3, y3;
+ double x, y;
+
+ moon_get_current_point (path, &x0, &y0);
+
+ x = control->x / 64.0;
+ y = control->y / 64.0;
+
+ x3 = to->x / 64.0;
+ y3 = to->y / 64.0;
+
+ x1 = x0 + 2.0/3.0 * (x - x0);
+ y1 = y0 + 2.0/3.0 * (y - y0);
+
+ x2 = x3 + 2.0/3.0 * (x - x3);
+ y2 = y3 + 2.0/3.0 * (y - y3);
+
+ moon_curve_to (path, x1, y1, x2, y2, x3, y3);
+
+ return 0;
+}
+
+static int
+font_cubic_to (FT_Vector *control1, FT_Vector *control2, FT_Vector *to, void
*user_data)
+{
+ moon_path *path = (moon_path *) user_data;
+ double x0, y0;
+ double x1, y1;
+ double x2, y2;
+
+ x0 = control1->x / 64.0;
+ y0 = control1->y / 64.0;
+
+ x1 = control2->x / 64.0;
+ y1 = control2->y / 64.0;
+
+ x2 = to->x / 64.0;
+ y2 = to->y / 64.0;
+
+ moon_curve_to (path, x0, y0, x1, y1, x2, y2);
+
+ return 0;
+}
+
+static const FT_Outline_Funcs outline_funcs = {
+ (FT_Outline_MoveToFunc) font_move_to,
+ (FT_Outline_LineToFunc) font_line_to,
+ (FT_Outline_ConicToFunc) font_conic_to,
+ (FT_Outline_CubicToFunc) font_cubic_to,
+ 0, /* shift */
+ 0, /* delta */
+};
+
GlyphInfo *
TextFont::GetGlyphInfo (uint32_t unichar)
{
@@ -263,12 +360,23 @@
glyph->surface = NULL;
}
+ if (glyph->path) {
+ moon_path_destroy (glyph->path);
+ glyph->path = NULL;
+ }
+
g_free (glyph->bitmap.buffer);
if (glyph->index > 0 && FT_Load_Glyph (face, glyph->index,
LOAD_FLAGS) == 0) {
if (FT_Render_Glyph (face->glyph,
FT_RENDER_MODE_NORMAL) != 0)
goto unavail;
+ if (face->face_flags & FT_FACE_FLAG_SCALABLE) {
+ glyph->path = moon_path_new (16);
+ FT_Outline_Transform (&face->glyph->outline,
&invert_y);
+ FT_Outline_Decompose (&face->glyph->outline,
&outline_funcs, glyph->path);
+ }
+
//memcpy (&glyph->metrics, &face->glyph->metrics,
sizeof (glyph->metrics));
glyph->metrics.horiBearingX =
face->glyph->metrics.horiBearingX / 64;
glyph->metrics.horiBearingY =
face->glyph->metrics.horiBearingY / 64;
@@ -1130,6 +1238,20 @@
}
void
+TextLayout::RenderGlyphPath (cairo_t *cr, GlyphInfo *glyph, double x, double y)
+{
+ cairo_save (cr);
+
+ cairo_new_path (cr);
+ cairo_translate (cr, x, y);
+ cairo_append_path (cr, &glyph->path->cairo);
+ cairo_close_path (cr);
+ cairo_fill (cr);
+
+ cairo_restore (cr);
+}
+
+void
TextLayout::Render (cairo_t *cr, UIElement *element, double x, double y)
{
TextSegment *segment;
@@ -1140,12 +1262,9 @@
TextLine *line;
int bx, by, oy;
double dx, dy;
- GString *str;
int ascend;
int i;
- str = g_string_new ("");
-
Layout ();
line = (TextLine *) lines->First ();
@@ -1170,8 +1289,6 @@
if (!(glyph = font->GetGlyphInfo
(segment->text[i])))
continue;
- g_string_append_c (str, (char)
segment->text[i]);
-
if (glyph->index > 0) {
bx = glyph->metrics.horiBearingX;
by = glyph->metrics.horiBearingY;
@@ -1179,10 +1296,17 @@
// y-offset for the top of the glyph
oy = ascend - (line->height - ascend) -
by;
- //printf ("'%c' lh = %d, ascend = %d,
height = %d, by = %d, oy = %d\n",
- // (char) segment->text[i],
line->height, ascend, glyph->bitmap.rows, by, oy);
-
- RenderGlyphBitmap (cr, glyph, x + dx +
bx, y + dy + oy);
+ if (glyph->path) {
+ // y-offset for the bottom of
the glyph
+ oy = ascend - (line->height -
ascend);
+
+ RenderGlyphPath (cr, glyph, x +
dx + bx, y + dy + oy);
+ } else {
+ // y-offset for the top of the
glyph
+ oy = ascend - (line->height -
ascend) - by;
+
+ RenderGlyphBitmap (cr, glyph, x
+ dx + bx, y + dy + oy);
+ }
}
dx += (double) glyph->metrics.horiAdvance;
@@ -1195,11 +1319,5 @@
line = (TextLine *) line->next;
dx = 0.0f;
-
- if (line)
- g_string_append_c (str, '\n');
}
-
- printf ("rendered text (%d, %d) (%d, %d): \"%s\"\n", width, height,
max_width, max_height, str->str);
- g_string_free (str, true);
}
Modified: trunk/moon/src/font.h
===================================================================
--- trunk/moon/src/font.h 2007-09-21 19:44:10 UTC (rev 86178)
+++ trunk/moon/src/font.h 2007-09-21 21:01:36 UTC (rev 86179)
@@ -198,6 +198,7 @@
int width;
void RenderGlyphBitmap (cairo_t *cr, GlyphInfo *glyph, double x, double
y);
+ void RenderGlyphPath (cairo_t *cr, GlyphInfo *glyph, double x, double
y);
public:
Modified: trunk/moon/src/moon-path.c
===================================================================
--- trunk/moon/src/moon-path.c 2007-09-21 19:44:10 UTC (rev 86178)
+++ trunk/moon/src/moon-path.c 2007-09-21 21:01:36 UTC (rev 86179)
@@ -137,13 +137,21 @@
void
moon_move_to (moon_path *path, double x, double y)
{
- if (!path || !CHECK_SPACE (path, MOON_PATH_MOVE_TO_LENGTH)) {
- g_warning ("moon_move_to(%p,%g,%g)", path, x, y);
- return;
- }
-
cairo_path_data_t *data = path->cairo.data;
int pos = path->cairo.num_data;
+ int n = 1;
+
+ g_return_if_fail (path != NULL);
+
+ if (!CHECK_SPACE (path, MOON_PATH_MOVE_TO_LENGTH)) {
+ while (n < pos + MOON_PATH_MOVE_TO_LENGTH)
+ n <<= 1;
+
+ data = g_realloc (data, sizeof (cairo_path_data_t) * n);
+ path->cairo.data = data;
+ path->allocated = n;
+ }
+
data[pos].header.type = CAIRO_PATH_MOVE_TO;
data[pos].header.length = MOON_PATH_MOVE_TO_LENGTH;
pos++;
@@ -163,13 +171,21 @@
void
moon_line_to (moon_path *path, double x, double y)
{
- if (!path || !CHECK_SPACE (path, MOON_PATH_LINE_TO_LENGTH)) {
- g_warning ("moon_line_to(%p,%g,%g)", path, x, y);
- return;
- }
-
cairo_path_data_t *data = path->cairo.data;
int pos = path->cairo.num_data;
+ int n = 1;
+
+ g_return_if_fail (path != NULL);
+
+ if (!CHECK_SPACE (path, MOON_PATH_LINE_TO_LENGTH)) {
+ while (n < pos + MOON_PATH_LINE_TO_LENGTH)
+ n <<= 1;
+
+ data = g_realloc (data, sizeof (cairo_path_data_t) * n);
+ path->cairo.data = data;
+ path->allocated = n;
+ }
+
data[pos].header.type = CAIRO_PATH_LINE_TO;
data[pos].header.length = MOON_PATH_MOVE_TO_LENGTH;
pos++;
@@ -190,13 +206,21 @@
void
moon_curve_to (moon_path *path, double x1, double y1, double x2, double y2,
double x3, double y3)
{
- if (!path || !CHECK_SPACE (path, MOON_PATH_CURVE_TO_LENGTH)) {
- g_warning ("moon_curve_to(%p,%g,%g,%g,%g,%g,%g)", path, x1, y1,
x2, y2, x3, y3);
- return;
- }
-
cairo_path_data_t *data = path->cairo.data;
int pos = path->cairo.num_data;
+ int n = 1;
+
+ g_return_if_fail (path != NULL);
+
+ if (!CHECK_SPACE (path, MOON_PATH_CURVE_TO_LENGTH)) {
+ while (n < pos + MOON_PATH_CURVE_TO_LENGTH)
+ n <<= 1;
+
+ data = g_realloc (data, sizeof (cairo_path_data_t) * n);
+ path->cairo.data = data;
+ path->allocated = n;
+ }
+
data[pos].header.type = CAIRO_PATH_CURVE_TO;
data[pos].header.length = MOON_PATH_CURVE_TO_LENGTH;
pos++;
@@ -225,20 +249,27 @@
void
moon_ellipse (moon_path *path, double x, double y, double w, double h)
{
- if (!path || !CHECK_SPACE (path, MOON_PATH_ELLIPSE_LENGTH)) {
- g_warning ("moon_ellipse(%p,%g,%g,%g,%g)", path, x, y, w, h);
- return;
- }
-
+ cairo_path_data_t *data = path->cairo.data;
+ int pos = path->cairo.num_data;
double rx = w / 2.0;
double ry = h / 2.0;
double cx = x + rx;
double cy = y + ry;
double brx = ARC_TO_BEZIER * rx;
double bry = ARC_TO_BEZIER * ry;
-
- cairo_path_data_t *data = path->cairo.data;
- int pos = path->cairo.num_data;
+ int n = 1;
+
+ g_return_if_fail (path != NULL);
+
+ if (!CHECK_SPACE (path, MOON_PATH_ELLIPSE_LENGTH)) {
+ while (n < pos + MOON_PATH_ELLIPSE_LENGTH)
+ n <<= 1;
+
+ data = g_realloc (data, sizeof (cairo_path_data_t) * n);
+ path->cairo.data = data;
+ path->allocated = n;
+ }
+
data[pos].header.type = CAIRO_PATH_MOVE_TO;
data[pos].header.length = MOON_PATH_MOVE_TO_LENGTH;
pos++;
@@ -312,13 +343,21 @@
void
moon_rectangle (moon_path *path, double x, double y, double w, double h)
{
- if (!path || !CHECK_SPACE (path, MOON_PATH_RECTANGLE_LENGTH)) {
- g_warning ("moon_rectangle(%p,%g,%g,%g,%g)", path, x, y, w, h);
- return;
- }
-
cairo_path_data_t *data = path->cairo.data;
int pos = path->cairo.num_data;
+ int n = 1;
+
+ g_return_if_fail (path != NULL);
+
+ if (!CHECK_SPACE (path, MOON_PATH_RECTANGLE_LENGTH)) {
+ while (n < pos + MOON_PATH_RECTANGLE_LENGTH)
+ n <<= 1;
+
+ data = g_realloc (data, sizeof (cairo_path_data_t) * n);
+ path->cairo.data = data;
+ path->allocated = n;
+ }
+
data[pos].header.type = CAIRO_PATH_MOVE_TO;
data[pos].header.length = MOON_PATH_MOVE_TO_LENGTH;
pos++;
@@ -364,11 +403,21 @@
void
moon_rounded_rectangle (moon_path *path, double x, double y, double w, double
h, double radius_x, double radius_y)
{
- if (!path || !CHECK_SPACE (path, MOON_PATH_ROUNDED_RECTANGLE_LENGTH)) {
- g_warning ("moon_rounded_rectangle(%p,%g,%g,%g,%g,%g,%g)",
path, x, y, w, h, radius_x, radius_y);
- return;
+ cairo_path_data_t *data = path->cairo.data;
+ int pos = path->cairo.num_data;
+ int n = 1;
+
+ g_return_if_fail (path != NULL);
+
+ if (!CHECK_SPACE (path, MOON_PATH_ROUNDED_RECTANGLE_LENGTH)) {
+ while (n < pos + MOON_PATH_ROUNDED_RECTANGLE_LENGTH)
+ n <<= 1;
+
+ data = g_realloc (data, sizeof (cairo_path_data_t) * n);
+ path->cairo.data = data;
+ path->allocated = n;
}
-
+
if (radius_x < 0.0)
radius_x = -radius_x;
if (radius_y < 0.0)
@@ -383,9 +432,7 @@
// approximate (quite close) the arc using a bezier curve
double c1 = ARC_TO_BEZIER * radius_x;
double c2 = ARC_TO_BEZIER * radius_y;
-
- cairo_path_data_t *data = path->cairo.data;
- int pos = path->cairo.num_data;
+
data[pos].header.type = CAIRO_PATH_MOVE_TO;
data[pos].header.length = MOON_PATH_MOVE_TO_LENGTH;
pos++;
@@ -479,13 +526,21 @@
void
moon_close_path (moon_path *path)
{
- if (!path || !CHECK_SPACE (path, MOON_PATH_CLOSE_PATH_LENGTH)) {
- g_warning ("moon_close_path(%p)", path);
- return;
- }
-
cairo_path_data_t *data = path->cairo.data;
int pos = path->cairo.num_data;
+ int n = 1;
+
+ g_return_if_fail (path != NULL);
+
+ if (!CHECK_SPACE (path, MOON_PATH_CLOSE_PATH_LENGTH)) {
+ while (n < pos + MOON_PATH_CLOSE_PATH_LENGTH)
+ n <<= 1;
+
+ data = g_realloc (data, sizeof (cairo_path_data_t) * n);
+ path->cairo.data = data;
+ path->allocated = n;
+ }
+
data[pos].header.type = CAIRO_PATH_CLOSE_PATH;
data[pos].header.length = MOON_PATH_CLOSE_PATH_LENGTH;
path->cairo.num_data += MOON_PATH_CLOSE_PATH_LENGTH;
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches