Skip to content

Commit

Permalink
dvisvgm: import from GitHub repository
Browse files Browse the repository at this point in the history
  • Loading branch information
t-tk committed May 13, 2023
1 parent 3e82714 commit aea9856
Show file tree
Hide file tree
Showing 63 changed files with 850 additions and 300 deletions.
2 changes: 1 addition & 1 deletion texk/dvisvgm/dvisvgm-src/src/BgColorSpecialHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void BgColorSpecialHandler::dviBeginPage (unsigned pageno, SpecialActions &actio
}


vector<const char*> BgColorSpecialHandler::prefixes() const {
vector<const char*> BgColorSpecialHandler::prefixes () const {
vector<const char*> pfx {"background"};
return pfx;
}
5 changes: 3 additions & 2 deletions texk/dvisvgm/dvisvgm-src/src/BgColorSpecialHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class BgColorSpecialHandler : public SpecialHandler {
void preprocess (const std::string &prefix, std::istream &is, SpecialActions &actions) override;
bool process (const std::string &prefix, std::istream &is, SpecialActions &actions) override;
const char* info () const override {return "background color special";}
const char* name () const override {return "bgcolor";}
std::vector<const char*> prefixes() const override;
const char* name () const override {return handlerName();}
static const char* handlerName () {return "bgcolor";}
std::vector<const char*> prefixes () const override;

protected:
void dviBeginPage (unsigned pageno, SpecialActions &actions) override;
Expand Down
2 changes: 1 addition & 1 deletion texk/dvisvgm/dvisvgm-src/src/ColorSpecialHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool ColorSpecialHandler::process (const string&, istream &is, SpecialActions &a
}


vector<const char*> ColorSpecialHandler::prefixes() const {
vector<const char*> ColorSpecialHandler::prefixes () const {
vector<const char*> pfx {"color"};
return pfx;
}
Expand Down
5 changes: 3 additions & 2 deletions texk/dvisvgm/dvisvgm-src/src/ColorSpecialHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ class ColorSpecialHandler : public SpecialHandler {
bool process (const std::string &prefix, std::istream &is, SpecialActions &actions) override;
static Color readColor (std::istream &is);
static Color readColor (const std::string &model, std::istream &is);
const char* name () const override {return "color";}
const char* name () const override {return handlerName();}
static const char* handlerName () {return "color";}
const char* info () const override {return "complete support of color specials";}
std::vector<const char*> prefixes() const override;
std::vector<const char*> prefixes () const override;

private:
std::stack<Color> _colorStack;
Expand Down
1 change: 1 addition & 0 deletions texk/dvisvgm/dvisvgm-src/src/DVIActions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef DVIACTIONS_HPP
#define DVIACTIONS_HPP

#include <cstdint>
#include <string>
#include <vector>

Expand Down
7 changes: 5 additions & 2 deletions texk/dvisvgm/dvisvgm-src/src/DVIReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ void DVIReader::putVFChar (Font *font, uint32_t c) {
FontManager &fm = FontManager::instance();
const vector<uint8_t> *dvi = vf->getDVI(c); // try to get DVI snippet that represents character c
Font *firstFont = fm.vfFirstFont(vf);
if (!dvi && (!firstFont || !firstFont->getMetrics()->isJFM()))
return;
if (!dvi) {
const FontMetrics *ffm = firstFont ? firstFont->getMetrics() : nullptr;
if (!ffm || (!ffm->isJFM() && !ffm->isOFM()))
return;
}
fm.enterVF(vf); // enter VF font number context
int savedFontNum = _currFontNum; // save current font number
setFont(fm.vfFirstFontNum(vf), SetFontMode::VF_ENTER);
Expand Down
63 changes: 41 additions & 22 deletions texk/dvisvgm/dvisvgm-src/src/DVIToSVG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,48 +377,67 @@ void DVIToSVG::embedFonts (XMLElement *svgElement) {
}


static vector<string> extract_prefixes (const char *ignorelist) {
vector<string> prefixes;
if (ignorelist) {
const char *left = ignorelist;
while (*left) {
while (*left && !isalnum(*left))
left++;
const char *right = left;
while (*right && isalnum(*right))
right++;
if (*left)
prefixes.emplace_back(left, right-left);
left = right;
}
}
return prefixes;
}


/** Enables or disables processing of specials. If ignorelist == 0, all
* supported special handlers are loaded. To disable selected sets of specials,
* the corresponding prefixes can be given separated by non alpha-numeric characters,
* e.g. "color, ps, em" or "color: ps em" etc.
* A single "*" in the ignore list disables all specials.
* @param[in] ignorelist list of special prefixes to ignore
* @param[in] ignorelist list of hanlder names to ignore
* @param[in] pswarning if true, shows warning about disabled PS support
* @return the SpecialManager that handles special statements */
void DVIToSVG::setProcessSpecials (const char *ignorelist, bool pswarning) {
if (ignorelist && strcmp(ignorelist, "*") == 0) // ignore all specials?
SpecialManager::instance().unregisterHandlers();
else {
auto ignoredHandlerName = extract_prefixes(ignorelist);
// add special handlers
vector<unique_ptr<SpecialHandler>> handlers;
handlers.emplace_back(util::make_unique<BgColorSpecialHandler>()); // handles background color special
handlers.emplace_back(util::make_unique<ColorSpecialHandler>()); // handles color specials
handlers.emplace_back(util::make_unique<DvisvgmSpecialHandler>()); // handles raw SVG embeddings
handlers.emplace_back(util::make_unique<EmSpecialHandler>()); // handles emTeX specials
handlers.emplace_back(util::make_unique<HtmlSpecialHandler>()); // handles hyperref specials
handlers.emplace_back(util::make_unique<PapersizeSpecialHandler>()); // handles papersize special
handlers.emplace_back(util::make_unique<PdfSpecialHandler>()); // handles pdf specials
handlers.emplace_back(util::make_unique<TpicSpecialHandler>()); // handles tpic specials
SpecialManager::registerHandler<BgColorSpecialHandler>(ignoredHandlerName); // handles background color special
SpecialManager::registerHandler<ColorSpecialHandler>(ignoredHandlerName); // handles color specials
SpecialManager::registerHandler<DvisvgmSpecialHandler>(ignoredHandlerName); // handles raw SVG embeddings
SpecialManager::registerHandler<EmSpecialHandler>(ignoredHandlerName); // handles emTeX specials
SpecialManager::registerHandler<HtmlSpecialHandler>(ignoredHandlerName); // handles hyperref specials
SpecialManager::registerHandler<PapersizeSpecialHandler>(ignoredHandlerName); // handles papersize special
SpecialManager::registerHandler<PdfSpecialHandler>(ignoredHandlerName); // handles pdf specials
SpecialManager::registerHandler<TpicSpecialHandler>(ignoredHandlerName); // handles tpic specials
if (find(ignoredHandlerName.begin(), ignoredHandlerName.end(), PsSpecialHandler::handlerName()) == ignoredHandlerName.end()) {
#ifndef DISABLE_GS
if (Ghostscript().available())
handlers.emplace_back(util::make_unique<PsSpecialHandler>()); // handles PostScript specials
else
if (Ghostscript().available())
SpecialManager::registerHandler<PsSpecialHandler>(ignoredHandlerName); // handles PostScript specials
else
#endif
{
{
#ifndef HAVE_LIBGS
// dummy PS special handler that only prints warning messages
handlers.emplace_back(util::make_unique<NoPsSpecialHandler>());
if (pswarning) {
// dummy PS special handler that only prints warning messages
SpecialManager::registerHandler<NoPsSpecialHandler>(ignoredHandlerName);
if (pswarning) {
#ifdef DISABLE_GS
Message::wstream() << "processing of PostScript specials has been disabled permanently\n";
Message::wstream() << "processing of PostScript specials has been disabled permanently\n";
#else
Message::wstream() << "processing of PostScript specials is disabled (Ghostscript not found)\n";
Message::wstream() << "processing of PostScript specials is disabled (Ghostscript not found)\n";
#endif
}
}
#endif
}
}
SpecialManager::instance().unregisterHandlers();
SpecialManager::instance().registerHandlers(handlers, ignorelist);
}
}

Expand Down
2 changes: 1 addition & 1 deletion texk/dvisvgm/dvisvgm-src/src/DvisvgmSpecialHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ void DvisvgmSpecialHandler::dviEndPage (unsigned, SpecialActions &actions) {
}


vector<const char*> DvisvgmSpecialHandler::prefixes() const {
vector<const char*> DvisvgmSpecialHandler::prefixes () const {
vector<const char*> pfx {"dvisvgm:"};
return pfx;
}
7 changes: 4 additions & 3 deletions texk/dvisvgm/dvisvgm-src/src/DvisvgmSpecialHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ class DvisvgmSpecialHandler : public SpecialHandler {

public:
DvisvgmSpecialHandler ();
const char* name () const override {return "dvisvgm";}
const char* info () const override {return "special set for embedding raw SVG snippets";}
std::vector<const char*> prefixes() const override;
void preprocess (const std::string &prefix, std::istream &is, SpecialActions &actions) override;
bool process (const std::string &prefix, std::istream &is, SpecialActions &actions) override;
const char* info () const override {return "special set for embedding raw SVG snippets";}
const char* name () const override {return handlerName();}
static const char* handlerName () {return "dvisvgm";}
std::vector<const char*> prefixes () const override;

protected:
void preprocessRaw (InputReader &ir);
Expand Down
2 changes: 1 addition & 1 deletion texk/dvisvgm/dvisvgm-src/src/EmSpecialHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void EmSpecialHandler::dviEndPage (unsigned pageno, SpecialActions &actions) {
}


vector<const char*> EmSpecialHandler::prefixes() const {
vector<const char*> EmSpecialHandler::prefixes () const {
vector<const char*> pfx {"em:"};
return pfx;
}
7 changes: 4 additions & 3 deletions texk/dvisvgm/dvisvgm-src/src/EmSpecialHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class EmSpecialHandler : public SpecialHandler {
};

public:
const char* name () const override {return "em";}
const char* info () const override {return "line drawing statements of the emTeX special set";}
std::vector<const char*> prefixes() const override;
bool process (const std::string &prefix, std::istream &in, SpecialActions &actions) override;
const char* info () const override {return "line drawing statements of the emTeX special set";}
const char* name () const override {return handlerName();}
static const char* handlerName () {return "em";}
std::vector<const char*> prefixes () const override;

protected:
void dviEndPage (unsigned pageno, SpecialActions &actions) override;
Expand Down
1 change: 1 addition & 0 deletions texk/dvisvgm/dvisvgm-src/src/FileFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const char* FileFinder::findFile (const std::string &fname, const char *ftype) c
#ifndef MIKTEX
static std::map<std::string, kpse_file_format_type> types = {
{"tfm", kpse_tfm_format},
{"ofm", kpse_ofm_format},
{"pfb", kpse_type1_format},
{"vf", kpse_vf_format},
{"mf", kpse_mf_format},
Expand Down
7 changes: 4 additions & 3 deletions texk/dvisvgm/dvisvgm-src/src/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ const FontMetrics* TFMFont::getMetrics () const {
try {
_metrics = FontMetrics::read(_fontname);
if (!_metrics) {
_metrics = util::make_unique<NullFontMetric>();
_metrics = util::make_unique<NullFontMetrics>();
Message::wstream(true) << "can't find "+_fontname+".tfm\n";
}
}
catch (FontMetricException &e) {
_metrics = util::make_unique<NullFontMetric>();
Message::wstream(true) << e.what() << " in " << _fontname << ".tfm\n";
const char *ext = (_metrics && _metrics->isOFM()) ? ".ofm" : ".tfm";
_metrics = util::make_unique<NullFontMetrics>();
Message::wstream(true) << e.what() << " in " << _fontname << ext << "\n";
}
}
return _metrics.get();
Expand Down
2 changes: 2 additions & 0 deletions texk/dvisvgm/dvisvgm-src/src/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ class NativeFont : public PhysicalFont {

protected:
NativeFont (double ptsize, const FontStyle &style, Color color) : _ptsize(ptsize), _style(style), _color(color) {}
using PhysicalFont::hAdvance;
using PhysicalFont::vAdvance;

private:
double _ptsize; ///< font size in PS point units
Expand Down
2 changes: 1 addition & 1 deletion texk/dvisvgm/dvisvgm-src/src/FontCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ bool FontCache::fontinfo (const string &dirname, vector<FontInfo> &infos, vector
string path = dirname+"/"+(fname.substr(1));
ifstream ifs(path, ios::binary);
if (fontinfo(ifs, info))
infos.push_back(move(info));
infos.push_back(std::move(info));
else
invalid.push_back(fname.substr(1));
}
Expand Down
7 changes: 4 additions & 3 deletions texk/dvisvgm/dvisvgm-src/src/FontManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,13 @@ int FontManager::registerFont (uint32_t fontnum, const string &name, uint32_t ch
else {
// create dummy font as a placeholder if the proper font is not available
newfont = util::make_unique<EmptyFont>(name);
if (filename.rfind('.') == string::npos)
filename += ".mf";
// print warning message about missing font file (only once for each filename)
static set<string> missing_fonts;
if (missing_fonts.find(filename) == missing_fonts.end()) {
Message::wstream(true) << "font file '" << filename << "' not found\n";
if (filename.rfind('.') == string::npos)
Message::wstream(true) << "no font file found for '" << filename << "'\n";
else
Message::wstream(true) << "font file '" << filename << "' not found\n";
missing_fonts.insert(filename);
}
}
Expand Down
39 changes: 31 additions & 8 deletions texk/dvisvgm/dvisvgm-src/src/FontMetrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,42 @@
#include "FileFinder.hpp"
#include "FontMetrics.hpp"
#include "JFM.hpp"
#include "OFM.hpp"
#include "utility.hpp"

using namespace std;

static inline uint16_t read_uint16 (istream &is) {
uint16_t val = 256*is.get();
return val + is.get();
}


/** Reads the font metrics for a given font name from a TFM, JFM, or OFM file.
* @param[in] fontname name of font to get the metrics for.
* @return pointer to object that holds the font metrics, or nullptr if no matching file was found */
unique_ptr<FontMetrics> FontMetrics::read (const string &fontname) {
const char *path = FileFinder::instance().lookup(fontname + ".tfm");
unique_ptr<FontMetrics> metrics;
const char *path = FileFinder::instance().lookup(fontname + ".ofm", false);
if (!path)
path = FileFinder::instance().lookup(fontname + ".tfm");
ifstream ifs(path, ios::binary);
if (!ifs)
return unique_ptr<FontMetrics>();
uint16_t id = 256*ifs.get();
id += ifs.get();
if (id == 9 || id == 11) // Japanese font metric file?
return util::make_unique<JFM>(ifs);
return util::make_unique<TFM>(ifs);
if (ifs) {
auto id = read_uint16(ifs);
if (id == 0) { // OFM?
auto ofmLevel = read_uint16(ifs);
if (ofmLevel == 0)
metrics = util::make_unique<OFM0>();
else if (ofmLevel == 1)
metrics = util::make_unique<OFM1>();
else
throw FontMetricException("OFM level "+to_string(ofmLevel)+" not supported");
}
else if (id == 9 || id == 11) // Japanese font metric file?
metrics = util::make_unique<JFM>();
else
metrics = util::make_unique<TFM>();
metrics->read(ifs);
}
return metrics;
}
13 changes: 8 additions & 5 deletions texk/dvisvgm/dvisvgm-src/src/FontMetrics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
#ifndef FONTMETRICS_HPP
#define FONTMETRICS_HPP

#include <cstdint>
#include <istream>
#include <memory>
#include <string>
#include "MessageException.hpp"

struct FontMetrics {
virtual ~FontMetrics () =default;
virtual void read (std::istream &is) {}
virtual double getDesignSize () const =0;
virtual double getCharWidth (int c) const =0;
virtual double getCharHeight (int c) const =0;
Expand All @@ -41,14 +43,15 @@ struct FontMetrics {
virtual double getDescent () const =0;
virtual bool verticalLayout () const =0;
virtual uint32_t getChecksum () const =0;
virtual uint16_t firstChar () const =0;
virtual uint16_t lastChar () const =0;
virtual uint32_t firstChar () const =0;
virtual uint32_t lastChar () const =0;
virtual bool isJFM () const {return false;}
virtual bool isOFM () const {return false;}
static std::unique_ptr<FontMetrics> read (const std::string &fontname);
};


struct NullFontMetric : public FontMetrics {
struct NullFontMetrics : public FontMetrics {
double getDesignSize () const override {return 1;}
double getCharWidth (int c) const override {return 0;}
double getCharHeight (int c) const override {return 0;}
Expand All @@ -62,8 +65,8 @@ struct NullFontMetric : public FontMetrics {
double getDescent () const override {return 0;}
bool verticalLayout () const override {return false;}
uint32_t getChecksum () const override {return 0;}
uint16_t firstChar () const override {return 0;}
uint16_t lastChar () const override {return 0;}
uint32_t firstChar () const override {return 0;}
uint32_t lastChar () const override {return 0;}
};


Expand Down
5 changes: 4 additions & 1 deletion texk/dvisvgm/dvisvgm-src/src/Ghostscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ string Ghostscript::revisionstr () {
int major = rev / 1000;
int minor = (rev - major*1000)/10;
int patch = rev % 10;
revstr = to_string(major) + "." + to_string(minor) + "." + to_string(patch);
revstr = to_string(major) + ".";
if (minor > 0 && minor < 10)
revstr += '0';
revstr += to_string(minor) + "." + to_string(patch);
}
}
return revstr;
Expand Down
Loading

0 comments on commit aea9856

Please sign in to comment.