📄 pangocairo-win32font.c
字号:
/* Pango * pangocairowin32-font.c: Cairo font handling, Win32 backend * * Copyright (C) 2000-2005 Red Hat Software * * 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. */#include <config.h>#include <math.h>#include <stdlib.h>#include <string.h>#include "pango-fontmap.h"#include "pango-impl-utils.h"#include "pangocairo-private.h"#include "pangocairo-win32.h"#include <cairo-win32.h>#define PANGO_TYPE_CAIRO_WIN32_FONT (pango_cairo_win32_font_get_type ())#define PANGO_CAIRO_WIN32_FONT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), PANGO_TYPE_CAIRO_WIN32_FONT, PangoCairoWin32Font))#define PANGO_CAIRO_WIN32_FONT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PANGO_TYPE_CAIRO_WIN32_FONT, PangoCairoWin32FontClass))#define PANGO_CAIRO_IS_FONT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PANGO_TYPE_CAIRO_WIN32_FONT))#define PANGO_CAIRO_WIN32_FONT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PANGO_TYPE_CAIRO_WIN32_FONT, PangoCairoWin32FontClass))typedef struct _PangoCairoWin32Font PangoCairoWin32Font;typedef struct _PangoCairoWin32FontClass PangoCairoWin32FontClass;typedef struct _PangoCairoWin32GlyphInfo PangoCairoWin32GlyphInfo;struct _PangoCairoWin32Font{ PangoWin32Font font; int size; cairo_font_face_t *font_face; cairo_scaled_font_t *scaled_font; cairo_matrix_t font_matrix; cairo_matrix_t ctm; cairo_font_options_t *options; GSList *metrics_by_lang; GHashTable *glyph_info;};struct _PangoCairoWin32FontClass{ PangoWin32FontClass parent_class;};struct _PangoCairoWin32GlyphInfo{ PangoRectangle logical_rect; PangoRectangle ink_rect;};GType pango_cairo_win32_font_get_type (void);/******************************* * Utility functions * *******************************/static cairo_font_face_t *pango_cairo_win32_font_get_font_face (PangoCairoFont *font){ PangoCairoWin32Font *cwfont = PANGO_CAIRO_WIN32_FONT (font); PangoWin32Font *win32font = PANGO_WIN32_FONT (cwfont); if (cwfont->font_face == NULL) { LOGFONTW logfontw; /* Count here on the fact that all the struct fields are the * same for LOGFONTW and LOGFONTA except lfFaceName which is the * last field */ memcpy (&logfontw, &win32font->logfont, sizeof (LOGFONTA)); if (!MultiByteToWideChar (CP_ACP, MB_ERR_INVALID_CHARS, win32font->logfont.lfFaceName, -1, logfontw.lfFaceName, G_N_ELEMENTS (logfontw.lfFaceName))) logfontw.lfFaceName[0] = 0; /* Hopefully this will select some font */ cwfont->font_face = cairo_win32_font_face_create_for_logfontw (&logfontw); /* Failure of the above should only occur for out of memory, * we can't proceed at that point */ if (!cwfont->font_face) g_error ("Unable to create Win32 cairo font face.\nThis means out of memory or a cairo/fontconfig/FreeType bug"); } return cwfont->font_face;}static cairo_scaled_font_t *pango_cairo_win32_font_get_scaled_font (PangoCairoFont *font){ PangoCairoWin32Font *cwfont = PANGO_CAIRO_WIN32_FONT (font); if (!cwfont->scaled_font) { cairo_font_face_t *font_face; font_face = pango_cairo_win32_font_get_font_face (font); cwfont->scaled_font = cairo_scaled_font_create (font_face, &cwfont->font_matrix, &cwfont->ctm, cwfont->options); /* Failure of the above should only occur for out of memory, * we can't proceed at that point */ if (!cwfont->scaled_font) g_error ("Unable to create Win32 cairo scaled font.\nThis means out of memory or a cairo/fontconfig/FreeType bug"); } return cwfont->scaled_font;}/******************************** * Method implementations * ********************************/static gbooleanpango_cairo_win32_font_install (PangoCairoFont *font, cairo_t *cr){ PangoCairoWin32Font *cwfont = PANGO_CAIRO_WIN32_FONT (font); cairo_set_font_face (cr, pango_cairo_win32_font_get_font_face (font)); cairo_set_font_matrix (cr, &cwfont->font_matrix); cairo_set_font_options (cr, cwfont->options); return TRUE;}static voidcairo_font_iface_init (PangoCairoFontIface *iface){ iface->install = pango_cairo_win32_font_install; iface->get_font_face = pango_cairo_win32_font_get_font_face; iface->get_scaled_font = pango_cairo_win32_font_get_scaled_font;}G_DEFINE_TYPE_WITH_CODE (PangoCairoWin32Font, pango_cairo_win32_font, PANGO_TYPE_WIN32_FONT, { G_IMPLEMENT_INTERFACE (PANGO_TYPE_CAIRO_FONT, cairo_font_iface_init) });static voidfree_metrics_info (PangoWin32MetricsInfo *info){ pango_font_metrics_unref (info->metrics); g_slice_free (PangoWin32MetricsInfo, info);}static voidpango_cairo_win32_font_finalize (GObject *object){ PangoCairoWin32Font *cwfont = PANGO_CAIRO_WIN32_FONT (object); g_hash_table_destroy (cwfont->glyph_info); g_slist_foreach (cwfont->metrics_by_lang, (GFunc)free_metrics_info, NULL); g_slist_free (cwfont->metrics_by_lang); if (cwfont->scaled_font) cairo_scaled_font_destroy (cwfont->scaled_font); if (cwfont->options) cairo_font_options_destroy (cwfont->options); G_OBJECT_CLASS (pango_cairo_win32_font_parent_class)->finalize (object);}static voidcompute_glyph_extents (PangoFont *font, PangoGlyph glyph, PangoRectangle *ink_rect, PangoRectangle *logical_rect){ PangoCairoFont *cfont = (PangoCairoFont *)font; cairo_scaled_font_t *scaled_font = pango_cairo_win32_font_get_scaled_font (PANGO_CAIRO_FONT (cfont)); /* It may well be worth caching the font_extents here, since getting them * is pretty expensive. */ cairo_font_extents_t font_extents; cairo_text_extents_t extents; cairo_glyph_t cairo_glyph; cairo_scaled_font_extents (scaled_font, &font_extents); logical_rect->x = 0; logical_rect->y = - font_extents.ascent * PANGO_SCALE; logical_rect->width = 0; logical_rect->height = (font_extents.ascent + font_extents.descent) * PANGO_SCALE; if (glyph == PANGO_GLYPH_EMPTY) { /* already initialized above */ } else if (glyph & PANGO_GLYPH_UNKNOWN_FLAG) { /* space for the hex box */ _pango_cairo_get_glyph_extents_missing(cfont, glyph, ink_rect, logical_rect); } else { cairo_glyph.index = glyph; cairo_glyph.x = 0; cairo_glyph.y = 0; cairo_scaled_font_glyph_extents (scaled_font, &cairo_glyph, 1, &extents); ink_rect->x = extents.x_bearing * PANGO_SCALE; ink_rect->y = extents.y_bearing * PANGO_SCALE; ink_rect->width = extents.width * PANGO_SCALE; ink_rect->height = extents.height * PANGO_SCALE; logical_rect->width = extents.x_advance * PANGO_SCALE; }}static PangoCairoWin32GlyphInfo *pango_cairo_win32_font_get_glyph_info (PangoFont *font, PangoGlyph glyph){ PangoCairoWin32Font *cwfont = (PangoCairoWin32Font *)font; PangoCairoWin32GlyphInfo *info; info = g_hash_table_lookup (cwfont->glyph_info, GUINT_TO_POINTER (glyph)); if (!info) { info = g_new0 (PangoCairoWin32GlyphInfo, 1); compute_glyph_extents (font, glyph, &info->ink_rect, &info->logical_rect); g_hash_table_insert (cwfont->glyph_info, GUINT_TO_POINTER (glyph), info); } return info;}static voidpango_cairo_win32_font_get_glyph_extents (PangoFont *font, PangoGlyph glyph, PangoRectangle *ink_rect, PangoRectangle *logical_rect){ PangoCairoWin32GlyphInfo *info; info = pango_cairo_win32_font_get_glyph_info (font, glyph); if (ink_rect) *ink_rect = info->ink_rect; if (logical_rect) *logical_rect = info->logical_rect;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -