/gtkhtml-4.5.3/gtkhtml/htmltext.c
C | 4308 lines | 3399 code | 682 blank | 227 comment | 686 complexity | 022039b99966830c5f01726ab7c42ec2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
Large files files are truncated, but you can click here to view the full file
- /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
- /* This file is part of the GtkHTML library.
- *
- * Copyright (C) 1997 Martin Jones (mjones@kde.org)
- * Copyright (C) 1997 Torben Weis (weis@kde.org)
- * Copyright (C) 1999, 2000 Helix Code, Inc.
- *
- * 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; see the file COPYING.LIB. If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
- #include <config.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <regex.h>
- #include <math.h>
- #define PANGO_ENABLE_BACKEND /* Required to get PANGO_GLYPH_EMPTY */
- #include <pango/pango.h>
- #include "htmltext.h"
- #include "htmlcolor.h"
- #include "htmlcolorset.h"
- #include "htmlcluealigned.h"
- #include "htmlclueflow.h"
- #include "htmlcursor.h"
- #include "htmlgdkpainter.h"
- #include "htmlplainpainter.h"
- #include "htmlprinter.h"
- #include "htmlengine.h"
- #include "htmlengine-edit.h"
- #include "htmlengine-edit-cut-and-paste.h"
- #include "htmlengine-save.h"
- #include "htmlentity.h"
- #include "htmlsettings.h"
- #include "htmltextslave.h"
- #include "htmlundo.h"
- HTMLTextClass html_text_class;
- static HTMLObjectClass *parent_class = NULL;
- static const PangoAttrClass html_pango_attr_font_size_klass;
- #define HT_CLASS(x) HTML_TEXT_CLASS (HTML_OBJECT (x)->klass)
- #ifdef PANGO_GLYPH_EMPTY
- #define EMPTY_GLYPH PANGO_GLYPH_EMPTY
- #else
- #define EMPTY_GLYPH 0
- #endif
- static SpellError * spell_error_new (guint off, guint len);
- static void spell_error_destroy (SpellError *se);
- static void move_spell_errors (GList *spell_errors, guint offset, gint delta);
- static GList * remove_spell_errors (GList *spell_errors, guint offset, guint len);
- static GList * merge_spell_errors (GList *se1, GList *se2);
- static void remove_text_slaves (HTMLObject *self);
- /* void
- debug_spell_errors (GList *se)
- {
- for (; se; se = se->next)
- printf ("SE: %4d, %4d\n", ((SpellError *) se->data)->off, ((SpellError *) se->data)->len);
- } */
- static inline gboolean
- is_in_the_save_cluev (HTMLObject *text,
- HTMLObject *o)
- {
- return html_object_nth_parent (o, 2) == html_object_nth_parent (text, 2);
- }
- /* HTMLObject methods. */
- HTMLTextPangoInfo *
- html_text_pango_info_new (gint n)
- {
- HTMLTextPangoInfo *pi;
- pi = g_new (HTMLTextPangoInfo, 1);
- pi->n = n;
- pi->entries = g_new0 (HTMLTextPangoInfoEntry, n);
- pi->attrs = NULL;
- pi->have_font = FALSE;
- pi->font_style = GTK_HTML_FONT_STYLE_DEFAULT;
- pi->face = NULL;
- return pi;
- }
- void
- html_text_pango_info_destroy (HTMLTextPangoInfo *pi)
- {
- gint i;
- for (i = 0; i < pi->n; i++) {
- pango_item_free (pi->entries[i].glyph_item.item);
- if (pi->entries[i].glyph_item.glyphs)
- pango_glyph_string_free (pi->entries[i].glyph_item.glyphs);
- g_free (pi->entries[i].widths);
- }
- g_free (pi->entries);
- g_free (pi->attrs);
- g_free (pi->face);
- g_free (pi);
- }
- static void
- pango_info_destroy (HTMLText *text)
- {
- if (text->pi) {
- html_text_pango_info_destroy (text->pi);
- text->pi = NULL;
- }
- }
- static void
- free_links (GSList *list)
- {
- if (list) {
- GSList *l;
- for (l = list; l; l = l->next)
- html_link_free ((Link *) l->data);
- g_slist_free (list);
- }
- }
- void
- html_text_free_attrs (GSList *attrs)
- {
- if (attrs) {
- GSList *l;
- for (l = attrs; l; l = l->next)
- pango_attribute_destroy ((PangoAttribute *) l->data);
- g_slist_free (attrs);
- }
- }
- static void
- copy (HTMLObject *s,
- HTMLObject *d)
- {
- HTMLText *src = HTML_TEXT (s);
- HTMLText *dest = HTML_TEXT (d);
- GList *cur;
- GSList *csl;
- (* HTML_OBJECT_CLASS (parent_class)->copy) (s, d);
- dest->text = g_strdup (src->text);
- dest->text_len = src->text_len;
- dest->text_bytes = src->text_bytes;
- dest->font_style = src->font_style;
- dest->face = g_strdup (src->face);
- dest->color = src->color;
- dest->select_start = 0;
- dest->select_length = 0;
- dest->attr_list = pango_attr_list_copy (src->attr_list);
- dest->extra_attr_list = src->extra_attr_list ? pango_attr_list_copy (src->extra_attr_list) : NULL;
- html_color_ref (dest->color);
- dest->spell_errors = g_list_copy (src->spell_errors);
- cur = dest->spell_errors;
- while (cur) {
- SpellError *se = (SpellError *) cur->data;
- cur->data = spell_error_new (se->off, se->len);
- cur = cur->next;
- }
- dest->links = g_slist_copy (src->links);
- for (csl = dest->links; csl; csl = csl->next)
- csl->data = html_link_dup ((Link *) csl->data);
- dest->pi = NULL;
- dest->direction = src->direction;
- }
- /* static void
- debug_word_width (HTMLText *t)
- {
- guint i;
- *
- printf ("words: %d | ", t->words);
- for (i = 0; i < t->words; i++)
- printf ("%d ", t->word_width [i]);
- printf ("\n");
- }
- *
- static void
- word_get_position (HTMLText *text,
- * guint off,
- * guint *word_out,
- * guint *left_out,
- * guint *right_out)
- {
- const gchar *s, *ls;
- guint coff, loff;
- *
- coff = 0;
- *word_out = 0;
- s = text->text;
- do {
- ls = s;
- loff = coff;
- s = strchr (s, ' ');
- coff += s ? g_utf8_pointer_to_offset (ls, s) : g_utf8_strlen (ls, -1);
- (*word_out) ++;
- if (s)
- s++;
- } while (s && coff < off);
- *
- *left_out = off - loff;
- *right_out = coff - off;
- *
- printf ("get position w: %d l: %d r: %d\n", *word_out, *left_out, *right_out);
- } */
- static gboolean
- cut_attr_list_filter (PangoAttribute *attr,
- gpointer data)
- {
- PangoAttribute *range = (PangoAttribute *) data;
- gint delta;
- if (attr->start_index >= range->start_index && attr->end_index <= range->end_index)
- return TRUE;
- delta = range->end_index - range->start_index;
- if (attr->start_index > range->end_index) {
- attr->start_index -= delta;
- attr->end_index -= delta;
- } else if (attr->start_index > range->start_index) {
- attr->start_index = range->start_index;
- attr->end_index -= delta;
- if (attr->end_index <= attr->start_index)
- return TRUE;
- } else if (attr->end_index >= range->end_index)
- attr->end_index -= delta;
- else if (attr->end_index >= range->start_index)
- attr->end_index = range->start_index;
- return FALSE;
- }
- static void
- cut_attr_list_list (PangoAttrList *attr_list,
- gint begin_index,
- gint end_index)
- {
- PangoAttrList *removed;
- PangoAttribute range;
- range.start_index = begin_index;
- range.end_index = end_index;
- removed = pango_attr_list_filter (attr_list, cut_attr_list_filter, &range);
- if (removed)
- pango_attr_list_unref (removed);
- }
- static void
- cut_attr_list (HTMLText *text,
- gint begin_index,
- gint end_index)
- {
- cut_attr_list_list (text->attr_list, begin_index, end_index);
- if (text->extra_attr_list)
- cut_attr_list_list (text->extra_attr_list, begin_index, end_index);
- }
- static void
- cut_links_full (HTMLText *text,
- gint start_offset,
- gint end_offset,
- gint start_index,
- gint end_index,
- gint shift_offset,
- gint shift_index)
- {
- GSList *l, *next;
- Link *link;
- for (l = text->links; l; l = next) {
- next = l->next;
- link = (Link *) l->data;
- if (start_offset <= link->start_offset && link->end_offset <= end_offset) {
- html_link_free (link);
- text->links = g_slist_delete_link (text->links, l);
- } else if (end_offset <= link->start_offset) {
- link->start_offset -= shift_offset;
- link->start_index -= shift_index;
- link->end_offset -= shift_offset;
- link->end_index -= shift_index;
- } else if (start_offset <= link->start_offset) {
- link->start_offset = end_offset - shift_offset;
- link->end_offset -= shift_offset;
- link->start_index = end_index - shift_index;
- link->end_index -= shift_index;
- } else if (end_offset <= link->end_offset) {
- if (shift_offset > 0) {
- link->end_offset -= shift_offset;
- link->end_index -= shift_index;
- } else {
- if (link->end_offset == end_offset) {
- link->end_offset = start_offset;
- link->end_index = start_index;
- } else if (link->start_offset == start_offset) {
- link->start_offset = end_offset;
- link->start_index = end_index;
- } else {
- Link *dup = html_link_dup (link);
- link->start_offset = end_offset;
- link->start_index = end_index;
- dup->end_offset = start_offset;
- dup->end_index = start_index;
- l = g_slist_prepend (l, dup);
- next = l->next->next;
- }
- }
- } else if (start_offset < link->end_offset) {
- link->end_offset = start_offset;
- link->end_index = start_index;
- }
- }
- }
- static void
- cut_links (HTMLText *text,
- gint start_offset,
- gint end_offset,
- gint start_index,
- gint end_index)
- {
- cut_links_full (text, start_offset, end_offset, start_index, end_index, end_offset - start_offset, end_index - start_index);
- }
- HTMLObject *
- html_text_op_copy_helper (HTMLText *text,
- GList *from,
- GList *to,
- guint *len)
- {
- HTMLObject *rv;
- HTMLText *rvt;
- gchar *tail, *nt;
- gint begin, end, begin_index, end_index;
- begin = (from) ? GPOINTER_TO_INT (from->data) : 0;
- end = (to) ? GPOINTER_TO_INT (to->data) : text->text_len;
- tail = html_text_get_text (text, end);
- begin_index = html_text_get_index (text, begin);
- end_index = tail - text->text;
- *len += end - begin;
- rv = html_object_dup (HTML_OBJECT (text));
- rvt = HTML_TEXT (rv);
- rvt->text_len = end - begin;
- rvt->text_bytes = end_index - begin_index;
- nt = g_strndup (rvt->text + begin_index, rvt->text_bytes);
- g_free (rvt->text);
- rvt->text = nt;
- rvt->spell_errors = remove_spell_errors (rvt->spell_errors, 0, begin);
- rvt->spell_errors = remove_spell_errors (rvt->spell_errors, end, text->text_len - end);
- if (end_index < text->text_bytes)
- cut_attr_list (rvt, end_index, text->text_bytes);
- if (begin_index > 0)
- cut_attr_list (rvt, 0, begin_index);
- if (end < text->text_len)
- cut_links (rvt, end, text->text_len, end_index, text->text_bytes);
- if (begin > 0)
- cut_links (rvt, 0, begin, 0, begin_index);
- return rv;
- }
- HTMLObject *
- html_text_op_cut_helper (HTMLText *text,
- HTMLEngine *e,
- GList *from,
- GList *to,
- GList *left,
- GList *right,
- guint *len)
- {
- HTMLObject *rv;
- HTMLText *rvt;
- gint begin, end;
- begin = (from) ? GPOINTER_TO_INT (from->data) : 0;
- end = (to) ? GPOINTER_TO_INT (to->data) : text->text_len;
- g_assert (begin <= end);
- g_assert (end <= text->text_len);
- /* printf ("before cut '%s'\n", text->text);
- * debug_word_width (text); */
- remove_text_slaves (HTML_OBJECT (text));
- if (!html_object_could_remove_whole (HTML_OBJECT (text), from, to, left, right) || begin || end < text->text_len) {
- gchar *nt, *tail;
- gint begin_index, end_index;
- if (begin == end)
- return HTML_OBJECT (html_text_new_with_len ("", 0, text->font_style, text->color));
- rv = html_object_dup (HTML_OBJECT (text));
- rvt = HTML_TEXT (rv);
- tail = html_text_get_text (text, end);
- begin_index = html_text_get_index (text, begin);
- end_index = tail - text->text;
- text->text_bytes -= tail - (text->text + begin_index);
- text->text[begin_index] = 0;
- cut_attr_list (text, begin_index, end_index);
- if (end_index < rvt->text_bytes)
- cut_attr_list (rvt, end_index, rvt->text_bytes);
- if (begin_index > 0)
- cut_attr_list (rvt, 0, begin_index);
- cut_links (text, begin, end, begin_index, end_index);
- if (end < rvt->text_len)
- cut_links (rvt, end, rvt->text_len, end_index, rvt->text_bytes);
- if (begin > 0)
- cut_links (rvt, 0, begin, 0, begin_index);
- nt = g_strconcat (text->text, tail, NULL);
- g_free (text->text);
- rvt->spell_errors = remove_spell_errors (rvt->spell_errors, 0, begin);
- rvt->spell_errors = remove_spell_errors (rvt->spell_errors, end, text->text_len - end);
- move_spell_errors (rvt->spell_errors, begin, -begin);
- text->text = nt;
- text->text_len -= end - begin;
- *len += end - begin;
- nt = g_strndup (rvt->text + begin_index, end_index - begin_index);
- g_free (rvt->text);
- rvt->text = nt;
- rvt->text_len = end - begin;
- rvt->text_bytes = end_index - begin_index;
- text->spell_errors = remove_spell_errors (text->spell_errors, begin, end - begin);
- move_spell_errors (text->spell_errors, end, - (end - begin));
- html_text_convert_nbsp (text, TRUE);
- html_text_convert_nbsp (rvt, TRUE);
- pango_info_destroy (text);
- } else {
- text->spell_errors = remove_spell_errors (text->spell_errors, 0, text->text_len);
- html_object_move_cursor_before_remove (HTML_OBJECT (text), e);
- html_object_change_set (HTML_OBJECT (text)->parent, HTML_CHANGE_ALL_CALC);
- /* force parent redraw */
- HTML_OBJECT (text)->parent->width = 0;
- html_object_remove_child (HTML_OBJECT (text)->parent, HTML_OBJECT (text));
- rv = HTML_OBJECT (text);
- *len += text->text_len;
- }
- html_object_change_set (HTML_OBJECT (text), HTML_CHANGE_ALL_CALC);
- /* printf ("after cut '%s'\n", text->text);
- * debug_word_width (text); */
- return rv;
- }
- static HTMLObject *
- op_copy (HTMLObject *self,
- HTMLObject *parent,
- HTMLEngine *e,
- GList *from,
- GList *to,
- guint *len)
- {
- return html_text_op_copy_helper (HTML_TEXT (self), from, to, len);
- }
- static HTMLObject *
- op_cut (HTMLObject *self,
- HTMLEngine *e,
- GList *from,
- GList *to,
- GList *left,
- GList *right,
- guint *len)
- {
- return html_text_op_cut_helper (HTML_TEXT (self), e, from, to, left, right, len);
- }
- static void
- merge_links (HTMLText *t1,
- HTMLText *t2)
- {
- Link *tail, *head;
- GSList *l;
- if (t2->links) {
- for (l = t2->links; l; l = l->next) {
- Link *link = (Link *) l->data;
- link->start_offset += t1->text_len;
- link->start_index += t1->text_bytes;
- link->end_offset += t1->text_len;
- link->end_index += t1->text_bytes;
- }
- if (t1->links) {
- head = (Link *) t1->links->data;
- tail = (Link *) g_slist_last (t2->links)->data;
- if (tail->start_offset == head->end_offset && html_link_equal (head, tail)) {
- tail->start_offset = head->start_offset;
- tail->start_index = head->start_index;
- html_link_free (head);
- t1->links = g_slist_delete_link (t1->links, t1->links);
- }
- }
- t1->links = g_slist_concat (t2->links, t1->links);
- t2->links = NULL;
- }
- }
- static gboolean
- object_merge (HTMLObject *self,
- HTMLObject *with,
- HTMLEngine *e,
- GList **left,
- GList **right,
- HTMLCursor *cursor)
- {
- HTMLText *t1, *t2;
- gchar *to_free;
- t1 = HTML_TEXT (self);
- t2 = HTML_TEXT (with);
- /* printf ("merge '%s' '%s'\n", t1->text, t2->text); */
- /* merge_word_width (t1, t2, e->painter); */
- if (e->cursor->object == with) {
- e->cursor->object = self;
- e->cursor->offset += t1->text_len;
- }
- /* printf ("--- before merge\n");
- * debug_spell_errors (t1->spell_errors);
- * printf ("---\n");
- * debug_spell_errors (t2->spell_errors);
- * printf ("---\n");
- */
- move_spell_errors (t2->spell_errors, 0, t1->text_len);
- t1->spell_errors = merge_spell_errors (t1->spell_errors, t2->spell_errors);
- t2->spell_errors = NULL;
- pango_attr_list_splice (t1->attr_list, t2->attr_list, t1->text_bytes, t2->text_bytes);
- if (t2->extra_attr_list) {
- if (!t1->extra_attr_list)
- t1->extra_attr_list = pango_attr_list_new ();
- pango_attr_list_splice (t1->extra_attr_list, t2->extra_attr_list, t1->text_bytes, t2->text_bytes);
- }
- merge_links (t1, t2);
- to_free = t1->text;
- t1->text = g_strconcat (t1->text, t2->text, NULL);
- t1->text_len += t2->text_len;
- t1->text_bytes += t2->text_bytes;
- g_free (to_free);
- html_text_convert_nbsp (t1, TRUE);
- html_object_change_set (self, HTML_CHANGE_ALL_CALC);
- pango_info_destroy (t1);
- pango_info_destroy (t2);
- /* html_text_request_word_width (t1, e->painter); */
- /* printf ("merged '%s'\n", t1->text);
- * printf ("--- after merge\n");
- * debug_spell_errors (t1->spell_errors);
- * printf ("---\n"); */
- return TRUE;
- }
- static gboolean
- split_attrs_filter_head (PangoAttribute *attr,
- gpointer data)
- {
- gint index = GPOINTER_TO_INT (data);
- if (attr->start_index >= index)
- return TRUE;
- else if (attr->end_index > index)
- attr->end_index = index;
- return FALSE;
- }
- static gboolean
- split_attrs_filter_tail (PangoAttribute *attr,
- gpointer data)
- {
- gint index = GPOINTER_TO_INT (data);
- if (attr->end_index <= index)
- return TRUE;
- if (attr->start_index > index)
- attr->start_index -= index;
- else
- attr->start_index = 0;
- attr->end_index -= index;
- return FALSE;
- }
- static void
- split_attrs (HTMLText *t1,
- HTMLText *t2,
- gint index)
- {
- PangoAttrList *delete;
- delete = pango_attr_list_filter (t1->attr_list, split_attrs_filter_head, GINT_TO_POINTER (index));
- if (delete)
- pango_attr_list_unref (delete);
- if (t1->extra_attr_list) {
- delete = pango_attr_list_filter (t1->extra_attr_list, split_attrs_filter_head, GINT_TO_POINTER (index));
- if (delete)
- pango_attr_list_unref (delete);
- }
- delete = pango_attr_list_filter (t2->attr_list, split_attrs_filter_tail, GINT_TO_POINTER (index));
- if (delete)
- pango_attr_list_unref (delete);
- if (t2->extra_attr_list) {
- delete = pango_attr_list_filter (t2->extra_attr_list, split_attrs_filter_tail, GINT_TO_POINTER (index));
- if (delete)
- pango_attr_list_unref (delete);
- }
- }
- static void
- split_links (HTMLText *t1,
- HTMLText *t2,
- gint offset,
- gint index)
- {
- GSList *l, *prev = NULL;
- for (l = t1->links; l; l = l->next) {
- Link *link = (Link *) l->data;
- if (link->start_offset < offset) {
- if (link->end_offset > offset) {
- link->end_offset = offset;
- link->end_index = index;
- }
- if (prev) {
- prev->next = NULL;
- free_links (t1->links);
- }
- t1->links = l;
- break;
- }
- prev = l;
- if (!l->next) {
- free_links (t1->links);
- t1->links = NULL;
- break;
- }
- }
- prev = NULL;
- for (l = t2->links; l; l = l->next) {
- Link *link = (Link *) l->data;
- if (link->start_offset < offset) {
- if (link->end_offset > offset) {
- link->start_offset = offset;
- link->start_index = index;
- prev = l;
- l = l->next;
- }
- if (prev) {
- prev->next = NULL;
- free_links (l);
- } else {
- free_links (t2->links);
- t2->links = NULL;
- }
- break;
- }
- prev = l;
- }
- for (l = t2->links; l; l = l->next) {
- Link *link = (Link *) l->data;
- link->start_offset -= offset;
- link->start_index -= index;
- link->end_offset -= offset;
- link->end_index -= index;
- }
- }
- static void
- object_split (HTMLObject *self,
- HTMLEngine *e,
- HTMLObject *child,
- gint offset,
- gint level,
- GList **left,
- GList **right)
- {
- HTMLObject *dup, *prev;
- HTMLText *t1, *t2;
- gchar *tt;
- gint split_index;
- g_assert (self->parent);
- html_clue_remove_text_slaves (HTML_CLUE (self->parent));
- t1 = HTML_TEXT (self);
- dup = html_object_dup (self);
- tt = t1->text;
- split_index = html_text_get_index (t1, offset);
- t1->text = g_strndup (tt, split_index);
- t1->text_len = offset;
- t1->text_bytes = split_index;
- g_free (tt);
- html_text_convert_nbsp (t1, TRUE);
- t2 = HTML_TEXT (dup);
- tt = t2->text;
- t2->text = html_text_get_text (t2, offset);
- t2->text_len -= offset;
- t2->text_bytes -= split_index;
- split_attrs (t1, t2, split_index);
- split_links (t1, t2, offset, split_index);
- if (!html_text_convert_nbsp (t2, FALSE))
- t2->text = g_strdup (t2->text);
- g_free (tt);
- html_clue_append_after (HTML_CLUE (self->parent), dup, self);
- prev = self->prev;
- if (t1->text_len == 0 && prev && html_object_merge (prev, self, e, NULL, NULL, NULL))
- self = prev;
- if (t2->text_len == 0 && dup->next)
- html_object_merge (dup, dup->next, e, NULL, NULL, NULL);
- /* printf ("--- before split offset %d dup len %d\n", offset, HTML_TEXT (dup)->text_len);
- * debug_spell_errors (HTML_TEXT (self)->spell_errors); */
- HTML_TEXT (self)->spell_errors = remove_spell_errors (HTML_TEXT (self)->spell_errors,
- offset, HTML_TEXT (dup)->text_len);
- HTML_TEXT (dup)->spell_errors = remove_spell_errors (HTML_TEXT (dup)->spell_errors,
- 0, HTML_TEXT (self)->text_len);
- move_spell_errors (HTML_TEXT (dup)->spell_errors, 0, - HTML_TEXT (self)->text_len);
- /* printf ("--- after split\n");
- * printf ("left\n");
- * debug_spell_errors (HTML_TEXT (self)->spell_errors);
- * printf ("right\n");
- * debug_spell_errors (HTML_TEXT (dup)->spell_errors);
- * printf ("---\n");
- */
- *left = g_list_prepend (*left, self);
- *right = g_list_prepend (*right, dup);
- html_object_change_set (self, HTML_CHANGE_ALL_CALC);
- html_object_change_set (dup, HTML_CHANGE_ALL_CALC);
- pango_info_destroy (HTML_TEXT (self));
- level--;
- if (level)
- html_object_split (self->parent, e, dup, 0, level, left, right);
- }
- static gboolean
- html_text_real_calc_size (HTMLObject *self,
- HTMLPainter *painter,
- GList **changed_objs)
- {
- self->width = 0;
- html_object_calc_preferred_width (self, painter);
- return FALSE;
- }
- static const gchar *
- html_utf8_strnchr (const gchar *s,
- gchar c,
- gint len,
- gint *offset)
- {
- const gchar *res = NULL;
- *offset = 0;
- while (s && *s && *offset < len) {
- if (*s == c) {
- res = s;
- break;
- }
- s = g_utf8_next_char (s);
- (*offset) ++;
- }
- return res;
- }
- gint
- html_text_text_line_length (const gchar *text,
- gint *line_offset,
- guint len,
- gint *tabs)
- {
- const gchar *tab, *found_tab;
- gint cl, l, skip, sum_skip;
- /* printf ("lo: %d len: %d t: '%s'\n", line_offset, len, text); */
- if (tabs)
- *tabs = 0;
- l = 0;
- sum_skip = skip = 0;
- tab = text;
- while (tab && (found_tab = html_utf8_strnchr (tab, '\t', len - l, &cl)) && l < len) {
- l += cl;
- if (l >= len)
- break;
- if (*line_offset != -1) {
- *line_offset += cl;
- skip = 8 - (*line_offset % 8);
- }
- tab = found_tab + 1;
- *line_offset += skip;
- if (*line_offset != -1)
- sum_skip += skip - 1;
- l++;
- if (tabs)
- (*tabs) ++;
- }
- if (*line_offset != -1)
- (*line_offset) += len - l;
- /* printf ("ll: %d\n", len + sum_skip); */
- return len + sum_skip;
- }
- static guint
- get_line_length (HTMLObject *self,
- HTMLPainter *p,
- gint line_offset)
- {
- return html_clueflow_tabs (HTML_CLUEFLOW (self->parent), p)
- ? html_text_text_line_length (HTML_TEXT (self)->text, &line_offset, HTML_TEXT (self)->text_len, NULL)
- : HTML_TEXT (self)->text_len;
- }
- gint
- html_text_get_line_offset (HTMLText *text,
- HTMLPainter *painter,
- gint offset)
- {
- gint line_offset = -1;
- if (html_clueflow_tabs (HTML_CLUEFLOW (HTML_OBJECT (text)->parent), painter)) {
- line_offset = html_clueflow_get_line_offset (HTML_CLUEFLOW (HTML_OBJECT (text)->parent),
- painter, HTML_OBJECT (text));
- if (offset) {
- gchar *s = text->text;
- while (offset > 0 && s && *s) {
- if (*s == '\t')
- line_offset += 8 - (line_offset % 8);
- else
- line_offset++;
- s = g_utf8_next_char (s);
- offset--;
- }
- }
- }
- return line_offset;
- }
- gint
- html_text_get_item_index (HTMLText *text,
- HTMLPainter *painter,
- gint offset,
- gint *item_offset)
- {
- HTMLTextPangoInfo *pi = html_text_get_pango_info (text, painter);
- gint idx = 0;
- if (pi->n > 0) {
- while (idx < pi->n - 1 && offset >= pi->entries[idx].glyph_item.item->num_chars) {
- offset -= pi->entries[idx].glyph_item.item->num_chars;
- idx++;
- }
- *item_offset = offset;
- }
- return idx;
- }
- static void
- update_asc_dsc (HTMLPainter *painter,
- PangoItem *item,
- gint *asc,
- gint *dsc)
- {
- PangoFontMetrics *pfm;
- pfm = pango_font_get_metrics (item->analysis.font, item->analysis.language);
- if (asc)
- *asc = MAX (*asc, pango_font_metrics_get_ascent (pfm));
- if (dsc)
- *dsc = MAX (*dsc, pango_font_metrics_get_descent (pfm));
- pango_font_metrics_unref (pfm);
- }
- static void
- html_text_get_attr_list_list (PangoAttrList *get_attrs,
- PangoAttrList *attr_list,
- gint start_index,
- gint end_index)
- {
- PangoAttrIterator *iter = pango_attr_list_get_iterator (attr_list);
- if (iter) {
- do {
- gint begin, end;
- pango_attr_iterator_range (iter, &begin, &end);
- if (MAX (begin, start_index) < MIN (end, end_index)) {
- GSList *c, *l = pango_attr_iterator_get_attrs (iter);
- for (c = l; c; c = c->next) {
- PangoAttribute *attr = (PangoAttribute *) c->data;
- if (attr->start_index < start_index)
- attr->start_index = 0;
- else
- attr->start_index -= start_index;
- if (attr->end_index > end_index)
- attr->end_index = end_index - start_index;
- else
- attr->end_index -= start_index;
- c->data = NULL;
- pango_attr_list_insert (get_attrs, attr);
- }
- g_slist_free (l);
- }
- } while (pango_attr_iterator_next (iter));
- pango_attr_iterator_destroy (iter);
- }
- }
- PangoAttrList *
- html_text_get_attr_list (HTMLText *text,
- gint start_index,
- gint end_index)
- {
- PangoAttrList *attrs = pango_attr_list_new ();
- html_text_get_attr_list_list (attrs, text->attr_list, start_index, end_index);
- if (text->extra_attr_list)
- html_text_get_attr_list_list (attrs, text->extra_attr_list, start_index, end_index);
- return attrs;
- }
- void
- html_text_calc_text_size (HTMLText *t,
- HTMLPainter *painter,
- gint start_byte_offset,
- guint len,
- HTMLTextPangoInfo *pi,
- GList *glyphs,
- gint *line_offset,
- gint *width,
- gint *asc,
- gint *dsc)
- {
- gchar *text = t->text + start_byte_offset;
- html_painter_calc_entries_size (painter, text, len, pi, glyphs,
- line_offset, width, asc, dsc);
- }
- gint
- html_text_calc_part_width (HTMLText *text,
- HTMLPainter *painter,
- gchar *start,
- gint offset,
- gint len,
- gint *asc,
- gint *dsc)
- {
- gint idx, width = 0, line_offset;
- gint ascent = 0, descent = 0; /* Quiet GCC */
- gboolean need_ascent_descent = asc || dsc;
- HTMLTextPangoInfo *pi;
- PangoLanguage *language = NULL;
- PangoFont *font = NULL;
- gchar *s;
- if (offset < 0)
- return 0;
- if (offset + len > text->text_len)
- return 0;
- if (need_ascent_descent) {
- ascent = html_painter_engine_to_pango (painter,
- html_painter_get_space_asc (painter, html_text_get_font_style (text), text->face));
- descent = html_painter_engine_to_pango (painter,
- html_painter_get_space_dsc (painter, html_text_get_font_style (text), text->face));
- }
- if (text->text_len == 0 || len == 0)
- goto out;
- line_offset = html_text_get_line_offset (text, painter, offset);
- if (start == NULL)
- start = html_text_get_text (text, offset);
- s = start;
- pi = html_text_get_pango_info (text, painter);
- idx = html_text_get_item_index (text, painter, offset, &offset);
- if (need_ascent_descent) {
- update_asc_dsc (painter, pi->entries[idx].glyph_item.item, &ascent, &descent);
- font = pi->entries[idx].glyph_item.item->analysis.font;
- language = pi->entries[idx].glyph_item.item->analysis.language;
- }
- while (len > 0) {
- gint old_idx;
- if (*s == '\t') {
- gint skip = 8 - (line_offset % 8);
- width += skip * pi->entries[idx].widths[offset];
- line_offset += skip;
- } else {
- width += pi->entries[idx].widths[offset];
- line_offset++;
- }
- len--;
- old_idx = idx;
- if (html_text_pi_forward (pi, &idx, &offset) && idx != old_idx)
- if (len > 0 && (need_ascent_descent) && (pi->entries[idx].glyph_item.item->analysis.font != font
- || pi->entries[idx].glyph_item.item->analysis.language != language)) {
- update_asc_dsc (painter, pi->entries[idx].glyph_item.item, &ascent, &descent);
- }
- s = g_utf8_next_char (s);
- }
- out:
- if (asc)
- *asc = html_painter_pango_to_engine (painter, ascent);
- if (dsc)
- *dsc = html_painter_pango_to_engine (painter, descent);
- return html_painter_pango_to_engine (painter, width);
- }
- static gint
- calc_preferred_width (HTMLObject *self,
- HTMLPainter *painter)
- {
- HTMLText *text;
- gint width;
- text = HTML_TEXT (self);
- width = html_text_calc_part_width (text, painter, text->text, 0, text->text_len, &self->ascent, &self->descent);
- self->y = self->ascent;
- if (html_clueflow_tabs (HTML_CLUEFLOW (self->parent), painter)) {
- gint line_offset;
- gint tabs;
- line_offset = html_text_get_line_offset (text, painter, 0);
- width += (html_text_text_line_length (text->text, &line_offset, text->text_len, &tabs) - text->text_len)*
- html_painter_get_space_width (painter, html_text_get_font_style (text), text->face);
- }
- return MAX (1, width);
- }
- static void
- remove_text_slaves (HTMLObject *self)
- {
- HTMLObject *next_obj;
- /* Remove existing slaves */
- next_obj = self->next;
- while (next_obj != NULL
- && (HTML_OBJECT_TYPE (next_obj) == HTML_TYPE_TEXTSLAVE)) {
- self->next = next_obj->next;
- html_clue_remove (HTML_CLUE (next_obj->parent), next_obj);
- html_object_destroy (next_obj);
- next_obj = self->next;
- }
- }
- static HTMLFitType
- ht_fit_line (HTMLObject *o,
- HTMLPainter *painter,
- gboolean startOfLine,
- gboolean firstRun,
- gboolean next_to_floating,
- gint widthLeft)
- {
- HTMLText *text;
- HTMLObject *text_slave;
- text = HTML_TEXT (o);
- remove_text_slaves (o);
- /* Turn all text over to our slaves */
- text_slave = html_text_slave_new (text, 0, HTML_TEXT (text)->text_len);
- html_clue_append_after (HTML_CLUE (o->parent), text_slave, o);
- return HTML_FIT_COMPLETE;
- }
- #if 0 /* No longer used? */
- static gint
- min_word_width_calc_tabs (HTMLText *text,
- HTMLPainter *p,
- gint idx,
- gint *len)
- {
- gchar *str, *end;
- gint rv = 0, line_offset, wt, wl, i;
- gint epos;
- gboolean tab = FALSE;
- if (!html_clueflow_tabs (HTML_CLUEFLOW (HTML_OBJECT (text)->parent), p))
- return 0;
- /* printf ("tabs %d\n", idx); */
- str = text->text;
- i = idx;
- while (i > 0 && *str) {
- if (*str == ' ')
- i--;
- str = g_utf8_next_char (str);
- }
- if (!*str)
- return 0;
- epos = 0;
- end = str;
- while (*end && *end != ' ') {
- tab |= *end == '\t';
- end = g_utf8_next_char (end);
- epos++;
- }
- if (tab) {
- line_offset = 0;
- if (idx == 0) {
- HTMLObject *prev;
- prev = html_object_prev_not_slave (HTML_OBJECT (text));
- if (prev && html_object_is_text (prev) /* FIXME-words && HTML_TEXT (prev)->words > 0 */) {
- min_word_width_calc_tabs (HTML_TEXT (prev), p, /* FIXME-words HTML_TEXT (prev)->words - 1 */ HTML_TEXT (prev)->text_len - 1, &line_offset);
- /* printf ("lo: %d\n", line_offset); */
- }
- }
- wl = html_text_text_line_length (str, &line_offset, epos, &wt);
- } else {
- wl = epos;
- }
- rv = wl - epos;
- if (len)
- *len = wl;
- /* printf ("tabs delta %d\n", rv); */
- return rv;
- }
- #endif
- gint
- html_text_pango_info_get_index (HTMLTextPangoInfo *pi,
- gint byte_offset,
- gint idx)
- {
- while (idx < pi->n && pi->entries[idx].glyph_item.item->offset + pi->entries[idx].glyph_item.item->length <= byte_offset)
- idx++;
- return idx;
- }
- static void
- html_text_add_cite_color (PangoAttrList *attrs,
- HTMLText *text,
- HTMLClueFlow *flow,
- HTMLEngine *e)
- {
- HTMLColor *cite_color = html_colorset_get_color (e->settings->color_set, HTMLCiteColor);
- if (cite_color && flow->levels->len > 0 && flow->levels->data[0] == HTML_LIST_TYPE_BLOCKQUOTE_CITE) {
- PangoAttribute *attr;
- attr = pango_attr_foreground_new (cite_color->color.red, cite_color->color.green, cite_color->color.blue);
- attr->start_index = 0;
- attr->end_index = text->text_bytes;
- pango_attr_list_change (attrs, attr);
- }
- }
- void
- html_text_remove_unwanted_line_breaks (gchar *s,
- gint len,
- PangoLogAttr *attrs)
- {
- gint i;
- gunichar last_uc = 0;
- for (i = 0; i < len; i++) {
- gunichar uc = g_utf8_get_char (s);
- if (attrs[i].is_line_break) {
- if (last_uc == '.' || last_uc == '/' ||
- last_uc == '-' || last_uc == '$' ||
- last_uc == '+' || last_uc == '?' ||
- last_uc == ')' ||
- last_uc == '}' ||
- last_uc == ']' ||
- last_uc == '>')
- attrs[i].is_line_break = 0;
- else if ((uc == '(' ||
- uc == '{' ||
- uc == '[' ||
- uc == '<'
- )
- && i > 0 && !attrs[i - 1].is_white)
- attrs[i].is_line_break = 0;
- }
- s = g_utf8_next_char (s);
- last_uc = uc;
- }
- }
- PangoAttrList *
- html_text_prepare_attrs (HTMLText *text,
- HTMLPainter *painter)
- {
- PangoAttrList *attrs;
- HTMLClueFlow *flow = NULL;
- HTMLEngine *e = NULL;
- PangoAttribute *attr;
- attrs = pango_attr_list_new ();
- if (HTML_OBJECT (text)->parent && HTML_IS_CLUEFLOW (HTML_OBJECT (text)->parent))
- flow = HTML_CLUEFLOW (HTML_OBJECT (text)->parent);
- if (painter->widget && GTK_IS_HTML (painter->widget))
- e = html_object_engine (HTML_OBJECT (text), GTK_HTML (painter->widget)->engine);
- if (flow && e) {
- html_text_add_cite_color (attrs, text, flow, e);
- }
- if (HTML_IS_PLAIN_PAINTER (painter)) {
- attr = pango_attr_family_new (painter->font_manager.fixed.face ? painter->font_manager.fixed.face : "Monospace");
- attr->start_index = 0;
- attr->end_index = text->text_bytes;
- pango_attr_list_insert (attrs, attr);
- if (painter->font_manager.fix_size != painter->font_manager.var_size || fabs (painter->font_manager.magnification - 1.0) > 0.001) {
- attr = pango_attr_size_new (painter->font_manager.fix_size * painter->font_manager.magnification);
- attr->start_index = 0;
- attr->end_index = text->text_bytes;
- pango_attr_list_insert (attrs, attr);
- }
- } else {
- if (fabs (painter->font_manager.magnification - 1.0) > 0.001) {
- attr = pango_attr_size_new (painter->font_manager.var_size * painter->font_manager.magnification);
- attr->start_index = 0;
- attr->end_index = text->text_bytes;
- pango_attr_list_insert (attrs, attr);
- }
- pango_attr_list_splice (attrs, text->attr_list, 0, 0);
- }
- if (text->extra_attr_list)
- pango_attr_list_splice (attrs, text->extra_attr_list, 0, 0);
- if (!HTML_IS_PLAIN_PAINTER (painter)) {
- if (flow && e)
- html_text_change_attrs (attrs, html_clueflow_get_default_font_style (flow), e, 0, text->text_bytes, TRUE);
- }
- if (text->links && e) {
- HTMLColor *link_color;
- GSList *l;
- for (l = text->links; l; l = l->next) {
- Link *link;
- link = (Link *) l->data;
- if (link->is_visited == FALSE)
- link_color = html_colorset_get_color (e->settings->color_set, HTMLLinkColor);
- else
- link_color = html_colorset_get_color (e->settings->color_set, HTMLVLinkColor);
- attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
- attr->start_index = link->start_index;
- attr->end_index = link->end_index;
- pango_attr_list_change (attrs, attr);
- attr = pango_attr_foreground_new (link_color->color.red, link_color->color.green, link_color->color.blue);
- attr->start_index = link->start_index;
- attr->end_index = link->end_index;
- pango_attr_list_change (attrs, attr);
- }
- }
- return attrs;
- }
- PangoDirection
- html_text_get_pango_direction (HTMLText *text)
- {
- if (HTML_OBJECT (text)->change & HTML_CHANGE_RECALC_PI)
- return pango_find_base_dir (text->text, text->text_bytes);
- else
- return text->direction;
- }
- static PangoDirection
- get_pango_base_direction (HTMLText *text)
- {
- switch (html_object_get_direction (HTML_OBJECT (text))) {
- case HTML_DIRECTION_RTL:
- return PANGO_DIRECTION_RTL;
- case HTML_DIRECTION_LTR:
- return PANGO_DIRECTION_LTR;
- case HTML_DIRECTION_DERIVED:
- default:
- if (text->text)
- return html_text_get_pango_direction (text);
- else
- return HTML_DIRECTION_LTR;
- }
- }
- /**
- * pango_glyph_string_get_logical_widths:
- * @glyphs: a #PangoGlyphString
- * @text: the text corresponding to the glyphs
- * @length: the length of @text, in bytes
- * @embedding_level: the embedding level of the string
- * @logical_widths: an array whose length is g_utf8_strlen (text, length)
- * to be filled in with the resulting character widths.
- *
- * Given a #PangoGlyphString resulting from pango_shape() and the corresponding
- * text, determine the screen width corresponding to each character. When
- * multiple characters compose a single cluster, the width of the entire
- * cluster is divided equally among the characters.
- **/
- void
- html_tmp_fix_pango_glyph_string_get_logical_widths (PangoGlyphString *glyphs,
- const gchar *text,
- gint length,
- gint embedding_level,
- gint *logical_widths)
- {
- gint i, j;
- gint last_cluster = 0;
- gint width = 0;
- gint last_cluster_width = 0;
- const gchar *p = text; /* Points to start of current cluster */
- /* printf ("html_tmp_fix_pango_glyph_string_get_logical_widths"); */
- for (i = 0; i <= glyphs->num_glyphs; i++)
- {
- gint glyph_index = (embedding_level % 2 == 0) ? i : glyphs->num_glyphs - i - 1;
- /* If this glyph belongs to a new cluster, or we're at the end, find
- * the start of the next cluster, and assign the widths for this cluster.
- */
- if (i == glyphs->num_glyphs || p != text + glyphs->log_clusters[glyph_index])
- {
- gint next_cluster = last_cluster;
- if (i < glyphs->num_glyphs)
- {
- while (p < text + glyphs->log_clusters[glyph_index])
- {
- next_cluster++;
- p = g_utf8_next_char (p);
- }
- }
- else
- {
- while (p < text + length)
- {
- next_cluster++;
- p = g_utf8_next_char (p);
- }
- }
- for (j = last_cluster; j < next_cluster; j++) {
- logical_widths[j] = (width - last_cluster_width) / (next_cluster - last_cluster);
- /* printf (" %d", logical_widths [j]); */
- }
- if (last_cluster != next_cluster) {
- last_cluster = next_cluster;
- last_cluster_width = width;
- }
- }
- if (i < glyphs->num_glyphs)
- width += glyphs->glyphs[glyph_index].geometry.width;
- }
- /* printf ("\n"); */
- }
- static void
- html_text_shape_tab (HTMLText *text,
- PangoGlyphString *glyphs)
- {
- /* copied from pango sources */
- pango_glyph_string_set_size (glyphs, 1);
- glyphs->glyphs[0].glyph = EMPTY_GLYPH;
- glyphs->glyphs[0].geometry.x_offset = 0;
- glyphs->glyphs[0].geometry.y_offset = 0;
- glyphs->glyphs[0].attr.is_cluster_start = 1;
- glyphs->log_clusters[0] = 0;
- glyphs->glyphs[0].geometry.width = 48 * PANGO_SCALE;
- }
- HTMLTextPangoInfo *
- html_text_get_pango_info (HTMLText *text,
- HTMLPainter *painter)
- {
- if (HTML_OBJECT (text)->change & HTML_CHANGE_RECALC_PI) {
- pango_info_destroy (text);
- HTML_OBJECT (text)->change &= ~HTML_CHANGE_RECALC_PI;
- text->direction = pango_find_base_dir (text->text, text->text_bytes);
- }
- if (!text->pi) {
- GList *items, *cur;
- PangoAttrList *attrs;
- gint i, offset;
- attrs = html_text_prepare_attrs (text, painter);
- items = pango_itemize_with_base_dir (painter->pango_context, get_pango_base_direction (text), text->text, 0, text->text_bytes, attrs, NULL);
- pango_attr_list_unref (attrs);
- /* create pango info */
- text->pi = html_text_pango_info_new (g_list_length (items));
- text->pi->have_font = TRUE;
- text->pi->font_style = html_text_get_font_style (text);
- text->pi->face = g_strdup (text->face);
- text->pi->attrs = g_new (PangoLogAttr, text->text_len + 1);
- /* get line breaks */
- offset = 0;
- for (cur = items; cur; cur = cur->next) {
- PangoItem tmp_item;
- PangoItem *item;
- gint start_offset;
- start_offset = offset;
- item = (PangoItem *) cur->data;
- offset += item->num_chars;
- tmp_item = *item;
- while (cur->next) {
- PangoItem *next_item = (PangoItem *) cur->next->data;
- if (tmp_item.analysis.lang_engine == next_item->analysis.lang_engine) {
- tmp_item.length += next_item->length;
- tmp_item.num_chars += next_item->num_chars;
- offset += next_item->num_chars;
- cur = cur->next;
- } else
- break;
- }
- pango_break (text->text + tmp_item.offset, tmp_item.length, &tmp_item.analysis, text->pi->attrs + start_offset, tmp_item.num_chars + 1);
- }
- if (text->pi && text->pi->attrs)
- html_text_remove_unwanted_line_breaks (text->text, text->text_len, text->pi->attrs);
- for (i = 0, cur = items; i < text->pi->n; i++, cur = cur->next)
- text->pi->entries[i].glyph_item.item = (PangoItem *) cur->data;
- for (i = 0; i < text->pi->n; i++) {
- PangoItem *item;
- PangoGlyphString *glyphs;
- item = text->pi->entries[i].glyph_item.item;
- glyphs = text->pi->entries[i].glyph_item.glyphs = pango_glyph_string_new ();
- /* printf ("item pos %d len %d\n", item->offset, item->length); */
- text->pi->entries[i].widths = g_new (PangoGlyphUnit, item->num_chars);
- if (text->text[item->offset] == '\t')
- html_text_shape_tab (text, glyphs);
- else
- pango_shape (text->text + item->offset, item->length, &item->analysis, glyphs);
- html_tmp_fix_pango_glyph_string_get_logical_widths (glyphs, text->text + item->offset, item->length,
- item->analysis.level, text->pi->entries[i].widths);
- }
- g_list_free (items);
- }
- return text->pi;
- }
- gboolean
- html_text_pi_backward (HTMLTextPangoInfo *pi,
- gint *ii,
- gint *io)
- {
- if (*io <= 0) {
- if (*ii <= 0)
- return FALSE;
- (*ii) --;
- *io = pi->entries [*ii].glyph_item.item->num_chars - 1;
- } else
- (*io) --;
- return TRUE;
- }
- gboolean
- html_text_pi_forward (HTMLTextPangoInfo *pi,
- gint *ii,
- gint *io)
- {
- if (*io >= pi->entries[*ii].glyph_item.item->num_chars - 1) {
- if (*ii >= pi->n -1)
- return FALSE;
- (*ii) ++;
- *io = 0;
- } else
- (*io) ++;
- return TRUE;
- }
- /**
- * html_text_tail_white_space:
- * @text: a #HTMLText object
- * @painter: a #HTMLPainter object
- * @offset: offset into the text of @text, in characters
- * @ii: index of current item
- * @io: offset within current item, in characters
- * @white_len: length of found trailing white space, in characters
- * @line_offset:
- * @s: pointer into the text of @text corresponding to @offset
- *
- * Used to chop off one character worth of whitespace at a particular position.
- *
- * Return value: width of found trailing white space, in Pango units
- **/
- gint
- html_text_tail_white_space (HTMLText *text,
- HTMLPainter *painter,
- gint offset,
- gint ii,
- gint io,
- gint *white_len,
- gint line_offset,
- gchar *s)
- {
- HTMLTextPangoInfo *pi = html_text_get_pango_info (text, painter);
- gint wl = 0;
- gint ww = 0;
- if (html_text_pi_backward (pi, &ii, &io)) {
- s = g_utf8_prev_char (s);
- offset--;
- if (pi->attrs[offset].is_white) {
- if (*s == '\t' && offset > 1) {
- gint skip = 8, co = offset - 1;
- do {
- s = g_utf8_prev_char (s);
- co--;
- if (*s != '\t')
- skip--;
- } while (s && co > 0 && *s != '\t');
- ww += skip * pi->entries[ii].widths[io];
- } else {
- ww += pi->entries[ii].widths[io];
- }
- wl++;
- }
- }
- if (white_len)
- *white_len = wl;
- return ww;
- }
- static void
- update_mw (HTMLText *text,
- HTMLPainter *painter,
- gint offset,
- gint *last_offset,
- gint *ww,
- gint *mw,
- gint ii,
- gint io,
- gchar *s,
- gint line_offset) {
- *ww -= html_text_tail_white_space (text, painter, offset, ii, io, NULL, line_offset, s);
- if (*ww > *mw)
- *mw = *ww;
- *ww = 0;
- *last_offset = offset;
- }
- gboolean
- html_text_is_line_break (PangoLogAttr attr)
- {
- return attr.is_line_break;
- }
- static gint
- calc_min_width (HTMLObject *self,
- HTMLPainter *painter)
- {
- HTMLText *text = HTML_TEXT (self);
- HTMLTextPangoInfo *pi = html_text_get_pango_info (text, painter);
- gint mw = 0, ww;
- gint ii, io, offset, last_offset, line_offset;
- gchar *s;
- ww = 0;
- last_offset = offset = 0;
- ii = io = 0;
- line_offset = html_text_get_line_offset (text, painter, 0);
- s = text->text;
- while (offset < text->text_len) {
- if (offset > 0 && html_text_is_line_break (pi->attrs[offset]))
- update_mw (text, painter, offset, &last_offset, &ww, &mw, ii, io, s, line_offset);
- if (*s == '\t') {
- gint skip = 8 - (line_offset % 8);
- ww += skip * pi->entries[ii].widths[io];
- line_offset += skip;
- } else {
- ww += pi->entries[ii].widths[io];
- line_offset++;
- }
- s = g_utf8_next_char (s);
- offset++;
- html_text_pi_forward (pi, &ii, &io);
- }
- if (ww > mw)
- mw = ww;
- return MAX (1, html_painter_pango_to_engine (painter, mw));
- }
- static void
- draw (HTMLObject *o,
- HTMLPainter *p,
- gint x,
- gint y,
- gint width,
- gint height,
- gint tx,
- gint ty)
- {
- }
- static gboolean
- accepts_cursor (HTMLObject *object)
- {
- return TRUE;
- }
- static gboolean
- save_open_attrs (HTMLEngineSaveState *state,
- GSList *attrs)
- {
- gboolean rv = TRUE;
- for (; attrs; attrs = attrs->next) {
- PangoAttribute *attr = (PangoAttribute *) attrs->data;
- HTMLEngine *e = state->engine;
- const gchar *tag = NULL;
- gboolean free_tag = FALSE;
- switch (attr->klass->type) {
- case PANGO_ATTR_WEIGHT:
- tag = "<B>";
- break;
- case PANGO_ATTR_STYLE:
- tag = "<I>";
- break;
- case PANGO_ATTR_UNDERLINE:
- tag = "<U>";
- break;
- case PANGO_ATTR_STRIKETHROUGH:
- tag = "<S>";
- break;
- case PANGO_ATTR_SIZE:
- if (attr->klass == &html_pango_attr_font_size_klass) {
- HTMLPangoAttrFontSize *size = (HTMLPangoAttrFontSize *) attr;
- if ((size->style & GTK_HTML_FONT_STYLE_SIZE_MASK) != GTK_HTML_FONT_STYLE_SIZE_3 && (size->style & GTK_HTML_FONT_STYLE_SIZE_MASK) != 0) {
- tag = g_strdup_printf ("<FONT SIZE=\"%d\">", size->style & GTK_HTML_FONT_STYLE_SIZE_MASK);
- free_tag = TRUE;
- }
- }
- break;
- case PANGO_ATTR_FAMILY: {
- PangoAttrString *family_attr = (PangoAttrString *) attr;
- if (!g_ascii_strcasecmp (e->painter->font_manager.fixed.face
- ? e->painter->font_manager.fixed.face : "Monospace",
- family_attr->value))
- tag = "<TT>";
- }
- break;
- case PANGO_ATTR_FOREGROUND: {
- PangoAttrColor *color = (PangoAttrColor *) attr;
- tag = g_strdup_printf ("<FONT COLOR=\"#%02x%02x%02x\">",
- (color->color.red >> 8) & 0xff, (color->color.green >> 8) & 0xff, (color->color.blue >> 8) & 0xff);
- free_tag = TRUE;
- }
- break;
- default:
- break;
- }
- if (tag) {
- if (!html_engine_save_output_string (state, "%s", tag))
- rv = FALSE;
- if (free_tag)
- g_free ((gpointer) tag);
- if (!rv)
- break;
- }
- }
- return TRUE;
- }
- static gboolean
- save_close_attrs (HTMLEngineSaveState *state,
- GSList *attrs)
- {
- for (; attrs; attrs = attrs->next) {
- PangoAttribute *attr = (PangoAttribute *) attrs->data;
- HTMLEngine *e = state->engine;
- const gchar *tag = NULL;
- switch (attr->klass->type) {
- case PANGO_ATTR_WEIGHT:
- tag = "</B>";
- break;
- case PANGO_ATTR_STYLE:
- tag = "</I>";
- break;
- case PANGO_ATTR_UNDERLINE:
- tag = "</U>";
- break;
- case PANGO_ATTR_STRIKETHROUGH:
- tag = "</S>";
- break;
- case PANGO_ATTR_SIZE:
- if (attr->klass == &html_pango_attr_font_size_klass) {
- HTMLPangoAttrFontSize *size = (HTMLPangoAttrFontSize *) attr;
- if ((size->style & GTK_HTML_FONT_STYLE_SIZE_MASK) != GTK_HTML_FONT_STYLE_SIZE_3 && (size->style & GTK_HTML_FONT_STYLE_SIZE_MASK) != 0)
- tag = "</FONT>";
- }
- break;
- case PANGO_ATTR_FOREGROUND:
- tag = "</FONT>";
- break;
- case PANGO_ATTR_FAMILY: {
- PangoAttrString *family_attr = (PangoAttrString *) attr;
- if (!g_ascii_strcasecmp (e->painter->font_manager.fixed.face
- ? e->painter->font_manager.fixed.face : "Monospace",
- family_attr->value))
- tag = "</TT>";
- }
- break;
- default:
- break;
- }
- if (tag)
- if (!html_engine_save_output_string (state, "%s", tag))
- return FALSE;
- }
- return TRUE;
- }
- static gboolean
- save_text_part (HTMLText *text,
- HTMLEngineSaveState *state,
- guint start_index,
- guint end_index)
- {
- gchar *str;
- gint len;
- gboolean rv;
- str = g_strndup (text->text + start_index, end_index - start_index);
- len = g_utf8_pointer_to_offset (text->text + start_index, text->text + end_index);
- rv = html_engine_save_encode (state, str, len);
- g_free (str);
- return rv;
- }
- static gboolean
- save_link_open (Link *link,
- HTMLEngineSaveState *state)
- {
- return html_engine_save_delims_and_vals (state,
- "<A HREF=\"", link->url,
- "\">", NULL);
- }
- static gboolean
- save_link_close (Link *link,
- HTMLEngineSaveState *state)
- {
- return html_engi…
Large files files are truncated, but you can click here to view the full file