From ce0e33668ad9279ffb2d9d0c8226b83d553d5824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Aliste?= Date: Fri, 8 Nov 2013 01:36:28 -0200 Subject: [PATCH] Add RTL support --- CMakeLists.txt | 10 +++ Makefile.am | 1 + cmake/modules/FindICU.cmake | 52 +++++++++++++ configure.ac | 24 ++++++ poppler/GlobalParams.cc | 41 ++++++++++ poppler/GlobalParams.h | 11 +++ poppler/Makefile.am | 7 ++ poppler/RTL.cc | 112 +++++++++++++++++++++++++++ poppler/RTL.h | 71 +++++++++++++++++ poppler/TextOutputDev.cc | 181 ++++++++++++++++++++++++++------------------ poppler/TextOutputDev.h | 12 ++- utils/Makefile.am | 2 + utils/pdftotext.cc | 33 +++++++- utils/printreorderings.cc | 34 +++++++++ utils/printreorderings.h | 24 ++++++ 15 files changed, 534 insertions(+), 81 deletions(-) create mode 100644 cmake/modules/FindICU.cmake create mode 100644 poppler/RTL.cc create mode 100644 poppler/RTL.h create mode 100644 utils/printreorderings.cc create mode 100644 utils/printreorderings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6814d01..5221446 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ option(ENABLE_CPP "Compile poppler cpp wrapper." ON) option(ENABLE_LIBOPENJPEG "Use libopenjpeg for JPX streams." ON) set(ENABLE_CMS "auto" CACHE STRING "Use color management system. Possible values: auto, lcms1, lcms2. 'auto' prefers lcms2 over lcms1 if both are available. Unset to disable color management system.") option(ENABLE_LIBCURL "Build libcurl based HTTP support." OFF) +option(ENABLE_ICU "Build with ICU." ON) option(ENABLE_ZLIB "Build with zlib (not totally safe)." OFF) option(USE_FIXEDPOINT "Use fixed point arithmetic in the Splash backend" OFF) option(USE_FLOAT "Use single precision arithmetic in the Splash backend" OFF) @@ -144,6 +145,15 @@ if(ENABLE_CPP) set(ENABLE_CPP ${ICONV_FOUND}) set(HAVE_ICONV ${ICONV_FOUND}) endif(ENABLE_CPP) + +# well i don't know what to do here. +# the icu module is there. +if(ENABLE_ICU) + macro_optional_find_package(ICU) + set(ENABLE_ICU ${ICU_FOUND}) + set(HAVE_ICU ${ICU_FOUND}) +endif(ENABLE_ICU) + if(ENABLE_ZLIB) find_package(ZLIB) if(ZLIB_FOUND) diff --git a/Makefile.am b/Makefile.am index e7e5bac..748fd7c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -101,6 +101,7 @@ EXTRA_DIST += \ cmake/modules/PopplerDefaults.cmake \ cmake/modules/PopplerMacros.cmake \ cmake/modules/FindLIBOPENJPEG.cmake \ + cmake/modules/FindICU.cmake \ config.h.cmake \ poppler-cairo.pc.cmake \ poppler/poppler-config.h.cmake \ diff --git a/cmake/modules/FindICU.cmake b/cmake/modules/FindICU.cmake new file mode 100644 index 0000000..4f582dd --- /dev/null +++ b/cmake/modules/FindICU.cmake @@ -0,0 +1,52 @@ +# Imperfect module to find the ICU library with cmake + +find_program(ICU_CONFIG icu-config) + +if(NOT ICU_CONFIG) + set(ICU_FOUND FALSE) +else(NOT ICU_CONFIG) + + execute_process(COMMAND ${ICU_CONFIG} --exists + RESULT_VARIABLE ICU_EXISTS_RESULT) + if(NOT ${ICU_EXISTS_RESULT} EQUAL 0) + set(ICU_FOUND FALSE) + else(NOT ${ICU_EXISTS_RESULT} EQUAL 0) + + execute_process(COMMAND ${ICU_CONFIG} --cppflags-searchpath + COMMAND sed s/^-I//g + OUTPUT_VARIABLE ICU_INCLUDE_DIRS) + + execute_process(COMMAND ${ICU_CONFIG} --ldflags-libsonly + COMMAND tr -d \n + COMMAND sed "s/[\t ]*-l/ /g" + COMMAND sed "s/^[\t ]*//" + COMMAND sed "s/[\t ]*$//" + OUTPUT_VARIABLE ICU_LIBRARY_NAMES) + + separate_arguments(ICU_LIBRARY_NAMES) + +# execute_process(COMMAND ${ICU_CONFIG} --ldflags-searchpath +# COMMAND sed s/^-L// +# OUTPUT_VARIABLE ICU_LIB_DIR) + + foreach(CURRENT_LIB_NAME ${ICU_LIBRARY_NAMES}) + set(CURRENT_LIBRARY ${CURRENT_LIB_NAME}-NOTFOUND) + find_library(CURRENT_LIBRARY ${CURRENT_LIB_NAME}) + list(APPEND ICU_LIBRARIES ${CURRENT_LIBRARY}) + endforeach(CURRENT_LIB_NAME ${ICU_LIBRARY_NAMES}) + + set(ICU_FOUND TRUE) + + endif(NOT ${ICU_EXISTS_RESULT} EQUAL 0) +endif(NOT ICU_CONFIG) + +if(ICU_FOUND) + if(NOT ICU_FIND_QUIETLY) + MESSAGE(STATUS "Found ICU") + endif(NOT ICU_FIND_QUIETLY) +else(ICU_FOUND) + if(ICU_FIND_REQUIRED) + MESSAGE(FATAL_ERROR "Could not find ICU") + endif(ICU_FIND_REQUIRED) +endif(ICU_FOUND) + diff --git a/configure.ac b/configure.ac index ee6b37b..64a8224 100644 --- a/configure.ac +++ b/configure.ac @@ -270,6 +270,29 @@ if test "$xpdf_cv_func_fseek64" = yes -a "$xpdf_cv_func_ftell64" = yes; then fi AC_CHECK_FUNCS(pread64 lseek64) +dnl Test for icu +AC_ARG_ENABLE(icu, + AC_HELP_STRING([--enable-icu], + [Build with icu based BIDI support. Enabled by default.]), + enable_icu=$enableval, + enable_icu="yes") +if test x$enable_icu = xyes; then + PKG_CHECK_MODULES(ICU, icu-uc, + [icu_pkgconfig=yes], [icu_pkgconfig=no]) + if test "x$icu_pkgconfig" = "xyes"; then + AC_DEFINE(HAVE_ICU, 1, [Have ICU include files]) + else + ICU_LIBS= + ICU_CFLAGS= + AC_PATH_PROG(ICU_CONFIG, icu-config, no) + if test "x$ICU_CONFIG" != "xno" ; then + ICU_CFLAGS= + ICU_LIBS=`$ICU_CONFIG --ldflags-libsonly` + AC_DEFINE(HAVE_ICU, 1, [Have ICU include files]) + fi + fi +fi + dnl Test for zlib AC_ARG_ENABLE([zlib], [AS_HELP_STRING([--enable-zlib],[Build with zlib])], @@ -897,6 +920,7 @@ echo " use gtk-doc: $enable_gtk_doc" echo " use libjpeg: $enable_libjpeg" echo " use libpng: $enable_libpng" echo " use libtiff: $enable_libtiff" +echo " use icu: $enable_icu" echo " use zlib: $enable_zlib" echo " use libcurl: $enable_libcurl" echo " use libopenjpeg: $enable_libopenjpeg" diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc index 6efdd0c..3ddf6fe 100644 --- a/poppler/GlobalParams.cc +++ b/poppler/GlobalParams.cc @@ -582,6 +582,9 @@ GlobalParams::GlobalParams(const char *customPopplerDataDir) residentUnicodeMaps = new GooHash(); unicodeMaps = new GooHash(gTrue); cMapDirs = new GooHash(gTrue); +#if HAVE_ICU + reorderingModes = new RTLReorderingModes(); +#endif toUnicodeDirs = new GooList(); fontFiles = new GooHash(gTrue); fontDirs = new GooList(); @@ -826,6 +829,9 @@ GlobalParams::~GlobalParams() { deleteGooList(list, GooString); } delete cMapDirs; +#if HAVE_ICU + delete reorderingModes; +#endif delete cidToUnicodeCache; delete unicodeToUnicodeCache; @@ -1915,6 +1921,22 @@ GooList *GlobalParams::getEncodingNames() return result; } +GooList *GlobalParams::getRTLReorderingNames() +{ + GooList *result = new GooList; +#if HAVE_ICU + GooHashIter *iter; + GooString *key; + void *val; + reorderingModes->startIter(&iter); + while (reorderingModes->getNext(&iter, &key, &val)) { + result->append(key); + } + reorderingModes->killIter(&iter); +#endif + return result; +} + //------------------------------------------------------------------------ // functions to set parameters //------------------------------------------------------------------------ @@ -2037,6 +2059,25 @@ void GlobalParams::setTextEncoding(char *encodingName) { unlockGlobalParams; } +void GlobalParams::setRTLReordering(char *reorderingName) { +#if HAVE_ICU + lockGlobalParams; + reorderingMode = reorderingModes->lookup(reorderingName); + reorderingName = NULL; + unlockGlobalParams; +#endif +} + +#if HAVE_ICU +UBiDiReorderingMode GlobalParams::icu_ReorderingMode(bool inverse, ReorderingMode *modep) +{ + ReorderingMode mode = ReorderingDefault; + if (modep != NULL) + mode = *modep; + return reorderingModes->icu_ReorderingMode(mode, inverse); +} +#endif + GBool GlobalParams::setTextEOL(char *s) { lockGlobalParams; if (!strcmp(s, "unix")) { diff --git a/poppler/GlobalParams.h b/poppler/GlobalParams.h index bc11684..6d8ffc4 100644 --- a/poppler/GlobalParams.h +++ b/poppler/GlobalParams.h @@ -43,6 +43,7 @@ #include #include "goo/gtypes.h" #include "CharTypes.h" +#include "RTL.h" #if MULTITHREADED #include "goo/GooMutex.h" @@ -199,11 +200,16 @@ public: UnicodeMap *getUnicodeMap(GooString *encodingName); CMap *getCMap(GooString *collection, GooString *cMapName, Stream *stream = NULL); UnicodeMap *getTextEncoding(); + const ReorderingMode getRTLReordering() {return reorderingMode;} +#if HAVE_ICU + UBiDiReorderingMode icu_ReorderingMode(bool inverse=true, ReorderingMode *modep=NULL); +#endif #ifdef ENABLE_PLUGINS GBool loadPlugin(char *type, char *name); #endif GooList *getEncodingNames(); + GooList *getRTLReorderingNames(); //----- functions to set parameters void addFontFile(GooString *fontName, GooString *path); @@ -225,6 +231,7 @@ public: void setPSRasterResolution(double res); void setPSRasterMono(GBool mono); void setTextEncoding(char *encodingName); + void setRTLReordering(char *reorderingName); GBool setTextEOL(char *s); void setTextPageBreaks(GBool pageBreaks); void setTextKeepTinyChars(GBool keep); @@ -284,6 +291,9 @@ private: // codes, indexed by encoding name [GooString] GooHash *cMapDirs; // list of CMap dirs, indexed by collection // name [GooList[GooString]] +#if HAVE_ICU + RTLReorderingModes *reorderingModes; // rtl reordering modes +#endif GooList *toUnicodeDirs; // list of ToUnicode CMap dirs [GooString] GBool baseFontsInitialized; #ifdef _WIN32 @@ -326,6 +336,7 @@ private: // in color (RGB/CMYK) GooString *textEncoding; // encoding (unicodeMap) to use for text // output + ReorderingMode reorderingMode; EndOfLineKind textEOL; // type of EOL marker to use for text // output GBool textPageBreaks; // insert end-of-page markers? diff --git a/poppler/Makefile.am b/poppler/Makefile.am index 9f90c9d..9fba5cb 100644 --- a/poppler/Makefile.am +++ b/poppler/Makefile.am @@ -120,10 +120,14 @@ cms_includes = $(LCMS_CFLAGS) cms_libs = $(LCMS_LIBS) endif +icu_includes = $(ICU_CFLAGS) +icu_libs = $(ICU_LIBS) + INCLUDES = \ -I$(top_srcdir) \ -I$(top_srcdir)/goo \ $(cms_includes) \ + $(icu_includes) \ $(splash_includes) \ $(cairo_includes) \ $(libjpeg_includes) \ @@ -145,6 +149,7 @@ libpoppler_la_LIBADD = \ $(top_builddir)/goo/libgoo.la \ $(top_builddir)/fofi/libfofi.la \ $(cms_libs) \ + $(icu_libs) \ $(splash_libs) \ $(libtiff_libs) \ $(libjpeg_libs) \ @@ -235,6 +240,7 @@ poppler_include_HEADERS = \ SecurityHandler.h \ UTF.h \ UTF8.h \ + RTL.h \ XpdfPluginAPI.h \ Sound.h nodist_poppler_include_HEADERS = poppler-config.h @@ -302,6 +308,7 @@ libpoppler_la_SOURCES = \ UnicodeMap.cc \ UnicodeTypeTable.cc \ UTF.cc \ + RTL.cc \ ViewerPreferences.cc \ XRef.cc \ PSOutputDev.cc \ diff --git a/poppler/RTL.cc b/poppler/RTL.cc new file mode 100644 index 0000000..9bdd51a --- /dev/null +++ b/poppler/RTL.cc @@ -0,0 +1,112 @@ +//======================================================================== +// +// RTL.cc +// +// Copyright 2013 Alex Bodnaru +// +//======================================================================== + +//======================================================================== +// +// Modified under the Poppler project - http://poppler.freedesktop.org +// +// All changes made under the Poppler project to this file are licensed +// under GPL version 2 or later +// +// Copyright (C) 2013 Alex Bodnaru +// +// To see a description of the changes please see the Changelog file that +// came with your tarball or type make ChangeLog if you are building from git +// +//======================================================================== + +#include + +#ifdef USE_GCC_PRAGMAS +#pragma implementation +#endif + +#include "RTL.h" +#include "goo/GooString.h" +#include "goo/GooHash.h" + +//------------------------------------------------------------------------ +// RTL +//------------------------------------------------------------------------ + +RTLReorderingModes::RTLReorderingModes() +{ + static struct { + char *name; + ReorderingMode mode; + } reorderingModes[] = + { + {"none", ReorderingNotNeeded}, +#if HAVE_ICU + {"windows", ReorderingNumbersSpecial}, + {"unicode", ReorderingLikeDirect}, +#endif + {"default", ReorderingDefault}, + {"", ReorderingNotNeeded} + }; + reorderingNames = new GooHash(gTrue); + int c; + for (c = 0; *reorderingModes[c].name; ++c) + { + reorderingNames->add( + new GooString(reorderingModes[c].name), reorderingModes[c].mode); + } +} + +RTLReorderingModes::~RTLReorderingModes() +{ +} + +ReorderingMode RTLReorderingModes::lookup(const char *key) +{ + int mode = reorderingNames->lookupInt(key); + return mode ? ReorderingMode(mode) : ReorderingDefault; +} + +void RTLReorderingModes::startIter(GooHashIter **iter) +{ + reorderingNames->startIter(iter); +} + +void RTLReorderingModes::killIter(GooHashIter **iter) +{ + reorderingNames->killIter(iter); +} + +GBool RTLReorderingModes::getNext(GooHashIter **iter, GooString **key, void **val) +{ + return reorderingNames->getNext(iter, key, val); +} + +GBool RTLReorderingModes::getNext(GooHashIter **iter, GooString **key, int *val) +{ + return reorderingNames->getNext(iter, key, val); +} + +#if HAVE_ICU +UBiDiReorderingMode RTLReorderingModes::icu_ReorderingMode(ReorderingMode mode, bool inverse) +{ + switch (mode) + { + case ReorderingNumbersSpecial: // windows + return inverse ? UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL : UBIDI_REORDER_NUMBERS_SPECIAL; + case ReorderingLikeDirect: // unicode + return inverse ? UBIDI_REORDER_INVERSE_LIKE_DIRECT : UBIDI_REORDER_DEFAULT; + case ReorderingDefault: + return icu_ReorderingMode( +#ifdef _WIN32 + ReorderingNumbersSpecial, +#else + ReorderingLikeDirect, +#endif + inverse); + default: + return UBIDI_REORDER_DEFAULT; + }; +} +#endif diff --git a/poppler/RTL.h b/poppler/RTL.h new file mode 100644 index 0000000..2907d12 --- /dev/null +++ b/poppler/RTL.h @@ -0,0 +1,71 @@ +//======================================================================== +// +// RTL.h +// +// Copyright 2013 Alex Bodnaru +// +//======================================================================== + +//======================================================================== +// +// Modified under the Poppler project - http://poppler.freedesktop.org +// +// All changes made under the Poppler project to this file are licensed +// under GPL version 2 or later +// +// Copyright (C) 2013 Alex Bodnaru +// +// To see a description of the changes please see the Changelog file that +// came with your tarball or type make ChangeLog if you are building from git +// +//======================================================================== + +#ifndef RTL_H +#define RTL_H + +#ifdef USE_GCC_PRAGMAS +#pragma interface +#endif + +#include "poppler-config.h" +#if HAVE_ICU +#include +#endif +#include "goo/gtypes.h" +class GooString; +class GooHash; +class GooHashIter; + +enum ReorderingMode { + ReorderingNotNeeded=1, // 0 would mess the lookup +#if HAVE_ICU + // windows + ReorderingNumbersSpecial, + // unicode + ReorderingLikeDirect, + ReorderingDefault +#else + ReorderingDefault=ReorderingNotNeeded +#endif +}; + +class RTLReorderingModes { +public: + RTLReorderingModes(); + virtual ~RTLReorderingModes(); + + ReorderingMode lookup(const char *key); + void startIter(GooHashIter **iter); + void killIter(GooHashIter **iter); + GBool getNext(GooHashIter **iter, GooString **key, void **val); + GBool getNext(GooHashIter **iter, GooString **key, int *val); + +#if HAVE_ICU + UBiDiReorderingMode icu_ReorderingMode(ReorderingMode mode, bool inverse=true); +#endif + +private: + GooHash *reorderingNames; +}; + +#endif diff --git a/poppler/TextOutputDev.cc b/poppler/TextOutputDev.cc index 4adb3c2..2807435 100644 --- a/poppler/TextOutputDev.cc +++ b/poppler/TextOutputDev.cc @@ -53,6 +53,10 @@ #include #include #include +#if HAVE_ICU +#include +#include +#endif #ifdef _WIN32 #include // for O_BINARY #include // for setmode @@ -2040,11 +2044,13 @@ TextWord *TextWordList::get(int idx) { // TextPage //------------------------------------------------------------------------ -TextPage::TextPage(GBool rawOrderA) { +TextPage::TextPage(GBool rawOrderA, + ReorderingMode reordering_modeA) { int rot; refCnt = 1; rawOrder = rawOrderA; + reordering_mode = reordering_modeA; curWord = NULL; charPos = 0; curFont = NULL; @@ -3537,7 +3543,42 @@ GBool TextPage::findText(Unicode *s, int len, double xMin1, yMin1, xMax1, yMax1; GBool found; - //~ needs to handle right-to-left text +#if HAVE_ICU + UBiDi *bidi = NULL; + UErrorCode err; + UChar *output = NULL, *input = NULL; + int32_t ulen; + UConverter *conv32 = NULL; + + err = U_ZERO_ERROR; + conv32 = ucnv_open("UTF-32_PlatformEndian", &err); + ulen = ucnv_toUChars( + conv32, NULL, 0, (const char *) s, len * sizeof(Unicode), &err); + input = new UChar[ulen]; + output = new UChar[ulen]; + bidi = ubidi_open(); + + if (input && output && bidi && conv32) { + UBiDiReorderingMode ubidi_reordering_mode = + globalParams->icu_ReorderingMode(false, &reordering_mode); + UBiDiLevel level = (primaryLR) ? UBIDI_DEFAULT_LTR : UBIDI_DEFAULT_RTL; + err = U_ZERO_ERROR; + ulen = ucnv_toUChars( + conv32, input, ulen, (const char *) s, len * sizeof(Unicode), &err); + ubidi_setReorderingMode( bidi, ubidi_reordering_mode ); + ubidi_setReorderingOptions(bidi, UBIDI_OPTION_REMOVE_CONTROLS); + ubidi_setPara( bidi, input, len, level, NULL, &err ); + ubidi_writeReordered( bidi, output, len, UBIDI_DO_MIRRORING, &err ); + ucnv_fromUChars( + conv32, (char *) s, len * sizeof(Unicode), output, ulen, &err); + } + if (input != NULL) + delete input; + if (output != NULL) + delete output; + ubidi_close(bidi); + ucnv_close(conv32); +#endif if (rawOrder) { return gFalse; @@ -5159,89 +5200,77 @@ void TextPage::assignColumns(TextLineFrag *frags, int nFrags, GBool oneRot) { int TextPage::dumpFragment(Unicode *text, int len, UnicodeMap *uMap, GooString *s) { - char lre[8], rle[8], popdf[8], buf[8]; - int lreLen, rleLen, popdfLen, n; - int nCols, i, j, k; - - nCols = 0; - - if (uMap->isUnicode()) { + char buf[8]; + int nCols = 0, i, n; + +#if HAVE_ICU + if (reordering_mode != ReorderingNotNeeded && uMap->isUnicode()) { + + char lre[8], rle[8], popdf[8]; + int lreLen, rleLen, popdfLen, j, k; + UBiDi *bidi = NULL; + UErrorCode err = U_ZERO_ERROR; + UBiDiLevel level = UBIDI_DEFAULT_LTR; + UChar *output = NULL, *input = NULL; + int32_t ulen; + UConverter *conv32 = NULL; lreLen = uMap->mapUnicode(0x202a, lre, sizeof(lre)); rleLen = uMap->mapUnicode(0x202b, rle, sizeof(rle)); popdfLen = uMap->mapUnicode(0x202c, popdf, sizeof(popdf)); - if (primaryLR) { - - i = 0; - while (i < len) { - // output a left-to-right section - for (j = i; j < len && !unicodeTypeR(text[j]); ++j) ; - for (k = i; k < j; ++k) { - n = uMap->mapUnicode(text[k], buf, sizeof(buf)); - s->append(buf, n); - ++nCols; - } - i = j; - // output a right-to-left section - for (j = i; - j < len && !(unicodeTypeL(text[j]) || unicodeTypeNum(text[j])); - ++j) ; - if (j > i) { - s->append(rle, rleLen); - for (k = j - 1; k >= i; --k) { - n = uMap->mapUnicode(text[k], buf, sizeof(buf)); - s->append(buf, n); - ++nCols; - } - s->append(popdf, popdfLen); - i = j; - } + conv32 = ucnv_open("UTF-32_PlatformEndian", &err); + ulen = ucnv_toUChars( + conv32, NULL, 0, (const char *) text, len * sizeof(Unicode), &err); + input = new UChar[ulen]; + output = new UChar[ulen]; + bidi = ubidi_open(); + + if (input && output && bidi && conv32) { + UBiDiReorderingMode ubidi_reordering_mode = + globalParams->icu_ReorderingMode(true, &reordering_mode); + if (primaryLR) { + level = UBIDI_DEFAULT_LTR; + s->append(lre, lreLen); } - - } else { - - // Note: This code treats numeric characters (European and - // Arabic/Indic) as left-to-right, which isn't strictly correct - // (incurs extra LRE/POPDF pairs), but does produce correct - // visual formatting. - s->append(rle, rleLen); - i = len - 1; - while (i >= 0) { - // output a right-to-left section - for (j = i; - j >= 0 && !(unicodeTypeL(text[j]) || unicodeTypeNum(text[j])); - --j) ; - for (k = i; k > j; --k) { - n = uMap->mapUnicode(text[k], buf, sizeof(buf)); - s->append(buf, n); - ++nCols; - } - i = j; - // output a left-to-right section - for (j = i; j >= 0 && !unicodeTypeR(text[j]); --j) ; - if (j < i) { - s->append(lre, lreLen); - for (k = j + 1; k <= i; ++k) { - n = uMap->mapUnicode(text[k], buf, sizeof(buf)); - s->append(buf, n); - ++nCols; - } - s->append(popdf, popdfLen); - i = j; - } + else { + level = UBIDI_DEFAULT_RTL; + s->append(rle, rleLen); + } + err = U_ZERO_ERROR; + ulen = ucnv_toUChars( + conv32, input, ulen, (const char *) text, len * sizeof(Unicode), &err); + ubidi_setReorderingMode( bidi, ubidi_reordering_mode ); + ubidi_setReorderingOptions(bidi, UBIDI_OPTION_REMOVE_CONTROLS); + ubidi_setPara( bidi, input, len, level, NULL, &err ); + ubidi_writeReordered( bidi, output, len, UBIDI_DO_MIRRORING, &err ); + ucnv_fromUChars(conv32, (char *) text, len * sizeof(Unicode), + output, ulen, &err); + UCharCharacterIterator iter(output, ulen); + for (iter.first(); iter.current() != CharacterIterator::DONE; iter.next()) { + n = uMap->mapUnicode(iter.current(), buf, sizeof(buf)); + s->append(buf, n); + ++nCols; } s->append(popdf, popdfLen); - + if (input != NULL) + delete input; + if (output != NULL) + delete output; + ubidi_close(bidi); + ucnv_close(conv32); } - - } else { + } + else { +#endif for (i = 0; i < len; ++i) { n = uMap->mapUnicode(text[i], buf, sizeof(buf)); s->append(buf, n); nCols += n; } +#if (HAVE_ICU) } +#endif return nCols; } @@ -5326,11 +5355,12 @@ static void TextOutputDev_outputToFile(void *stream, const char *text, int len) TextOutputDev::TextOutputDev(char *fileName, GBool physLayoutA, double fixedPitchA, GBool rawOrderA, - GBool append) { + GBool append, ReorderingMode reordering_modeA) { text = NULL; physLayout = physLayoutA; fixedPitch = physLayout ? fixedPitchA : 0; rawOrder = rawOrderA; + reordering_mode = reordering_modeA; doHTML = gFalse; ok = gTrue; @@ -5357,21 +5387,22 @@ TextOutputDev::TextOutputDev(char *fileName, GBool physLayoutA, } // set up text object - text = new TextPage(rawOrderA); + text = new TextPage(rawOrderA, reordering_modeA); actualText = new ActualText(text); } TextOutputDev::TextOutputDev(TextOutputFunc func, void *stream, GBool physLayoutA, double fixedPitchA, - GBool rawOrderA) { + GBool rawOrderA, ReorderingMode reordering_modeA) { outputFunc = func; outputStream = stream; needClose = gFalse; physLayout = physLayoutA; fixedPitch = physLayout ? fixedPitchA : 0; rawOrder = rawOrderA; + reordering_mode = reordering_modeA; doHTML = gFalse; - text = new TextPage(rawOrderA); + text = new TextPage(rawOrderA, reordering_modeA); actualText = new ActualText(text); ok = gTrue; } @@ -5638,6 +5669,6 @@ TextPage *TextOutputDev::takeText() { TextPage *ret; ret = text; - text = new TextPage(rawOrder); + text = new TextPage(rawOrder, reordering_mode); return ret; } diff --git a/poppler/TextOutputDev.h b/poppler/TextOutputDev.h index 56736b3..96faad4 100644 --- a/poppler/TextOutputDev.h +++ b/poppler/TextOutputDev.h @@ -39,6 +39,7 @@ #include "goo/gtypes.h" #include "GfxFont.h" #include "GfxState.h" +#include "RTL.h" #include "OutputDev.h" class GooString; @@ -486,7 +487,8 @@ class TextPage { public: // Constructor. - TextPage(GBool rawOrderA); + TextPage(GBool rawOrderA, + ReorderingMode reordering_modeA=ReorderingDefault); void incRefCnt(); void decRefCnt(); @@ -618,6 +620,7 @@ private: int primaryRot; // primary rotation GBool primaryLR; // primary direction (true means L-to-R, // false means R-to-L) + ReorderingMode reordering_mode; TextWord *rawWords; // list of words, in raw order (only if // rawOrder is set) TextWord *rawLastWord; // last word on rawWords list @@ -685,7 +688,8 @@ public: // content stream order. TextOutputDev(char *fileName, GBool physLayoutA, double fixedPitchA, GBool rawOrderA, - GBool append); + GBool append, + ReorderingMode reordering_modeA=ReorderingDefault); // Create a TextOutputDev which will write to a generic stream. If // is true, the original physical layout of the text @@ -693,7 +697,8 @@ public: // content stream order. TextOutputDev(TextOutputFunc func, void *stream, GBool physLayoutA, double fixedPitchA, - GBool rawOrderA); + GBool rawOrderA, + ReorderingMode reordering_modeA=ReorderingDefault); // Destructor. virtual ~TextOutputDev(); @@ -824,6 +829,7 @@ private: GBool rawOrder; // keep text in content stream order GBool doHTML; // extra processing for HTML conversion GBool ok; // set up ok? + ReorderingMode reordering_mode; ActualText *actualText; }; diff --git a/utils/Makefile.am b/utils/Makefile.am index 1dd9a12..74acf10 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -108,6 +108,8 @@ pdftotext_SOURCES = \ pdftotext.cc \ printencodings.cc \ printencodings.h \ + printreorderings.cc \ + printreorderings.h \ $(common) pdftohtml_SOURCES = \ diff --git a/utils/pdftotext.cc b/utils/pdftotext.cc index f7b2b0e..47cdbd1 100644 --- a/utils/pdftotext.cc +++ b/utils/pdftotext.cc @@ -37,6 +37,7 @@ #include #include "parseargs.h" #include "printencodings.h" +#include "printreorderings.h" #include "goo/GooString.h" #include "goo/gmem.h" #include "GlobalParams.h" @@ -73,6 +74,7 @@ static double fixedPitch = 0; static GBool rawOrder = gFalse; static GBool htmlMeta = gFalse; static char textEncName[128] = ""; +static char textReorderName[33] = ""; static char textEOL[16] = ""; static GBool noPageBreaks = gFalse; static char ownerPassword[33] = "\001"; @@ -81,6 +83,7 @@ static GBool quiet = gFalse; static GBool printVersion = gFalse; static GBool printHelp = gFalse; static GBool printEnc = gFalse; +static GBool printReorder = gFalse; static const ArgDesc argDesc[] = { {"-f", argInt, &firstPage, 0, @@ -109,6 +112,10 @@ static const ArgDesc argDesc[] = { "output text encoding name"}, {"-listenc",argFlag, &printEnc, 0, "list available encodings"}, + {"-reorder", argString, textReorderName, sizeof(textReorderName), + "rtl text reordering name"}, + {"-listreorder",argFlag, &printReorder, 0, + "list available rtl text reordering modes"}, {"-eol", argString, textEOL, sizeof(textEOL), "output end-of-line convention (unix, dos, or mac)"}, {"-nopgbrk", argFlag, &noPageBreaks, 0, @@ -170,6 +177,7 @@ int main(int argc, char *argv[]) { GBool ok; char *p; int exitCode; + ReorderingMode reorderingMode = ReorderingDefault; exitCode = 99; @@ -178,7 +186,7 @@ int main(int argc, char *argv[]) { if (bbox) { htmlMeta = gTrue; } - if (!ok || (argc < 2 && !printEnc) || argc > 3 || printVersion || printHelp) { + if (!ok || (argc < 2 && !printEnc && !printReorder) || argc > 3 || printVersion || printHelp) { fprintf(stderr, "pdftotext version %s\n", PACKAGE_VERSION); fprintf(stderr, "%s\n", popplerCopyright); fprintf(stderr, "%s\n", xpdfCopyright); @@ -200,6 +208,13 @@ int main(int argc, char *argv[]) { goto err0; } + if (printReorder) { + printReorderings(); + delete globalParams; + exitCode = 0; + goto err0; + } + fileName = new GooString(argv[1]); if (fixedPitch) { physLayout = gTrue; @@ -208,6 +223,9 @@ int main(int argc, char *argv[]) { if (textEncName[0]) { globalParams->setTextEncoding(textEncName); } + if (textReorderName[0]) { + globalParams->setRTLReordering(textReorderName); + } if (textEOL[0]) { if (!globalParams->setTextEOL(textEOL)) { fprintf(stderr, "Bad '-eol' value on command line\n"); @@ -226,6 +244,8 @@ int main(int argc, char *argv[]) { delete fileName; goto err1; } + + reorderingMode = globalParams->getRTLReordering(); // open PDF file if (ownerPassword[0] != '\001') { @@ -342,7 +362,14 @@ int main(int argc, char *argv[]) { // write text file if (bbox) { - textOut = new TextOutputDev(NULL, physLayout, fixedPitch, rawOrder, htmlMeta); + textOut = new TextOutputDev(NULL, physLayout, fixedPitch, rawOrder, htmlMeta, + reorderingMode); + if (!(f = fopen(textFileName->getCString(), "ab"))) { + error(errIO, -1, "Couldn't open text file '{0:t}' for append", textFileName); + exitCode = 2; + delete textOut; + goto err3; + } if (textOut->isOk()) { fprintf(f, "\n"); @@ -370,7 +397,7 @@ int main(int argc, char *argv[]) { fclose(f); } else { textOut = new TextOutputDev(textFileName->getCString(), - physLayout, fixedPitch, rawOrder, htmlMeta); + physLayout, fixedPitch, rawOrder, htmlMeta, reorderingMode); if (textOut->isOk()) { if ((w==0) && (h==0) && (x==0) && (y==0)) { doc->displayPages(textOut, firstPage, lastPage, resolution, resolution, 0, diff --git a/utils/printreorderings.cc b/utils/printreorderings.cc new file mode 100644 index 0000000..8d1d44e --- /dev/null +++ b/utils/printreorderings.cc @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2013, Alex Bodnaru + * + * 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, 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "printreorderings.h" + +#include "GlobalParams.h" +#include "goo/GooList.h" +#include "goo/GooString.h" + +void printReorderings() +{ + GooList *reorderNames = globalParams->getRTLReorderingNames(); + printf("Available RTL reorderings are:\n"); + for (int i = 0; i < reorderNames->getLength(); ++i) { + GooString *reorder = (GooString*)reorderNames->get(i); + printf("%s\n", reorder->getCString()); + } + delete reorderNames; +} diff --git a/utils/printreorderings.h b/utils/printreorderings.h new file mode 100644 index 0000000..826e93f --- /dev/null +++ b/utils/printreorderings.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2008, Albert Astals Cid + * + * 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, 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef PRINTREORDERINGS_H +#define PRINTREORDERINGS_H + +void printReorderings(); + +#endif -- 1.8.3.1