forked from jonathanverner/comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderTeX.cpp
165 lines (133 loc) · 4.67 KB
/
renderTeX.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/** This file is part of project comment
*
* File: renderTeX.cpp
* Created: 2008-11-17
* Author: Jonathan Verner <[email protected]>
* License: GPL v2 or later
*
* Copyright (C) 2010 Jonathan Verner <[email protected]>
*
* 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 "teXjob.h"
#include "renderTeX.h"
#include <QtCore/QDebug>
QCache<int, struct renderTeX::cachedPage> renderTeX::renderCache(200000);
renderTeX::renderTeX( QString preamb ):
preambule( preamb )
{
}
void renderTeX::setPaths( QString latex, QString gs ) {
compileJob::setPaths( latex, gs );
}
void renderTeX::setPreambule( QString preamb ) {
preambule = preamb;
}
/* This method should always succeed */
int renderTeX::addItem( QString source, QString preamb ) {
if ( preamb == "" ) preamb = preambule;
renderItem *it = new renderItem( source, preamb );
int id;
if ( available_ids.size() > 0 ) {
id = available_ids.pop();
items[id] = it;
} else {
items.append( it );
id = items.size()-1;
}
return id;
}
void renderTeX::setItem( int itemID, QString source, QString preamb ) {
if ( preamb == "" ) preamb = preambule;
renderItem *it = new renderItem( source, preamb );
if ( items.size() <= itemID ) items.resize( itemID + 10 );
else {
deleteItem( itemID );
available_ids.pop();
}
items[itemID] = it;
}
void renderTeX::deleteItem( int item ) {
Q_ASSERT( 0 <= item && item < items.size() );
delete items[item];
renderCache.remove( item );
items[item]=NULL;
available_ids.push(item);
}
void renderTeX::updateItem( int item, QString source, QString preamb ) {
Q_ASSERT( 0 <= item && item < items.size() && items[item] );
if ( preamb == "" ) preamb = preambule;
connect( items[item], SIGNAL(renderingReady(int)), this, SIGNAL(itemReady(int)) );
items[item]->updateItem( source, preamb, item );
renderCache.remove( item );
}
QPixmap renderTeX::render( int item, bool format_inline, qreal zoom, int sizeHint ) {
Q_ASSERT( 0 <= item && item < items.size() && items[item] );
struct cachedPage *pg = renderCache.object( item );
if ( pg ) {
if ( pg->format_inline == format_inline && pg->zoom == zoom ) return pg->pix;
if ( compileJob::pathsOK() ) {
pg->pix = items[item]->render( zoom, format_inline, sizeHint );
pg->format_inline = format_inline;
pg->zoom = zoom;
return pg->pix;
} else {
qWarning() << "renderTeX::render: PdfLaTex or GhostScript not found.";
return QPixmap();
}
} else {
if ( compileJob::pathsOK() ) {
QPixmap ret = items[item]->render( zoom, format_inline, sizeHint );
int cost = (int) (items[item]->size()*zoom);
if ( cost >= renderCache.maxCost() ) {
qWarning() << "Warning, render cache too small, result will not be cached !!!";
return ret;
} else {
pg = new cachedPage;
pg->pix = ret;
pg->format_inline = format_inline;
pg->zoom = zoom;
renderCache.insert( item, pg, (int) (items[item]->size()*zoom) );
return ret;
}
} else {
qWarning() << "renderTeX::render: PdfLaTex or GhostScript not found.";
return QPixmap();
}
}
}
void renderTeX::preRender( int item, bool format_inline, int sizeHint ) {
Q_ASSERT( 0 <= item && item < items.size() && items[item] );
if ( compileJob::pathsOK() ) {
connect( items[item], SIGNAL(renderingReady(int)), this, SIGNAL(itemReady(int)) );
items[item]->preRender( item, format_inline, sizeHint );
} else {
qWarning() << "renderTeX::render: PdfLaTex or GhostScript not found.";
}
}
QString renderTeX::getPDF(int item) {
Q_ASSERT( 0 <= item && item < items.size() && items[item] );
return items[item]->getPDFFileName();
}
QRectF renderTeX::getBBox(int item) {
Q_ASSERT( 0 <= item && item < items.size() && items[item] );
return items[item]->getBBox();
}
void renderTeX::renderingFinished( int i ) {
Q_ASSERT( 0 <= i && i < items.size() && items[i] );
emit itemReady( i );
}
#include "renderTeX.moc"