From b49fde35c93fc2277e4afac78f7ab1497b7bb381 Mon Sep 17 00:00:00 2001 From: James Bowes Date: Sat, 22 Mar 2008 13:52:06 -0400 Subject: [PATCH] implement TextFormat.getTextExtent --- swfdec/swfdec_as_strings.c | 4 ++ swfdec/swfdec_text_format.c | 74 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletions(-) diff --git a/swfdec/swfdec_as_strings.c b/swfdec/swfdec_as_strings.c index 8e6cb9c..2170366 100644 --- a/swfdec/swfdec_as_strings.c +++ b/swfdec/swfdec_as_strings.c @@ -496,5 +496,9 @@ const char swfdec_as_strings[] = SWFDEC_AS_CONSTANT_STRING ("Selection") SWFDEC_AS_CONSTANT_STRING ("tabEnabled") SWFDEC_AS_CONSTANT_STRING ("focusEnabled") + SWFDEC_AS_CONSTANT_STRING ("ascent") + SWFDEC_AS_CONSTANT_STRING ("descent") + SWFDEC_AS_CONSTANT_STRING ("textFieldHeight") + SWFDEC_AS_CONSTANT_STRING ("textFieldWidth") /* add more here */ ; diff --git a/swfdec/swfdec_text_format.c b/swfdec/swfdec_text_format.c index 79e82ee..288a5fd 100644 --- a/swfdec/swfdec_text_format.c +++ b/swfdec/swfdec_text_format.c @@ -24,6 +24,7 @@ #include #include +#include #include "swfdec_text_format.h" #include "swfdec_as_native_function.h" @@ -852,7 +853,78 @@ static void swfdec_text_format_getTextExtent (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret) { - SWFDEC_STUB ("TextFormat.getTextExtent"); + SwfdecTextFormat *format; + SwfdecAsObject *obj = swfdec_as_object_new_empty (cx); + SwfdecAsValue val; + const gchar *text; + GList *item_list; + PangoAnalysis analysis; + PangoFontMap *fontmap; + PangoContext *pcontext; + PangoAttrList *attrs; + PangoGlyphString *glyph_string = pango_glyph_string_new(); + PangoRectangle ink_rect; + PangoFontDescription *desc; + PangoFont *font; + + g_return_if_fail (SWFDEC_IS_TEXT_FORMAT (object)); + + format = SWFDEC_TEXT_FORMAT (object); + swfdec_as_object_get_variable (object, SWFDEC_AS_STR_TextFormat, &val); + text = swfdec_as_value_to_string(cx, &argv[0]); + + fontmap = pango_cairo_font_map_get_default (); + pcontext = + pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap)); + + attrs = pango_attr_list_new (); + item_list = pango_itemize (pcontext, text, 0, strlen(text), attrs, NULL); + pango_attr_list_unref (attrs); + + analysis = ((PangoItem *)(item_list[0].data))->analysis; + pango_shape (text, strlen(text), &analysis, glyph_string); + + desc = pango_font_description_new (); + pango_font_description_set_family_static (desc, format->font); + pango_font_description_set_size (desc, format->size); + if (format->bold){ + pango_font_description_set_weight (desc, PANGO_WEIGHT_BOLD); + } + if (format->italic) + pango_font_description_set_style (desc, PANGO_STYLE_ITALIC); + + font = pango_font_map_load_font (fontmap, pcontext, desc); + pango_glyph_string_extents (glyph_string, font, &ink_rect, NULL); + + pango_glyph_string_free (glyph_string); + g_list_foreach (item_list, (GFunc) pango_item_free, NULL); + g_list_free (item_list); + pango_font_description_free (desc); + g_object_unref (G_OBJECT (pcontext)); + g_object_unref (G_OBJECT (fontmap)); + g_object_unref (G_OBJECT (font)); + + SWFDEC_AS_VALUE_SET_INT (&val, ink_rect.width + format->left_margin + + format->right_margin); + swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_width, &val); + + SWFDEC_AS_VALUE_SET_INT (&val, (ink_rect.height + format->leading)); + swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_height, &val); + + SWFDEC_AS_VALUE_SET_INT (&val, PANGO_ASCENT(ink_rect)); + swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_ascent, &val); + + SWFDEC_AS_VALUE_SET_INT (&val, PANGO_DESCENT (ink_rect)); + swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_descent, &val); + + SWFDEC_AS_VALUE_SET_INT (&val, ink_rect.width + format->left_margin + + format->right_margin + 4); + swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_textFieldWidth, &val); + + SWFDEC_AS_VALUE_SET_INT (&val, ink_rect.height + 4 + format->leading); + swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_textFieldHeight, &val); + + SWFDEC_AS_VALUE_SET_OBJECT (ret, obj); } void -- 1.5.5.rc2.155.gb0a67