gimptextbuffer.c 43.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * GimpTextBuffer
 * Copyright (C) 2010  Michael Natterer <mitch@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 <http://www.gnu.org/licenses/>.
 */

#include "config.h"

23
#include <stdlib.h>
24 25 26 27 28

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
29
#include "libgimpcolor/gimpcolor.h"
30 31 32 33

#include "widgets-types.h"

#include "gimptextbuffer.h"
34
#include "gimptextbuffer-serialize.h"
35
#include "gimptexttag.h"
36
#include "gimpwidgets-utils.h"
37 38 39 40 41 42

#include "gimp-intl.h"


/*  local function prototypes  */

43 44 45
static void   gimp_text_buffer_constructed (GObject           *object);
static void   gimp_text_buffer_dispose     (GObject           *object);
static void   gimp_text_buffer_finalize    (GObject           *object);
46

47 48 49
static void   gimp_text_buffer_mark_set    (GtkTextBuffer     *buffer,
                                            const GtkTextIter *location,
                                            GtkTextMark       *mark);
50

51 52 53 54 55 56 57 58 59

G_DEFINE_TYPE (GimpTextBuffer, gimp_text_buffer, GTK_TYPE_TEXT_BUFFER)

#define parent_class gimp_text_buffer_parent_class


static void
gimp_text_buffer_class_init (GimpTextBufferClass *klass)
{
60 61
  GObjectClass       *object_class = G_OBJECT_CLASS (klass);
  GtkTextBufferClass *buffer_class = GTK_TEXT_BUFFER_CLASS (klass);
62

63
  object_class->constructed = gimp_text_buffer_constructed;
64 65
  object_class->dispose     = gimp_text_buffer_dispose;
  object_class->finalize    = gimp_text_buffer_finalize;
66 67

  buffer_class->mark_set    = gimp_text_buffer_mark_set;
68 69 70 71 72
}

static void
gimp_text_buffer_init (GimpTextBuffer *buffer)
{
73 74 75 76 77 78 79 80 81 82
  buffer->markup_atom =
    gtk_text_buffer_register_serialize_format (GTK_TEXT_BUFFER (buffer),
                                               "application/x-gimp-pango-markup",
                                               gimp_text_buffer_serialize,
                                               NULL, NULL);

  gtk_text_buffer_register_deserialize_format (GTK_TEXT_BUFFER (buffer),
                                               "application/x-gimp-pango-markup",
                                               gimp_text_buffer_deserialize,
                                               NULL, NULL);
83 84
}

85 86
static void
gimp_text_buffer_constructed (GObject *object)
87
{
88
  GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object);
89

90
  G_OBJECT_CLASS (parent_class)->constructed (object);
91 92 93

  gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), "", -1);

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  buffer->bold_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                 "bold",
                                                 "weight", PANGO_WEIGHT_BOLD,
                                                 NULL);

  buffer->italic_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                   "italic",
                                                   "style", PANGO_STYLE_ITALIC,
                                                   NULL);

  buffer->underline_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                      "underline",
                                                      "underline", PANGO_UNDERLINE_SINGLE,
                                                      NULL);

  buffer->strikethrough_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                          "strikethrough",
                                                          "strikethrough", TRUE,
                                                          NULL);
113 114 115 116 117 118 119 120 121 122 123 124 125
}

