--- orig/poppler-0.20.3/configure.ac 2012-08-11 01:02:57.000000000 +0300 +++ poppler-0.20.3/configure.ac 2012-10-14 17:08:29.000000000 +0200 @@ -265,6 +264,42 @@ AC_DEFINE(HAVE_FSEEK64) fi +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 fribidi +AC_ARG_ENABLE(fribidi, + AC_HELP_STRING([--enable-fribidi], + [Build with fribidi based BIDI support. Enabled by default.]), + enable_fribidi=$enableval, + enable_fribidi="yes") +if test x$enable_fribidi = xyes; then + PKG_CHECK_MODULES([FRIBIDI], [fribidi], + [AC_DEFINE(HAVE_FRIBIDI, 1, [whether FriBidi is available])], [:]) + AC_SUBST(FRIBIDI_CFLAGS) + AC_SUBST(FRIBIDI_LIBS) +fi + dnl Test for zlib AC_ARG_ENABLE([zlib], [AS_HELP_STRING([--enable-zlib],[Build with zlib])], @@ -816,6 +851,8 @@ echo " use libjpeg: $enable_libjpeg" echo " use libpng: $enable_libpng" echo " use libtiff: $enable_libtiff" +echo " use fribidi: $enable_fribidi" +echo " use icu: $enable_icu" echo " use zlib: $enable_zlib" echo " use libcurl: $enable_libcurl" echo " use libopenjpeg: $enable_libopenjpeg" diff -Naur orig/poppler-0.20.3/poppler/Makefile.am poppler-0.20.3/poppler/Makefile.am --- orig/poppler-0.20.3/poppler/Makefile.am 2012-08-11 01:04:47.000000000 +0300 +++ poppler-0.20.3/poppler/Makefile.am 2012-10-11 19:20:54.000000000 +0200 @@ -138,10 +138,18 @@ cms_libs = $(LCMS_LIBS) endif +icu_includes = $(ICU_CFLAGS) +icu_libs = $(ICU_LIBS) + +fribidi_includes = $(FRIBIDI_CFLAGS) +fribidi_libs = $(FRIBIDI_LIBS) + INCLUDES = \ -I$(top_srcdir) \ -I$(top_srcdir)/goo \ $(cms_includes) \ + $(icu_includes) \ + $(fribidi_includes) \ $(splash_includes) \ $(cairo_includes) \ $(arthur_includes) \ @@ -165,6 +173,8 @@ $(top_builddir)/goo/libgoo.la \ $(top_builddir)/fofi/libfofi.la \ $(cms_libs) \ + $(icu_libs) \ + $(fribidi_libs) \ $(splash_libs) \ $(libtiff_libs) \ $(libjpeg_libs) \ diff -Naur orig/poppler-0.20.3/poppler/TextOutputDev.cc poppler-0.20.3/poppler/TextOutputDev.cc --- orig/poppler-0.20.3/poppler/TextOutputDev.cc 2012-06-11 16:19:41.000000000 +0300 +++ poppler-0.20.3/poppler/TextOutputDev.cc 2012-10-14 19:30:36.000000000 +0200 @@ -47,6 +47,13 @@ #include #include #include +#if HAVE_ICU +#include +#include +#endif +#if HAVE_FRIBIDI +#include +#endif #ifdef _WIN32 #include // for O_BINARY #include // for setmode @@ -3755,7 +3762,11 @@ } GooString *TextPage::getText(double xMin, double yMin, - double xMax, double yMax) { + double xMax, double yMax +#if HAVE_ICU + , ReorderingMode reorder_mode +#endif + ) { GooString *s; UnicodeMap *uMap; TextBlock *blk; @@ -3980,7 +3991,11 @@ } // get the fragment text - col += dumpFragment(frag->line->text + frag->start, frag->len, uMap, s); + col += dumpFragment(frag->line->text + frag->start, frag->len, uMap, s +#if HAVE_ICU + , reorder_mode +#endif + ); } if (multiLine) { @@ -4039,7 +4054,11 @@ virtual void visitWord (TextWord *word, int begin, int end, PDFRectangle *selection) { }; - GooString *getText(void); + GooString *getText( +#if HAVE_ICU + ReorderingMode reorder_mode=ReorderingNumbersSpecial +#endif + ); private: TextLineFrag *frags; @@ -4076,7 +4095,11 @@ } -GooString *TextSelectionDumper::getText (void) +GooString *TextSelectionDumper::getText ( +#if HAVE_ICU + ReorderingMode reorder_mode +#endif +) { GooString *s; TextLineFrag *frag; @@ -4157,7 +4180,11 @@ actual_line = 0; } - page->dumpFragment(frag->line->text + frag->start, frag->len, uMap, ((GooString*) strings->get (actual_line))); + page->dumpFragment(frag->line->text + frag->start, frag->len, uMap, ((GooString*) strings->get (actual_line)) +#if HAVE_ICU + , reorder_mode +#endif + ); last_length = frag->len; if (!frag->line->blk->tableEnd) { @@ -4166,7 +4193,11 @@ } // not a table else { - page->dumpFragment (frag->line->text + frag->start, frag->len, uMap, s); + page->dumpFragment (frag->line->text + frag->start, frag->len, uMap, s +#if HAVE_ICU + , reorder_mode +#endif + ); if (i < nFrags - 1) { s->append (eol, eolLen); } @@ -4828,7 +4859,11 @@ } void TextPage::dump(void *outputStream, TextOutputFunc outputFunc, - GBool physLayout) { + GBool physLayout +#if HAVE_ICU + , ReorderingMode reorder_mode +#endif + ) { UnicodeMap *uMap; TextFlow *flow; TextBlock *blk; @@ -4872,7 +4907,11 @@ for (word = rawWords; word; word = word->next) { s = new GooString(); - dumpFragment(word->text, word->len, uMap, s); + dumpFragment(word->text, word->len, uMap, s +#if HAVE_ICU + , reorder_mode +#endif + ); (*outputFunc)(outputStream, s->getCString(), s->getLength()); delete s; if (word->next && @@ -4946,7 +4985,11 @@ // print the line s = new GooString(); - col += dumpFragment(frag->line->text + frag->start, frag->len, uMap, s); + col += dumpFragment(frag->line->text + frag->start, frag->len, uMap, s +#if HAVE_ICU + , reorder_mode +#endif + ); (*outputFunc)(outputStream, s->getCString(), s->getLength()); delete s; @@ -4985,7 +5028,11 @@ --n; } s = new GooString(); - dumpFragment(line->text, n, uMap, s); + dumpFragment(line->text, n, uMap, s +#if HAVE_ICU + , reorder_mode +#endif + ); (*outputFunc)(outputStream, s->getCString(), s->getLength()); delete s; // output a newline when a hyphen is not suppressed @@ -5103,10 +5150,25 @@ } int TextPage::dumpFragment(Unicode *text, int len, UnicodeMap *uMap, - GooString *s) { + GooString *s +#if HAVE_ICU + , ReorderingMode reorder_mode +#endif + ) { char lre[8], rle[8], popdf[8], buf[8]; int lreLen, rleLen, popdfLen, n; int nCols, i, j, k; +#if HAVE_ICU + UBiDi *bidi = NULL; + UErrorCode err = U_ZERO_ERROR; + UBiDiLevel level = UBIDI_DEFAULT_LTR; + UChar *output = NULL, *input = NULL; + int32_t ulen; + UConverter *conv32 = NULL; +#elif HAVE_FRIBIDI + FriBidiCharType base_dir = FRIBIDI_TYPE_ON; + FriBidiChar *visual_str = NULL; +#endif nCols = 0; @@ -5116,6 +5178,85 @@ rleLen = uMap->mapUnicode(0x202b, rle, sizeof(rle)); popdfLen = uMap->mapUnicode(0x202c, popdf, sizeof(popdf)); +#if HAVE_ICU + if (reorder_mode != ReorderingNotNeeded) { + 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) { + if (primaryLR) { + level = UBIDI_DEFAULT_LTR; + s->append(lre, lreLen); + } + 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, (UBiDiReorderingMode) reorder_mode ); + 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 { +#elif HAVE_FRIBIDI + visual_str = new FriBidiChar[len]; + + if (visual_str) { + if (primaryLR) { + base_dir = FRIBIDI_TYPE_LTR; + s->append(lre, lreLen); + } + else { + base_dir = FRIBIDI_TYPE_RTL; + s->append(rle, rleLen); + } + fribidi_log2vis ( + /* input */ + (FriBidiChar *) text, + (FriBidiStrIndex) len, + /* input and output */ + &base_dir, + /* output */ + visual_str, + NULL, + NULL, + NULL + ); + for (i = 0; i < len; ++i) { + n = uMap->mapUnicode(Unicode(visual_str[i]), buf, sizeof(buf)); + if (!FRIBIDI_IS_EXPLICIT(Unicode(visual_str[i]))) + ++nCols; + s->append(buf, n); + } + s->append(popdf, popdfLen); + if (visual_str != NULL) + delete visual_str; + } + else { +#endif + if (primaryLR) { i = 0; @@ -5179,7 +5320,9 @@ s->append(popdf, popdfLen); } - +#if (HAVE_ICU || HAVE_FRIBIDI) + } +#endif } else { for (i = 0; i < len; ++i) { n = uMap->mapUnicode(text[i], buf, sizeof(buf)); @@ -5187,7 +5330,6 @@ nCols += n; } } - return nCols; } diff -Naur orig/poppler-0.20.3/poppler/TextOutputDev.h poppler-0.20.3/poppler/TextOutputDev.h --- orig/poppler-0.20.3/poppler/TextOutputDev.h 2012-05-10 23:05:44.000000000 +0300 +++ poppler-0.20.3/poppler/TextOutputDev.h 2012-10-14 19:14:12.000000000 +0200 @@ -33,6 +33,9 @@ #endif #include "poppler-config.h" +#if HAVE_ICU +#include +#endif #include #include "goo/gtypes.h" #include "GfxFont.h" @@ -479,6 +482,18 @@ // TextPage //------------------------------------------------------------------------ +#if HAVE_ICU +enum ReorderingMode { + ReorderingNotNeeded=0, + // all numbers ltr + ReorderingNumbersAsL=UBIDI_REORDER_INVERSE_NUMBERS_AS_L, + // windows + ReorderingNumbersSpecial=UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL, + // unicode (default) + ReorderingLikeDirect=UBIDI_REORDER_INVERSE_LIKE_DIRECT +}; +#endif + class TextPage { public: @@ -540,7 +555,11 @@ // Get the text which is inside the specified rectangle. GooString *getText(double xMin, double yMin, - double xMax, double yMax); + double xMax, double yMax +#if HAVE_ICU + , ReorderingMode reorder_mode=ReorderingNumbersSpecial +#endif + ); void visitSelection(TextSelectionVisitor *visitor, PDFRectangle *selection, @@ -569,7 +588,11 @@ // Dump contents of page to a file. void dump(void *outputStream, TextOutputFunc outputFunc, - GBool physLayout); + GBool physLayout +#if HAVE_ICU + , ReorderingMode reorder_mode=ReorderingNumbersSpecial +#endif + ); // Get the head of the linked list of TextFlows. TextFlow *getFlows() { return flows; } @@ -589,7 +612,11 @@ void clear(); void assignColumns(TextLineFrag *frags, int nFrags, GBool rot); - int dumpFragment(Unicode *text, int len, UnicodeMap *uMap, GooString *s); + int dumpFragment(Unicode *text, int len, UnicodeMap *uMap, GooString *s +#if HAVE_ICU + , ReorderingMode reorder_mode +#endif + ); GBool rawOrder; // keep text in content stream order