1//========================================================================
2//
3// This file comes from pdftohtml project
4// http://pdftohtml.sourceforge.net
5//
6// Copyright from:
7// Gueorgui Ovtcharov
8// Rainer Dorsch <http://www.ra.informatik.uni-stuttgart.de/~rainer/>
9// Mikhail Kruk <meshko@cs.brandeis.edu>
10//
11//========================================================================
12
13//========================================================================
14//
15// Modified under the Poppler project - http://poppler.freedesktop.org
16//
17// All changes made under the Poppler project to this file are licensed
18// under GPL version 2 or later
19//
20// Copyright (C) 2008 Boris Toloknov <tlknv@yandex.ru>
21// Copyright (C) 2010, 2021, 2022 Albert Astals Cid <aacid@kde.org>
22// Copyright (C) 2013 Julien Nabet <serval2412@yahoo.fr>
23//
24// To see a description of the changes please see the Changelog file that
25// came with your tarball or type make ChangeLog if you are building from git
26//
27//========================================================================
28
29#include "HtmlLinks.h"
30
31extern bool xml;
32
33HtmlLink::HtmlLink(const HtmlLink &x)
34{
35 Xmin = x.Xmin;
36 Ymin = x.Ymin;
37 Xmax = x.Xmax;
38 Ymax = x.Ymax;
39 dest = new GooString(x.dest);
40}
41
42HtmlLink::HtmlLink(double xmin, double ymin, double xmax, double ymax, GooString *_dest)
43{
44 if (xmin < xmax) {
45 Xmin = xmin;
46 Xmax = xmax;
47 } else {
48 Xmin = xmax;
49 Xmax = xmin;
50 }
51 if (ymin < ymax) {
52 Ymin = ymin;
53 Ymax = ymax;
54 } else {
55 Ymin = ymax;
56 Ymax = ymin;
57 }
58 dest = new GooString(_dest);
59}
60
61HtmlLink::~HtmlLink()
62{
63 delete dest;
64}
65
66bool HtmlLink::isEqualDest(const HtmlLink &x) const
67{
68 return (!strcmp(s1: dest->c_str(), s2: x.dest->c_str()));
69}
70
71bool HtmlLink::inLink(double xmin, double ymin, double xmax, double ymax) const
72{
73 double y = (ymin + ymax) / 2;
74 if (y > Ymax) {
75 return false;
76 }
77 return (y > Ymin) && (xmin < Xmax) && (xmax > Xmin);
78}
79
80static GooString *EscapeSpecialChars(GooString *s)
81{
82 GooString *tmp = nullptr;
83 for (int i = 0, j = 0; i < s->getLength(); i++, j++) {
84 const char *replace = nullptr;
85 switch (s->getChar(i)) {
86 case '"':
87 replace = "&quot;";
88 break;
89 case '&':
90 replace = "&amp;";
91 break;
92 case '<':
93 replace = "&lt;";
94 break;
95 case '>':
96 replace = "&gt;";
97 break;
98 default:
99 continue;
100 }
101 if (replace) {
102 if (!tmp) {
103 tmp = new GooString(s);
104 }
105 if (tmp) {
106 tmp->del(i: j, n: 1);
107 int l = strlen(s: replace);
108 tmp->insert(i: j, str: replace, lengthA: l);
109 j += l - 1;
110 }
111 }
112 }
113 return tmp ? tmp : s;
114}
115
116GooString *HtmlLink::getLinkStart() const
117{
118 GooString *res = new GooString("<a href=\"");
119 GooString *d = xml ? EscapeSpecialChars(s: dest) : dest;
120 res->append(str: d);
121 if (d != dest) {
122 delete d;
123 }
124 res->append(str: "\">");
125 return res;
126}
127
128/*GooString* HtmlLink::Link(GooString* content){
129 //GooString* _dest=new GooString(dest);
130 GooString *tmp=new GooString("<a href=\"");
131 tmp->append(dest);
132 tmp->append("\">");
133 tmp->append(content);
134 tmp->append("</a>");
135 //delete _dest;
136 return tmp;
137 }*/
138
139HtmlLinks::HtmlLinks() { }
140
141HtmlLinks::~HtmlLinks() { }
142
143bool HtmlLinks::inLink(double xmin, double ymin, double xmax, double ymax, size_t &p) const
144{
145
146 for (std::vector<HtmlLink>::const_iterator i = accu.begin(); i != accu.end(); ++i) {
147 if (i->inLink(xmin, ymin, xmax, ymax)) {
148 p = (i - accu.begin());
149 return true;
150 }
151 }
152 return false;
153}
154
155const HtmlLink *HtmlLinks::getLink(size_t i) const
156{
157 return &accu[i];
158}
159

source code of poppler/utils/HtmlLinks.cc