PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/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

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
  2. /* This file is part of the GtkHTML library.
  3. *
  4. * Copyright (C) 1997 Martin Jones (mjones@kde.org)
  5. * Copyright (C) 1997 Torben Weis (weis@kde.org)
  6. * Copyright (C) 1999, 2000 Helix Code, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public License
  19. * along with this library; see the file COPYING.LIB. If not, write to
  20. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. */
  23. #include <config.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <regex.h>
  28. #include <math.h>
  29. #define PANGO_ENABLE_BACKEND /* Required to get PANGO_GLYPH_EMPTY */
  30. #include <pango/pango.h>
  31. #include "htmltext.h"
  32. #include "htmlcolor.h"
  33. #include "htmlcolorset.h"
  34. #include "htmlcluealigned.h"
  35. #include "htmlclueflow.h"
  36. #include "htmlcursor.h"
  37. #include "htmlgdkpainter.h"
  38. #include "htmlplainpainter.h"
  39. #include "htmlprinter.h"
  40. #include "htmlengine.h"
  41. #include "htmlengine-edit.h"
  42. #include "htmlengine-edit-cut-and-paste.h"
  43. #include "htmlengine-save.h"
  44. #include "htmlentity.h"
  45. #include "htmlsettings.h"
  46. #include "htmltextslave.h"
  47. #include "htmlundo.h"
  48. HTMLTextClass html_text_class;
  49. static HTMLObjectClass *parent_class = NULL;
  50. static const PangoAttrClass html_pango_attr_font_size_klass;
  51. #define HT_CLASS(x) HTML_TEXT_CLASS (HTML_OBJECT (x)->klass)
  52. #ifdef PANGO_GLYPH_EMPTY
  53. #define EMPTY_GLYPH PANGO_GLYPH_EMPTY
  54. #else
  55. #define EMPTY_GLYPH 0
  56. #endif
  57. static SpellError * spell_error_new (guint off, guint len);
  58. static void spell_error_destroy (SpellError *se);
  59. static void move_spell_errors (GList *spell_errors, guint offset, gint delta);
  60. static GList * remove_spell_errors (GList *spell_errors, guint offset, guint len);
  61. static GList * merge_spell_errors (GList *se1, GList *se2);
  62. static void remove_text_slaves (HTMLObject *self);
  63. /* void
  64. debug_spell_errors (GList *se)
  65. {
  66. for (; se; se = se->next)
  67. printf ("SE: %4d, %4d\n", ((SpellError *) se->data)->off, ((SpellError *) se->data)->len);
  68. } */
  69. static inline gboolean
  70. is_in_the_save_cluev (HTMLObject *text,
  71. HTMLObject *o)
  72. {
  73. return html_object_nth_parent (o, 2) == html_object_nth_parent (text, 2);
  74. }
  75. /* HTMLObject methods. */
  76. HTMLTextPangoInfo *
  77. html_text_pango_info_new (gint n)
  78. {
  79. HTMLTextPangoInfo *pi;
  80. pi = g_new (HTMLTextPangoInfo, 1);
  81. pi->n = n;
  82. pi->entries = g_new0 (HTMLTextPangoInfoEntry, n);
  83. pi->attrs = NULL;
  84. pi->have_font = FALSE;
  85. pi->font_style = GTK_HTML_FONT_STYLE_DEFAULT;
  86. pi->face = NULL;
  87. return pi;
  88. }
  89. void
  90. html_text_pango_info_destroy (HTMLTextPangoInfo *pi)
  91. {
  92. gint i;
  93. for (i = 0; i < pi->n; i++) {
  94. pango_item_free (pi->entries[i].glyph_item.item);
  95. if (pi->entries[i].glyph_item.glyphs)
  96. pango_glyph_string_free (pi->entries[i].glyph_item.glyphs);
  97. g_free (pi->entries[i].widths);
  98. }
  99. g_free (pi->entries);
  100. g_free (pi->attrs);
  101. g_free (pi->face);
  102. g_free (pi);
  103. }
  104. static void
  105. pango_info_destroy (HTMLText *text)
  106. {
  107. if (text->pi) {
  108. html_text_pango_info_destroy (text->pi);
  109. text->pi = NULL;
  110. }
  111. }
  112. static void
  113. free_links (GSList *list)
  114. {
  115. if (list) {
  116. GSList *l;
  117. for (l = list; l; l = l->next)
  118. html_link_free ((Link *) l->data);
  119. g_slist_free (list);
  120. }
  121. }
  122. void
  123. html_text_free_attrs (GSList *attrs)
  124. {
  125. if (attrs) {
  126. GSList *l;
  127. for (l = attrs; l; l = l->next)
  128. pango_attribute_destroy ((PangoAttribute *) l->data);
  129. g_slist_free (attrs);
  130. }
  131. }
  132. static void
  133. copy (HTMLObject *s,
  134. HTMLObject *d)
  135. {
  136. HTMLText *src = HTML_TEXT (s);
  137. HTMLText *dest = HTML_TEXT (d);
  138. GList *cur;
  139. GSList *csl;
  140. (* HTML_OBJECT_CLASS (parent_class)->copy) (s, d);
  141. dest->text = g_strdup (src->text);
  142. dest->text_len = src->text_len;
  143. dest->text_bytes = src->text_bytes;
  144. dest->font_style = src->font_style;
  145. dest->face = g_strdup (src->face);
  146. dest->color = src->color;
  147. dest->select_start = 0;
  148. dest->select_length = 0;
  149. dest->attr_list = pango_attr_list_copy (src->attr_list);
  150. dest->extra_attr_list = src->extra_attr_list ? pango_attr_list_copy (src->extra_attr_list) : NULL;
  151. html_color_ref (dest->color);
  152. dest->spell_errors = g_list_copy (src->spell_errors);
  153. cur = dest->spell_errors;
  154. while (cur) {
  155. SpellError *se = (SpellError *) cur->data;
  156. cur->data = spell_error_new (se->off, se->len);
  157. cur = cur->next;
  158. }
  159. dest->links = g_slist_copy (src->links);
  160. for (csl = dest->links; csl; csl = csl->next)
  161. csl->data = html_link_dup ((Link *) csl->data);
  162. dest->pi = NULL;
  163. dest->direction = src->direction;
  164. }
  165. /* static void
  166. debug_word_width (HTMLText *t)
  167. {
  168. guint i;
  169. *
  170. printf ("words: %d | ", t->words);
  171. for (i = 0; i < t->words; i++)
  172. printf ("%d ", t->word_width [i]);
  173. printf ("\n");
  174. }
  175. *
  176. static void
  177. word_get_position (HTMLText *text,
  178. * guint off,
  179. * guint *word_out,
  180. * guint *left_out,
  181. * guint *right_out)
  182. {
  183. const gchar *s, *ls;
  184. guint coff, loff;
  185. *
  186. coff = 0;
  187. *word_out = 0;
  188. s = text->text;
  189. do {
  190. ls = s;
  191. loff = coff;
  192. s = strchr (s, ' ');
  193. coff += s ? g_utf8_pointer_to_offset (ls, s) : g_utf8_strlen (ls, -1);
  194. (*word_out) ++;
  195. if (s)
  196. s++;
  197. } while (s && coff < off);
  198. *
  199. *left_out = off - loff;
  200. *right_out = coff - off;
  201. *
  202. printf ("get position w: %d l: %d r: %d\n", *word_out, *left_out, *right_out);
  203. } */
  204. static gboolean
  205. cut_attr_list_filter (PangoAttribute *attr,
  206. gpointer data)
  207. {
  208. PangoAttribute *range = (PangoAttribute *) data;
  209. gint delta;
  210. if (attr->start_index >= range->start_index && attr->end_index <= range->end_index)
  211. return TRUE;
  212. delta = range->end_index - range->start_index;
  213. if (attr->start_index > range->end_index) {
  214. attr->start_index -= delta;
  215. attr->end_index -= delta;
  216. } else if (attr->start_index > range->start_index) {
  217. attr->start_index = range->start_index;
  218. attr->end_index -= delta;
  219. if (attr->end_index <= attr->start_index)
  220. return TRUE;
  221. } else if (attr->end_index >= range->end_index)
  222. attr->end_index -= delta;
  223. else if (attr->end_index >= range->start_index)
  224. attr->end_index = range->start_index;
  225. return FALSE;
  226. }
  227. static void
  228. cut_attr_list_list (PangoAttrList *attr_list,
  229. gint begin_index,
  230. gint end_index)
  231. {
  232. PangoAttrList *removed;
  233. PangoAttribute range;
  234. range.start_index = begin_index;
  235. range.end_index = end_index;
  236. removed = pango_attr_list_filter (attr_list, cut_attr_list_filter, &range);
  237. if (removed)
  238. pango_attr_list_unref (removed);
  239. }
  240. static void
  241. cut_attr_list (HTMLText *text,
  242. gint begin_index,
  243. gint end_index)
  244. {
  245. cut_attr_list_list (text->attr_list, begin_index, end_index);
  246. if (text->extra_attr_list)
  247. cut_attr_list_list (text->extra_attr_list, begin_index, end_index);
  248. }
  249. static void
  250. cut_links_full (HTMLText *text,
  251. gint start_offset,
  252. gint end_offset,
  253. gint start_index,
  254. gint end_index,
  255. gint shift_offset,
  256. gint shift_index)
  257. {
  258. GSList *l, *next;
  259. Link *link;
  260. for (l = text->links; l; l = next) {
  261. next = l->next;
  262. link = (Link *) l->data;
  263. if (start_offset <= link->start_offset && link->end_offset <= end_offset) {
  264. html_link_free (link);
  265. text->links = g_slist_delete_link (text->links, l);
  266. } else if (end_offset <= link->start_offset) {
  267. link->start_offset -= shift_offset;
  268. link->start_index -= shift_index;
  269. link->end_offset -= shift_offset;
  270. link->end_index -= shift_index;
  271. } else if (start_offset <= link->start_offset) {
  272. link->start_offset = end_offset - shift_offset;
  273. link->end_offset -= shift_offset;
  274. link->start_index = end_index - shift_index;
  275. link->end_index -= shift_index;
  276. } else if (end_offset <= link->end_offset) {
  277. if (shift_offset > 0) {
  278. link->end_offset -= shift_offset;
  279. link->end_index -= shift_index;
  280. } else {
  281. if (link->end_offset == end_offset) {
  282. link->end_offset = start_offset;
  283. link->end_index = start_index;
  284. } else if (link->start_offset == start_offset) {
  285. link->start_offset = end_offset;
  286. link->start_index = end_index;
  287. } else {
  288. Link *dup = html_link_dup (link);
  289. link->start_offset = end_offset;
  290. link->start_index = end_index;
  291. dup->end_offset = start_offset;
  292. dup->end_index = start_index;
  293. l = g_slist_prepend (l, dup);
  294. next = l->next->next;
  295. }
  296. }
  297. } else if (start_offset < link->end_offset) {
  298. link->end_offset = start_offset;
  299. link->end_index = start_index;
  300. }
  301. }
  302. }
  303. static void
  304. cut_links (HTMLText *text,
  305. gint start_offset,
  306. gint end_offset,
  307. gint start_index,
  308. gint end_index)
  309. {
  310. cut_links_full (text, start_offset, end_offset, start_index, end_index, end_offset - start_offset, end_index - start_index);
  311. }
  312. HTMLObject *
  313. html_text_op_copy_helper (HTMLText *text,
  314. GList *from,
  315. GList *to,
  316. guint *len)
  317. {
  318. HTMLObject *rv;
  319. HTMLText *rvt;
  320. gchar *tail, *nt;
  321. gint begin, end, begin_index, end_index;
  322. begin = (from) ? GPOINTER_TO_INT (from->data) : 0;
  323. end = (to) ? GPOINTER_TO_INT (to->data) : text->text_len;
  324. tail = html_text_get_text (text, end);
  325. begin_index = html_text_get_index (text, begin);
  326. end_index = tail - text->text;
  327. *len += end - begin;
  328. rv = html_object_dup (HTML_OBJECT (text));
  329. rvt = HTML_TEXT (rv);
  330. rvt->text_len = end - begin;
  331. rvt->text_bytes = end_index - begin_index;
  332. nt = g_strndup (rvt->text + begin_index, rvt->text_bytes);
  333. g_free (rvt->text);
  334. rvt->text = nt;
  335. rvt->spell_errors = remove_spell_errors (rvt->spell_errors, 0, begin);
  336. rvt->spell_errors = remove_spell_errors (rvt->spell_errors, end, text->text_len - end);
  337. if (end_index < text->text_bytes)
  338. cut_attr_list (rvt, end_index, text->text_bytes);
  339. if (begin_index > 0)
  340. cut_attr_list (rvt, 0, begin_index);
  341. if (end < text->text_len)
  342. cut_links (rvt, end, text->text_len, end_index, text->text_bytes);
  343. if (begin > 0)
  344. cut_links (rvt, 0, begin, 0, begin_index);
  345. return rv;
  346. }
  347. HTMLObject *
  348. html_text_op_cut_helper (HTMLText *text,
  349. HTMLEngine *e,
  350. GList *from,
  351. GList *to,
  352. GList *left,
  353. GList *right,
  354. guint *len)
  355. {
  356. HTMLObject *rv;
  357. HTMLText *rvt;
  358. gint begin, end;
  359. begin = (from) ? GPOINTER_TO_INT (from->data) : 0;
  360. end = (to) ? GPOINTER_TO_INT (to->data) : text->text_len;
  361. g_assert (begin <= end);
  362. g_assert (end <= text->text_len);
  363. /* printf ("before cut '%s'\n", text->text);
  364. * debug_word_width (text); */
  365. remove_text_slaves (HTML_OBJECT (text));
  366. if (!html_object_could_remove_whole (HTML_OBJECT (text), from, to, left, right) || begin || end < text->text_len) {
  367. gchar *nt, *tail;
  368. gint begin_index, end_index;
  369. if (begin == end)
  370. return HTML_OBJECT (html_text_new_with_len ("", 0, text->font_style, text->color));
  371. rv = html_object_dup (HTML_OBJECT (text));
  372. rvt = HTML_TEXT (rv);
  373. tail = html_text_get_text (text, end);
  374. begin_index = html_text_get_index (text, begin);
  375. end_index = tail - text->text;
  376. text->text_bytes -= tail - (text->text + begin_index);
  377. text->text[begin_index] = 0;
  378. cut_attr_list (text, begin_index, end_index);
  379. if (end_index < rvt->text_bytes)
  380. cut_attr_list (rvt, end_index, rvt->text_bytes);
  381. if (begin_index > 0)
  382. cut_attr_list (rvt, 0, begin_index);
  383. cut_links (text, begin, end, begin_index, end_index);
  384. if (end < rvt->text_len)
  385. cut_links (rvt, end, rvt->text_len, end_index, rvt->text_bytes);
  386. if (begin > 0)
  387. cut_links (rvt, 0, begin, 0, begin_index);
  388. nt = g_strconcat (text->text, tail, NULL);
  389. g_free (text->text);
  390. rvt->spell_errors = remove_spell_errors (rvt->spell_errors, 0, begin);
  391. rvt->spell_errors = remove_spell_errors (rvt->spell_errors, end, text->text_len - end);
  392. move_spell_errors (rvt->spell_errors, begin, -begin);
  393. text->text = nt;
  394. text->text_len -= end - begin;
  395. *len += end - begin;
  396. nt = g_strndup (rvt->text + begin_index, end_index - begin_index);
  397. g_free (rvt->text);
  398. rvt->text = nt;
  399. rvt->text_len = end - begin;
  400. rvt->text_bytes = end_index - begin_index;
  401. text->spell_errors = remove_spell_errors (text->spell_errors, begin, end - begin);
  402. move_spell_errors (text->spell_errors, end, - (end - begin));
  403. html_text_convert_nbsp (text, TRUE);
  404. html_text_convert_nbsp (rvt, TRUE);
  405. pango_info_destroy (text);
  406. } else {
  407. text->spell_errors = remove_spell_errors (text->spell_errors, 0, text->text_len);
  408. html_object_move_cursor_before_remove (HTML_OBJECT (text), e);
  409. html_object_change_set (HTML_OBJECT (text)->parent, HTML_CHANGE_ALL_CALC);
  410. /* force parent redraw */
  411. HTML_OBJECT (text)->parent->width = 0;
  412. html_object_remove_child (HTML_OBJECT (text)->parent, HTML_OBJECT (text));
  413. rv = HTML_OBJECT (text);
  414. *len += text->text_len;
  415. }
  416. html_object_change_set (HTML_OBJECT (text), HTML_CHANGE_ALL_CALC);
  417. /* printf ("after cut '%s'\n", text->text);
  418. * debug_word_width (text); */
  419. return rv;
  420. }
  421. static HTMLObject *
  422. op_copy (HTMLObject *self,
  423. HTMLObject *parent,
  424. HTMLEngine *e,
  425. GList *from,
  426. GList *to,
  427. guint *len)
  428. {
  429. return html_text_op_copy_helper (HTML_TEXT (self), from, to, len);
  430. }
  431. static HTMLObject *
  432. op_cut (HTMLObject *self,
  433. HTMLEngine *e,
  434. GList *from,
  435. GList *to,
  436. GList *left,
  437. GList *right,
  438. guint *len)
  439. {
  440. return html_text_op_cut_helper (HTML_TEXT (self), e, from, to, left, right, len);
  441. }
  442. static void
  443. merge_links (HTMLText *t1,
  444. HTMLText *t2)
  445. {
  446. Link *tail, *head;
  447. GSList *l;
  448. if (t2->links) {
  449. for (l = t2->links; l; l = l->next) {
  450. Link *link = (Link *) l->data;
  451. link->start_offset += t1->text_len;
  452. link->start_index += t1->text_bytes;
  453. link->end_offset += t1->text_len;
  454. link->end_index += t1->text_bytes;
  455. }
  456. if (t1->links) {
  457. head = (Link *) t1->links->data;
  458. tail = (Link *) g_slist_last (t2->links)->data;
  459. if (tail->start_offset == head->end_offset && html_link_equal (head, tail)) {
  460. tail->start_offset = head->start_offset;
  461. tail->start_index = head->start_index;
  462. html_link_free (head);
  463. t1->links = g_slist_delete_link (t1->links, t1->links);
  464. }
  465. }
  466. t1->links = g_slist_concat (t2->links, t1->links);
  467. t2->links = NULL;
  468. }
  469. }
  470. static gboolean
  471. object_merge (HTMLObject *self,
  472. HTMLObject *with,
  473. HTMLEngine *e,
  474. GList **left,
  475. GList **right,
  476. HTMLCursor *cursor)
  477. {
  478. HTMLText *t1, *t2;
  479. gchar *to_free;
  480. t1 = HTML_TEXT (self);
  481. t2 = HTML_TEXT (with);
  482. /* printf ("merge '%s' '%s'\n", t1->text, t2->text); */
  483. /* merge_word_width (t1, t2, e->painter); */
  484. if (e->cursor->object == with) {
  485. e->cursor->object = self;
  486. e->cursor->offset += t1->text_len;
  487. }
  488. /* printf ("--- before merge\n");
  489. * debug_spell_errors (t1->spell_errors);
  490. * printf ("---\n");
  491. * debug_spell_errors (t2->spell_errors);
  492. * printf ("---\n");
  493. */
  494. move_spell_errors (t2->spell_errors, 0, t1->text_len);
  495. t1->spell_errors = merge_spell_errors (t1->spell_errors, t2->spell_errors);
  496. t2->spell_errors = NULL;
  497. pango_attr_list_splice (t1->attr_list, t2->attr_list, t1->text_bytes, t2->text_bytes);
  498. if (t2->extra_attr_list) {
  499. if (!t1->extra_attr_list)
  500. t1->extra_attr_list = pango_attr_list_new ();
  501. pango_attr_list_splice (t1->extra_attr_list, t2->extra_attr_list, t1->text_bytes, t2->text_bytes);
  502. }
  503. merge_links (t1, t2);
  504. to_free = t1->text;
  505. t1->text = g_strconcat (t1->text, t2->text, NULL);
  506. t1->text_len += t2->text_len;
  507. t1->text_bytes += t2->text_bytes;
  508. g_free (to_free);
  509. html_text_convert_nbsp (t1, TRUE);
  510. html_object_change_set (self, HTML_CHANGE_ALL_CALC);
  511. pango_info_destroy (t1);
  512. pango_info_destroy (t2);
  513. /* html_text_request_word_width (t1, e->painter); */
  514. /* printf ("merged '%s'\n", t1->text);
  515. * printf ("--- after merge\n");
  516. * debug_spell_errors (t1->spell_errors);
  517. * printf ("---\n"); */
  518. return TRUE;
  519. }
  520. static gboolean
  521. split_attrs_filter_head (PangoAttribute *attr,
  522. gpointer data)
  523. {
  524. gint index = GPOINTER_TO_INT (data);
  525. if (attr->start_index >= index)
  526. return TRUE;
  527. else if (attr->end_index > index)
  528. attr->end_index = index;
  529. return FALSE;
  530. }
  531. static gboolean
  532. split_attrs_filter_tail (PangoAttribute *attr,
  533. gpointer data)
  534. {
  535. gint index = GPOINTER_TO_INT (data);
  536. if (attr->end_index <= index)
  537. return TRUE;
  538. if (attr->start_index > index)
  539. attr->start_index -= index;
  540. else
  541. attr->start_index = 0;
  542. attr->end_index -= index;
  543. return FALSE;
  544. }
  545. static void
  546. split_attrs (HTMLText *t1,
  547. HTMLText *t2,
  548. gint index)
  549. {
  550. PangoAttrList *delete;
  551. delete = pango_attr_list_filter (t1->attr_list, split_attrs_filter_head, GINT_TO_POINTER (index));
  552. if (delete)
  553. pango_attr_list_unref (delete);
  554. if (t1->extra_attr_list) {
  555. delete = pango_attr_list_filter (t1->extra_attr_list, split_attrs_filter_head, GINT_TO_POINTER (index));
  556. if (delete)
  557. pango_attr_list_unref (delete);
  558. }
  559. delete = pango_attr_list_filter (t2->attr_list, split_attrs_filter_tail, GINT_TO_POINTER (index));
  560. if (delete)
  561. pango_attr_list_unref (delete);
  562. if (t2->extra_attr_list) {
  563. delete = pango_attr_list_filter (t2->extra_attr_list, split_attrs_filter_tail, GINT_TO_POINTER (index));
  564. if (delete)
  565. pango_attr_list_unref (delete);
  566. }
  567. }
  568. static void
  569. split_links (HTMLText *t1,
  570. HTMLText *t2,
  571. gint offset,
  572. gint index)
  573. {
  574. GSList *l, *prev = NULL;
  575. for (l = t1->links; l; l = l->next) {
  576. Link *link = (Link *) l->data;
  577. if (link->start_offset < offset) {
  578. if (link->end_offset > offset) {
  579. link->end_offset = offset;
  580. link->end_index = index;
  581. }
  582. if (prev) {
  583. prev->next = NULL;
  584. free_links (t1->links);
  585. }
  586. t1->links = l;
  587. break;
  588. }
  589. prev = l;
  590. if (!l->next) {
  591. free_links (t1->links);
  592. t1->links = NULL;
  593. break;
  594. }
  595. }
  596. prev = NULL;
  597. for (l = t2->links; l; l = l->next) {
  598. Link *link = (Link *) l->data;
  599. if (link->start_offset < offset) {
  600. if (link->end_offset > offset) {
  601. link->start_offset = offset;
  602. link->start_index = index;
  603. prev = l;
  604. l = l->next;
  605. }
  606. if (prev) {
  607. prev->next = NULL;
  608. free_links (l);
  609. } else {
  610. free_links (t2->links);
  611. t2->links = NULL;
  612. }
  613. break;
  614. }
  615. prev = l;
  616. }
  617. for (l = t2->links; l; l = l->next) {
  618. Link *link = (Link *) l->data;
  619. link->start_offset -= offset;
  620. link->start_index -= index;
  621. link->end_offset -= offset;
  622. link->end_index -= index;
  623. }
  624. }
  625. static void
  626. object_split (HTMLObject *self,
  627. HTMLEngine *e,
  628. HTMLObject *child,
  629. gint offset,
  630. gint level,
  631. GList **left,
  632. GList **right)
  633. {
  634. HTMLObject *dup, *prev;
  635. HTMLText *t1, *t2;
  636. gchar *tt;
  637. gint split_index;
  638. g_assert (self->parent);
  639. html_clue_remove_text_slaves (HTML_CLUE (self->parent));
  640. t1 = HTML_TEXT (self);
  641. dup = html_object_dup (self);
  642. tt = t1->text;
  643. split_index = html_text_get_index (t1, offset);
  644. t1->text = g_strndup (tt, split_index);
  645. t1->text_len = offset;
  646. t1->text_bytes = split_index;
  647. g_free (tt);
  648. html_text_convert_nbsp (t1, TRUE);
  649. t2 = HTML_TEXT (dup);
  650. tt = t2->text;
  651. t2->text = html_text_get_text (t2, offset);
  652. t2->text_len -= offset;
  653. t2->text_bytes -= split_index;
  654. split_attrs (t1, t2, split_index);
  655. split_links (t1, t2, offset, split_index);
  656. if (!html_text_convert_nbsp (t2, FALSE))
  657. t2->text = g_strdup (t2->text);
  658. g_free (tt);
  659. html_clue_append_after (HTML_CLUE (self->parent), dup, self);
  660. prev = self->prev;
  661. if (t1->text_len == 0 && prev && html_object_merge (prev, self, e, NULL, NULL, NULL))
  662. self = prev;
  663. if (t2->text_len == 0 && dup->next)
  664. html_object_merge (dup, dup->next, e, NULL, NULL, NULL);
  665. /* printf ("--- before split offset %d dup len %d\n", offset, HTML_TEXT (dup)->text_len);
  666. * debug_spell_errors (HTML_TEXT (self)->spell_errors); */
  667. HTML_TEXT (self)->spell_errors = remove_spell_errors (HTML_TEXT (self)->spell_errors,
  668. offset, HTML_TEXT (dup)->text_len);
  669. HTML_TEXT (dup)->spell_errors = remove_spell_errors (HTML_TEXT (dup)->spell_errors,
  670. 0, HTML_TEXT (self)->text_len);
  671. move_spell_errors (HTML_TEXT (dup)->spell_errors, 0, - HTML_TEXT (self)->text_len);
  672. /* printf ("--- after split\n");
  673. * printf ("left\n");
  674. * debug_spell_errors (HTML_TEXT (self)->spell_errors);
  675. * printf ("right\n");
  676. * debug_spell_errors (HTML_TEXT (dup)->spell_errors);
  677. * printf ("---\n");
  678. */
  679. *left = g_list_prepend (*left, self);
  680. *right = g_list_prepend (*right, dup);
  681. html_object_change_set (self, HTML_CHANGE_ALL_CALC);
  682. html_object_change_set (dup, HTML_CHANGE_ALL_CALC);
  683. pango_info_destroy (HTML_TEXT (self));
  684. level--;
  685. if (level)
  686. html_object_split (self->parent, e, dup, 0, level, left, right);
  687. }
  688. static gboolean
  689. html_text_real_calc_size (HTMLObject *self,
  690. HTMLPainter *painter,
  691. GList **changed_objs)
  692. {
  693. self->width = 0;
  694. html_object_calc_preferred_width (self, painter);
  695. return FALSE;
  696. }
  697. static const gchar *
  698. html_utf8_strnchr (const gchar *s,
  699. gchar c,
  700. gint len,
  701. gint *offset)
  702. {
  703. const gchar *res = NULL;
  704. *offset = 0;
  705. while (s && *s && *offset < len) {
  706. if (*s == c) {
  707. res = s;
  708. break;
  709. }
  710. s = g_utf8_next_char (s);
  711. (*offset) ++;
  712. }
  713. return res;
  714. }
  715. gint
  716. html_text_text_line_length (const gchar *text,
  717. gint *line_offset,
  718. guint len,
  719. gint *tabs)
  720. {
  721. const gchar *tab, *found_tab;
  722. gint cl, l, skip, sum_skip;
  723. /* printf ("lo: %d len: %d t: '%s'\n", line_offset, len, text); */
  724. if (tabs)
  725. *tabs = 0;
  726. l = 0;
  727. sum_skip = skip = 0;
  728. tab = text;
  729. while (tab && (found_tab = html_utf8_strnchr (tab, '\t', len - l, &cl)) && l < len) {
  730. l += cl;
  731. if (l >= len)
  732. break;
  733. if (*line_offset != -1) {
  734. *line_offset += cl;
  735. skip = 8 - (*line_offset % 8);
  736. }
  737. tab = found_tab + 1;
  738. *line_offset += skip;
  739. if (*line_offset != -1)
  740. sum_skip += skip - 1;
  741. l++;
  742. if (tabs)
  743. (*tabs) ++;
  744. }
  745. if (*line_offset != -1)
  746. (*line_offset) += len - l;
  747. /* printf ("ll: %d\n", len + sum_skip); */
  748. return len + sum_skip;
  749. }
  750. static guint
  751. get_line_length (HTMLObject *self,
  752. HTMLPainter *p,
  753. gint line_offset)
  754. {
  755. return html_clueflow_tabs (HTML_CLUEFLOW (self->parent), p)
  756. ? html_text_text_line_length (HTML_TEXT (self)->text, &line_offset, HTML_TEXT (self)->text_len, NULL)
  757. : HTML_TEXT (self)->text_len;
  758. }
  759. gint
  760. html_text_get_line_offset (HTMLText *text,
  761. HTMLPainter *painter,
  762. gint offset)
  763. {
  764. gint line_offset = -1;
  765. if (html_clueflow_tabs (HTML_CLUEFLOW (HTML_OBJECT (text)->parent), painter)) {
  766. line_offset = html_clueflow_get_line_offset (HTML_CLUEFLOW (HTML_OBJECT (text)->parent),
  767. painter, HTML_OBJECT (text));
  768. if (offset) {
  769. gchar *s = text->text;
  770. while (offset > 0 && s && *s) {
  771. if (*s == '\t')
  772. line_offset += 8 - (line_offset % 8);
  773. else
  774. line_offset++;
  775. s = g_utf8_next_char (s);
  776. offset--;
  777. }
  778. }
  779. }
  780. return line_offset;
  781. }
  782. gint
  783. html_text_get_item_index (HTMLText *text,
  784. HTMLPainter *painter,
  785. gint offset,
  786. gint *item_offset)
  787. {
  788. HTMLTextPangoInfo *pi = html_text_get_pango_info (text, painter);
  789. gint idx = 0;
  790. if (pi->n > 0) {
  791. while (idx < pi->n - 1 && offset >= pi->entries[idx].glyph_item.item->num_chars) {
  792. offset -= pi->entries[idx].glyph_item.item->num_chars;
  793. idx++;
  794. }
  795. *item_offset = offset;
  796. }
  797. return idx;
  798. }
  799. static void
  800. update_asc_dsc (HTMLPainter *painter,
  801. PangoItem *item,
  802. gint *asc,
  803. gint *dsc)
  804. {
  805. PangoFontMetrics *pfm;
  806. pfm = pango_font_get_metrics (item->analysis.font, item->analysis.language);
  807. if (asc)
  808. *asc = MAX (*asc, pango_font_metrics_get_ascent (pfm));
  809. if (dsc)
  810. *dsc = MAX (*dsc, pango_font_metrics_get_descent (pfm));
  811. pango_font_metrics_unref (pfm);
  812. }
  813. static void
  814. html_text_get_attr_list_list (PangoAttrList *get_attrs,
  815. PangoAttrList *attr_list,
  816. gint start_index,
  817. gint end_index)
  818. {
  819. PangoAttrIterator *iter = pango_attr_list_get_iterator (attr_list);
  820. if (iter) {
  821. do {
  822. gint begin, end;
  823. pango_attr_iterator_range (iter, &begin, &end);
  824. if (MAX (begin, start_index) < MIN (end, end_index)) {
  825. GSList *c, *l = pango_attr_iterator_get_attrs (iter);
  826. for (c = l; c; c = c->next) {
  827. PangoAttribute *attr = (PangoAttribute *) c->data;
  828. if (attr->start_index < start_index)
  829. attr->start_index = 0;
  830. else
  831. attr->start_index -= start_index;
  832. if (attr->end_index > end_index)
  833. attr->end_index = end_index - start_index;
  834. else
  835. attr->end_index -= start_index;
  836. c->data = NULL;
  837. pango_attr_list_insert (get_attrs, attr);
  838. }
  839. g_slist_free (l);
  840. }
  841. } while (pango_attr_iterator_next (iter));
  842. pango_attr_iterator_destroy (iter);
  843. }
  844. }
  845. PangoAttrList *
  846. html_text_get_attr_list (HTMLText *text,
  847. gint start_index,
  848. gint end_index)
  849. {
  850. PangoAttrList *attrs = pango_attr_list_new ();
  851. html_text_get_attr_list_list (attrs, text->attr_list, start_index, end_index);
  852. if (text->extra_attr_list)
  853. html_text_get_attr_list_list (attrs, text->extra_attr_list, start_index, end_index);
  854. return attrs;
  855. }
  856. void
  857. html_text_calc_text_size (HTMLText *t,
  858. HTMLPainter *painter,
  859. gint start_byte_offset,
  860. guint len,
  861. HTMLTextPangoInfo *pi,
  862. GList *glyphs,
  863. gint *line_offset,
  864. gint *width,
  865. gint *asc,
  866. gint *dsc)
  867. {
  868. gchar *text = t->text + start_byte_offset;
  869. html_painter_calc_entries_size (painter, text, len, pi, glyphs,
  870. line_offset, width, asc, dsc);
  871. }
  872. gint
  873. html_text_calc_part_width (HTMLText *text,
  874. HTMLPainter *painter,
  875. gchar *start,
  876. gint offset,
  877. gint len,
  878. gint *asc,
  879. gint *dsc)
  880. {
  881. gint idx, width = 0, line_offset;
  882. gint ascent = 0, descent = 0; /* Quiet GCC */
  883. gboolean need_ascent_descent = asc || dsc;
  884. HTMLTextPangoInfo *pi;
  885. PangoLanguage *language = NULL;
  886. PangoFont *font = NULL;
  887. gchar *s;
  888. if (offset < 0)
  889. return 0;
  890. if (offset + len > text->text_len)
  891. return 0;
  892. if (need_ascent_descent) {
  893. ascent = html_painter_engine_to_pango (painter,
  894. html_painter_get_space_asc (painter, html_text_get_font_style (text), text->face));
  895. descent = html_painter_engine_to_pango (painter,
  896. html_painter_get_space_dsc (painter, html_text_get_font_style (text), text->face));
  897. }
  898. if (text->text_len == 0 || len == 0)
  899. goto out;
  900. line_offset = html_text_get_line_offset (text, painter, offset);
  901. if (start == NULL)
  902. start = html_text_get_text (text, offset);
  903. s = start;
  904. pi = html_text_get_pango_info (text, painter);
  905. idx = html_text_get_item_index (text, painter, offset, &offset);
  906. if (need_ascent_descent) {
  907. update_asc_dsc (painter, pi->entries[idx].glyph_item.item, &ascent, &descent);
  908. font = pi->entries[idx].glyph_item.item->analysis.font;
  909. language = pi->entries[idx].glyph_item.item->analysis.language;
  910. }
  911. while (len > 0) {
  912. gint old_idx;
  913. if (*s == '\t') {
  914. gint skip = 8 - (line_offset % 8);
  915. width += skip * pi->entries[idx].widths[offset];
  916. line_offset += skip;
  917. } else {
  918. width += pi->entries[idx].widths[offset];
  919. line_offset++;
  920. }
  921. len--;
  922. old_idx = idx;
  923. if (html_text_pi_forward (pi, &idx, &offset) && idx != old_idx)
  924. if (len > 0 && (need_ascent_descent) && (pi->entries[idx].glyph_item.item->analysis.font != font
  925. || pi->entries[idx].glyph_item.item->analysis.language != language)) {
  926. update_asc_dsc (painter, pi->entries[idx].glyph_item.item, &ascent, &descent);
  927. }
  928. s = g_utf8_next_char (s);
  929. }
  930. out:
  931. if (asc)
  932. *asc = html_painter_pango_to_engine (painter, ascent);
  933. if (dsc)
  934. *dsc = html_painter_pango_to_engine (painter, descent);
  935. return html_painter_pango_to_engine (painter, width);
  936. }
  937. static gint
  938. calc_preferred_width (HTMLObject *self,
  939. HTMLPainter *painter)
  940. {
  941. HTMLText *text;
  942. gint width;
  943. text = HTML_TEXT (self);
  944. width = html_text_calc_part_width (text, painter, text->text, 0, text->text_len, &self->ascent, &self->descent);
  945. self->y = self->ascent;
  946. if (html_clueflow_tabs (HTML_CLUEFLOW (self->parent), painter)) {
  947. gint line_offset;
  948. gint tabs;
  949. line_offset = html_text_get_line_offset (text, painter, 0);
  950. width += (html_text_text_line_length (text->text, &line_offset, text->text_len, &tabs) - text->text_len)*
  951. html_painter_get_space_width (painter, html_text_get_font_style (text), text->face);
  952. }
  953. return MAX (1, width);
  954. }
  955. static void
  956. remove_text_slaves (HTMLObject *self)
  957. {
  958. HTMLObject *next_obj;
  959. /* Remove existing slaves */
  960. next_obj = self->next;
  961. while (next_obj != NULL
  962. && (HTML_OBJECT_TYPE (next_obj) == HTML_TYPE_TEXTSLAVE)) {
  963. self->next = next_obj->next;
  964. html_clue_remove (HTML_CLUE (next_obj->parent), next_obj);
  965. html_object_destroy (next_obj);
  966. next_obj = self->next;
  967. }
  968. }
  969. static HTMLFitType
  970. ht_fit_line (HTMLObject *o,
  971. HTMLPainter *painter,
  972. gboolean startOfLine,
  973. gboolean firstRun,
  974. gboolean next_to_floating,
  975. gint widthLeft)
  976. {
  977. HTMLText *text;
  978. HTMLObject *text_slave;
  979. text = HTML_TEXT (o);
  980. remove_text_slaves (o);
  981. /* Turn all text over to our slaves */
  982. text_slave = html_text_slave_new (text, 0, HTML_TEXT (text)->text_len);
  983. html_clue_append_after (HTML_CLUE (o->parent), text_slave, o);
  984. return HTML_FIT_COMPLETE;
  985. }
  986. #if 0 /* No longer used? */
  987. static gint
  988. min_word_width_calc_tabs (HTMLText *text,
  989. HTMLPainter *p,
  990. gint idx,
  991. gint *len)
  992. {
  993. gchar *str, *end;
  994. gint rv = 0, line_offset, wt, wl, i;
  995. gint epos;
  996. gboolean tab = FALSE;
  997. if (!html_clueflow_tabs (HTML_CLUEFLOW (HTML_OBJECT (text)->parent), p))
  998. return 0;
  999. /* printf ("tabs %d\n", idx); */
  1000. str = text->text;
  1001. i = idx;
  1002. while (i > 0 && *str) {
  1003. if (*str == ' ')
  1004. i--;
  1005. str = g_utf8_next_char (str);
  1006. }
  1007. if (!*str)
  1008. return 0;
  1009. epos = 0;
  1010. end = str;
  1011. while (*end && *end != ' ') {
  1012. tab |= *end == '\t';
  1013. end = g_utf8_next_char (end);
  1014. epos++;
  1015. }
  1016. if (tab) {
  1017. line_offset = 0;
  1018. if (idx == 0) {
  1019. HTMLObject *prev;
  1020. prev = html_object_prev_not_slave (HTML_OBJECT (text));
  1021. if (prev && html_object_is_text (prev) /* FIXME-words && HTML_TEXT (prev)->words > 0 */) {
  1022. min_word_width_calc_tabs (HTML_TEXT (prev), p, /* FIXME-words HTML_TEXT (prev)->words - 1 */ HTML_TEXT (prev)->text_len - 1, &line_offset);
  1023. /* printf ("lo: %d\n", line_offset); */
  1024. }
  1025. }
  1026. wl = html_text_text_line_length (str, &line_offset, epos, &wt);
  1027. } else {
  1028. wl = epos;
  1029. }
  1030. rv = wl - epos;
  1031. if (len)
  1032. *len = wl;
  1033. /* printf ("tabs delta %d\n", rv); */
  1034. return rv;
  1035. }
  1036. #endif
  1037. gint
  1038. html_text_pango_info_get_index (HTMLTextPangoInfo *pi,
  1039. gint byte_offset,
  1040. gint idx)
  1041. {
  1042. while (idx < pi->n && pi->entries[idx].glyph_item.item->offset + pi->entries[idx].glyph_item.item->length <= byte_offset)
  1043. idx++;
  1044. return idx;
  1045. }
  1046. static void
  1047. html_text_add_cite_color (PangoAttrList *attrs,
  1048. HTMLText *text,
  1049. HTMLClueFlow *flow,
  1050. HTMLEngine *e)
  1051. {
  1052. HTMLColor *cite_color = html_colorset_get_color (e->settings->color_set, HTMLCiteColor);
  1053. if (cite_color && flow->levels->len > 0 && flow->levels->data[0] == HTML_LIST_TYPE_BLOCKQUOTE_CITE) {
  1054. PangoAttribute *attr;
  1055. attr = pango_attr_foreground_new (cite_color->color.red, cite_color->color.green, cite_color->color.blue);
  1056. attr->start_index = 0;
  1057. attr->end_index = text->text_bytes;
  1058. pango_attr_list_change (attrs, attr);
  1059. }
  1060. }
  1061. void
  1062. html_text_remove_unwanted_line_breaks (gchar *s,
  1063. gint len,
  1064. PangoLogAttr *attrs)
  1065. {
  1066. gint i;
  1067. gunichar last_uc = 0;
  1068. for (i = 0; i < len; i++) {
  1069. gunichar uc = g_utf8_get_char (s);
  1070. if (attrs[i].is_line_break) {
  1071. if (last_uc == '.' || last_uc == '/' ||
  1072. last_uc == '-' || last_uc == '$' ||
  1073. last_uc == '+' || last_uc == '?' ||
  1074. last_uc == ')' ||
  1075. last_uc == '}' ||
  1076. last_uc == ']' ||
  1077. last_uc == '>')
  1078. attrs[i].is_line_break = 0;
  1079. else if ((uc == '(' ||
  1080. uc == '{' ||
  1081. uc == '[' ||
  1082. uc == '<'
  1083. )
  1084. && i > 0 && !attrs[i - 1].is_white)
  1085. attrs[i].is_line_break = 0;
  1086. }
  1087. s = g_utf8_next_char (s);
  1088. last_uc = uc;
  1089. }
  1090. }
  1091. PangoAttrList *
  1092. html_text_prepare_attrs (HTMLText *text,
  1093. HTMLPainter *painter)
  1094. {
  1095. PangoAttrList *attrs;
  1096. HTMLClueFlow *flow = NULL;
  1097. HTMLEngine *e = NULL;
  1098. PangoAttribute *attr;
  1099. attrs = pango_attr_list_new ();
  1100. if (HTML_OBJECT (text)->parent && HTML_IS_CLUEFLOW (HTML_OBJECT (text)->parent))
  1101. flow = HTML_CLUEFLOW (HTML_OBJECT (text)->parent);
  1102. if (painter->widget && GTK_IS_HTML (painter->widget))
  1103. e = html_object_engine (HTML_OBJECT (text), GTK_HTML (painter->widget)->engine);
  1104. if (flow && e) {
  1105. html_text_add_cite_color (attrs, text, flow, e);
  1106. }
  1107. if (HTML_IS_PLAIN_PAINTER (painter)) {
  1108. attr = pango_attr_family_new (painter->font_manager.fixed.face ? painter->font_manager.fixed.face : "Monospace");
  1109. attr->start_index = 0;
  1110. attr->end_index = text->text_bytes;
  1111. pango_attr_list_insert (attrs, attr);
  1112. if (painter->font_manager.fix_size != painter->font_manager.var_size || fabs (painter->font_manager.magnification - 1.0) > 0.001) {
  1113. attr = pango_attr_size_new (painter->font_manager.fix_size * painter->font_manager.magnification);
  1114. attr->start_index = 0;
  1115. attr->end_index = text->text_bytes;
  1116. pango_attr_list_insert (attrs, attr);
  1117. }
  1118. } else {
  1119. if (fabs (painter->font_manager.magnification - 1.0) > 0.001) {
  1120. attr = pango_attr_size_new (painter->font_manager.var_size * painter->font_manager.magnification);
  1121. attr->start_index = 0;
  1122. attr->end_index = text->text_bytes;
  1123. pango_attr_list_insert (attrs, attr);
  1124. }
  1125. pango_attr_list_splice (attrs, text->attr_list, 0, 0);
  1126. }
  1127. if (text->extra_attr_list)
  1128. pango_attr_list_splice (attrs, text->extra_attr_list, 0, 0);
  1129. if (!HTML_IS_PLAIN_PAINTER (painter)) {
  1130. if (flow && e)
  1131. html_text_change_attrs (attrs, html_clueflow_get_default_font_style (flow), e, 0, text->text_bytes, TRUE);
  1132. }
  1133. if (text->links && e) {
  1134. HTMLColor *link_color;
  1135. GSList *l;
  1136. for (l = text->links; l; l = l->next) {
  1137. Link *link;
  1138. link = (Link *) l->data;
  1139. if (link->is_visited == FALSE)
  1140. link_color = html_colorset_get_color (e->settings->color_set, HTMLLinkColor);
  1141. else
  1142. link_color = html_colorset_get_color (e->settings->color_set, HTMLVLinkColor);
  1143. attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
  1144. attr->start_index = link->start_index;
  1145. attr->end_index = link->end_index;
  1146. pango_attr_list_change (attrs, attr);
  1147. attr = pango_attr_foreground_new (link_color->color.red, link_color->color.green, link_color->color.blue);
  1148. attr->start_index = link->start_index;
  1149. attr->end_index = link->end_index;
  1150. pango_attr_list_change (attrs, attr);
  1151. }
  1152. }
  1153. return attrs;
  1154. }
  1155. PangoDirection
  1156. html_text_get_pango_direction (HTMLText *text)
  1157. {
  1158. if (HTML_OBJECT (text)->change & HTML_CHANGE_RECALC_PI)
  1159. return pango_find_base_dir (text->text, text->text_bytes);
  1160. else
  1161. return text->direction;
  1162. }
  1163. static PangoDirection
  1164. get_pango_base_direction (HTMLText *text)
  1165. {
  1166. switch (html_object_get_direction (HTML_OBJECT (text))) {
  1167. case HTML_DIRECTION_RTL:
  1168. return PANGO_DIRECTION_RTL;
  1169. case HTML_DIRECTION_LTR:
  1170. return PANGO_DIRECTION_LTR;
  1171. case HTML_DIRECTION_DERIVED:
  1172. default:
  1173. if (text->text)
  1174. return html_text_get_pango_direction (text);
  1175. else
  1176. return HTML_DIRECTION_LTR;
  1177. }
  1178. }
  1179. /**
  1180. * pango_glyph_string_get_logical_widths:
  1181. * @glyphs: a #PangoGlyphString
  1182. * @text: the text corresponding to the glyphs
  1183. * @length: the length of @text, in bytes
  1184. * @embedding_level: the embedding level of the string
  1185. * @logical_widths: an array whose length is g_utf8_strlen (text, length)
  1186. * to be filled in with the resulting character widths.
  1187. *
  1188. * Given a #PangoGlyphString resulting from pango_shape() and the corresponding
  1189. * text, determine the screen width corresponding to each character. When
  1190. * multiple characters compose a single cluster, the width of the entire
  1191. * cluster is divided equally among the characters.
  1192. **/
  1193. void
  1194. html_tmp_fix_pango_glyph_string_get_logical_widths (PangoGlyphString *glyphs,
  1195. const gchar *text,
  1196. gint length,
  1197. gint embedding_level,
  1198. gint *logical_widths)
  1199. {
  1200. gint i, j;
  1201. gint last_cluster = 0;
  1202. gint width = 0;
  1203. gint last_cluster_width = 0;
  1204. const gchar *p = text; /* Points to start of current cluster */
  1205. /* printf ("html_tmp_fix_pango_glyph_string_get_logical_widths"); */
  1206. for (i = 0; i <= glyphs->num_glyphs; i++)
  1207. {
  1208. gint glyph_index = (embedding_level % 2 == 0) ? i : glyphs->num_glyphs - i - 1;
  1209. /* If this glyph belongs to a new cluster, or we're at the end, find
  1210. * the start of the next cluster, and assign the widths for this cluster.
  1211. */
  1212. if (i == glyphs->num_glyphs || p != text + glyphs->log_clusters[glyph_index])
  1213. {
  1214. gint next_cluster = last_cluster;
  1215. if (i < glyphs->num_glyphs)
  1216. {
  1217. while (p < text + glyphs->log_clusters[glyph_index])
  1218. {
  1219. next_cluster++;
  1220. p = g_utf8_next_char (p);
  1221. }
  1222. }
  1223. else
  1224. {
  1225. while (p < text + length)
  1226. {
  1227. next_cluster++;
  1228. p = g_utf8_next_char (p);
  1229. }
  1230. }
  1231. for (j = last_cluster; j < next_cluster; j++) {
  1232. logical_widths[j] = (width - last_cluster_width) / (next_cluster - last_cluster);
  1233. /* printf (" %d", logical_widths [j]); */
  1234. }
  1235. if (last_cluster != next_cluster) {
  1236. last_cluster = next_cluster;
  1237. last_cluster_width = width;
  1238. }
  1239. }
  1240. if (i < glyphs->num_glyphs)
  1241. width += glyphs->glyphs[glyph_index].geometry.width;
  1242. }
  1243. /* printf ("\n"); */
  1244. }
  1245. static void
  1246. html_text_shape_tab (HTMLText *text,
  1247. PangoGlyphString *glyphs)
  1248. {
  1249. /* copied from pango sources */
  1250. pango_glyph_string_set_size (glyphs, 1);
  1251. glyphs->glyphs[0].glyph = EMPTY_GLYPH;
  1252. glyphs->glyphs[0].geometry.x_offset = 0;
  1253. glyphs->glyphs[0].geometry.y_offset = 0;
  1254. glyphs->glyphs[0].attr.is_cluster_start = 1;
  1255. glyphs->log_clusters[0] = 0;
  1256. glyphs->glyphs[0].geometry.width = 48 * PANGO_SCALE;
  1257. }
  1258. HTMLTextPangoInfo *
  1259. html_text_get_pango_info (HTMLText *text,
  1260. HTMLPainter *painter)
  1261. {
  1262. if (HTML_OBJECT (text)->change & HTML_CHANGE_RECALC_PI) {
  1263. pango_info_destroy (text);
  1264. HTML_OBJECT (text)->change &= ~HTML_CHANGE_RECALC_PI;
  1265. text->direction = pango_find_base_dir (text->text, text->text_bytes);
  1266. }
  1267. if (!text->pi) {
  1268. GList *items, *cur;
  1269. PangoAttrList *attrs;
  1270. gint i, offset;
  1271. attrs = html_text_prepare_attrs (text, painter);
  1272. items = pango_itemize_with_base_dir (painter->pango_context, get_pango_base_direction (text), text->text, 0, text->text_bytes, attrs, NULL);
  1273. pango_attr_list_unref (attrs);
  1274. /* create pango info */
  1275. text->pi = html_text_pango_info_new (g_list_length (items));
  1276. text->pi->have_font = TRUE;
  1277. text->pi->font_style = html_text_get_font_style (text);
  1278. text->pi->face = g_strdup (text->face);
  1279. text->pi->attrs = g_new (PangoLogAttr, text->text_len + 1);
  1280. /* get line breaks */
  1281. offset = 0;
  1282. for (cur = items; cur; cur = cur->next) {
  1283. PangoItem tmp_item;
  1284. PangoItem *item;
  1285. gint start_offset;
  1286. start_offset = offset;
  1287. item = (PangoItem *) cur->data;
  1288. offset += item->num_chars;
  1289. tmp_item = *item;
  1290. while (cur->next) {
  1291. PangoItem *next_item = (PangoItem *) cur->next->data;
  1292. if (tmp_item.analysis.lang_engine == next_item->analysis.lang_engine) {
  1293. tmp_item.length += next_item->length;
  1294. tmp_item.num_chars += next_item->num_chars;
  1295. offset += next_item->num_chars;
  1296. cur = cur->next;
  1297. } else
  1298. break;
  1299. }
  1300. pango_break (text->text + tmp_item.offset, tmp_item.length, &tmp_item.analysis, text->pi->attrs + start_offset, tmp_item.num_chars + 1);
  1301. }
  1302. if (text->pi && text->pi->attrs)
  1303. html_text_remove_unwanted_line_breaks (text->text, text->text_len, text->pi->attrs);
  1304. for (i = 0, cur = items; i < text->pi->n; i++, cur = cur->next)
  1305. text->pi->entries[i].glyph_item.item = (PangoItem *) cur->data;
  1306. for (i = 0; i < text->pi->n; i++) {
  1307. PangoItem *item;
  1308. PangoGlyphString *glyphs;
  1309. item = text->pi->entries[i].glyph_item.item;
  1310. glyphs = text->pi->entries[i].glyph_item.glyphs = pango_glyph_string_new ();
  1311. /* printf ("item pos %d len %d\n", item->offset, item->length); */
  1312. text->pi->entries[i].widths = g_new (PangoGlyphUnit, item->num_chars);
  1313. if (text->text[item->offset] == '\t')
  1314. html_text_shape_tab (text, glyphs);
  1315. else
  1316. pango_shape (text->text + item->offset, item->length, &item->analysis, glyphs);
  1317. html_tmp_fix_pango_glyph_string_get_logical_widths (glyphs, text->text + item->offset, item->length,
  1318. item->analysis.level, text->pi->entries[i].widths);
  1319. }
  1320. g_list_free (items);
  1321. }
  1322. return text->pi;
  1323. }
  1324. gboolean
  1325. html_text_pi_backward (HTMLTextPangoInfo *pi,
  1326. gint *ii,
  1327. gint *io)
  1328. {
  1329. if (*io <= 0) {
  1330. if (*ii <= 0)
  1331. return FALSE;
  1332. (*ii) --;
  1333. *io = pi->entries [*ii].glyph_item.item->num_chars - 1;
  1334. } else
  1335. (*io) --;
  1336. return TRUE;
  1337. }
  1338. gboolean
  1339. html_text_pi_forward (HTMLTextPangoInfo *pi,
  1340. gint *ii,
  1341. gint *io)
  1342. {
  1343. if (*io >= pi->entries[*ii].glyph_item.item->num_chars - 1) {
  1344. if (*ii >= pi->n -1)
  1345. return FALSE;
  1346. (*ii) ++;
  1347. *io = 0;
  1348. } else
  1349. (*io) ++;
  1350. return TRUE;
  1351. }
  1352. /**
  1353. * html_text_tail_white_space:
  1354. * @text: a #HTMLText object
  1355. * @painter: a #HTMLPainter object
  1356. * @offset: offset into the text of @text, in characters
  1357. * @ii: index of current item
  1358. * @io: offset within current item, in characters
  1359. * @white_len: length of found trailing white space, in characters
  1360. * @line_offset:
  1361. * @s: pointer into the text of @text corresponding to @offset
  1362. *
  1363. * Used to chop off one character worth of whitespace at a particular position.
  1364. *
  1365. * Return value: width of found trailing white space, in Pango units
  1366. **/
  1367. gint
  1368. html_text_tail_white_space (HTMLText *text,
  1369. HTMLPainter *painter,
  1370. gint offset,
  1371. gint ii,
  1372. gint io,
  1373. gint *white_len,
  1374. gint line_offset,
  1375. gchar *s)
  1376. {
  1377. HTMLTextPangoInfo *pi = html_text_get_pango_info (text, painter);
  1378. gint wl = 0;
  1379. gint ww = 0;
  1380. if (html_text_pi_backward (pi, &ii, &io)) {
  1381. s = g_utf8_prev_char (s);
  1382. offset--;
  1383. if (pi->attrs[offset].is_white) {
  1384. if (*s == '\t' && offset > 1) {
  1385. gint skip = 8, co = offset - 1;
  1386. do {
  1387. s = g_utf8_prev_char (s);
  1388. co--;
  1389. if (*s != '\t')
  1390. skip--;
  1391. } while (s && co > 0 && *s != '\t');
  1392. ww += skip * pi->entries[ii].widths[io];
  1393. } else {
  1394. ww += pi->entries[ii].widths[io];
  1395. }
  1396. wl++;
  1397. }
  1398. }
  1399. if (white_len)
  1400. *white_len = wl;
  1401. return ww;
  1402. }
  1403. static void
  1404. update_mw (HTMLText *text,
  1405. HTMLPainter *painter,
  1406. gint offset,
  1407. gint *last_offset,
  1408. gint *ww,
  1409. gint *mw,
  1410. gint ii,
  1411. gint io,
  1412. gchar *s,
  1413. gint line_offset) {
  1414. *ww -= html_text_tail_white_space (text, painter, offset, ii, io, NULL, line_offset, s);
  1415. if (*ww > *mw)
  1416. *mw = *ww;
  1417. *ww = 0;
  1418. *last_offset = offset;
  1419. }
  1420. gboolean
  1421. html_text_is_line_break (PangoLogAttr attr)
  1422. {
  1423. return attr.is_line_break;
  1424. }
  1425. static gint
  1426. calc_min_width (HTMLObject *self,
  1427. HTMLPainter *painter)
  1428. {
  1429. HTMLText *text = HTML_TEXT (self);
  1430. HTMLTextPangoInfo *pi = html_text_get_pango_info (text, painter);
  1431. gint mw = 0, ww;
  1432. gint ii, io, offset, last_offset, line_offset;
  1433. gchar *s;
  1434. ww = 0;
  1435. last_offset = offset = 0;
  1436. ii = io = 0;
  1437. line_offset = html_text_get_line_offset (text, painter, 0);
  1438. s = text->text;
  1439. while (offset < text->text_len) {
  1440. if (offset > 0 && html_text_is_line_break (pi->attrs[offset]))
  1441. update_mw (text, painter, offset, &last_offset, &ww, &mw, ii, io, s, line_offset);
  1442. if (*s == '\t') {
  1443. gint skip = 8 - (line_offset % 8);
  1444. ww += skip * pi->entries[ii].widths[io];
  1445. line_offset += skip;
  1446. } else {
  1447. ww += pi->entries[ii].widths[io];
  1448. line_offset++;
  1449. }
  1450. s = g_utf8_next_char (s);
  1451. offset++;
  1452. html_text_pi_forward (pi, &ii, &io);
  1453. }
  1454. if (ww > mw)
  1455. mw = ww;
  1456. return MAX (1, html_painter_pango_to_engine (painter, mw));
  1457. }
  1458. static void
  1459. draw (HTMLObject *o,
  1460. HTMLPainter *p,
  1461. gint x,
  1462. gint y,
  1463. gint width,
  1464. gint height,
  1465. gint tx,
  1466. gint ty)
  1467. {
  1468. }
  1469. static gboolean
  1470. accepts_cursor (HTMLObject *object)
  1471. {
  1472. return TRUE;
  1473. }
  1474. static gboolean
  1475. save_open_attrs (HTMLEngineSaveState *state,
  1476. GSList *attrs)
  1477. {
  1478. gboolean rv = TRUE;
  1479. for (; attrs; attrs = attrs->next) {
  1480. PangoAttribute *attr = (PangoAttribute *) attrs->data;
  1481. HTMLEngine *e = state->engine;
  1482. const gchar *tag = NULL;
  1483. gboolean free_tag = FALSE;
  1484. switch (attr->klass->type) {
  1485. case PANGO_ATTR_WEIGHT:
  1486. tag = "<B>";
  1487. break;
  1488. case PANGO_ATTR_STYLE:
  1489. tag = "<I>";
  1490. break;
  1491. case PANGO_ATTR_UNDERLINE:
  1492. tag = "<U>";
  1493. break;
  1494. case PANGO_ATTR_STRIKETHROUGH:
  1495. tag = "<S>";
  1496. break;
  1497. case PANGO_ATTR_SIZE:
  1498. if (attr->klass == &html_pango_attr_font_size_klass) {
  1499. HTMLPangoAttrFontSize *size = (HTMLPangoAttrFontSize *) attr;
  1500. if ((size->style & GTK_HTML_FONT_STYLE_SIZE_MASK) != GTK_HTML_FONT_STYLE_SIZE_3 && (size->style & GTK_HTML_FONT_STYLE_SIZE_MASK) != 0) {
  1501. tag = g_strdup_printf ("<FONT SIZE=\"%d\">", size->style & GTK_HTML_FONT_STYLE_SIZE_MASK);
  1502. free_tag = TRUE;
  1503. }
  1504. }
  1505. break;
  1506. case PANGO_ATTR_FAMILY: {
  1507. PangoAttrString *family_attr = (PangoAttrString *) attr;
  1508. if (!g_ascii_strcasecmp (e->painter->font_manager.fixed.face
  1509. ? e->painter->font_manager.fixed.face : "Monospace",
  1510. family_attr->value))
  1511. tag = "<TT>";
  1512. }
  1513. break;
  1514. case PANGO_ATTR_FOREGROUND: {
  1515. PangoAttrColor *color = (PangoAttrColor *) attr;
  1516. tag = g_strdup_printf ("<FONT COLOR=\"#%02x%02x%02x\">",
  1517. (color->color.red >> 8) & 0xff, (color->color.green >> 8) & 0xff, (color->color.blue >> 8) & 0xff);
  1518. free_tag = TRUE;
  1519. }
  1520. break;
  1521. default:
  1522. break;
  1523. }
  1524. if (tag) {
  1525. if (!html_engine_save_output_string (state, "%s", tag))
  1526. rv = FALSE;
  1527. if (free_tag)
  1528. g_free ((gpointer) tag);
  1529. if (!rv)
  1530. break;
  1531. }
  1532. }
  1533. return TRUE;
  1534. }
  1535. static gboolean
  1536. save_close_attrs (HTMLEngineSaveState *state,
  1537. GSList *attrs)
  1538. {
  1539. for (; attrs; attrs = attrs->next) {
  1540. PangoAttribute *attr = (PangoAttribute *) attrs->data;
  1541. HTMLEngine *e = state->engine;
  1542. const gchar *tag = NULL;
  1543. switch (attr->klass->type) {
  1544. case PANGO_ATTR_WEIGHT:
  1545. tag = "</B>";
  1546. break;
  1547. case PANGO_ATTR_STYLE:
  1548. tag = "</I>";
  1549. break;
  1550. case PANGO_ATTR_UNDERLINE:
  1551. tag = "</U>";
  1552. break;
  1553. case PANGO_ATTR_STRIKETHROUGH:
  1554. tag = "</S>";
  1555. break;
  1556. case PANGO_ATTR_SIZE:
  1557. if (attr->klass == &html_pango_attr_font_size_klass) {
  1558. HTMLPangoAttrFontSize *size = (HTMLPangoAttrFontSize *) attr;
  1559. if ((size->style & GTK_HTML_FONT_STYLE_SIZE_MASK) != GTK_HTML_FONT_STYLE_SIZE_3 && (size->style & GTK_HTML_FONT_STYLE_SIZE_MASK) != 0)
  1560. tag = "</FONT>";
  1561. }
  1562. break;
  1563. case PANGO_ATTR_FOREGROUND:
  1564. tag = "</FONT>";
  1565. break;
  1566. case PANGO_ATTR_FAMILY: {
  1567. PangoAttrString *family_attr = (PangoAttrString *) attr;
  1568. if (!g_ascii_strcasecmp (e->painter->font_manager.fixed.face
  1569. ? e->painter->font_manager.fixed.face : "Monospace",
  1570. family_attr->value))
  1571. tag = "</TT>";
  1572. }
  1573. break;
  1574. default:
  1575. break;
  1576. }
  1577. if (tag)
  1578. if (!html_engine_save_output_string (state, "%s", tag))
  1579. return FALSE;
  1580. }
  1581. return TRUE;
  1582. }
  1583. static gboolean
  1584. save_text_part (HTMLText *text,
  1585. HTMLEngineSaveState *state,
  1586. guint start_index,
  1587. guint end_index)
  1588. {
  1589. gchar *str;
  1590. gint len;
  1591. gboolean rv;
  1592. str = g_strndup (text->text + start_index, end_index - start_index);
  1593. len = g_utf8_pointer_to_offset (text->text + start_index, text->text + end_index);
  1594. rv = html_engine_save_encode (state, str, len);
  1595. g_free (str);
  1596. return rv;
  1597. }
  1598. static gboolean
  1599. save_link_open (Link *link,
  1600. HTMLEngineSaveState *state)
  1601. {
  1602. return html_engine_save_delims_and_vals (state,
  1603. "<A HREF=\"", link->url,
  1604. "\">", NULL);
  1605. }
  1606. static gboolean
  1607. save_link_close (Link *link,
  1608. HTMLEngineSaveState *state)
  1609. {
  1610. return html_engi

Large files files are truncated, but you can click here to view the full file