static void
gimp_text_buffer_dispose (GObject *object)
{
  /* GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object); */

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gimp_text_buffer_finalize (GObject *object)
{
126 127
  GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object);

128 129 130 131 132 133
  if (buffer->size_tags)
    {
      g_list_free (buffer->size_tags);
      buffer->size_tags = NULL;
    }

134 135 136 137 138 139
  if (buffer->baseline_tags)
    {
      g_list_free (buffer->baseline_tags);
      buffer->baseline_tags = NULL;
    }

140
  if (buffer->kerning_tags)
141
    {
142 143
      g_list_free (buffer->kerning_tags);
      buffer->kerning_tags = NULL;
144 145
    }

146 147 148 149 150 151
  if (buffer->font_tags)
    {
      g_list_free (buffer->font_tags);
      buffer->font_tags = NULL;
    }

152 153 154 155 156 157
  if (buffer->color_tags)
    {
      g_list_free (buffer->color_tags);
      buffer->color_tags = NULL;
    }

158 159 160 161 162 163
  if (buffer->bg_color_tags)
    {
      g_list_free (buffer->bg_color_tags);
      buffer->bg_color_tags = NULL;
    }

164
  gimp_text_buffer_clear_insert_tags (buffer);
165 166 167 168

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

169 170 171 172 173 174 175 176 177 178
static void
gimp_text_buffer_mark_set (GtkTextBuffer     *buffer,
                           const GtkTextIter *location,
                           GtkTextMark       *mark)
{
  gimp_text_buffer_clear_insert_tags (GIMP_TEXT_BUFFER (buffer));

  GTK_TEXT_BUFFER_CLASS (parent_class)->mark_set (buffer, location, mark);
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

/*  public functions  */

GimpTextBuffer *
gimp_text_buffer_new (void)
{
  return g_object_new (GIMP_TYPE_TEXT_BUFFER, NULL);
}

void
gimp_text_buffer_set_text (GimpTextBuffer *buffer,
                           const gchar    *text)
{
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));

  if (text == NULL)
    text = "";

  gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), text, -1);
198 199

  gimp_text_buffer_clear_insert_tags (buffer);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
}

gchar *
gimp_text_buffer_get_text (GimpTextBuffer *buffer)
{
  GtkTextIter start, end;

  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);

  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);

  return gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer),
                                   &start, &end, TRUE);
}

215 216 217 218 219 220 221 222 223 224
void
gimp_text_buffer_set_markup (GimpTextBuffer *buffer,
                             const gchar    *markup)
{
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));

  gimp_text_buffer_set_text (buffer, NULL);

  if (markup)
    {
225 226 227 228
      GtkTextTagTable *tag_table;
      GtkTextBuffer   *content;
      GtkTextIter      insert;
      GError          *error = NULL;
229

230 231
      tag_table = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (buffer));
      content = gtk_text_buffer_new (tag_table);
232 233

      gtk_text_buffer_get_start_iter (content, &insert);
234 235

      if (! gtk_text_buffer_deserialize (GTK_TEXT_BUFFER (buffer),
236
                                         content,
237
                                         buffer->markup_atom,
238
                                         &insert,
239 240 241 242 243 244
                                         (const guint8 *) markup, -1,
                                         &error))
        {
          g_printerr ("EEK: %s\n", error->message);
          g_clear_error (&error);
        }
245 246 247 248
      else
        {
          GtkTextIter start, end;

249 250
          gimp_text_buffer_post_deserialize (buffer, content);

251 252 253 254 255 256 257 258
          gtk_text_buffer_get_bounds (content, &start, &end);
          gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &insert);

          gtk_text_buffer_insert_range (GTK_TEXT_BUFFER (buffer),
                                        &insert, &start, &end);
        }

      g_object_unref (content);
259
    }
260 261

  gimp_text_buffer_clear_insert_tags (buffer);
262 263 264 265 266
}

gchar *
gimp_text_buffer_get_markup (GimpTextBuffer *buffer)
{
267 268 269 270 271 272
  GtkTextTagTable *tag_table;
  GtkTextBuffer   *content;
  GtkTextIter      insert;
  GtkTextIter      start, end;
  gchar           *markup;
  gsize            length;
273 274 275

  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);

276 277
  tag_table = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (buffer));
  content = gtk_text_buffer_new (tag_table);
278

279
  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);
280 281 282 283
  gtk_text_buffer_get_start_iter (content, &insert);

  gtk_text_buffer_insert_range (content, &insert, &start, &end);

284 285
  gimp_text_buffer_pre_serialize (buffer, content);

286 287 288 289 290 291 292 293 294
  gtk_text_buffer_get_bounds (content, &start, &end);

  markup = (gchar *) gtk_text_buffer_serialize (GTK_TEXT_BUFFER (buffer),
                                                content,
                                                buffer->markup_atom,
                                                &start, &end,
                                                &length);

  g_object_unref (content);
295

296
  return markup;
