/* Copyright (C) 2007 One Laptop Per Child * Author: Marc Maurer * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #ifndef ABI_DEVELOP_HIGHLIGHTER_H #define ABI_DEVELOP_HIGHLIGHTER_H #include #include #include "ut_types.h" #include "pl_Listener.h" #include "fl_DocLayout.h" #include "LanguageDefinition.h" using std::map; class pf_Frag_Strux_Block; class RegExMatch { public: RegExMatch() : byte_start(0), byte_end(0), char_start(0), char_end(0), continuation(false), closed(true) { } // byte index int byte_start; int byte_end; // the first byte that is NOT included in the match // character index PT_BlockOffset char_start; PT_BlockOffset char_end; // the first character that is NOT included in the match bool continuation; bool closed; const LanguagePattern* pattern; }; typedef map RegionMap; // TODO: s/int/PT_BlockOffset ? class Highlighter : public PL_Listener { public: Highlighter(FL_DocLayout* pDocLayout, const LanguageDefinition* pLangDef); virtual ~Highlighter(); virtual bool populate(PL_StruxFmtHandle sfh, const PX_ChangeRecord *pcr); virtual bool populateStrux(PL_StruxDocHandle sdh, const PX_ChangeRecord *pcr, PL_StruxFmtHandle *psfh); virtual bool change(PL_StruxFmtHandle sfh, const PX_ChangeRecord *pcr); virtual bool insertStrux(PL_StruxFmtHandle sfh, const PX_ChangeRecord *pcr, PL_StruxDocHandle sdhNew, PL_ListenerId lid, void(*pfnBindHandles)(PL_StruxDocHandle sdhNew, PL_ListenerId lid, PL_StruxFmtHandle sfhNew)); virtual bool signal(UT_uint32 iSignal); virtual PLListenerType getType() const; private: bool _setupBlockMatches(const PX_ChangeRecord *pcr, RegionMap& matches); UT_RGBColor _getColor(const LanguagePattern& pattern); // todo: remove this, the color should be a property of a LanguagePattern bool _highlight(PL_StruxFmtHandle sfh, const PX_ChangeRecord *pcr, bool isPopulating = false); PT_BlockOffset _findDamageOffset(const pf_Frag_Strux_Block* fsb, const PX_ChangeRecord *pcr, RegionMap& matches); bool _isContinuation(RegionMap& matches); bool _isOpen(PT_BlockOffset endOffset, RegionMap& matches); bool _nextBlockIsContinuation(fl_BlockLayout* pBL); RegionMap* _getRegionMap(const fl_BlockLayout* pBL); void _pruneRegionsUpTo(PT_BlockOffset startOffset, RegionMap& matches); void _pruneRegions(PT_BlockOffset startOffset, RegionMap& matches); void _matchRegions(const UT_GrowBuf& textBuffer, PT_BlockOffset startOffset, RegionMap& matches); void _pruneAndRematchRegions(const UT_GrowBuf& textBuffer, PT_BlockOffset startOffset, RegionMap& matches); bool _matchEndRegex(const UT_GrowBuf& textBuffer, LanguagePattern& pattern, PT_BlockOffset* iOffset); void _decorateRuns(fl_BlockLayout* pBL, UT_uint32 runOffset, RegionMap& matches); int _search(regex_t*& comp_regex, const string& regex, gchar* str, int length, int start, RegExMatch* pMatch); FL_DocLayout* m_pDocLayout; const LanguageDefinition* m_pLangDef; PL_ListenerId m_iListenerId; }; #endif /* ABI_DEVELOP_HIGHLIGHTER_H */