2012-08-14 06:35:55 +00:00
|
|
|
/*
|
|
|
|
* export.cc
|
|
|
|
*
|
|
|
|
* Export styles to HTML
|
|
|
|
*
|
|
|
|
* by WangLu
|
|
|
|
* 2012.08.14
|
|
|
|
*/
|
|
|
|
|
2012-09-07 16:38:41 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2012-08-14 06:35:55 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2012-08-14 09:50:16 +00:00
|
|
|
#include "HTMLRenderer.h"
|
|
|
|
#include "namespace.h"
|
2012-08-29 17:01:45 +00:00
|
|
|
#include "config.h"
|
2012-08-14 09:50:16 +00:00
|
|
|
|
|
|
|
using boost::algorithm::ifind_first;
|
2012-08-14 08:23:15 +00:00
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, const string & fontfileformat, GfxFont * font)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << "@font-face{font-family:f" << info.id << ";src:url(";
|
2012-08-14 13:23:33 +00:00
|
|
|
|
|
|
|
{
|
2012-09-07 17:09:09 +00:00
|
|
|
const char * fn = str_fmt("f%llx%s", info.id, suffix.c_str());
|
|
|
|
if(param->single_html)
|
|
|
|
{
|
|
|
|
allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
allcss_fout << fn;
|
|
|
|
}
|
2012-08-14 13:23:33 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ")format(\"" << fontfileformat << "\");}.f" << info.id << "{font-family:f" << info.id << ";line-height:" << (info.ascent - info.descent) << ";}" << endl;
|
2012-08-14 06:35:55 +00:00
|
|
|
}
|
|
|
|
|
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
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_remote_default_font(long long fn_id)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ".f" << fn_id << "{font-family:sans-serif;color:transparent;visibility:hidden;}" << endl;
|
2012-08-14 06:35:55 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_local_font(const FontInfo & info, GfxFont * font, const string & original_font_name, const string & cssfont)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ".f" << info.id << "{";
|
2012-08-14 06:35:55 +00:00
|
|
|
allcss_fout << "font-family:" << ((cssfont == "") ? (original_font_name + "," + general_font_family(font)) : cssfont) << ";";
|
|
|
|
|
2012-08-26 23:17:48 +00:00
|
|
|
if(font->isBold() || ifind_first(original_font_name, "bold"))
|
2012-08-14 06:35:55 +00:00
|
|
|
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;";
|
2012-08-26 23:17:48 +00:00
|
|
|
else if(font->isItalic() || ifind_first(original_font_name, "italic"))
|
2012-08-14 06:35:55 +00:00
|
|
|
allcss_fout << "font-style:italic;";
|
|
|
|
|
2012-08-31 07:50:14 +00:00
|
|
|
allcss_fout << "line-height:" << (info.ascent - info.descent) << ";";
|
2012-08-30 12:08:42 +00:00
|
|
|
|
2012-08-14 06:35:55 +00:00
|
|
|
allcss_fout << "}" << endl;
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_font_size (long long fs_id, double font_size)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ".s" << fs_id << "{font-size:" << font_size << "px;}" << endl;
|
2012-08-14 06:35:55 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ".t" << tm_id << "{";
|
2012-08-14 06:35:55 +00:00
|
|
|
|
2012-08-16 06:24:38 +00:00
|
|
|
// always ignore tm[4] and tm[5] because
|
|
|
|
// we have already shifted the origin
|
|
|
|
|
2012-08-14 06:35:55 +00:00
|
|
|
// TODO: recognize common matices
|
2012-09-09 06:48:10 +00:00
|
|
|
auto prefixes = {"", "-ms-", "-moz-", "-webkit-", "-o-"};
|
|
|
|
for(auto iter = prefixes.begin(); iter != prefixes.end(); ++iter)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
2012-09-09 06:48:10 +00:00
|
|
|
const auto & prefix = *iter;
|
2012-08-21 20:22:56 +00:00
|
|
|
// PDF use a different coordinate system from Web
|
|
|
|
allcss_fout << prefix << "transform:matrix("
|
|
|
|
<< tm[0] << ','
|
|
|
|
<< -tm[1] << ','
|
|
|
|
<< -tm[2] << ','
|
|
|
|
<< tm[3] << ',';
|
|
|
|
|
|
|
|
allcss_fout << "0,0);";
|
2012-08-14 06:35:55 +00:00
|
|
|
}
|
|
|
|
allcss_fout << "}" << endl;
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_letter_space (long long ls_id, double letter_space)
|
2012-08-15 10:48:11 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ".l" << ls_id << "{letter-spacing:" << letter_space << "px;}" << endl;
|
2012-08-15 10:48:11 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_word_space (long long ws_id, double word_space)
|
2012-08-15 10:48:11 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ".w" << ws_id << "{word-spacing:" << word_space << "px;}" << endl;
|
2012-08-15 10:48:11 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_color (long long color_id, const GfxRGB * rgb)
|
2012-08-14 06:35:55 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ".c" << color_id << "{color:rgb("
|
|
|
|
<< dec << (int)colToByte(rgb->r) << "," << (int)colToByte(rgb->g) << "," << (int)colToByte(rgb->b) << ");}" << hex
|
2012-08-14 06:35:55 +00:00
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_whitespace (long long ws_id, double ws_width)
|
2012-08-15 10:48:11 +00:00
|
|
|
{
|
2012-08-16 12:26:09 +00:00
|
|
|
if(ws_width > 0)
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << "._" << ws_id << "{display:inline-block;width:" << ws_width << "px;}" << endl;
|
2012-08-16 12:26:09 +00:00
|
|
|
else
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << "._" << ws_id << "{display:inline;margin-left:" << ws_width << "px;}" << endl;
|
2012-08-15 10:48:11 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 15:33:15 +00:00
|
|
|
void HTMLRenderer::export_rise (long long rise_id, double rise)
|
2012-08-24 17:40:43 +00:00
|
|
|
{
|
2012-09-07 16:38:41 +00:00
|
|
|
allcss_fout << ".r" << rise_id << "{top:" << (-rise) << "px;}" << endl;
|
2012-08-24 17:40:43 +00:00
|
|
|
}
|
|
|
|
|