297 298
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
gboolean
gimp_text_buffer_has_markup (GimpTextBuffer *buffer)
{
  GtkTextIter iter;

  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), FALSE);

  gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &iter);

  do
    {
      GSList *tags = gtk_text_iter_get_tags (&iter);

      if (tags)
        {
          g_slist_free (tags);
          return TRUE;
        }
    }
  while (gtk_text_iter_forward_char (&iter));

  return FALSE;
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336
GtkTextTag *
gimp_text_buffer_get_iter_size (GimpTextBuffer    *buffer,
                                const GtkTextIter *iter,
                                gint              *size)
{
  GList *list;

  for (list = buffer->size_tags; list; list = g_list_next (list))
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
          if (size)
337
            *size = gimp_text_tag_get_size (tag);
338 339 340 341 342 343 344 345 346 347 348

          return tag;
        }
    }

  if (size)
    *size = 0;

  return NULL;
}

349
GtkTextTag *
350 351 352 353 354 355 356 357 358 359 360
gimp_text_buffer_get_size_tag (GimpTextBuffer *buffer,
                               gint            size)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[32];

  for (list = buffer->size_tags; list; list = g_list_next (list))
    {
      tag = list->data;

361
      if (size == gimp_text_tag_get_size (tag))
362 363 364 365 366 367 368
        return tag;
    }

  g_snprintf (name, sizeof (name), "size-%d", size);

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
369
                                    GIMP_TEXT_PROP_NAME_SIZE, size,
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
                                    NULL);

  buffer->size_tags = g_list_prepend (buffer->size_tags, tag);

  return tag;
}

void
gimp_text_buffer_set_size (GimpTextBuffer    *buffer,
                           const GtkTextIter *start,
                           const GtkTextIter *end,
                           gint               size)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->size_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (size != 0)
    {
      GtkTextTag *tag;

      tag = gimp_text_buffer_get_size_tag (buffer, size);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

void
gimp_text_buffer_change_size (GimpTextBuffer    *buffer,
                              const GtkTextIter *start,
                              const GtkTextIter *end,
                              gint               count)
{
  GtkTextIter  iter;
  GtkTextIter  span_start;
  GtkTextIter  span_end;
  GtkTextTag  *span_tag;
  gint         span_size;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  iter       = *start;
  span_start = *start;
  span_tag   = gimp_text_buffer_get_iter_size (buffer, &iter,
                                               &span_size);

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  do
    {
      GtkTextTag *iter_tag;
      gint        iter_size;

      gtk_text_iter_forward_char (&iter);

      iter_tag = gimp_text_buffer_get_iter_size (buffer, &iter,
                                                 &iter_size);

      span_end = iter;

      if (iter_size != span_size ||
          gtk_text_iter_compare (&iter, end) >= 0)
        {
          if (span_size != 0)
            {
              gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                          &span_start, &span_end);
            }

          if ((span_size + count) > 0)
            {
              span_tag = gimp_text_buffer_get_size_tag (buffer,
                                                        span_size + count);

              gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                         &span_start, &span_end);
            }

          span_start = iter;
          span_size  = iter_size;
          span_tag   = iter_tag;
        }

      /* We might have moved too far */
      if (gtk_text_iter_compare (&iter, end) > 0)
        iter = *end;
    }
  while (! gtk_text_iter_equal (&iter, end));

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

483 484 485 486
GtkTextTag *
gimp_text_buffer_get_iter_baseline (GimpTextBuffer    *buffer,
                                    const GtkTextIter *iter,
                                    gint              *baseline)
487 488 489 490 491 492 493 494 495
{
  GList *list;

  for (list = buffer->baseline_tags; list; list = g_list_next (list))
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
496
          if (baseline)
497
            *baseline = gimp_text_tag_get_baseline (tag);
498

499
          return tag;
500 501 502
        }
    }

503 504
  if (baseline)
    *baseline = 0;
505

506
  return NULL;
507 508 509
}

static GtkTextTag *
510 511
gimp_text_buffer_get_baseline_tag (GimpTextBuffer *buffer,
                                   gint            baseline)
512 513 514 515 516 517 518 519 520
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[32];

  for (list = buffer->baseline_tags; list; list = g_list_next (list))
    {
      tag = list->data;

521
      if (baseline == gimp_text_tag_get_baseline (tag))
522 523 524 525 526 527 528
        return tag;
    }

  g_snprintf (name, sizeof (name), "baseline-%d", baseline);

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
529
                                    GIMP_TEXT_PROP_NAME_BASELINE, baseline,
