[dia] font: cleanup style
- From: Zander Brown <zbrown src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [dia] font: cleanup style
- Date: Wed, 22 Sep 2021 23:23:55 +0000 (UTC)
commit 6aaeec3cde7d07a2cbd9875602fcc4ab573d6642
Author: Zander Brown <zbrown gnome org>
Date: Wed Sep 22 01:51:25 2021 +0100
font: cleanup style
GObject macros are nice you know
Also move doc-y comments to be gtk-doc style comments
lib/dia_xml.c | 2 +-
lib/dia_xml.h | 2 +-
lib/diafontselector.h | 1 +
lib/diagramdata.h | 1 +
lib/diatypes.h | 4 -
lib/font.c | 611 ++++++++++++++++++++++++++--------------------
lib/font.h | 207 +++++-----------
lib/libdia.def | 3 -
lib/meson.build | 7 +-
lib/properties.h | 1 +
plug-ins/svg/render_svg.c | 2 +-
11 files changed, 422 insertions(+), 419 deletions(-)
---
diff --git a/lib/dia_xml.c b/lib/dia_xml.c
index e7f4be3f3..32aeafbc8 100644
--- a/lib/dia_xml.c
+++ b/lib/dia_xml.c
@@ -1375,7 +1375,7 @@ data_add_filename(DataNode data, const char *str, DiaContext *ctx)
* \ingroup DiagramXmlOut
*/
void
-data_add_font(AttributeNode attr, const DiaFont *font, DiaContext *ctx)
+data_add_font(AttributeNode attr, DiaFont *font, DiaContext *ctx)
{
DataNode data_node;
char buffer[20+1]; /* Enought for 64bit int + zero */
diff --git a/lib/dia_xml.h b/lib/dia_xml.h
index d624292d5..8922b8b67 100644
--- a/lib/dia_xml.h
+++ b/lib/dia_xml.h
@@ -96,7 +96,7 @@ void data_add_bezpoint(AttributeNode attr, const BezPoint *point, DiaContext *ct
void data_add_rectangle(AttributeNode attr, const DiaRectangle *rect, DiaContext *ctx);
void data_add_string(AttributeNode attr, const char *str, DiaContext *ctx);
void data_add_filename(AttributeNode attr, const char *str, DiaContext *ctx);
-void data_add_font(AttributeNode attr, const DiaFont *font, DiaContext *ctx);
+void data_add_font(AttributeNode attr, DiaFont *font, DiaContext *ctx);
DataNode data_add_composite(AttributeNode attr,
const char *type, /* can be NULL */
DiaContext *ctx);
diff --git a/lib/diafontselector.h b/lib/diafontselector.h
index 59437e64f..7bef86c51 100644
--- a/lib/diafontselector.h
+++ b/lib/diafontselector.h
@@ -20,6 +20,7 @@
#include "diatypes.h"
#include "dia-autoptr.h"
+#include "font.h"
#pragma once
diff --git a/lib/diagramdata.h b/lib/diagramdata.h
index bc18575ee..765e943e5 100644
--- a/lib/diagramdata.h
+++ b/lib/diagramdata.h
@@ -26,6 +26,7 @@
#include "color.h"
#include "geometry.h"
#include "paper.h"
+#include "font.h"
#include "diarenderer.h"
G_BEGIN_DECLS
diff --git a/lib/diatypes.h b/lib/diatypes.h
index c090de15f..d9d6d3f64 100644
--- a/lib/diatypes.h
+++ b/lib/diatypes.h
@@ -98,10 +98,6 @@ typedef struct _DiaCallbackFilter DiaCallbackFilter;
/* In focus.h: */
typedef struct _Focus Focus;
-/* In font.h: */
-typedef struct _DiaFont DiaFont;
-typedef struct _DiaFontClass DiaFontClass;
-
/* In geometry.h: */
typedef struct _Point Point;
typedef struct _DiaRectangle DiaRectangle;
diff --git a/lib/font.c b/lib/font.c
index afdda960d..49adc14fb 100644
--- a/lib/font.c
+++ b/lib/font.c
@@ -39,92 +39,53 @@
#include "intl.h"
#include "textline.h"
-static PangoContext* pango_context = NULL;
+static PangoContext *pango_context = NULL;
-/*!
- * \brief Opaque type to represent a font
- * \ingroup ObjectFonts
+/**
+ * DiaFont:
+ *
+ * Opaque type to represent a font
*/
-struct _DiaFont
-{
+struct _DiaFont {
GObject parent_instance;
- PangoFontDescription* pfd;
- /* mutable */ char* legacy_name;
+ PangoFontDescription *pfd;
+ /* mutable */ char *legacy_name;
/* there is a difference between Pango's font size and Dia's font height */
/* Calculated font_size is to make 'font_height = ascent + descent */
/* The font_height is used as default line height, there used to be a hard-coded size = 0.7 * height */
/* before using pango_set_absolute_size() to overcome font size differences between renderers */
- real height;
+ double height;
/* Need to load a font to query it's metrics */
PangoFont *loaded;
PangoFontMetrics *metrics;
};
+G_DEFINE_TYPE (DiaFont, dia_font, G_TYPE_OBJECT)
+
/* This is the global factor that says what zoom factor is 100%. It's
* normally 20.0 (and likely to stay that way). It is defined by how many
* pixels one cm is represented as.
*/
-static real global_zoom_factor = 20.0;
-
-
-static void
-dia_font_check_for_font (int font)
-{
- DiaFont *check;
- PangoFont *loaded;
- static double height = 1.0;
-
- check = dia_font_new_from_style (font, height);
- loaded = pango_context_load_font (dia_font_get_context (), check->pfd);
-
- if (!loaded) {
- message_error (_("Can't load font %s.\n"), dia_font_get_family (check));
- } else {
- g_clear_object (&loaded);
- }
-
- g_clear_object (&check);
-}
+static double global_zoom_factor = 20.0;
-void
-dia_font_init(PangoContext* pcontext)
-{
- pango_context = pcontext;
- /* We must have these three fonts! */
- dia_font_check_for_font(DIA_FONT_SANS);
- dia_font_check_for_font(DIA_FONT_SERIF);
- dia_font_check_for_font(DIA_FONT_MONOSPACE);
-}
-
-/* We might not need these anymore, when using FT2/Win32 fonts only */
-static GList *pango_contexts = NULL;
-
/*! DEPRECATED: there should be only one "measure context" */
-void
+static void
dia_font_push_context (PangoContext *pcontext)
{
- pango_contexts = g_list_prepend(pango_contexts, pango_context);
- pango_context = pcontext;
- pango_context_set_language (pango_context, gtk_get_default_language ());
- g_object_ref(pcontext);
-}
-
-
-/*! DEPRECATED: there should be only one "measure context" */
-void
-dia_font_pop_context (void)
-{
- g_clear_object (&pango_context);
- pango_context = (PangoContext*) pango_contexts->data;
+ g_set_object (&pango_context, pcontext);
pango_context_set_language (pango_context, gtk_get_default_language ());
- pango_contexts = g_list_next (pango_contexts);
}
+/**
+ * dia_font_get_context:
+ *
+ * Retrieve the current context (used for the font widget)
+ */
PangoContext *
dia_font_get_context (void)
{
@@ -160,65 +121,61 @@ dia_font_get_context (void)
return pango_context;
}
- /* dia centimetres to pango device units */
-static gint
-dcm_to_pdu(real dcm) { return dcm * global_zoom_factor * PANGO_SCALE; }
- /* pango device units to dia centimetres */
-static real
-pdu_to_dcm(gint pdu) { return (real)pdu / (global_zoom_factor * PANGO_SCALE); }
-
-static void dia_font_class_init(DiaFontClass* class);
-static void dia_font_finalize(GObject* object);
-static void dia_font_init_instance(DiaFont*);
-
-GType
-dia_font_get_type (void)
-{
- static GType object_type = 0;
-
- if (!object_type) {
- static const GTypeInfo object_info =
- {
- sizeof (DiaFontClass),
- (GBaseInitFunc) NULL,
- (GBaseFinalizeFunc) NULL,
- (GClassInitFunc) dia_font_class_init, /* class_init */
- NULL, /* class_finalize */
- NULL, /* class_data */
- sizeof (DiaFont),
- 0, /* n_preallocs */
- (GInstanceInitFunc)dia_font_init_instance
- };
- object_type = g_type_register_static (G_TYPE_OBJECT,
- "DiaFont",
- &object_info, 0);
- }
- return object_type;
+
+/* dia centimetres to pango device units */
+static int
+dcm_to_pdu (double dcm)
+{
+ return dcm * global_zoom_factor * PANGO_SCALE;
+}
+
+
+/* pango device units to dia centimetres */
+static double
+pdu_to_dcm (int pdu)
+{
+ return (double) pdu / (global_zoom_factor * PANGO_SCALE);
}
-static gpointer parent_class;
+
static void
-dia_font_class_init(DiaFontClass* klass)
+dia_font_finalize (GObject *object)
+{
+ DiaFont *font = DIA_FONT (object);
+
+ g_clear_pointer (&font->pfd, pango_font_description_free);
+ g_clear_pointer (&font->metrics, pango_font_metrics_unref);
+
+ g_clear_object (&font->loaded);
+
+ G_OBJECT_CLASS (dia_font_parent_class)->finalize (object);
+}
+
+
+static void
+dia_font_class_init (DiaFontClass* klass)
{
GObjectClass* object_class = G_OBJECT_CLASS(klass);
- parent_class = g_type_class_peek_parent(klass);
+
object_class->finalize = dia_font_finalize;
}
+
static void
-dia_font_init_instance(DiaFont* font)
+dia_font_init (DiaFont *font)
{
- /*GObject *gobject = G_OBJECT(font); */
}
+
static void
-dia_pfd_set_height(PangoFontDescription* pfd, real height)
+dia_pfd_set_height(PangoFontDescription *pfd, double height)
{
/* ONLY place for the magic factor! */
g_return_if_fail (height > 0.0);
- pango_font_description_set_absolute_size(pfd, dcm_to_pdu(height) * 0.8);
+ pango_font_description_set_absolute_size (pfd, dcm_to_pdu (height) * 0.8);
}
+
/*!
* In Dia a font is usually referred to by it's (line-) height, not it's size.
*
@@ -227,7 +184,7 @@ dia_pfd_set_height(PangoFontDescription* pfd, real height)
* really calculating the font size from the height would involve two font loads which seem to be two
expensive.
*/
static void
-_dia_font_adjust_size (DiaFont *font, real height, gboolean recalc_alwways)
+_dia_font_adjust_size (DiaFont *font, double height, gboolean recalc_alwways)
{
if (font->height != height || !font->metrics || recalc_alwways) {
@@ -240,25 +197,32 @@ _dia_font_adjust_size (DiaFont *font, real height, gboolean recalc_alwways)
g_clear_object (&loaded);
- if (font->metrics)
- pango_font_metrics_unref (font->metrics);
+ g_clear_pointer (&font->metrics, pango_font_metrics_unref);
/* caching metrics */
font->metrics = pango_font_get_metrics (font->loaded, NULL);
font->height = height;
}
}
-DiaFont*
-dia_font_new(const char *family, DiaFontStyle style, real height)
+
+
+/**
+ * dia_font_new:
+ *
+ * Get a font matching family,style,height.
+ */
+DiaFont *
+dia_font_new (const char *family, DiaFontStyle style, double height)
{
DiaFont* font = dia_font_new_from_style(style, height);
gboolean changed;
- changed = family != NULL && strcmp (pango_font_description_get_family(font->pfd), family) != 0;
+ changed = family != NULL && g_strcmp0 (pango_font_description_get_family (font->pfd), family) != 0;
pango_font_description_set_family(font->pfd, family);
- if (changed)
+ if (changed) {
_dia_font_adjust_size (font, font->height, TRUE);
+ }
return font;
}
@@ -287,7 +251,7 @@ dia_pfd_set_family (PangoFontDescription* pfd, DiaFontFamily fam)
static void
-dia_pfd_set_weight(PangoFontDescription* pfd, DiaFontWeight fw)
+dia_pfd_set_weight (PangoFontDescription* pfd, DiaFontWeight fw)
{
switch (fw) {
case DIA_FONT_ULTRALIGHT :
@@ -320,8 +284,9 @@ dia_pfd_set_weight(PangoFontDescription* pfd, DiaFontWeight fw)
}
}
+
static void
-dia_pfd_set_slant(PangoFontDescription* pfd, DiaFontSlant fo)
+dia_pfd_set_slant (PangoFontDescription* pfd, DiaFontSlant fo)
{
switch (fo) {
case DIA_FONT_NORMAL :
@@ -338,57 +303,56 @@ dia_pfd_set_slant(PangoFontDescription* pfd, DiaFontSlant fo)
}
}
-DiaFont*
-dia_font_new_from_style(DiaFontStyle style, real height)
+
+/**
+ * dia_font_new_from_style:
+ *
+ * Get a font matching style. This is the preferred method to
+ * create default fonts within objects.
+ */
+DiaFont *
+dia_font_new_from_style (DiaFontStyle style, double height)
{
- DiaFont* retval;
+ DiaFont *retval;
/* in the future we could establish Dia's own default font
* matching to be as (font-)system independent as possible.
* For now fall back to Pangos configuration --hb
*/
- PangoFontDescription* pfd = pango_font_description_new();
- dia_pfd_set_family(pfd,DIA_FONT_STYLE_GET_FAMILY(style));
- dia_pfd_set_weight(pfd,DIA_FONT_STYLE_GET_WEIGHT(style));
- dia_pfd_set_slant(pfd,DIA_FONT_STYLE_GET_SLANT(style));
- dia_pfd_set_height(pfd,height);
+ PangoFontDescription *pfd = pango_font_description_new ();
+ dia_pfd_set_family (pfd, DIA_FONT_STYLE_GET_FAMILY (style));
+ dia_pfd_set_weight (pfd, DIA_FONT_STYLE_GET_WEIGHT (style));
+ dia_pfd_set_slant (pfd, DIA_FONT_STYLE_GET_SLANT (style));
+ dia_pfd_set_height (pfd, height);
- retval = DIA_FONT(g_object_new(DIA_TYPE_FONT, NULL));
+ retval = g_object_new (DIA_TYPE_FONT, NULL);
retval->pfd = pfd;
_dia_font_adjust_size (retval, height, FALSE);
retval->legacy_name = NULL;
- return retval;
-}
-DiaFont* dia_font_copy(const DiaFont* font)
-{
- if (!font)
- return NULL;
- return dia_font_new(dia_font_get_family(font),
- dia_font_get_style(font),
- dia_font_get_height(font));
+ return retval;
}
-void
-dia_font_finalize(GObject *object)
+DiaFont *
+dia_font_copy (DiaFont *font)
{
- DiaFont *font = DIA_FONT (object);
-
- if (font->pfd)
- pango_font_description_free (font->pfd);
- font->pfd = NULL;
- if (font->metrics)
- pango_font_metrics_unref (font->metrics);
- font->metrics = NULL;
-
- g_clear_object (&font->loaded);
+ if (!font) {
+ return NULL;
+ }
- G_OBJECT_CLASS (parent_class)->finalize (object);
+ return dia_font_new (dia_font_get_family (font),
+ dia_font_get_style (font),
+ dia_font_get_height (font));
}
+/**
+ * dia_font_get_style:
+ *
+ * Retrieves the style of the font
+ */
DiaFontStyle
-dia_font_get_style(const DiaFont* font)
+dia_font_get_style (DiaFont *font)
{
guint style;
@@ -402,20 +366,27 @@ dia_font_get_style(const DiaFont* font)
PangoStyle pango_style = pango_font_description_get_style(font->pfd);
PangoWeight pango_weight = pango_font_description_get_weight(font->pfd);
- g_assert(PANGO_WEIGHT_ULTRALIGHT <= pango_weight && pango_weight <= PANGO_WEIGHT_HEAVY);
- g_assert(PANGO_WEIGHT_ULTRALIGHT == 200);
- g_assert(PANGO_WEIGHT_NORMAL == 400);
- g_assert(PANGO_WEIGHT_BOLD == 700);
+ g_return_val_if_fail (PANGO_WEIGHT_ULTRALIGHT <= pango_weight &&
+ pango_weight <= PANGO_WEIGHT_HEAVY,
+ DIA_FONT_NORMAL);
+ g_return_val_if_fail (PANGO_WEIGHT_ULTRALIGHT == 200, DIA_FONT_NORMAL);
+ g_return_val_if_fail (PANGO_WEIGHT_NORMAL == 400, DIA_FONT_NORMAL);
+ g_return_val_if_fail (PANGO_WEIGHT_BOLD == 700, DIA_FONT_NORMAL);
- style = weight_map[(pango_weight - PANGO_WEIGHT_ULTRALIGHT) / 100];
+ style = weight_map[(pango_weight - PANGO_WEIGHT_ULTRALIGHT) / 100];
style |= (pango_style << 2);
return style;
}
+/**
+ * dia_font_get_family:
+ *
+ * Retrieves the family of the font. Caller must NOT free.
+ */
const char *
-dia_font_get_family (const DiaFont *font)
+dia_font_get_family (DiaFont *font)
{
g_return_val_if_fail (font != NULL, NULL);
@@ -423,8 +394,13 @@ dia_font_get_family (const DiaFont *font)
}
+/**
+ * dia_font_get_description:
+ *
+ * Acessor for the PangoFontDescription
+ */
const PangoFontDescription *
-dia_font_get_description (const DiaFont *font)
+dia_font_get_description (DiaFont *font)
{
g_return_val_if_fail (font != NULL, NULL);
@@ -432,8 +408,13 @@ dia_font_get_description (const DiaFont *font)
}
+/**
+ * dia_font_get_height:
+ *
+ * Retrieves the height of the font
+ */
double
-dia_font_get_height (const DiaFont *font)
+dia_font_get_height (DiaFont *font)
{
g_return_val_if_fail (font != NULL, 0.0);
@@ -441,8 +422,13 @@ dia_font_get_height (const DiaFont *font)
}
+/**
+ * dia_font_get_size:
+ *
+ * Delivers the size of the font
+ */
double
-dia_font_get_size (const DiaFont *font)
+dia_font_get_size (DiaFont *font)
{
g_return_val_if_fail (font != NULL, 0.0);
@@ -454,54 +440,83 @@ dia_font_get_size (const DiaFont *font)
}
+/**
+ * dia_font_set_height:
+ *
+ * Change the height inside a font record.
+ */
void
-dia_font_set_height(DiaFont* font, real height)
+dia_font_set_height (DiaFont* font, double height)
{
_dia_font_adjust_size (font, height, FALSE);
}
-const char*
-dia_font_get_psfontname(const DiaFont *font)
+/**
+ * dia_font_get_psfontname:
+ *
+ * FIXME: what do we do with this, actually ?
+ *
+ * Name lives for as long as the DiaFont lives.
+ */
+const char *
+dia_font_get_psfontname (DiaFont *font)
{
/* This hack corrects a couple fonts that were misnamed in
* earlier versions of Dia. See bug #477079.
*/
- const char *fontname = dia_font_get_legacy_name(font);
+ const char *fontname = dia_font_get_legacy_name (font);
- if (!fontname)
+ if (!fontname) {
return NULL;
+ }
- if (strcmp(fontname, "NewCenturySchoolbook-Roman") == 0)
+ if (g_strcmp0 (fontname, "NewCenturySchoolbook-Roman") == 0) {
return "NewCenturySchlbk-Roman";
- else if (strcmp(fontname, "NewCenturySchoolbook-Italic") == 0)
+ } else if (g_strcmp0 (fontname, "NewCenturySchoolbook-Italic") == 0) {
return "NewCenturySchlbk-Italic";
- else if (strcmp(fontname, "NewCenturySchoolbook-Bold") == 0)
+ } else if (g_strcmp0 (fontname, "NewCenturySchoolbook-Bold") == 0) {
return "NewCenturySchlbk-Bold";
- else if (strcmp(fontname, "NewCenturySchoolbook-BoldItalic") == 0)
+ } else if (g_strcmp0 (fontname, "NewCenturySchoolbook-BoldItalic") == 0) {
return "NewCenturySchlbk-BoldItalic";
+ }
return fontname;
}
+
+/**
+ * dia_font_set_any_family:
+ *
+ * Changes the family of an existing font to any family
+ *
+ * The name is system configuration dependent, but font files are portable nowadays.
+ */
void
-dia_font_set_any_family(DiaFont* font, const char* family)
+dia_font_set_any_family (DiaFont *font, const char *family)
{
gboolean changed;
- g_return_if_fail(font != NULL);
+ g_return_if_fail (font != NULL);
- changed = strcmp (pango_font_description_get_family(font->pfd), family) != 0;
- pango_font_description_set_family(font->pfd, family);
- if (changed) /* force recalculation on name change */
+ changed = g_strcmp0 (pango_font_description_get_family (font->pfd), family) != 0;
+ pango_font_description_set_family (font->pfd, family);
+ if (changed) {
+ /* force recalculation on name change */
_dia_font_adjust_size (font, font->height, TRUE);
+ }
g_clear_pointer (&font->legacy_name, g_free);
}
+/**
+ * dia_font_set_family:
+ *
+ * Changes the family of an existing font to one of the three standard families
+ */
void
-dia_font_set_family (DiaFont *font, DiaFontFamily family)
+dia_font_set_family (DiaFont *font, DiaFontFamily family)
{
g_return_if_fail (font != NULL);
@@ -511,18 +526,33 @@ dia_font_set_family (DiaFont *font, DiaFontFamily family)
}
+/**
+ * dia_font_set_weight:
+ *
+ * Changes the weight of an existing font
+ */
void
-dia_font_set_weight(DiaFont* font, DiaFontWeight weight)
+dia_font_set_weight (DiaFont *font, DiaFontWeight weight)
{
- DiaFontWeight old_weight = DIA_FONT_STYLE_GET_WEIGHT(dia_font_get_style(font));
- g_return_if_fail(font != NULL);
+ DiaFontWeight old_weight = DIA_FONT_STYLE_GET_WEIGHT (dia_font_get_style (font));
+
+ g_return_if_fail (font != NULL);
+
dia_pfd_set_weight(font->pfd,weight);
- if (old_weight != weight)
+
+ if (old_weight != weight) {
_dia_font_adjust_size (font, font->height, TRUE);
+ }
}
+
+/**
+ * dia_font_set_slant:
+ *
+ * Changes the slant of an existing font
+ */
void
-dia_font_set_slant(DiaFont* font, DiaFontSlant slant)
+dia_font_set_slant (DiaFont *font, DiaFontSlant slant)
{
DiaFontSlant old_slant = DIA_FONT_STYLE_GET_SLANT(dia_font_get_style(font));
g_return_if_fail(font != NULL);
@@ -537,44 +567,59 @@ typedef struct _WeightName {
const char *name;
} WeightName;
static const WeightName weight_names[] = {
- {DIA_FONT_ULTRALIGHT, "200"},
- {DIA_FONT_LIGHT,"300"},
- {DIA_FONT_WEIGHT_NORMAL,"normal"},
- {DIA_FONT_WEIGHT_NORMAL,"400"},
- {DIA_FONT_MEDIUM, "500"},
- {DIA_FONT_DEMIBOLD, "600"},
- {DIA_FONT_BOLD, "700"},
- {DIA_FONT_ULTRABOLD, "800"},
- {DIA_FONT_HEAVY, "900"},
- {0,NULL}
+ { DIA_FONT_ULTRALIGHT, "200"},
+ { DIA_FONT_LIGHT,"300"},
+ { DIA_FONT_WEIGHT_NORMAL,"normal"},
+ { DIA_FONT_WEIGHT_NORMAL,"400"},
+ { DIA_FONT_MEDIUM, "500"},
+ { DIA_FONT_DEMIBOLD, "600"},
+ { DIA_FONT_BOLD, "700"},
+ { DIA_FONT_ULTRABOLD, "800"},
+ { DIA_FONT_HEAVY, "900"},
+ { 0, NULL}
};
+
+/**
+ * dia_font_get_weight_string:
+ *
+ * returns a static string suitable for SVG
+ */
const char *
-dia_font_get_weight_string(const DiaFont* font)
+dia_font_get_weight_string (DiaFont *font)
{
- const WeightName* p;
- DiaFontWeight fw = DIA_FONT_STYLE_GET_WEIGHT(dia_font_get_style(font));
+ const WeightName *p;
+ DiaFontWeight fw = DIA_FONT_STYLE_GET_WEIGHT (dia_font_get_style (font));
- for (p = weight_names; p->name != NULL; ++p) {
- if (p->fw == fw) return p->name;
+ for (p = weight_names; p->name != NULL; ++p) {
+ if (p->fw == fw) {
+ return p->name;
}
- return "normal";
+ }
+
+ return "normal";
}
+
+/**
+ * dia_font_set_weight_from_string:
+ *
+ * uses an SVG style string
+ */
void
-dia_font_set_weight_from_string(DiaFont* font, const char* weight)
+dia_font_set_weight_from_string (DiaFont *font, const char *weight)
{
- DiaFontWeight fw = DIA_FONT_WEIGHT_NORMAL;
- const WeightName* p;
+ DiaFontWeight fw = DIA_FONT_WEIGHT_NORMAL;
+ const WeightName *p;
- for (p = weight_names; p->name != NULL; ++p) {
- if (0 == strncmp(weight,p->name,8)) {
- fw = p->fw;
- break;
- }
+ for (p = weight_names; p->name != NULL; ++p) {
+ if (0 == strncmp (weight, p->name, 8)) {
+ fw = p->fw;
+ break;
}
+ }
- dia_font_set_weight(font,fw);
+ dia_font_set_weight (font, fw);
}
@@ -589,33 +634,47 @@ static const SlantName slant_names[] = {
{ 0, NULL}
};
+
+/**
+ * dia_font_get_slant_string:
+ *
+ * returns a static string suitable for SVG
+ */
const char *
-dia_font_get_slant_string(const DiaFont* font)
+dia_font_get_slant_string (DiaFont *font)
{
- const SlantName* p;
- DiaFontSlant fo =
- DIA_FONT_STYLE_GET_SLANT(dia_font_get_style(font));
+ const SlantName *p;
+ DiaFontSlant fo = DIA_FONT_STYLE_GET_SLANT (dia_font_get_style (font));
for (p = slant_names; p->name != NULL; ++p) {
- if (p->fo == fo)
+ if (p->fo == fo) {
return p->name;
+ }
}
+
return "normal";
}
+
+/**
+ * dia_font_set_slant_from_string:
+ *
+ * uses an SVG style string
+ */
void
-dia_font_set_slant_from_string(DiaFont* font, const char* obli)
+dia_font_set_slant_from_string (DiaFont* font, const char *obli)
{
DiaFontSlant fo = DIA_FONT_NORMAL;
- const SlantName* p;
+ const SlantName *p;
for (p = slant_names; p->name != NULL; ++p) {
- if (0 == strncmp(obli,p->name,8)) {
+ if (0 == strncmp (obli, p->name, 8)) {
fo = p->fo;
break;
}
}
- dia_font_set_slant(font,fo);
+
+ dia_font_set_slant (font, fo);
}
@@ -623,83 +682,107 @@ dia_font_set_slant_from_string(DiaFont* font, const char* obli)
/* Non-scaled versions of the utility routines */
/* ************************************************************************ */
-real
-dia_font_string_width(const char* string, DiaFont *font, real height)
+/**
+ * dia_font_string_width:
+ *
+ * Get the width of the string with the given font in cm
+ */
+double
+dia_font_string_width (const char *string, DiaFont *font, double height)
{
- real result = 0;
+ double result = 0;
+
if (string && *string) {
- TextLine *text_line = text_line_new(string, font, height);
- result = text_line_get_width(text_line);
- text_line_destroy(text_line);
+ TextLine *text_line = text_line_new (string, font, height);
+ result = text_line_get_width (text_line);
+ text_line_destroy (text_line);
}
+
return result;
}
-real
-dia_font_ascent(const char* string, DiaFont* font, real height)
+
+/**
+ * dia_font_ascent:
+ *
+ * Get the ascent of this string in cm (a positive number).
+ */
+double
+dia_font_ascent (const char *string, DiaFont *font, double height)
{
if (font->metrics) {
- real ascent = pdu_to_dcm(pango_font_metrics_get_ascent (font->metrics));
+ double ascent = pdu_to_dcm (pango_font_metrics_get_ascent (font->metrics));
return ascent * (height / font->height);
} else {
/* previous, _expensive_ but string specific way */
- TextLine *text_line = text_line_new(string, font, height);
- real result = text_line_get_ascent(text_line);
- text_line_destroy(text_line);
+ TextLine *text_line = text_line_new (string, font, height);
+ double result = text_line_get_ascent (text_line);
+ text_line_destroy (text_line);
return result;
}
}
-real
-dia_font_descent(const char* string, DiaFont* font, real height)
+
+/**
+ * dia_font_descent:
+ *
+ * Get the descent of the font in cm (a positive number)
+ */
+double
+dia_font_descent (const char *string, DiaFont *font, double height)
{
if (font->metrics) {
- real descent = pdu_to_dcm(pango_font_metrics_get_descent (font->metrics));
+ double descent = pdu_to_dcm (pango_font_metrics_get_descent (font->metrics));
return descent * (height / font->height);
} else {
/* previous, _expensive_ but string specific way */
- TextLine *text_line = text_line_new(string, font, height);
- real result = text_line_get_descent(text_line);
- text_line_destroy(text_line);
+ TextLine *text_line = text_line_new (string, font, height);
+ double result = text_line_get_descent (text_line);
+ text_line_destroy (text_line);
return result;
}
}
-PangoLayout*
-dia_font_build_layout(const char* string, DiaFont* font, real height)
+/**
+ * dia_font_build_layout:
+ *
+ * prepares a layout of the text, in font 'font'.
+ */
+PangoLayout *
+dia_font_build_layout (const char *string, DiaFont *font, double height)
{
- PangoLayout* layout;
- PangoAttrList* list;
- PangoAttribute* attr;
+ PangoLayout *layout;
+ PangoAttrList *list;
+ PangoAttribute *attr;
guint length;
PangoFontDescription *pfd;
- real factor;
+ double factor;
- layout = pango_layout_new(dia_font_get_context());
+ layout = pango_layout_new (dia_font_get_context ());
- length = string ? strlen(string) : 0;
- pango_layout_set_text(layout, string, length);
+ length = string ? strlen (string) : 0;
+ pango_layout_set_text (layout, string, length);
- list = pango_attr_list_new();
+ list = pango_attr_list_new ();
pfd = pango_font_description_copy (font->pfd);
/* account for difference between size and height as well as between font height and given one */
- factor = dia_font_get_size(font) / dia_font_get_height (font);
+ factor = dia_font_get_size (font) / dia_font_get_height (font);
pango_font_description_set_absolute_size (pfd, dcm_to_pdu (height) * factor);
- attr = pango_attr_font_desc_new(pfd);
+ attr = pango_attr_font_desc_new (pfd);
pango_font_description_free (pfd);
attr->start_index = 0;
attr->end_index = length;
- pango_attr_list_insert(list,attr); /* eats attr */
+ pango_attr_list_insert (list, attr); /* eats attr */
- pango_layout_set_attributes(layout,list);
- pango_attr_list_unref(list);
+ pango_layout_set_attributes (layout, list);
+ pango_attr_list_unref (list);
- pango_layout_set_indent(layout,0);
- pango_layout_set_justify(layout,FALSE);
- pango_layout_set_alignment(layout,PANGO_ALIGN_LEFT);
+ pango_layout_set_indent (layout,0);
+ pango_layout_set_justify (layout, FALSE);
+ pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
return layout;
}
@@ -717,12 +800,12 @@ dia_font_build_layout(const char* string, DiaFont* font, real height)
* This currently assumes only one run per iter, which is all we can input.
*/
static void
-get_string_offsets(PangoLayoutIter *iter, real** offsets, int* n_offsets)
+get_string_offsets(PangoLayoutIter *iter, double **offsets, int *n_offsets)
{
int i;
- PangoLayoutLine* line = pango_layout_iter_get_line(iter);
- PangoGlyphItem* item;
- PangoGlyphString* string;
+ PangoLayoutLine *line = pango_layout_iter_get_line (iter);
+ PangoGlyphItem *item;
+ PangoGlyphString *string;
if(0 == line->length)
{
@@ -733,7 +816,7 @@ get_string_offsets(PangoLayoutIter *iter, real** offsets, int* n_offsets)
string = item->glyphs;
*n_offsets = string->num_glyphs;
- *offsets = g_new(real, *n_offsets);
+ *offsets = g_new (double, *n_offsets);
for (i = 0; i < string->num_glyphs; i++) {
PangoGlyphGeometry geom = string->glyphs[i].geometry;
@@ -816,10 +899,10 @@ dia_font_get_sizes (const char *string,
{
PangoLayout* layout;
PangoLayoutIter* iter;
- real top, bline, bottom;
- const gchar* non_empty_string;
+ double top, bline, bottom;
+ const char *non_empty_string;
PangoRectangle ink_rect,logical_rect;
- real* offsets = NULL; /* avoid: 'offsets' may be used uninitialized in this function */
+ double *offsets = NULL; /* avoid: 'offsets' may be used uninitialized in this function */
/* We need some reasonable ascent/descent values even for empty strings. */
if (string == NULL || string[0] == '\0') {
@@ -889,9 +972,9 @@ dia_font_get_sizes (const char *string,
* Some additional fairly standard fonts for asian scripts are also added.
*/
static struct _legacy_font {
- gchar* oldname;
- gchar* newname;
- DiaFontStyle style; /* the DIA_FONT_FAMILY() is used as falback only */
+ char *oldname;
+ char *newname;
+ DiaFontStyle style; /* the DIA_FONT_FAMILY() is used as falback only */
} legacy_fonts[] = {
{ "AvantGarde-Book", "AvantGarde", DIA_FONT_SERIF },
{ "AvantGarde-BookOblique", "AvantGarde", DIA_FONT_SERIF | DIA_FONT_OBLIQUE },
@@ -964,13 +1047,13 @@ static struct _legacy_font {
*
* Since: dawn-of-time
*/
-DiaFont*
+DiaFont *
dia_font_new_from_legacy_name (const char* name)
{
/* do NOT translate anything here !!! */
DiaFont *retval = NULL;
struct _legacy_font* found = NULL;
- real height = 1.0;
+ double height = 1.0;
int i;
for (i = 0; i < G_N_ELEMENTS (legacy_fonts); i++) {
@@ -991,8 +1074,16 @@ dia_font_new_from_legacy_name (const char* name)
return retval;
}
-const char*
-dia_font_get_legacy_name(const DiaFont *font)
+
+/**
+ * dia_font_get_legacy_name:
+ *
+ * Get a simple font name from a font.
+ *
+ * Name will be valid for the duration of the DiaFont* lifetime.
+ */
+const char *
+dia_font_get_legacy_name (DiaFont *font)
{
const char* matched_name = NULL;
const char* family;
@@ -1000,24 +1091,26 @@ dia_font_get_legacy_name(const DiaFont *font)
int i;
/* if we have loaded it from an old file, use the old name */
- if (font->legacy_name)
+ if (font->legacy_name) {
return font->legacy_name;
+ }
family = dia_font_get_family (font);
style = dia_font_get_style (font);
- for (i = 0; i < G_N_ELEMENTS(legacy_fonts); i++) {
+ for (i = 0; i < G_N_ELEMENTS (legacy_fonts); i++) {
if (0 == g_ascii_strcasecmp (legacy_fonts[i].newname, family)) {
/* match weight and slant */
DiaFontStyle st = legacy_fonts[i].style;
- if ((DIA_FONT_STYLE_GET_SLANT(style) | DIA_FONT_STYLE_GET_WEIGHT(style))
- == (DIA_FONT_STYLE_GET_SLANT(st) | DIA_FONT_STYLE_GET_WEIGHT(st))) {
+ if ((DIA_FONT_STYLE_GET_SLANT (style) | DIA_FONT_STYLE_GET_WEIGHT (style))
+ == (DIA_FONT_STYLE_GET_SLANT (st) | DIA_FONT_STYLE_GET_WEIGHT (st))) {
return legacy_fonts[i].oldname; /* exact match */
- } else if (0 == (DIA_FONT_STYLE_GET_SLANT(st) | DIA_FONT_STYLE_GET_WEIGHT(st))) {
+ } else if (0 == (DIA_FONT_STYLE_GET_SLANT (st) | DIA_FONT_STYLE_GET_WEIGHT (st))) {
matched_name = legacy_fonts[i].oldname;
/* 'unmodified' font, continue matching */
}
}
}
+
return matched_name ? matched_name : "Courier";
}
diff --git a/lib/font.h b/lib/font.h
index 2000629db..aaf19ef6d 100644
--- a/lib/font.h
+++ b/lib/font.h
@@ -26,15 +26,6 @@
#include "dia-enums.h"
#include "geometry.h"
-/*!
- \file font.h -- services based on font definitions
- */
-/*!
- \defgroup ObjectFonts Dia's font definiton
- \brief Dia's font system based on http://www.pango.org
- \ingroup ObjectParts
- */
-
G_BEGIN_DECLS
/* Do NOT put these strings through the .po mechanism. If you need to add
@@ -63,23 +54,20 @@ G_BEGIN_DECLS
*/
typedef guint DiaFontStyle;
-typedef enum
-{
+typedef enum /*< flags >*/ {
DIA_FONT_FAMILY_ANY = 0,
DIA_FONT_SANS = 1,
DIA_FONT_SERIF = 2,
DIA_FONT_MONOSPACE = 3
} DiaFontFamily;
-typedef enum
-{
+typedef enum /*< flags >*/ {
DIA_FONT_NORMAL = (0<<2),
DIA_FONT_OBLIQUE = (1<<2),
DIA_FONT_ITALIC = (2<<2)
} DiaFontSlant;
-typedef enum
-{
+typedef enum /*< flags >*/ {
DIA_FONT_ULTRALIGHT = (1<<4),
DIA_FONT_LIGHT = (2<<4),
DIA_FONT_WEIGHT_NORMAL = (0<<4), /* Deliberately 0 for DIA_FONT_NORMAL */
@@ -95,145 +83,66 @@ typedef enum
#define DIA_FONT_STYLE_GET_SLANT(st) ((st) & (0x3<<2))
#define DIA_FONT_STYLE_GET_WEIGHT(st) ((st) & (0x7<<4))
-GType dia_font_get_type(void) G_GNUC_CONST;
-
-#define DIA_TYPE_FONT (dia_font_get_type())
-
-#define DIA_FONT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), \
- DIA_TYPE_FONT, \
- DiaFont))
-#define DIA_FONT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), \
- DIA_TYPE_FONT, \
- DiaFontClass))
-
-struct _DiaFontClass {
- GObjectClass parent_class;
-};
-
-/*! Set the PangoContext used to render text.
- * \ingroup ObjectFonts
- */
-void dia_font_init(PangoContext* pcontext);
-/* Start using a new context (for AA rendering) */
-void dia_font_push_context(PangoContext *pcontext);
-/* Go back to using the old context */
-void dia_font_pop_context(void);
-/* Retrieve the current context (used for the font widget) */
-PangoContext *dia_font_get_context(void);
-
-/*!
- * \brief Font creation for object implementation
- * \ingroup ObjectFonts
- * Get a font matching family,style,height.
- */
-DiaFont* dia_font_new(const char *family, DiaFontStyle style,
- real height);
-
-/*!
- * \brief Font creation for object implementation
- * Get a font matching style. This is the preferred method to
- * create default fonts within objects.
- * \ingroup ObjectFonts
- */
-DiaFont* dia_font_new_from_style(DiaFontStyle style, real height);
-
-/*!
- * \brief Font creation for object implementation
- * Get a font from a legacy font name
- * \ingroup ObjectFonts
- */
-DiaFont* dia_font_new_from_legacy_name(const char *name);
-
-/*!
- * \brief Font creation for object implementation
- * Get a simple font name from a font.
- * Name will be valid for the duration of the DiaFont* lifetime.
- * \ingroup ObjectFonts
- */
-const char* dia_font_get_legacy_name(const DiaFont* font);
-
- /* Same attributes */
-DiaFont *dia_font_copy(const DiaFont* font);
-
- /* Retrieves the style of the font */
-DiaFontStyle dia_font_get_style(const DiaFont* font);
-
- /* Retrieves the family of the font. Caller must NOT free. */
-const char* dia_font_get_family(const DiaFont* font);
-/* Acessor for the PangoFontDescription */
-const PangoFontDescription *dia_font_get_description (const DiaFont* font);
-
-/*!
- * \brief Retrieves the height of the font
- * \memberof DiaFont
- */
-real dia_font_get_height(const DiaFont* font);
-/*!
- * \brief Change the height inside a font record.
- * \memberof DiaFont
- */
-void dia_font_set_height(DiaFont* font, real height);
-/*!
- * \brief Delivers the size of the font
- * \memberof DiaFont
- */
-real dia_font_get_size(const DiaFont* font);
-/*!
- * \brief Changes the slant of an existing font
- * \memberof DiaFont
- */
-void dia_font_set_slant(DiaFont* font, DiaFontSlant slant);
-/*!
- * \brief Changes the weight of an existing font
- * \memberof DiaFont
- */
-void dia_font_set_weight(DiaFont* font, DiaFontWeight weight);
-/*!
- * \brief Changes the family of an existing font to one of the three standard families
- * \memberof DiaFont
- */
-void dia_font_set_family(DiaFont* font, DiaFontFamily family);
-/*!
- * \brief Changes the family of an existing font to any family
- * The name is system configuration dependent, but font files are portable nowadays.
- * \memberof DiaFont
- */
-void dia_font_set_any_family(DiaFont* font, const char* family);
-
- /* FIXME: what do we do with this, actually ?
- Name lives for as long as the DiaFont lives. */
-const char *dia_font_get_psfontname(const DiaFont *font);
-
- /* returns a static string suitable for SVG */
-const char *dia_font_get_weight_string(const DiaFont* font);
-
- /* returns a static string suitable for SVG */
-const char *dia_font_get_slant_string(const DiaFont* font);
-
- /* uses an SVG style string */
-void dia_font_set_weight_from_string(DiaFont* font, const char* weight);
- /* uses an SVG style string */
-void dia_font_set_slant_from_string(DiaFont* font, const char* slant);
+#define DIA_TYPE_FONT dia_font_get_type ()
+G_DECLARE_FINAL_TYPE (DiaFont, dia_font, DIA, FONT, GObject)
+
+
+PangoContext *dia_font_get_context (void);
+DiaFont *dia_font_new (const char *family,
+ DiaFontStyle style,
+ double height);
+DiaFont *dia_font_new_from_style (DiaFontStyle style,
+ double height);
+DiaFont *dia_font_new_from_legacy_name (const char *name);
+const char *dia_font_get_legacy_name (DiaFont *font);
+DiaFont *dia_font_copy (DiaFont *font);
+DiaFontStyle dia_font_get_style (DiaFont *font);
+const char *dia_font_get_family (DiaFont *font);
+const PangoFontDescription *dia_font_get_description (DiaFont *font);
+double dia_font_get_height (DiaFont *font);
+void dia_font_set_height (DiaFont *font,
+ double height);
+double dia_font_get_size (DiaFont *font);
+void dia_font_set_slant (DiaFont *font,
+ DiaFontSlant slant);
+void dia_font_set_weight (DiaFont *font,
+ DiaFontWeight weight);
+void dia_font_set_family (DiaFont *font,
+ DiaFontFamily family);
+void dia_font_set_any_family (DiaFont *font,
+ const char *family);
+const char *dia_font_get_psfontname (DiaFont *font);
+const char *dia_font_get_weight_string (DiaFont *font);
+const char *dia_font_get_slant_string (DiaFont *font);
+void dia_font_set_weight_from_string (DiaFont *font,
+ const char *weight);
+void dia_font_set_slant_from_string (DiaFont *font,
+ const char *slant);
/* -------- Font and string functions - unscaled versions.
Use these version in Objects, primarily. */
- /* Get the width of the string with the given font in cm */
-real dia_font_string_width(const char* string, DiaFont* font,
- real height);
- /* Get the ascent of this string in cm (a positive number). */
-real dia_font_ascent(const char* string, DiaFont* font, real height);
- /* Get the descent of the font in cm (a positive number) */
-real dia_font_descent(const char* string, DiaFont* font, real height);
-
- /* prepares a layout of the text, in font 'font'. */
-PangoLayout* dia_font_build_layout(const char* string, DiaFont* font,
- real height);
-
-real* dia_font_get_sizes(const char* string, DiaFont *font, real height,
- real* width, real* ascent, real* descent,
- int *n_offsets, PangoLayoutLine **layout_offsets);
+double dia_font_string_width (const char *string,
+ DiaFont *font,
+ double height);
+double dia_font_ascent (const char *string,
+ DiaFont *font,
+ double height);
+double dia_font_descent (const char *string,
+ DiaFont *font,
+ double height);
+PangoLayout *dia_font_build_layout (const char *string,
+ DiaFont *font,
+ double height);
+double *dia_font_get_sizes (const char *string,
+ DiaFont *font,
+ double height,
+ double *width,
+ double *ascent,
+ double *descent,
+ int *n_offsets,
+ PangoLayoutLine **layout_offsets);
/* -------- Font and string functions - scaled versions.
Use these version in Renderers, exclusively. */
diff --git a/lib/libdia.def b/lib/libdia.def
index 37eb41580..f1bf11d1d 100644
--- a/lib/libdia.def
+++ b/lib/libdia.def
@@ -241,7 +241,6 @@ EXPORTS
dia_font_get_slant_string
dia_font_get_style
dia_font_get_weight_string
- dia_font_init
dia_font_new
dia_font_new_from_legacy_name
dia_font_new_from_style
@@ -861,8 +860,6 @@ EXPORTS
calculate_object_edge
; exported for libart renderer only
- dia_font_pop_context
- dia_font_push_context
dia_transform_coords_double
text_line_adjust_layout_line
diff --git a/lib/meson.build b/lib/meson.build
index bf13db1d6..7bd679e30 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -173,7 +173,12 @@ libdia_sources = stdprop_sources + [
gnome = import('gnome')
-libdia_sources += gnome.mkenums_simple('dia-lib-enums', sources: 'units.h')
+libdia_sources += gnome.mkenums_simple('dia-lib-enums',
+ sources: [
+ 'font.h',
+ 'units.h',
+ ]
+)
diamarshal = gnome.genmarshal(
'diamarshal',
diff --git a/lib/properties.h b/lib/properties.h
index 118e56949..a7e3b11c1 100644
--- a/lib/properties.h
+++ b/lib/properties.h
@@ -40,6 +40,7 @@
#include <glib.h>
#include "diatypes.h"
+#include "font.h"
#include "diarenderer.h"
#include "geometry.h"
#include "arrows.h"
diff --git a/plug-ins/svg/render_svg.c b/plug-ins/svg/render_svg.c
index 1f285eae3..261a8a66e 100644
--- a/plug-ins/svg/render_svg.c
+++ b/plug-ins/svg/render_svg.c
@@ -414,7 +414,7 @@ draw_object(DiaRenderer *self,
static void
node_set_text_style (xmlNodePtr node,
DiaSvgRenderer *renderer,
- const DiaFont *font,
+ DiaFont *font,
double font_height,
Alignment alignment,
Color *colour)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]