figtoipe-20080517/0000755000175000017500000000000011013642765011323 5ustar ababfigtoipe-20080517/GPL-20000644000175000017500000004310311013642765012030 0ustar abab GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. figtoipe-20080517/README0000644000175000017500000000266311013642765012212 0ustar abab Figtoipe ======== This is Figtoipe, a program that reads FIG files (as generated by xfig) and generates an XML file readable by Ipe. Compile by saying make A changelog is in the source file "figtoipe.cpp". Before reporting a bug, check that you have the latest version, and check that it is not yet mentioned in the FAQ on the Ipe webpage. You can report bugs on the bug tracking system at "http://tclab.kaist.ac.kr/ipe/bugzilla.html". Check the existing reports to see whether your bug has already been reported. Please do not send bug reports directly to me (the first thing I would do with the report is to enter it into the bug tracking system). Suggestions for features, or random comments on Figtoipe can be sent to the Ipe discussion mailing list at . You can also send suggestions or comments directly to us by Email, but you should then not expect a reply. Alexander Bürger, acfb@users.sourceforge.net Otfried Cheong, otfried@ipe.airpost.net Ipe webpage: http://tclab.kaist.ac.kr/ipe -------------------------------------------------------------------- figtoipe comes with ABSOLUTELY NO WARRANTY. It is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the file GPL-2 for details. figtoipe-20080517/Makefile0000644000175000017500000000123511013642765012764 0ustar abab############################################################################# # Makefile for building figtoipe ############################################################################# CXX = g++ CXXFLAGS = -O2 -Wall -W RM = rm -f LIBS = -lz TARGET = figtoipe SOURCES = $(TARGET).cpp SRCDISTFILES = $(TARGET).1 $(SOURCES) README Makefile GPL-2 all: $(TARGET) clean: -$(RM) $(TARGET) $(TARGET): $(SOURCES) $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) dist: DATE=`date +"%Y%m%d"`; D=/tmp/$(TARGET)-$$DATE; \ rm -rf $$D; mkdir $$D; \ cp $(SRCDISTFILES) $$D; \ tar czvf $$D.tar.gz -C /tmp $(TARGET)-$$DATE .PHONY: all figtoipe-20080517/figtoipe.10000644000175000017500000000526711013642765013225 0ustar abab.\" Hey, EMACS: -*- nroff -*- .\" First parameter, NAME, should be all caps .\" Second parameter, SECTION, should be 1-8, maybe w/ subsection .\" other parameters are allowed: see man(7), man(1) .\" TeX users may be more comfortable with the \fB\fP and .\" \fI\fP escape sequences to invode bold face and italics, .\" respectively. .TH FIGTOIPE 1 "April 26, 2008" .\" Please adjust this date whenever revising the manpage. .\" .\" Some roff macros, for reference: .\" .nh disable hyphenation .\" .hy enable hyphenation .\" .ad l left justify .\" .ad b justify to both left and right margins .\" .nf disable filling .\" .fi enable filling .\" .br insert line break .\" .sp insert n+1 empty lines .\" for manpage-specific macros, see man(7) .SH NAME figtoipe \- Convert FIG figures into Ipe format .SH SYNOPSIS .B figtoipe \fP[\-g] [\-p \fIpreamble\fP] \fIFIGfile\fP \fIXMLfile\fP .SH DESCRIPTION \fBfigtoipe\fP converts files in FIG format (as created, e.g., by \fBxfig\fP) to Ipe's XML format. \fBfigtoipe\fP is not complete. The main lacking feature is the conversion of splines. Arc-boxes are replaced by rectangles. Feel free to improve this version! The drawing models of FIG and Ipe are somewhat different. Ipe does not support depth ordering independent of grouping, pattern fill, and Postscript fonts. \fBfigtoipe\fP tries to include images specified in the XFIG file. For JPEG pictures, it tries include the compressed image data into the XML file. For files not recognized as JPEG, \fBanytopnm\fP is called; its output is compressed and included in the XML file. Some output of \fBanytopnm\fP might be rejected (e.g. images larger than 5000x5000 pixels or B&W bitmaps), or misunderstood. .SH OPTIONS .B \-g group the figure in the output XML .TP .B \-p \fIlatex\fP add \fIlatex\fP as a preamble to the generated XML file, e.g. .nf .in +.5i \'\\usepackage{amsmath}\' .in -.5i .fi .SH AUTHORS .ft CW .nf \&Otfried Cheong \&Alexander B\[:u]rger (image handling) .ft R .fi .SH REPORTING BUGS .ad l Please report bugs using Ipe bugzilla at .I "http://tclab.kaist.ac.kr/ipe/bugzilla.html" .SH SEE ALSO .ad l More information about Ipe can be found in .IR "The Ipe Manual" , which can be found in your Ipe installation. .SH LICENSE & WARRANTY .ad l \fBfigtoipe\fP comes with ABSOLUTELY NO WARRANTY. It is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the file \fBGPL-2\fP accompanying the source for details. figtoipe-20080517/figtoipe.cpp0000644000175000017500000011056311013642765013643 0ustar abab/* This file is part of the extensible drawing editor Ipe. Copyright (C) 1993-2008 Otfried Cheong Ipe is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. As a special exception, you have permission to link Ipe with the CGAL library and distribute executables, as long as you follow the requirements of the Gnu General Public License in regard to all of the software in the executable aside from CGAL. Ipe is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* * figtoipe.cpp * * This program converts files in FIG format (as used by Xfig) to XML * format as used by Ipe 6.0. * * All versions of the FIG file format are documented here: * "http://duke.usask.ca/~macphed/soft/fig/formats.html" * * This program can read only versions 3.0, 3.1, and 3.2. * * Changes: * * 2005/10/31 - replace double backslash by single one in text. * 2005/11/14 - generate correct header for Ipe 6.0 preview 25. * 2007/08/19 - Alexander Buerger acfb@users.sf.net * + include some images with anytopnm * + correct FIG3.1 problem (bugzilla #237) * + fixed rotation of text and ellipses * + do not write invisible ellipses / polylines / arcs * + accept double values in some places (points, thickness, ...; * extends FIG format) * + skip comments between FIG objects * + replaced some %g formats as they illegally appear in the PDF * 2007/09/26 - Alexander Buerger acfb@users.sf.net * + compress bitmap images * + include compressed JPEG images * 2008/04/26 - Alexander Buerger acfb@users.sf.net * + support grayscale pnm * + added -g and -p options */ #include #include #include #include #include #include #include #include #include #include #define FIGTOIPE_VERSION "figtoipe 2007/09/26" const int MEDIABOX_WIDTH = 595; const int MEDIABOX_HEIGHT = 842; // -------------------------------------------------------------------- struct Arrow { int iType; int iStyle; double iThickness; // 1/80 inch double iWidth; // Fig units double iHeight; // Fig units }; struct Point { double iX, iY; }; struct FigObject { int iType; int iSubtype; // meaning depends on type int iLinestyle; // solid, dashed, etc double iThickness; // 0 means no stroke int iPenColor; int iFillColor; int iDepth; // depth ordering int iPenStyle; // not used by FIG int iAreaFill; // how to fill: color, pattern double iStyle; // length of dash/dot pattern int iCapStyle; int iJoinStyle; int iDirection; int iForwardArrow; Arrow iForward; int iBackwardArrow; Arrow iBackward; double iCenterX, iCenterY; // center of ellipse and arc Point iArc1, iArc2, iArc3; double iAngle; // orientation of main axis of ellipse Point iRadius; // half-axes of ellipse int iArcBoxRadius; Point iPos; // position of text int iFont; int iFontFlags; double iFontSize; std::vector iString; std::vector iPoints; std::string image_filename; bool image_flipped; }; // -------------------------------------------------------------------- class FigReader { public: FigReader(FILE *fig) : iFig(fig) { } bool ReadHeader(); double Magnification() const { return iMagnification; } double UnitsPerPoint() const { return iUnitsPerPoint; } bool ReadObjects(); const std::vector &Objects() const { return iObjects; } const unsigned int *UserColors() const { return iUserColors; } private: bool GetLine(char *buf); int GetInt(); double GetDouble(); Point GetPoint(); void GetColor(); void GetArc(FigObject &obj); void GetEllipse(FigObject &obj); void GetPolyline(FigObject &obj); void GetSpline(FigObject &obj); void GetText(FigObject &obj); void GetArrow(Arrow &a); void GetArrows(FigObject &obj); int ComputeDepth(unsigned int &i); private: FILE *iFig; int iVersion; // minor version of FIG format double iMagnification; double iUnitsPerPoint; std::vector iObjects; unsigned int iUserColors[512]; }; // -------------------------------------------------------------------- const int BUFSIZE = 0x100; // skip comment lines (in the header) bool FigReader::GetLine(char *buf) { do { if (fgets(buf, BUFSIZE, iFig) != buf) return false; } while (buf[0] == '#'); return true; } int FigReader::GetInt() { int num = -1; (void) fscanf(iFig, "%d", &num); return num; } double FigReader::GetDouble() { double num; (void) fscanf(iFig, "%lg", &num); return num; } Point FigReader::GetPoint() { Point p; p.iX = GetDouble(); p.iY = GetDouble(); return p; } void FigReader::GetArrow(Arrow &a) { a.iType = GetInt(); a.iStyle = GetInt(); a.iThickness = GetDouble(); a.iWidth = GetDouble(); a.iHeight = GetDouble(); } void FigReader::GetArrows(FigObject &obj) { if (obj.iForwardArrow) GetArrow(obj.iForward); if (obj.iBackwardArrow) GetArrow(obj.iBackward); } // -------------------------------------------------------------------- bool FigReader::ReadHeader() { char line[BUFSIZE]; if (fgets(line, BUFSIZE, iFig) != line) return false; if (strncmp(line, "#FIG", 4)) return false; // check FIG version int majorVersion; sscanf(line + 4, "%d.%d", &majorVersion, &iVersion); if (majorVersion != 3 || iVersion<0 || iVersion>2 ) { fprintf(stderr, "Figtoipe supports FIG versions 3.0 - 3.2 only.\n"); return false; } fprintf(stderr, "FIG format version %d.%d\n", majorVersion, iVersion); // skip orientation and justification if (!GetLine(line)) return false; if (!GetLine(line)) return false; bool metric = false; if (!GetLine(line)) return false; if (!strncmp(line, "Metric", 6)) metric = true; (void) metric; // not yet used int magnification = 100, resolution=1200; if (iVersion == 2) { // Version 3.2: // papersize if (!GetLine(line)) return false; // export and print magnification if (!GetLine(line)) return false; sscanf(line, "%d", &magnification); // multi-page mode if (!GetLine(line)) return false; // transparent color if (!GetLine(line)) return false; } // resolution and coord_system if (!GetLine(line)) return false; int coord_system; sscanf(line, "%d %d", &resolution, &coord_system); iUnitsPerPoint = (resolution / 72.0); iMagnification = magnification / 100.0; return true; } // link start and end of compounds together, // and assign depth to compound object int FigReader::ComputeDepth(unsigned int &i) { if (iObjects[i].iType != 6) return iObjects[i++].iDepth; int pos = i; int depth = 1000; ++i; while (iObjects[i].iType != -6) { int od = ComputeDepth(i); if (od < depth) depth = od; } iObjects[pos].iDepth = depth; iObjects[pos].iSubtype = i; ++i; return depth; } // -------------------------------------------------------------------- // objects are appended to list bool FigReader::ReadObjects() { int level = 0; for (;;) { int objType = GetInt(); //fprintf(stderr, "object type %d\n", objType); if (objType == -1 && fgetc(iFig)=='#' ) { char buf[1024]; fgets(buf, sizeof(buf), iFig); continue; } if (objType == -1) { // EOF if (level > 0) return false; unsigned int i = 0; while (i < iObjects.size()) ComputeDepth(i); return true; } if (objType == 0) { // user-defined color GetColor(); } else { FigObject obj; obj.iType = objType; switch (obj.iType) { case 1: // ELLIPSE GetEllipse(obj); break; case 2: // POLYLINE GetPolyline(obj); break; case 3: // SPLINE GetSpline(obj); break; case 4: // TEXT GetText(obj); break; case 5: // ARC GetArc(obj); break; case 6: // COMPOUND (void) GetInt(); // read and ignore bounding box (void) GetInt(); (void) GetInt(); (void) GetInt(); ++level; break; case -6: // END of COMPOUND if (level == 0) return false; --level; break; default: fprintf(stderr, "Unknown object type in FIG file.\n"); return false; } iObjects.push_back(obj); } } } void FigReader::GetColor() { int colorNum = GetInt(); // color number int rgb; (void) fscanf(iFig," #%x", &rgb); // RGB string in hex iUserColors[colorNum - 32] = rgb; } void FigReader::GetEllipse(FigObject &obj) { obj.iSubtype = GetInt(); obj.iLinestyle = GetInt(); obj.iThickness = GetDouble(); obj.iPenColor = GetInt(); obj.iFillColor = GetInt(); obj.iDepth = GetInt(); obj.iPenStyle = GetInt(); // not used obj.iAreaFill = GetInt(); obj.iStyle = GetDouble(); obj.iDirection = GetInt(); // always 1 obj.iAngle = GetDouble(); // radians, the angle of the x-axis obj.iCenterX = GetDouble(); obj.iCenterY = GetDouble(); obj.iRadius = GetPoint(); (void) GetPoint(); // start (void) GetPoint(); // end } void FigReader::GetPolyline(FigObject &obj) { obj.iSubtype = GetInt(); obj.iLinestyle = GetInt(); obj.iThickness = GetDouble(); obj.iPenColor = GetInt(); obj.iFillColor = GetInt(); obj.iDepth = GetInt(); obj.iPenStyle = GetInt(); // not used obj.iAreaFill = GetInt(); obj.iStyle = GetDouble(); obj.iJoinStyle = GetInt(); obj.iCapStyle = GetInt(); obj.iArcBoxRadius = GetInt(); obj.iForwardArrow = GetInt(); obj.iBackwardArrow = GetInt(); int nPoints = GetInt(); GetArrows(obj); if (obj.iSubtype == 5) { // Imported image int orientation; char image_filename[1024]; // orientation and filename (void) fscanf(iFig, "%d %s", &orientation, image_filename); obj.image_flipped = (orientation==1); obj.image_filename = std::string(image_filename); } for (int i = 0; i < nPoints; ++i) obj.iPoints.push_back( GetPoint() ); } void FigReader::GetSpline(FigObject &obj) { /* 0: opened approximated spline 1: closed approximated spline 2: opened interpolated spline 3: closed interpolated spline 4: opened x-spline (FIG 3.2) 5: closed x-spline (FIG 3.2) */ obj.iSubtype = GetInt(); obj.iLinestyle = GetInt(); obj.iThickness = GetDouble(); obj.iPenColor = GetInt(); obj.iFillColor = GetInt(); obj.iDepth = GetInt(); obj.iPenStyle = GetInt(); // not used obj.iAreaFill = GetInt(); obj.iStyle = GetDouble(); obj.iCapStyle = GetInt(); obj.iForwardArrow = GetInt(); obj.iBackwardArrow = GetInt(); int nPoints = GetInt(); GetArrows(obj); for (int i = 0; i < nPoints; ++i) obj.iPoints.push_back( GetPoint() ); if (iVersion == 2) { // shape factors exist in FIG 3.2 only for (int i = 0; i < nPoints; ++i) { (void) GetDouble(); // double shapeFactor } } else { if (obj.iSubtype > 1) { for (int i = 0; i < nPoints; ++i) { (void) GetDouble(); // double lx (void) GetDouble(); // double ly (void) GetDouble(); // double rx (void) GetDouble(); // double ry } } } } void FigReader::GetText(FigObject &obj) { obj.iSubtype = GetInt(); obj.iThickness = 1; // stroke obj.iPenColor = GetInt(); obj.iDepth = GetInt(); obj.iPenStyle = GetInt(); // not used obj.iFont = GetInt(); obj.iFontSize = GetDouble(); obj.iAngle = GetDouble(); obj.iFontFlags = GetInt(); (void) GetDouble(); // height (void) GetDouble(); // length obj.iPos = GetPoint(); // skip blank fgetc(iFig); std::vector string; for (;;) { int ch = fgetc(iFig); if (ch == EOF) break; if (ch < 0x80) { string.push_back(char(ch)); } else { // convert to UTF-8 string.push_back(char(0xc0 + ((ch >> 6) & 0x3))); string.push_back(char(ch & 0x3f)); } if (string.size() >= 4 && !strncmp(&string[string.size() - 4], "\\001", 4)) { string.resize(string.size() - 4); break; } // fig seems to store "\" as "\\" if (string.size() >= 2 && !strncmp(&string[string.size() - 2], "\\\\", 2)) { string.resize(string.size() - 1); } } string.push_back('\0'); obj.iString = string; } void FigReader::GetArc(FigObject &obj) { obj.iSubtype = GetInt(); obj.iLinestyle = GetInt(); obj.iThickness = GetDouble(); obj.iPenColor = GetInt(); obj.iFillColor = GetInt(); obj.iDepth = GetInt(); obj.iPenStyle = GetInt(); // not used obj.iAreaFill = GetInt(); obj.iStyle = GetDouble(); obj.iCapStyle = GetInt(); obj.iDirection = GetInt(); obj.iForwardArrow = GetInt(); obj.iBackwardArrow = GetInt(); obj.iCenterX = GetDouble(); obj.iCenterY = GetDouble(); obj.iArc1 = GetPoint(); obj.iArc2 = GetPoint(); obj.iArc3 = GetPoint(); GetArrows(obj); } // -------------------------------------------------------------------- unsigned int ColorTable[] = { 0x000000, 0x0000ff, 0x00ff00, 0x00ffff, 0xff0000, 0xff00ff, 0xffff00, 0xffffff, 0x000090, 0x0000b0, 0x0000d0, 0x87ceff, 0x009000, 0x00b000, 0x00d000, 0x009090, 0x00b0b0, 0x00d0d0, 0x900000, 0xb00000, 0xd00000, 0x900090, 0xb000b0, 0xd000d0, 0x803000, 0xa04000, 0xc06000, 0xff8080, 0xffa0a0, 0xffc0c0, 0xffe0e0, 0xffd700 }; class FigWriter { public: FigWriter(FILE *xml, const std::string& figname, double mag, double upp, const unsigned int *uc) : iXml(xml), iFigName(figname), iMagnification(mag), iUnitsPerPoint(upp), iUserColors(uc) { } void WriteObjects(const std::vector &objects, int start, int fin); private: void WriteEllipse(const FigObject &obj); void WriteImage(const FigObject &obj); void WritePolyline(const FigObject &obj); void WriteSpline(const FigObject &obj); void WriteText(const FigObject &obj); void WriteArc(const FigObject &obj); void WriteStroke(const FigObject &obj); void WriteFill(const FigObject &obj); void WriteLineStyle(const FigObject &obj); void WriteArrows(const FigObject &obj); double X(double x); double Y(double y); private: FILE *iXml; const std::string iFigName; double iMagnification; double iUnitsPerPoint; const unsigned int *iUserColors; }; double FigWriter::X(double x) { return (x / iUnitsPerPoint) * iMagnification; } double FigWriter::Y(double y) { return MEDIABOX_HEIGHT - X(y); } void FigWriter::WriteStroke(const FigObject &obj) { if (obj.iThickness == 0) // no stroke return; int col = obj.iPenColor; if (col < 0) col = 0; // make default black unsigned int rgb; if (col < 32) rgb = ColorTable[col]; else rgb = iUserColors[col - 32]; fprintf(iXml, " stroke=\"%g %g %g\"", ((rgb >> 16) & 0xff) / 255.0, ((rgb >> 8) & 0xff) / 255.0, (rgb & 0xff) / 255.0); } void FigWriter::WriteFill(const FigObject &obj) { // unfilled if (obj.iAreaFill == -1) return; int fill = obj.iAreaFill; if (fill > 40) { fprintf(stderr, "WARNING: fill pattern %d replaced by solid filling.\n", fill); fill = 20; } if (obj.iFillColor < 1) { // BLACK & DEFAULT fprintf(iXml, " fill=\"%g\"", 1.0 - (fill / 20.0)); } else { unsigned int rgb; if (obj.iFillColor < 32) rgb = ColorTable[obj.iFillColor]; else rgb = iUserColors[obj.iFillColor - 32]; double r = ((rgb >> 16) & 0xff) / 255.0; double g = ((rgb >> 8) & 0xff) / 255.0; double b = (rgb & 0xff) / 255.0; if (fill < 20) { // mix down to black double scale = fill / 20.0; r *= scale; g *= scale; b *= scale; } else if (fill > 20) { // mix up to white double scale = (40 - fill) / 20.0; // 40 is pure white r = 1.0 - (1.0 - r) * scale; g = 1.0 - (1.0 - g) * scale; b = 1.0 - (1.0 - b) * scale; } fprintf(iXml, " fill=\"%g %g %g\"", r, g, b); } } void FigWriter::WriteLineStyle(const FigObject &obj) { if (obj.iThickness == 0) return; fprintf(iXml, " pen=\"%g\"", iMagnification * 72.0 * (obj.iThickness / 80.0)); switch (obj.iLinestyle) { case -1: // Default case 0: // Solid break; case 1: fprintf(iXml, " dash=\"dashed\""); break; case 2: fprintf(iXml, " dash=\"dotted\""); break; case 3: fprintf(iXml, " dash=\"dash dotted\""); break; case 4: fprintf(iXml, " dash=\"dash dot dotted\""); break; case 5: // Dash-triple-dotted (maybe put this in a stylesheet) fprintf(iXml, " dash=\"[4 2 1 2 1 2 1 2] 0\""); break; } } void FigWriter::WriteArrows(const FigObject &obj) { if (obj.iForwardArrow) fprintf(iXml, " arrow=\"%g\"", X(obj.iForward.iHeight)); if (obj.iBackwardArrow) fprintf(iXml, " backarrow=\"%g\"", X(obj.iBackward.iHeight)); } // -------------------------------------------------------------------- class DepthCompare { public: DepthCompare(const std::vector &objects) : iObjects(objects) { /* nothing */ } int operator()(int lhs, int rhs) const { return (iObjects[lhs].iDepth > iObjects[rhs].iDepth); } private: const std::vector &iObjects; }; void FigWriter::WriteObjects(const std::vector &objects, int start, int fin) { // collect indices of objects std::vector objs; int i = start; while (i < fin) { objs.push_back(i); if (objects[i].iType == 6) i = objects[i].iSubtype; // link to END OF COMPOUND ++i; } // now sort the objects DepthCompare comp(objects); std::stable_sort(objs.begin(), objs.end(), comp); // now render them for (unsigned int j = 0; j < objs.size(); ++j) { i = objs[j]; switch (objects[i].iType) { case 1: // ELLIPSE WriteEllipse(objects[i]); break; case 2: // POLYLINE WritePolyline(objects[i]); break; case 3: // SPLINE WriteSpline(objects[i]); break; case 4: // TEXT WriteText(objects[i]); break; case 5: // ARC WriteArc(objects[i]); break; case 6: // COMPOUND fprintf(iXml, "\n"); // recursively render elements of the compound WriteObjects(objects, i+1, objects[i].iSubtype); fprintf(iXml, "\n"); break; } } } // -------------------------------------------------------------------- void FigWriter::WriteEllipse(const FigObject &obj) { if (obj.iThickness == 0 && obj.iAreaFill==-1 ) { fprintf(stderr, "WARNING: ellipse with neither fill nor line ignored.\n"); return; } fprintf(iXml, "\n"); const double ca = cos(obj.iAngle); const double sa = sin(obj.iAngle); fprintf(iXml, "%g %g %g %g %g %g e\n\n", X( obj.iRadius.iX * ca), X( obj.iRadius.iX * sa), X(-obj.iRadius.iY * sa), X( obj.iRadius.iY * ca), X( obj.iCenterX), Y( obj.iCenterY) ); } // -------------------------------------------------------------------- namespace { unsigned int JPEG_read1( std::ifstream& jpeg_in ) { unsigned char c; jpeg_in.read( reinterpret_cast(&c), 1 ); if( jpeg_in.fail() ) throw std::string( "Failed to read 1 byte." ); return c; } unsigned int JPEG_read2( std::ifstream& jpeg_in ) { return (JPEG_read1(jpeg_in) << 8) + JPEG_read1(jpeg_in); } typedef enum { jpeg_SOF0 = 0xC0, jpeg_SOF1 = 0xC1, jpeg_SOF2 = 0xC2, jpeg_SOF3 = 0xC3, jpeg_SOF5 = 0xC5, jpeg_SOF6 = 0xC6, jpeg_SOF7 = 0xC7, jpeg_SOF9 = 0xC9, jpeg_SOF10 = 0xCA, jpeg_SOF11 = 0xCB, jpeg_SOF13 = 0xCD, jpeg_SOF14 = 0xCE, jpeg_SOF15 = 0xCF, jpeg_SOI = 0xD8, jpeg_EOI = 0xD9, jpeg_SOS = 0xDA, jpeg_APP0 = 0xE0, jpeg_APP12 = 0xEC, jpeg_COM = 0xFE } JPEG_markers; int JPEG_next_marker( std::ifstream& jpeg_in ) { unsigned int c; do { c = JPEG_read1(jpeg_in); } while( c != 0xFF ); do { c = JPEG_read1(jpeg_in); } while( c == 0xFF ); return c; } void JPEG_skip_segment( std::ifstream& jpeg_in ) { unsigned int l = JPEG_read2(jpeg_in); jpeg_in.seekg( l-2, std::ios::cur ); } typedef enum { IMAGE_GRAY=0, IMAGE_RGB=1, IMAGE_CMYK=2 } IPE_COLORSPACE; bool ReadJPEGData( std::ifstream& jpeg_in, int& image_width, int& image_height, int& image_colorspace, int& bitspercomponent ) { int required_segments = 0; try { const unsigned int soi = JPEG_read2(jpeg_in); if( (soi & 0xFF) != jpeg_SOI ) return false; while( required_segments != 3 && !jpeg_in.eof() ) { const unsigned int marker = JPEG_next_marker(jpeg_in); switch( marker ) { case jpeg_APP0: { unsigned int l = JPEG_read2(jpeg_in); const char* JFIF = "JFIF"; for( int i=0; i<5; ++i ) if( JFIF[i] != (int)JPEG_read1(jpeg_in) ) return false; jpeg_in.seekg( l-5-2, std::ios::cur ); required_segments |= 1; break; } case jpeg_SOF0: case jpeg_SOF1: case jpeg_SOF2: case jpeg_SOF3: { unsigned int l = JPEG_read2(jpeg_in); bitspercomponent = JPEG_read1(jpeg_in); image_height = JPEG_read2(jpeg_in); image_width = JPEG_read2(jpeg_in); unsigned int ncomponents = JPEG_read1(jpeg_in); switch( ncomponents ) { case 1: image_colorspace = IMAGE_GRAY; break; case 3: image_colorspace = IMAGE_RGB; break; case 4: image_colorspace = IMAGE_CMYK; break; default: return false; } if( l != 8 + 3*ncomponents ) throw std::string("Unexpected SOFx length."); required_segments |= 2; break; } default: JPEG_skip_segment(jpeg_in); } } } catch( std::string msg ) { fprintf(stderr, "Error while reading JPEG: %s.\n", msg.c_str() ); } return required_segments == 3; } std::string make_safe_filename( const std::string& filename ) { std::ostringstream s_sf; s_sf << '\''; for( unsigned i=0; i0 && lastdash != std::string::npos ) filename = iFigName.substr( 0, lastdash+1 ); filename += obj.image_filename; // try to build safe filename const std::string safe_filename = make_safe_filename( filename ); // image data to be filled by reading procedure int image_width=0, image_height=0; int image_colorspace = IMAGE_RGB; int bitspercomponent = 8; std::string image_data, filter; std::ifstream jpeg_in( filename.c_str() ); // try reading a JPEG file if( ReadJPEGData( jpeg_in, image_width, image_height, image_colorspace, bitspercomponent ) ) { // determine file size jpeg_in.seekg( 0, std::ios::end ); const unsigned jpeg_size = jpeg_in.tellg(); jpeg_in.seekg( 0, std::ios::beg ); // copy file contents image_data.resize( jpeg_size ); jpeg_in.read( const_cast(image_data.data()), jpeg_size ); if( jpeg_in.fail() ) { fprintf(stderr, "WARNING: reading jpeg data failed. Skipping image.\n"); return; } filter = "DCTDecode"; } else { // other images are read by anytopnm and then compressed // build anytopnm command line std::string cmd = "anytopnm "; cmd += safe_filename; // open pipe to read anytopnm output FILE* anytopnm = popen( cmd.c_str(), "r" ); // running anytopnm somehow failed if( !anytopnm ) { fprintf(stderr, "WARNING: anytopnm failed to run. Skipping image.\n"); return; } try { // check image type; should be P4 (pbm), P5 (pgm) or P6 (ppm) char type_P, fmt; fscanf( anytopnm, "%c%c", &type_P, &fmt ); if( type_P!='P' || ( fmt!='4' && fmt!='5' && fmt!='6' ) ) throw std::string("anytopnm output not understood"); if( fscanf( anytopnm, "%d %d", &image_width, &image_height ) != 2 ) throw std::string("anytopnm output not understood (w&h)"); if( image_width<=0 || image_width>5000 ) throw std::string("image width out of range [0,5000])"); if( image_height<=0 || image_height>5000 ) throw std::string("image height out of range [0,5000])"); if( fmt=='4' ) { // bitmap throw std::string("bitmap not implemented"); } else { int maxcolor; fscanf( anytopnm, "%d", &maxcolor ); if( maxcolor<=0 || maxcolor>65535 ) throw std::string("anytopnm output not understood (maxcolor)"); bitspercomponent = 8; image_colorspace = (fmt=='5') ? IMAGE_GRAY : IMAGE_RGB; // skip single whitespace (void)fgetc( anytopnm ); std::ostringstream idata; for( int r=0; r=256 ) { int component2 = fgetc( anytopnm ); if( component2<0 ) throw std::string("anytopnm output: eof"); if( maxcolor == 65535 ) { component = component2; } else { component = (component<<8) | component2; component = int(255.0*component/float(maxcolor)); } } else if( maxcolor!=255 ) { component = int(255.0*component/float(maxcolor)); } const unsigned char c = (unsigned char)component; idata.write( (const char*)&c, 1 ); } } } image_data = idata.str(); } pclose( anytopnm ); } catch( std::string msg ) { pclose( anytopnm ); fprintf(stderr, "WARNING: anytopnm problem. Skipping image.\n"); return; } if( true ) { // compress image const uLongf zbuf_size = compressBound( image_data.size() ); uLongf compressed_size = zbuf_size; char zbuf[zbuf_size]; const int ok = compress2( (Bytef*)zbuf, &compressed_size, (const Bytef*)image_data.data(), image_data.size(), 9 ); if( ok == Z_OK ) { image_data = std::string(zbuf, zbuf+compressed_size); filter = "FlateDecode"; } else { fprintf(stderr, "Failed to compress image (%d)." " Will store uncompressed image.\n", ok ); } } } if( bitspercomponent != 8 ) { fprintf(stderr, "WARNING: Unsupported n.o. bits per component. Skipping image.\n"); return; } fprintf(iXml, "0 && image_height>0 ) fprintf(iXml, "width=\"%d\" height=\"%d\" ", image_width, image_height ); const char* image_colorspaces[3] = { "DeviceGray", "DeviceRGB", "DeviceCMYK" }; fprintf(iXml, "ColorSpace=\"%s\" BitsPerComponent=\"8\" ", image_colorspaces[image_colorspace] ); if( !filter.empty() ) fprintf(iXml, " length=\"%ld\" Filter=\"%s\"", long(image_data.size()), filter.c_str() ); const double x1 = X(obj.iPoints[0].iX), y1 = Y(obj.iPoints[0].iY); const double x2 = X(obj.iPoints[2].iX), y2 = Y(obj.iPoints[2].iY); if( obj.image_flipped ) { const double r = (x2-x1)/(y2-y1); const double tx = (x1+x2)/2, ty = (y1+y2)/2; fprintf(iXml, " matrix=\"0 %f %f 0 %f %f\"", 1/r, r, tx-r*ty, ty-tx/r ); } fprintf(iXml, " rect=\"%g %g %g %g\"", x1, y1, x2, y2 ); fprintf(iXml, ">\n" ); int linebreak=0; for( unsigned i=0; i 0 ) fputc( '\n', iXml ); fprintf(iXml, "\n"); return; } void FigWriter::WritePolyline(const FigObject &obj) { /* 1: polyline 2: box 3: polygon 4: arc-box 5: imported-picture bounding-box */ if (obj.iPoints.size() < 2) { fprintf(stderr, "WARNING: polyline with less than two vertices ignored.\n"); return; } if (obj.iThickness == 0 && obj.iAreaFill==-1 ) { fprintf(stderr, "WARNING: polyline with neither fill nor line ignored.\n"); return; } if (obj.iSubtype == 4) fprintf(stderr, "WARNING: turning arc-box into rectangle.\n"); if (obj.iSubtype == 5) { WriteImage( obj ); return; } fprintf(iXml, "\n"); for (unsigned int i = 0; i < obj.iPoints.size(); ++i) { double x = obj.iPoints[i].iX; double y = obj.iPoints[i].iY; if (i == 0) { fprintf(iXml, "%g %g m\n", X(x), Y(y)); } else if (i == obj.iPoints.size() - 1 && obj.iSubtype > 1) { fprintf(iXml, "h\n"); // close path } else { fprintf(iXml, "%g %g l\n", X(x), Y(y)); } } fprintf(iXml, "\n"); } void FigWriter::WriteSpline(const FigObject &obj) { /* 0: opened approximated spline 1: closed approximated spline 2: opened interpolated spline 3: closed interpolated spline 4: opened x-spline (FIG 3.2) 5: closed x-spline (FIG 3.2) */ FigObject obj1 = obj; obj1.iJoinStyle = 0; obj1.iSubtype = (obj.iSubtype & 1) ? 3 : 1; fprintf(stderr, "WARNING: spline replaced by polyline.\n"); WritePolyline(obj1); } void FigWriter::WriteText(const FigObject &obj) { const double tx = X(obj.iPos.iX), ty = Y(obj.iPos.iY); fprintf(iXml, ""); int font = obj.iFont; if (obj.iFontFlags & 2) { // "special" Latex text font = 0; // needs no special treatment } else if (obj.iFontFlags & 4) { // Postscript font: set font to default font = 0; fprintf(stderr, "WARNING: postscript font ignored.\n"); } switch (font) { case 0: // Default font fprintf(iXml, "%s", &obj.iString[0]); break; case 1: // Roman fprintf(iXml, "\\textrm{%s}", &obj.iString[0]); break; case 2: // Bold fprintf(iXml, "\\textbf{%s}", &obj.iString[0]); break; case 3: // Italic fprintf(iXml, "\\emph{%s}", &obj.iString[0]); break; case 4: // Sans Serif fprintf(iXml, "\\textsf{%s}", &obj.iString[0]); break; case 5: // Typewriter fprintf(iXml, "\\texttt{%s}", &obj.iString[0]); break; } fprintf(iXml, "\n"); } void FigWriter::WriteArc(const FigObject &obj) { if (obj.iThickness == 0 && obj.iAreaFill==-1 ) { fprintf(stderr, "WARNING: arc with neither fill nor line ignored.\n"); return; } // 0: pie-wedge (closed); 1: open ended arc fprintf(iXml, "\n"); Point beg = (obj.iDirection == 0) ? obj.iArc3 : obj.iArc1; Point end = (obj.iDirection == 0) ? obj.iArc1 : obj.iArc3; fprintf(iXml, "%g %g m\n", X(beg.iX), Y(beg.iY)); double dx = obj.iArc1.iX - obj.iCenterX; double dy = obj.iArc1.iY - obj.iCenterY; double radius = sqrt(dx*dx + dy*dy); fprintf(iXml, "%g 0 0 %g %g %g %g %g a\n\n", X(radius), X(radius), X(obj.iCenterX), Y(obj.iCenterY), X(end.iX), Y(end.iY)); } // -------------------------------------------------------------------- static void print_help_message() { fprintf(stderr, "figtoipe is part of the extensible drawing editor Ipe.\n" " Copyright (C) 1993-2008 Otfried Cheong \n" " This is free software with ABSOLUTELY NO WARRANTY.\n" "\n" "Use: figtoipe [-g] [-p preamble] \n" " converts a file in FIG format to Ipe's XML format\n" " -g -- puts the produced figure into a group\n" " -p preamble -- inserts a preamble (e.g. '\\usepackage{amsmath}')\n" ); exit(9); } // -------------------------------------------------------------------- int main(int argc, char **argv) { bool group = false; std::string preamble = ""; int a = 1; if( a= argc ) print_help_message(); preamble = argv[a+1]; a += 2; } if( a+2 != argc ) print_help_message(); const char *figname = argv[a]; const char *xmlname = argv[a+1]; FILE *fig = fopen(figname, "r"); if (!fig) { fprintf(stderr,"figtoipe: cannot open '%s'\n", figname); exit(-1); } FigReader fr(fig); if (!fr.ReadHeader()) { fprintf(stderr, "figtoipe: cannot parse header of '%s'\n", figname); exit(-1); } fprintf(stderr, "Converting at %g FIG units per point, magnification %g.\n", fr.UnitsPerPoint(), fr.Magnification()); if (!fr.ReadObjects()) { fprintf(stderr, "Error reading FIG file.\n"); exit(9); } fclose(fig); FILE *xml = fopen(xmlname, "w"); if (!xml) { fprintf(stderr, "figtoipe: cannot open '%s'\n", xmlname); exit(-1); } FigWriter fw(xml, figname, fr.Magnification(), fr.UnitsPerPoint(), fr.UserColors()); fprintf(xml, "\n\n", FIGTOIPE_VERSION, 0, 0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT); if( !preamble.empty() ) fprintf(xml, "\n%s\n\n", preamble.c_str()); fprintf(xml, "\n"); if( group ) fprintf(xml, "\n"); fw.WriteObjects(fr.Objects(), 0, fr.Objects().size()); if( group ) fprintf(xml, "\n"); fprintf(xml, "\n"); fprintf(xml, "\n"); fclose(xml); return 0; } // --------------------------------------------------------------------