Skip to content

Commit 09ae305

Browse files
committed
Fix crash on LaTeX parsing error.
1 parent 5ae0cc2 commit 09ae305

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

imagewidgets.cpp

+43-19
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,51 @@ CLatexWidget::CLatexWidget(Glib::RefPtr<Gdk::Window> wnd, Glib::ustring text, Gd
123123

124124
unsigned int clr;
125125
clr=0xff000000|int(fg.get_red()*255)<<16|int(fg.get_green()*255)<<8|int(fg.get_blue()*255);
126-
127-
TeXRender *r;
128-
r = LaTeX::parse(utf82wide(text.c_str()),
129-
500,
130-
18,
131-
18 / 3.f,
132-
clr);
126+
127+
try {
128+
TeXRender *r;
129+
r = LaTeX::parse(utf82wide(text.c_str()),
130+
500,
131+
18,
132+
18 / 3.f,
133+
clr);
134+
135+
float h = r->getHeight(), w = r->getWidth();
136+
137+
SetSize(w+4,h+2);
133138

134-
float h = r->getHeight(), w = r->getWidth();
139+
baseline=(int)(round((2.5+(h)*(1.0f-r->getBaseline()))));
135140

136-
SetSize(w+4,h+2);
137-
138-
baseline=(int)(round((2.5+(h)*(1.0f-r->getBaseline()))));
139-
140-
// workaround for not entirely correct baseline arithmetic
141-
if(h<12) baseline-=1;
142-
143-
Graphics2D_cairo g2(image_ctx);
144-
r->draw(g2,2,1);
145-
146-
if(r) delete r;
141+
// workaround for not entirely correct baseline arithmetic
142+
if(h<12) baseline-=1;
143+
144+
Graphics2D_cairo g2(image_ctx);
145+
r->draw(g2,2,1);
146+
147+
if(r) delete r;
148+
} catch(tex::ex_parse &e) {
149+
printf("LaTeX parsing error: %s\n",e.what());
150+
SetSize(128,16);
151+
baseline=0;
152+
153+
image_ctx->set_source_rgb(1,0,0);
154+
image_ctx->set_font_size(10);
155+
image_ctx->move_to(0,14);
156+
image_ctx->show_text("LaTeX parse failed");
157+
158+
image_ctx->fill();
159+
} catch(tex::ex_invalid_state &e) {
160+
printf("LaTeX rendering error: %s\n",e.what());
161+
SetSize(128,16);
162+
baseline=0;
163+
164+
image_ctx->set_source_rgb(1,0,0);
165+
image_ctx->set_font_size(10);
166+
image_ctx->move_to(0,14);
167+
image_ctx->show_text("LaTeX render failed");
168+
169+
image_ctx->fill();
170+
}
147171
}
148172

149173
CLatexWidget::~CLatexWidget()

0 commit comments

Comments
 (0)