530 531 532 533 534 535 536
                                    NULL);

  buffer->baseline_tags = g_list_prepend (buffer->baseline_tags, tag);

  return tag;
}

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
void
gimp_text_buffer_set_baseline (GimpTextBuffer    *buffer,
                               const GtkTextIter *start,
                               const GtkTextIter *end,
                               gint               baseline)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->baseline_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (baseline != 0)
    {
      GtkTextTag *tag;

      tag = gimp_text_buffer_get_baseline_tag (buffer, baseline);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

573 574 575 576 577 578 579 580 581 582
void
gimp_text_buffer_change_baseline (GimpTextBuffer    *buffer,
                                  const GtkTextIter *start,
                                  const GtkTextIter *end,
                                  gint               count)
{
  GtkTextIter  iter;
  GtkTextIter  span_start;
  GtkTextIter  span_end;
  GtkTextTag  *span_tag;
583
  gint         span_baseline;
584 585 586 587 588

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

589
  if (gtk_text_iter_equal (start, end))
590 591
    return;

592 593 594 595
  iter       = *start;
  span_start = *start;
  span_tag   = gimp_text_buffer_get_iter_baseline (buffer, &iter,
                                                   &span_baseline);
596

597 598
  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

599 600 601
  do
    {
      GtkTextTag *iter_tag;
602
      gint        iter_baseline;
603 604 605

      gtk_text_iter_forward_char (&iter);

606 607
      iter_tag = gimp_text_buffer_get_iter_baseline (buffer, &iter,
                                                     &iter_baseline);
608 609 610 611

      span_end = iter;

      if (iter_baseline != span_baseline ||
612
          gtk_text_iter_compare (&iter, end) >= 0)
613 614 615 616 617 618 619 620 621
        {
          if (span_baseline != 0)
            {
              gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                          &span_start, &span_end);
            }

          if (span_baseline + count != 0)
            {
622 623
              span_tag = gimp_text_buffer_get_baseline_tag (buffer,
                                                            span_baseline + count);
624 625 626 627 628 629 630 631 632

              gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                         &span_start, &span_end);
            }

          span_start    = iter;
          span_baseline = iter_baseline;
          span_tag      = iter_tag;
        }
633 634 635 636

      /* We might have moved too far */
      if (gtk_text_iter_compare (&iter, end) > 0)
        iter = *end;
637 638
    }
  while (! gtk_text_iter_equal (&iter, end));
639 640

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
641 642
}

643
GtkTextTag *
644
gimp_text_buffer_get_iter_kerning (GimpTextBuffer    *buffer,
645
                                   const GtkTextIter *iter,
646
                                   gint              *kerning)
647 648 649
{
  GList *list;

650
  for (list = buffer->kerning_tags; list; list = g_list_next (list))
651 652 653 654 655
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
656
          if (kerning)
657
            *kerning = gimp_text_tag_get_kerning (tag);
658

659
          return tag;
660 661 662
        }
    }

663 664
  if (kerning)
    *kerning = 0;
665

666
  return NULL;
667 668 669
}

static GtkTextTag *
670 671
gimp_text_buffer_get_kerning_tag (GimpTextBuffer *buffer,
                                  gint            kerning)
672 673 674 675 676
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[32];

677
  for (list = buffer->kerning_tags; list; list = g_list_next (list))
678 679 680
    {
      tag = list->data;

681
      if (kerning == gimp_text_tag_get_kerning (tag))
682 683 684
        return tag;
    }

685
  g_snprintf (name, sizeof (name), "kerning-%d", kerning);
686 687 688

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
689
                                    GIMP_TEXT_PROP_NAME_KERNING, kerning,
690 691
                                    NULL);

692
  buffer->kerning_tags = g_list_prepend (buffer->kerning_tags, tag);
