mirror of
https://github.com/pdf2htmlEX/pdf2htmlEX.git
synced 2024-12-22 13:00:08 +00:00
FontSizeManager
This commit is contained in:
parent
dc0681ad84
commit
cccb85f729
@ -30,7 +30,7 @@
|
||||
#include "util/StringFormatter.h"
|
||||
#include "util/TmpFiles.h"
|
||||
#include "util/misc.h"
|
||||
#include "util/StateTracker.h"
|
||||
#include "util/StateManager.h"
|
||||
|
||||
namespace pdf2htmlEX {
|
||||
|
||||
@ -164,7 +164,6 @@ class HTMLRenderer : public OutputDev
|
||||
void install_base_font(GfxFont * font, GfxFontLoc * font_loc, FontInfo & info);
|
||||
void install_external_font (GfxFont * font, FontInfo & info);
|
||||
|
||||
long long install_font_size(double font_size);
|
||||
long long install_transform_matrix(const double * tm);
|
||||
long long install_fill_color(const GfxRGB * rgb);
|
||||
long long install_stroke_color(const GfxRGB * rgb);
|
||||
@ -184,7 +183,6 @@ class HTMLRenderer : public OutputDev
|
||||
void export_remote_default_font(long long fn_id);
|
||||
void export_local_font(const FontInfo & info, GfxFont * font, const std::string & original_font_name, const std::string & cssfont);
|
||||
|
||||
void export_font_size(long long fs_id, double font_size);
|
||||
void export_transform_matrix(long long tm_id, const double * tm);
|
||||
void export_fill_color(long long color_id, const GfxRGB * rgb);
|
||||
void export_stroke_color(long long color_id, const GfxRGB * rgb);
|
||||
@ -283,9 +281,11 @@ class HTMLRenderer : public OutputDev
|
||||
|
||||
// font & size
|
||||
const FontInfo * cur_font_info;
|
||||
double cur_font_size;
|
||||
long long cur_fs_id;
|
||||
bool font_changed;
|
||||
// cur_font_size is as in GfxState,
|
||||
// font_size_manager saves the final font size used in HTML
|
||||
double cur_font_size;
|
||||
FontSizeManager font_size_manager;
|
||||
|
||||
// transform matrix
|
||||
long long cur_ttm_id;
|
||||
@ -300,11 +300,11 @@ class HTMLRenderer : public OutputDev
|
||||
|
||||
// letter spacing
|
||||
bool letter_space_changed;
|
||||
LetterSpaceTracker letter_space_tracker;
|
||||
LetterSpaceManager letter_space_manager;
|
||||
|
||||
// word spacing
|
||||
bool word_space_changed;
|
||||
WordSpaceTracker word_space_tracker;
|
||||
WordSpaceManager word_space_manager;
|
||||
|
||||
// fill color
|
||||
long long cur_fill_color_id;
|
||||
@ -330,7 +330,6 @@ class HTMLRenderer : public OutputDev
|
||||
// draw_text_tm is cur_text_tm scaled by 1/draw_text_scale,
|
||||
// so everything redenered should be multiplied by draw_text_scale
|
||||
double draw_text_tm[6];
|
||||
double draw_font_size;
|
||||
double draw_text_scale;
|
||||
|
||||
// the position of next char, in text coords
|
||||
|
@ -3,8 +3,7 @@
|
||||
*
|
||||
* Generate and optimized HTML for one line
|
||||
*
|
||||
* by WangLu
|
||||
* 2012.09.04
|
||||
* Copyright (C) 2012,2013 Lu Wang <coolwanglu@gmail.com>
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
@ -186,17 +185,17 @@ void HTMLRenderer::TextLineBuffer::flush(void)
|
||||
void HTMLRenderer::TextLineBuffer::set_state (State & state)
|
||||
{
|
||||
state.ids[State::FONT_ID] = renderer->cur_font_info->id;
|
||||
state.ids[State::FONT_SIZE_ID] = renderer->cur_fs_id;
|
||||
state.ids[State::FONT_SIZE_ID] = renderer->font_size_manager.get_id();
|
||||
state.ids[State::FILL_COLOR_ID] = renderer->cur_fill_color_id;
|
||||
state.ids[State::STROKE_COLOR_ID] = renderer->cur_stroke_color_id;
|
||||
state.ids[State::LETTER_SPACE_ID] = renderer->letter_space_tracker.get_id();
|
||||
state.ids[State::WORD_SPACE_ID] = renderer->word_space_tracker.get_id();
|
||||
state.ids[State::LETTER_SPACE_ID] = renderer->letter_space_manager.get_id();
|
||||
state.ids[State::WORD_SPACE_ID] = renderer->word_space_manager.get_id();
|
||||
state.ids[State::RISE_ID] = renderer->cur_rise_id;
|
||||
|
||||
const FontInfo * info = renderer->cur_font_info;
|
||||
state.ascent = info->ascent;
|
||||
state.descent = info->descent;
|
||||
state.draw_font_size = renderer->draw_font_size;
|
||||
state.draw_font_size = renderer->font_size_manager.get_value();
|
||||
}
|
||||
|
||||
void HTMLRenderer::TextLineBuffer::State::begin (ostream & out, const State * prev_state)
|
||||
|
@ -3,8 +3,7 @@
|
||||
*
|
||||
* Export styles to HTML
|
||||
*
|
||||
* by WangLu
|
||||
* 2012.08.14
|
||||
* Copyright (C) 2012,2013 Lu Wang <coolwanglu@gmail.com>
|
||||
*/
|
||||
|
||||
#include <sstream>
|
||||
@ -131,10 +130,6 @@ void HTMLRenderer::export_local_font(const FontInfo & info, GfxFont * font, cons
|
||||
f_css.fs << "}" << endl;
|
||||
}
|
||||
|
||||
void HTMLRenderer::export_font_size (long long fs_id, double font_size)
|
||||
{
|
||||
f_css.fs << ".s" << fs_id << "{font-size:" << round(font_size) << "px;}" << endl;
|
||||
}
|
||||
|
||||
void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
|
||||
{
|
||||
|
@ -3,8 +3,7 @@
|
||||
*
|
||||
* Handling general stuffs
|
||||
*
|
||||
* Copyright (C) 2012 Lu Wang <coolwanglu@gmail.com>
|
||||
* 2012.08.14
|
||||
* Copyright (C) 2012,2013 Lu Wang <coolwanglu@gmail.com>
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
@ -57,8 +56,9 @@ HTMLRenderer::HTMLRenderer(const Param * param)
|
||||
cur_mapping2 = new char* [0x100];
|
||||
width_list = new int [0x10000];
|
||||
|
||||
letter_space_tracker.set_param(CSS::LETTER_SPACE_CN, EPS);
|
||||
word_space_tracker .set_param(CSS::WORD_SPACE_CN , EPS);
|
||||
font_size_manager .set_param(CSS::FONT_SIZE_CN , EPS);
|
||||
letter_space_manager.set_param(CSS::LETTER_SPACE_CN, EPS);
|
||||
word_space_manager .set_param(CSS::WORD_SPACE_CN , EPS);
|
||||
}
|
||||
|
||||
HTMLRenderer::~HTMLRenderer()
|
||||
@ -336,7 +336,9 @@ void HTMLRenderer::pre_process(PDFDoc * doc)
|
||||
void HTMLRenderer::post_process()
|
||||
{
|
||||
// dump css
|
||||
letter_space_tracker.dump_css(f_css.fs);
|
||||
font_size_manager .dump_css(f_css.fs);
|
||||
letter_space_manager.dump_css(f_css.fs);
|
||||
word_space_manager .dump_css(f_css.fs);
|
||||
|
||||
// close files
|
||||
f_outline.fs.close();
|
||||
|
@ -3,8 +3,7 @@
|
||||
*
|
||||
* maintaining all known styles
|
||||
*
|
||||
* by WangLu
|
||||
* 2012.08.14
|
||||
* Copyright (C) 2012,2013 Lu Wang <coolwanglu@gmail.com>
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
@ -213,18 +212,6 @@ void HTMLRenderer::install_external_font(GfxFont * font, FontInfo & info)
|
||||
export_local_font(info, font, fontname, "");
|
||||
}
|
||||
|
||||
long long HTMLRenderer::install_font_size(double font_size)
|
||||
{
|
||||
auto iter = font_size_map.lower_bound(font_size - EPS);
|
||||
if((iter != font_size_map.end()) && (equal(iter->first, font_size)))
|
||||
return iter->second;
|
||||
|
||||
long long new_fs_id = font_size_map.size();
|
||||
font_size_map.insert(make_pair(font_size, new_fs_id));
|
||||
export_font_size(new_fs_id, font_size);
|
||||
return new_fs_id;
|
||||
}
|
||||
|
||||
long long HTMLRenderer::install_transform_matrix(const double * tm)
|
||||
{
|
||||
Matrix m;
|
||||
|
@ -97,15 +97,15 @@ void HTMLRenderer::reset_state()
|
||||
draw_text_scale = 1.0;
|
||||
|
||||
cur_font_info = install_font(nullptr);
|
||||
cur_font_size = draw_font_size = 0;
|
||||
cur_fs_id = install_font_size(cur_font_size);
|
||||
|
||||
font_size_manager.reset();
|
||||
|
||||
memcpy(cur_text_tm, ID_MATRIX, sizeof(cur_text_tm));
|
||||
memcpy(draw_text_tm, ID_MATRIX, sizeof(draw_text_tm));
|
||||
cur_ttm_id = install_transform_matrix(draw_text_tm);
|
||||
|
||||
letter_space_tracker.reset();
|
||||
word_space_tracker .reset();
|
||||
letter_space_manager.reset();
|
||||
word_space_manager .reset();
|
||||
|
||||
cur_fill_color.r = cur_fill_color.g = cur_fill_color.b = 0;
|
||||
cur_fill_color_id = install_fill_color(&cur_fill_color);
|
||||
@ -262,11 +262,9 @@ void HTMLRenderer::check_state_change(GfxState * state)
|
||||
draw_text_scale = new_draw_text_scale;
|
||||
}
|
||||
|
||||
if(!(equal(new_draw_font_size, draw_font_size)))
|
||||
if(!font_size_manager.install(new_draw_font_size))
|
||||
{
|
||||
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
|
||||
draw_font_size = new_draw_font_size;
|
||||
cur_fs_id = install_font_size(draw_font_size);
|
||||
}
|
||||
if(!(tm_equal(new_draw_text_tm, draw_text_tm, 4)))
|
||||
{
|
||||
@ -339,7 +337,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
|
||||
// letter space
|
||||
// depends: draw_text_scale
|
||||
if((all_changed || letter_space_changed || draw_text_scale_changed)
|
||||
&& (letter_space_tracker.install(state->getCharSpace() * draw_text_scale)))
|
||||
&& (letter_space_manager.install(state->getCharSpace() * draw_text_scale)))
|
||||
{
|
||||
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
|
||||
}
|
||||
@ -347,7 +345,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
|
||||
// word space
|
||||
// depends draw_text_scale
|
||||
if((all_changed || word_space_changed || draw_text_scale_changed)
|
||||
&& (word_space_tracker.install(state->getWordSpace() * draw_text_scale)))
|
||||
&& (word_space_manager.install(state->getWordSpace() * draw_text_scale)))
|
||||
{
|
||||
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
|
||||
}
|
||||
|
@ -1,14 +1,13 @@
|
||||
/*
|
||||
* StateTracker.h
|
||||
* StateManager.h
|
||||
*
|
||||
* track specific PDF state
|
||||
* manage reusable CSS classes
|
||||
*
|
||||
* Copyright (C) 2013 Lu Wang <coolwanglu@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef STATETRACKER_H__
|
||||
#define STATETRACKER_H__
|
||||
#ifndef STATEMANAGER_H__
|
||||
#define STATEMANAGER_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
@ -17,13 +16,13 @@
|
||||
|
||||
namespace pdf2htmlEX {
|
||||
|
||||
template<class ValueType, class Imp> class StateTracker {};
|
||||
template<class ValueType, class Imp> class StateManager {};
|
||||
|
||||
template<class Imp>
|
||||
class StateTracker<double, Imp>
|
||||
class StateManager<double, Imp>
|
||||
{
|
||||
public:
|
||||
StateTracker()
|
||||
StateManager()
|
||||
: eps(0)
|
||||
, imp(static_cast<Imp*>(this))
|
||||
{
|
||||
@ -52,8 +51,9 @@ public:
|
||||
return _install(new_value);
|
||||
}
|
||||
|
||||
long long get_id (void) const { return id; }
|
||||
double get_actual_value (void) const { return actual_value; }
|
||||
long long get_id (void) const { return id; }
|
||||
double get_value (void) const { return value; }
|
||||
double get_actual_value (void) const { return actual_value; }
|
||||
|
||||
void dump_css(std::ostream & out) {
|
||||
for(auto iter = value_map.begin(); iter != value_map.end(); ++iter)
|
||||
@ -93,21 +93,21 @@ protected:
|
||||
std::map<double, long long> value_map;
|
||||
};
|
||||
|
||||
class FontSizeTracker : public StateTracker<double, FontSizeTracker>
|
||||
class FontSizeManager : public StateManager<double, FontSizeManager>
|
||||
{
|
||||
public:
|
||||
double default_value(void) { return 0; }
|
||||
void dump_value(std::ostream & out, double value) { out << "font-size:" << round(value) << "px;"; }
|
||||
};
|
||||
|
||||
class LetterSpaceTracker : public StateTracker<double, LetterSpaceTracker>
|
||||
class LetterSpaceManager : public StateManager<double, LetterSpaceManager>
|
||||
{
|
||||
public:
|
||||
double default_value(void) { return 0; }
|
||||
void dump_value(std::ostream & out, double value) { out << "letter-spacing:" << round(value) << "px;"; }
|
||||
};
|
||||
|
||||
class WordSpaceTracker : public StateTracker<double, WordSpaceTracker>
|
||||
class WordSpaceManager : public StateManager<double, WordSpaceManager>
|
||||
{
|
||||
public:
|
||||
double default_value(void) { return 0; }
|
||||
@ -116,4 +116,4 @@ public:
|
||||
|
||||
} // namespace pdf2htmlEX
|
||||
|
||||
#endif //STATETRACKER_H__
|
||||
#endif //STATEMANAGER_H__
|
Loading…
Reference in New Issue
Block a user