1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-10-06 12:01:39 +00:00
pdf2htmlEX/src/util/StateManager.h

160 lines
4.6 KiB
C
Raw Normal View History

2013-02-02 19:35:56 +00:00
/*
2013-02-05 06:36:36 +00:00
* StateManager.h
2013-02-02 19:35:56 +00:00
*
* manage reusable CSS classes
*
* Copyright (C) 2013 Lu Wang <coolwanglu@gmail.com>
*/
2013-02-05 06:36:36 +00:00
#ifndef STATEMANAGER_H__
#define STATEMANAGER_H__
2013-02-02 19:35:56 +00:00
2013-02-03 16:40:07 +00:00
#include <iostream>
2013-02-03 09:36:28 +00:00
#include <map>
2013-02-02 19:35:56 +00:00
2013-02-05 05:57:11 +00:00
#include "util/math.h"
2013-02-05 10:19:25 +00:00
#include "util/CSSClassNames.h"
2013-02-02 19:35:56 +00:00
namespace pdf2htmlEX {
2013-02-05 06:36:36 +00:00
template<class ValueType, class Imp> class StateManager {};
2013-02-03 09:36:28 +00:00
template<class Imp>
2013-02-05 06:36:36 +00:00
class StateManager<double, Imp>
2013-02-02 19:35:56 +00:00
{
public:
2013-02-05 06:36:36 +00:00
StateManager()
2013-02-03 16:40:07 +00:00
: eps(0)
2013-02-05 05:57:11 +00:00
, imp(static_cast<Imp*>(this))
2013-02-03 16:40:07 +00:00
{
reset();
}
// values no father than eps are treated as equal
2013-02-05 10:19:25 +00:00
void set_param (double eps) {
2013-02-03 16:40:07 +00:00
this->eps = eps;
}
2013-02-02 19:35:56 +00:00
// usually called at the beginning of a page
2013-02-05 05:57:11 +00:00
void reset(void) {
value = imp->default_value();
_install(value);
}
2013-02-03 09:36:28 +00:00
2013-02-02 19:35:56 +00:00
/*
2013-02-05 05:57:11 +00:00
* install new_value if changed (equal() should be faster than map::lower_bound)
2013-02-03 09:36:28 +00:00
* return if the state has been indeed changed
2013-02-02 19:35:56 +00:00
*/
2013-02-05 05:57:11 +00:00
bool install(double new_value) {
2013-02-03 09:36:28 +00:00
if(equal(new_value, value))
return false;
2013-02-05 11:51:04 +00:00
_install(new_value);
return true;
2013-02-03 09:36:28 +00:00
}
2013-02-05 05:57:11 +00:00
2013-02-05 06:36:36 +00:00
long long get_id (void) const { return id; }
double get_value (void) const { return value; }
double get_actual_value (void) const { return actual_value; }
2013-02-05 05:57:11 +00:00
void dump_css(std::ostream & out) {
for(auto iter = value_map.begin(); iter != value_map.end(); ++iter)
{
2013-02-05 10:19:25 +00:00
out << "." << imp->get_css_class_name() << iter->second << "{";
2013-02-05 05:57:11 +00:00
imp->dump_value(out, iter->first);
out << "}" << std::endl;
}
}
protected:
// this version of install does not check if value has been updated
2013-02-05 11:51:04 +00:00
// return if a new entry has been created
2013-02-05 05:57:11 +00:00
bool _install(double new_value) {
2013-02-03 09:36:28 +00:00
value = new_value;
auto iter = value_map.lower_bound(new_value - eps);
2013-02-03 16:40:07 +00:00
if((iter != value_map.end()) && (abs(iter->first - value) <= eps))
{
2013-02-03 09:36:28 +00:00
actual_value = iter->first;
2013-02-05 05:57:11 +00:00
id = iter->second;
return false;
2013-02-03 09:36:28 +00:00
}
actual_value = new_value;
2013-02-05 05:57:11 +00:00
id = value_map.size();
value_map.insert(std::make_pair(new_value, id));
return true;
2013-02-03 16:40:07 +00:00
}
double eps;
2013-02-03 09:36:28 +00:00
Imp * imp;
2013-02-02 19:35:56 +00:00
2013-02-03 09:36:28 +00:00
long long id;
double value; // the value we are tracking
double actual_value; // the value we actually exported to HTML
std::map<double, long long> value_map;
2013-02-02 19:35:56 +00:00
};
2013-02-05 06:36:36 +00:00
class FontSizeManager : public StateManager<double, FontSizeManager>
2013-02-03 16:40:07 +00:00
{
public:
2013-02-05 10:19:25 +00:00
static const char * get_css_class_name (void) { return CSS::FONT_SIZE_CN; }
2013-02-03 16:40:07 +00:00
double default_value(void) { return 0; }
2013-02-05 05:57:11 +00:00
void dump_value(std::ostream & out, double value) { out << "font-size:" << round(value) << "px;"; }
2013-02-03 16:40:07 +00:00
};
2013-02-05 10:19:25 +00:00
class LetterSpaceManager : public StateManager<double, LetterSpaceManager>
2013-02-03 16:40:07 +00:00
{
public:
2013-02-05 10:19:25 +00:00
static const char * get_css_class_name (void) { return CSS::LETTER_SPACE_CN; }
2013-02-03 16:40:07 +00:00
double default_value(void) { return 0; }
2013-02-05 05:57:11 +00:00
void dump_value(std::ostream & out, double value) { out << "letter-spacing:" << round(value) << "px;"; }
2013-02-03 16:40:07 +00:00
};
2013-02-05 06:36:36 +00:00
class WordSpaceManager : public StateManager<double, WordSpaceManager>
2013-02-05 06:21:07 +00:00
{
public:
2013-02-05 10:19:25 +00:00
static const char * get_css_class_name (void) { return CSS::WORD_SPACE_CN;}
2013-02-05 06:21:07 +00:00
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "word-spacing:" << round(value) << "px;"; }
};
2013-02-05 06:45:40 +00:00
class RiseManager : public StateManager<double, RiseManager>
{
public:
2013-02-05 10:19:25 +00:00
static const char * get_css_class_name (void) { return CSS::RISE_CN; }
2013-02-05 06:45:40 +00:00
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "top:" << round(-value) << "px;"; }
};
2013-02-05 06:55:44 +00:00
class WhitespaceManager : public StateManager<double, WhitespaceManager>
{
public:
2013-02-05 10:19:25 +00:00
static const char * get_css_class_name (void) { return CSS::WHITESPACE_CN; }
2013-02-05 06:55:44 +00:00
double default_value(void) { return 0; }
2013-02-05 07:05:36 +00:00
void dump_value(std::ostream & out, double value) {
out << ((value > 0) ? "display:inline-block;width:"
: "display:inline;margin-left:")
<< round(value) << "px;";
}
2013-02-05 06:55:44 +00:00
};
2013-02-05 06:51:00 +00:00
class HeightManager : public StateManager<double, HeightManager>
{
public:
2013-02-05 10:19:25 +00:00
static const char * get_css_class_name (void) { return CSS::HEIGHT_CN; }
2013-02-05 06:51:00 +00:00
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "height:" << round(value) << "px;"; }
};
2013-02-05 07:05:36 +00:00
class LeftManager : public StateManager<double, LeftManager>
{
public:
2013-02-05 10:19:25 +00:00
static const char * get_css_class_name (void) { return CSS::LEFT_CN; }
2013-02-05 07:05:36 +00:00
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "left:" << round(value) << "px;"; }
};
2013-02-02 19:35:56 +00:00
} // namespace pdf2htmlEX
2013-02-05 06:36:36 +00:00
#endif //STATEMANAGER_H__