diff --git a/src/belvuApp/belvuAlignment.cpp b/src/belvuApp/belvuAlignment.cpp index c29ddce3335de6629ed8dc7a543e6d708dcf2979..eb05eb92789bbfb2c6e9e41ffb4aa5f740fede34 100644 --- a/src/belvuApp/belvuAlignment.cpp +++ b/src/belvuApp/belvuAlignment.cpp @@ -177,7 +177,7 @@ void onBelvuAlignmentFontSizeChanged(GtkWidget *belvuAlignment) if (!properties || !properties->seqArea) return; - getFontCharSize(properties->seqArea, properties->seqArea->style->font_desc, &properties->charWidth, &properties->charHeight); + getFontCharSize(properties->seqArea, properties->seqArea->style->font_desc, &properties->charWidth, &properties->charHeight, TRUE); /* Update the column padding, which is based on the character width. We use * slightly different padding if the view is wrapped because we don't draw diff --git a/src/blixemApp/blxcontext.cpp b/src/blixemApp/blxcontext.cpp index 0563802bc390ed3db45535f4edda9d0eda3e742b..da4fbb0c2e50b3ed71a419bb669c3be042a26f37 100644 --- a/src/blixemApp/blxcontext.cpp +++ b/src/blixemApp/blxcontext.cpp @@ -299,7 +299,7 @@ BlxContext::BlxContext(CommandLineOptions *options, /* Calculate the font size */ if (widget_in) - getFontCharSize(widget_in, widget_in->style->font_desc, &m_charWidth, &m_charHeight); + getFontCharSize(widget_in, widget_in->style->font_desc, &m_charWidth, &m_charHeight, TRUE); } diff --git a/src/blixemApp/detailview.cpp b/src/blixemApp/detailview.cpp index b03cfc6d9aa0857f3a8d1e5f9856585139407bd0..1f8ac4514633043089e60617dc2406e0c359fade 100755 --- a/src/blixemApp/detailview.cpp +++ b/src/blixemApp/detailview.cpp @@ -3814,7 +3814,7 @@ static void updateCellRendererFont(GtkWidget *detailView, PangoFontDescription * { /* Calculate the row height from the font size */ gdouble charWidth, charHeight; - getFontCharSize(detailView, fontDesc, &charWidth, &charHeight); + getFontCharSize(detailView, fontDesc, &charWidth, &charHeight, TRUE); /* Cache these results, because we use them often for calculations */ DetailViewProperties *properties = detailViewGetProperties(detailView); diff --git a/src/dotterApp/dotter.cpp b/src/dotterApp/dotter.cpp index e9627245fa984c1e13b6eefa7464dda631963dc5..feff408e14970046f7defa145c299f5ca4f5ade7 100644 --- a/src/dotterApp/dotter.cpp +++ b/src/dotterApp/dotter.cpp @@ -571,7 +571,7 @@ static DotterContext* createDotterContext(DotterOptions *options, const char *fontFamily = findFixedWidthFont(tmp); result->fontDesc = pango_font_description_from_string(fontFamily); pango_font_description_set_size(result->fontDesc, pango_font_description_get_size(tmp->style->font_desc)); - getFontCharSize(tmp, result->fontDesc, &result->charWidth, &result->charHeight); + getFontCharSize(tmp, result->fontDesc, &result->charWidth, &result->charHeight, TRUE); gtk_widget_destroy(tmp); } else diff --git a/src/seqtoolsUtils/utilities.cpp b/src/seqtoolsUtils/utilities.cpp index 632187d819ca17326ad07c9db6df6fca8c03a2cf..cc3e13d9bd024bbc6877266c03a57e1aec84e898 100644 --- a/src/seqtoolsUtils/utilities.cpp +++ b/src/seqtoolsUtils/utilities.cpp @@ -3352,19 +3352,39 @@ const char* findFixedWidthFont(GtkWidget *widget) /* Utility to get the character width and height of a given pango font */ -void getFontCharSize(GtkWidget *widget, PangoFontDescription *fontDesc, gdouble *width, gdouble *height) +void getFontCharSize(GtkWidget *widget, + PangoFontDescription *fontDesc, + gdouble *width, + gdouble *height, + gboolean fixedWidthFont) { PangoContext *context = gtk_widget_get_pango_context(widget); PangoFontMetrics *metrics = pango_context_get_metrics(context, fontDesc, pango_context_get_language(context)); - - if (height) + + if (fixedWidthFont) { - *height = (gdouble)(pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)) / (gdouble)PANGO_SCALE; + /* All chars are the same width so create a layout containing one char and find its + * size. It is important that we get an accurate width for fixed-width fonts because it is + * used to calculate character positions (for non-fixed-width fonts the approximate method + * below is probably better to get a representative size). */ + PangoLayout *layout = gtk_widget_create_pango_layout(widget, "a"); + pango_layout_set_font_description(layout, widget->style->font_desc); + int width_i = 0, height_i = 0; + pango_layout_get_pixel_size(layout, &width_i, &height_i); + + if (width) + *width = (gdouble)width_i; + + if (height) + *height = (gdouble)height_i; } - - if (width) + else { - *width = (gdouble)pango_font_metrics_get_approximate_digit_width(metrics) / (gdouble)PANGO_SCALE; + if (height) + *height = (gdouble)(pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)) / (gdouble)PANGO_SCALE; + + if (width) + *width = (gdouble)pango_font_metrics_get_approximate_digit_width(metrics) / (gdouble)PANGO_SCALE; } pango_font_metrics_unref(metrics); diff --git a/src/seqtoolsUtils/utilities.hpp b/src/seqtoolsUtils/utilities.hpp index 104364072ed3a5e914456e6b8cdf3e71075f0af1..200cc0900db5a3279c477ec5c9bf81911ecba735 100644 --- a/src/seqtoolsUtils/utilities.hpp +++ b/src/seqtoolsUtils/utilities.hpp @@ -582,7 +582,7 @@ gchar* getSequenceSegment(const char* const dnaSequence, const char* findFixedWidthFont(GtkWidget *widget); const char* findFixedWidthFontFamily(GtkWidget *widget, GList *pref_families); -void getFontCharSize(GtkWidget *widget, PangoFontDescription *fontDesc, gdouble *width, gdouble *height); +void getFontCharSize(GtkWidget *widget, PangoFontDescription *fontDesc, gdouble *width, gdouble *height, const gboolean fixedWidthFont = FALSE); GtkWidget* createToolbarHandle(); GtkToolItem* addToolbarWidget(GtkToolbar *toolbar, GtkWidget *widget, const int position);