693 694 695 696

  return tag;
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
void
gimp_text_buffer_set_kerning (GimpTextBuffer    *buffer,
                              const GtkTextIter *start,
                              const GtkTextIter *end,
                              gint               kerning)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->kerning_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (kerning != 0)
    {
      GtkTextTag *tag;

      tag = gimp_text_buffer_get_kerning_tag (buffer, kerning);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

733
void
734
gimp_text_buffer_change_kerning (GimpTextBuffer    *buffer,
735 736 737 738
                                 const GtkTextIter *start,
                                 const GtkTextIter *end,
                                 gint               count)
{
739 740 741 742
  GtkTextIter  iter;
  GtkTextIter  span_start;
  GtkTextIter  span_end;
  GtkTextTag  *span_tag;
743
  gint         span_kerning;
744

745 746 747
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);
748

749 750 751
  if (gtk_text_iter_equal (start, end))
    return;

752 753
  iter       = *start;
  span_start = *start;
754 755
  span_tag   = gimp_text_buffer_get_iter_kerning (buffer, &iter,
                                                  &span_kerning);
756

757 758
  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

759 760 761
  do
    {
      GtkTextTag *iter_tag;
762
      gint        iter_kerning;
763 764 765

      gtk_text_iter_forward_char (&iter);

766 767
      iter_tag = gimp_text_buffer_get_iter_kerning (buffer, &iter,
                                                    &iter_kerning);
768 769 770

      span_end = iter;

771
      if (iter_kerning != span_kerning ||
772 773
          gtk_text_iter_compare (&iter, end) >= 0)
        {
774
          if (span_kerning != 0)
775 776 777 778 779
            {
              gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                          &span_start, &span_end);
            }

780
          if (span_kerning + count != 0)
781
            {
782 783
              span_tag = gimp_text_buffer_get_kerning_tag (buffer,
                                                           span_kerning + count);
784 785 786 787 788 789

              gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                         &span_start, &span_end);
            }

          span_start   = iter;
790
          span_kerning = iter_kerning;
791 792 793 794 795 796 797 798
          span_tag     = iter_tag;
        }

      /* We might have moved too far */
      if (gtk_text_iter_compare (&iter, end) > 0)
        iter = *end;
    }
  while (! gtk_text_iter_equal (&iter, end));
799 800

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
801 802
}

803 804 805 806
GtkTextTag *
gimp_text_buffer_get_iter_font (GimpTextBuffer     *buffer,
                                const GtkTextIter  *iter,
                                gchar             **font)
807 808 809 810 811 812 813 814 815
{
  GList *list;

  for (list = buffer->font_tags; list; list = g_list_next (list))
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
816
          if (font)
817
            *font = gimp_text_tag_get_font (tag);
818

819
          return tag;
820 821 822
        }
    }

823 824
  if (font)
    *font = NULL;
825 826 827 828

  return NULL;
}

829
GtkTextTag *
830 831 832 833 834 835
gimp_text_buffer_get_font_tag (GimpTextBuffer *buffer,
                               const gchar    *font)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];
836 837 838 839
  PangoFontDescription *pfd = pango_font_description_from_string (font);
  char *description = pango_font_description_to_string (pfd);

  pango_font_description_free (pfd);
840 841 842 843 844 845 846

  for (list = buffer->font_tags; list; list = g_list_next (list))
    {
      gchar *tag_font;

      tag = list->data;

847
      tag_font = gimp_text_tag_get_font (tag);
848

849
      if (! strcmp (description, tag_font))
850 851
        {
          g_free (tag_font);
852
          g_free (description);
853 854 855 856 857 858
          return tag;
        }

      g_free (tag_font);
    }

859
  g_snprintf (name, sizeof (name), "font-%s", description);
860 861 862

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
863
                                    "font", description,
864
                                    NULL);
865
  g_free (description);
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
  buffer->font_tags = g_list_prepend (buffer->font_tags, tag);

  return tag;
}

void
gimp_text_buffer_set_font (GimpTextBuffer    *buffer,
                           const GtkTextIter *start,
                           const GtkTextIter *end,
                           const gchar       *font)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->font_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (font)
    {
      GtkTextTag *tag = gimp_text_buffer_get_font_tag (buffer, font);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
GtkTextTag *
gimp_text_buffer_get_iter_color (GimpTextBuffer    *buffer,
                                 const GtkTextIter *iter,
                                 GimpRGB           *color)
{
  GList *list;

  for (list = buffer->color_tags; list; list = g_list_next (list))
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
          if (color)
            gimp_text_tag_get_color (tag, color);

          return tag;
        }
    }

  return NULL;
}

928
GtkTextTag *
929 930 931 932 933 934 935
gimp_text_buffer_get_color_tag (GimpTextBuffer *buffer,
                                const GimpRGB  *color)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];
  GdkColor    gdk_color;
936 937 938
  guchar      r, g, b;

  gimp_rgb_get_uchar (color, &r, &g, &b);
