fonts.c
来自「ANFY特效」· C语言 代码 · 共 927 行 · 第 1/2 页
C
927 行
sprintf(font_buffer, "%s (%s)", font->family, fontsel_info->properties[FOUNDRY][font->foundry]); list = g_list_append (list, g_strdup (font_buffer)); } else { list = g_list_append (list, g_strdup (font->family)); } } gtk_combo_set_popdown_strings (GTK_COMBO (combo), list); while (list) { g_free (list->data); list = g_list_remove (list, list->data); }}/* This shows all the available sizes in the size clist, according to the current metric and the current font & style. */void fill_sizes_combo (GtkWidget* combo, char* name){#if 0 FontInfo *font; FontStyle *styles, *style; const guint16 *standard_sizes; guint16 *bitmapped_sizes; gint nstandard_sizes, nbitmapped_sizes; gchar buffer[16]; gfloat bitmap_size_float = 0.; guint16 bitmap_size = 0; gboolean can_match; gint type_filter; GList *list = NULL; font = &fontsel_info->font_info[find_index(name)]; styles = &fontsel_info->font_styles[font->style_index]; style = &styles[0];//fontsel->style]; standard_sizes = font_sizes; nstandard_sizes = sizeof(font_sizes) / sizeof(font_sizes[0]); if (1)//fontsel->metric == GTK_FONT_METRIC_POINTS) { bitmapped_sizes = &fontsel_info->point_sizes[style->point_sizes_index]; nbitmapped_sizes = style->npoint_sizes; } else { bitmapped_sizes = &fontsel_info->pixel_sizes[style->pixel_sizes_index]; nbitmapped_sizes = style->npixel_sizes; } /* Only show the standard sizes if a scalable font is available. */ type_filter = fontsel->filters[GTK_FONT_FILTER_BASE].font_type & fontsel->filters[GTK_FONT_FILTER_USER].font_type; if (!((style->flags & GTK_FONT_SCALABLE_BITMAP && type_filter & GTK_FONT_SCALABLE_BITMAP) || (style->flags & GTK_FONT_SCALABLE && type_filter & GTK_FONT_SCALABLE))) nstandard_sizes = 0; /* Interleave the standard sizes with the bitmapped sizes so we get a list of ascending sizes. If the metric is points, we have to convert the decipoints to points. */ while (nstandard_sizes || nbitmapped_sizes) { can_match = TRUE; if (nbitmapped_sizes) { if (fontsel->metric == GTK_FONT_METRIC_POINTS) { if (*bitmapped_sizes % 10 != 0) can_match = FALSE; bitmap_size = *bitmapped_sizes / 10; bitmap_size_float = *bitmapped_sizes / 10; } else { bitmap_size = *bitmapped_sizes; bitmap_size_float = *bitmapped_sizes; } } if (can_match && nstandard_sizes && nbitmapped_sizes && *standard_sizes == bitmap_size) { sprintf(buffer, "%i *", *standard_sizes); standard_sizes++; nstandard_sizes--; bitmapped_sizes++; nbitmapped_sizes--; } else if (nstandard_sizes && (!nbitmapped_sizes || (gfloat)*standard_sizes < bitmap_size_float)) { sprintf(buffer, "%i", *standard_sizes); standard_sizes++; nstandard_sizes--; } else { if (fontsel->metric == GTK_FONT_METRIC_POINTS) { if (*bitmapped_sizes % 10 == 0) sprintf(buffer, "%i *", *bitmapped_sizes / 10); else sprintf(buffer, "%i.%i *", *bitmapped_sizes / 10, *bitmapped_sizes % 10); } else { sprintf(buffer, "%i *", *bitmapped_sizes); } bitmapped_sizes++; nbitmapped_sizes--; } list = g_list_append (list, g_strdup (buffer)); } gtk_combo_set_popdown_strings (GTK_COMBO (combo), list); while (list) { g_free (list->data); list = g_list_remove (list, list->data); }#endif}void gtk_font_selection_get_fonts (){ gchar **xfontnames; GSList **fontnames; gchar *fontname; GSList * temp_list; gint num_fonts; gint i, prop, style, size; gint npixel_sizes = 0, npoint_sizes = 0; FontInfo *font; FontStyle *current_style, *prev_style, *tmp_style; gboolean matched_style, found_size; gint pixels, points, res_x, res_y; gchar field_buffer[XLFD_MAX_FIELD_LEN]; gchar *field; guint8 flags; guint16 *pixel_sizes, *point_sizes, *tmp_sizes; fontsel_info = g_new (GtkFontSelInfo, 1); /* Get a maximum of MAX_FONTS fontnames from the X server. Use "-*" as the pattern rather than "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" since the latter may result in fonts being returned which don't actually exist. xlsfonts also uses "*" so I think it's OK. "-*" gets rid of aliases. */ xfontnames = XListFonts (GDK_DISPLAY(), "-*", MAX_FONTS, &num_fonts); /* Output a warning if we actually get MAX_FONTS fonts. */ if (num_fonts == MAX_FONTS) g_warning(_("MAX_FONTS exceeded. Some fonts may be missing.")); /* The maximum size of all these tables is the number of font names returned. We realloc them later when we know exactly how many unique entries there are. */ fontsel_info->font_info = g_new (FontInfo, num_fonts); fontsel_info->font_styles = g_new (FontStyle, num_fonts); fontsel_info->pixel_sizes = g_new (guint16, num_fonts); fontsel_info->point_sizes = g_new (guint16, num_fonts); fontnames = g_new (GSList*, num_fonts); /* Create the initial arrays for the property value strings, though they may be realloc'ed later. Put the wildcard '*' in the first elements. */ for (prop = 0; prop < GTK_NUM_FONT_PROPERTIES; prop++) { fontsel_info->properties[prop] = g_new(gchar*, PROPERTY_ARRAY_INCREMENT); fontsel_info->space_allocated[prop] = PROPERTY_ARRAY_INCREMENT; fontsel_info->nproperties[prop] = 1; fontsel_info->properties[prop][0] = "*"; } /* Insert the font families into the main table, sorted by family and foundry (fonts with different foundries are placed in seaparate FontInfos. All fontnames in each family + foundry are placed into the fontnames array of lists. */ fontsel_info->nfonts = 0; for (i = 0; i < num_fonts; i++) {#ifdef FONTSEL_DEBUG g_message("%s\n", xfontnames[i]);#endif if (gtk_font_selection_is_xlfd_font_name (xfontnames[i])) gtk_font_selection_insert_font (fontnames, &fontsel_info->nfonts, xfontnames[i]); else {#ifdef FONTSEL_DEBUG g_warning("Skipping invalid font: %s", xfontnames[i]);#endif } } /* Since many font names will be in the same FontInfo not all of the allocated FontInfo table will be used, so we will now reallocate it with the real size. */ fontsel_info->font_info = g_realloc(fontsel_info->font_info, sizeof(FontInfo) * fontsel_info->nfonts); /* Now we work out which choices of weight/slant etc. are valid for each font. */ fontsel_info->nstyles = 0; current_style = fontsel_info->font_styles; for (i = 0; i < fontsel_info->nfonts; i++) { font = &fontsel_info->font_info[i]; /* Use the next free position in the styles array. */ font->style_index = fontsel_info->nstyles; /* Now step through each of the fontnames with this family, and create a style for each fontname. Each style contains the index into the weights/slants etc. arrays, and a number of pixel/point sizes. */ style = 0; temp_list = fontnames[i]; while (temp_list) { fontname = temp_list->data; temp_list = temp_list->next; for (prop = 0; prop < GTK_NUM_STYLE_PROPERTIES; prop++) { current_style->properties[prop] = gtk_font_selection_insert_field (fontname, prop); } current_style->pixel_sizes_index = npixel_sizes; current_style->npixel_sizes = 0; current_style->point_sizes_index = npoint_sizes; current_style->npoint_sizes = 0; current_style->flags = 0; field = gtk_font_selection_get_xlfd_field (fontname, XLFD_PIXELS, field_buffer); pixels = atoi(field); field = gtk_font_selection_get_xlfd_field (fontname, XLFD_POINTS, field_buffer); points = atoi(field); field = gtk_font_selection_get_xlfd_field (fontname, XLFD_RESOLUTION_X, field_buffer); res_x = atoi(field); field = gtk_font_selection_get_xlfd_field (fontname, XLFD_RESOLUTION_Y, field_buffer); res_y = atoi(field); if (pixels == 0 && points == 0) { if (res_x == 0 && res_y == 0) flags = GTK_FONT_SCALABLE; else flags = GTK_FONT_SCALABLE_BITMAP; } else flags = GTK_FONT_BITMAP; /* Now we check to make sure that the style is unique. If it isn't we forget it. */ prev_style = fontsel_info->font_styles + font->style_index; matched_style = FALSE; while (prev_style < current_style) { matched_style = TRUE; for (prop = 0; prop < GTK_NUM_STYLE_PROPERTIES; prop++) { if (prev_style->properties[prop] != current_style->properties[prop]) { matched_style = FALSE; break; } } if (matched_style) break; prev_style++; } /* If we matched an existing style, we need to add the pixels & point sizes to the style. If not, we insert the pixel & point sizes into our new style. Note that we don't add sizes for scalable fonts. */ if (matched_style) { prev_style->flags |= flags; if (flags == GTK_FONT_BITMAP) { pixel_sizes = fontsel_info->pixel_sizes + prev_style->pixel_sizes_index; found_size = FALSE; for (size = 0; size < prev_style->npixel_sizes; size++) { if (pixels == *pixel_sizes) { found_size = TRUE; break; } else if (pixels < *pixel_sizes) break; pixel_sizes++; } /* We need to move all the following pixel sizes up, and also update the indexes of any following styles. */ if (!found_size) { for (tmp_sizes = fontsel_info->pixel_sizes + npixel_sizes; tmp_sizes > pixel_sizes; tmp_sizes--) *tmp_sizes = *(tmp_sizes - 1); *pixel_sizes = pixels; npixel_sizes++; prev_style->npixel_sizes++; tmp_style = prev_style + 1; while (tmp_style < current_style) { tmp_style->pixel_sizes_index++; tmp_style++; } } point_sizes = fontsel_info->point_sizes + prev_style->point_sizes_index; found_size = FALSE; for (size = 0; size < prev_style->npoint_sizes; size++) { if (points == *point_sizes) { found_size = TRUE; break; } else if (points < *point_sizes) break; point_sizes++; } /* We need to move all the following point sizes up, and also update the indexes of any following styles. */ if (!found_size) { for (tmp_sizes = fontsel_info->point_sizes + npoint_sizes; tmp_sizes > point_sizes; tmp_sizes--) *tmp_sizes = *(tmp_sizes - 1); *point_sizes = points; npoint_sizes++; prev_style->npoint_sizes++; tmp_style = prev_style + 1; while (tmp_style < current_style) { tmp_style->point_sizes_index++; tmp_style++; } } } } else { current_style->flags = flags; if (flags == GTK_FONT_BITMAP) { fontsel_info->pixel_sizes[npixel_sizes++] = pixels; current_style->npixel_sizes = 1; fontsel_info->point_sizes[npoint_sizes++] = points; current_style->npoint_sizes = 1; } style++; fontsel_info->nstyles++; current_style++; } } g_slist_free(fontnames[i]); /* Set nstyles to the real value, minus duplicated fontnames. Note that we aren't using all the allocated memory if fontnames are duplicated. */ font->nstyles = style; } /* Since some repeated styles may be skipped we won't have used all the allocated space, so we will now reallocate it with the real size. */ fontsel_info->font_styles = g_realloc(fontsel_info->font_styles, sizeof(FontStyle) * fontsel_info->nstyles); fontsel_info->pixel_sizes = g_realloc(fontsel_info->pixel_sizes, sizeof(guint16) * npixel_sizes); fontsel_info->point_sizes = g_realloc(fontsel_info->point_sizes, sizeof(guint16) * npoint_sizes); g_free(fontnames); XFreeFontNames (xfontnames); /* Debugging Output */ /* This outputs all FontInfos. */#ifdef FONTSEL_DEBUG g_message("\n\n Font Family Weight Slant Set Width Spacing Charset\n\n"); for (i = 0; i < fontsel_info->nfonts; i++) { FontInfo *font = &fontsel_info->font_info[i]; FontStyle *styles = fontsel_info->font_styles + font->style_index; for (style = 0; style < font->nstyles; style++) { g_message("%5i %-16.16s ", i, font->family); for (prop = 0; prop < GTK_NUM_STYLE_PROPERTIES; prop++) g_message("%-9.9s ", fontsel_info->properties[prop][styles->properties[prop]]); g_message("\n "); if (styles->flags & GTK_FONT_BITMAP) g_message("Bitmapped font "); if (styles->flags & GTK_FONT_SCALABLE) g_message("Scalable font "); if (styles->flags & GTK_FONT_SCALABLE_BITMAP) g_message("Scalable-Bitmapped font "); g_message("\n"); if (styles->npixel_sizes) { g_message(" Pixel sizes: "); tmp_sizes = fontsel_info->pixel_sizes + styles->pixel_sizes_index; for (size = 0; size < styles->npixel_sizes; size++) g_message("%i ", *tmp_sizes++); g_message("\n"); } if (styles->npoint_sizes) { g_message(" Point sizes: "); tmp_sizes = fontsel_info->point_sizes + styles->point_sizes_index; for (size = 0; size < styles->npoint_sizes; size++) g_message("%i ", *tmp_sizes++); g_message("\n"); } g_message("\n"); styles++; } } /* This outputs all available properties. */ for (prop = 0; prop < GTK_NUM_FONT_PROPERTIES; prop++) { g_message("Property: %s\n", xlfd_field_names[xlfd_index[prop]]); for (i = 0; i < fontsel_info->nproperties[prop]; i++) g_message(" %s\n", fontsel_info->properties[prop][i]); }#endif}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?