Code

Correcting statements collapsed to a single line.
[inkscape.git] / src / widgets / font-selector.cpp
index c9e5d0bbbc94720c0c8b0f967b7a54bc4efd252b..ce0893430543728a89fdaaa18f250f4589c455b8 100644 (file)
@@ -21,6 +21,7 @@
 #endif
 
 #include <libnr/nr-blit.h>
+#include <libnr/nr-convert2geom.h>
 #include <libnrtype/font-instance.h>
 #include <libnrtype/raster-glyph.h>
 #include <libnrtype/RasterFont.h>
@@ -28,6 +29,8 @@
 #include <libnrtype/one-glyph.h>
 #include <libnrtype/font-lister.h>
 
+#include <2geom/transforms.h>
+
 #include <gtk/gtk.h>
 #include <gtk/gtkframe.h>
 #include <gtk/gtkscrolledwindow.h>
@@ -107,19 +110,23 @@ namespace {
 static GtkHBoxClass *fs_parent_class = NULL;
 static guint fs_signals[LAST_SIGNAL] = { 0 };
 
-GtkType sp_font_selector_get_type()
+GType sp_font_selector_get_type()
 {
-    static GtkType type = 0;
+    static GType type = 0;
     if (!type) {
-        static const GtkTypeInfo info = {
-            "SPFontSelector",
-            sizeof(SPFontSelector),
+        GTypeInfo info = {
             sizeof(SPFontSelectorClass),
-            (GtkClassInitFunc) sp_font_selector_class_init,
-            (GtkObjectInitFunc) sp_font_selector_init,
-            NULL, NULL, NULL
+            0, // base_init
+            0, // base_finalize
+            (GClassInitFunc)sp_font_selector_class_init,
+            0, // class_finalize
+            0, // class_data
+            sizeof(SPFontSelector),
+            0, // n_preallocs
+            (GInstanceInitFunc)sp_font_selector_init,
+            0 // value_table
         };
-        type = gtk_type_unique(GTK_TYPE_HBOX, &info);
+        type = g_type_register_static(GTK_TYPE_HBOX, "SPFontSelector", &info, static_cast<GTypeFlags>(0));
     }
     return type;
 }
@@ -177,8 +184,10 @@ static void sp_font_selector_init(SPFontSelector *fsel)
         g_object_set_data (G_OBJECT(fsel), "family-treeview", fsel->family_treeview);
 
 
+        //TRANSLATORS: only translate "string" in "context|string".
+        // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
         /* Style frame */
-        f = gtk_frame_new(_("Style"));
+        f = gtk_frame_new(Q_("fontselector|Style"));
         gtk_widget_show(f);
         gtk_box_pack_start(GTK_BOX (fsel), f, TRUE, TRUE, 0);
 
@@ -309,17 +318,30 @@ static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
 
 static void sp_font_selector_size_changed( GtkComboBox */*cbox*/, SPFontSelector *fsel )
 {
-    char *sstr = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
+    char *text = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
     gfloat old_size = fsel->fontsize;
-    fsel->fontsize = MAX(atof(sstr), 0.1);
+
+    gchar *endptr;
+    gdouble value = -1;
+    if (text) {
+        value = g_strtod (text, &endptr);
+        if (endptr == text) // conversion failed, non-numeric input
+            value = -1;
+        free (text);
+    }
+    if (value <= 0) {
+        return; // could not parse value 
+    }
+    if (value > 10000)
+        value = 10000; // somewhat arbitrary, but text&font preview freezes with too huge fontsizes
+
+    fsel->fontsize = value;
     if ( fabs(fsel->fontsize-old_size) > 0.001)
     {
         fsel->fontsize_dirty = true;
     }
 
     sp_font_selector_emit_set (fsel);
-
-    free (sstr);
 }
 
 static void sp_font_selector_emit_set (SPFontSelector *fsel)
@@ -353,7 +375,7 @@ static void sp_font_selector_emit_set (SPFontSelector *fsel)
 
     if ((!family) || (!style)) return;
 
-    font = (font_factory::Default())->FaceFromDescr (family, style);
+    font = (font_factory::Default())->FaceFromUIStrings (family, style);
 
     // FIXME: when a text object uses non-available font, font==NULL and we can't set size
     // (and the size shown in the widget is invalid). To fix, here we must always get some
@@ -386,73 +408,103 @@ GtkWidget *sp_font_selector_new()
 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
 {
     if (font)
-    {
-            gchar family[256];
-            font->Family (family, 256);
-
-            Gtk::TreePath path;
+    {  
+        Gtk::TreePath path;
+        font_instance *tempFont = NULL;
+        
+        Glib::ustring family = font_factory::Default()->GetUIFamilyString(font->descr);
+
+        try {
+            path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
+        } catch (...) {
+            return;
+        }
 
-            try {
-                path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
-            } catch (...) {
-                return;
+        fsel->block_emit = TRUE;
+        gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
+        gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
+        fsel->block_emit = FALSE;
+
+        GList *list = 0;
+        GtkTreeIter iter;
+        GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
+        gtk_tree_model_get_iter (model, &iter, path.gobj());
+        gtk_tree_model_get (model, &iter, 1, &list, -1);
+
+        unsigned int currentStyleNumber = 0;
+        unsigned int bestStyleNumber = 0;
+        
+        PangoFontDescription *incomingFont = pango_font_description_copy(font->descr);
+        pango_font_description_unset_fields(incomingFont, PANGO_FONT_MASK_SIZE);
+        
+        char *incomingFontString = pango_font_description_to_string(incomingFont);
+        
+        tempFont = (font_factory::Default())->FaceFromUIStrings(family.c_str(), (char*)list->data);
+        
+        PangoFontDescription *bestMatchForFont = NULL;
+        if (tempFont) {
+            bestMatchForFont = pango_font_description_copy(tempFont->descr);
+            tempFont->Unref();
+            tempFont = NULL;
+        }
+        
+        pango_font_description_unset_fields(bestMatchForFont, PANGO_FONT_MASK_SIZE);
+        
+        list = list->next;
+        
+        while (list) {
+            currentStyleNumber++;
+            
+            tempFont = font_factory::Default()->FaceFromUIStrings(family.c_str(), (char*)list->data);
+            
+            PangoFontDescription *currentMatchForFont = NULL;
+            if (tempFont) {
+                currentMatchForFont = pango_font_description_copy(tempFont->descr);
+                tempFont->Unref();
+                tempFont = NULL;
             }
-
-            fsel->block_emit = TRUE;
-            gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
-            gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
-            fsel->block_emit = FALSE;
-
-            GList *list = 0;
-            GtkTreeIter iter;
-            GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
-            gtk_tree_model_get_iter (model, &iter, path.gobj());
-            gtk_tree_model_get (model, &iter, 1, &list, -1);
-
-            gchar descr[256];
-            font->Name(descr, 256);
-            std::string descr_best (family);
-            descr_best += " ";
-            descr_best += ((char*)list->data);
-
-            PangoFontDescription *descr_ = pango_font_description_from_string(descr);
-            PangoFontDescription *best_ = pango_font_description_from_string(descr_best.c_str());
-
-            unsigned int i = 0;
-            unsigned int best_i = 0;
-
-            // try to find best match with style description (i.e. bold, italic ?)
-            for (list = list->next ; list ; list = list->next)
-            {
-                i++;
-                std::string descr_try (family);
-                descr_try += " ";
-                descr_try += ((char*)list->data);
-                PangoFontDescription *try_ = pango_font_description_from_string(descr_try.c_str());
-                if (pango_font_description_better_match (descr_, best_, try_))
-                {
-                    pango_font_description_free (best_);
-                    best_ = pango_font_description_from_string (descr_try.c_str ());
-                    best_i = i;
+            
+            if (currentMatchForFont) {
+                pango_font_description_unset_fields(currentMatchForFont, PANGO_FONT_MASK_SIZE);
+                
+                char *currentMatchString = pango_font_description_to_string(currentMatchForFont);
+                
+                if (!strcmp(incomingFontString, currentMatchString)
+                        || pango_font_description_better_match(incomingFont, bestMatchForFont, currentMatchForFont)) {
+                    // Found a better match for the font we are looking for
+                    pango_font_description_free(bestMatchForFont);
+                    bestMatchForFont = pango_font_description_copy(currentMatchForFont);
+                    bestStyleNumber = currentStyleNumber;
                 }
-                pango_font_description_free(try_);
-            }
-            pango_font_description_free(descr_);
-            pango_font_description_free(best_);
-
-            GtkTreePath *path_c = gtk_tree_path_new ();
-            gtk_tree_path_append_index (path_c, best_i);
-            gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
-            gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
-
-            if (size != fsel->fontsize)
-            {
-                gchar s[8];
-                g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
-                gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
-                fsel->fontsize = size;
+                
+                g_free(currentMatchString);
+                
+                pango_font_description_free(currentMatchForFont);
             }
+            
+            list = list->next;
+        }
+        
+        if (bestMatchForFont)
+            pango_font_description_free(bestMatchForFont);
+        if (incomingFont)
+            pango_font_description_free(incomingFont);
+        g_free(incomingFontString);
+
+        GtkTreePath *path_c = gtk_tree_path_new ();
+        gtk_tree_path_append_index (path_c, bestStyleNumber);
+        gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
+        gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
+    
+        if (size != fsel->fontsize)
+        {
+            gchar s[8];
+            g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
+            gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
+            fsel->fontsize = size;
+        }
     }
+    
 }
 
 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
@@ -495,19 +547,23 @@ static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
 
 static GtkDrawingAreaClass *fp_parent_class = NULL;
 
-GtkType sp_font_preview_get_type()
+GType sp_font_preview_get_type()
 {
-    static GtkType type = 0;
+    static GType type = 0;
     if (!type) {
-        static const GtkTypeInfo info = {
-            "SPFontPreview",
-            sizeof (SPFontPreview),
-            sizeof (SPFontPreviewClass),
-            (GtkClassInitFunc) sp_font_preview_class_init,
-            (GtkObjectInitFunc) sp_font_preview_init,
-            NULL, NULL, NULL
+        GTypeInfo info = {
+            sizeof(SPFontPreviewClass),
+            0, // base_init
+            0, // base_finalize
+            (GClassInitFunc)sp_font_preview_class_init,
+            0, // class_finalize
+            0, // class_data
+            sizeof(SPFontPreview),
+            0, // n_preallocs
+            (GInstanceInitFunc)sp_font_preview_init,
+            0 // value_table
         };
-        type = gtk_type_unique (GTK_TYPE_DRAWING_AREA, &info);
+        type = g_type_register_static(GTK_TYPE_DRAWING_AREA, "SPFontPreview", &info, static_cast<GTypeFlags>(0));
     }
     return type;
 }
@@ -572,7 +628,7 @@ static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
 
             font_instance *tface = fprev->rfont->daddy;
 
-            double theSize = NR_MATRIX_DF_EXPANSION (&fprev->rfont->style.transform);
+            double theSize = fprev->rfont->style.transform.descrim();
 
             gchar const *p;
             if (fprev->phrase) {
@@ -610,19 +666,19 @@ static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
                                 pango_font_description_free(pfd);
                             }
                         }
-                        NR::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
+                        Geom::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
                         base_pt *= theSize;
 
                         glyphs[len] = str_text->glyph_text[i].gl;
                         hpos[len] = base_pt[0];
                         len++;
                         if ( curF ) {
-                            NR::Maybe<NR::Rect> nbbox = curF->BBox(str_text->glyph_text[i].gl);
+                            Geom::OptRect nbbox = curF->BBox(str_text->glyph_text[i].gl);
                             if (nbbox) {
-                                bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox->min())[0]);
-                                bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox->max())[1]);
-                                bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox->max())[0]);
-                                bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox->min())[1]);
+                                bbox.x0 = MIN(bbox.x0, base_pt[Geom::X] + theSize * (nbbox->min())[0]);
+                                bbox.y0 = MIN(bbox.y0, base_pt[Geom::Y] - theSize * (nbbox->max())[1]);
+                                bbox.x1 = MAX(bbox.x1, base_pt[Geom::X] + theSize * (nbbox->max())[0]);
+                                bbox.y1 = MAX(bbox.y1, base_pt[Geom::Y] - theSize * (nbbox->min())[1]);
                             }
                         }
                     }