939 940 941 942

  for (list = buffer->color_tags; list; list = g_list_next (list))
    {
      GimpRGB tag_color;
943
      guchar  tag_r, tag_g, tag_b;
944 945 946 947 948

      tag = list->data;

      gimp_text_tag_get_color (tag, &tag_color);

949 950
      gimp_rgb_get_uchar (&tag_color, &tag_r, &tag_g, &tag_b);

951
      /* Do not compare the alpha channel, since it's unused */
952 953 954
      if (tag_r == r &&
          tag_g == g &&
          tag_b == b)
955 956 957 958 959
        {
          return tag;
        }
    }

960 961
  g_snprintf (name, sizeof (name), "color-#%02x%02x%02x",
              r, g, b);
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

  gimp_rgb_get_gdk_color (color, &gdk_color);

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
                                    "foreground-gdk", &gdk_color,
                                    "foreground-set", TRUE,
                                    NULL);

  buffer->color_tags = g_list_prepend (buffer->color_tags, tag);

  return tag;
}

void
gimp_text_buffer_set_color (GimpTextBuffer    *buffer,
                            const GtkTextIter *start,
                            const GtkTextIter *end,
                            const GimpRGB     *color)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->color_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (color)
    {
      GtkTextTag *tag = gimp_text_buffer_get_color_tag (buffer, color);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
GtkTextTag *
gimp_text_buffer_get_bg_color_tag (GimpTextBuffer *buffer,
                                   const GimpRGB  *color)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];
  GdkColor    gdk_color;
  guchar      r, g, b;

  gimp_rgb_get_uchar (color, &r, &g, &b);

  for (list = buffer->bg_color_tags; list; list = g_list_next (list))
    {
      GimpRGB tag_color;
      guchar  tag_r, tag_g, tag_b;

      tag = list->data;

      gimp_text_tag_get_bg_color (tag, &tag_color);

      gimp_rgb_get_uchar (&tag_color, &tag_r, &tag_g, &tag_b);

      /* Do not compare the alpha channel, since it's unused */
      if (tag_r == r &&
          tag_g == g &&
          tag_b == b)
        {
          return tag;
        }
    }

  g_snprintf (name, sizeof (name), "bg-color-#%02x%02x%02x",
              r, g, b);

  gimp_rgb_get_gdk_color (color, &gdk_color);

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
                                    "background-gdk", &gdk_color,
                                    "background-set", TRUE,
                                    NULL);

  buffer->bg_color_tags = g_list_prepend (buffer->bg_color_tags, tag);

  return tag;
}

