1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* GimpText
* Copyright (C) 2002-2003 Sven Neumann <sven@gimp.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <pango/pangocairo.h>
#include <pango/pangofc-fontmap.h>
#include <fontconfig/fontconfig.h>
#include "libgimpcolor/gimpcolor.h"
#include "text-types.h"
#include "core/gimp.h"
#include "core/gimpchannel.h"
#include "core/gimpcontext.h"
#include "core/gimpdatafactory.h"
#include "core/gimpimage.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimpimage-undo.h"
#include "core/gimplayer-floating-selection.h"
#include "gimpfont.h"
#include "gimptext.h"
#include "gimptext-compat.h"
#include "gimptextlayer.h"
#include "gimp-intl.h"
GimpLayer *
text_render (GimpImage *image,
GimpDrawable *drawable,
GimpContext *context,
gint text_x,
gint text_y,
GimpFont *font,
gdouble font_size,
const gchar *text,
gint border,
gboolean antialias)
{
GimpText *gtext;
GimpLayer *layer;
GeglColor *color;
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
g_return_val_if_fail (drawable == NULL || GIMP_IS_DRAWABLE (drawable), NULL);
g_return_val_if_fail (drawable == NULL ||
gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
g_return_val_if_fail (GIMP_IS_FONT (font), NULL);
g_return_val_if_fail (text != NULL, NULL);
if (! gimp_data_factory_data_wait (image->gimp->font_factory))
return NULL;
if (border < 0)
border = 0;
color = gimp_context_get_foreground (context);
gtext = g_object_new (GIMP_TYPE_TEXT,
"text", text,
"font", font,
"font-size", font_size,
"antialias", antialias,
"border", border,
"color", color,
NULL);
layer = gimp_text_layer_new (image, gtext);
g_object_unref (gtext);
if (!layer)
return NULL;
/* Start a group undo */
gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_TEXT,
_("Add Text Layer"));
/* Set the layer offsets */
gimp_item_set_offset (GIMP_ITEM (layer), text_x, text_y);
/* If there is a selection mask clear it--
* this might not always be desired, but in general,
* it seems like the correct behavior.
*/
if (! gimp_channel_is_empty (gimp_image_get_mask (image)))
gimp_channel_clear (gimp_image_get_mask (image), NULL, TRUE);
if (drawable == NULL)
{
/* If the drawable is NULL, create a new layer */
gimp_image_add_layer (image, layer, NULL, -1, TRUE);
}
else
{
/* Otherwise, instantiate the text as the new floating selection */
floating_sel_attach (layer, drawable);
}
/* end the group undo */
gimp_image_undo_group_end (image);
return layer;
}
gboolean
text_get_extents (Gimp *gimp,
GimpFont *font,
gdouble font_size,
const gchar *text,
gint *width,
gint *height,
gint *ascent,
gint *descent)
{
PangoFontDescription *font_desc;
PangoContext *context;
PangoLayout *layout;
PangoFontMap *fontmap;
PangoRectangle rect;
gchar *real_fontname;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
g_return_val_if_fail (GIMP_IS_FONT (font), FALSE);
g_return_val_if_fail (text != NULL, FALSE);
if (! gimp_data_factory_data_wait (gimp->font_factory))
return FALSE;
fontmap = pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT);
if (! fontmap)
g_error ("You are using a Pango that has been built against a cairo "
"that lacks the Freetype font backend");
/* In case a font becomes missing mid-session and is chosen (or is already in use)
* pango substitutes EVERY font for the default font, to avoid this
* the FcConfig has to be set everytime a pango fontmap is created
*/
pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (fontmap), FcConfigGetCurrent ());
pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (fontmap),
72.0); /* FIXME: resolution */
context = pango_font_map_create_context (fontmap);
g_object_unref (fontmap);
layout = pango_layout_new (context);
g_object_unref (context);
real_fontname = g_strdup_printf ("%s %d", gimp_font_get_lookup_name (font), (gint) font_size);
font_desc = pango_font_description_from_string (real_fontname);
pango_layout_set_font_description (layout, font_desc);
pango_font_description_free (font_desc);
g_free (real_fontname);
pango_layout_set_text (layout, text, -1);
pango_layout_get_pixel_extents (layout, NULL, &rect);
if (width)
*width = rect.width;
if (height)
*height = rect.height;
if (ascent || descent)
{
PangoLayoutIter *iter;
PangoLayoutLine *line;
iter = pango_layout_get_iter (layout);
line = pango_layout_iter_get_line_readonly (iter);
pango_layout_iter_free (iter);
pango_layout_line_get_pixel_extents (line, NULL, &rect);
if (ascent)
*ascent = PANGO_ASCENT (rect);
if (descent)
*descent = PANGO_DESCENT (rect);
}
g_object_unref (layout);
return TRUE;
}
|