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 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 
31 extern bool xml;
32 
HtmlLink(const HtmlLink & x)33 HtmlLink::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 
HtmlLink(double xmin,double ymin,double xmax,double ymax,GooString * _dest)42 HtmlLink::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 
~HtmlLink()61 HtmlLink::~HtmlLink()
62 {
63     delete dest;
64 }
65 
isEqualDest(const HtmlLink & x) const66 bool HtmlLink::isEqualDest(const HtmlLink &x) const
67 {
68     return (!strcmp(dest->c_str(), x.dest->c_str()));
69 }
70 
inLink(double xmin,double ymin,double xmax,double ymax) const71 bool 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     return (y > Ymin) && (xmin < Xmax) && (xmax > Xmin);
77 }
78 
EscapeSpecialChars(GooString * s)79 static GooString *EscapeSpecialChars(GooString *s)
80 {
81     GooString *tmp = nullptr;
82     for (int i = 0, j = 0; i < s->getLength(); i++, j++) {
83         const char *replace = nullptr;
84         switch (s->getChar(i)) {
85         case '"':
86             replace = "&quot;";
87             break;
88         case '&':
89             replace = "&amp;";
90             break;
91         case '<':
92             replace = "&lt;";
93             break;
94         case '>':
95             replace = "&gt;";
96             break;
97         default:
98             continue;
99         }
100         if (replace) {
101             if (!tmp)
102                 tmp = new GooString(s);
103             if (tmp) {
104                 tmp->del(j, 1);
105                 int l = strlen(replace);
106                 tmp->insert(j, replace, l);
107                 j += l - 1;
108             }
109         }
110     }
111     return tmp ? tmp : s;
112 }
113 
getLinkStart() const114 GooString *HtmlLink::getLinkStart() const
115 {
116     GooString *res = new GooString("<a href=\"");
117     GooString *d = xml ? EscapeSpecialChars(dest) : dest;
118     res->append(d);
119     if (d != dest)
120         delete d;
121     res->append("\">");
122     return res;
123 }
124 
125 /*GooString* HtmlLink::Link(GooString* content){
126   //GooString* _dest=new GooString(dest);
127   GooString *tmp=new GooString("<a href=\"");
128   tmp->append(dest);
129   tmp->append("\">");
130   tmp->append(content);
131   tmp->append("</a>");
132   //delete _dest;
133   return tmp;
134   }*/
135 
HtmlLinks()136 HtmlLinks::HtmlLinks() { }
137 
~HtmlLinks()138 HtmlLinks::~HtmlLinks() { }
139 
inLink(double xmin,double ymin,double xmax,double ymax,int & p) const140 bool HtmlLinks::inLink(double xmin, double ymin, double xmax, double ymax, int &p) const
141 {
142 
143     for (std::vector<HtmlLink>::const_iterator i = accu.begin(); i != accu.end(); ++i) {
144         if (i->inLink(xmin, ymin, xmax, ymax)) {
145             p = (i - accu.begin());
146             return true;
147         }
148     }
149     return false;
150 }
151 
getLink(int i) const152 const HtmlLink *HtmlLinks::getLink(int i) const
153 {
154     return &accu[i];
155 }
156