void
gimp_text_buffer_set_bg_color (GimpTextBuffer    *buffer,
                               const GtkTextIter *start,
                               const GtkTextIter *end,
                               const GimpRGB     *color)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->bg_color_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (color)
    {
      GtkTextTag *tag = gimp_text_buffer_get_bg_color_tag (buffer, color);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

1092 1093 1094 1095 1096 1097
/*  Pango markup attribute names  */

#define GIMP_TEXT_ATTR_NAME_SIZE     "size"
#define GIMP_TEXT_ATTR_NAME_BASELINE "rise"
#define GIMP_TEXT_ATTR_NAME_KERNING  "letter_spacing"
#define GIMP_TEXT_ATTR_NAME_FONT     "font"
1098
#define GIMP_TEXT_ATTR_NAME_COLOR    "foreground"
1099
#define GIMP_TEXT_ATTR_NAME_BG_COLOR "background"
1100

1101
const gchar *
1102 1103 1104 1105
gimp_text_buffer_tag_to_name (GimpTextBuffer  *buffer,
                              GtkTextTag      *tag,
                              const gchar    **attribute,
                              gchar          **value)
1106 1107 1108 1109
{
  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (GTK_IS_TEXT_TAG (tag), NULL);

1110 1111 1112 1113 1114 1115
  if (attribute)
    *attribute = NULL;

  if (value)
    *value = NULL;

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
  if (tag == buffer->bold_tag)
    {
      return "b";
    }
  else if (tag == buffer->italic_tag)
    {
      return "i";
    }
  else if (tag == buffer->underline_tag)
    {
      return "u";
    }
  else if (tag == buffer->strikethrough_tag)
    {
      return "s";
    }
1132 1133 1134
  else if (g_list_find (buffer->size_tags, tag))
    {
      if (attribute)
1135
        *attribute = GIMP_TEXT_ATTR_NAME_SIZE;
1136 1137

      if (value)
1138
        *value = g_strdup_printf ("%d", gimp_text_tag_get_size (tag));
1139 1140 1141

      return "span";
    }
1142 1143 1144
  else if (g_list_find (buffer->baseline_tags, tag))
    {
      if (attribute)
1145
        *attribute = GIMP_TEXT_ATTR_NAME_BASELINE;
1146 1147

      if (value)
1148
        *value = g_strdup_printf ("%d", gimp_text_tag_get_baseline (tag));
1149 1150 1151

      return "span";
    }
1152
  else if (g_list_find (buffer->kerning_tags, tag))
1153 1154
    {
      if (attribute)
1155
        *attribute = GIMP_TEXT_ATTR_NAME_KERNING;
1156 1157

      if (value)
1158
        *value = g_strdup_printf ("%d", gimp_text_tag_get_kerning (tag));
1159 1160 1161

      return "span";
    }
1162 1163 1164
  else if (g_list_find (buffer->font_tags, tag))
    {
      if (attribute)
1165
        *attribute = GIMP_TEXT_ATTR_NAME_FONT;
1166 1167

      if (value)
1168
        *value = gimp_text_tag_get_font (tag);
1169 1170 1171

      return "span";
    }
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
  else if (g_list_find (buffer->color_tags, tag))
    {
      if (attribute)
        *attribute = GIMP_TEXT_ATTR_NAME_COLOR;

      if (value)
        {
          GimpRGB color;
          guchar  r, g, b;

          gimp_text_tag_get_color (tag, &color);
          gimp_rgb_get_uchar (&color, &r, &g, &b);

          *value = g_strdup_printf ("#%02x%02x%02x", r, g, b);
        }

      return "span";
    }
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
  else if (g_list_find (buffer->bg_color_tags, tag))
    {
      if (attribute)
        *attribute = GIMP_TEXT_ATTR_NAME_BG_COLOR;

      if (value)
        {
          GimpRGB color;
          guchar  r, g, b;

          gimp_text_tag_get_bg_color (tag, &color);
          gimp_rgb_get_uchar (&color, &r, &g, &b);

          *value = g_strdup_printf ("#%02x%02x%02x", r, g, b);
        }

      return "span";
    }
1208 1209 1210 1211 1212 1213

  return NULL;
}

GtkTextTag *
gimp_text_buffer_name_to_tag (GimpTextBuffer *buffer,
1214 1215 1216
                              const gchar    *name,
                              const gchar    *attribute,
                              const gchar    *value)
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
{
  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  if (! strcmp (name, "b"))
    {
      return buffer->bold_tag;
    }
  else if (! strcmp (name, "i"))
    {
      return buffer->italic_tag;
    }
  else if (! strcmp (name, "u"))
    {
      return buffer->underline_tag;
    }
  else if (! strcmp (name, "s"))
    {
      return buffer->strikethrough_tag;
    }
1237 1238 1239
  else if (! strcmp (name, "span") &&
           attribute != NULL       &&
           value     != NULL)
1240
    {
1241
      if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_SIZE))
1242 1243 1244
        {
          return gimp_text_buffer_get_size_tag (buffer, atoi (value));
        }
1245
      else if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_BASELINE))
1246
        {
1247
          return gimp_text_buffer_get_baseline_tag (buffer, atoi (value));
1248
        }
1249
      else if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_KERNING))
1250
        {
1251
          return gimp_text_buffer_get_kerning_tag (buffer, atoi (value));
1252
        }
1253
      else if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_FONT))
1254 1255
        {
          return gimp_text_buffer_get_font_tag (buffer, value);
1256
        }
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
      else if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_COLOR))
        {
          GimpRGB color;
          guint   r, g, b;

          sscanf (value, "#%02x%02x%02x", &r, &g, &b);

          gimp_rgb_set_uchar (&color, r, g, b);

          return gimp_text_buffer_get_color_tag (buffer, &color);
        }
1268
    }
1269 1270 1271 1272

  return NULL;
}

1273 1274
void
gimp_text_buffer_set_insert_tags (GimpTextBuffer *buffer,
1275 1276
                                  GList          *insert_tags,
                                  GList          *remove_tags)