diff options
Diffstat (limited to 'dviware/dvisvgm/src/Font.hpp')
-rw-r--r-- | dviware/dvisvgm/src/Font.hpp | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/dviware/dvisvgm/src/Font.hpp b/dviware/dvisvgm/src/Font.hpp index 0a92cbeff5..f4bb58a9c6 100644 --- a/dviware/dvisvgm/src/Font.hpp +++ b/dviware/dvisvgm/src/Font.hpp @@ -2,7 +2,7 @@ ** Font.hpp ** ** ** ** This file is part of dvisvgm -- a fast DVI to SVG converter ** -** Copyright (C) 2005-2020 Martin Gieseking <martin.gieseking@uos.de> ** +** Copyright (C) 2005-2021 Martin Gieseking <martin.gieseking@uos.de> ** ** ** ** This program is free software; you can redistribute it and/or ** ** modify it under the terms of the GNU General Public License as ** @@ -50,6 +50,7 @@ struct GlyphMetrics { double wl, wr, h, d; }; +class FontVisitor; /** Abstract base for all font classes. */ class Font { @@ -69,17 +70,19 @@ class Font { virtual const char* path () const =0; virtual const char* filename () const; virtual const FontEncoding* encoding () const; - virtual bool getGlyph (int c, Glyph &glyph, GFGlyphTracer::Callback *callback=nullptr) const =0; + virtual bool getGlyph (int c, Glyph &glyph, GFGlyphTracer::Callback *callback) const =0; virtual void getGlyphMetrics (int c, bool vertical, GlyphMetrics &metrics) const; virtual uint32_t unicode (uint32_t c) const; virtual void tidy () const {} virtual bool findAndAssignBaseFontMap () {return true;} - virtual bool verticalLayout () const {return getMetrics() ? getMetrics()->verticalLayout() : false;} + virtual bool verticalLayout () const {return getMetrics() != nullptr && getMetrics()->verticalLayout();} virtual bool verifyChecksums () const {return true;} virtual int fontIndex () const {return 0;} virtual const FontStyle* style () const {return nullptr;} virtual Color color () const {return Color::BLACK;} virtual const FontMap::Entry* fontMapEntry () const; + virtual void visit (FontVisitor &visitor) =0; + virtual void visit (FontVisitor &visitor) const =0; }; @@ -100,7 +103,9 @@ class EmptyFont : public Font { double italicCorr (int c) const override {return 0;} const FontMetrics* getMetrics () const override {return nullptr;} const char* path () const override {return nullptr;} - bool getGlyph (int c, Glyph &glyph, GFGlyphTracer::Callback *cb=nullptr) const override {return false;} + bool getGlyph (int c, Glyph &glyph, GFGlyphTracer::Callback*) const override {return false;} + void visit (FontVisitor &visitor) override {} + void visit (FontVisitor &visitor) const override {} private: std::string _fontname; @@ -115,9 +120,9 @@ class PhysicalFont : public virtual Font { static std::unique_ptr<Font> create (const std::string &name, uint32_t checksum, double dsize, double ssize, PhysicalFont::Type type); static std::unique_ptr<Font> create (const std::string &name, int fontindex, uint32_t checksum, double dsize, double ssize); virtual Type type () const =0; - bool getGlyph (int c, Glyph &glyph, GFGlyphTracer::Callback *cb=nullptr) const override; - virtual bool getExactGlyphBox (int c, BoundingBox &bbox, GFGlyphTracer::Callback *cb=nullptr) const; - virtual bool getExactGlyphBox (int c, GlyphMetrics &metrics, bool vertical, GFGlyphTracer::Callback *cb=nullptr) const; + bool getGlyph (int c, Glyph &glyph, GFGlyphTracer::Callback *cb) const override; + virtual bool getExactGlyphBox (int c, BoundingBox &bbox, GFGlyphTracer::Callback *cb) const; + virtual bool getExactGlyphBox (int c, GlyphMetrics &metrics, bool vertical, GFGlyphTracer::Callback *cb) const; virtual bool isCIDFont () const; virtual int hAdvance () const; virtual std::string familyName () const; @@ -129,12 +134,14 @@ class PhysicalFont : public virtual Font { virtual double scaledAscent () const; virtual int ascent () const; virtual int descent () const; - virtual int traceAllGlyphs (bool includeCached, GFGlyphTracer::Callback *cb=nullptr) const; + virtual int traceAllGlyphs (bool includeCached, GFGlyphTracer::Callback *cb) const; virtual int collectCharMapIDs (std::vector<CharMapID> &charmapIDs) const; virtual CharMapID getCharMapID () const =0; virtual void setCharMapID (const CharMapID &id) {} virtual Character decodeChar (uint32_t c) const; const char* path () const override; + void visit (FontVisitor &visitor) override; + void visit (FontVisitor &visitor) const override; protected: bool createGF (std::string &gfname) const; @@ -159,7 +166,9 @@ class VirtualFont : public virtual Font { public: static std::unique_ptr<Font> create (const std::string &name, uint32_t checksum, double dsize, double ssize); virtual const DVIVector* getDVI (int c) const =0; - bool getGlyph (int c, Glyph &glyph, GFGlyphTracer::Callback *cb=nullptr) const override {return false;} + bool getGlyph (int c, Glyph &glyph, GFGlyphTracer::Callback*) const override {return false;} + void visit (FontVisitor &visitor) override; + void visit (FontVisitor &visitor) const override; protected: virtual void assignChar (uint32_t c, DVIVector &&dvi) =0; @@ -272,6 +281,8 @@ class NativeFont : public PhysicalFont { Color color () const override {return _color;} const FontMap::Entry* fontMapEntry () const override {return nullptr;} static std::string uniqueName (const std::string &path, const FontStyle &style); + void visit (FontVisitor &visitor) override; + void visit (FontVisitor &visitor) const override; protected: NativeFont (double ptsize, const FontStyle &style, Color color) : _ptsize(ptsize), _style(style), _color(color) {} @@ -390,6 +401,33 @@ class VirtualFontImpl : public VirtualFont, public TFMFont { }; +struct FontVisitor { + virtual ~FontVisitor () =default; + virtual void visited (const PhysicalFont *font) {} + virtual void visited (const VirtualFont *font) {} + virtual void visited (const NativeFont *font) {visited(static_cast<const PhysicalFont*>(font));} + virtual void visited (PhysicalFont *font) {} + virtual void visited (VirtualFont *font) {} + virtual void visited (NativeFont *font) {visited(static_cast<PhysicalFont*>(font));} +}; + + +/** This function works similar to dynamic_cast but only on pointers to Font classes. + * It uses double dispatch instead of RTTI and should therefore be faster. + * @param[in] font font pointer to be cast + * @return cast pointer on success, nullptr otherwise */ +template <typename C> +C font_cast (typename util::set_const_of<Font>::by<typename std::remove_pointer<C>::type>::type *font) { + struct : FontVisitor { + void visited (C font) override {result = font;} + C result = nullptr; + } visitor; + if (font) + font->visit(visitor); + return visitor.result; +} + + struct FontException : public MessageException { explicit FontException (const std::string &msg) : MessageException(msg) {} }; |