📄 gtkfontsel.c
字号:
/* GTK - The GIMP Toolkit * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald * * GtkFontSelection widget for Gtk+, by Damon Chaplin, May 1998. * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes. * The GnomeFontSelector was derived from app/text_tool.c in the GIMP. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *//* * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS * file for a list of people on the GTK+ Team. See the ChangeLog * files for a list of changes. These files are distributed with * GTK+ at ftp://ftp.gtk.org/pub/gtk/. *//* * Limits: * * Fontnames - A maximum of MAX_FONTS (32767) fontnames will be retrieved * from X Windows with XListFonts(). Any more are ignored. * I think this limit may have been set because of a limit in * GtkList. It could possibly be increased since we are using * GtkClists now, but I'd be surprised if it was reached. * Field length - XLFD_MAX_FIELD_LEN is the maximum length that any field of a * fontname can be for it to be considered valid. Others are * ignored. * Properties - Maximum of 65535 choices for each font property - guint16's * are used as indices, e.g. in the FontInfo struct. * Combinations - Maximum of 65535 combinations of properties for each font * family - a guint16 is used in the FontInfo struct. * Font size - Minimum font size of 2 pixels/points, since trying to load * some fonts with a size of 1 can cause X to hang. * (e.g. the Misc Fixed fonts). *//* * Possible Improvements: * * Font Styles - could sort the styles into a reasonable order - regular * first, then bold, bold italic etc. * * I18N - the default preview text is not useful for international * fonts. Maybe the first few characters of the font could be * displayed instead. * - fontsets? should these be handled by the font dialog? *//* * Debugging: compile with -DFONTSEL_DEBUG for lots of debugging output. */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <X11/Xlib.h>#include "gdk/gdkx.h"#include "gdk/gdkkeysyms.h"#include "gtkbutton.h"#include "gtkcheckbutton.h"#include "gtkclist.h"#include "gtkentry.h"#include "gtkfontsel.h"#include "gtkframe.h"#include "gtkhbbox.h"#include "gtkhbox.h"#include "gtklabel.h"#include "gtknotebook.h"#include "gtkradiobutton.h"#include "gtksignal.h"#include "gtktable.h"#include "gtkvbox.h"#include "gtkscrolledwindow.h"#include "gtkintl.h"/* The maximum number of fontnames requested with XListFonts(). */#define MAX_FONTS 32767/* This is the largest field length we will accept. If a fontname has a field larger than this we will skip it. */#define XLFD_MAX_FIELD_LEN 64/* These are what we use as the standard font sizes, for the size clist. Note that when using points we still show these integer point values but we work internally in decipoints (and decipoint values can be typed in). */static const guint16 font_sizes[] = { 8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 22, 24, 26, 28, 32, 36, 40, 48, 56, 64, 72};/* Initial font metric & size (Remember point sizes are in decipoints). The font size should match one of those in the font_sizes array. */#define INITIAL_METRIC GTK_FONT_METRIC_POINTS#define INITIAL_FONT_SIZE 140/* This is the default text shown in the preview entry, though the user can set it. Remember that some fonts only have capital letters. */#define PREVIEW_TEXT "abcdefghijk ABCDEFGHIJK"/* This is the initial and maximum height of the preview entry (it expands when large font sizes are selected). Initial height is also the minimum. */#define INITIAL_PREVIEW_HEIGHT 44#define MAX_PREVIEW_HEIGHT 300/* These are the sizes of the font, style & size clists. */#define FONT_LIST_HEIGHT 136#define FONT_LIST_WIDTH 190#define FONT_STYLE_LIST_WIDTH 170#define FONT_SIZE_LIST_WIDTH 60/* This is the number of fields in an X Logical Font Description font name. Note that we count the registry & encoding as 1. */#define GTK_XLFD_NUM_FIELDS 13typedef struct _GtkFontSelInfo GtkFontSelInfo;typedef struct _FontInfo FontInfo;typedef struct _FontStyle FontStyle;/* This struct represents one family of fonts (with one foundry), e.g. adobe courier or sony fixed. It stores the family name, the index of the foundry name, and the index of and number of available styles. */struct _FontInfo{ gchar *family; guint16 foundry; gint style_index; guint16 nstyles;};/* This represents one style, as displayed in the Font Style clist. It can have a number of available pixel sizes and point sizes. The indexes point into the two big fontsel_info->pixel_sizes & fontsel_info->point_sizes arrays. The displayed flag is used when displaying styles to remember which styles have already been displayed. Note that it is combined with the GtkFontType in the flags field. */#define GTK_FONT_DISPLAYED (1 << 7)struct _FontStyle{ guint16 properties[GTK_NUM_STYLE_PROPERTIES]; gint pixel_sizes_index; guint16 npixel_sizes; gint point_sizes_index; guint16 npoint_sizes; guint8 flags;};struct _GtkFontSelInfo { /* This is a table with each FontInfo representing one font family+foundry */ FontInfo *font_info; gint nfonts; /* This stores all the valid combinations of properties for every family. Each FontInfo holds an index into its own space in this one big array. */ FontStyle *font_styles; gint nstyles; /* This stores all the font sizes available for every style. Each style holds an index into these arrays. */ guint16 *pixel_sizes; guint16 *point_sizes; /* These are the arrays of strings of all possible weights, slants, set widths, spacings, charsets & foundries, and the amount of space allocated for each array. */ gchar **properties[GTK_NUM_FONT_PROPERTIES]; guint16 nproperties[GTK_NUM_FONT_PROPERTIES]; guint16 space_allocated[GTK_NUM_FONT_PROPERTIES];};/* These are the field numbers in the X Logical Font Description fontnames, e.g. -adobe-courier-bold-o-normal--25-180-100-100-m-150-iso8859-1 */typedef enum{ XLFD_FOUNDRY = 0, XLFD_FAMILY = 1, XLFD_WEIGHT = 2, XLFD_SLANT = 3, XLFD_SET_WIDTH = 4, XLFD_ADD_STYLE = 5, XLFD_PIXELS = 6, XLFD_POINTS = 7, XLFD_RESOLUTION_X = 8, XLFD_RESOLUTION_Y = 9, XLFD_SPACING = 10, XLFD_AVERAGE_WIDTH = 11, XLFD_CHARSET = 12} FontField;/* These are the names of the fields, used on the info & filter page. */static const gchar* xlfd_field_names[GTK_XLFD_NUM_FIELDS] = { N_("Foundry:"), N_("Family:"), N_("Weight:"), N_("Slant:"), N_("Set Width:"), N_("Add Style:"), N_("Pixel Size:"), N_("Point Size:"), N_("Resolution X:"), N_("Resolution Y:"), N_("Spacing:"), N_("Average Width:"), N_("Charset:"),};/* These are the array indices of the font properties used in several arrays, and should match the xlfd_index array below. */typedef enum{ WEIGHT = 0, SLANT = 1, SET_WIDTH = 2, SPACING = 3, CHARSET = 4, FOUNDRY = 5} PropertyIndexType;/* This is used to look up a field in a fontname given one of the above property indices. */static const FontField xlfd_index[GTK_NUM_FONT_PROPERTIES] = { XLFD_WEIGHT, XLFD_SLANT, XLFD_SET_WIDTH, XLFD_SPACING, XLFD_CHARSET, XLFD_FOUNDRY};/* These are the positions of the properties in the filter table - x, y. */static const gint filter_positions[GTK_NUM_FONT_PROPERTIES][2] = { { 1, 0 }, { 0, 2 }, { 1, 2 }, { 2, 2 }, { 2, 0 }, { 0, 0 }};static const gint filter_heights[GTK_NUM_FONT_PROPERTIES] = { 100, 70, 70, 40, 100, 100};/* This is returned by gtk_font_selection_filter_state to describe if a property value is filtered. e.g. if 'bold' has been selected on the filter page, then that will return 'FILTERED' and 'black' will be 'NOT_FILTERED'. If none of the weight values are selected, they all return 'NOT_SET'. */typedef enum{ FILTERED, NOT_FILTERED, NOT_SET} GtkFontPropertyFilterState;static GtkFontSelInfo *fontsel_info = NULL;/* The initial size and increment of each of the arrays of property values. */#define PROPERTY_ARRAY_INCREMENT 16static void gtk_font_selection_class_init (GtkFontSelectionClass *klass);static void gtk_font_selection_init (GtkFontSelection *fontsel);static void gtk_font_selection_destroy (GtkObject *object);/* These are all used for class initialization - loading in the fonts etc. */static void gtk_font_selection_get_fonts (void);static void gtk_font_selection_insert_font (GSList *fontnames[], gint *ntable, gchar *fontname);static gint gtk_font_selection_insert_field (gchar *fontname, gint prop);/* These are the callbacks & related functions. */static void gtk_font_selection_select_font (GtkWidget *w, gint row, gint column, GdkEventButton *bevent, gpointer data);static gint gtk_font_selection_on_clist_key_press (GtkWidget *clist, GdkEventKey *event, GtkFontSelection *fs);static gboolean gtk_font_selection_select_next (GtkFontSelection *fs, GtkWidget *clist, gint step);static void gtk_font_selection_show_available_styles(GtkFontSelection *fs);static void gtk_font_selection_select_best_style (GtkFontSelection *fs, gboolean use_first);static void gtk_font_selection_select_style (GtkWidget *w, gint row, gint column, GdkEventButton *bevent, gpointer data);static void gtk_font_selection_show_available_sizes(GtkFontSelection *fs);static gint gtk_font_selection_size_key_press (GtkWidget *w, GdkEventKey *event, gpointer data);static void gtk_font_selection_select_best_size (GtkFontSelection *fs);static void gtk_font_selection_select_size (GtkWidget *w, gint row, gint column, GdkEventButton *bevent, gpointer data);static void gtk_font_selection_metric_callback (GtkWidget *w, gpointer data);static void gtk_font_selection_expose_list (GtkWidget *w, GdkEventExpose *event, gpointer data);static void gtk_font_selection_realize_list (GtkWidget *widget, gpointer data);static void gtk_font_selection_switch_page (GtkWidget *w, GtkNotebookPage *page, gint page_num, gpointer data);static void gtk_font_selection_show_font_info (GtkFontSelection *fs);static void gtk_font_selection_select_filter (GtkWidget *w, gint row, gint column, GdkEventButton *bevent, GtkFontSelection *fs);static void gtk_font_selection_unselect_filter (GtkWidget *w, gint row, gint column, GdkEventButton *bevent, GtkFontSelection *fs);static void gtk_font_selection_update_filter (GtkFontSelection *fs);static gboolean gtk_font_selection_style_visible (GtkFontSelection *fs, FontInfo *font, gint style);static void gtk_font_selection_reset_filter (GtkWidget *w, GtkFontSelection *fs);static void gtk_font_selection_on_clear_filter (GtkWidget *w, GtkFontSelection *fs);static void gtk_font_selection_show_available_fonts (GtkFontSelection *fs);static void gtk_font_selection_clear_filter (GtkFontSelection *fs);static void gtk_font_selection_update_filter_lists(GtkFontSelection *fs);static GtkFontPropertyFilterState gtk_font_selection_filter_state (GtkFontSelection *fs, GtkFontFilterType filter_type, gint property, gint index);/* Misc. utility functions. */static gboolean gtk_font_selection_load_font (GtkFontSelection *fs);static void gtk_font_selection_update_preview (GtkFontSelection *fs);static gint gtk_font_selection_find_font (GtkFontSelection *fs, gchar *family, guint16 foundry);static guint16 gtk_font_selection_field_to_index (gchar **table, gint ntable, gchar *field);static gchar* gtk_font_selection_expand_slant_code (gchar *slant);static gchar* gtk_font_selection_expand_spacing_code(gchar *spacing);/* Functions for handling X Logical Font Description fontnames. */static gboolean gtk_font_selection_is_xlfd_font_name (const gchar *fontname);static char* gtk_font_selection_get_xlfd_field (const gchar *fontname, FontField field_num, gchar *buffer);static gchar * gtk_font_selection_create_xlfd (gint size, GtkFontMetricType metric, gchar *foundry, gchar *family, gchar *weight, gchar *slant, gchar *set_width,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -