Changeset 21757


Ignore:
Timestamp:
06/07/17 10:49:41 (8 years ago)
Author:
Eric.Larour
Message:

CHG: diverse

Location:
issm/branches/trunk-larour-NatGeoScience2016/externalpackages
Files:
17 added
6 deleted
39 edited

Legend:

Unmodified
Added
Removed
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adjoinablempi/install-macosx64.sh

    r15778 r21757  
    55rm -rf src  install
    66
     7# Keeping this for potential future use
    78#Mercurial cloning:
    8 hg clone http://mercurial.mcs.anl.gov//ad/AdjoinableMPI src
     9#hg clone -r 268 http://mercurial.mcs.anl.gov//ad/AdjoinableMPI src
     10
     11#Download from ISSM server
     12$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/adjoinablempi' 'adjoinablempi.tar.gz'
     13
     14#Untar ADOL-C
     15tar -zxf  adjoinablempi.tar.gz
    916
    1017export C_INCLUDE_PATH=:/usr/include/sys/:$C_INCLUDE_PATH
     
    1219#Configure adjoinablempi
    1320cd src
    14 autoreconf -fi
    15 
    1621./configure \
    1722        --prefix="$ISSM_DIR/externalpackages/adjoinablempi/install" \
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adjoinablempi/install-pleaides-gcc.sh

    r16585 r21757  
    55rm -rf src  install
    66
     7# Keeping this for potential future use
    78#Mercurial cloning:
    8 hg clone http://mercurial.mcs.anl.gov//ad/AdjoinableMPI src
     9#hg clone -r 268 http://mercurial.mcs.anl.gov//ad/AdjoinableMPI src
     10
     11#Download from ISSM server
     12$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/adjoinablempi' 'adjoinablempi.tar.gz'
     13
     14#Untar ADOL-C
     15tar -zxf  adjoinablempi.tar.gz
    916
    1017#Configure adjoinablempi
    1118cd src
    12 autoreconf -fi
    13 
    1419./configure \
    1520        --prefix="$ISSM_DIR/externalpackages/adjoinablempi/install" \
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adjoinablempi/install-pleaides.sh

    r16270 r21757  
    55rm -rf src  install
    66
     7# Keeping this for potential future use
    78#Mercurial cloning:
    8 hg clone http://mercurial.mcs.anl.gov//ad/AdjoinableMPI src
     9#hg clone -r 268 http://mercurial.mcs.anl.gov//ad/AdjoinableMPI src
     10
     11#Download from ISSM server
     12$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/adjoinablempi' 'adjoinablempi.tar.gz'
     13
     14#Untar ADOL-C
     15tar -zxf  adjoinablempi.tar.gz
    916
    1017#Configure adjoinablempi
    1118cd src
    12 autoreconf -fi
    13 
    1419./configure \
    1520        --prefix="$ISSM_DIR/externalpackages/adjoinablempi/install" \
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adjoinablempi/install.sh

    r17280 r21757  
    55rm -rf src  install
    66
     7# Keeping this for potential future use
    78#Mercurial cloning:
    8 hg clone http://mercurial.mcs.anl.gov//ad/AdjoinableMPI src
     9#hg clone -r 268 http://mercurial.mcs.anl.gov//ad/AdjoinableMPI src
     10
     11#Download from ISSM server
     12$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/adjoinablempi' 'adjoinablempi.tar.gz'
     13
     14#Untar ADOL-C
     15tar -zxf  adjoinablempi.tar.gz
    916
    1017#Configure adjoinablempi
    1118cd src
    12 autoreconf -fi
    13 
    1419./configure \
    1520        --prefix="$ISSM_DIR/externalpackages/adjoinablempi/install" \
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adolc/install-withampi-macosx64.sh

    r19761 r21757  
    55rm -rf install src
    66
    7 git clone https://gitlab.com/adol-c/adol-c.git src
    8 #git clone -b ampi git://git.mcs.anl.gov/adol-c.git src
    9 #git reset --hard b254b2a001a1b7a024a9184cd087ae06eb975cad
     7# Keeping the following commented line for potential future use.
     8#git clone https://gitlab.com/adol-c/adol-c.git src
     9
     10#Download from ISSM server
     11$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/ADOL-C' 'ADOL-C.tar.gz'
     12
     13#Untar ADOL-C
     14tar -zxf  ADOL-C.tar.gz
    1015
    1116#Compile ADOL-C
    12 cd src
    13 autoreconf -f -i
    14 
    1517export CFLAGS="-O2 -L$ISSM_DIR/externalpackages/mpich/install/lib -lpmpich"
    1618export CXXFLAGS="-O2 -L$ISSM_DIR/externalpackages/mpich/install/lib -lpmpich"
    1719
     20cd src
    1821./configure --prefix=$ISSM_DIR/externalpackages/adolc/install  \
    1922        --libdir=$ISSM_DIR/externalpackages/adolc/install/lib \
     
    2124        --enable-ampi \
    2225        --with-ampi=$ISSM_DIR/externalpackages/adjoinablempi/install \
    23         --with-soname=adolc \
     26        --with-soname=adolc \
    2427        --disable-tapedoc-values
    2528
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adolc/install-withampi-pleiades-gcc.sh

    r17292 r21757  
    55rm -rf install src
    66
    7 git clone -b ampi git://git.mcs.anl.gov/adol-c.git src
    8 #git reset --hard b254b2a001a1b7a024a9184cd087ae06eb975cad
     7# Keeping the following commented line for potential future use.
     8#git clone https://gitlab.com/adol-c/adol-c.git src
     9
     10#Download from ISSM server
     11$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/ADOL-C' 'ADOL-C.tar.gz'
     12
     13#Untar ADOL-C
     14tar -zxf  ADOL-C.tar.gz
    915
    1016#Compile ADOL-C
    1117cd src
    12 autoreconf -f -i
    13 
    1418./configure --prefix=$ISSM_DIR/externalpackages/adolc/install  \
    1519        --libdir=$ISSM_DIR/externalpackages/adolc/install/lib \
     
    1721        --enable-ampi \
    1822        --with-ampi=$ISSM_DIR/externalpackages/adjoinablempi/install \
    19         --with-soname=adolc \
     23        --with-soname=adolc \
    2024        --disable-tapedoc-values
    2125
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adolc/install-withampi-pleiades.sh

    r17292 r21757  
    55rm -rf install src
    66
    7 git clone -b ampi git://git.mcs.anl.gov/adol-c.git src
    8 #git reset --hard b254b2a001a1b7a024a9184cd087ae06eb975cad
     7# Keeping the following commented line for potential future use.
     8#git clone https://gitlab.com/adol-c/adol-c.git src
     9
     10#Download from ISSM server
     11$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/ADOL-C' 'ADOL-C.tar.gz'
     12
     13#Untar ADOL-C
     14tar -zxf  ADOL-C.tar.gz
    915
    1016#Compile ADOL-C
    1117cd src
    12 autoreconf -f -i
    13 
    1418./configure --prefix=$ISSM_DIR/externalpackages/adolc/install  \
    1519        --libdir=$ISSM_DIR/externalpackages/adolc/install/lib \
     
    1721        --enable-ampi \
    1822        --with-ampi=$ISSM_DIR/externalpackages/adjoinablempi/install \
    19         --with-soname=adolc \
     23        --with-soname=adolc \
    2024        --disable-tapedoc-values
    2125
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adolc/install-withampi.sh

    r19490 r21757  
    55rm -rf install src
    66
    7 git clone https://gitlab.com/adol-c/adol-c.git src
    8 #git reset --hard b254b2a001a1b7a024a9184cd087ae06eb975cad
     7# Keeping the following commented line for potential future use.
     8#git clone https://gitlab.com/adol-c/adol-c.git src
     9
     10#Download from ISSM server
     11$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/ADOL-C' 'ADOL-C.tar.gz'
     12
     13#Untar ADOL-C
     14tar -zxf  ADOL-C.tar.gz
    915
    1016#Compile ADOL-C
    1117cd src
    12 autoreconf -f -i
    13 
    1418./configure --prefix=$ISSM_DIR/externalpackages/adolc/install  \
    1519        --libdir=$ISSM_DIR/externalpackages/adolc/install/lib \
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/adolc/install.sh

    r19491 r21757  
    55rm -rf install src
    66
    7 git clone https://gitlab.com/adol-c/adol-c.git src
    8 #git reset --hard b254b2a001a1b7a024a9184cd087ae06eb975cad
     7# Keeping the following commented line for potential future use.
     8#git clone https://gitlab.com/adol-c/adol-c.git src
     9
     10#Download from ISSM server
     11$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/ADOL-C' 'ADOL-C.tar.gz'
     12
     13#Untar ADOL-C
     14tar -zxf  ADOL-C.tar.gz
    915
    1016#Compile ADOL-C
    1117cd src
    12 
    13 autoreconf -f -i
    1418./configure --prefix=$ISSM_DIR/externalpackages/adolc/install \
    1519        --libdir=$ISSM_DIR/externalpackages/adolc/install/lib
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/boost/install-1.55-lonestar.sh

    r19581 r21757  
    2222rm -rf boost_1_55_0
    2323
    24 patch src/boost/mpl/aux_/config/adl.hpp ./configs/1.55/adl.hpp.lonestar.patch
     24patch src/boost/mpl/aux_/config/adl.hpp ./configs/1.55/adl.hpp.patch
    2525
    2626#Configure and compile
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/boost/install-1.55-pleiades.sh

    r18340 r21757  
    2222rm -rf boost_1_55_0
    2323
    24 #patch src/boost/atomic/detail/cas128strong.hpp ./configs/1.55/cas128strong.hpp.patch
    25 #patch src/boost/atomic/detail/gcc-atomic.hpp ./configs/1.55/gcc-atomic.hpp.patch
    26 #patch src/tools/build/v2/user-config.jam ./configs/1.55/user-config.jam.patch
    27 #patch src/tools/build/v2/tools/darwin.jam ./configs/1.55/darwin.jam.patch
    28 #patch src/tools/build/v2/tools/darwin.py ./configs/1.55/darwin.py.patch
     24patch src/boost/mpl/aux_/config/adl.hpp ./configs/1.55/adl.hpp.patch
    2925
    3026#Configure and compile
     
    3228./bootstrap.sh \
    3329        --prefix="$ISSM_DIR/externalpackages/boost/install" \
    34         --with-python=python2.7 \
    35         --with-python-root="$ISSM_DIR/externalpackages/python/install" \
    36         --with-toolset=intel-linux
     30        --with-python=python3.2 \
     31        --with-python-root="$ISSM_DIR/externalpackages/python/install"
    3732
    3833#Compile boost
    39 # Need gcc with iconv installed in a location that has been added to your path
    40 ./b2 toolset=intel-linux cxxflags=-static-libstdc++ linkflags=-static-libstdc++ threading=multi install
    41 #./bjam install
    42 
    43 #./b2 toolset=clang cxxflags=-stdlib=libstdc++ linkflags=-stdlib=libstdc++ -j2 variant=release link=static threading=multi instal
     34./bjam install
    4435
    4536#put bjam into install also:
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/chaco/chaco_njs_notes.txt

    r11606 r21757  
    22sudo ln -s /usr/include/malloc/malloc.h /usr/include/malloc.h
    33or else make sure you include the malloc directory in path
     4
     5if on sierra, you must download a version of malloc.h -
     6one can be found here: http://web.mit.edu/jhawk/mnt/spo/phone-project/include/malloc.h
     7place this file in /usr/local/include/, and run the sierra install script
     8
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/dakota/configs/6.2/CMakeLists.txt.pfe.patch

    r19659 r21757  
    33---
    44> # TODO: Can't this be integrated into the following logic?
    5 > set(BLAS_LIBS "-L/nasa/intel/mkl/10.0.011/lib/em64t/ -lmkl -lmkl_lapack -liomp5 -lpthread")
    6 > set(LAPACK_LIBS "-L/nasa/intel/mkl/10.0.011/lib/em64t/ -lmkl -lmkl_lapack -liomp5 -lpthread")
     5> set(BLAS_LIBS "-L/nasa/intel/Compiler/2016.2.181/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64/ -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -L/usr/lib64/ -lpthread -lm")
     6> set(LAPACK_LIBS "-L/nasa/intel/Compiler/2016.2.181/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64/ -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -L/usr/lib64/ -lpthread -lm")
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/LICENSE

    r19993 r21757  
    1 Copyright (c) 2014, Oliver J. Woodford
     1Copyright (c) 2014, Oliver J. Woodford, Yair M. Altman
    22All rights reserved.
    33
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/README.md

    r19993 r21757  
    2525|![](https://farm6.staticflickr.com/5616/15589249291_16e485c29a_o_d.png)|![](https://farm4.staticflickr.com/3944/15406302850_4d2e1c7afa_o_d.png)|![](https://farm6.staticflickr.com/5607/15568225476_8ce9bd5f6b_o_d.png)|
    2626
    27 Note that the size and background colour of test2.png (the output of export_fig) are the same as those of the on screen figure, in contrast to test.png. Of course, if you want want the figure background to be white (or any other colour) in the exported file then you can set this prior to exporting using:
     27Note that the size and background colour of test2.png (the output of export_fig) are the same as those of the on screen figure, in contrast to test.png. Of course, if you want the figure background to be white (or any other colour) in the exported file then you can set this prior to exporting using:
    2828```Matlab
    2929set(gcf, 'Color', 'w');
     
    149149**Cropping** - by default, export_fig crops its output to minimize the amount of empty space around the figure. If you'd prefer the figure to be uncropped, and instead have the same appearance (in terms of border width) as the on screen figure, then use the `-nocrop` option. 
    150150 
    151 **Colourspace** - by default, export_fig generates files in the RGB [colourspace](http://en.wikipedia.org/wiki/Color_space). However, you can also export in greyscale or the CMYK colourspace, using the `-grey` (or `-gray`) and `-cmyk` options respectively. The CMYK option is useful for publishers who require documents in this colourspace, but the option is only supported for PDF, EPS and TIFF files.
     151**Colourspace** - by default, export_fig generates files in the RGB [colourspace](https://en.wikipedia.org/wiki/Color_space). However, you can also export in greyscale or the CMYK colourspace, using the `-grey` (or `-gray`) and `-cmyk` options respectively. The CMYK option is useful for publishers who require documents in this colourspace, but the option is only supported for PDF, EPS and TIFF files.
    152152
    153153**Specifying a target directory** - you can get export_fig to save output files to any directory (for which you have write permission), simply by specifying the full or relative path in the filename. For example:
     
    171171**Specifying the figure/axes** - if you have mutiple figures open you can specify which figure to export using its handle: 
    172172```Matlab
    173 export_fig(figure_handle, 'filename.fmt');
     173export_fig(figure_handle, filename);
    174174```
    175175Equally, if your figure contains several subplots then you can export just one of them by giving export_fig the handle to the relevant axes:
    176176```Matlab
    177 export_fig(axes_handle, 'filename.fmt');
     177export_fig(axes_handle, filename);
    178178```
    179179
     
    195195**Appending to a file** - you can use the `-append` option to append the figure to the end of an image/document, if it already exists. This is supported for PDF and TIFF files only. Note that if you wish to append a lot of figures consecutively to a PDF, it can be more efficient to save all the figures to PDF separately then append them all in one go at the end (e.g. using [append_pdfs](http://www.mathworks.com/matlabcentral/fileexchange/31215-appendpdfs)). 
    196196 
     197**Output to clipboard** - you can use the `-clipboard` option to copy the specified figure or axes to the system clipboard, for easy paste into other documents (e.g., Word or PowerPoint). Note that the image is copied in bitmap (not vector) format. 
     198 
    197199**Font size** - if you want to place an exported figure in a document with the font a particular size then you need to set the font to that size in the figure, and not resize the output of export_fig in the document. To avoid resizing, simply make sure that the on screen figure is the size you want the output to be in the document before exporting. 
    198200 
    199 **Renderers** - MATLAB has three renderers for displaying and exporting figures: painters, OpenGL and ZBuffer. The different renderers have different [features](http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f3-84337.html#f3-102410), so if you aren't happy with the result from one renderer try another. By default, vector formats (i.e. PDF and EPS outputs) use the painters renderer, while other formats use the OpenGL renderer. Non-default renderers can be selected by using one of these three export_fig input options: `-painters`, `-opengl`, `-zbuffer`. 
     201**Renderers** - MATLAB has three renderers for displaying and exporting figures: painters, OpenGL and ZBuffer. The different renderers have different [features](http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f3-84337.html#f3-102410), so if you aren't happy with the result from one renderer try another. By default, vector formats (i.e. PDF and EPS outputs) use the painters renderer, while other formats use the OpenGL renderer. Non-default renderers can be selected by using one of these three export_fig input options: `-painters`, `-opengl`, `-zbuffer`: 
     202```Matlab
     203export_fig test.png -painters
     204```
    200205 
    201206**Artifacts** - sometimes the output that you get from export_fig is not what you expected. If an output file contains artifacts that aren't in the on screen figure then make sure that the renderer used for rendering the figure on screen is the same as that used for exporting. To set the renderer used to display the figure, use: 
     
    215220
    216221**Undefined function errors** - If you download and run export_fig and get an error similar to this: 
    217 ```Matlab
     222```
    218223??? Undefined function or method 'print2array' for input arguments of type 'double'.
    219224```
     
    221226 
    222227### Known issues
    223 There are lots of problems with MATLAB's exporting functions, and unfortunately export_fig, which is simply a glorified wrapper for MATLAB's print function, doesn't solve all of them (yet?). Some of the problems I know about are:
    224  
    225 **Fonts** - when using the painters renderer, MATLAB can only export a small number of fonts, details of which can be found [here](http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f3-103191.html#f3-96545). Export_fig attempts to correct font names in the resulting EPS file (for upto a maximum of 11 different fonts in one figure), but this is not always guaranteed to work. In particular, the text positions will be affected. It also does not work for text blocks where the 'Interpreter' property is set to 'latex'.
    226 
    227 Also, when using the painters renderer, ghostscript will sometimes throw an error such as `Error: /undefined in /findfont`. This suggests that ghostscript could not find a definition file for one of your fonts. One possible fix for this is to make sure the file `EXPORT_FIG_PATH/.ignore/gs_font_path.txt` exists and contains a list of paths to the folder(s) containing the necessary font definitions (make sure they're TrueType definitions), separated by a semicolon.
    228 
    229 **RGB color data not yet supported in Painter's mode** - you will see this as a warning if you try to export a figure which contains patch objects whose face or vertex colors are specified as a an RGB colour, rather than an index into the colormap, using the painters renderer (the default renderer for vector output). This problem can arise if you use `pcolor`, for example. This is a problem with MATLAB's painters renderer, which also affects `print`; there is currently no fix available in export_fig (other than to export to bitmap). The suggested workaround is to avoid colouring patches using RGB. First, try to use colours in the figure's colourmap (instructions [here](http://www.mathworks.co.uk/support/solutions/en/data/1-6OTPQE/)) - change the colourmap, if necessary. If you are using `pcolor`, try using [uimagesc](http://www.mathworks.com/matlabcentral/fileexchange/11368) (on the file exchange) instead. 
     228There are lots of problems with MATLAB's exporting functions, especially `print`. Export_fig is simply a glorified wrapper for MATLAB's `print` function, and doesn't solve all of its bugs (yet?). Some of the problems I know about are:
     229 
     230**Fonts** - when using the painters renderer, MATLAB can only export a small number of fonts, details of which can be found [here](http://www.mathworks.com/help/releases/R2014a/matlab/creating_plots/choosing-a-printer-driver.html#f3-96545). Export_fig attempts to correct font names in the resulting EPS file (up to a maximum of 11 different fonts in one figure), but this is not always guaranteed to work. In particular, the text positions will be affected. It also does not work for text blocks where the 'Interpreter' property is set to 'latex'.
     231
     232Also, when using the painters renderer, ghostscript will sometimes throw an error such as `Error: /undefined in /findfont`. This suggests that ghostscript could not find a definition file for one of your fonts. One possible fix for this is to make sure the file `EXPORT_FIG_PATH/.ignore/gs_font_path.txt` exists and contains a list of paths to the folder(s) containing the necessary font definitions (make sure that they are TrueType definitions!), separated by a semicolon.
     233
     234**RGB color data not yet supported in Painter's mode** - you will see this as a warning if you try to export a figure which contains patch objects whose face or vertex colors are specified as an RGB colour, rather than an index into the colormap, using the painters renderer (the default renderer for vector output). This problem can arise if you use `pcolor`, for example. This is a problem with MATLAB's painters renderer, which also affects `print`; there is currently no fix available in export_fig (other than to export to bitmap). The suggested workaround is to avoid colouring patches using RGB. First, try to use colours in the figure's colourmap (instructions [here](http://www.mathworks.co.uk/support/solutions/en/data/1-6OTPQE/)) - change the colourmap, if necessary. If you are using `pcolor`, try using [uimagesc](http://www.mathworks.com/matlabcentral/fileexchange/11368) (on the file exchange) instead. 
    230235
    231236**Dashed contour lines appear solid** - when using the painters renderer, MATLAB cannot generate dashed lines using the `contour` function (either on screen or in exported PDF and EPS files). Details can be found [here](http://www.mathworks.com/support/solutions/en/data/1-14PPHB/?solution=1-14PPHB). 
     
    233238**Text size** - when using the OpenGL or ZBuffer renderers, large text can be resized relative to the figure when exporting at non-screen-resolution (including using anti-alising at screen resolution). This is a feature of MATLAB's `print `function. In this case, try using the `-painters` option. 
    234239 
    235 **Lighting and transparency** - when using the painters renderer, transparency and lighting effects are not supported. Sorry, but this is a feature of the renderer. To find out more about the capabilities of each rendering method, see [here](http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f3-84337.html#f3-102410). You can still export transparent objects to vector format (SVG) using the excellent [plot2svg](http://www.mathworks.com/matlabcentral/fileexchange/7401) package, then convert this to PDF, for example using [Inkscape](http://inkscape.org/). However, it can't handle lighting. 
    236  
    237 **Lines in patch objects** - when exporting patch objects to PDF using the painters renderer (default), sometimes the output can appear to have lines across the middle of rectangular patches; these lines are the colour of the background, as if there is a crack in the patch, allowing you to see through. This issue is a feature of the software used to display the PDF, rather than the PDF itself. Sometimes disabling anti-aliasing in this software can get rid of the lines
     240**Lighting and transparency** - when using the painters renderer, transparency and lighting effects are not supported. Sorry, but this is an inherent feature of MATLAB's painters renderer. To find out more about the capabilities of each rendering method, see [here](http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f3-84337.html#f3-102410). You can still export transparent objects to vector format (SVG) using the excellent [plot2svg](http://www.mathworks.com/matlabcentral/fileexchange/7401) package, then convert this to PDF, for example using [Inkscape](http://inkscape.org/). However, it can't handle lighting. 
     241 
     242**Lines in patch objects** - when exporting patch objects to PDF using the painters renderer (default), sometimes the output can appear to have lines across the middle of rectangular patches; these lines are the colour of the background, as if there is a crack in the patch, allowing you to see through. This appears to be due to bugs in MATLAB's internal vector rendering code. These lines can often be removed from the PDF using software such as [InkScape](https://inkscape.org). Sometimes disabling anti-aliasing in the PDF-reader software can get rid of the lines ([discussion](https://github.com/altmany/export_fig/issues/44))
    238243 
    239244**Out of memory** - if you run into memory issues when using export_fig, some ways to get round this are: 
    240245 1. Reduce the level of anti-aliasing.
    241  2. Reduce the size of the on screen figure.
    242  3. Reduce the resolution (dpi) the figure is exported at. 
     246 2. Reduce the size of the figure.
     247 3. Reduce the export resolution (dpi).
     248 4. Change the renderer to painters or ZBuffer. 
    243249 
    244250**Errors** - the other common type of errors people get with export_fig are OpenGL errors. This isn't a fault of export_fig, but either a bug in MATLAB's `print`, or your graphics driver getting itself into a state. Always make sure your graphics driver is up-to-date. If it still doesn't work, try using the ZBuffer renderer. 
     
    249255Secondly, if exporting to bitmap, do try all the renderers (i.e. try the options `-opengl`, `-zbuffer` and `-painters` separately), to see if one of them does produce an acceptable output, and if so, use that.
    250256
    251 If the figure looks correct on screen, but an error exists in the exported output (which cannot be solved using a different renderer) then please feel free to raise an [issue](https://github.com/ojwoodford/export_fig/issues). Please be sure to include the .fig file, the export_fig command you use, the output you get, and a description of what you expected. I can't promise anything, but if it's easy to fix I probably will do it. Often I will find that the error is due to a bug in MATLAB's print function, in which case I will suggest you submit it as a bug to TheMathWorks, and inform me of any fix they suggest. Also, if there's a feature you'd like that isn't supported please tell me what it is and I'll consider implementing it.
     257If this still does not help, then ensure that you are using the latest version of export_fig, which is available [here](https://github.com/altmany/export_fig/archive/master.zip). 
     258 
     259If the figure looks correct on screen, but an error exists in the exported output (which cannot be solved using a different renderer) then please feel free to raise an [issue](https://github.com/altmany/export_fig/issues). Please be sure to include the .fig file, the export_fig command you use, the output you get, and a description of what you expected. I can't promise anything, but if it's easy to fix I may indeed do it. Often I will find that the error is due to a bug in MATLAB's `print` function, in which case I will suggest you submit it as a bug to TheMathWorks, and inform me of any fix they suggest. Also, if there's a feature you'd like that isn't supported please tell me what it is and I'll consider implementing it.
    252260
    253261### And finally...
     
    255263![](https://farm4.staticflickr.com/3956/15591911455_b9008bd77e_o_d.jpg)
    256264
    257 If you've ever wondered what's going on in the icon on the export_fig download page (reproduced on the left), then this explanantion is for you. The icon is designed to demonstrate as many of export_fig's features as possible. Given a
    258 figure containing a translucent mesh (top right), export_fig can export to pdf (bottom centre), which allows the figure to be zoomed in without losing quality (because it's a vector graphic), but isn't able to reproduce the translucency, and also, depending on the viewer, creates small gaps between the patches, which are seen here as thin white lines. By contrast, when exporting to png (top left), translucency is preserved (see how the graphic below shows through), the figure is anti-aliased, but zooming in does not reveal more detail.
    259 
     265If you've ever wondered what's going on in the logo on the export_fig download page (reproduced here), then this explanantion is for you. The logo is designed to demonstrate as many of export_fig's features as possible:
     266 
     267Given a figure containing a translucent mesh (top right), export_fig can export to pdf (bottom centre), which allows the figure to be zoomed-in without losing quality (because it's a vector graphic), but isn't able to reproduce the translucency. Also, depending on the PDF viewer program, small gaps appear between the patches, which are seen here as thin white lines.
     268 
     269By contrast, when exporting to png (top left), translucency is preserved (see how the graphic below shows through), and the figure is anti-aliased. However, zooming-in does not reveal more detail since png is a bitmap format. Also, lines appear less sharp than in the pdf output.
     270
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/append_pdfs.m

    r19993 r21757  
    3333% setting
    3434
     35% 26/02/15: If temp dir is not writable, use the output folder for temp
     36%           files when appending (Javier Paredes); sanity check of inputs
     37
    3538function append_pdfs(varargin)
     39
     40if nargin < 2,  return;  end  % sanity check
     41
    3642% Are we appending or creating a new file
    3743append = exist(varargin{1}, 'file') == 2;
    38 if append
    39     output = [tempname '.pdf'];
    40 else
     44output = [tempname '.pdf'];
     45try
     46    % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
     47    fid = fopen(output,'w');
     48    fwrite(fid,1);
     49    fclose(fid);
     50    delete(output);
     51    isTempDirOk = true;
     52catch
     53    % Temp dir is not writable, so use the output folder
     54    [dummy,fname,fext] = fileparts(output); %#ok<ASGLU>
     55    fpath = fileparts(varargin{1});
     56    output = fullfile(fpath,[fname fext]);
     57    isTempDirOk = false;
     58end
     59if ~append
    4160    output = varargin{1};
    4261    varargin = varargin(2:end);
    4362end
    4463% Create the command file
    45 cmdfile = [tempname '.txt'];
     64if isTempDirOk
     65    cmdfile = [tempname '.txt'];
     66else
     67    cmdfile = fullfile(fpath,[fname '.txt']);
     68end
    4669fh = fopen(cmdfile, 'w');
    4770fprintf(fh, '-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="%s" -f', output);
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/copyfig.m

    r19993 r21757  
     1function fh = copyfig(fh)
    12%COPYFIG Create a copy of a figure, without changing the figure
    23%
     
    1516% Copyright (C) Oliver Woodford 2012
    1617
    17 function fh = copyfig(fh)
    18 % Set the default
    19 if nargin == 0
    20     fh = gcf;
     18% 26/02/15: If temp dir is not writable, use the dest folder for temp
     19%           destination files (Javier Paredes)
     20% 15/04/15: Suppress warnings during copyobj (Dun Kirk comment on FEX page 2013-10-02)
     21
     22    % Set the default
     23    if nargin == 0
     24        fh = gcf;
     25    end
     26    % Is there a legend?
     27    if isempty(findall(fh, 'Type', 'axes', 'Tag', 'legend'))
     28        % Safe to copy using copyobj
     29        oldWarn = warning('off'); %#ok<WNOFF>  %Suppress warnings during copyobj (Dun Kirk comment on FEX page 2013-10-02)
     30        fh = copyobj(fh, 0);
     31        warning(oldWarn);
     32    else
     33        % copyobj will change the figure, so save and then load it instead
     34        tmp_nam = [tempname '.fig'];
     35        try
     36            % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
     37            fid = fopen(tmp_nam,'w');
     38            fwrite(fid,1);
     39            fclose(fid);
     40            delete(tmp_nam);  % cleanup
     41        catch
     42            % Temp dir is not writable, so use the current folder
     43            [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
     44            fpath = pwd;
     45            tmp_nam = fullfile(fpath,[fname fext]);
     46        end
     47        hgsave(fh, tmp_nam);
     48        fh = hgload(tmp_nam);
     49        delete(tmp_nam);
     50    end
    2151end
    22 % Is there a legend?
    23 if isempty(findall(fh, 'Type', 'axes', 'Tag', 'legend'))
    24     % Safe to copy using copyobj
    25     fh = copyobj(fh, 0);
    26 else
    27     % copyobj will change the figure, so save and then load it instead
    28     tmp_nam = [tempname '.fig'];
    29     hgsave(fh, tmp_nam);
    30     fh = hgload(tmp_nam);
    31     delete(tmp_nam);
    32 end
    33 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/crop_borders.m

    r19993 r21757  
     1function [A, vA, vB, bb_rel] = crop_borders(A, bcol, padding, crop_amounts)
    12%CROP_BORDERS Crop the borders of an image or stack of images
    23%
    3 %   [B, v] = crop_borders(A, bcol, [padding])
     4%   [B, vA, vB, bb_rel] = crop_borders(A, bcol, [padding])
    45%
    56%IN:
    67%   A - HxWxCxN stack of images.
    78%   bcol - Cx1 background colour vector.
    8 %   padding - scalar indicating how many pixels padding to have. Default: 0.
     9%   padding - scalar indicating how much padding to have in relation to
     10%             the cropped-image-size (0<=padding<=1). Default: 0
     11%   crop_amounts - 4-element vector of crop amounts: [top,right,bottom,left]
     12%             where NaN/Inf indicate auto-cropping, 0 means no cropping,
     13%             and any other value mean cropping in pixel amounts.
    914%
    1015%OUT:
    1116%   B - JxKxCxN cropped stack of images.
    12 %   v - 1x4 vector of start and end indices for first two dimensions, s.t.
    13 %       B = A(v(1):v(2),v(3):v(4),:,:).
     17%   vA     - coordinates in A that contain the cropped image
     18%   vB     - coordinates in B where the cropped version of A is placed
     19%   bb_rel - relative bounding box (used for eps-cropping)
    1420
    15 function [A, v] = crop_borders(A, bcol, padding)
    16 if nargin < 3
    17     padding = 0;
    18 end
    19 [h, w, c, n] = size(A);
    20 if isscalar(bcol)
    21     bcol = bcol(ones(c, 1));
    22 end
    23 bail = false;
    24 for l = 1:w
    25     for a = 1:c
    26         if ~all(col(A(:,l,a,:)) == bcol(a))
    27             bail = true;
    28             break;
     21%{
     22% 06/03/15: Improved image cropping thanks to Oscar Hartogensis
     23% 08/06/15: Fixed issue #76: case of transparent figure bgcolor
     24% 21/02/16: Enabled specifying non-automated crop amounts
     25% 04/04/16: Fix per Luiz Carvalho for old Matlab releases
     26% 23/10/16: Fixed issue #175: there used to be a 1px minimal padding in case of crop, now removed
     27%}
     28
     29    if nargin < 3
     30        padding = 0;
     31    end
     32    if nargin < 4
     33        crop_amounts = nan(1,4);  % =auto-cropping
     34    end
     35    crop_amounts(end+1:4) = NaN;  % fill missing values with NaN
     36
     37    [h, w, c, n] = size(A);
     38    if isempty(bcol)  % case of transparent bgcolor
     39        bcol = A(ceil(end/2),1,:,1);
     40    end
     41    if isscalar(bcol)
     42        bcol = bcol(ones(c, 1));
     43    end
     44
     45    % Crop margin from left
     46    if ~isfinite(crop_amounts(4))
     47        bail = false;
     48        for l = 1:w
     49            for a = 1:c
     50                if ~all(col(A(:,l,a,:)) == bcol(a))
     51                    bail = true;
     52                    break;
     53                end
     54            end
     55            if bail
     56                break;
     57            end
    2958        end
     59    else
     60        l = 1 + abs(crop_amounts(4));
    3061    end
    31     if bail
    32         break;
     62
     63    % Crop margin from right
     64    if ~isfinite(crop_amounts(2))
     65        bcol = A(ceil(end/2),w,:,1);
     66        bail = false;
     67        for r = w:-1:l
     68            for a = 1:c
     69                if ~all(col(A(:,r,a,:)) == bcol(a))
     70                    bail = true;
     71                    break;
     72                end
     73            end
     74            if bail
     75                break;
     76            end
     77        end
     78    else
     79        r = w - abs(crop_amounts(2));
    3380    end
    34 end
    35 bcol = A(ceil(end/2),w,:,1);
    36 bail = false;
    37 for r = w:-1:l
    38     for a = 1:c
    39         if ~all(col(A(:,r,a,:)) == bcol(a))
    40             bail = true;
    41             break;
     81
     82    % Crop margin from top
     83    if ~isfinite(crop_amounts(1))
     84        bcol = A(1,ceil(end/2),:,1);
     85        bail = false;
     86        for t = 1:h
     87            for a = 1:c
     88                if ~all(col(A(t,:,a,:)) == bcol(a))
     89                    bail = true;
     90                    break;
     91                end
     92            end
     93            if bail
     94                break;
     95            end
    4296        end
     97    else
     98        t = 1 + abs(crop_amounts(1));
    4399    end
    44     if bail
    45         break;
     100
     101    % Crop margin from bottom
     102    bcol = A(h,ceil(end/2),:,1);
     103    if ~isfinite(crop_amounts(3))
     104        bail = false;
     105        for b = h:-1:t
     106            for a = 1:c
     107                if ~all(col(A(b,:,a,:)) == bcol(a))
     108                    bail = true;
     109                    break;
     110                end
     111            end
     112            if bail
     113                break;
     114            end
     115        end
     116    else
     117        b = h - abs(crop_amounts(3));
    46118    end
    47 end
    48 bcol = A(1,ceil(end/2),:,1);
    49 bail = false;
    50 for t = 1:h
    51     for a = 1:c
    52         if ~all(col(A(t,:,a,:)) == bcol(a))
    53             bail = true;
    54             break;
     119
     120    if padding == 0  % no padding
     121        % Issue #175: there used to be a 1px minimal padding in case of crop, now removed
     122        %{
     123        if ~isequal([t b l r], [1 h 1 w]) % Check if we're actually croppping
     124            padding = 1; % Leave one boundary pixel to avoid bleeding on resize
     125            bcol(:) = nan;  % make the 1px padding transparent
    55126        end
     127        %}
     128    elseif abs(padding) < 1  % pad value is a relative fraction of image size
     129        padding = sign(padding)*round(mean([b-t r-l])*abs(padding)); % ADJUST PADDING
     130    else  % pad value is in units of 1/72" points
     131        padding = round(padding);  % fix cases of non-integer pad value
    56132    end
    57     if bail
    58         break;
     133
     134    if padding > 0  % extra padding
     135        % Create an empty image, containing the background color, that has the
     136        % cropped image size plus the padded border
     137        B = repmat(bcol,[(b-t)+1+padding*2,(r-l)+1+padding*2,1,n]);  % Fix per Luiz Carvalho
     138        % vA - coordinates in A that contain the cropped image
     139        vA = [t b l r];
     140        % vB - coordinates in B where the cropped version of A will be placed
     141        vB = [padding+1, (b-t)+1+padding, padding+1, (r-l)+1+padding];
     142        % Place the original image in the empty image
     143        B(vB(1):vB(2), vB(3):vB(4), :, :) = A(vA(1):vA(2), vA(3):vA(4), :, :);
     144        A = B;
     145    else  % extra cropping
     146        vA = [t-padding b+padding l-padding r+padding];
     147        A = A(vA(1):vA(2), vA(3):vA(4), :, :);
     148        vB = [NaN NaN NaN NaN];
    59149    end
    60 end
    61 bcol = A(h,ceil(end/2),:,1);
    62 bail = false;
    63 for b = h:-1:t
    64     for a = 1:c
    65         if ~all(col(A(b,:,a,:)) == bcol(a))
    66             bail = true;
    67             break;
    68         end
    69     end
    70     if bail
    71         break;
    72     end
    73 end
    74 % Crop the background, leaving one boundary pixel to avoid bleeding on resize
    75 v = [max(t-padding, 1) min(b+padding, h) max(l-padding, 1) min(r+padding, w)];
    76 A = A(v(1):v(2),v(3):v(4),:,:);
     150
     151    % For EPS cropping, determine the relative BoundingBox - bb_rel
     152    bb_rel = [l-1 h-b-1 r+1 h-t+1]./[w h w h];
    77153end
    78154
    79155function A = col(A)
    80 A = A(:);
     156    A = A(:);
    81157end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/eps2pdf.m

    r19993 r21757  
     1function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
    12%EPS2PDF  Convert an eps file to pdf format using ghostscript
    23%
     
    78%   eps2pdf(source, dest, crop, append, gray)
    89%   eps2pdf(source, dest, crop, append, gray, quality)
     10%   eps2pdf(source, dest, crop, append, gray, quality, gs_options)
    911%
    1012% This function converts an eps file to pdf format. The output can be
     
    1719% system. Ghostscript can be downloaded from: http://www.ghostscript.com
    1820%
    19 %IN:
    20 %   source - filename of the source eps file to convert. The filename is
    21 %            assumed to already have the extension ".eps".
    22 %   dest - filename of the destination pdf file. The filename is assumed to
    23 %          already have the extension ".pdf".
    24 %   crop - boolean indicating whether to crop the borders off the pdf.
    25 %          Default: true.
    26 %   append - boolean indicating whether the eps should be appended to the
    27 %            end of the pdf as a new page (if the pdf exists already).
    28 %            Default: false.
    29 %   gray - boolean indicating whether the output pdf should be grayscale or
    30 %          not. Default: false.
     21% Inputs:
     22%   source  - filename of the source eps file to convert. The filename is
     23%             assumed to already have the extension ".eps".
     24%   dest    - filename of the destination pdf file. The filename is assumed
     25%             to already have the extension ".pdf".
     26%   crop    - boolean indicating whether to crop the borders off the pdf.
     27%             Default: true.
     28%   append  - boolean indicating whether the eps should be appended to the
     29%             end of the pdf as a new page (if the pdf exists already).
     30%             Default: false.
     31%   gray    - boolean indicating whether the output pdf should be grayscale
     32%             or not. Default: false.
    3133%   quality - scalar indicating the level of image bitmap quality to
    3234%             output. A larger value gives a higher quality. quality > 100
    3335%             gives lossless output. Default: ghostscript prepress default.
     36%   gs_options - optional ghostscript options (e.g.: '-dNoOutputFonts'). If
     37%                multiple options are needed, enclose in call array: {'-a','-b'}
    3438
    35 % Copyright (C) Oliver Woodford 2009-2011
     39% Copyright (C) Oliver Woodford 2009-2014, Yair Altman 2015-
    3640
    3741% Suggestion of appending pdf files provided by Matt C at:
     
    4448
    4549% 9/12/2011 Pass font path to ghostscript.
     50% 26/02/15: If temp dir is not writable, use the dest folder for temp
     51%           destination files (Javier Paredes)
     52% 28/02/15: Enable users to specify optional ghostscript options (issue #36)
     53% 01/03/15: Upon GS error, retry without the -sFONTPATH= option (this might solve
     54%           some /findfont errors according to James Rankin, FEX Comment 23/01/15)
     55% 23/06/15: Added extra debug info in case of ghostscript error; code indentation
     56% 04/10/15: Suggest a workaround for issue #41 (missing font path; thanks Mariia Fedotenkova)
     57% 22/02/16: Bug fix from latest release of this file (workaround for issue #41)
     58% 20/03/17: Added informational message in case of GS croak (issue #186)
    4659
    47 function eps2pdf(source, dest, crop, append, gray, quality)
    48 % Intialise the options string for ghostscript
    49 options = ['-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '"'];
    50 % Set crop option
    51 if nargin < 3 || crop
    52     options = [options ' -dEPSCrop'];
    53 end
    54 % Set the font path
    55 fp = font_path();
    56 if ~isempty(fp)
    57     options = [options ' -sFONTPATH="' fp '"'];
    58 end
    59 % Set the grayscale option
    60 if nargin > 4 && gray
    61     options = [options ' -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray'];
    62 end
    63 % Set the bitmap quality
    64 if nargin > 5 && ~isempty(quality)
    65     options = [options ' -dAutoFilterColorImages=false -dAutoFilterGrayImages=false'];
    66     if quality > 100
    67         options = [options ' -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -c ".setpdfwrite << /ColorImageDownsampleThreshold 10 /GrayImageDownsampleThreshold 10 >> setdistillerparams"'];
     60    % Intialise the options string for ghostscript
     61    options = ['-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '"'];
     62    % Set crop option
     63    if nargin < 3 || crop
     64        options = [options ' -dEPSCrop'];
     65    end
     66    % Set the font path
     67    fp = font_path();
     68    if ~isempty(fp)
     69        options = [options ' -sFONTPATH="' fp '"'];
     70    end
     71    % Set the grayscale option
     72    if nargin > 4 && gray
     73        options = [options ' -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray'];
     74    end
     75    % Set the bitmap quality
     76    if nargin > 5 && ~isempty(quality)
     77        options = [options ' -dAutoFilterColorImages=false -dAutoFilterGrayImages=false'];
     78        if quality > 100
     79            options = [options ' -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -c ".setpdfwrite << /ColorImageDownsampleThreshold 10 /GrayImageDownsampleThreshold 10 >> setdistillerparams"'];
     80        else
     81            options = [options ' -dColorImageFilter=/DCTEncode -dGrayImageFilter=/DCTEncode'];
     82            v = 1 + (quality < 80);
     83            quality = 1 - quality / 100;
     84            s = sprintf('<< /QFactor %.2f /Blend 1 /HSample [%d 1 1 %d] /VSample [%d 1 1 %d] >>', quality, v, v, v, v);
     85            options = sprintf('%s -c ".setpdfwrite << /ColorImageDict %s /GrayImageDict %s >> setdistillerparams"', options, s, s);
     86        end
     87    end
     88    % Enable users to specify optional ghostscript options (issue #36)
     89    if nargin > 6 && ~isempty(gs_options)
     90        if iscell(gs_options)
     91            gs_options = sprintf(' %s',gs_options{:});
     92        elseif ~ischar(gs_options)
     93            error('gs_options input argument must be a string or cell-array of strings');
     94        else
     95            gs_options = [' ' gs_options];
     96        end
     97        options = [options gs_options];
     98    end
     99    % Check if the output file exists
     100    if nargin > 3 && append && exist(dest, 'file') == 2
     101        % File exists - append current figure to the end
     102        tmp_nam = tempname;
     103        try
     104            % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
     105            fid = fopen(tmp_nam,'w');
     106            fwrite(fid,1);
     107            fclose(fid);
     108            delete(tmp_nam);
     109        catch
     110            % Temp dir is not writable, so use the dest folder
     111            [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
     112            fpath = fileparts(dest);
     113            tmp_nam = fullfile(fpath,[fname fext]);
     114        end
     115        % Copy the file
     116        copyfile(dest, tmp_nam);
     117        % Add the output file names
     118        options = [options ' -f "' tmp_nam '" "' source '"'];
     119        try
     120            % Convert to pdf using ghostscript
     121            [status, message] = ghostscript(options);
     122        catch me
     123            % Delete the intermediate file
     124            delete(tmp_nam);
     125            rethrow(me);
     126        end
     127        % Delete the intermediate file
     128        delete(tmp_nam);
    68129    else
    69         options = [options ' -dColorImageFilter=/DCTEncode -dGrayImageFilter=/DCTEncode'];
    70         v = 1 + (quality < 80);
    71         quality = 1 - quality / 100;
    72         s = sprintf('<< /QFactor %.2f /Blend 1 /HSample [%d 1 1 %d] /VSample [%d 1 1 %d] >>', quality, v, v, v, v);
    73         options = sprintf('%s -c ".setpdfwrite << /ColorImageDict %s /GrayImageDict %s >> setdistillerparams"', options, s, s);
    74     end
    75 end
    76 % Check if the output file exists
    77 if nargin > 3 && append && exist(dest, 'file') == 2
    78     % File exists - append current figure to the end
    79     tmp_nam = tempname;
    80     % Copy the file
    81     copyfile(dest, tmp_nam);
    82     % Add the output file names
    83     options = [options ' -f "' tmp_nam '" "' source '"'];
    84     try
     130        % File doesn't exist or should be over-written
     131        % Add the output file names
     132        options = [options ' -f "' source '"'];
    85133        % Convert to pdf using ghostscript
    86134        [status, message] = ghostscript(options);
    87     catch me
    88         % Delete the intermediate file
    89         delete(tmp_nam);
    90         rethrow(me);
    91135    end
    92     % Delete the intermediate file
    93     delete(tmp_nam);
    94 else
    95     % File doesn't exist or should be over-written
    96     % Add the output file names
    97     options = [options ' -f "' source '"'];
    98     % Convert to pdf using ghostscript
    99     [status, message] = ghostscript(options);
    100 end
    101 % Check for error
    102 if status
    103     % Report error
    104     if isempty(message)
    105         error('Unable to generate pdf. Check destination directory is writable.');
    106     else
    107         error(message);
     136    % Check for error
     137    if status
     138        % Retry without the -sFONTPATH= option (this might solve some GS
     139        % /findfont errors according to James Rankin, FEX Comment 23/01/15)
     140        orig_options = options;
     141        if ~isempty(fp)
     142            options = regexprep(options, ' -sFONTPATH=[^ ]+ ',' ');
     143            status = ghostscript(options);
     144            if ~status, return; end  % hurray! (no error)
     145        end
     146        % Report error
     147        if isempty(message)
     148            error('Unable to generate pdf. Check destination directory is writable.');
     149        elseif ~isempty(strfind(message,'/typecheck in /findfont'))
     150            % Suggest a workaround for issue #41 (missing font path)
     151            font_name = strtrim(regexprep(message,'.*Operand stack:\s*(.*)\s*Execution.*','$1'));
     152            fprintf(2, 'Ghostscript error: could not find the following font(s): %s\n', font_name);
     153            fpath = fileparts(mfilename('fullpath'));
     154            gs_fonts_file = fullfile(fpath, '.ignore', 'gs_font_path.txt');
     155            fprintf(2, '  try to add the font''s folder to your %s file\n\n', gs_fonts_file);
     156            error('export_fig error');
     157        else
     158            fprintf(2, '\nGhostscript error: perhaps %s is open by another application\n', dest);
     159            if ~isempty(gs_options)
     160                fprintf(2, '  or maybe the%s option(s) are not accepted by your GS version\n', gs_options);
     161            end
     162            fprintf(2, '  or maybe you have another gs executable in your system''s path\n');
     163            fprintf(2, 'Ghostscript options: %s\n\n', orig_options);
     164            error(message);
     165        end
    108166    end
    109 end
    110167end
    111168
    112169% Function to return (and create, where necessary) the font path
    113170function fp = font_path()
    114 fp = user_string('gs_font_path');
    115 if ~isempty(fp)
    116     return
     171    fp = user_string('gs_font_path');
     172    if ~isempty(fp)
     173        return
     174    end
     175    % Create the path
     176    % Start with the default path
     177    fp = getenv('GS_FONTPATH');
     178    % Add on the typical directories for a given OS
     179    if ispc
     180        if ~isempty(fp)
     181            fp = [fp ';'];
     182        end
     183        fp = [fp getenv('WINDIR') filesep 'Fonts'];
     184    else
     185        if ~isempty(fp)
     186            fp = [fp ':'];
     187        end
     188        fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
     189    end
     190    user_string('gs_font_path', fp);
    117191end
    118 % Create the path
    119 % Start with the default path
    120 fp = getenv('GS_FONTPATH');
    121 % Add on the typical directories for a given OS
    122 if ispc
    123     if ~isempty(fp)
    124         fp = [fp ';'];
    125     end
    126     fp = [fp getenv('WINDIR') filesep 'Fonts'];
    127 else
    128     if ~isempty(fp)
    129         fp = [fp ':'];
    130     end
    131     fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
    132 end
    133 user_string('gs_font_path', fp);
    134 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/export_fig.m

    r19993 r21757  
    1 %EXPORT_FIG  Exports figures suitable for publication
     1function [imageData, alpha] = export_fig(varargin)
     2%EXPORT_FIG  Exports figures in a publication-quality format
    23%
    34% Examples:
    4 %   im = export_fig
    5 %   [im alpha] = export_fig
     5%   imageData = export_fig
     6%   [imageData, alpha] = export_fig
    67%   export_fig filename
    78%   export_fig filename -format1 -format2
    89%   export_fig ... -nocrop
     10%   export_fig ... -c[<val>,<val>,<val>,<val>]
    911%   export_fig ... -transparent
    1012%   export_fig ... -native
     
    1416%   export_fig ... -q<val>
    1517%   export_fig ... -p<val>
     18%   export_fig ... -d<gs_option>
     19%   export_fig ... -depsc
    1620%   export_fig ... -<renderer>
    1721%   export_fig ... -<colorspace>
    1822%   export_fig ... -append
    1923%   export_fig ... -bookmark
     24%   export_fig ... -clipboard
     25%   export_fig ... -update
     26%   export_fig ... -nofontswap
     27%   export_fig ... -linecaps
    2028%   export_fig(..., handle)
    2129%
    2230% This function saves a figure or single axes to one or more vector and/or
    23 % bitmap file formats, and/or outputs a rasterized version to the
    24 % workspace, with the following properties:
     31% bitmap file formats, and/or outputs a rasterized version to the workspace,
     32% with the following properties:
    2533%   - Figure/axes reproduced as it appears on screen
    2634%   - Cropped borders (optional)
     
    2937%   - Anti-aliased graphics (bitmap formats)
    3038%   - Render images at native resolution (optional for bitmap formats)
    31 %   - Transparent background supported (pdf, eps, png)
    32 %   - Semi-transparent patch objects supported (png only)
     39%   - Transparent background supported (pdf, eps, png, tif)
     40%   - Semi-transparent patch objects supported (png & tif only)
    3341%   - RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
    3442%   - Variable image compression, including lossless (pdf, eps, jpg)
    3543%   - Optionally append to file (pdf, tiff)
    3644%   - Vector formats: pdf, eps
    37 %   - Bitmap formats: png, tiff, jpg, bmp, export to workspace
    38 %   
     45%   - Bitmap formats: png, tiff, jpg, bmp, export to workspace
     46%   - Rounded line-caps (optional; pdf & eps only)
     47%
    3948% This function is especially suited to exporting figures for use in
    4049% publications and presentations, because of the high quality and
     
    4655% objects), use the -transparent option or set the figure 'Color' property
    4756% to 'none'. To make axes transparent set the axes 'Color' property to
    48 % 'none'. Pdf, eps and png are the only file formats to support a
    49 % transparent background, whilst the png format alone supports transparency
    50 % of patch objects.
     57% 'none'. PDF, EPS, TIF & PNG are the only formats that support a transparent
     58% background; only TIF & PNG formats support transparency of patch objects.
    5159%
    5260% The choice of renderer (opengl, zbuffer or painters) has a large impact
    53 % on the quality of output. Whilst the default value (opengl for bitmaps,
    54 % painters for vector formats) generally gives good results, if you aren't
    55 % satisfied then try another renderer.  Notes: 1) For vector formats (eps,
    56 % pdf), only painters generates vector graphics. 2) For bitmaps, only
     61% on the quality of output. The default value (opengl for bitmaps, painters
     62% for vector formats) generally gives good results, but if you aren't
     63% satisfied then try another renderer.  Notes: 1) For vector formats (EPS,
     64% PDF), only painters generates vector graphics. 2) For bitmaps, only
    5765% opengl can render transparent patch objects correctly. 3) For bitmaps,
    5866% only painters will correctly scale line dash and dot lengths when
     
    6068% using painters.
    6169%
    62 % When exporting to vector format (pdf & eps) and bitmap format using the
     70% When exporting to vector format (PDF & EPS) and bitmap format using the
    6371% painters renderer, this function requires that ghostscript is installed
    6472% on your system. You can download this from:
     
    6876%   http://www.foolabs.com/xpdf
    6977%
    70 %IN:
     78% Inputs:
    7179%   filename - string containing the name (optionally including full or
    7280%              relative path) of the file the figure is to be saved as. If
     
    8391%   -nocrop - option indicating that the borders of the output are not to
    8492%             be cropped.
     93%   -c[<val>,<val>,<val>,<val>] - option indicating crop amounts. Must be
     94%             a 4-element vector of numeric values: [top,right,bottom,left]
     95%             where NaN/Inf indicate auto-cropping, 0 means no cropping,
     96%             and any other value mean cropping in pixel amounts.
    8597%   -transparent - option indicating that the figure background is to be
    86 %                  made transparent (png, pdf and eps output only).
     98%                  made transparent (png, pdf, tif and eps output only).
    8799%   -m<val> - option where val indicates the factor to magnify the
    88100%             on-screen figure pixel dimensions by when generating bitmap
    89 %             outputs. Default: '-m1'.
     101%             outputs (does not affect vector formats). Default: '-m1'.
    90102%   -r<val> - option val indicates the resolution (in pixels per inch) to
    91103%             export bitmap and vector outputs at, keeping the dimensions
     
    106118%                        use for bitmap outputs. '-a1' means no anti-
    107119%                        aliasing; '-a4' is the maximum amount (default).
    108 %   -<renderer> - option to force a particular renderer (painters, opengl
    109 %                 or zbuffer) to be used over the default: opengl for
    110 %                 bitmaps; painters for vector formats.
     120%   -<renderer> - option to force a particular renderer (painters, opengl or
     121%                 zbuffer). Default value: opengl for bitmap formats or
     122%                 figures with patches and/or transparent annotations;
     123%                 painters for vector formats without patches/transparencies.
    111124%   -<colorspace> - option indicating which colorspace color figures should
    112125%                   be saved in: RGB (default), CMYK or gray. CMYK is only
     
    119132%             sometimes give a smaller file size than the default lossy
    120133%             compression, depending on the type of images.
    121 %   -p<val> - option to add a border of width val to eps and pdf files,
    122 %             where val is in units of the intermediate eps file. Default:
    123 %             0 (i.e. no padding).
     134%   -p<val> - option to pad a border of width val to exported files, where
     135%             val is either a relative size with respect to cropped image
     136%             size (i.e. p=0.01 adds a 1% border). For EPS & PDF formats,
     137%             val can also be integer in units of 1/72" points (abs(val)>1).
     138%             val can be positive (padding) or negative (extra cropping).
     139%             If used, the -nocrop flag will be ignored, i.e. the image will
     140%             always be cropped and then padded. Default: 0 (i.e. no padding).
    124141%   -append - option indicating that if the file (pdfs only) already
    125142%             exists, the figure is to be appended as a new page, instead
     
    127144%   -bookmark - option to indicate that a bookmark with the name of the
    128145%               figure is to be created in the output file (pdf only).
    129 %   handle - The handle of the figure, axes or uipanels (can be an array of
    130 %            handles, but the objects must be in the same figure) to be
    131 %            saved. Default: gcf.
     146%   -clipboard - option to save output as an image on the system clipboard.
     147%                Note: background transparency is not preserved in clipboard
     148%   -d<gs_option> - option to indicate a ghostscript setting. For example,
     149%                   -dMaxBitmap=0 or -dNoOutputFonts (Ghostscript 9.15+).
     150%   -depsc -  option to use EPS level-3 rather than the default level-2 print
     151%             device. This solves some bugs with Matlab's default -depsc2 device
     152%             such as discolored subplot lines on images (vector formats only).
     153%   -update - option to download and install the latest version of export_fig
     154%   -nofontswap - option to avoid font swapping. Font swapping is automatically
     155%             done in vector formats (only): 11 standard Matlab fonts are
     156%             replaced by the original figure fonts. This option prevents this.
     157%   -linecaps - option to create rounded line-caps (vector formats only).
     158%   handle -  The handle of the figure, axes or uipanels (can be an array of
     159%             handles, but the objects must be in the same figure) to be
     160%             saved. Default: gcf.
    132161%
    133 %OUT:
    134 %   im - MxNxC uint8 image array of the figure.
    135 %   alpha - MxN single array of alphamatte values in range [0,1], for the
    136 %           case when the background is transparent.
     162% Outputs:
     163%   imageData - MxNxC uint8 image array of the exported image.
     164%   alpha     - MxN single array of alphamatte values in the range [0,1],
     165%               for the case when the background is transparent.
    137166%
    138167%   Some helpful examples and tips can be found at:
    139 %      https://github.com/ojwoodford/export_fig
     168%      https://github.com/altmany/export_fig
    140169%
    141 %   See also PRINT, SAVEAS.
    142 
    143 % Copyright (C) Oliver Woodford 2008-2014
     170%   See also PRINT, SAVEAS, ScreenCapture (on the Matlab File Exchange)
     171
     172%{
     173% Copyright (C) Oliver Woodford 2008-2014, Yair Altman 2015-
    144174
    145175% The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG
     
    162192% Thanks to Tammy Threadgill for reporting a bug where an axes is not
    163193% isolated from gui objects.
    164 
     194%}
     195%{
    165196% 23/02/12: Ensure that axes limits don't change during printing
    166 % 14/03/12: Fix bug in fixing the axes limits (thanks to Tobias Lamour for
    167 %           reporting it).
    168 % 02/05/12: Incorporate patch of Petr Nechaev (many thanks), enabling
    169 %           bookmarking of figures in pdf files.
    170 % 09/05/12: Incorporate patch of Arcelia Arrieta (many thanks), to keep
    171 %           tick marks fixed.
    172 % 12/12/12: Add support for isolating uipanels. Thanks to michael for
    173 %           suggesting it.
    174 % 25/09/13: Add support for changing resolution in vector formats. Thanks
    175 %           to Jan Jaap Meijer for suggesting it.
    176 % 07/05/14: Add support for '~' at start of path. Thanks to Sally Warner
    177 %           for suggesting it.
    178 
    179 function [im, alpha] = export_fig(varargin)
    180 % Make sure the figure is rendered correctly _now_ so that properties like
    181 % axes limits are up-to-date.
    182 drawnow;
    183 % Parse the input arguments
    184 [fig, options] = parse_args(nargout, varargin{:});
    185 % Isolate the subplot, if it is one
    186 cls = all(ismember(get(fig, 'Type'), {'axes', 'uipanel'}));
    187 if cls
    188     % Given handles of one or more axes, so isolate them from the rest
    189     fig = isolate_axes(fig);
    190 else
    191     % Check we have a figure
    192     if ~isequal(get(fig, 'Type'), 'figure');
    193         error('Handle must be that of a figure, axes or uipanel');
    194     end
    195     % Get the old InvertHardcopy mode
    196     old_mode = get(fig, 'InvertHardcopy');
     197% 14/03/12: Fix bug in fixing the axes limits (thanks to Tobias Lamour for reporting it).
     198% 02/05/12: Incorporate patch of Petr Nechaev (many thanks), enabling bookmarking of figures in pdf files.
     199% 09/05/12: Incorporate patch of Arcelia Arrieta (many thanks), to keep tick marks fixed.
     200% 12/12/12: Add support for isolating uipanels. Thanks to michael for suggesting it.
     201% 25/09/13: Add support for changing resolution in vector formats. Thanks to Jan Jaap Meijer for suggesting it.
     202% 07/05/14: Add support for '~' at start of path. Thanks to Sally Warner for suggesting it.
     203% 24/02/15: Fix Matlab R2014b bug (issue #34): plot markers are not displayed when ZLimMode='manual'
     204% 25/02/15: Fix issue #4 (using HG2 on R2014a and earlier)
     205% 25/02/15: Fix issue #21 (bold TeX axes labels/titles in R2014b)
     206% 26/02/15: If temp dir is not writable, use the user-specified folder for temporary EPS/PDF files (Javier Paredes)
     207% 27/02/15: Modified repository URL from github.com/ojwoodford to /altmany
     208%           Indented main function
     209%           Added top-level try-catch block to display useful workarounds
     210% 28/02/15: Enable users to specify optional ghostscript options (issue #36)
     211% 06/03/15: Improved image padding & cropping thanks to Oscar Hartogensis
     212% 26/03/15: Fixed issue #49 (bug with transparent grayscale images); fixed out-of-memory issue
     213% 26/03/15: Fixed issue #42: non-normalized annotations on HG1
     214% 26/03/15: Fixed issue #46: Ghostscript crash if figure units <> pixels
     215% 27/03/15: Fixed issue #39: bad export of transparent annotations/patches
     216% 28/03/15: Fixed issue #50: error on some Matlab versions with the fix for issue #42
     217% 29/03/15: Fixed issue #33: bugs in Matlab's print() function with -cmyk
     218% 29/03/15: Improved processing of input args (accept space between param name & value, related to issue #51)
     219% 30/03/15: When exporting *.fig files, then saveas *.fig if figure is open, otherwise export the specified fig file
     220% 30/03/15: Fixed edge case bug introduced yesterday (commit #ae1755bd2e11dc4e99b95a7681f6e211b3fa9358)
     221% 09/04/15: Consolidated header comment sections; initialize output vars only if requested (nargout>0)
     222% 14/04/15: Workaround for issue #45: lines in image subplots are exported in invalid color
     223% 15/04/15: Fixed edge-case in parsing input parameters; fixed help section to show the -depsc option (issue #45)
     224% 21/04/15: Bug fix: Ghostscript croaks on % chars in output PDF file (reported by Sven on FEX page, 15-Jul-2014)
     225% 22/04/15: Bug fix: Pdftops croaks on relative paths (reported by Tintin Milou on FEX page, 19-Jan-2015)
     226% 04/05/15: Merged fix #63 (Kevin Mattheus Moerman): prevent tick-label changes during export
     227% 07/05/15: Partial fix for issue #65: PDF export used painters rather than opengl renderer (thanks Nguyenr)
     228% 08/05/15: Fixed issue #65: bad PDF append since commit #e9f3cdf 21/04/15 (thanks Robert Nguyen)
     229% 12/05/15: Fixed issue #67: exponent labels cropped in export, since fix #63 (04/05/15)
     230% 28/05/15: Fixed issue #69: set non-bold label font only if the string contains symbols (\beta etc.), followup to issue #21
     231% 29/05/15: Added informative error message in case user requested SVG output (issue #72)
     232% 09/06/15: Fixed issue #58: -transparent removed anti-aliasing when exporting to PNG
     233% 19/06/15: Added -update option to download and install the latest version of export_fig
     234% 07/07/15: Added -nofontswap option to avoid font-swapping in EPS/PDF
     235% 16/07/15: Fixed problem with anti-aliasing on old Matlab releases
     236% 11/09/15: Fixed issue #103: magnification must never become negative; also fixed reported error msg in parsing input params
     237% 26/09/15: Alert if trying to export transparent patches/areas to non-PNG outputs (issue #108)
     238% 04/10/15: Do not suggest workarounds for certain errors that have already been handled previously
     239% 01/11/15: Fixed issue #112: use same renderer in print2eps as export_fig (thanks to Jesús Pestana Puerta)
     240% 10/11/15: Custom GS installation webpage for MacOS. Thanks to Andy Hueni via FEX
     241% 19/11/15: Fixed clipboard export in R2015b (thanks to Dan K via FEX)
     242% 21/02/16: Added -c option for indicating specific crop amounts (idea by Cedric Noordam on FEX)
     243% 08/05/16: Added message about possible error reason when groot.Units~=pixels (issue #149)
     244% 17/05/16: Fixed case of image YData containing more than 2 elements (issue #151)
     245% 08/08/16: Enabled exporting transparency to TIF, in addition to PNG/PDF (issue #168)
     246% 11/12/16: Added alert in case of error creating output PDF/EPS file (issue #179)
     247% 13/12/16: Minor fix to the commit for issue #179 from 2 days ago
     248% 22/03/17: Fixed issue #187: only set manual ticks when no exponent is present
     249% 09/04/17: Added -linecaps option (idea by Baron Finer, issue #192)
     250%}
     251
     252    if nargout
     253        [imageData, alpha] = deal([]);
     254    end
     255    hadError = false;
     256    displaySuggestedWorkarounds = true;
     257
     258    % Ensure the figure is rendered correctly _now_ so that properties like axes limits are up-to-date
     259    drawnow;
     260    pause(0.05);  % this solves timing issues with Java Swing's EDT (http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem)
     261
     262    % Parse the input arguments
     263    fig = get(0, 'CurrentFigure');
     264    [fig, options] = parse_args(nargout, fig, varargin{:});
     265
     266    % Ensure that we have a figure handle
     267    if isequal(fig,-1)
     268        return;  % silent bail-out
     269    elseif isempty(fig)
     270        error('No figure found');
     271    end
     272
     273    % Isolate the subplot, if it is one
     274    cls = all(ismember(get(fig, 'Type'), {'axes', 'uipanel'}));
     275    if cls
     276        % Given handles of one or more axes, so isolate them from the rest
     277        fig = isolate_axes(fig);
     278    else
     279        % Check we have a figure
     280        if ~isequal(get(fig, 'Type'), 'figure')
     281            error('Handle must be that of a figure, axes or uipanel');
     282        end
     283        % Get the old InvertHardcopy mode
     284        old_mode = get(fig, 'InvertHardcopy');
     285    end
     286
     287    % Hack the font units where necessary (due to a font rendering bug in print?).
     288    % This may not work perfectly in all cases.
     289    % Also it can change the figure layout if reverted, so use a copy.
     290    magnify = options.magnify * options.aa_factor;
     291    if isbitmap(options) && magnify ~= 1
     292        fontu = findall(fig, 'FontUnits', 'normalized');
     293        if ~isempty(fontu)
     294            % Some normalized font units found
     295            if ~cls
     296                fig = copyfig(fig);
     297                set(fig, 'Visible', 'off');
     298                fontu = findall(fig, 'FontUnits', 'normalized');
     299                cls = true;
     300            end
     301            set(fontu, 'FontUnits', 'points');
     302        end
     303    end
     304
     305    try
     306        % MATLAB "feature": axes limits and tick marks can change when printing
     307        Hlims = findall(fig, 'Type', 'axes');
     308        if ~cls
     309            % Record the old axes limit and tick modes
     310            Xlims = make_cell(get(Hlims, 'XLimMode'));
     311            Ylims = make_cell(get(Hlims, 'YLimMode'));
     312            Zlims = make_cell(get(Hlims, 'ZLimMode'));
     313            Xtick = make_cell(get(Hlims, 'XTickMode'));
     314            Ytick = make_cell(get(Hlims, 'YTickMode'));
     315            Ztick = make_cell(get(Hlims, 'ZTickMode'));
     316            Xlabel = make_cell(get(Hlims, 'XTickLabelMode'));
     317            Ylabel = make_cell(get(Hlims, 'YTickLabelMode'));
     318            Zlabel = make_cell(get(Hlims, 'ZTickLabelMode'));
     319        end
     320
     321        % Set all axes limit and tick modes to manual, so the limits and ticks can't change
     322        % Fix Matlab R2014b bug (issue #34): plot markers are not displayed when ZLimMode='manual'
     323        set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual');
     324        set_tick_mode(Hlims, 'X');
     325        set_tick_mode(Hlims, 'Y');
     326        if ~using_hg2(fig)
     327            set(Hlims,'ZLimMode', 'manual');
     328            set_tick_mode(Hlims, 'Z');
     329        end
     330    catch
     331        % ignore - fix issue #4 (using HG2 on R2014a and earlier)
     332    end
     333
     334    % Fix issue #21 (bold TeX axes labels/titles in R2014b when exporting to EPS/PDF)
     335    try
     336        if using_hg2(fig) && isvector(options)
     337            % Set the FontWeight of axes labels/titles to 'normal'
     338            % Fix issue #69: set non-bold font only if the string contains symbols (\beta etc.)
     339            texLabels = findall(fig, 'type','text', 'FontWeight','bold');
     340            symbolIdx = ~cellfun('isempty',strfind({texLabels.String},'\'));
     341            set(texLabels(symbolIdx), 'FontWeight','normal');
     342        end
     343    catch
     344        % ignore
     345    end
     346
     347    % Fix issue #42: non-normalized annotations on HG1 (internal Matlab bug)
     348    annotationHandles = [];
     349    try
     350        if ~using_hg2(fig)
     351            annotationHandles = findall(fig,'Type','hggroup','-and','-property','Units','-and','-not','Units','norm');
     352            try  % suggested by Jesús Pestana Puerta (jespestana) 30/9/2015
     353                originalUnits = get(annotationHandles,'Units');
     354                set(annotationHandles,'Units','norm');
     355            catch
     356            end
     357        end
     358    catch
     359        % should never happen, but ignore in any case - issue #50
     360    end
     361
     362    % Fix issue #46: Ghostscript crash if figure units <> pixels
     363    oldFigUnits = get(fig,'Units');
     364    set(fig,'Units','pixels');
     365
     366    % Set to print exactly what is there
     367    set(fig, 'InvertHardcopy', 'off');
     368    % Set the renderer
     369    switch options.renderer
     370        case 1
     371            renderer = '-opengl';
     372        case 2
     373            renderer = '-zbuffer';
     374        case 3
     375            renderer = '-painters';
     376        otherwise
     377            renderer = '-opengl'; % Default for bitmaps
     378    end
     379
     380    % Handle transparent patches
     381    hasTransparency = ~isempty(findall(fig,'-property','FaceAlpha','-and','-not','FaceAlpha',1));
     382    hasPatches      = ~isempty(findall(fig,'type','patch'));
     383    if hasTransparency
     384        % Alert if trying to export transparent patches/areas to non-supported outputs (issue #108)
     385        % http://www.mathworks.com/matlabcentral/answers/265265-can-export_fig-or-else-draw-vector-graphics-with-transparent-surfaces
     386        % TODO - use transparency when exporting to PDF by not passing via print2eps
     387        msg = 'export_fig currently supports transparent patches/areas only in PNG output. ';
     388        if options.pdf
     389            warning('export_fig:transparency', '%s\nTo export transparent patches/areas to PDF, use the print command:\n print(gcf, ''-dpdf'', ''%s.pdf'');', msg, options.name);
     390        elseif ~options.png && ~options.tif  % issue #168
     391            warning('export_fig:transparency', '%s\nTo export the transparency correctly, try using the ScreenCapture utility on the Matlab File Exchange: http://bit.ly/1QFrBip', msg);
     392        end
     393    end
     394
     395    try
     396        % Do the bitmap formats first
     397        if isbitmap(options)
     398            if abs(options.bb_padding) > 1
     399                displaySuggestedWorkarounds = false;
     400                error('For bitmap output (png,jpg,tif,bmp) the padding value (-p) must be between -1<p<1')
     401            end
     402            % Get the background colour
     403            if options.transparent && (options.png || options.alpha)
     404                % Get out an alpha channel
     405                % MATLAB "feature": black colorbar axes can change to white and vice versa!
     406                hCB = findall(fig, 'Type','axes', 'Tag','Colorbar');
     407                if isempty(hCB)
     408                    yCol = [];
     409                    xCol = [];
     410                else
     411                    yCol = get(hCB, 'YColor');
     412                    xCol = get(hCB, 'XColor');
     413                    if iscell(yCol)
     414                        yCol = cell2mat(yCol);
     415                        xCol = cell2mat(xCol);
     416                    end
     417                    yCol = sum(yCol, 2);
     418                    xCol = sum(xCol, 2);
     419                end
     420                % MATLAB "feature": apparently figure size can change when changing
     421                % colour in -nodisplay mode
     422                pos = get(fig, 'Position');
     423                % Set the background colour to black, and set size in case it was
     424                % changed internally
     425                tcol = get(fig, 'Color');
     426                set(fig, 'Color', 'k', 'Position', pos);
     427                % Correct the colorbar axes colours
     428                set(hCB(yCol==0), 'YColor', [0 0 0]);
     429                set(hCB(xCol==0), 'XColor', [0 0 0]);
     430
     431                % The following code might cause out-of-memory errors
     432                try
     433                    % Print large version to array
     434                    B = print2array(fig, magnify, renderer);
     435                    % Downscale the image
     436                    B = downsize(single(B), options.aa_factor);
     437                catch
     438                    % This is more conservative in memory, but kills transparency (issue #58)
     439                    B = single(print2array(fig, magnify/options.aa_factor, renderer));
     440                end
     441
     442                % Set background to white (and set size)
     443                set(fig, 'Color', 'w', 'Position', pos);
     444                % Correct the colorbar axes colours
     445                set(hCB(yCol==3), 'YColor', [1 1 1]);
     446                set(hCB(xCol==3), 'XColor', [1 1 1]);
     447
     448                % The following code might cause out-of-memory errors
     449                try
     450                    % Print large version to array
     451                    A = print2array(fig, magnify, renderer);
     452                    % Downscale the image
     453                    A = downsize(single(A), options.aa_factor);
     454                catch
     455                    % This is more conservative in memory, but kills transparency (issue #58)
     456                    A = single(print2array(fig, magnify/options.aa_factor, renderer));
     457                end
     458
     459                % Set the background colour (and size) back to normal
     460                set(fig, 'Color', tcol, 'Position', pos);
     461                % Compute the alpha map
     462                alpha = round(sum(B - A, 3)) / (255 * 3) + 1;
     463                A = alpha;
     464                A(A==0) = 1;
     465                A = B ./ A(:,:,[1 1 1]);
     466                clear B
     467                % Convert to greyscale
     468                if options.colourspace == 2
     469                    A = rgb2grey(A);
     470                end
     471                A = uint8(A);
     472                % Crop the background
     473                if options.crop
     474                    %[alpha, v] = crop_borders(alpha, 0, 1, options.crop_amounts);
     475                    %A = A(v(1):v(2),v(3):v(4),:);
     476                    [alpha, vA, vB] = crop_borders(alpha, 0, options.bb_padding, options.crop_amounts);
     477                    if ~any(isnan(vB)) % positive padding
     478                        B = repmat(uint8(zeros(1,1,size(A,3))),size(alpha));
     479                        B(vB(1):vB(2), vB(3):vB(4), :) = A(vA(1):vA(2), vA(3):vA(4), :); % ADDED BY OH
     480                        A = B;
     481                    else  % negative padding
     482                        A = A(vA(1):vA(2), vA(3):vA(4), :);
     483                    end
     484                end
     485                if options.png
     486                    % Compute the resolution
     487                    res = options.magnify * get(0, 'ScreenPixelsPerInch') / 25.4e-3;
     488                    % Save the png
     489                    imwrite(A, [options.name '.png'], 'Alpha', double(alpha), 'ResolutionUnit', 'meter', 'XResolution', res, 'YResolution', res);
     490                    % Clear the png bit
     491                    options.png = false;
     492                end
     493                % Return only one channel for greyscale
     494                if isbitmap(options)
     495                    A = check_greyscale(A);
     496                end
     497                if options.alpha
     498                    % Store the image
     499                    imageData = A;
     500                    % Clear the alpha bit
     501                    options.alpha = false;
     502                end
     503                % Get the non-alpha image
     504                if isbitmap(options)
     505                    alph = alpha(:,:,ones(1, size(A, 3)));
     506                    A = uint8(single(A) .* alph + 255 * (1 - alph));
     507                    clear alph
     508                end
     509                if options.im
     510                    % Store the new image
     511                    imageData = A;
     512                end
     513            else
     514                % Print large version to array
     515                if options.transparent
     516                    % MATLAB "feature": apparently figure size can change when changing
     517                    % colour in -nodisplay mode
     518                    pos = get(fig, 'Position');
     519                    tcol = get(fig, 'Color');
     520                    set(fig, 'Color', 'w', 'Position', pos);
     521                    A = print2array(fig, magnify, renderer);
     522                    set(fig, 'Color', tcol, 'Position', pos);
     523                    tcol = 255;
     524                else
     525                    [A, tcol] = print2array(fig, magnify, renderer);
     526                end
     527                % Crop the background
     528                if options.crop
     529                    A = crop_borders(A, tcol, options.bb_padding, options.crop_amounts);
     530                end
     531                % Downscale the image
     532                A = downsize(A, options.aa_factor);
     533                if options.colourspace == 2
     534                    % Convert to greyscale
     535                    A = rgb2grey(A);
     536                else
     537                    % Return only one channel for greyscale
     538                    A = check_greyscale(A);
     539                end
     540                % Outputs
     541                if options.im
     542                    imageData = A;
     543                end
     544                if options.alpha
     545                    imageData = A;
     546                    alpha = zeros(size(A, 1), size(A, 2), 'single');
     547                end
     548            end
     549            % Save the images
     550            if options.png
     551                res = options.magnify * get(0, 'ScreenPixelsPerInch') / 25.4e-3;
     552                imwrite(A, [options.name '.png'], 'ResolutionUnit', 'meter', 'XResolution', res, 'YResolution', res);
     553            end
     554            if options.bmp
     555                imwrite(A, [options.name '.bmp']);
     556            end
     557            % Save jpeg with given quality
     558            if options.jpg
     559                quality = options.quality;
     560                if isempty(quality)
     561                    quality = 95;
     562                end
     563                if quality > 100
     564                    imwrite(A, [options.name '.jpg'], 'Mode', 'lossless');
     565                else
     566                    imwrite(A, [options.name '.jpg'], 'Quality', quality);
     567                end
     568            end
     569            % Save tif images in cmyk if wanted (and possible)
     570            if options.tif
     571                if options.colourspace == 1 && size(A, 3) == 3
     572                    A = double(255 - A);
     573                    K = min(A, [], 3);
     574                    K_ = 255 ./ max(255 - K, 1);
     575                    C = (A(:,:,1) - K) .* K_;
     576                    M = (A(:,:,2) - K) .* K_;
     577                    Y = (A(:,:,3) - K) .* K_;
     578                    A = uint8(cat(3, C, M, Y, K));
     579                    clear C M Y K K_
     580                end
     581                append_mode = {'overwrite', 'append'};
     582                imwrite(A, [options.name '.tif'], 'Resolution', options.magnify*get(0, 'ScreenPixelsPerInch'), 'WriteMode', append_mode{options.append+1});
     583            end
     584        end
     585
     586        % Now do the vector formats
     587        if isvector(options)
     588            % Set the default renderer to painters
     589            if ~options.renderer
     590                if hasTransparency || hasPatches
     591                    % This is *MUCH* slower, but more accurate for patches and transparent annotations (issue #39)
     592                    renderer = '-painters'; %ISSM fix
     593                else
     594                    renderer = '-painters';
     595                end
     596            end
     597            options.rendererStr = renderer;  % fix for issue #112
     598            % Generate some filenames
     599            tmp_nam = [tempname '.eps'];
     600            try
     601                % Ensure that the temp dir is writable (Javier Paredes 30/1/15)
     602                fid = fopen(tmp_nam,'w');
     603                fwrite(fid,1);
     604                fclose(fid);
     605                delete(tmp_nam);
     606                isTempDirOk = true;
     607            catch
     608                % Temp dir is not writable, so use the user-specified folder
     609                [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
     610                fpath = fileparts(options.name);
     611                tmp_nam = fullfile(fpath,[fname fext]);
     612                isTempDirOk = false;
     613            end
     614            if isTempDirOk
     615                pdf_nam_tmp = [tempname '.pdf'];
     616            else
     617                pdf_nam_tmp = fullfile(fpath,[fname '.pdf']);
     618            end
     619            if options.pdf
     620                pdf_nam = [options.name '.pdf'];
     621                try copyfile(pdf_nam, pdf_nam_tmp, 'f'); catch, end  % fix for issue #65
     622            else
     623                pdf_nam = pdf_nam_tmp;
     624            end
     625            % Generate the options for print
     626            p2eArgs = {renderer, sprintf('-r%d', options.resolution)};
     627            if options.colourspace == 1  % CMYK
     628                % Issue #33: due to internal bugs in Matlab's print() function, we can't use its -cmyk option
     629                %p2eArgs{end+1} = '-cmyk';
     630            end
     631            if ~options.crop
     632                % Issue #56: due to internal bugs in Matlab's print() function, we can't use its internal cropping mechanism,
     633                % therefore we always use '-loose' (in print2eps.m) and do our own cropping (in crop_borders)
     634                %p2eArgs{end+1} = '-loose';
     635            end
     636            if any(strcmpi(varargin,'-depsc'))
     637                % Issue #45: lines in image subplots are exported in invalid color.
     638                % The workaround is to use the -depsc parameter instead of the default -depsc2
     639                p2eArgs{end+1} = '-depsc';
     640            end
     641            try
     642                % Generate an eps
     643                print2eps(tmp_nam, fig, options, p2eArgs{:});
     644                % Remove the background, if desired
     645                if options.transparent && ~isequal(get(fig, 'Color'), 'none')
     646                    eps_remove_background(tmp_nam, 1 + using_hg2(fig));
     647                end
     648                % Fix colorspace to CMYK, if requested (workaround for issue #33)
     649                if options.colourspace == 1  % CMYK
     650                    % Issue #33: due to internal bugs in Matlab's print() function, we can't use its -cmyk option
     651                    change_rgb_to_cmyk(tmp_nam);
     652                end
     653                % Add a bookmark to the PDF if desired
     654                if options.bookmark
     655                    fig_nam = get(fig, 'Name');
     656                    if isempty(fig_nam)
     657                        warning('export_fig:EmptyBookmark', 'Bookmark requested for figure with no name. Bookmark will be empty.');
     658                    end
     659                    add_bookmark(tmp_nam, fig_nam);
     660                end
     661                % Generate a pdf
     662                eps2pdf(tmp_nam, pdf_nam_tmp, 1, options.append, options.colourspace==2, options.quality, options.gs_options);
     663                % Ghostscript croaks on % chars in the output PDF file, so use tempname and then rename the file
     664                try
     665                    % Rename the file (except if it is already the same)
     666                    % Abbie K's comment on the commit for issue #179 (#commitcomment-20173476)
     667                    if ~isequal(pdf_nam_tmp, pdf_nam)
     668                        movefile(pdf_nam_tmp, pdf_nam, 'f');
     669                    end
     670                catch
     671                    % Alert in case of error creating output PDF/EPS file (issue #179)
     672                    if exist(pdf_nam_tmp, 'file')
     673                        error(['Could not create ' pdf_nam ' - perhaps the folder does not exist, or you do not have write permissions']);
     674                    else
     675                        error('Could not generate the intermediary EPS file.');
     676                    end
     677                end
     678            catch ex
     679                % Delete the eps
     680                delete(tmp_nam);
     681                rethrow(ex);
     682            end
     683            % Delete the eps
     684            delete(tmp_nam);
     685            if options.eps || options.linecaps
     686                try
     687                    % Generate an eps from the pdf
     688                    % since pdftops can't handle relative paths (e.g., '..\'), use a temp file
     689                    eps_nam_tmp = strrep(pdf_nam_tmp,'.pdf','.eps');
     690                    pdf2eps(pdf_nam, eps_nam_tmp);
     691
     692                    % Issue #192: enable rounded line-caps
     693                    if options.linecaps
     694                        fstrm = read_write_entire_textfile(eps_nam_tmp);
     695                        fstrm = regexprep(fstrm, '[02] J', '1 J');
     696                        read_write_entire_textfile(eps_nam_tmp, fstrm);
     697                        if options.pdf
     698                            eps2pdf(eps_nam_tmp, pdf_nam, 1, options.append, options.colourspace==2, options.quality, options.gs_options);
     699                        end
     700                    end
     701
     702                    if options.eps
     703                        movefile(eps_nam_tmp, [options.name '.eps'], 'f');
     704                    else  % if options.pdf
     705                        try delete(eps_nam_tmp); catch, end
     706                    end
     707                catch ex
     708                    if ~options.pdf
     709                        % Delete the pdf
     710                        delete(pdf_nam);
     711                    end
     712                    try delete(eps_nam_tmp); catch, end
     713                    rethrow(ex);
     714                end
     715                if ~options.pdf
     716                    % Delete the pdf
     717                    delete(pdf_nam);
     718                end
     719            end
     720        end
     721
     722        % Revert the figure or close it (if requested)
     723        if cls || options.closeFig
     724            % Close the created figure
     725            close(fig);
     726        else
     727            % Reset the hardcopy mode
     728            set(fig, 'InvertHardcopy', old_mode);
     729            % Reset the axes limit and tick modes
     730            for a = 1:numel(Hlims)
     731                try
     732                    set(Hlims(a), 'XLimMode', Xlims{a}, 'YLimMode', Ylims{a}, 'ZLimMode', Zlims{a},...
     733                                  'XTickMode', Xtick{a}, 'YTickMode', Ytick{a}, 'ZTickMode', Ztick{a},...
     734                                  'XTickLabelMode', Xlabel{a}, 'YTickLabelMode', Ylabel{a}, 'ZTickLabelMode', Zlabel{a});
     735                catch
     736                    % ignore - fix issue #4 (using HG2 on R2014a and earlier)
     737                end
     738            end
     739            % Revert the tex-labels font weights
     740            try set(texLabels, 'FontWeight','bold'); catch, end
     741            % Revert annotation units
     742            for handleIdx = 1 : numel(annotationHandles)
     743                try
     744                    oldUnits = originalUnits{handleIdx};
     745                catch
     746                    oldUnits = originalUnits;
     747                end
     748                try set(annotationHandles(handleIdx),'Units',oldUnits); catch, end
     749            end
     750            % Revert figure units
     751            set(fig,'Units',oldFigUnits);
     752        end
     753
     754        % Output to clipboard (if requested)
     755        if options.clipboard
     756            % Delete the output file if unchanged from the default name ('export_fig_out.png')
     757            if strcmpi(options.name,'export_fig_out')
     758                try
     759                    fileInfo = dir('export_fig_out.png');
     760                    if ~isempty(fileInfo)
     761                        timediff = now - fileInfo.datenum;
     762                        ONE_SEC = 1/24/60/60;
     763                        if timediff < ONE_SEC
     764                            delete('export_fig_out.png');
     765                        end
     766                    end
     767                catch
     768                    % never mind...
     769                end
     770            end
     771
     772            % Save the image in the system clipboard
     773            % credit: Jiro Doke's IMCLIPBOARD: http://www.mathworks.com/matlabcentral/fileexchange/28708-imclipboard
     774            try
     775                error(javachk('awt', 'export_fig -clipboard output'));
     776            catch
     777                warning('export_fig -clipboard output failed: requires Java to work');
     778                return;
     779            end
     780            try
     781                % Import necessary Java classes
     782                import java.awt.Toolkit
     783                import java.awt.image.BufferedImage
     784                import java.awt.datatransfer.DataFlavor
     785
     786                % Get System Clipboard object (java.awt.Toolkit)
     787                cb = Toolkit.getDefaultToolkit.getSystemClipboard();
     788
     789                % Add java class (ImageSelection) to the path
     790                if ~exist('ImageSelection', 'class')
     791                    javaaddpath(fileparts(which(mfilename)), '-end');
     792                end
     793
     794                % Get image size
     795                ht = size(imageData, 1);
     796                wd = size(imageData, 2);
     797
     798                % Convert to Blue-Green-Red format
     799                try
     800                    imageData2 = imageData(:, :, [3 2 1]);
     801                catch
     802                    % Probably gray-scaled image (2D, without the 3rd [RGB] dimension)
     803                    imageData2 = imageData(:, :, [1 1 1]);
     804                end
     805
     806                % Convert to 3xWxH format
     807                imageData2 = permute(imageData2, [3, 2, 1]);
     808
     809                % Append Alpha data (unused - transparency is not supported in clipboard copy)
     810                alphaData2 = uint8(permute(255*alpha,[3,2,1])); %=255*ones(1,wd,ht,'uint8')
     811                imageData2 = cat(1, imageData2, alphaData2);
     812
     813                % Create image buffer
     814                imBuffer = BufferedImage(wd, ht, BufferedImage.TYPE_INT_RGB);
     815                imBuffer.setRGB(0, 0, wd, ht, typecast(imageData2(:), 'int32'), 0, wd);
     816
     817                % Create ImageSelection object from the image buffer
     818                imSelection = ImageSelection(imBuffer);
     819
     820                % Set clipboard content to the image
     821                cb.setContents(imSelection, []);
     822            catch
     823                warning('export_fig -clipboard output failed: %s', lasterr); %#ok<LERR>
     824            end
     825        end
     826
     827        % Don't output the data to console unless requested
     828        if ~nargout
     829            clear imageData alpha
     830        end
     831    catch err
     832        % Display possible workarounds before the error message
     833        if displaySuggestedWorkarounds && ~strcmpi(err.message,'export_fig error')
     834            if ~hadError,  fprintf(2, 'export_fig error. ');  end
     835            fprintf(2, 'Please ensure:\n');
     836            fprintf(2, '  that you are using the <a href="https://github.com/altmany/export_fig/archive/master.zip">latest version</a> of export_fig\n');
     837            if ismac
     838                fprintf(2, '  and that you have <a href="http://pages.uoregon.edu/koch">Ghostscript</a> installed\n');
     839            else
     840                fprintf(2, '  and that you have <a href="http://www.ghostscript.com">Ghostscript</a> installed\n');
     841            end
     842            try
     843                if options.eps
     844                    fprintf(2, '  and that you have <a href="http://www.foolabs.com/xpdf">pdftops</a> installed\n');
     845                end
     846            catch
     847                % ignore - probably an error in parse_args
     848            end
     849            fprintf(2, '  and that you do not have <a href="matlab:which export_fig -all">multiple versions</a> of export_fig installed by mistake\n');
     850            fprintf(2, '  and that you did not made a mistake in the <a href="matlab:help export_fig">expected input arguments</a>\n');
     851            try
     852                % Alert per issue #149
     853                if ~strncmpi(get(0,'Units'),'pixel',5)
     854                    fprintf(2, '  or try to set groot''s Units property back to its default value of ''pixels'' (<a href="matlab:web(''https://github.com/altmany/export_fig/issues/149'',''-browser'');">details</a>)\n');
     855                end
     856            catch
     857                % ignore - maybe an old MAtlab release
     858            end
     859            fprintf(2, '\nIf the problem persists, then please <a href="https://github.com/altmany/export_fig/issues">report a new issue</a>.\n\n');
     860        end
     861        rethrow(err)
     862    end
    197863end
    198 % Hack the font units where necessary (due to a font rendering bug in
    199 % print?). This may not work perfectly in all cases. Also it can change the
    200 % figure layout if reverted, so use a copy.
    201 magnify = options.magnify * options.aa_factor;
    202 if isbitmap(options) && magnify ~= 1
    203     fontu = findobj(fig, 'FontUnits', 'normalized');
    204     if ~isempty(fontu)
    205         % Some normalized font units found
    206         if ~cls
    207             fig = copyfig(fig);
    208             set(fig, 'Visible', 'off');
    209             fontu = findobj(fig, 'FontUnits', 'normalized');
    210             cls = true;
    211         end
    212         set(fontu, 'FontUnits', 'points');
    213     end
     864
     865function options = default_options()
     866    % Default options used by export_fig
     867    options = struct(...
     868        'name',         'export_fig_out', ...
     869        'crop',         true, ...
     870        'crop_amounts', nan(1,4), ...  % auto-crop all 4 image sides
     871        'transparent',  false, ...
     872        'renderer',     0, ...         % 0: default, 1: OpenGL, 2: ZBuffer, 3: Painters
     873        'pdf',          false, ...
     874        'eps',          false, ...
     875        'png',          false, ...
     876        'tif',          false, ...
     877        'jpg',          false, ...
     878        'bmp',          false, ...
     879        'clipboard',    false, ...
     880        'colourspace',  0, ...         % 0: RGB/gray, 1: CMYK, 2: gray
     881        'append',       false, ...
     882        'im',           false, ...
     883        'alpha',        false, ...
     884        'aa_factor',    0, ...
     885        'bb_padding',   0, ...
     886        'magnify',      [], ...
     887        'resolution',   [], ...
     888        'bookmark',     false, ...
     889        'closeFig',     false, ...
     890        'quality',      [], ...
     891        'update',       false, ...
     892        'fontswap',     true, ...
     893        'linecaps',     false, ...
     894        'gs_options',   {{}});
    214895end
    215 % MATLAB "feature": axes limits and tick marks can change when printing
    216 Hlims = findall(fig, 'Type', 'axes');
    217 if ~cls
    218     % Record the old axes limit and tick modes
    219     Xlims = make_cell(get(Hlims, 'XLimMode'));
    220     Ylims = make_cell(get(Hlims, 'YLimMode'));
    221     Zlims = make_cell(get(Hlims, 'ZLimMode'));
    222     Xtick = make_cell(get(Hlims, 'XTickMode'));
    223     Ytick = make_cell(get(Hlims, 'YTickMode'));
    224     Ztick = make_cell(get(Hlims, 'ZTickMode'));
     896
     897function [fig, options] = parse_args(nout, fig, varargin)
     898    % Parse the input arguments
     899
     900    % Set the defaults
     901    native = false; % Set resolution to native of an image
     902    options = default_options();
     903    options.im =    (nout == 1);  % user requested imageData output
     904    options.alpha = (nout == 2);  % user requested alpha output
     905
     906    % Go through the other arguments
     907    skipNext = false;
     908    for a = 1:nargin-2
     909        if skipNext
     910            skipNext = false;
     911            continue;
     912        end
     913        if all(ishandle(varargin{a}))
     914            fig = varargin{a};
     915        elseif ischar(varargin{a}) && ~isempty(varargin{a})
     916            if varargin{a}(1) == '-'
     917                switch lower(varargin{a}(2:end))
     918                    case 'nocrop'
     919                        options.crop = false;
     920                        options.crop_amounts = [0,0,0,0];
     921                    case {'trans', 'transparent'}
     922                        options.transparent = true;
     923                    case 'opengl'
     924                        options.renderer = 1;
     925                    case 'zbuffer'
     926                        options.renderer = 2;
     927                    case 'painters'
     928                        options.renderer = 3;
     929                    case 'pdf'
     930                        options.pdf = true;
     931                    case 'eps'
     932                        options.eps = true;
     933                    case 'png'
     934                        options.png = true;
     935                    case {'tif', 'tiff'}
     936                        options.tif = true;
     937                    case {'jpg', 'jpeg'}
     938                        options.jpg = true;
     939                    case 'bmp'
     940                        options.bmp = true;
     941                    case 'rgb'
     942                        options.colourspace = 0;
     943                    case 'cmyk'
     944                        options.colourspace = 1;
     945                    case {'gray', 'grey'}
     946                        options.colourspace = 2;
     947                    case {'a1', 'a2', 'a3', 'a4'}
     948                        options.aa_factor = str2double(varargin{a}(3));
     949                    case 'append'
     950                        options.append = true;
     951                    case 'bookmark'
     952                        options.bookmark = true;
     953                    case 'native'
     954                        native = true;
     955                    case 'clipboard'
     956                        options.clipboard = true;
     957                        options.im = true;
     958                        options.alpha = true;
     959                    case 'svg'
     960                        msg = ['SVG output is not supported by export_fig. Use one of the following alternatives:\n' ...
     961                               '  1. saveas(gcf,''filename.svg'')\n' ...
     962                               '  2. plot2svg utility: http://github.com/jschwizer99/plot2svg\n' ...
     963                               '  3. export_fig to EPS/PDF, then convert to SVG using generic (non-Matlab) tools\n'];
     964                        error(sprintf(msg)); %#ok<SPERR>
     965                    case 'update'
     966                        % Download the latest version of export_fig into the export_fig folder
     967                        try
     968                            zipFileName = 'https://github.com/altmany/export_fig/archive/master.zip';
     969                            folderName = fileparts(which(mfilename('fullpath')));
     970                            targetFileName = fullfile(folderName, datestr(now,'yyyy-mm-dd.zip'));
     971                            urlwrite(zipFileName,targetFileName);
     972                        catch
     973                            error('Could not download %s into %s\n',zipFileName,targetFileName);
     974                        end
     975
     976                        % Unzip the downloaded zip file in the export_fig folder
     977                        try
     978                            unzip(targetFileName,folderName);
     979                        catch
     980                            error('Could not unzip %s\n',targetFileName);
     981                        end
     982                    case 'nofontswap'
     983                        options.fontswap = false;
     984                    case 'linecaps'
     985                        options.linecaps = true;
     986                    otherwise
     987                        try
     988                            wasError = false;
     989                            if strcmpi(varargin{a}(1:2),'-d')
     990                                varargin{a}(2) = 'd';  % ensure lowercase 'd'
     991                                options.gs_options{end+1} = varargin{a};
     992                            elseif strcmpi(varargin{a}(1:2),'-c')
     993                                if numel(varargin{a})==2
     994                                    skipNext = true;
     995                                    vals = str2num(varargin{a+1}); %#ok<ST2NM>
     996                                else
     997                                    vals = str2num(varargin{a}(3:end)); %#ok<ST2NM>
     998                                end
     999                                if numel(vals)~=4
     1000                                    wasError = true;
     1001                                    error('option -c cannot be parsed: must be a 4-element numeric vector');
     1002                                end
     1003                                options.crop_amounts = vals;
     1004                                options.crop = true;
     1005                            else  % scalar parameter value
     1006                                val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q|p|P))-?\d*.?\d+', 'match'));
     1007                                if isempty(val) || isnan(val)
     1008                                    % Issue #51: improved processing of input args (accept space between param name & value)
     1009                                    val = str2double(varargin{a+1});
     1010                                    if isscalar(val) && ~isnan(val)
     1011                                        skipNext = true;
     1012                                    end
     1013                                end
     1014                                if ~isscalar(val) || isnan(val)
     1015                                    wasError = true;
     1016                                    error('option %s is not recognised or cannot be parsed', varargin{a});
     1017                                end
     1018                                switch lower(varargin{a}(2))
     1019                                    case 'm'
     1020                                        % Magnification may never be negative
     1021                                        if val <= 0
     1022                                            wasError = true;
     1023                                            error('Bad magnification value: %g (must be positive)', val);
     1024                                        end
     1025                                        options.magnify = val;
     1026                                    case 'r'
     1027                                        options.resolution = val;
     1028                                    case 'q'
     1029                                        options.quality = max(val, 0);
     1030                                    case 'p'
     1031                                        options.bb_padding = val;
     1032                                end
     1033                            end
     1034                        catch err
     1035                            % We might have reached here by raising an intentional error
     1036                            if wasError  % intentional raise
     1037                                rethrow(err)
     1038                            else  % unintentional
     1039                                error(['Unrecognized export_fig input option: ''' varargin{a} '''']);
     1040                            end
     1041                        end
     1042                end
     1043            else
     1044                [p, options.name, ext] = fileparts(varargin{a});
     1045                if ~isempty(p)
     1046                    options.name = [p filesep options.name];
     1047                end
     1048                switch lower(ext)
     1049                    case {'.tif', '.tiff'}
     1050                        options.tif = true;
     1051                    case {'.jpg', '.jpeg'}
     1052                        options.jpg = true;
     1053                    case '.png'
     1054                        options.png = true;
     1055                    case '.bmp'
     1056                        options.bmp = true;
     1057                    case '.eps'
     1058                        options.eps = true;
     1059                    case '.pdf'
     1060                        options.pdf = true;
     1061                    case '.fig'
     1062                        % If no open figure, then load the specified .fig file and continue
     1063                        if isempty(fig)
     1064                            fig = openfig(varargin{a},'invisible');
     1065                            varargin{a} = fig;
     1066                            options.closeFig = true;
     1067                        else
     1068                            % save the current figure as the specified .fig file and exit
     1069                            saveas(fig(1),varargin{a});
     1070                            fig = -1;
     1071                            return
     1072                        end
     1073                    case '.svg'
     1074                        msg = ['SVG output is not supported by export_fig. Use one of the following alternatives:\n' ...
     1075                               '  1. saveas(gcf,''filename.svg'')\n' ...
     1076                               '  2. plot2svg utility: http://github.com/jschwizer99/plot2svg\n' ...
     1077                               '  3. export_fig to EPS/PDF, then convert to SVG using generic (non-Matlab) tools\n'];
     1078                        error(sprintf(msg)); %#ok<SPERR>
     1079                    otherwise
     1080                        options.name = varargin{a};
     1081                end
     1082            end
     1083        end
     1084    end
     1085
     1086    % Quick bail-out if no figure found
     1087    if isempty(fig),  return;  end
     1088
     1089    % Do border padding with repsect to a cropped image
     1090    if options.bb_padding
     1091        options.crop = true;
     1092    end
     1093
     1094    % Set default anti-aliasing now we know the renderer
     1095    if options.aa_factor == 0
     1096        try isAA = strcmp(get(ancestor(fig, 'figure'), 'GraphicsSmoothing'), 'on'); catch, isAA = false; end
     1097        options.aa_factor = 1 + 2 * (~(using_hg2(fig) && isAA) | (options.renderer == 3));
     1098    end
     1099
     1100    % Convert user dir '~' to full path
     1101    if numel(options.name) > 2 && options.name(1) == '~' && (options.name(2) == '/' || options.name(2) == '\')
     1102        options.name = fullfile(char(java.lang.System.getProperty('user.home')), options.name(2:end));
     1103    end
     1104
     1105    % Compute the magnification and resolution
     1106    if isempty(options.magnify)
     1107        if isempty(options.resolution)
     1108            options.magnify = 1;
     1109            options.resolution = 864;
     1110        else
     1111            options.magnify = options.resolution ./ get(0, 'ScreenPixelsPerInch');
     1112        end
     1113    elseif isempty(options.resolution)
     1114        options.resolution = 864;
     1115    end
     1116
     1117    % Set the default format
     1118    if ~isvector(options) && ~isbitmap(options)
     1119        options.png = true;
     1120    end
     1121
     1122    % Check whether transparent background is wanted (old way)
     1123    if isequal(get(ancestor(fig(1), 'figure'), 'Color'), 'none')
     1124        options.transparent = true;
     1125    end
     1126
     1127    % If requested, set the resolution to the native vertical resolution of the
     1128    % first suitable image found
     1129    if native && isbitmap(options)
     1130        % Find a suitable image
     1131        list = findall(fig, 'Type','image', 'Tag','export_fig_native');
     1132        if isempty(list)
     1133            list = findall(fig, 'Type','image', 'Visible','on');
     1134        end
     1135        for hIm = list(:)'
     1136            % Check height is >= 2
     1137            height = size(get(hIm, 'CData'), 1);
     1138            if height < 2
     1139                continue
     1140            end
     1141            % Account for the image filling only part of the axes, or vice versa
     1142            yl = get(hIm, 'YData');
     1143            if isscalar(yl)
     1144                yl = [yl(1)-0.5 yl(1)+height+0.5];
     1145            else
     1146                yl = [min(yl), max(yl)];  % fix issue #151 (case of yl containing more than 2 elements)
     1147                if ~diff(yl)
     1148                    continue
     1149                end
     1150                yl = yl + [-0.5 0.5] * (diff(yl) / (height - 1));
     1151            end
     1152            hAx = get(hIm, 'Parent');
     1153            yl2 = get(hAx, 'YLim');
     1154            % Find the pixel height of the axes
     1155            oldUnits = get(hAx, 'Units');
     1156            set(hAx, 'Units', 'pixels');
     1157            pos = get(hAx, 'Position');
     1158            set(hAx, 'Units', oldUnits);
     1159            if ~pos(4)
     1160                continue
     1161            end
     1162            % Found a suitable image
     1163            % Account for stretch-to-fill being disabled
     1164            pbar = get(hAx, 'PlotBoxAspectRatio');
     1165            pos = min(pos(4), pbar(2)*pos(3)/pbar(1));
     1166            % Set the magnification to give native resolution
     1167            options.magnify = abs((height * diff(yl2)) / (pos * diff(yl)));  % magnification must never be negative: issue #103
     1168            break
     1169        end
     1170    end
    2251171end
    226 % Set all axes limit and tick modes to manual, so the limits and ticks can't change
    227 set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual');
    228 set_tick_mode(Hlims, 'X');
    229 set_tick_mode(Hlims, 'Y');
    230 set_tick_mode(Hlims, 'Z');
    231 % Set to print exactly what is there
    232 set(fig, 'InvertHardcopy', 'off');
    233 % Set the renderer
    234 switch options.renderer
    235     case 1
    236         renderer = '-opengl';
    237     case 2
    238         renderer = '-zbuffer';
    239     case 3
    240         renderer = '-painters';
    241     otherwise
    242         renderer = '-opengl'; % Default for bitmaps
     1172
     1173function A = downsize(A, factor)
     1174    % Downsample an image
     1175    if factor == 1
     1176        % Nothing to do
     1177        return
     1178    end
     1179    try
     1180        % Faster, but requires image processing toolbox
     1181        A = imresize(A, 1/factor, 'bilinear');
     1182    catch
     1183        % No image processing toolbox - resize manually
     1184        % Lowpass filter - use Gaussian as is separable, so faster
     1185        % Compute the 1d Gaussian filter
     1186        filt = (-factor-1:factor+1) / (factor * 0.6);
     1187        filt = exp(-filt .* filt);
     1188        % Normalize the filter
     1189        filt = single(filt / sum(filt));
     1190        % Filter the image
     1191        padding = floor(numel(filt) / 2);
     1192        for a = 1:size(A, 3)
     1193            A(:,:,a) = conv2(filt, filt', single(A([ones(1, padding) 1:end repmat(end, 1, padding)],[ones(1, padding) 1:end repmat(end, 1, padding)],a)), 'valid');
     1194        end
     1195        % Subsample
     1196        A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:);
     1197    end
    2431198end
    244 % Do the bitmap formats first
    245 if isbitmap(options)
    246     % Get the background colour
    247     if options.transparent && (options.png || options.alpha)
    248         % Get out an alpha channel
    249         % MATLAB "feature": black colorbar axes can change to white and vice versa!
    250         hCB = findobj(fig, 'Type', 'axes', 'Tag', 'Colorbar');
    251         if isempty(hCB)
    252             yCol = [];
    253             xCol = [];
    254         else
    255             yCol = get(hCB, 'YColor');
    256             xCol = get(hCB, 'XColor');
    257             if iscell(yCol)
    258                 yCol = cell2mat(yCol);
    259                 xCol = cell2mat(xCol);
    260             end
    261             yCol = sum(yCol, 2);
    262             xCol = sum(xCol, 2);
    263         end
    264         % MATLAB "feature": apparently figure size can change when changing
    265         % colour in -nodisplay mode
    266         pos = get(fig, 'Position');
    267         % Set the background colour to black, and set size in case it was
    268         % changed internally
    269         tcol = get(fig, 'Color');
    270         set(fig, 'Color', 'k', 'Position', pos);
    271         % Correct the colorbar axes colours
    272         set(hCB(yCol==0), 'YColor', [0 0 0]);
    273         set(hCB(xCol==0), 'XColor', [0 0 0]);
    274         % Print large version to array
    275         B = print2array(fig, magnify, renderer);
    276         % Downscale the image
    277         B = downsize(single(B), options.aa_factor);
    278         % Set background to white (and set size)
    279         set(fig, 'Color', 'w', 'Position', pos);
    280         % Correct the colorbar axes colours
    281         set(hCB(yCol==3), 'YColor', [1 1 1]);
    282         set(hCB(xCol==3), 'XColor', [1 1 1]);
    283         % Print large version to array
    284         A = print2array(fig, magnify, renderer);
    285         % Downscale the image
    286         A = downsize(single(A), options.aa_factor);
    287         % Set the background colour (and size) back to normal
    288         set(fig, 'Color', tcol, 'Position', pos);
    289         % Compute the alpha map
    290         alpha = round(sum(B - A, 3)) / (255 * 3) + 1;
    291         A = alpha;
    292         A(A==0) = 1;
    293         A = B ./ A(:,:,[1 1 1]);
    294         clear B
    295         % Convert to greyscale
    296         if options.colourspace == 2
    297             A = rgb2grey(A);
    298         end
    299         A = uint8(A);
    300         % Crop the background
    301         if options.crop
    302             [alpha, v] = crop_borders(alpha, 0, 1);
    303             A = A(v(1):v(2),v(3):v(4),:);
    304         end
    305         if options.png
    306             % Compute the resolution
    307             res = options.magnify * get(0, 'ScreenPixelsPerInch') / 25.4e-3;
    308             % Save the png
    309             imwrite(A, [options.name '.png'], 'Alpha', double(alpha), 'ResolutionUnit', 'meter', 'XResolution', res, 'YResolution', res);
    310             % Clear the png bit
    311             options.png = false;
    312         end
    313         % Return only one channel for greyscale
    314         if isbitmap(options)
    315             A = check_greyscale(A);
    316         end
    317         if options.alpha
    318             % Store the image
    319             im = A;
    320             % Clear the alpha bit
    321             options.alpha = false;
    322         end
    323         % Get the non-alpha image
    324         if isbitmap(options)
    325             alph = alpha(:,:,ones(1, size(A, 3)));
    326             A = uint8(single(A) .* alph + 255 * (1 - alph));
    327             clear alph
    328         end
    329         if options.im
    330             % Store the new image
    331             im = A;
    332         end
    333     else
    334         % Print large version to array
    335         if options.transparent
    336             % MATLAB "feature": apparently figure size can change when changing
    337             % colour in -nodisplay mode
    338             pos = get(fig, 'Position');
    339             tcol = get(fig, 'Color');
    340             set(fig, 'Color', 'w', 'Position', pos);
    341             A = print2array(fig, magnify, renderer);
    342             set(fig, 'Color', tcol, 'Position', pos);
    343             tcol = 255;
    344         else
    345             [A, tcol] = print2array(fig, magnify, renderer);
    346         end
    347         % Crop the background
    348         if options.crop
    349             A = crop_borders(A, tcol, 1);
    350         end
    351         % Downscale the image
    352         A = downsize(A, options.aa_factor);
    353         if options.colourspace == 2
    354             % Convert to greyscale
    355             A = rgb2grey(A);
    356         else
    357             % Return only one channel for greyscale
    358             A = check_greyscale(A);
    359         end
    360         % Outputs
    361         if options.im
    362             im = A;
    363         end
    364         if options.alpha
    365             im = A;
    366             alpha = zeros(size(A, 1), size(A, 2), 'single');
    367         end
    368     end
    369     % Save the images
    370     if options.png
    371         res = options.magnify * get(0, 'ScreenPixelsPerInch') / 25.4e-3;
    372         imwrite(A, [options.name '.png'], 'ResolutionUnit', 'meter', 'XResolution', res, 'YResolution', res);
    373     end
    374     if options.bmp
    375         imwrite(A, [options.name '.bmp']);
    376     end
    377     % Save jpeg with given quality
    378     if options.jpg
    379         quality = options.quality;
    380         if isempty(quality)
    381             quality = 95;
    382         end
    383         if quality > 100
    384             imwrite(A, [options.name '.jpg'], 'Mode', 'lossless');
    385         else
    386             imwrite(A, [options.name '.jpg'], 'Quality', quality);
    387         end
    388     end
    389     % Save tif images in cmyk if wanted (and possible)
    390     if options.tif
    391         if options.colourspace == 1 && size(A, 3) == 3
    392             A = double(255 - A);
    393             K = min(A, [], 3);
    394             K_ = 255 ./ max(255 - K, 1);
    395             C = (A(:,:,1) - K) .* K_;
    396             M = (A(:,:,2) - K) .* K_;
    397             Y = (A(:,:,3) - K) .* K_;
    398             A = uint8(cat(3, C, M, Y, K));
    399             clear C M Y K K_
    400         end
    401         append_mode = {'overwrite', 'append'};
    402         imwrite(A, [options.name '.tif'], 'Resolution', options.magnify*get(0, 'ScreenPixelsPerInch'), 'WriteMode', append_mode{options.append+1});
    403     end
     1199
     1200function A = rgb2grey(A)
     1201    A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A)); % #ok<ZEROLIKE>
    4041202end
    405 % Now do the vector formats
    406 if isvector(options)
    407     % Set the default renderer to painters
    408     if ~options.renderer
    409         renderer = '-painters';
    410     end
    411     % Generate some filenames
    412     tmp_nam = [tempname '.eps'];
    413     if options.pdf
    414         pdf_nam = [options.name '.pdf'];
    415     else
    416         pdf_nam = [tempname '.pdf'];
    417     end
    418     % Generate the options for print
    419     p2eArgs = {renderer, sprintf('-r%d', options.resolution)};
    420     if options.colourspace == 1
    421         p2eArgs = [p2eArgs {'-cmyk'}];
    422     end
    423     if ~options.crop
    424         p2eArgs = [p2eArgs {'-loose'}];
    425     end
    426     try
    427         % Generate an eps
    428         print2eps(tmp_nam, fig, options.bb_padding, p2eArgs{:});
    429         % Remove the background, if desired
    430         if options.transparent && ~isequal(get(fig, 'Color'), 'none')
    431             eps_remove_background(tmp_nam, 1 + using_hg2(fig));
    432         end
    433         % Add a bookmark to the PDF if desired
    434         if options.bookmark
    435             fig_nam = get(fig, 'Name');
    436             if isempty(fig_nam)
    437                 warning('export_fig:EmptyBookmark', 'Bookmark requested for figure with no name. Bookmark will be empty.');
    438             end
    439             add_bookmark(tmp_nam, fig_nam);
    440         end
    441         % Generate a pdf
    442         eps2pdf(tmp_nam, pdf_nam, 1, options.append, options.colourspace==2, options.quality);
    443     catch ex
    444         % Delete the eps
    445         delete(tmp_nam);
    446         rethrow(ex);
    447     end
    448     % Delete the eps
    449     delete(tmp_nam);
    450     if options.eps
    451         try
    452             % Generate an eps from the pdf
    453             pdf2eps(pdf_nam, [options.name '.eps']);
    454         catch ex
    455             if ~options.pdf
    456                 % Delete the pdf
    457                 delete(pdf_nam);
    458             end
    459             rethrow(ex);
    460         end
    461         if ~options.pdf
    462             % Delete the pdf
    463             delete(pdf_nam);
    464         end
     1203
     1204function A = check_greyscale(A)
     1205    % Check if the image is greyscale
     1206    if size(A, 3) == 3 && ...
     1207            all(reshape(A(:,:,1) == A(:,:,2), [], 1)) && ...
     1208            all(reshape(A(:,:,2) == A(:,:,3), [], 1))
     1209        A = A(:,:,1); % Save only one channel for 8-bit output
    4651210    end
    4661211end
    467 if cls
    468     % Close the created figure
    469     close(fig);
    470 else
    471     % Reset the hardcopy mode
    472     set(fig, 'InvertHardcopy', old_mode);
    473     % Reset the axes limit and tick modes
    474     for a = 1:numel(Hlims)
    475         set(Hlims(a), 'XLimMode', Xlims{a}, 'YLimMode', Ylims{a}, 'ZLimMode', Zlims{a}, 'XTickMode', Xtick{a}, 'YTickMode', Ytick{a}, 'ZTickMode', Ztick{a});
    476     end
     1212
     1213function eps_remove_background(fname, count)
     1214    % Remove the background of an eps file
     1215    % Open the file
     1216    fh = fopen(fname, 'r+');
     1217    if fh == -1
     1218        error('Not able to open file %s.', fname);
     1219    end
     1220    % Read the file line by line
     1221    while count
     1222        % Get the next line
     1223        l = fgets(fh);
     1224        if isequal(l, -1)
     1225            break; % Quit, no rectangle found
     1226        end
     1227        % Check if the line contains the background rectangle
     1228        if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +r[fe] *[\n\r]+', 'start'), 1)
     1229            % Set the line to whitespace and quit
     1230            l(1:regexp(l, '[\n\r]', 'start', 'once')-1) = ' ';
     1231            fseek(fh, -numel(l), 0);
     1232            fprintf(fh, l);
     1233            % Reduce the count
     1234            count = count - 1;
     1235        end
     1236    end
     1237    % Close the file
     1238    fclose(fh);
    4771239end
     1240
     1241function b = isvector(options)
     1242    b = options.pdf || options.eps;
    4781243end
    4791244
    480 function [fig, options] = parse_args(nout, varargin)
    481 % Parse the input arguments
    482 % Set the defaults
    483 fig = get(0, 'CurrentFigure');
    484 options = struct('name', 'export_fig_out', ...
    485                  'crop', true, ...
    486                  'transparent', false, ...
    487                  'renderer', 0, ... % 0: default, 1: OpenGL, 2: ZBuffer, 3: Painters
    488                  'pdf', false, ...
    489                  'eps', false, ...
    490                  'png', false, ...
    491                  'tif', false, ...
    492                  'jpg', false, ...
    493                  'bmp', false, ...
    494                  'colourspace', 0, ... % 0: RGB/gray, 1: CMYK, 2: gray
    495                  'append', false, ...
    496                  'im', nout == 1, ...
    497                  'alpha', nout == 2, ...
    498                  'aa_factor', 0, ...
    499                  'bb_padding', 0, ...
    500                  'magnify', [], ...
    501                  'resolution', [], ...
    502                  'bookmark', false, ...
    503                  'quality', []);
    504 native = false; % Set resolution to native of an image
    505 
    506 % Go through the other arguments
    507 for a = 1:nargin-1
    508     if all(ishandle(varargin{a}))
    509         fig = varargin{a};
    510     elseif ischar(varargin{a}) && ~isempty(varargin{a})
    511         if varargin{a}(1) == '-'
    512             switch lower(varargin{a}(2:end))
    513                 case 'nocrop'
    514                     options.crop = false;
    515                 case {'trans', 'transparent'}
    516                     options.transparent = true;
    517                 case 'opengl'
    518                     options.renderer = 1;
    519                 case 'zbuffer'
    520                     options.renderer = 2;
    521                 case 'painters'
    522                     options.renderer = 3;
    523                 case 'pdf'
    524                     options.pdf = true;
    525                 case 'eps'
    526                     options.eps = true;
    527                 case 'png'
    528                     options.png = true;
    529                 case {'tif', 'tiff'}
    530                     options.tif = true;
    531                 case {'jpg', 'jpeg'}
    532                     options.jpg = true;
    533                 case 'bmp'
    534                     options.bmp = true;
    535                 case 'rgb'
    536                     options.colourspace = 0;
    537                 case 'cmyk'
    538                     options.colourspace = 1;
    539                 case {'gray', 'grey'}
    540                     options.colourspace = 2;
    541                 case {'a1', 'a2', 'a3', 'a4'}
    542                     options.aa_factor = str2double(varargin{a}(3));
    543                 case 'append'
    544                     options.append = true;
    545                 case 'bookmark'
    546                     options.bookmark = true;
    547                 case 'native'
    548                     native = true;
    549                 otherwise
    550                     val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q|p|P))-?\d*.?\d+', 'match'));
    551                     if ~isscalar(val)
    552                         error('option %s not recognised', varargin{a});
    553                     end
    554                     switch lower(varargin{a}(2))
    555                         case 'm'
    556                             options.magnify = val;
    557                         case 'r'
    558                             options.resolution = val;
    559                         case 'q'
    560                             options.quality = max(val, 0);
    561                         case 'p'
    562                             options.bb_padding = val;
    563                     end
    564             end
    565         else
    566             [p, options.name, ext] = fileparts(varargin{a});
    567             if ~isempty(p)
    568                 options.name = [p filesep options.name];
    569             end
    570             switch lower(ext)
    571                 case {'.tif', '.tiff'}
    572                     options.tif = true;
    573                 case {'.jpg', '.jpeg'}
    574                     options.jpg = true;
    575                 case '.png'
    576                     options.png = true;
    577                 case '.bmp'
    578                     options.bmp = true;
    579                 case '.eps'
    580                     options.eps = true;
    581                 case '.pdf'
    582                     options.pdf = true;
    583                 otherwise
    584                     options.name = varargin{a};
    585             end
    586         end
    587     end
    588 end
    589 
    590 % Set default anti-aliasing now we know the renderer
    591 if options.aa_factor == 0
    592     options.aa_factor = 1 + 2 * (~(using_hg2(fig) && strcmp(get(ancestor(fig, 'figure'), 'GraphicsSmoothing'), 'on')) | (options.renderer == 3));
    593 end
    594 
    595 % Convert user dir '~' to full path
    596 if numel(options.name) > 2 && options.name(1) == '~' && (options.name(2) == '/' || options.name(2) == '\')
    597     options.name = fullfile(char(java.lang.System.getProperty('user.home')), options.name(2:end));
    598 end
    599 
    600 % Compute the magnification and resolution
    601 if isempty(options.magnify)
    602     if isempty(options.resolution)
    603         options.magnify = 1;
    604         options.resolution = 864;
    605     else
    606         options.magnify = options.resolution ./ get(0, 'ScreenPixelsPerInch');
    607     end
    608 elseif isempty(options.resolution)
    609     options.resolution = 864;
    610 end 
    611 
    612 % Check we have a figure handle
    613 if isempty(fig)
    614     error('No figure found');
    615 end
    616 
    617 % Set the default format
    618 if ~isvector(options) && ~isbitmap(options)
    619     options.png = true;
    620 end
    621 
    622 % Check whether transparent background is wanted (old way)
    623 if isequal(get(ancestor(fig(1), 'figure'), 'Color'), 'none')
    624     options.transparent = true;
    625 end
    626 
    627 % If requested, set the resolution to the native vertical resolution of the
    628 % first suitable image found
    629 if native && isbitmap(options)
    630     % Find a suitable image
    631     list = findobj(fig, 'Type', 'image', 'Tag', 'export_fig_native');
    632     if isempty(list)
    633         list = findobj(fig, 'Type', 'image', 'Visible', 'on');
    634     end
    635     for hIm = list(:)'
    636         % Check height is >= 2
    637         height = size(get(hIm, 'CData'), 1);
    638         if height < 2
    639             continue
    640         end
    641         % Account for the image filling only part of the axes, or vice
    642         % versa
    643         yl = get(hIm, 'YData');
    644         if isscalar(yl)
    645             yl = [yl(1)-0.5 yl(1)+height+0.5];
    646         else
    647             if ~diff(yl)
    648                 continue
    649             end
    650             yl = yl + [-0.5 0.5] * (diff(yl) / (height - 1));
    651         end
    652         hAx = get(hIm, 'Parent');
    653         yl2 = get(hAx, 'YLim');
    654         % Find the pixel height of the axes
    655         oldUnits = get(hAx, 'Units');
    656         set(hAx, 'Units', 'pixels');
    657         pos = get(hAx, 'Position');
    658         set(hAx, 'Units', oldUnits);
    659         if ~pos(4)
    660             continue
    661         end
    662         % Found a suitable image
    663         % Account for stretch-to-fill being disabled
    664         pbar = get(hAx, 'PlotBoxAspectRatio');
    665         pos = min(pos(4), pbar(2)*pos(3)/pbar(1));
    666         % Set the magnification to give native resolution
    667         options.magnify = (height * diff(yl2)) / (pos * diff(yl));
    668         break
    669     end
    670 end
    671 end
    672 
    673 function A = downsize(A, factor)
    674 % Downsample an image
    675 if factor == 1
    676     % Nothing to do
    677     return
    678 end
    679 try
    680     % Faster, but requires image processing toolbox
    681     A = imresize(A, 1/factor, 'bilinear');
    682 catch
    683     % No image processing toolbox - resize manually
    684     % Lowpass filter - use Gaussian as is separable, so faster
    685     % Compute the 1d Gaussian filter
    686     filt = (-factor-1:factor+1) / (factor * 0.6);
    687     filt = exp(-filt .* filt);
    688     % Normalize the filter
    689     filt = single(filt / sum(filt));
    690     % Filter the image
    691     padding = floor(numel(filt) / 2);
    692     for a = 1:size(A, 3)
    693         A(:,:,a) = conv2(filt, filt', single(A([ones(1, padding) 1:end repmat(end, 1, padding)],[ones(1, padding) 1:end repmat(end, 1, padding)],a)), 'valid');
    694     end
    695     % Subsample
    696     A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:);
    697 end
    698 end
    699 
    700 function A = rgb2grey(A)
    701 A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A));
    702 end
    703 
    704 function A = check_greyscale(A)
    705 % Check if the image is greyscale
    706 if size(A, 3) == 3 && ...
    707         all(reshape(A(:,:,1) == A(:,:,2), [], 1)) && ...
    708         all(reshape(A(:,:,2) == A(:,:,3), [], 1))
    709     A = A(:,:,1); % Save only one channel for 8-bit output
    710 end
    711 end
    712 
    713 function eps_remove_background(fname, count)
    714 % Remove the background of an eps file
    715 % Open the file
    716 fh = fopen(fname, 'r+');
    717 if fh == -1
    718     error('Not able to open file %s.', fname);
    719 end
    720 % Read the file line by line
    721 while count
    722     % Get the next line
    723     l = fgets(fh);
    724     if isequal(l, -1)
    725         break; % Quit, no rectangle found
    726     end
    727     % Check if the line contains the background rectangle
    728     if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +r[fe] *[\n\r]+', 'start'), 1)
    729         % Set the line to whitespace and quit
    730         l(1:regexp(l, '[\n\r]', 'start', 'once')-1) = ' ';
    731         fseek(fh, -numel(l), 0);
    732         fprintf(fh, l);
    733         % Reduce the count
    734         count = count - 1;
    735     end
    736 end
    737 % Close the file
    738 fclose(fh);
    739 end
    740 
    741 function b = isvector(options)
    742 b = options.pdf || options.eps;
    743 end
    744 
    7451245function b = isbitmap(options)
    746 b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha;
     1246    b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha;
    7471247end
    7481248
    7491249% Helper function
    7501250function A = make_cell(A)
    751 if ~iscell(A)
    752     A = {A};
     1251    if ~iscell(A)
     1252        A = {A};
     1253    end
    7531254end
     1255
     1256function add_bookmark(fname, bookmark_text)
     1257    % Adds a bookmark to the temporary EPS file after %%EndPageSetup
     1258    % Read in the file
     1259    fh = fopen(fname, 'r');
     1260    if fh == -1
     1261        error('File %s not found.', fname);
     1262    end
     1263    try
     1264        fstrm = fread(fh, '*char')';
     1265    catch ex
     1266        fclose(fh);
     1267        rethrow(ex);
     1268    end
     1269    fclose(fh);
     1270
     1271    % Include standard pdfmark prolog to maximize compatibility
     1272    fstrm = strrep(fstrm, '%%BeginProlog', sprintf('%%%%BeginProlog\n/pdfmark where {pop} {userdict /pdfmark /cleartomark load put} ifelse'));
     1273    % Add page bookmark
     1274    fstrm = strrep(fstrm, '%%EndPageSetup', sprintf('%%%%EndPageSetup\n[ /Title (%s) /OUT pdfmark',bookmark_text));
     1275
     1276    % Write out the updated file
     1277    fh = fopen(fname, 'w');
     1278    if fh == -1
     1279        error('Unable to open %s for writing.', fname);
     1280    end
     1281    try
     1282        fwrite(fh, fstrm, 'char*1');
     1283    catch ex
     1284        fclose(fh);
     1285        rethrow(ex);
     1286    end
     1287    fclose(fh);
    7541288end
    7551289
    756 function add_bookmark(fname, bookmark_text)
    757 % Adds a bookmark to the temporary EPS file after %%EndPageSetup
    758 % Read in the file
    759 fh = fopen(fname, 'r');
    760 if fh == -1
    761     error('File %s not found.', fname);
     1290function set_tick_mode(Hlims, ax)
     1291    % Set the tick mode of linear axes to manual
     1292    % Leave log axes alone as these are tricky
     1293    M = get(Hlims, [ax 'Scale']);
     1294    if ~iscell(M)
     1295        M = {M};
     1296    end
     1297    %idx = cellfun(@(c) strcmp(c, 'linear'), M);
     1298    idx = find(strcmp(M,'linear'));
     1299    %set(Hlims(idx), [ax 'TickMode'], 'manual');  % issue #187
     1300    %set(Hlims(idx), [ax 'TickLabelMode'], 'manual');  % this hides exponent label in HG2!
     1301    for idx2 = 1 : numel(idx)
     1302        try
     1303            % Fix for issue #187 - only set manual ticks when no exponent is present
     1304            hAxes = Hlims(idx(idx2));
     1305            props = {[ax 'TickMode'],'manual', [ax 'TickLabelMode'],'manual'};
     1306            if isempty(strtrim(hAxes.([ax 'Ruler']).SecondaryLabel.String))
     1307                set(hAxes, props{:});  % no exponent, so update moth ticks and tick labels to manual
     1308            end
     1309        catch  % probably HG1
     1310            set(hAxes, props{:});  % revert back to old behavior
     1311        end
     1312    end
    7621313end
    763 try
    764     fstrm = fread(fh, '*char')';
    765 catch ex
    766     fclose(fh);
    767     rethrow(ex);
     1314
     1315function change_rgb_to_cmyk(fname)  % convert RGB => CMYK within an EPS file
     1316    % Do post-processing on the eps file
     1317    try
     1318        % Read the EPS file into memory
     1319        fstrm = read_write_entire_textfile(fname);
     1320
     1321        % Replace all gray-scale colors
     1322        fstrm = regexprep(fstrm, '\n([\d.]+) +GC\n', '\n0 0 0 ${num2str(1-str2num($1))} CC\n');
     1323       
     1324        % Replace all RGB colors
     1325        fstrm = regexprep(fstrm, '\n[0.]+ +[0.]+ +[0.]+ +RC\n', '\n0 0 0 1 CC\n');  % pure black
     1326        fstrm = regexprep(fstrm, '\n([\d.]+) +([\d.]+) +([\d.]+) +RC\n', '\n${sprintf(''%.4g '',[1-[str2num($1),str2num($2),str2num($3)]/max([str2num($1),str2num($2),str2num($3)]),1-max([str2num($1),str2num($2),str2num($3)])])} CC\n');
     1327
     1328        % Overwrite the file with the modified contents
     1329        read_write_entire_textfile(fname, fstrm);
     1330    catch
     1331        % never mind - leave as is...
     1332    end
    7681333end
    769 fclose(fh);
    770 
    771 % Include standard pdfmark prolog to maximize compatibility
    772 fstrm = strrep(fstrm, '%%BeginProlog', sprintf('%%%%BeginProlog\n/pdfmark where {pop} {userdict /pdfmark /cleartomark load put} ifelse'));
    773 % Add page bookmark
    774 fstrm = strrep(fstrm, '%%EndPageSetup', sprintf('%%%%EndPageSetup\n[ /Title (%s) /OUT pdfmark',bookmark_text));
    775 
    776 % Write out the updated file
    777 fh = fopen(fname, 'w');
    778 if fh == -1
    779     error('Unable to open %s for writing.', fname);
    780 end
    781 try
    782     fwrite(fh, fstrm, 'char*1');
    783 catch ex
    784     fclose(fh);
    785     rethrow(ex);
    786 end
    787 fclose(fh);
    788 end
    789 
    790 function set_tick_mode(Hlims, ax)
    791 % Set the tick mode of linear axes to manual
    792 % Leave log axes alone as these are tricky
    793 M = get(Hlims, [ax 'Scale']);
    794 if ~iscell(M)
    795     M = {M};
    796 end
    797 M = cellfun(@(c) strcmp(c, 'linear'), M);
    798 set(Hlims(M), [ax 'TickMode'], 'manual');
    799 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/fix_lines.m

    r19993 r21757  
    3838% opened.
    3939
     40% 01/03/15: Issue #20: warn users if using this function in HG2 (R2014b+)
     41% 27/03/15: Fixed out of memory issue with enormous EPS files (generated by print() with OpenGL renderer), related to issue #39
     42
    4043function fstrm = fix_lines(fstrm, fname2)
    4144
     45% Issue #20: warn users if using this function in HG2 (R2014b+)
     46if using_hg2
     47    warning('export_fig:hg2','The fix_lines function should not be used in this Matlab version.');
     48end
     49   
    4250if nargout == 0 || nargin > 1
    4351    if nargin < 2
     
    114122end
    115123
    116 % Isolate line style definition section
    117 first_sec = strfind(fstrm, '% line types:');
    118 [second_sec, remaining] = strtok(fstrm(first_sec+1:end), '/');
    119 [remaining, remaining] = strtok(remaining, '%');
    120 
    121124% Define the new styles, including the new GR format
    122125% Dot and dash lengths have two parts: a constant amount plus a line width
     
    135138
    136139% Construct the output
    137 fstrm = [fstrm(1:first_sec) second_sec sprintf('%s\r', new_style{:}) remaining];
     140% This is the original (memory-intensive) code:
     141%first_sec = strfind(fstrm, '% line types:'); % Isolate line style definition section
     142%[second_sec, remaining] = strtok(fstrm(first_sec+1:end), '/');
     143%[remaining, remaining] = strtok(remaining, '%');
     144%fstrm = [fstrm(1:first_sec) second_sec sprintf('%s\r', new_style{:}) remaining];
     145fstrm = regexprep(fstrm,'(% line types:.+?)/.+?%',['$1',sprintf('%s\r',new_style{:}),'%']);
    138146
    139147% Write the output file
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/ghostscript.m

    r19993 r21757  
     1function varargout = ghostscript(cmd)
    12%GHOSTSCRIPT  Calls a local GhostScript executable with the input command
    23%
     
    2021%   result - Output from ghostscript.
    2122
    22 % Copyright: Oliver Woodford, 2009-2013
     23% Copyright: Oliver Woodford, 2009-2015, Yair Altman 2015-
     24%{
     25% Thanks to Jonas Dorn for the fix for the title of the uigetdir window on Mac OS.
     26% Thanks to Nathan Childress for the fix to default location on 64-bit Windows systems.
     27% 27/04/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
     28%            Shaun Kline for pointing out the issue
     29% 04/05/11 - Thanks to David Chorlian for pointing out an alternative
     30%            location for gs on linux.
     31% 12/12/12 - Add extra executable name on Windows. Thanks to Ratish
     32%            Punnoose for highlighting the issue.
     33% 28/06/13 - Fix error using GS 9.07 in Linux. Many thanks to Jannick
     34%            Steinbring for proposing the fix.
     35% 24/10/13 - Fix error using GS 9.07 in Linux. Many thanks to Johannes
     36%            for the fix.
     37% 23/01/14 - Add full path to ghostscript.txt in warning. Thanks to Koen
     38%            Vermeer for raising the issue.
     39% 27/02/15 - If Ghostscript croaks, display suggested workarounds
     40% 30/03/15 - Improved performance by caching status of GS path check, if ok
     41% 14/05/15 - Clarified warning message in case GS path could not be saved
     42% 29/05/15 - Avoid cryptic error in case the ghostscipt path cannot be saved (issue #74)
     43% 10/11/15 - Custom GS installation webpage for MacOS. Thanks to Andy Hueni via FEX
     44%}
    2345
    24 % Thanks to Jonas Dorn for the fix for the title of the uigetdir window on
    25 % Mac OS.
    26 % Thanks to Nathan Childress for the fix to the default location on 64-bit
    27 % Windows systems.
    28 % 27/4/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
    29 % Shaun Kline for pointing out the issue
    30 % 4/5/11 - Thanks to David Chorlian for pointing out an alternative
    31 % location for gs on linux.
    32 % 12/12/12 - Add extra executable name on Windows. Thanks to Ratish
    33 % Punnoose for highlighting the issue.
    34 % 28/6/13 - Fix error using GS 9.07 in Linux. Many thanks to Jannick
    35 % Steinbring for proposing the fix.
    36 % 24/10/13 - Fix error using GS 9.07 in Linux. Many thanks to Johannes
    37 % for the fix.
    38 % 23/01/2014 - Add full path to ghostscript.txt in warning. Thanks to Koen
    39 % Vermeer for raising the issue.
    40 
    41 function varargout = ghostscript(cmd)
    42 % Initialize any required system calls before calling ghostscript
    43 shell_cmd = '';
    44 if isunix
    45     shell_cmd = 'export LD_LIBRARY_PATH=""; '; % Avoids an error on Linux with GS 9.07
    46 end
    47 if ismac
    48     shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';  % Avoids an error on Mac with GS 9.07
    49 end
    50 % Call ghostscript
    51 [varargout{1:nargout}] = system(sprintf('%s"%s" %s', shell_cmd, gs_path, cmd));
     46    try
     47        % Call ghostscript
     48        [varargout{1:nargout}] = system([gs_command(gs_path()) cmd]);
     49    catch err
     50        % Display possible workarounds for Ghostscript croaks
     51        url1 = 'https://github.com/altmany/export_fig/issues/12#issuecomment-61467998';  % issue #12
     52        url2 = 'https://github.com/altmany/export_fig/issues/20#issuecomment-63826270';  % issue #20
     53        hg2_str = ''; if using_hg2, hg2_str = ' or Matlab R2014a'; end
     54        fprintf(2, 'Ghostscript error. Rolling back to GS 9.10%s may possibly solve this:\n * <a href="%s">%s</a> ',hg2_str,url1,url1);
     55        if using_hg2
     56            fprintf(2, '(GS 9.10)\n * <a href="%s">%s</a> (R2014a)',url2,url2);
     57        end
     58        fprintf('\n\n');
     59        if ismac || isunix
     60            url3 = 'https://github.com/altmany/export_fig/issues/27';  % issue #27
     61            fprintf(2, 'Alternatively, this may possibly be due to a font path issue:\n * <a href="%s">%s</a>\n\n',url3,url3);
     62            % issue #20
     63            fpath = which(mfilename);
     64            if isempty(fpath), fpath = [mfilename('fullpath') '.m']; end
     65            fprintf(2, 'Alternatively, if you are using csh, modify shell_cmd from "export..." to "setenv ..."\nat the bottom of <a href="matlab:opentoline(''%s'',174)">%s</a>\n\n',fpath,fpath);
     66        end
     67        rethrow(err);
     68    end
    5269end
    5370
    5471function path_ = gs_path
    55 % Return a valid path
    56 % Start with the currently set path
    57 path_ = user_string('ghostscript');
    58 % Check the path works
    59 if check_gs_path(path_)
    60     return
    61 end
    62 % Check whether the binary is on the path
    63 if ispc
    64     bin = {'gswin32c.exe', 'gswin64c.exe', 'gs'};
    65 else
    66     bin = {'gs'};
    67 end
    68 for a = 1:numel(bin)
    69     path_ = bin{a};
    70     if check_store_gs_path(path_)
     72    % Return a valid path
     73    % Start with the currently set path
     74    path_ = user_string('ghostscript');
     75    % Check the path works
     76    if check_gs_path(path_)
    7177        return
    7278    end
    73 end
    74 % Search the obvious places
    75 if ispc
    76     default_location = 'C:\Program Files\gs\';
    77     dir_list = dir(default_location);
    78     if isempty(dir_list)
    79         default_location = 'C:\Program Files (x86)\gs\'; % Possible location on 64-bit systems
     79    % Check whether the binary is on the path
     80    if ispc
     81        bin = {'gswin32c.exe', 'gswin64c.exe', 'gs'};
     82    else
     83        bin = {'gs'};
     84    end
     85    for a = 1:numel(bin)
     86        path_ = bin{a};
     87        if check_store_gs_path(path_)
     88            return
     89        end
     90    end
     91    % Search the obvious places
     92    if ispc
     93        default_location = 'C:\Program Files\gs\';
    8094        dir_list = dir(default_location);
     95        if isempty(dir_list)
     96            default_location = 'C:\Program Files (x86)\gs\'; % Possible location on 64-bit systems
     97            dir_list = dir(default_location);
     98        end
     99        executable = {'\bin\gswin32c.exe', '\bin\gswin64c.exe'};
     100        ver_num = 0;
     101        % If there are multiple versions, use the newest
     102        for a = 1:numel(dir_list)
     103            ver_num2 = sscanf(dir_list(a).name, 'gs%g');
     104            if ~isempty(ver_num2) && ver_num2 > ver_num
     105                for b = 1:numel(executable)
     106                    path2 = [default_location dir_list(a).name executable{b}];
     107                    if exist(path2, 'file') == 2
     108                        path_ = path2;
     109                        ver_num = ver_num2;
     110                    end
     111                end
     112            end
     113        end
     114        if check_store_gs_path(path_)
     115            return
     116        end
     117    else
     118        executable = {'/usr/bin/gs', '/usr/local/bin/gs'};
     119        for a = 1:numel(executable)
     120            path_ = executable{a};
     121            if check_store_gs_path(path_)
     122                return
     123            end
     124        end
    81125    end
    82     executable = {'\bin\gswin32c.exe', '\bin\gswin64c.exe'};
    83     ver_num = 0;
    84     % If there are multiple versions, use the newest
    85     for a = 1:numel(dir_list)
    86         ver_num2 = sscanf(dir_list(a).name, 'gs%g');
    87         if ~isempty(ver_num2) && ver_num2 > ver_num
    88             for b = 1:numel(executable)
    89                 path2 = [default_location dir_list(a).name executable{b}];
    90                 if exist(path2, 'file') == 2
    91                     path_ = path2;
    92                     ver_num = ver_num2;
     126    % Ask the user to enter the path
     127    while true
     128        if strncmp(computer, 'MAC', 3) % Is a Mac
     129            % Give separate warning as the uigetdir dialogue box doesn't have a
     130            % title
     131            uiwait(warndlg('Ghostscript not found. Please locate the program.'))
     132        end
     133        base = uigetdir('/', 'Ghostcript not found. Please locate the program.');
     134        if isequal(base, 0)
     135            % User hit cancel or closed window
     136            break;
     137        end
     138        base = [base filesep]; %#ok<AGROW>
     139        bin_dir = {'', ['bin' filesep], ['lib' filesep]};
     140        for a = 1:numel(bin_dir)
     141            for b = 1:numel(bin)
     142                path_ = [base bin_dir{a} bin{b}];
     143                if exist(path_, 'file') == 2
     144                    if check_store_gs_path(path_)
     145                        return
     146                    end
    93147                end
    94148            end
    95149        end
    96150    end
    97     if check_store_gs_path(path_)
    98         return
     151    if ismac
     152        error('Ghostscript not found. Have you installed it (http://pages.uoregon.edu/koch)?');
     153    else
     154        error('Ghostscript not found. Have you installed it from www.ghostscript.com?');
    99155    end
    100 else
    101     executable = {'/usr/bin/gs', '/usr/local/bin/gs'};
    102     for a = 1:numel(executable)
    103         path_ = executable{a};
    104         if check_store_gs_path(path_)
    105             return
    106         end
    107     end
    108 end
    109 % Ask the user to enter the path
    110 while 1
    111     if strncmp(computer, 'MAC', 3) % Is a Mac
    112         % Give separate warning as the uigetdir dialogue box doesn't have a
    113         % title
    114         uiwait(warndlg('Ghostscript not found. Please locate the program.'))
    115     end
    116     base = uigetdir('/', 'Ghostcript not found. Please locate the program.');
    117     if isequal(base, 0)
    118         % User hit cancel or closed window
    119         break;
    120     end
    121     base = [base filesep];
    122     bin_dir = {'', ['bin' filesep], ['lib' filesep]};
    123     for a = 1:numel(bin_dir)
    124         for b = 1:numel(bin)
    125             path_ = [base bin_dir{a} bin{b}];
    126             if exist(path_, 'file') == 2
    127                 if check_store_gs_path(path_)
    128                     return
    129                 end
    130             end
    131         end
    132     end
    133 end
    134 error('Ghostscript not found. Have you installed it from www.ghostscript.com?');
    135156end
    136157
    137158function good = check_store_gs_path(path_)
    138 % Check the path is valid
    139 good = check_gs_path(path_);
    140 if ~good
    141     return
    142 end
    143 % Update the current default path to the path found
    144 if ~user_string('ghostscript', path_)
    145     warning('Path to ghostscript installation could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'ghostscript.txt'));
    146     return
    147 end
     159    % Check the path is valid
     160    good = check_gs_path(path_);
     161    if ~good
     162        return
     163    end
     164    % Update the current default path to the path found
     165    if ~user_string('ghostscript', path_)
     166        filename = fullfile(fileparts(which('user_string.m')), '.ignore', 'ghostscript.txt');
     167        warning('Path to ghostscript installation could not be saved in %s (perhaps a permissions issue). You can manually create this file and set its contents to %s, to improve performance in future invocations (this warning is safe to ignore).', filename, path_);
     168        return
     169    end
    148170end
    149171
    150172function good = check_gs_path(path_)
    151 % Check the path is valid
    152 shell_cmd = '';
    153 if ismac
    154     shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';  % Avoids an error on Mac with GS 9.07
     173    persistent isOk
     174    if isempty(path_)
     175        isOk = false;
     176    elseif ~isequal(isOk,true)
     177        % Check whether the path is valid
     178        [status, message] = system([gs_command(path_) '-h']); %#ok<ASGLU>
     179        isOk = status == 0;
     180    end
     181    good = isOk;
    155182end
    156 [good, message] = system(sprintf('%s"%s" -h', shell_cmd, path_));
    157 good = good == 0;
     183
     184function cmd = gs_command(path_)
     185    % Initialize any required system calls before calling ghostscript
     186    % TODO: in Unix/Mac, find a way to determine whether to use "export" (bash) or "setenv" (csh/tcsh)
     187    shell_cmd = '';
     188    if isunix
     189        shell_cmd = 'export LD_LIBRARY_PATH=""; '; % Avoids an error on Linux with GS 9.07
     190    end
     191    if ismac
     192        shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';  % Avoids an error on Mac with GS 9.07
     193    end
     194    % Construct the command string
     195    cmd = sprintf('%s"%s" ', shell_cmd, path_);
    158196end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/isolate_axes.m

    r19993 r21757  
     1function fh = isolate_axes(ah, vis)
    12%ISOLATE_AXES Isolate the specified axes in a figure on their own
    23%
     
    2223
    2324% Thank you to Rosella Blatt for reporting a bug to do with axes in GUIs
    24 % 16/3/2012 Moved copyfig to its own function. Thanks to Bob Fratantonio
    25 % for pointing out that the function is also used in export_fig.m.
    26 % 12/12/12 - Add support for isolating uipanels. Thanks to michael for
    27 % suggesting it.
    28 % 08/10/13 - Bug fix to allchildren suggested by Will Grant (many thanks!).
    29 % 05/12/13 - Bug fix to axes having different units. Thanks to Remington
    30 % Reid for reporting the issue.
     25% 16/03/12: Moved copyfig to its own function. Thanks to Bob Fratantonio
     26%           for pointing out that the function is also used in export_fig.m
     27% 12/12/12: Add support for isolating uipanels. Thanks to michael for suggesting it
     28% 08/10/13: Bug fix to allchildren suggested by Will Grant (many thanks!)
     29% 05/12/13: Bug fix to axes having different units. Thanks to Remington Reid for reporting
     30% 21/04/15: Bug fix for exporting uipanels with legend/colorbar on HG1 (reported by Alvaro
     31%           on FEX page as a comment on 24-Apr-2014); standardized indentation & help section
     32% 22/04/15: Bug fix: legends and colorbars were not exported when exporting axes handle in HG2
    3133
    32 function fh = isolate_axes(ah, vis)
    33 % Make sure we have an array of handles
    34 if ~all(ishandle(ah))
    35     error('ah must be an array of handles');
    36 end
    37 % Check that the handles are all for axes or uipanels, and are all in the same figure
    38 fh = ancestor(ah(1), 'figure');
    39 nAx = numel(ah);
    40 for a = 1:nAx
    41     if ~ismember(get(ah(a), 'Type'), {'axes', 'uipanel'})
    42         error('All handles must be axes or uipanel handles.');
     34    % Make sure we have an array of handles
     35    if ~all(ishandle(ah))
     36        error('ah must be an array of handles');
    4337    end
    44     if ~isequal(ancestor(ah(a), 'figure'), fh)
    45         error('Axes must all come from the same figure.');
     38    % Check that the handles are all for axes or uipanels, and are all in the same figure
     39    fh = ancestor(ah(1), 'figure');
     40    nAx = numel(ah);
     41    for a = 1:nAx
     42        if ~ismember(get(ah(a), 'Type'), {'axes', 'uipanel'})
     43            error('All handles must be axes or uipanel handles.');
     44        end
     45        if ~isequal(ancestor(ah(a), 'figure'), fh)
     46            error('Axes must all come from the same figure.');
     47        end
    4648    end
    47 end
    48 % Tag the objects so we can find them in the copy
    49 old_tag = get(ah, 'Tag');
    50 if nAx == 1
    51     old_tag = {old_tag};
    52 end
    53 set(ah, 'Tag', 'ObjectToCopy');
    54 % Create a new figure exactly the same as the old one
    55 fh = copyfig(fh); %copyobj(fh, 0);
    56 if nargin < 2 || ~vis
    57     set(fh, 'Visible', 'off');
    58 end
    59 % Reset the object tags
    60 for a = 1:nAx
    61     set(ah(a), 'Tag', old_tag{a});
    62 end
    63 % Find the objects to save
    64 ah = findall(fh, 'Tag', 'ObjectToCopy');
    65 if numel(ah) ~= nAx
    66     close(fh);
    67     error('Incorrect number of objects found.');
    68 end
    69 % Set the axes tags to what they should be
    70 for a = 1:nAx
    71     set(ah(a), 'Tag', old_tag{a});
    72 end
    73 % Keep any legends and colorbars which overlap the subplots
    74 lh = findall(fh, 'Type', 'axes', '-and', {'Tag', 'legend', '-or', 'Tag', 'Colorbar'});
    75 nLeg = numel(lh);
    76 if nLeg > 0
    77     set([ah(:); lh(:)], 'Units', 'normalized');
    78     ax_pos = get(ah, 'OuterPosition');
    79     if nAx > 1
    80         ax_pos = cell2mat(ax_pos(:));
     49    % Tag the objects so we can find them in the copy
     50    old_tag = get(ah, 'Tag');
     51    if nAx == 1
     52        old_tag = {old_tag};
    8153    end
    82     ax_pos(:,3:4) = ax_pos(:,3:4) + ax_pos(:,1:2);
    83     leg_pos = get(lh, 'OuterPosition');
    84     if nLeg > 1;
    85         leg_pos = cell2mat(leg_pos);
     54    set(ah, 'Tag', 'ObjectToCopy');
     55    % Create a new figure exactly the same as the old one
     56    fh = copyfig(fh); %copyobj(fh, 0);
     57    if nargin < 2 || ~vis
     58        set(fh, 'Visible', 'off');
    8659    end
    87     leg_pos(:,3:4) = leg_pos(:,3:4) + leg_pos(:,1:2);
    88     ax_pos = shiftdim(ax_pos, -1);
    89     % Overlap test
    90     M = bsxfun(@lt, leg_pos(:,1), ax_pos(:,:,3)) & ...
    91         bsxfun(@lt, leg_pos(:,2), ax_pos(:,:,4)) & ...
    92         bsxfun(@gt, leg_pos(:,3), ax_pos(:,:,1)) & ...
    93         bsxfun(@gt, leg_pos(:,4), ax_pos(:,:,2));
    94     ah = [ah; lh(any(M, 2))];
    95 end
    96 % Get all the objects in the figure
    97 axs = findall(fh);
    98 % Delete everything except for the input objects and associated items
    99 delete(axs(~ismember(axs, [ah; allchildren(ah); allancestors(ah)])));
     60    % Reset the object tags
     61    for a = 1:nAx
     62        set(ah(a), 'Tag', old_tag{a});
     63    end
     64    % Find the objects to save
     65    ah = findall(fh, 'Tag', 'ObjectToCopy');
     66    if numel(ah) ~= nAx
     67        close(fh);
     68        error('Incorrect number of objects found.');
     69    end
     70    % Set the axes tags to what they should be
     71    for a = 1:nAx
     72        set(ah(a), 'Tag', old_tag{a});
     73    end
     74    % Keep any legends and colorbars which overlap the subplots
     75    % Note: in HG1 these are axes objects; in HG2 they are separate objects, therefore we
     76    %       don't test for the type, only the tag (hopefully nobody but Matlab uses them!)
     77    lh = findall(fh, 'Tag', 'legend', '-or', 'Tag', 'Colorbar');
     78    nLeg = numel(lh);
     79    if nLeg > 0
     80        set([ah(:); lh(:)], 'Units', 'normalized');
     81        try
     82            ax_pos = get(ah, 'OuterPosition'); % axes and figures have the OuterPosition property
     83        catch
     84            ax_pos = get(ah, 'Position'); % uipanels only have Position, not OuterPosition
     85        end
     86        if nAx > 1
     87            ax_pos = cell2mat(ax_pos(:));
     88        end
     89        ax_pos(:,3:4) = ax_pos(:,3:4) + ax_pos(:,1:2);
     90        try
     91            leg_pos = get(lh, 'OuterPosition');
     92        catch
     93            leg_pos = get(lh, 'Position');  % No OuterPosition in HG2, only in HG1
     94        end
     95        if nLeg > 1;
     96            leg_pos = cell2mat(leg_pos);
     97        end
     98        leg_pos(:,3:4) = leg_pos(:,3:4) + leg_pos(:,1:2);
     99        ax_pos = shiftdim(ax_pos, -1);
     100        % Overlap test
     101        M = bsxfun(@lt, leg_pos(:,1), ax_pos(:,:,3)) & ...
     102            bsxfun(@lt, leg_pos(:,2), ax_pos(:,:,4)) & ...
     103            bsxfun(@gt, leg_pos(:,3), ax_pos(:,:,1)) & ...
     104            bsxfun(@gt, leg_pos(:,4), ax_pos(:,:,2));
     105        ah = [ah; lh(any(M, 2))];
     106    end
     107    % Get all the objects in the figure
     108    axs = findall(fh);
     109    % Delete everything except for the input objects and associated items
     110    delete(axs(~ismember(axs, [ah; allchildren(ah); allancestors(ah)])));
    100111end
    101112
    102113function ah = allchildren(ah)
    103 ah = findall(ah);
    104 if iscell(ah)
    105     ah = cell2mat(ah);
    106 end
    107 ah = ah(:);
     114    ah = findall(ah);
     115    if iscell(ah)
     116        ah = cell2mat(ah);
     117    end
     118    ah = ah(:);
    108119end
    109120
    110121function ph = allancestors(ah)
    111 ph = [];
    112 for a = 1:numel(ah)
    113     h = get(ah(a), 'parent');
    114     while h ~= 0
    115         ph = [ph; h];
    116         h = get(h, 'parent');
     122    ph = [];
     123    for a = 1:numel(ah)
     124        h = get(ah(a), 'parent');
     125        while h ~= 0
     126            ph = [ph; h];
     127            h = get(h, 'parent');
     128        end
    117129    end
    118130end
    119 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/pdftops.m

    r19993 r21757  
    1616%
    1717% IN:
    18 %   cmd - Command string to be passed into pdftops.
     18%   cmd - Command string to be passed into pdftops (e.g. '-help').
    1919%
    2020% OUT:
     
    2424% Copyright: Oliver Woodford, 2009-2010
    2525
    26 % Thanks to Jonas Dorn for the fix for the title of the uigetdir window on
    27 % Mac OS.
    28 % Thanks to Christoph Hertel for pointing out a bug in check_xpdf_path
    29 % under linux.
     26% Thanks to Jonas Dorn for the fix for the title of the uigetdir window on Mac OS.
     27% Thanks to Christoph Hertel for pointing out a bug in check_xpdf_path under linux.
    3028% 23/01/2014 - Add full path to pdftops.txt in warning.
     29% 27/05/2015 - Fixed alert in case of missing pdftops; fixed code indentation
     30% 02/05/2016 - Added possible error explanation suggested by Michael Pacer (issue #137)
     31% 02/05/2016 - Search additional possible paths suggested by Jonas Stein (issue #147)
     32% 03/05/2016 - Display the specific error message if pdftops fails for some reason (issue #148)
    3133
    32 % Call pdftops
    33 [varargout{1:nargout}] = system(sprintf('"%s" %s', xpdf_path, cmd));
     34    % Call pdftops
     35    [varargout{1:nargout}] = system([xpdf_command(xpdf_path()) cmd]);
    3436end
    3537
    3638function path_ = xpdf_path
    37 % Return a valid path
    38 % Start with the currently set path
    39 path_ = user_string('pdftops');
    40 % Check the path works
    41 if check_xpdf_path(path_)
    42     return
    43 end
    44 % Check whether the binary is on the path
    45 if ispc
    46     bin = 'pdftops.exe';
    47 else
    48     bin = 'pdftops';
    49 end
    50 if check_store_xpdf_path(bin)
    51     path_ = bin;
    52     return
    53 end
    54 % Search the obvious places
    55 if ispc
    56     path_ = 'C:\Program Files\xpdf\pdftops.exe';
    57 else
    58     path_ = '/usr/local/bin/pdftops';
    59 end
    60 if check_store_xpdf_path(path_)
    61     return
    62 end
    63 % Ask the user to enter the path
    64 while 1
    65     if strncmp(computer,'MAC',3) % Is a Mac
    66         % Give separate warning as the uigetdir dialogue box doesn't have a
    67         % title
    68         uiwait(warndlg('Pdftops not found. Please locate the program, or install xpdf-tools from http://users.phg-online.de/tk/MOSXS/.'))
     39    % Return a valid path
     40    % Start with the currently set path
     41    path_ = user_string('pdftops');
     42    % Check the path works
     43    if check_xpdf_path(path_)
     44        return
    6945    end
    70     base = uigetdir('/', 'Pdftops not found. Please locate the program.');
    71     if isequal(base, 0)
    72         % User hit cancel or closed window
    73         break;
     46    % Check whether the binary is on the path
     47    if ispc
     48        bin = 'pdftops.exe';
     49    else
     50        bin = 'pdftops';
    7451    end
    75     base = [base filesep];
    76     bin_dir = {'', ['bin' filesep], ['lib' filesep]};
    77     for a = 1:numel(bin_dir)
    78         path_ = [base bin_dir{a} bin];
    79         if exist(path_, 'file') == 2
    80             break;
     52    if check_store_xpdf_path(bin)
     53        path_ = bin;
     54        return
     55    end
     56    % Search the obvious places
     57    if ispc
     58        paths = {'C:\Program Files\xpdf\pdftops.exe', 'C:\Program Files (x86)\xpdf\pdftops.exe'};
     59    else
     60        paths = {'/usr/bin/pdftops', '/usr/local/bin/pdftops'};
     61    end
     62    for a = 1:numel(paths)
     63        path_ = paths{a};
     64        if check_store_xpdf_path(path_)
     65            return
    8166        end
    8267    end
    83     if check_store_xpdf_path(path_)
     68
     69    % Ask the user to enter the path
     70    errMsg1 = 'Pdftops not found. Please locate the program, or install xpdf-tools from ';
     71    url1 = 'http://foolabs.com/xpdf';
     72    fprintf(2, '%s\n', [errMsg1 '<a href="matlab:web(''-browser'',''' url1 ''');">' url1 '</a>']);
     73    errMsg1 = [errMsg1 url1];
     74    %if strncmp(computer,'MAC',3) % Is a Mac
     75    %    % Give separate warning as the MacOS uigetdir dialogue box doesn't have a title
     76    %    uiwait(warndlg(errMsg1))
     77    %end
     78
     79    % Provide an alternative possible explanation as per issue #137
     80    errMsg2 = 'If you have pdftops installed, perhaps Matlab is shaddowing it as described in ';
     81    url2 = 'https://github.com/altmany/export_fig/issues/137';
     82    fprintf(2, '%s\n', [errMsg2 '<a href="matlab:web(''-browser'',''' url2 ''');">issue #137</a>']);
     83    errMsg2 = [errMsg2 url1];
     84
     85    state = 0;
     86    while 1
     87        if state
     88            option1 = 'Install pdftops';
     89        else
     90            option1 = 'Issue #137';
     91        end
     92        answer = questdlg({errMsg1,'',errMsg2},'Pdftops error',option1,'Locate pdftops','Cancel','Cancel');
     93        drawnow;  % prevent a Matlab hang: http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem
     94        switch answer
     95            case 'Install pdftops'
     96                web('-browser',url1);
     97            case 'Issue #137'
     98                web('-browser',url2);
     99                state = 1;
     100            case 'Locate pdftops'
     101                base = uigetdir('/', errMsg1);
     102                if isequal(base, 0)
     103                    % User hit cancel or closed window
     104                    break
     105                end
     106                base = [base filesep]; %#ok<AGROW>
     107                bin_dir = {'', ['bin' filesep], ['lib' filesep]};
     108                for a = 1:numel(bin_dir)
     109                    path_ = [base bin_dir{a} bin];
     110                    if exist(path_, 'file') == 2
     111                        break
     112                    end
     113                end
     114                if check_store_xpdf_path(path_)
     115                    return
     116                end
     117
     118            otherwise  % User hit Cancel or closed window
     119                break
     120        end
     121    end
     122    error('pdftops executable not found.');
     123end
     124
     125function good = check_store_xpdf_path(path_)
     126    % Check the path is valid
     127    good = check_xpdf_path(path_);
     128    if ~good
     129        return
     130    end
     131    % Update the current default path to the path found
     132    if ~user_string('pdftops', path_)
     133        warning('Path to pdftops executable could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'pdftops.txt'));
    84134        return
    85135    end
    86136end
    87 error('pdftops executable not found.');
     137
     138function good = check_xpdf_path(path_)
     139    % Check the path is valid
     140    [good, message] = system([xpdf_command(path_) '-h']); %#ok<ASGLU>
     141    % system returns good = 1 even when the command runs
     142    % Look for something distinct in the help text
     143    good = ~isempty(strfind(message, 'PostScript'));
     144
     145    % Display the error message if the pdftops executable exists but fails for some reason
     146    if ~good && exist(path_,'file')  % file exists but generates an error
     147        fprintf('Error running %s:\n', path_);
     148        fprintf(2,'%s\n\n',message);
     149    end
    88150end
    89151
    90 function good = check_store_xpdf_path(path_)
    91 % Check the path is valid
    92 good = check_xpdf_path(path_);
    93 if ~good
    94     return
     152function cmd = xpdf_command(path_)
     153    % Initialize any required system calls before calling ghostscript
     154    % TODO: in Unix/Mac, find a way to determine whether to use "export" (bash) or "setenv" (csh/tcsh)
     155    shell_cmd = '';
     156    if isunix
     157        % Avoids an error on Linux with outdated MATLAB lib files
     158        % R20XXa/bin/glnxa64/libtiff.so.X
     159        % R20XXa/sys/os/glnxa64/libstdc++.so.X
     160        shell_cmd = 'export LD_LIBRARY_PATH=""; ';
     161    end
     162    if ismac
     163        shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';
     164    end
     165    % Construct the command string
     166    cmd = sprintf('%s"%s" ', shell_cmd, path_);
    95167end
    96 % Update the current default path to the path found
    97 if ~user_string('pdftops', path_)
    98     warning('Path to pdftops executable could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'pdftops.txt'));
    99     return
    100 end
    101 end
    102 
    103 function good = check_xpdf_path(path_)
    104 % Check the path is valid
    105 [good, message] = system(sprintf('"%s" -h', path_));
    106 % system returns good = 1 even when the command runs
    107 % Look for something distinct in the help text
    108 good = ~isempty(strfind(message, 'PostScript'));
    109 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/print2array.m

    r19993 r21757  
     1function [A, bcol] = print2array(fig, res, renderer, gs_options)
    12%PRINT2ARRAY  Exports a figure to an image array
    23%
     
    67%   A = print2array(figure_handle, resolution)
    78%   A = print2array(figure_handle, resolution, renderer)
     9%   A = print2array(figure_handle, resolution, renderer, gs_options)
    810%   [A bcol] = print2array(...)
    911%
     
    2022%   renderer - string containing the renderer paramater to be passed to
    2123%              print. Default: '-opengl'.
     24%   gs_options - optional ghostscript options (e.g.: '-dNoOutputFonts'). If
     25%                multiple options are needed, enclose in call array: {'-a','-b'}
    2226%
    2327% OUT:
     
    2529%   bcol - 1x3 uint8 vector of the background color
    2630
    27 % Copyright (C) Oliver Woodford 2008-2012
    28 
     31% Copyright (C) Oliver Woodford 2008-2014, Yair Altman 2015-
     32%{
    2933% 05/09/11: Set EraseModes to normal when using opengl or zbuffer
    30 %           renderers. Thanks to Pawel Kocieniewski for reporting the
    31 %           issue.
    32 % 21/09/11: Bug fix: unit8 -> uint8! Thanks to Tobias Lamour for reporting
    33 %           the issue.
    34 % 14/11/11: Bug fix: stop using hardcopy(), as it interfered with figure
    35 %           size and erasemode settings. Makes it a bit slower, but more
    36 %           reliable. Thanks to Phil Trinh and Meelis Lootus for reporting
    37 %           the issues.
     34%           renderers. Thanks to Pawel Kocieniewski for reporting the issue.
     35% 21/09/11: Bug fix: unit8 -> uint8! Thanks to Tobias Lamour for reporting it.
     36% 14/11/11: Bug fix: stop using hardcopy(), as it interfered with figure size
     37%           and erasemode settings. Makes it a bit slower, but more reliable.
     38%           Thanks to Phil Trinh and Meelis Lootus for reporting the issues.
    3839% 09/12/11: Pass font path to ghostscript.
    3940% 27/01/12: Bug fix affecting painters rendering tall figures. Thanks to
    4041%           Ken Campbell for reporting it.
    41 % 03/04/12: Bug fix to median input. Thanks to Andy Matthews for reporting
    42 %           it.
     42% 03/04/12: Bug fix to median input. Thanks to Andy Matthews for reporting it.
    4343% 26/10/12: Set PaperOrientation to portrait. Thanks to Michael Watts for
    4444%           reporting the issue.
    45 
    46 function [A, bcol] = print2array(fig, res, renderer)
    47 % Generate default input arguments, if needed
    48 if nargin < 2
    49     res = 1;
    50     if nargin < 1
    51         fig = gcf;
     45% 26/02/15: If temp dir is not writable, use the current folder for temp
     46%           EPS/TIF files (Javier Paredes)
     47% 27/02/15: Display suggested workarounds to internal print() error (issue #16)
     48% 28/02/15: Enable users to specify optional ghostscript options (issue #36)
     49% 10/03/15: Fixed minor warning reported by Paul Soderlind; fixed code indentation
     50% 28/05/15: Fixed issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() func)
     51% 07/07/15: Fixed issue #83: use numeric handles in HG1
     52% 11/12/16: Fixed cropping issue reported by Harry D.
     53%}
     54
     55    % Generate default input arguments, if needed
     56    if nargin < 2
     57        res = 1;
     58        if nargin < 1
     59            fig = gcf;
     60        end
     61    end
     62    % Warn if output is large
     63    old_mode = get(fig, 'Units');
     64    set(fig, 'Units', 'pixels');
     65    px = get(fig, 'Position');
     66    set(fig, 'Units', old_mode);
     67    npx = prod(px(3:4)*res)/1e6;
     68    if npx > 30
     69        % 30M pixels or larger!
     70        warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
     71    end
     72    % Retrieve the background colour
     73    bcol = get(fig, 'Color');
     74    % Set the resolution parameter
     75    res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
     76    % Generate temporary file name
     77    tmp_nam = [tempname '.tif'];
     78    try
     79        % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
     80        fid = fopen(tmp_nam,'w');
     81        fwrite(fid,1);
     82        fclose(fid);
     83        delete(tmp_nam);  % cleanup
     84        isTempDirOk = true;
     85    catch
     86        % Temp dir is not writable, so use the current folder
     87        [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
     88        fpath = pwd;
     89        tmp_nam = fullfile(fpath,[fname fext]);
     90        isTempDirOk = false;
     91    end
     92    % Enable users to specify optional ghostscript options (issue #36)
     93    if nargin > 3 && ~isempty(gs_options)
     94        if iscell(gs_options)
     95            gs_options = sprintf(' %s',gs_options{:});
     96        elseif ~ischar(gs_options)
     97            error('gs_options input argument must be a string or cell-array of strings');
     98        else
     99            gs_options = [' ' gs_options];
     100        end
     101    else
     102        gs_options = '';
     103    end
     104    if nargin > 2 && strcmp(renderer, '-painters')
     105        % First try to print directly to tif file
     106        try
     107            % Print the file into a temporary TIF file and read it into array A
     108            [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam);
     109            if err, rethrow(ex); end
     110        catch  % error - try to print to EPS and then using Ghostscript to TIF
     111            % Print to eps file
     112            if isTempDirOk
     113                tmp_eps = [tempname '.eps'];
     114            else
     115                tmp_eps = fullfile(fpath,[fname '.eps']);
     116            end
     117            print2eps(tmp_eps, fig, 0, renderer, '-loose');
     118            try
     119                % Initialize the command to export to tiff using ghostscript
     120                cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
     121                % Set the font path
     122                fp = font_path();
     123                if ~isempty(fp)
     124                    cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
     125                end
     126                % Add the filenames
     127                cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"' gs_options];
     128                % Execute the ghostscript command
     129                ghostscript(cmd_str);
     130            catch me
     131                % Delete the intermediate file
     132                delete(tmp_eps);
     133                rethrow(me);
     134            end
     135            % Delete the intermediate file
     136            delete(tmp_eps);
     137            % Read in the generated bitmap
     138            A = imread(tmp_nam);
     139            % Delete the temporary bitmap file
     140            delete(tmp_nam);
     141        end
     142        % Set border pixels to the correct colour
     143        if isequal(bcol, 'none')
     144            bcol = [];
     145        elseif isequal(bcol, [1 1 1])
     146            bcol = uint8([255 255 255]);
     147        else
     148            for l = 1:size(A, 2)
     149                if ~all(reshape(A(:,l,:) == 255, [], 1))
     150                    break;
     151                end
     152            end
     153            for r = size(A, 2):-1:l
     154                if ~all(reshape(A(:,r,:) == 255, [], 1))
     155                    break;
     156                end
     157            end
     158            for t = 1:size(A, 1)
     159                if ~all(reshape(A(t,:,:) == 255, [], 1))
     160                    break;
     161                end
     162            end
     163            for b = size(A, 1):-1:t
     164                if ~all(reshape(A(b,:,:) == 255, [], 1))
     165                    break;
     166                end
     167            end
     168            bcol = uint8(median(single([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))]), 1));
     169            for c = 1:size(A, 3)
     170                A(:,[1:l-1, r+1:end],c) = bcol(c);
     171                A([1:t-1, b+1:end],:,c) = bcol(c);
     172            end
     173        end
     174    else
     175        if nargin < 3
     176            renderer = '-opengl';
     177        end
     178        % Print the file into a temporary TIF file and read it into array A
     179        [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam);
     180        % Throw any error that occurred
     181        if err
     182            % Display suggested workarounds to internal print() error (issue #16)
     183            fprintf(2, 'An error occured with Matlab''s builtin print function.\nTry setting the figure Renderer to ''painters'' or use opengl(''software'').\n\n');
     184            rethrow(ex);
     185        end
     186        % Set the background color
     187        if isequal(bcol, 'none')
     188            bcol = [];
     189        else
     190            bcol = bcol * 255;
     191            if isequal(bcol, round(bcol))
     192                bcol = uint8(bcol);
     193            else
     194                bcol = squeeze(A(1,1,:));
     195            end
     196        end
     197    end
     198    % Check the output size is correct
     199    if isequal(res, round(res))
     200        px = round([px([4 3])*res 3]);  % round() to avoid an indexing warning below
     201        if ~isequal(size(A), px)
     202            % Correct the output size
     203            A = A(1:min(end,px(1)),1:min(end,px(2)),:);
     204        end
    52205    end
    53206end
    54 % Warn if output is large
    55 old_mode = get(fig, 'Units');
    56 set(fig, 'Units', 'pixels');
    57 px = get(fig, 'Position');
    58 set(fig, 'Units', old_mode);
    59 npx = prod(px(3:4)*res)/1e6;
    60 if npx > 30
    61     % 30M pixels or larger!
    62     warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
    63 end
    64 % Retrieve the background colour
    65 bcol = get(fig, 'Color');
    66 % Set the resolution parameter
    67 res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
    68 % Generate temporary file name
    69 tmp_nam = [tempname '.tif'];
    70 if nargin > 2 && strcmp(renderer, '-painters')
    71     % Print to eps file
    72     tmp_eps = [tempname '.eps'];
    73     print2eps(tmp_eps, fig, 0, renderer, '-loose');
     207
     208% Function to create a TIF image of the figure and read it into an array
     209function [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam)
     210    err = false;
     211    ex = [];
     212    % Temporarily set the paper size
     213    old_pos_mode    = get(fig, 'PaperPositionMode');
     214    old_orientation = get(fig, 'PaperOrientation');
     215    set(fig, 'PaperPositionMode','auto', 'PaperOrientation','portrait');
    74216    try
    75         % Initialize the command to export to tiff using ghostscript
    76         cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
    77         % Set the font path
    78         fp = font_path();
    79         if ~isempty(fp)
    80             cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
    81         end
    82         % Add the filenames
    83         cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"'];
    84         % Execute the ghostscript command
    85         ghostscript(cmd_str);
    86     catch me
    87         % Delete the intermediate file
    88         delete(tmp_eps);
    89         rethrow(me);
    90     end
    91     % Delete the intermediate file
    92     delete(tmp_eps);
    93     % Read in the generated bitmap
    94     A = imread(tmp_nam);
    95     % Delete the temporary bitmap file
    96     delete(tmp_nam);
    97     % Set border pixels to the correct colour
    98     if isequal(bcol, 'none')
    99         bcol = [];
    100     elseif isequal(bcol, [1 1 1])
    101         bcol = uint8([255 255 255]);
    102     else
    103         for l = 1:size(A, 2)
    104             if ~all(reshape(A(:,l,:) == 255, [], 1))
    105                 break;
    106             end
    107         end
    108         for r = size(A, 2):-1:l
    109             if ~all(reshape(A(:,r,:) == 255, [], 1))
    110                 break;
    111             end
    112         end
    113         for t = 1:size(A, 1)
    114             if ~all(reshape(A(t,:,:) == 255, [], 1))
    115                 break;
    116             end
    117         end
    118         for b = size(A, 1):-1:t
    119             if ~all(reshape(A(b,:,:) == 255, [], 1))
    120                 break;
    121             end
    122         end
    123         bcol = uint8(median(single([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))]), 1));
    124         for c = 1:size(A, 3)
    125             A(:,[1:l-1, r+1:end],c) = bcol(c);
    126             A([1:t-1, b+1:end],:,c) = bcol(c);
    127         end
    128     end
    129 else
    130     if nargin < 3
    131         renderer = '-opengl';
    132     end
    133     err = false;
    134     % Set paper size
    135     old_pos_mode = get(fig, 'PaperPositionMode');
    136     old_orientation = get(fig, 'PaperOrientation');
    137     set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
    138     try
     217        % Workaround for issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() function)
     218        fp = [];  % in case we get an error below
     219        fp = findall(fig, 'Type','patch', 'LineWidth',0.75);
     220        set(fp, 'LineWidth',0.5);
     221        % Fix issue #83: use numeric handles in HG1
     222        if ~using_hg2(fig),  fig = double(fig);  end
    139223        % Print to tiff file
    140224        print(fig, renderer, res_str, '-dtiff', tmp_nam);
     
    146230        err = true;
    147231    end
    148     % Reset paper size
    149     set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
    150     % Throw any error that occurred
    151     if err
    152         rethrow(ex);
    153     end
    154     % Set the background color
    155     if isequal(bcol, 'none')
    156         bcol = [];
    157     else
    158         bcol = bcol * 255;
    159         if isequal(bcol, round(bcol))
    160             bcol = uint8(bcol);
    161         else
    162             bcol = squeeze(A(1,1,:));
    163         end
    164     end
    165 end
    166 % Check the output size is correct
    167 if isequal(res, round(res))
    168     px = [px([4 3])*res 3];
    169     if ~isequal(size(A), px)
    170         % Correct the output size
    171         A = A(1:min(end,px(1)),1:min(end,px(2)),:);
    172     end
    173 end
     232    set(fp, 'LineWidth',0.75);  % restore original figure appearance
     233    % Reset the paper size
     234    set(fig, 'PaperPositionMode',old_pos_mode, 'PaperOrientation',old_orientation);
    174235end
    175236
    176237% Function to return (and create, where necessary) the font path
    177238function fp = font_path()
    178 fp = user_string('gs_font_path');
    179 if ~isempty(fp)
    180     return
     239    fp = user_string('gs_font_path');
     240    if ~isempty(fp)
     241        return
     242    end
     243    % Create the path
     244    % Start with the default path
     245    fp = getenv('GS_FONTPATH');
     246    % Add on the typical directories for a given OS
     247    if ispc
     248        if ~isempty(fp)
     249            fp = [fp ';'];
     250        end
     251        fp = [fp getenv('WINDIR') filesep 'Fonts'];
     252    else
     253        if ~isempty(fp)
     254            fp = [fp ':'];
     255        end
     256        fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
     257    end
     258    user_string('gs_font_path', fp);
    181259end
    182 % Create the path
    183 % Start with the default path
    184 fp = getenv('GS_FONTPATH');
    185 % Add on the typical directories for a given OS
    186 if ispc
    187     if ~isempty(fp)
    188         fp = [fp ';'];
    189     end
    190     fp = [fp getenv('WINDIR') filesep 'Fonts'];
    191 else
    192     if ~isempty(fp)
    193         fp = [fp ':'];
    194     end
    195     fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
    196 end
    197 user_string('gs_font_path', fp);
    198 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/print2eps.m

    r19993 r21757  
     1function print2eps(name, fig, export_options, varargin)
    12%PRINT2EPS  Prints figures to eps with improved line styles
    23%
     
    45%   print2eps filename
    56%   print2eps(filename, fig_handle)
    6 %   print2eps(filename, fig_handle, bb_padding)
    7 %   print2eps(filename, fig_handle, bb_padding, options)
     7%   print2eps(filename, fig_handle, export_options)
     8%   print2eps(filename, fig_handle, export_options, print_options)
    89%
    910% This function saves a figure as an eps file, with two improvements over
    1011% MATLAB's print command. First, it improves the line style, making dashed
    11 % lines more like those on screen and giving grid lines their own dotted
    12 % style. Secondly, it substitutes original font names back into the eps
    13 % file, where these have been changed by MATLAB, for up to 11 different
    14 % fonts.
     12% lines more like those on screen and giving grid lines a dotted line style.
     13% Secondly, it substitutes original font names back into the eps file,
     14% where these have been changed by MATLAB, for up to 11 different fonts.
    1515%
    1616%IN:
     
    2020%              not specified, the figure is saved in the current directory.
    2121%   fig_handle - The handle of the figure to be saved. Default: gcf().
    22 %   bb_padding - Scalar value of amount of padding to add to border around
    23 %                the figure, in points. Can be negative as well as
    24 %                positive. Default: 0.
    25 %   options - Additional parameter strings to be passed to print.
    26 
    27 % Copyright (C) Oliver Woodford 2008-2014
     22%   export_options - array or struct of optional scalar values:
     23%       bb_padding - Scalar value of amount of padding to add to border around
     24%                    the cropped image, in points (if >1) or percent (if <1).
     25%                    Can be negative as well as positive; Default: 0
     26%       crop       - Cropping flag. Deafult: 0
     27%       fontswap   - Whether to swap non-default fonts in figure. Default: true
     28%       renderer   - Renderer used to generate bounding-box. Default: 'opengl'
     29%                    (available only via the struct alternative)
     30%       crop_amounts - 4-element vector of crop amounts: [top,right,bottom,left]
     31%                    (available only via the struct alternative)
     32%   print_options - Additional parameter strings to be passed to the print command
     33
     34%{
     35% Copyright (C) Oliver Woodford 2008-2014, Yair Altman 2015-
    2836
    2937% The idea of editing the EPS file to change line styles comes from Jiro
     
    3139% The idea of changing dash length with line width came from comments on
    3240% fex id: 5743, but the implementation is mine :)
    33 
    34 % 14/11/2011: Fix a MATLAB bug rendering black or white text incorrectly.
    35 %             Thanks to Mathieu Morlighem for reporting the issue and
    36 %             obtaining a fix from TMW.
     41%}
     42%{
     43% 14/11/11: Fix a MATLAB bug rendering black or white text incorrectly.
     44%           Thanks to Mathieu Morlighem for reporting the issue and
     45%           obtaining a fix from TMW.
    3746% 08/12/11: Added ability to correct fonts. Several people have requested
    3847%           this at one time or another, and also pointed me to printeps
     
    5766%           issue.
    5867% 13/08/13: Fix MATLAB feature of not exporting white lines correctly.
    59 %           Thanks to Sebastian Heßlinger for reporting it.
    60 
    61 function print2eps(name, fig, bb_padding, varargin)
    62 options = {'-depsc2'};
    63 if nargin > 3
    64     options = [options varargin];
    65 elseif nargin < 3
    66     bb_padding = 0;
    67     if nargin < 2
    68         fig = gcf();
    69     end
     68%           Thanks to Sebastian Hesslinger for reporting it.
     69% 24/02/15: Fix for Matlab R2014b bug (issue #31): LineWidths<0.75 are not
     70%           set in the EPS (default line width is used)
     71% 25/02/15: Fixed issue #32: BoundingBox problem caused uncropped EPS/PDF files
     72% 05/03/15: Fixed issue #43: Inability to perform EPS file post-processing
     73% 06/03/15: Improved image padding & cropping thanks to Oscar Hartogensis
     74% 21/03/15: Fixed edge-case of missing handles having a 'FontName' property
     75% 26/03/15: Attempt to fix issue #45: white lines in subplots do not print correctly
     76% 27/03/15: Attempt to fix issue #44: white artifact lines appearing in patch exports
     77% 30/03/15: Fixed issue #52: improved performance on HG2 (R2014b+)
     78% 09/04/15: Comment blocks consolidation and minor code cleanup (no real code change)
     79% 12/04/15: Fixed issue #56: bad cropping
     80% 14/04/15: Workaround for issue #45: lines in image subplots are exported in invalid color
     81% 07/07/15: Added option to avoid font-swapping in EPS/PDF
     82% 07/07/15: Fixed issue #83: use numeric handles in HG1
     83% 22/07/15: Fixed issue #91 (thanks to Carlos Moffat)
     84% 28/09/15: Fixed issue #108 (thanks to JacobD10)
     85% 01/11/15: Fixed issue #112: optional renderer for bounding-box computation (thanks to Jesús Pestana Puerta)
     86% 21/02/16: Enabled specifying non-automated crop amounts
     87% 22/02/16: Better support + backward compatibility for transparency (issue #108)
     88% 10/06/16: Fixed issue #159: text handles get cleared by Matlab in the print() command
     89% 12/06/16: Improved the fix for issue #159 (in the previous commit)
     90% 12/06/16: Fixed issue #158: transparent patch color in PDF/EPS
     91%}
     92
     93    options = {'-loose'};
     94    if nargin > 3
     95        options = [options varargin];
     96    elseif nargin < 3
     97        export_options = 0;
     98        if nargin < 2
     99            fig = gcf();
     100        end
     101    end
     102
     103    % Retrieve padding, crop & font-swap values
     104    crop_amounts = nan(1,4);  % auto-crop all 4 sides by default
     105    if isstruct(export_options)
     106        try fontswap     = export_options.fontswap;     catch, fontswap = true;     end
     107        try bb_crop      = export_options.crop;         catch, bb_crop = 0;         end
     108        try crop_amounts = export_options.crop_amounts; catch,                      end
     109        try bb_padding   = export_options.bb_padding;   catch, bb_padding = 0;      end
     110        try renderer     = export_options.rendererStr;  catch, renderer = 'opengl'; end  % fix for issue #110
     111        if renderer(1)~='-',  renderer = ['-' renderer];  end
     112    else
     113        if numel(export_options) > 2  % font-swapping
     114            fontswap = export_options(3);
     115        else
     116            fontswap = true;
     117        end
     118        if numel(export_options) > 1  % cropping
     119            bb_crop = export_options(2);
     120        else
     121            bb_crop = 0;  % scalar value, so use default bb_crop value of 0
     122        end
     123        if numel(export_options) > 0  % padding
     124            bb_padding = export_options(1);
     125        else
     126            bb_padding = 0;
     127        end
     128        renderer = '-opengl';
     129    end
     130
     131    % Construct the filename
     132    if numel(name) < 5 || ~strcmpi(name(end-3:end), '.eps')
     133        name = [name '.eps']; % Add the missing extension
     134    end
     135
     136    % Set paper size
     137    old_pos_mode = get(fig, 'PaperPositionMode');
     138    old_orientation = get(fig, 'PaperOrientation');
     139    set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
     140
     141    % Find all the used fonts in the figure
     142    font_handles = findall(fig, '-property', 'FontName');
     143    fonts = get(font_handles, 'FontName');
     144    if isempty(fonts)
     145        fonts = {};
     146    elseif ~iscell(fonts)
     147        fonts = {fonts};
     148    end
     149
     150    % Map supported font aliases onto the correct name
     151    fontsl = lower(fonts);
     152    for a = 1:numel(fonts)
     153        f = fontsl{a};
     154        f(f==' ') = [];
     155        switch f
     156            case {'times', 'timesnewroman', 'times-roman'}
     157                fontsl{a} = 'times-roman';
     158            case {'arial', 'helvetica'}
     159                fontsl{a} = 'helvetica';
     160            case {'newcenturyschoolbook', 'newcenturyschlbk'}
     161                fontsl{a} = 'newcenturyschlbk';
     162            otherwise
     163        end
     164    end
     165    fontslu = unique(fontsl);
     166
     167    % Determine the font swap table
     168    if fontswap
     169        matlab_fonts = {'Helvetica', 'Times-Roman', 'Palatino', 'Bookman', 'Helvetica-Narrow', 'Symbol', ...
     170                        'AvantGarde', 'NewCenturySchlbk', 'Courier', 'ZapfChancery', 'ZapfDingbats'};
     171        matlab_fontsl = lower(matlab_fonts);
     172        require_swap = find(~ismember(fontslu, matlab_fontsl));
     173        unused_fonts = find(~ismember(matlab_fontsl, fontslu));
     174        font_swap = cell(3, min(numel(require_swap), numel(unused_fonts)));
     175        fonts_new = fonts;
     176        for a = 1:size(font_swap, 2)
     177            font_swap{1,a} = find(strcmp(fontslu{require_swap(a)}, fontsl));
     178            font_swap{2,a} = matlab_fonts{unused_fonts(a)};
     179            font_swap{3,a} = fonts{font_swap{1,a}(1)};
     180            fonts_new(font_swap{1,a}) = font_swap(2,a);
     181        end
     182    else
     183        font_swap = [];
     184    end
     185
     186    % Swap the fonts
     187    if ~isempty(font_swap)
     188        fonts_size = get(font_handles, 'FontSize');
     189        if iscell(fonts_size)
     190            fonts_size = cell2mat(fonts_size);
     191        end
     192        M = false(size(font_handles));
     193
     194        % Loop because some changes may not stick first time, due to listeners
     195        c = 0;
     196        update = zeros(1000, 1);
     197        for b = 1:10 % Limit number of loops to avoid infinite loop case
     198            for a = 1:numel(M)
     199                M(a) = ~isequal(get(font_handles(a), 'FontName'), fonts_new{a}) || ~isequal(get(font_handles(a), 'FontSize'), fonts_size(a));
     200                if M(a)
     201                    set(font_handles(a), 'FontName', fonts_new{a}, 'FontSize', fonts_size(a));
     202                    c = c + 1;
     203                    update(c) = a;
     204                end
     205            end
     206            if ~any(M)
     207                break;
     208            end
     209        end
     210
     211        % Compute the order to revert fonts later, without the need of a loop
     212        [update, M] = unique(update(1:c));
     213        [M, M] = sort(M);
     214        update = reshape(update(M), 1, []);
     215    end
     216
     217    % MATLAB bug fix - black and white text can come out inverted sometimes
     218    % Find the white and black text
     219    black_text_handles = findall(fig, 'Type', 'text', 'Color', [0 0 0]);
     220    white_text_handles = findall(fig, 'Type', 'text', 'Color', [1 1 1]);
     221    % Set the font colors slightly off their correct values
     222    set(black_text_handles, 'Color', [0 0 0] + eps);
     223    set(white_text_handles, 'Color', [1 1 1] - eps);
     224
     225    % MATLAB bug fix - white lines can come out funny sometimes
     226    % Find the white lines
     227    white_line_handles = findall(fig, 'Type', 'line', 'Color', [1 1 1]);
     228    % Set the line color slightly off white
     229    set(white_line_handles, 'Color', [1 1 1] - 0.00001);
     230
     231    % Workaround for issue #45: lines in image subplots are exported in invalid color
     232    % In this case the -depsc driver solves the problem, but then all the other workarounds
     233    % below (for all the other issues) will fail, so it's better to let the user decide by
     234    % just issuing a warning and accepting the '-depsc' input parameter
     235    epsLevel2 = ~any(strcmpi(options,'-depsc'));
     236    if epsLevel2
     237        % Use -depsc2 (EPS color level-2) if -depsc (EPS color level-3) was not specifically requested
     238        options{end+1} = '-depsc2';
     239        % Issue a warning if multiple images & lines were found in the figure, and HG1 with painters renderer is used
     240        isPainters = any(strcmpi(options,'-painters'));
     241        if isPainters && ~using_hg2 && numel(findall(fig,'Type','image'))>1 && ~isempty(findall(fig,'Type','line'))
     242            warning('YMA:export_fig:issue45', ...
     243                    ['Multiple images & lines detected. In such cases, the lines might \n' ...
     244                     'appear with an invalid color due to an internal MATLAB bug (fixed in R2014b). \n' ...
     245                     'Possible workaround: add a ''-depsc'' or ''-opengl'' parameter to the export_fig command.']);
     246        end
     247    end
     248
     249    % Fix issue #83: use numeric handles in HG1
     250    if ~using_hg2(fig),  fig = double(fig);  end
     251
     252    % Workaround for when transparency is lost through conversion fig>EPS>PDF (issue #108)
     253    % Replace transparent patch RGB values with an ID value (rare chance that ID color is being used already)
     254    if using_hg2
     255        origAlphaColors = eps_maintainAlpha(fig);
     256    end
     257
     258    % Print to eps file
     259    print(fig, options{:}, name);
     260
     261    % Do post-processing on the eps file
     262    try
     263        % Read the EPS file into memory
     264        fstrm = read_write_entire_textfile(name);
     265    catch
     266        fstrm = '';
     267    end
     268
     269    % Restore colors for transparent patches/lines and apply the
     270    % setopacityalpha setting in the EPS file (issue #108)
     271    if using_hg2
     272        [~,fstrm,foundFlags] = eps_maintainAlpha(fig, fstrm, origAlphaColors);
     273
     274        % If some of the transparencies were not found in the EPS file, then rerun the
     275        % export with only the found transparencies modified (backward compatibility)
     276        if ~isempty(fstrm) && ~all(foundFlags)
     277            foundIdx = find(foundFlags);
     278            for objIdx = 1 : sum(foundFlags)
     279                colorsIdx = foundIdx(objIdx);
     280                colorsData = origAlphaColors{colorsIdx};
     281                hObj     = colorsData{1};
     282                propName = colorsData{2};
     283                newColor = colorsData{4};
     284                hObj.(propName).ColorData = newColor;
     285            end
     286            delete(name);
     287            print(fig, options{:}, name);
     288            fstrm = read_write_entire_textfile(name);
     289            [~,fstrm] = eps_maintainAlpha(fig, fstrm, origAlphaColors(foundFlags));
     290        end
     291    end
     292
     293    % Fix for Matlab R2014b bug (issue #31): LineWidths<0.75 are not set in the EPS (default line width is used)
     294    try
     295        if ~isempty(fstrm) && using_hg2(fig)
     296            % Convert miter joins to line joins
     297            %fstrm = regexprep(fstrm, '\n10.0 ML\n', '\n1 LJ\n');
     298            % This is faster (the original regexprep could take many seconds when the axes contains many lines):
     299            fstrm = strrep(fstrm, sprintf('\n10.0 ML\n'), sprintf('\n1 LJ\n'));
     300
     301            % In HG2, grid lines and axes Ruler Axles have a default LineWidth of 0.5 => replace en-bulk (assume that 1.0 LineWidth = 1.333 LW)
     302            %   hAxes=gca; hAxes.YGridHandle.LineWidth, hAxes.YRuler.Axle.LineWidth
     303            %fstrm = regexprep(fstrm, '(GC\n2 setlinecap\n1 LJ)\nN', '$1\n0.667 LW\nN');
     304            % This is faster:
     305            fstrm = strrep(fstrm, sprintf('GC\n2 setlinecap\n1 LJ\nN'), sprintf('GC\n2 setlinecap\n1 LJ\n0.667 LW\nN'));
     306
     307            % This is more accurate but *MUCH* slower (issue #52)
     308            %{
     309            % Modify all thin lines in the figure to have 10x LineWidths
     310            hLines = findall(fig,'Type','line');
     311            hThinLines = [];
     312            for lineIdx = 1 : numel(hLines)
     313                thisLine = hLines(lineIdx);
     314                if thisLine.LineWidth < 0.75 && strcmpi(thisLine.Visible,'on')
     315                    hThinLines(end+1) = thisLine; %#ok<AGROW>
     316                    thisLine.LineWidth = thisLine.LineWidth * 10;
     317                end
     318            end
     319
     320            % If any thin lines were found
     321            if ~isempty(hThinLines)
     322                % Prepare an EPS with large-enough line widths
     323                print(fig, options{:}, name);
     324                % Restore the original LineWidths in the figure
     325                for lineIdx = 1 : numel(hThinLines)
     326                    thisLine = handle(hThinLines(lineIdx));
     327                    thisLine.LineWidth = thisLine.LineWidth / 10;
     328                end
     329
     330                % Compare the original and the new EPS files and correct the original stream's LineWidths
     331                fstrm_new = read_write_entire_textfile(name);
     332                idx = 500;  % skip heading with its possibly-different timestamp
     333                markerStr = sprintf('10.0 ML\nN');
     334                markerLen = length(markerStr);
     335                while ~isempty(idx) && idx < length(fstrm)
     336                    lastIdx = min(length(fstrm), length(fstrm_new));
     337                    delta = fstrm(idx+1:lastIdx) - fstrm_new(idx+1:lastIdx);
     338                    idx = idx + find(delta,1);
     339                    if ~isempty(idx) && ...
     340                            isequal(fstrm(idx-markerLen+1:idx), markerStr) && ...
     341                            ~isempty(regexp(fstrm_new(idx-markerLen+1:idx+12),'10.0 ML\n[\d\.]+ LW\nN')) %#ok<RGXP1>
     342                        value = str2double(regexprep(fstrm_new(idx:idx+12),' .*',''));
     343                        if isnan(value), break; end  % something's wrong... - bail out
     344                        newStr = sprintf('%0.3f LW\n',value/10);
     345                        fstrm = [fstrm(1:idx-1) newStr fstrm(idx:end)];
     346                        idx = idx + 12;
     347                    else
     348                        break;
     349                    end
     350                end
     351            end
     352            %}
     353
     354            % This is much faster although less accurate: fix all non-gray lines to have a LineWidth of 0.75 (=1 LW)
     355            % Note: This will give incorrect LineWidth of 075 for lines having LineWidth<0.75, as well as for non-gray grid-lines (if present)
     356            %       However, in practice these edge-cases are very rare indeed, and the difference in LineWidth should not be noticeable
     357            %fstrm = regexprep(fstrm, '([CR]C\n2 setlinecap\n1 LJ)\nN', '$1\n1 LW\nN');
     358            % This is faster (the original regexprep could take many seconds when the axes contains many lines):
     359            fstrm = strrep(fstrm, sprintf('\n2 setlinecap\n1 LJ\nN'), sprintf('\n2 setlinecap\n1 LJ\n1 LW\nN'));
     360        end
     361    catch err
     362        fprintf(2, 'Error fixing LineWidths in EPS file: %s\n at %s:%d\n', err.message, err.stack(1).file, err.stack(1).line);
     363    end
     364
     365    % Reset the font and line colors
     366    try
     367        set(black_text_handles, 'Color', [0 0 0]);
     368        set(white_text_handles, 'Color', [1 1 1]);
     369    catch
     370        % Fix issue #159: redo findall() '*text_handles'
     371        black_text_handles = findall(fig, 'Type', 'text', 'Color', [0 0 0]+eps);
     372        white_text_handles = findall(fig, 'Type', 'text', 'Color', [1 1 1]-eps);
     373        set(black_text_handles, 'Color', [0 0 0]);
     374        set(white_text_handles, 'Color', [1 1 1]);
     375    end
     376    set(white_line_handles, 'Color', [1 1 1]);
     377
     378    % Reset paper size
     379    set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
     380
     381    % Reset the font names in the figure
     382    if ~isempty(font_swap)
     383        for a = update
     384            set(font_handles(a), 'FontName', fonts{a}, 'FontSize', fonts_size(a));
     385        end
     386    end
     387
     388    % Bail out if EPS post-processing is not possible
     389    if isempty(fstrm)
     390        warning('Loading EPS file failed, so unable to perform post-processing. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.');
     391        return
     392    end
     393
     394    % Replace the font names
     395    if ~isempty(font_swap)
     396        for a = 1:size(font_swap, 2)
     397            %fstrm = regexprep(fstrm, [font_swap{1,a} '-?[a-zA-Z]*\>'], font_swap{3,a}(~isspace(font_swap{3,a})));
     398            fstrm = regexprep(fstrm, font_swap{2,a}, font_swap{3,a}(~isspace(font_swap{3,a})));
     399        end
     400    end
     401
     402    % Move the bounding box to the top of the file (HG2 only), or fix the line styles (HG1 only)
     403    if using_hg2(fig)
     404        % Move the bounding box to the top of the file (HG2 only)
     405        [s, e] = regexp(fstrm, '%%BoundingBox: [^%]*%%');
     406        if numel(s) == 2
     407            fstrm = fstrm([1:s(1)-1 s(2):e(2)-2 e(1)-1:s(2)-1 e(2)-1:end]);
     408        end
     409    else
     410        % Fix the line styles (HG1 only)
     411        fstrm = fix_lines(fstrm);
     412    end
     413
     414    % Apply the bounding box padding & cropping, replacing Matlab's print()'s bounding box
     415    if bb_crop
     416        % Calculate a new bounding box based on a bitmap print using crop_border.m
     417        % 1. Determine the Matlab BoundingBox and PageBoundingBox
     418        [s,e] = regexp(fstrm, '%%BoundingBox: [^%]*%%'); % location BB in eps file
     419        if numel(s)==2, s=s(2); e=e(2); end
     420        aa = fstrm(s+15:e-3); % dimensions bb - STEP1
     421        bb_matlab = cell2mat(textscan(aa,'%f32%f32%f32%f32'));  % dimensions bb - STEP2
     422
     423        [s,e] = regexp(fstrm, '%%PageBoundingBox: [^%]*%%'); % location bb in eps file
     424        if numel(s)==2, s=s(2); e=e(2); end
     425        aa = fstrm(s+19:e-3); % dimensions bb - STEP1
     426        pagebb_matlab = cell2mat(textscan(aa,'%f32%f32%f32%f32'));  % dimensions bb - STEP2
     427
     428        % 2. Create a bitmap image and use crop_borders to create the relative
     429        %    bb with respect to the PageBoundingBox
     430        [A, bcol] = print2array(fig, 1, renderer);
     431        [aa, aa, aa, bb_rel] = crop_borders(A, bcol, bb_padding, crop_amounts);
     432
     433        % 3. Calculate the new Bounding Box
     434        pagew = pagebb_matlab(3)-pagebb_matlab(1);
     435        pageh = pagebb_matlab(4)-pagebb_matlab(2);
     436        %bb_new = [pagebb_matlab(1)+pagew*bb_rel(1) pagebb_matlab(2)+pageh*bb_rel(2) ...
     437        %          pagebb_matlab(1)+pagew*bb_rel(3) pagebb_matlab(2)+pageh*bb_rel(4)];
     438        bb_new = pagebb_matlab([1,2,1,2]) + [pagew,pageh,pagew,pageh].*bb_rel;  % clearer
     439        bb_offset = (bb_new-bb_matlab) + [-1,-1,1,1];  % 1px margin so that cropping is not TOO tight
     440
     441        % Apply the bounding box padding
     442        if bb_padding
     443            if abs(bb_padding)<1
     444                bb_padding = round((mean([bb_new(3)-bb_new(1) bb_new(4)-bb_new(2)])*bb_padding)/0.5)*0.5; % ADJUST BB_PADDING
     445            end
     446            add_padding = @(n1, n2, n3, n4) sprintf(' %d', str2double({n1, n2, n3, n4}) + [-bb_padding -bb_padding bb_padding bb_padding] + bb_offset);
     447        else
     448            add_padding = @(n1, n2, n3, n4) sprintf(' %d', str2double({n1, n2, n3, n4}) + bb_offset); % fix small but noticeable bounding box shift
     449        end
     450        fstrm = regexprep(fstrm, '%%BoundingBox:[ ]+([-]?\d+)[ ]+([-]?\d+)[ ]+([-]?\d+)[ ]+([-]?\d+)', '%%BoundingBox:${add_padding($1, $2, $3, $4)}');
     451    end
     452
     453    % Fix issue #44: white artifact lines appearing in patch exports
     454    % Note: the problem is due to the fact that Matlab's print() function exports patches
     455    %       as a combination of filled triangles, and a white line appears where the triangles touch
     456    % In the workaround below, we will modify such dual-triangles into a filled rectangle.
     457    % We are careful to only modify regexps that exactly match specific patterns - it's better to not
     458    % correct some white-line artifacts than to change the geometry of a patch, or to corrupt the EPS.
     459    %   e.g.: '0 -450 937 0 0 450 3 MP PP 937 0 0 -450 0 450 3 MP PP' => '0 -450 937 0 0 450 0 0 4 MP'
     460    fstrm = regexprep(fstrm, '\n([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) 3 MP\nPP\n\2 \1 \3 3 MP\nPP\n','\n$1 $2 $3 0 0 4 MP\nPP\n');
     461    fstrm = regexprep(fstrm, '\n([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) 3 MP\nPP\n\2 \3 \1 3 MP\nPP\n','\n$1 $2 $3 0 0 4 MP\nPP\n');
     462    fstrm = regexprep(fstrm, '\n([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) 3 MP\nPP\n\3 \1 \2 3 MP\nPP\n','\n$1 $2 $3 0 0 4 MP\nPP\n');
     463    fstrm = regexprep(fstrm, '\n([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) 3 MP\nPP\n\3 \2 \1 3 MP\nPP\n','\n$1 $2 $3 0 0 4 MP\nPP\n');
     464
     465    % Write out the fixed eps file
     466    read_write_entire_textfile(name, fstrm);
    70467end
    71 % Construct the filename
    72 if numel(name) < 5 || ~strcmpi(name(end-3:end), '.eps')
    73     name = [name '.eps']; % Add the missing extension
     468
     469function [StoredColors, fstrm, foundFlags] = eps_maintainAlpha(fig, fstrm, StoredColors)
     470    if nargin == 1  % in: convert transparency in Matlab figure into unique RGB colors
     471        hObjs = findall(fig); %findobj(fig,'Type','Area');
     472        StoredColors = {};
     473        propNames = {'Face','Edge'};
     474        for objIdx = 1:length(hObjs)
     475            hObj = hObjs(objIdx);
     476            for propIdx = 1 : numel(propNames)
     477                try
     478                    propName = propNames{propIdx};
     479                    if strcmp(hObj.(propName).ColorType, 'truecoloralpha')
     480                        nColors = length(StoredColors);
     481                        oldColor = hObj.(propName).ColorData;
     482                        newColor = uint8([101; 102+floor(nColors/255); mod(nColors,255); 255]);
     483                        StoredColors{end+1} = {hObj, propName, oldColor, newColor};
     484                        hObj.(propName).ColorData = newColor;
     485                    end
     486                catch
     487                    % Never mind - ignore (either doesn't have the property or cannot change it)
     488                end
     489            end
     490        end
     491    else  % restore transparency in Matlab figure by converting back from the unique RGBs
     492        %Find the transparent patches
     493        wasError = false;
     494        nColors = length(StoredColors);
     495        foundFlags = false(1,nColors);
     496        for objIdx = 1 : nColors
     497            colorsData = StoredColors{objIdx};
     498            hObj      = colorsData{1};
     499            propName  = colorsData{2};
     500            origColor = colorsData{3};
     501            newColor  = colorsData{4};
     502            try
     503                %Restore the EPS files patch color
     504                colorID   = num2str(round(double(newColor(1:3)') /255,3),'%.3g %.3g %.3g'); %ID for searching
     505                origRGB   = num2str(round(double(origColor(1:3)')/255,3),'%.3g %.3g %.3g'); %Replace with original color
     506                origAlpha = num2str(round(double(origColor(end)) /255,3),'%.3g'); %Convert alpha value for EPS
     507
     508                %Find and replace the RGBA values within the EPS text fstrm
     509                if strcmpi(propName,'Face')
     510                    oldStr = sprintf(['\n' colorID ' RC\nN\n']);
     511                    newStr = sprintf(['\n' origRGB ' RC\n' origAlpha ' .setopacityalpha true\nN\n']);
     512                else  %'Edge'
     513                    oldStr = sprintf(['\n' colorID ' RC\n1 LJ\n']);
     514                    newStr = sprintf(['\n' origRGB ' RC\n' origAlpha ' .setopacityalpha true\n']);
     515                end
     516                foundFlags(objIdx) = ~isempty(strfind(fstrm, oldStr));
     517                fstrm = strrep(fstrm, oldStr, newStr);
     518
     519                %Restore the figure object's original color
     520                hObj.(propName).ColorData = origColor;
     521            catch err
     522                % something is wrong - cannot restore transparent color...
     523                if ~wasError
     524                    fprintf(2, 'Error maintaining transparency in EPS file: %s\n at %s:%d\n', err.message, err.stack(1).file, err.stack(1).line);
     525                    wasError = true;
     526                end
     527            end
     528        end
     529    end
    74530end
    75 % Set paper size
    76 old_pos_mode = get(fig, 'PaperPositionMode');
    77 old_orientation = get(fig, 'PaperOrientation');
    78 set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
    79 % Find all the used fonts in the figure
    80 font_handles = findall(fig, '-property', 'FontName');
    81 fonts = get(font_handles, 'FontName');
    82 if ~iscell(fonts)
    83     fonts = {fonts};
    84 end
    85 % Map supported font aliases onto the correct name
    86 fontsl = lower(fonts);
    87 for a = 1:numel(fonts)
    88     f = fontsl{a};
    89     f(f==' ') = [];
    90     switch f
    91         case {'times', 'timesnewroman', 'times-roman'}
    92             fontsl{a} = 'times-roman';
    93         case {'arial', 'helvetica'}
    94             fontsl{a} = 'helvetica';
    95         case {'newcenturyschoolbook', 'newcenturyschlbk'}
    96             fontsl{a} = 'newcenturyschlbk';
    97         otherwise
    98     end
    99 end
    100 fontslu = unique(fontsl);
    101 % Determine the font swap table
    102 matlab_fonts = {'Helvetica', 'Times-Roman', 'Palatino', 'Bookman', 'Helvetica-Narrow', 'Symbol', ...
    103                 'AvantGarde', 'NewCenturySchlbk', 'Courier', 'ZapfChancery', 'ZapfDingbats'};
    104 matlab_fontsl = lower(matlab_fonts);
    105 require_swap = find(~ismember(fontslu, matlab_fontsl));
    106 unused_fonts = find(~ismember(matlab_fontsl, fontslu));
    107 font_swap = cell(3, min(numel(require_swap), numel(unused_fonts)));
    108 fonts_new = fonts;
    109 for a = 1:size(font_swap, 2)
    110     font_swap{1,a} = find(strcmp(fontslu{require_swap(a)}, fontsl));
    111     font_swap{2,a} = matlab_fonts{unused_fonts(a)};
    112     font_swap{3,a} = fonts{font_swap{1,a}(1)};
    113     fonts_new(font_swap{1,a}) = {font_swap{2,a}};
    114 end
    115 % Swap the fonts
    116 if ~isempty(font_swap)
    117     fonts_size = get(font_handles, 'FontSize');
    118     if iscell(fonts_size)
    119         fonts_size = cell2mat(fonts_size);
    120     end
    121     M = false(size(font_handles));
    122     % Loop because some changes may not stick first time, due to listeners
    123     c = 0;
    124     update = zeros(1000, 1);
    125     for b = 1:10 % Limit number of loops to avoid infinite loop case
    126         for a = 1:numel(M)
    127             M(a) = ~isequal(get(font_handles(a), 'FontName'), fonts_new{a}) || ~isequal(get(font_handles(a), 'FontSize'), fonts_size(a));
    128             if M(a)
    129                 set(font_handles(a), 'FontName', fonts_new{a}, 'FontSize', fonts_size(a));
    130                 c = c + 1;
    131                 update(c) = a;
    132             end
    133         end
    134         if ~any(M)
    135             break;
    136         end
    137     end
    138     % Compute the order to revert fonts later, without the need of a loop
    139     [update, M] = unique(update(1:c));
    140     [M, M] = sort(M);
    141     update = reshape(update(M), 1, []);
    142 end
    143 % MATLAB bug fix - black and white text can come out inverted sometimes
    144 % Find the white and black text
    145 white_text_handles = findobj(fig, 'Type', 'text');
    146 M = get(white_text_handles, 'Color');
    147 if iscell(M)
    148     M = cell2mat(M);
    149 end
    150 M = sum(M, 2);
    151 black_text_handles = white_text_handles(M == 0);
    152 white_text_handles = white_text_handles(M == 3);
    153 % Set the font colors slightly off their correct values
    154 set(black_text_handles, 'Color', [0 0 0] + eps);
    155 set(white_text_handles, 'Color', [1 1 1] - eps);
    156 % MATLAB bug fix - white lines can come out funny sometimes
    157 % Find the white lines
    158 white_line_handles = findobj(fig, 'Type', 'line');
    159 M = get(white_line_handles, 'Color');
    160 if iscell(M)
    161     M = cell2mat(M);
    162 end
    163 white_line_handles = white_line_handles(sum(M, 2) == 3);
    164 % Set the line color slightly off white
    165 set(white_line_handles, 'Color', [1 1 1] - 0.00001);
    166 % Print to eps file
    167 print(fig, options{:}, name);
    168 % Reset the font and line colors
    169 set(black_text_handles, 'Color', [0 0 0]);
    170 set(white_text_handles, 'Color', [1 1 1]);
    171 set(white_line_handles, 'Color', [1 1 1]);
    172 % Reset paper size
    173 set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
    174 % Reset the font names in the figure
    175 if ~isempty(font_swap)
    176     for a = update
    177         set(font_handles(a), 'FontName', fonts{a}, 'FontSize', fonts_size(a));
    178     end
    179 end
    180 % Do post-processing on the eps file
    181 try
    182     fstrm = read_write_entire_textfile(name);
    183 catch
    184     warning('Loading EPS file failed, so unable to perform post-processing. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.');
    185     return
    186 end
    187 % Replace the font names
    188 if ~isempty(font_swap)
    189     for a = 1:size(font_swap, 2)
    190         %fstrm = regexprep(fstrm, [font_swap{1,a} '-?[a-zA-Z]*\>'], font_swap{3,a}(~isspace(font_swap{3,a})));
    191         fstrm = regexprep(fstrm, font_swap{2,a}, font_swap{3,a}(~isspace(font_swap{3,a})));
    192     end
    193 end
    194 if using_hg2(fig)
    195     % Convert miter joins to line joins
    196     fstrm = regexprep(fstrm, '10.0 ML\n', '1 LJ\n');
    197     % Move the bounding box to the top of the file
    198     [s, e] = regexp(fstrm, '%%BoundingBox: [\w\s()]*%%');
    199     if numel(s) == 2
    200         fstrm = fstrm([1:s(1)-1 s(2):e(2)-2 e(1)-1:s(2)-1 e(2)-1:end]);
    201     end
    202 else
    203     % Fix the line styles
    204     fstrm = fix_lines(fstrm);
    205 end
    206 % Apply the bounding box padding
    207 if bb_padding
    208     add_padding = @(n1, n2, n3, n4) sprintf(' %d', str2double({n1, n2, n3, n4}) + [-bb_padding -bb_padding bb_padding bb_padding]);
    209     fstrm = regexprep(fstrm, '%%BoundingBox:[ ]+([-]?\d+)[ ]+([-]?\d+)[ ]+([-]?\d+)[ ]+([-]?\d+)', '%%BoundingBox:${add_padding($1, $2, $3, $4)}');
    210 end
    211 % Write out the fixed eps file
    212 read_write_entire_textfile(name, fstrm);
    213 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/user_string.m

    r19993 r21757  
     1function string = user_string(string_name, string)
    12%USER_STRING  Get/set a user specific string
    23%
    34% Examples:
    4 %   string = user_string(string_name)
    5 %   saved = user_string(string_name, new_string)
     5%   string  = user_string(string_name)
     6%   isSaved = user_string(string_name, new_string)
    67%
    78% Function to get and set a string in a system or user specific file. This
    89% enables, for example, system specific paths to binaries to be saved.
    910%
     11% The specified string will be saved in a file named <string_name>.txt,
     12% either in a subfolder named .ignore under this file's folder, or in the
     13% user's prefdir folder (in case this file's folder is non-writable).
     14%
    1015% IN:
    11 %   string_name - String containing the name of the string required. The
    12 %                 string is extracted from a file called (string_name).txt,
    13 %                 stored in the same directory as user_string.m.
    14 %   new_string - The new string to be saved under the name given by
    15 %                string_name.
     16%   string_name - String containing the name of the string required, which
     17%                 sets the filename storing the string: <string_name>.txt
     18%   new_string  - The new string to be saved in the <string_name>.txt file
    1619%
    1720% OUT:
    18 %   string - The currently saved string. Default: ''.
    19 %   saved - Boolean indicating whether the save was succesful
     21%   string  - The currently saved string. Default: ''
     22%   isSaved - Boolean indicating whether the save was succesful
    2023
    21 % Copyright (C) Oliver Woodford 2011-2013
     24% Copyright (C) Oliver Woodford 2011-2014, Yair Altman 2015-
    2225
    2326% This method of saving paths avoids changing .m files which might be in a
     
    2831
    2932% 10/01/2013 - Access files in text, not binary mode, as latter can cause
    30 % errors. Thanks to Christian for pointing this out.
     33%              errors. Thanks to Christian for pointing this out.
     34% 29/05/2015 - Save file in prefdir if current folder is non-writable (issue #74)
    3135
    32 function string = user_string(string_name, string)
    33 if ~ischar(string_name)
    34     error('string_name must be a string.');
    35 end
    36 % Create the full filename
    37 string_name = fullfile(fileparts(mfilename('fullpath')), '.ignore', [string_name '.txt']);
    38 if nargin > 1
    39     % Set string
    40     if ~ischar(string)
    41         error('new_string must be a string.');
     36    if ~ischar(string_name)
     37        error('string_name must be a string.');
    4238    end
    43     % Make sure the save directory exists
    44     dname = fileparts(string_name);
    45     if ~exist(dname, 'dir')
    46         % Create the directory
    47         try
    48             if ~mkdir(dname)               
     39    % Create the full filename
     40    fname = [string_name '.txt'];
     41    dname = fullfile(fileparts(mfilename('fullpath')), '.ignore');
     42    file_name = fullfile(dname, fname);
     43    if nargin > 1
     44        % Set string
     45        if ~ischar(string)
     46            error('new_string must be a string.');
     47        end
     48        % Make sure the save directory exists
     49        %dname = fileparts(file_name);
     50        if ~exist(dname, 'dir')
     51            % Create the directory
     52            try
     53                if ~mkdir(dname)
     54                    string = false;
     55                    return
     56                end
     57            catch
    4958                string = false;
    5059                return
    5160            end
     61            % Make it hidden
     62            try
     63                fileattrib(dname, '+h');
     64            catch
     65            end
     66        end
     67        % Write the file
     68        fid = fopen(file_name, 'wt');
     69        if fid == -1
     70            % file cannot be created/updated - use prefdir if file does not already exist
     71            % (if file exists but is simply not writable, don't create a duplicate in prefdir)
     72            if ~exist(file_name,'file')
     73                file_name = fullfile(prefdir, fname);
     74                fid = fopen(file_name, 'wt');
     75            end
     76            if fid == -1
     77                string = false;
     78                return;
     79            end
     80        end
     81        try
     82            fprintf(fid, '%s', string);
    5283        catch
     84            fclose(fid);
    5385            string = false;
    5486            return
    5587        end
    56         % Make it hidden
    57         try
    58             fileattrib(dname, '+h');
    59         catch
     88        fclose(fid);
     89        string = true;
     90    else
     91        % Get string
     92        fid = fopen(file_name, 'rt');
     93        if fid == -1
     94            % file cannot be read, try to read the file in prefdir
     95            file_name = fullfile(prefdir, fname);
     96            fid = fopen(file_name, 'rt');
     97            if fid == -1
     98                string = '';
     99                return
     100            end
    60101        end
     102        string = fgetl(fid);
     103        fclose(fid);
    61104    end
    62     % Write the file
    63     fid = fopen(string_name, 'wt');
    64     if fid == -1
    65         string = false;
    66         return
    67     end
    68     try
    69         fprintf(fid, '%s', string);
    70     catch
    71         fclose(fid);
    72         string = false;
    73         return
    74     end
    75     fclose(fid);
    76     string = true;
    77 else
    78     % Get string
    79     fid = fopen(string_name, 'rt');
    80     if fid == -1
    81         string = '';
    82         return
    83     end
    84     string = fgetl(fid);
    85     fclose(fid);
    86105end
    87 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/export_fig/using_hg2.m

    r19993 r21757  
    1 %USING_HG2 Determine if the HG2 graphics pipeline is used
     1%USING_HG2 Determine if the HG2 graphics engine is used
    22%
    33%   tf = using_hg2(fig)
     
    77%
    88%OUT:
    9 %   tf - boolean indicating whether the HG2 graphics pipeline is being used
     9%   tf - boolean indicating whether the HG2 graphics engine is being used
    1010%        (true) or not (false).
    1111
     12% 19/06/2015 - Suppress warning in R2015b; cache result for improved performance
     13% 06/06/2016 - Fixed issue #156 (bad return value in R2016b)
     14
    1215function tf = using_hg2(fig)
    13 try
    14     tf = ~graphicsversion(fig, 'handlegraphics');
    15 catch
    16     tf = false;
     16    persistent tf_cached
     17    if isempty(tf_cached)
     18        try
     19            if nargin < 1,  fig = figure('visible','off');  end
     20            oldWarn = warning('off','MATLAB:graphicsversion:GraphicsVersionRemoval');
     21            try
     22                % This generates a [supressed] warning in R2015b:
     23                tf = ~graphicsversion(fig, 'handlegraphics');
     24            catch
     25                tf = ~verLessThan('matlab','8.4');  % =R2014b
     26            end
     27            warning(oldWarn);
     28        catch
     29            tf = false;
     30        end
     31        if nargin < 1,  delete(fig);  end
     32        tf_cached = tf;
     33    else
     34        tf = tf_cached;
     35    end
    1736end
    18 end
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/modules/install-macosx64.sh

    r16584 r21757  
    44#Some cleanup
    55rm -rf install
    6 rm -rf modules-3.2.10
     6rm -rf modules-3.2.9c
    77mkdir install
    88
    99#Download from ISSM server
    10 $ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/modules-3.2.10.tar.gz' 'modules-3.2.10.tar.gz'
     10$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/modules-3.2.9c.tar.gz' 'modules-3.2.9c.tar.gz'
    1111
    1212#Untar
    13 tar -zxvf  modules-3.2.10.tar.gz
     13tar -zxvf  modules-3.2.9c.tar.gz
    1414
    1515#Move modules into src directory
    1616
    1717#Configure modules
    18 cd modules-3.2.10
    19 sudo ./configure \
    20         --prefix=/usr/local/modules \
    21         --with-tcl-lib=$ISSM_DIR/externalpackages/tcl/install/Library/Frameworks/Tcl.framework/Versions/8.5\
    22         --with-tcl-inc=$ISSM_DIR/externalpackages/tcl/install/Library/Frameworks/Tcl.framework/Versions/8.5/Headers\
     18cd modules-3.2.9
     19./configure \
     20        --prefix=$ISSM_DIR/externalpackages/modules/install\
     21        --with-tcl-lib=$ISSM_DIR/externalpackages/tcl/install/lib\
     22        --with-tcl-inc=$ISSM_DIR/externalpackages/tcl/install/include\
    2323        --with-tcl-ver=8.5 \
    2424        --with-tclx-lib=$ISSM_DIR/externalpackages/tclx/install/lib/tclx8.4\
     
    3333#Compile and install modules
    3434if [ $# -eq 0 ]; then
    35         sudo make
     35        make
    3636else
    37         sudo make -j $1
     37        make -j $1
    3838fi
    3939sudo make install
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/mpich/install-3.0-linux64-static.sh

    r20606 r21757  
    1616rm -rf mpich-3.0.4
    1717
     18export CXXFLAGS='-fPIC'
     19export CFLAGS='-fPIC'
     20export FFLAGS='-fPIC'
     21
    1822#Configure mpich
    1923cd src
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/petsc/install-3.6-macosx64.sh

    r20424 r21757  
    1313mv petsc-3.6.3/* src/
    1414rm -rf petsc-3.6.3
     15
     16#patch
     17cat src/config/BuildSystem/config/packages/metis.py | sed -e "s/metis-5.1.0-p1/metis-5.1.0-p3/g" > TEMP
     18mv TEMP src/config/BuildSystem/config/packages/metis.py
    1519
    1620#configure
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/petsc/install-3.6-pleiades.sh

    r20424 r21757  
    2020        --PETSC_DIR="$ISSM_DIR/externalpackages/petsc/src" \
    2121        --with-cc=icc --with-cxx=icpc --with-fc=ifort --with-f77=ifort \
    22         --with-blas-lapack-dir="/nasa/intel/Compiler/2015.0.090/composer_xe_2015.0.090/mkl/" \
    23         --with-mpi-lib="/nasa/sgi/mpt/2.12r16/lib/libmpi.so" \
    24         --with-mpi-include="/nasa/sgi/mpt/2.12r16/include" \
     22        --with-blas-lapack-dir="/nasa/intel/Compiler/2016.2.181/compilers_and_libraries_2016.2.181/linux/mkl/" \
    2523        --known-mpi-shared-libraries=1 \
    2624        --with-debugging=0 \
     
    4442
    4543. /usr/share/modules/init/bash
    46 module load comp-intel/2015.0.090
    47 module load mpi-sgi/mpt.2.12r16
     44module load comp-intel/2016.2.181
     45module load mpi-sgi/mpt
    4846
    4947export PATH="$PATH:."
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/petsc/install-3.7-greenplanet.sh

    r20953 r21757  
    55
    66#Some cleanup
    7 rm -rf install petsc-3.7.2 src
     7rm -rf install petsc-3.7.6 src
    88mkdir install src
    99
    1010#Download from ISSM server
    11 $ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.2.tar.gz' 'petsc-3.7.2.tar.gz'
     11$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.6.tar.gz' 'petsc-3.7.6.tar.gz'
    1212
    1313#Untar and move petsc to install directory
    14 tar -zxvf  petsc-3.7.2.tar.gz
    15 mv petsc-3.7.2/* src/
    16 rm -rf petsc-3.7.2
     14tar -zxvf  petsc-3.7.6.tar.gz
     15mv petsc-3.7.6/* src/
     16rm -rf petsc-3.7.6
    1717
    1818#configure
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/petsc/install-3.7-hpc.sh

    r20953 r21757  
    55
    66#Some cleanup
    7 rm -rf install petsc-3.7.2 src
     7rm -rf install petsc-3.7.6 src
    88mkdir install src
    99
    1010#Download from ISSM server
    11 $ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.2.tar.gz' 'petsc-3.7.2.tar.gz'
     11$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.6.tar.gz' 'petsc-3.7.6.tar.gz'
    1212
    1313#Untar and move petsc to install directory
    14 tar -zxvf  petsc-3.7.2.tar.gz
    15 mv petsc-3.7.2/* src/
    16 rm -rf petsc-3.7.2
     14tar -zxvf  petsc-3.7.6.tar.gz
     15mv petsc-3.7.6/* src/
     16rm -rf petsc-3.7.6
    1717
    1818#configure
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/petsc/install-3.7-linux64.sh

    r20953 r21757  
    33
    44#Some cleanup
    5 rm -rf install petsc-3.7.2 src
     5rm -rf install petsc-3.7.6 src
    66mkdir install src
    77
    88#Download from ISSM server
    9 $ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.2.tar.gz' 'petsc-3.7.2.tar.gz'
     9$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.6.tar.gz' 'petsc-3.7.6.tar.gz'
    1010
    1111#Untar and move petsc to install directory
    12 tar -zxvf  petsc-3.7.2.tar.gz
    13 mv petsc-3.7.2/* src/
    14 rm -rf petsc-3.7.2
     12tar -zxvf  petsc-3.7.6.tar.gz
     13mv petsc-3.7.6/* src/
     14rm -rf petsc-3.7.6
    1515
    1616#configure
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/petsc/install-3.7-macosx64.sh

    r20953 r21757  
    33
    44#Some cleanup
    5 rm -rf install petsc-3.7.2 src
     5rm -rf install petsc-3.7.6 src
    66mkdir install src
    77
    88#Download from ISSM server
    9 $ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.2.tar.gz' 'petsc-3.7.2.tar.gz'
     9$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.6.tar.gz' 'petsc-3.7.6.tar.gz'
    1010
    1111#Untar and move petsc to install directory
    12 tar -zxvf  petsc-3.7.2.tar.gz
    13 mv petsc-3.7.2/* src/
    14 rm -rf petsc-3.7.2
     12tar -zxvf  petsc-3.7.6.tar.gz
     13mv petsc-3.7.6/* src/
     14rm -rf petsc-3.7.6
    1515
    1616#configure
    1717cd src
    1818./config/configure.py \
    19         --CXXOPTFLAGS="-g -O3" \
    20         --COPTFLAGS="-g -O3" \
    21         --FOPTFLAGS="-g -O3" \
    2219        --prefix="$ISSM_DIR/externalpackages/petsc/install" \
    2320        --with-mpi-dir="$ISSM_DIR/externalpackages/mpich/install" \
    2421        --PETSC_DIR="$ISSM_DIR/externalpackages/petsc/src" \
    25         --with-debugging=0 \
     22        --with-debugging=1 \
    2623        --with-valgrind=0 \
    2724        --with-x=0 \
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/petsc/install-3.7-pleiades.sh

    r21212 r21757  
    33
    44#Some cleanup
    5 rm -rf install petsc-3.7.2 src
     5rm -rf install petsc-3.7.6 src
    66mkdir install src
    77
    88#Download from ISSM server
    9 $ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.2.tar.gz' 'petsc-3.7.2.tar.gz'
     9$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/petsc-lite-3.7.6.tar.gz' 'petsc-3.7.6.tar.gz'
    1010
    1111#Untar and move petsc to install directory
    12 tar -zxvf  petsc-3.7.2.tar.gz
    13 mv petsc-3.7.2/* src/
    14 rm -rf petsc-3.7.2
     12tar -zxvf  petsc-3.7.6.tar.gz
     13mv petsc-3.7.6/* src/
     14rm -rf petsc-3.7.6
    1515
    1616#configure
     
    2020        --PETSC_DIR="$ISSM_DIR/externalpackages/petsc/src" \
    2121        --with-cc=icc --with-cxx=icpc --with-fc=ifort --with-f77=ifort \
    22         --with-blas-lapack-dir="/nasa/intel/Compiler/2015.0.090/composer_xe_2015.0.090/mkl/" \
    23         --with-mpi-lib="/nasa/sgi/mpt/2.12r16/lib/libmpi.so" \
    24         --with-mpi-include="/nasa/sgi/mpt/2.12r16/include" \
     22        --with-blas-lapack-dir="/nasa/intel/Compiler/2016.2.181/compilers_and_libraries_2016.2.181/linux/mkl/" \
    2523        --known-mpi-shared-libraries=1 \
    2624        --with-debugging=0 \
     
    3331        --download-parmetis=1 \
    3432        --download-mumps=1 \
    35         --download-scalapack=1 \
    36         --download-ml=1 \
    37         --download-pastix=1 \
    38         --download-ptscotch=1 \
    39         --download-hypre=1 \
    40         --download-euclid=1 \
    41         --download-spooles=1 \
    42         --download-spai=1 \
    43         --download-superlu_dist=1 \
    4433        --download-scalapack=1
    4534
     
    5342
    5443. /usr/share/modules/init/bash
    55 module load comp-intel/2015.0.090
    56 module load mpi-sgi/mpt.2.12r16
     44module load pkgsrc
     45module load comp-intel/2016.2.181
     46module load mpi-sgi/mpt
    5747
    5848export PATH="$PATH:."
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/tclx/install.sh

    r13249 r21757  
    2222./configure --prefix="$ISSM_DIR/externalpackages/tclx/install"  \
    2323                        --exec-prefix="$ISSM_DIR/externalpackages/tclx/install"  \
    24                 --with-tcl=$ISSM_DIR/externalpackages/tcl/install/Library/Frameworks/Tcl.framework
     24                --with-tcl=$ISSM_DIR/externalpackages/tcl/install/lib
    2525
    2626#Compile and install tclx
  • issm/branches/trunk-larour-NatGeoScience2016/externalpackages/valgrind/issm.supp

    r18846 r21757  
    3939   fun:_ZNSs12_S_constructIPKcEEPcT_S3_RKSaIcESt20forward_iterator_tag
    4040   fun:_ZNSsC1EPKcRKSaIcE
     41}
     42{
     43        <Libgfortranronne>
     44          Memcheck:Cond
     45          obj:/usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0
     46          obj:/usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0
     47          obj:/usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0
     48          fun:call_init.part.0
     49          fun:call_init
     50          fun:_dl_init
     51          obj:/lib/x86_64-linux-gnu/ld-2.15.so
     52          obj:*
     53          obj:*
     54          obj:*
     55          obj:*
     56          obj:*
    4157}
    4258#}}}
     
    227243          fun:PCSetUp_LU
    228244}
     245{
     246        <mumps15>
     247          Memcheck:Cond
     248          fun:MPIC_Waitall
     249          fun:MPIR_Alltoall_intra
     250          fun:MPIR_Alltoall
     251          fun:MPIR_Alltoall_impl
     252          fun:PMPI_Alltoall
     253          fun:PMPI_ALLTOALL
     254          fun:dmumps_numvolsndrcv_
     255          fun:dmumps_simscaleabsuns_
     256          fun:dmumps_simscaleabs_
     257          fun:dmumps_fac_driver_
     258          fun:dmumps_
     259          fun:dmumps_f77_
     260}
    229261#}}}
Note: See TracChangeset for help on using the changeset viewer.