@@ -639,13 +695,13 @@ static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
                                 unival = g_utf8_get_char (p);
                                 glyphs[len] =  tface->MapUnicodeChar( unival);
                                 hpos[len] = (int)px;
-                                NR::Point adv = fprev->rfont->Advance(glyphs[len]);
+                                Geom::Point adv = fprev->rfont->Advance(glyphs[len]);
                                 fprev->rfont->BBox( glyphs[len], &gbox);
                                 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
                                 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
                                 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
                                 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
-                                px += adv[NR::X];
+                                px += adv[Geom::X];
                                 len += 1;
                                 p = g_utf8_next_char (p);
                                 }*/
@@ -690,7 +746,7 @@ static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
                         }
                         raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
                         if ( g ) {
-                            g->Blit(NR::Point(hpos[i] + startx, starty), m);
+                            g->Blit(Geom::Point(hpos[i] + startx, starty), m);
                         }
                     }
                     if (curRF) {
@@ -751,8 +807,7 @@ void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontS
 
         if (fprev->font)
         {
-            NRMatrix flip;
-            nr_matrix_set_scale (&flip, fsel->fontsize, -fsel->fontsize);
+            Geom::Matrix flip(Geom::Scale(fsel->fontsize, -fsel->fontsize));
             fprev->rfont = fprev->font->RasterFont(flip, 0);
         }