2012-08-14 06:35:55 +00:00
|
|
|
/*
|
|
|
|
* export.cc
|
|
|
|
*
|
|
|
|
* Export styles to HTML
|
|
|
|
*
|
|
|
|
* by WangLu
|
|
|
|
* 2012.08.14
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
2012-08-14 09:50:16 +00:00
|
|
|
#include "HTMLRenderer.h"
|
|
|
|
#include "namespace.h"
|
|
|
|
|
|
|
|
using boost::algorithm::ifind_first;
|
2012-08-14 08:23:15 +00:00
|
|
|
|
2012-08-14 09:50:16 +00:00
|
|
|
void HTMLRenderer::export_remote_font(long long fn_id, const string & suffix, const string & fontfileformat, GfxFont * font)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
2012-08-14 13:23:33 +00:00
|
|
|
allcss_fout << format("@font-face{font-family:f%|1$x|;src:url(") % fn_id;
|
|
|
|
|
2012-08-14 18:46:53 +00:00
|
|
|
const std::string fn = (format("f%|1$x|%2%") % fn_id % suffix).str();
|
2012-08-14 13:23:33 +00:00
|
|
|
if(param->single_html)
|
|
|
|
{
|
2012-08-15 03:15:33 +00:00
|
|
|
allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'";
|
2012-08-14 13:23:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-14 18:46:53 +00:00
|
|
|
allcss_fout << fn;
|
2012-08-14 13:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allcss_fout << format(")format(\"%1%\");}.f%|2$x|{font-family:f%|2$x|;") % fontfileformat % fn_id;
|
2012-08-14 06:35:55 +00:00
|
|
|
|
|
|
|
double a = font->getAscent();
|
|
|
|
double d = font->getDescent();
|
|
|
|
double r = _is_positive(a-d) ? (a/(a-d)) : 1.0;
|
|
|
|
|
2012-08-14 09:13:29 +00:00
|
|
|
for(const string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
|
|
|
allcss_fout << prefix << "transform-origin:0% " << (r*100.0) << "%;";
|
|
|
|
}
|
|
|
|
|
|
|
|
allcss_fout << "line-height:" << (a-d) << ";";
|
|
|
|
|
|
|
|
allcss_fout << "}" << endl;
|
|
|
|
}
|
|
|
|
|
2012-08-14 09:13:29 +00:00
|
|
|
static string general_font_family(GfxFont * font)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
|
|
|
if(font -> isFixedWidth())
|
|
|
|
return "monospace";
|
|
|
|
else if (font -> isSerif())
|
|
|
|
return "serif";
|
|
|
|
else
|
|
|
|
return "sans-serif";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this function is called when some font is unable to process, may use the name there as a hint
|
|
|
|
void HTMLRenderer::export_remote_default_font(long long fn_id)
|
|
|
|
{
|
2012-08-14 09:50:16 +00:00
|
|
|
allcss_fout << format(".f%|1$x|{font-family:sans-serif;color:transparent;visibility:hidden;}")%fn_id << endl;
|
2012-08-14 06:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLRenderer::export_local_font(long long fn_id, GfxFont * font, const string & original_font_name, const string & cssfont)
|
|
|
|
{
|
2012-08-14 09:50:16 +00:00
|
|
|
allcss_fout << format(".f%|1$x|{") % fn_id;
|
2012-08-14 06:35:55 +00:00
|
|
|
allcss_fout << "font-family:" << ((cssfont == "") ? (original_font_name + "," + general_font_family(font)) : cssfont) << ";";
|
|
|
|
|
|
|
|
if(font->isBold())
|
|
|
|
allcss_fout << "font-weight:bold;";
|
|
|
|
|
2012-08-14 09:50:16 +00:00
|
|
|
if(ifind_first(original_font_name, "oblique"))
|
2012-08-14 06:35:55 +00:00
|
|
|
allcss_fout << "font-style:oblique;";
|
|
|
|
else if(font->isItalic())
|
|
|
|
allcss_fout << "font-style:italic;";
|
|
|
|
|
|
|
|
double a = font->getAscent();
|
|
|
|
double d = font->getDescent();
|
|
|
|
double r = _is_positive(a-d) ? (a/(a-d)) : 1.0;
|
|
|
|
|
2012-08-14 09:13:29 +00:00
|
|
|
for(const string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
|
|
|
allcss_fout << prefix << "transform-origin:0% " << (r*100.0) << "%;";
|
|
|
|
}
|
|
|
|
|
|
|
|
allcss_fout << "line-height:" << (a-d) << ";";
|
|
|
|
|
|
|
|
allcss_fout << "}" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HTMLRenderer::export_font_size (long long fs_id, double font_size)
|
|
|
|
{
|
2012-08-14 09:50:16 +00:00
|
|
|
allcss_fout << format(".s%|1$x|{font-size:%2%px;}") % fs_id % font_size << endl;
|
2012-08-14 06:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLRenderer::export_whitespace (long long ws_id, double ws_width)
|
|
|
|
{
|
2012-08-14 09:50:16 +00:00
|
|
|
allcss_fout << format(".w%|1$x|{width:%2%px;}") % ws_id % ws_width << endl;
|
2012-08-14 06:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
|
|
|
|
{
|
2012-08-14 09:50:16 +00:00
|
|
|
allcss_fout << format(".t%|1$x|{") % tm_id;
|
2012-08-14 06:35:55 +00:00
|
|
|
|
|
|
|
// TODO: recognize common matices
|
|
|
|
if(_tm_equal(tm, id_matrix))
|
|
|
|
{
|
|
|
|
// no need to output anything
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-14 09:13:29 +00:00
|
|
|
for(const string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
|
|
|
// PDF use a different coordinate system from Web
|
|
|
|
allcss_fout << prefix << "transform:matrix("
|
|
|
|
<< tm[0] << ','
|
|
|
|
<< -tm[1] << ','
|
|
|
|
<< -tm[2] << ','
|
|
|
|
<< tm[3] << ',';
|
|
|
|
|
|
|
|
if(prefix == "-moz-")
|
2012-08-14 09:50:16 +00:00
|
|
|
allcss_fout << format("%1%px,%2%px);") % tm[4] % -tm[5];
|
2012-08-14 06:35:55 +00:00
|
|
|
else
|
2012-08-14 09:50:16 +00:00
|
|
|
allcss_fout << format("%1%,%2%);") % tm[4] % -tm[5];
|
2012-08-14 06:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
allcss_fout << "}" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLRenderer::export_color(long long color_id, const GfxRGB * rgb)
|
|
|
|
{
|
2012-08-14 09:50:16 +00:00
|
|
|
allcss_fout << format(".c%|1$x|{color:rgb(%2%,%3%,%4%);}")
|
2012-08-14 06:35:55 +00:00
|
|
|
% color_id % (int)colToByte(rgb->r) % (int)colToByte(rgb->g) % (int)colToByte(rgb->b)
|
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
|