From 4543f474442af4192668bb1462826a33607e17ec Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 6 Dec 2008 14:41:38 +0100 Subject: [PATCH 01/54] added ConTeXt terminal (adapted to work with 4.2; need to check for changes) --- src/term.h | 2 + term/context.trm | 2333 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 2335 insertions(+) create mode 100644 term/context.trm diff --git a/src/term.h b/src/term.h index 4db040a78..27418ccc5 100644 --- a/src/term.h +++ b/src/term.h @@ -406,6 +406,8 @@ /* latex and emtex */ #include "latex.trm" +/* context */ +#include "context.trm" /* latex/tex with picture in postscript */ #ifdef PSLATEX_DRIVER diff --git a/term/context.trm b/term/context.trm new file mode 100644 index 000000000..f06ec17e6 --- /dev/null +++ b/term/context.trm @@ -0,0 +1,2333 @@ +/* Hello, Emacs, this is -*-C-*- + * $Id:$ + */ + +/* GNUPLOT - context.trm */ + +/*[ + * Copyleft 2006 Mojca Miklavec + * + * Permission to use, copy, and distribute this software and its + * documentation for any purpose with or without fee is hereby granted, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. + * + * Permission to modify the software is granted, but not the right to + * distribute the complete modified source code. Modifications are to + * be distributed as patches to the released version. Permission to + * distribute binaries produced by compiling modified sources is granted, + * provided you + * 1. distribute the corresponding source modifications from the + * released version in the form of a patch file along with the binaries, + * 2. add special version identification to distinguish your version + * in addition to the base release version number, + * 3. provide your name and address as the primary contact for the + * support of your modified version, and + * 4. retain our contact information in regard to use of the base + * software. + * Permission to distribute the released version of the source code along + * with corresponding source modifications in the form of a patch file is + * granted with same provisions 2 through 4 for binary distributions. + * + * This software is provided "as is" without express or implied warranty + * to the extent permitted by applicable law. +]*/ + +/* + * AUTHORS: + * Mojca Miklavec + * + * - author of this file + * - improvements to m-gnuplot.tex + * - mp-gnuplot.mp - metapost macros to support gnuplot-specifics + * inside ConTeXt + * + * Hans Hagen (author of ConTeXt) + * + * - a lot of functionality added to ConTeXt to enable better support + * of Gnuplot module, many bugs fixed + * - author of the module m-gnuplot.tex to support inclusion of Gnuplot + * graphics into ConTeXt + * - constant support on the most tricky issues + * Taco Hoekwater (developer of Metapost & pdfTeX) + * + * - some code of this file, many tricks, lots of bug fixes, + * suggestions and testing + * - Hans's right hand in ConTeXt development: + * constant support on even more tricky issues + * + * For questions, suggestions, comments, improvements please contact: + * - Mojca Miklavec or + * - + * - http://wiki.contextgarden.net/Gnuplot + * + * With special thanks to: + * - Peter M\"unster for the initiative (Peter, sory for \") + * - Renaud Aubin for maintaining the darcs repository + * - Aditya Mahajan for some tricky parts in m-gnuplot.tex + * - Timoth\'ee Lecomte for many suggestion about improving this terminal + * + */ + +/* + * GNUPLOT -- context.trm + * + * This driver creates a ConTeXt document with embded metapost (metafun) + * commands and is consequently used for creation of PDF documents. + * + * It is in a way similar to pslatex, but specialized for ConTeXt, + * where it can also be used in the following way: + * + * \usemodule[gnuplot] + * + * \starttext + * \section{Drawing nice graphs with \sc gnuplot} + * \startGNUPLOTscript{sin} + * pot sin(x) + * \stopGNUPLOTscript + * \useGNUPLOTgraphic[sin] + * \stoptext + * + * For more information see http://wiki.contextgarden.net/Gnuplot + * + * Mostly based on: + * - default settings copied from LaTeX terminal (because I liked it) + * - path construction & drawing routines from MetaPost terminal + * - advanced functionality with reference to PostScript terminal + * + * Future plans: + * - add missing functionality: + * - improved support for palettes + * - binary images + * - smooth shading in color bars + * - transparent colors and other color spaces + * - gouraud shading (once implemented in 4.3) + * - derive a better metapost terminal out of this one (to replace the old one) + * - a new terminal based on TukZ + * - compatible with LaTeX/ConTeX/plain TeX + * - works with e-TeX/pdfTeX/XeTeX/luaTeX/etc. + */ + +#include "driver.h" +#include "pm3d.h" + +#ifdef TERM_REGISTER +register_term(context) +#endif + +#ifdef TERM_PROTO +TERM_PUBLIC void CONTEXT_options __PROTO((void)); +TERM_PUBLIC void CONTEXT_init __PROTO((void)); +TERM_PUBLIC void CONTEXT_reset __PROTO((void)); +TERM_PUBLIC void CONTEXT_text __PROTO((void)); +TERM_PUBLIC void CONTEXT_graphics __PROTO((void)); +TERM_PUBLIC void CONTEXT_move __PROTO((unsigned int x, unsigned int y)); +TERM_PUBLIC void CONTEXT_vector __PROTO((unsigned int x, unsigned int y)); +TERM_PUBLIC void CONTEXT_linetype __PROTO((int lt)); +TERM_PUBLIC void CONTEXT_put_text __PROTO((unsigned int x, unsigned int y, const char *str)); + +TERM_PUBLIC int CONTEXT_text_angle __PROTO((int ang)); +TERM_PUBLIC int CONTEXT_justify_text __PROTO((enum JUSTIFY mode)); +TERM_PUBLIC void CONTEXT_point __PROTO((unsigned int x, unsigned int y, int number)); +TERM_PUBLIC void CONTEXT_arrow __PROTO((unsigned int sx, unsigned int sy, unsigned int ex, unsigned int ey, int head)); +TERM_PUBLIC int CONTEXT_set_font __PROTO((const char *font)); /* "font,size" */ +TERM_PUBLIC void CONTEXT_pointsize __PROTO((double pointsize)); +TERM_PUBLIC void CONTEXT_fillbox __PROTO((int style, unsigned int x1, unsigned int y1, unsigned int width, unsigned int height)); /* clear part of multiplot */ +TERM_PUBLIC void CONTEXT_fill __PROTO((int style)); +TERM_PUBLIC void CONTEXT_linewidth __PROTO((double linewidth)); +TERM_PUBLIC int CONTEXT_make_palette __PROTO((t_sm_palette *palette)); +/* TERM_PUBLIC void CONTEXT_previous_palette __PROTO((void)); do we need it? */ +TERM_PUBLIC void CONTEXT_set_color __PROTO((t_colorspec *colorspec)); +TERM_PUBLIC void CONTEXT_filled_polygon __PROTO((int points, gpiPoint *corners)); +#ifdef WITH_IMAGE +/* TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint *, t_imagecolor)); TODO */ +#endif /* WITH_IMAGE */ + +/* Metapost can only deal with numbers between 2^{-16} and 2^12=4069 */ +/* the values will be set later */ +/* scale is 1pt = 100 units */ + +#define CONTEXT_DPI (2400) +/* default plot size will be 5in x 3in*/ +#define CONTEXT_XSIZE_VALUE 5 +#define CONTEXT_YSIZE_VALUE 3 +#define CONTEXT_XSIZE_UNIT UNIT_IN +#define CONTEXT_YSIZE_UNIT UNIT_IN +#define CONTEXT_XMAX (CONTEXT_XSIZE_VALUE*CONTEXT_DPI) +#define CONTEXT_YMAX (CONTEXT_YSIZE_VALUE*CONTEXT_DPI) +/* default fontsize: 12pt */ +#define CONTEXT_FONTSIZE 12.0 +/* default height of char: 12pt by default */ +#define CONTEXT_VCHAR (CONTEXT_DPI*CONTEXT_FONTSIZE/72.27) +/* in ConTeXt 12pt LM font is the default; + * at that size digits are 5.8749847pt wide + * in LaTeX, which assumes 11pt, the ratio is 5.3/11, which is similar */ +#define CONTEXT_LM_H_TO_V_RATIO 0.4895 +#define CONTEXT_HCHAR (CONTEXT_LM_H_TO_V_RATIO*CONTEXT_VCHAR) +/* default tic size: 3.5bp (chosen to suit the size of plot approximately) + * - in LaTeXt it's 5bp + * - in PostScript it's 3.15pt; + */ +#define CONTEXT_HTIC (3.5*CONTEXT_DPI/72) +#define CONTEXT_VTIC CONTEXT_HTIC + +#endif /* TERM_PROTO */ + + +#ifndef TERM_PROTO_ONLY +#ifdef TERM_BODY + +/********* + * UNITS * + ********* + * + * This part of code is meant for parsing the units in + * set term context size 5in,3in + * or even + * set term context size (5+3/2) cm, 8-2**2in + * which will make the picture 6.5 centimeters wide and 4 inches high. + * + * It is also used for (rather dirty) parsing of font sizes, + * which is needed for estimating character width & height. + * + * This code may be taken out of the terminal if anyone needs it. + * + * Similar functionality is provided for some drivers (such as PostScript), + * to parse terminal size, but is more limited and supports less units. + */ + +typedef enum size_unit { + UNIT_PT, /* point */ + UNIT_PC, /* pica: 1pc = 12pt */ + UNIT_IN, /* inch: 1in = 72.27pt */ + UNIT_BP, /* big point: 72bp = 1in */ + UNIT_CM, /* centimeter: 2.54cm = 1in */ + UNIT_MM, /* millimeter: 10mm = 1cm */ + UNIT_DD, /* didot point: 1157dd = 1238pt */ + UNIT_CC, /* cicero: 1cc = 12dd */ + /* ----- // --- [not absolute units] */ + UNIT_PX, /* pixel: [dpi resolution] * 1px = 1in */ + UNIT_RELATIVE, /* relative: no specific unit */ + UNIT_OTHER +} size_unit; + +static struct gen_table units[] = { + { "pt", UNIT_PT }, /* point */ + { "pc", UNIT_PC }, /* pica */ + { "in$ch", UNIT_IN }, /* inch */ + { "bp", UNIT_BP }, /* big point */ + { "cm", UNIT_CM }, /* centimeter */ + { "mm", UNIT_MM }, /* millimeter */ + { "dd", UNIT_DD }, /* didot point */ + { "cc", UNIT_CC }, /* cicero */ + { "px", UNIT_PX }, /* pixel */ + { NULL, UNIT_OTHER } +}; + +typedef struct dimension_t { + float value; + size_unit unit; +} dimension_t; + +static double convert_to_unit __PROTO((dimension_t from, size_unit to_unit)); +static char* unit_name __PROTO((size_unit unit)); +/* needs a better name; lookup_table should be OK for it, but only if unit is a token, + * otherwise the work has to be done "manually" */ +static size_unit string_to_unit __PROTO((char *unit_name)); + +/* -------------- + * string_to_unit + * -------------- + * + * Returns two-letter name for the unit + * (it can be used in TeX or metapost directly) + */ +static char* +unit_name(size_unit unit) +{ + switch(unit) { + case UNIT_PT: return "pt"; + case UNIT_PC: return "pc"; + case UNIT_IN: return "in"; + case UNIT_BP: return "bp"; + case UNIT_CM: return "cm"; + case UNIT_MM: return "mm"; + case UNIT_DD: return "dd"; + case UNIT_CC: return "cc"; + case UNIT_PX: return "px"; + default: return NULL; /* XXX: do we need some error handling here? */ + } +} + +/* -------------- + * string_to_unit + * -------------- + * + * Accepts a two-letter name for an unit + * and returns the unit type + */ +static size_unit +string_to_unit(char *name) +{ + /* invalid unit for no string or for a string of any other size than two */ + if (name == NULL) return UNIT_OTHER; + if (strlen(name) != 2) return UNIT_OTHER; + + switch (name[0]) { + case 'b': + if (name[1] == 'p') return UNIT_BP; + case 'c': + if (name[1] == 'c') return UNIT_CC; + if (name[1] == 'm') return UNIT_CM; + case 'd': + if (name[1] == 'd') return UNIT_DD; + case 'i': + if (name[1] == 'n') return UNIT_IN; + case 'm': + if (name[1] == 'm') return UNIT_MM; + case 'p': + if (name[1] == 'c') return UNIT_PC; + if (name[1] == 't') return UNIT_PT; + if (name[1] == 'x') return UNIT_PX; + default: + return UNIT_OTHER; + } +} + +/* --------------- + * convert_to_unit + * --------------- + * + * Converts the value (from.value) from one unit (from.unit) + * to another (to_unit). + * + * This "stupid" converter should be rewritten completely for better efficiency, + * although it currently works OK. It should be initilized only once. + */ +static double +convert_to_unit(dimension_t from, size_unit to_unit) +{ + /* conversion is done via inches + * conversion[UNIT_CM] returns how much inches is one cm (ie. 1/2.54) */ + double conversion[20]; /* XXX: shoud be sizeof(size_unit) */ + + if(from.unit == to_unit) { + return (double)from.value; + /* XXX: this is wrong, but I have no other idea what to do with it. + It's not yet used anywhere so far, so let's care about it + when the need arises. */ + } else if(to_unit == UNIT_RELATIVE || to_unit == UNIT_OTHER) { + return (double)from.value; + } + + conversion[UNIT_PT] = 1.0/72.27; + conversion[UNIT_PC] = 12.0*conversion[UNIT_PT]; + conversion[UNIT_IN] = 1.0; + conversion[UNIT_BP] = 1.0/72.0; + conversion[UNIT_CM] = 1.0/2.54; + conversion[UNIT_MM] = conversion[UNIT_CM]/10.0; + conversion[UNIT_DD] = 1238.0/1157.0*conversion[UNIT_PT]; + conversion[UNIT_CC] = 12.0*conversion[UNIT_DD]; + /* for pixel some resolution would be needed */ + conversion[UNIT_PX] = 1/72.0; /* XXX: wrong! (but not used anywhere yet) */ + + return from.value*conversion[from.unit]/conversion[to_unit]; +} + +/**************** + * End of UNITS * + ****************/ + +static void CONTEXT_params_reset __PROTO((void)); + +static void CONTEXT_adjust_dimensions __PROTO((void)); +static void CONTEXT_fontstring_parse __PROTO((char *from_string, char *to_string, int to_size, dimension_t *dimension)); + +static void CONTEXT_startpath __PROTO((void)); +static void CONTEXT_endpath __PROTO((void)); + +static void CONTEXT_write_palette __PROTO((t_sm_palette *palette)); +static void CONTEXT_write_palette_gradient __PROTO((gradient_struct *gradient, int cnt)); + + +/* Each number is divided by 100 (1/100th of a point is drawn) */ + +static int CONTEXT_posx; +static int CONTEXT_posy; +/* remembers where we started the path: + * if we finish it in the same point, the path is closed with --cycle + * + * After I impplemented this, the functionality has been added to gnuplot core + * and PostScript terminal uses it, but to be on the safe side, + * I prefer not to add last-minute patches which could potentially break something. + * (the code written here proved to be rather safe & working so far) + * In the next version the core functionality should be integrated & tested. + */ +static int CONTEXT_path_start_x; +static int CONTEXT_path_start_y; +/* fontname, fontsize */ +static char CONTEXT_font[MAX_ID_LEN+1] = ""; +static dimension_t CONTEXT_fontsize = {CONTEXT_FONTSIZE, UNIT_PT}; + +/* fontname,fontsize to be put next to font labels if needed */ +static char CONTEXT_font_explicit[2*MAX_ID_LEN+1] = ""; +/* this is only set to >0 if asked for explicitely (for example with set_font(",15pt")) */ +static dimension_t CONTEXT_fontsize_explicit = {0, UNIT_PT}; + +/* the last pointsize used (it will only be changed if it becomes different) */ +static double CONTEXT_old_pointsize = 1.0; +/* the last linewidth used (it will only be changed if it becomes different) */ +static double CONTEXT_old_linewidth = 1.0; +/* the last linetype used (it will only be changed if it becomes different) */ +static int CONTEXT_old_linetype = -3; +/* was the color changed explicitly? */ +static TBOOLEAN CONTEXT_color_changed = FALSE; + +/* counts the number of graphics */ +static int CONTEXT_counter = 0; + +/* the number of path nodes before a newline (doesn't really matter, + * could be set to 1000; check if any editors have problems with that) */ +#define CONTEXT_LINEMAX 4 + +/* if we're inside a path (unfinished line) then path_count > 0 + * (PDF has PDF_pathIsOpen) */ +static unsigned int CONTEXT_path_count = 0; +/* this true/false switch is used to help distinguish dots from paths */ +static unsigned int CONTEXT_path_is_dot = 0; + +/* angle of text rotation */ +static int CONTEXT_ang = 0; +/* left/middle/right text justification */ +static enum JUSTIFY CONTEXT_justify = LEFT; + +enum LINEJOIN { LINEJOIN_MITER, LINEJOIN_ROUND, LINEJOIN_BEVEL }; +enum LINECAP { LINECAP_BUTT, LINECAP_ROUND, LINECAP_SQUARE }; + +/* Pallete has to be stored for the usage in later plots */ +static t_sm_palette *CONTEXT_old_palette; + +/*********************/ +/* global parameters */ +/*********************/ + +/* btex ... etex is hopefully not needed any more (and not supported either) + * it may be removed, but it was left here for historical reasons + * and if I decide to extend this terminal to support plain metapost, + * it will be needed again. + * + * textext("...") is not needed and supported any more either, + * but was left here for historical reasons & (hidden) testing purposes + * It will be removed. + * + * At the beginning textext("...") has been used, but: + * - it was extremely inefficient + * - it caused only troubles (different settings etc.) + * Because of the second, btex ... etex has been provided as a slightly + * more reliable alternative. The first one hasn't been solved until + * a new command for text placement showed up. + * + * After Hans Hagen has patched it, he implemented a new command \sometxt{...}, + * which was one of the most crucial steps towards usability of this terminal. + * Without it the terminal was close-to-useless because of unbearably slow speed. + * + * See also "\sometxt" MyWay on contextgarden.net about that. + * Hans & Taco: thanks a lot for this wonderful macro!!! + */ +enum CONTEXT_TEXT { CONTEXT_TEXT_TEXTEXT, CONTEXT_TEXT_SOMETXT, CONTEXT_TEXT_BTEX }; +/* whether points are drawn with metapost or typeset with TeX (easy configurable) */ +enum CONTEXT_POINTS { CONTEXT_POINTS_WITH_METAPOST, CONTEXT_POINTS_WITH_TEX }; + +typedef struct CONTEXT_params_t { + dimension_t xsize; /* 5in */ + dimension_t ysize; /* 3in */ + TBOOLEAN standalone; /* false */ + char *header; /* "" */ + TBOOLEAN color; /* true */ + TBOOLEAN dashed; /* true */ + enum LINEJOIN linejoin; /* MITER */ + enum LINECAP linecap; /* BUTT */ + float scale_dashlength; /* 1.0 */ + float scale_linewidth; /* 1.0 */ + float scale_text; /* 1.0 */ + enum CONTEXT_TEXT text; /* CONTEXT_TEXT_SOMETXT */ + enum CONTEXT_POINTS points; /* CONTEXT_POINTS_WITH_METAPOST */ + char font[MAX_ID_LEN+1]; /* "" */ + dimension_t fontsize; /* 12pt */ +} CONTEXT_params_t; + + +#define CONTEXT_PARAMS_DEFAULT { \ + {CONTEXT_XSIZE_VALUE, CONTEXT_XSIZE_UNIT},\ + {CONTEXT_YSIZE_VALUE, CONTEXT_YSIZE_UNIT},\ + FALSE,\ + NULL,\ + TRUE,\ + TRUE,\ + LINEJOIN_MITER,\ + LINECAP_BUTT,\ + 1.0,\ + 1.0,\ + 1.0,\ + CONTEXT_TEXT_SOMETXT,\ + CONTEXT_POINTS_WITH_METAPOST,\ + "",\ + {CONTEXT_FONTSIZE, UNIT_PT}\ +} + +static CONTEXT_params_t CONTEXT_params = CONTEXT_PARAMS_DEFAULT; + +enum CONTEXT_id { + CONTEXT_OPT_DEFAULT, + CONTEXT_OPT_SIZE, + CONTEXT_OPT_SIZE_DEFAULT, + CONTEXT_OPT_INPUT, CONTEXT_OPT_STANDALONE, + CONTEXT_OPT_HEADER, CONTEXT_OPT_NOHEADER, + CONTEXT_OPT_COLOR, CONTEXT_OPT_MONOCHROME, + CONTEXT_OPT_DASHED, CONTEXT_OPT_SOLID, + CONTEXT_OPT_LINEJOIN_MITERED, CONTEXT_OPT_LINEJOIN_ROUNDED, CONTEXT_OPT_LINEJOIN_BEVELED, + CONTEXT_OPT_LINECAP_BUTT, CONTEXT_OPT_LINECAP_ROUNDED, CONTEXT_OPT_LINECAP_SQUARED, + CONTEXT_OPT_SCALE_DASHLENGTH, CONTEXT_OPT_SCALE_LINEWIDTH, CONTEXT_OPT_SCALE_TEXT, + CONTEXT_OPT_TEXT_SOMETXT, CONTEXT_OPT_TEXT_TEXTEXT, + CONTEXT_OPT_POINTS_WITH_METAPOST, CONTEXT_OPT_POINTS_WITH_TEX, + CONTEXT_OPT_DEFAULTFONT, + CONTEXT_OPT_FONT, + CONTEXT_OPT_OTHER +}; +static struct gen_table CONTEXT_opts[] = { + { "d$efault", CONTEXT_OPT_DEFAULT }, + { "size", CONTEXT_OPT_SIZE }, + { "defaultsize", CONTEXT_OPT_SIZE_DEFAULT }, + { "inp$ut", CONTEXT_OPT_INPUT }, + { "stand$alone", CONTEXT_OPT_STANDALONE }, + { "header", CONTEXT_OPT_HEADER }, + { "noheader", CONTEXT_OPT_NOHEADER }, + { "col$or", CONTEXT_OPT_COLOR }, + { "col$our", CONTEXT_OPT_COLOR }, + { "mono$chrome", CONTEXT_OPT_MONOCHROME }, + { "da$shed", CONTEXT_OPT_DASHED }, + { "so$lid", CONTEXT_OPT_SOLID }, + { "miter$ed", CONTEXT_OPT_LINEJOIN_MITERED }, + { "rounded", CONTEXT_OPT_LINEJOIN_ROUNDED }, + { "bevel$ed", CONTEXT_OPT_LINEJOIN_BEVELED }, + { "butt", CONTEXT_OPT_LINECAP_BUTT }, + { "round", CONTEXT_OPT_LINECAP_ROUNDED }, + { "square$d", CONTEXT_OPT_LINECAP_SQUARED }, + { "dashl$ength", CONTEXT_OPT_SCALE_DASHLENGTH }, + { "dl", CONTEXT_OPT_SCALE_DASHLENGTH }, + { "linew$idth", CONTEXT_OPT_SCALE_LINEWIDTH }, + { "lw", CONTEXT_OPT_SCALE_LINEWIDTH }, + { "textscale", CONTEXT_OPT_SCALE_TEXT }, + { "textext", CONTEXT_OPT_TEXT_TEXTEXT }, + { "sometxt", CONTEXT_OPT_TEXT_SOMETXT }, + { "pointswithmp", CONTEXT_OPT_POINTS_WITH_METAPOST}, + { "pointswithmetapost", CONTEXT_OPT_POINTS_WITH_METAPOST}, + { "pointswithtex", CONTEXT_OPT_POINTS_WITH_TEX}, + { "font", CONTEXT_OPT_FONT }, + { "defaultfont", CONTEXT_OPT_DEFAULTFONT }, + { NULL, CONTEXT_OPT_OTHER } +}; + +/* **************** + * CONTEXT_options* + * **************** + * + * The routine resets all the parameters of the terminal to their default + * value; the routine should be better implemented if it would use the values + * from "#define CONTEXT_PARAMS_DEFAULT", but I don't know how to do this; + * There must be a cleaner way, I'm sure. I double-checked that the values here + * and there agree with each other. + */ +static void +CONTEXT_params_reset() +{ + /* default size is 5x3 inches */ + CONTEXT_params.xsize.value = CONTEXT_XSIZE_VALUE; + CONTEXT_params.ysize.value = CONTEXT_YSIZE_VALUE; + CONTEXT_params.xsize.unit = CONTEXT_XSIZE_UNIT; + CONTEXT_params.ysize.unit = CONTEXT_YSIZE_UNIT; + + CONTEXT_params.standalone = FALSE; + if (CONTEXT_params.header) { + free(CONTEXT_params.header); + CONTEXT_params.header = NULL; + } + CONTEXT_params.color = TRUE; + CONTEXT_params.dashed = TRUE; + CONTEXT_params.linejoin = LINEJOIN_MITER; + CONTEXT_params.linecap = LINECAP_BUTT; + CONTEXT_params.scale_dashlength = 1.0; + CONTEXT_params.scale_linewidth = 1.0; + CONTEXT_params.scale_text = 1.0; + CONTEXT_params.text = CONTEXT_TEXT_SOMETXT; + CONTEXT_params.points = CONTEXT_POINTS_WITH_METAPOST; + CONTEXT_params.font[0] = 0; + CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; + CONTEXT_params.fontsize.unit = UNIT_PT; +} + + +/* **************** + * CONTEXT_options* + * **************** + * + * Parses "set term context [options]". + */ +TERM_PUBLIC void +CONTEXT_options() +{ + int i; + struct value a; + char *tmp_string; + char tmp_font[MAX_ID_LEN+1] = ""; + char tmp_term_options[MAX_LINE_LEN+1] = ""; + + enum size_unit tmp_unit[2]; + float tmp_float[2]; + dimension_t tmp_dimension; + + while (!END_OF_COMMAND) { + switch (lookup_table(&CONTEXT_opts[0], c_token)) { + case CONTEXT_OPT_DEFAULT: + c_token++; + /* there should be a better way to do it, but I don't know how */ + CONTEXT_params_reset(); + break; + /*---------------------------------------------------*/ + case CONTEXT_OPT_SIZE: + c_token++; + /* size {cm|in}, {cm|in} + * + * value of the xsize + * XXX: perhaps the values shouldn't be saved right away + * just take care that all the values supported here are supported in conversion and + * in output routine as well + */ + for (i=0; i<2; i++) { + tmp_float[i] = real(const_express(&a)); + /* check if the size is positive (minimal requirement) */ + if (tmp_float[i] <= 0) + int_error(c_token-1,"Size has to be positive."); + /* if unit (inch or cm) is used, use that one, otherwise interpret that value + * as if it was a relative to the default value (5in); ie: 1.5 means 7.5 inches */ + switch (lookup_table(&units[0], c_token)) { + case UNIT_IN: + case UNIT_CM: + case UNIT_MM: + tmp_unit[i] = lookup_table(&units[0], c_token); + c_token++; + /* read the comma after the first number */ + if (i==0) { + if (equals(c_token, ",")) { + c_token++; + } else { + int_error(c_token,"',' expected.\nSize has to be specified as\n\tsize {in|cm}, {in|cm}\nor as\n\tsize "); + } + } + break; + case UNIT_PT: + case UNIT_PC: + case UNIT_BP: + case UNIT_DD: + case UNIT_CC: + int_error(c_token,"Sorry, this unit is not supported. Use cm or in."); + break; + case UNIT_PX: + int_error(c_token,"Size for this terminal cannot be specified in pixels. Use cm or in."); + break; + /* if comma follows, interpret as relative unit + * if some other unit follows */ + default: + /* first run: if no unit is specified, we expect at least a comma */ + if (i==0) { + if (equals(c_token, ",")) { + c_token++; + /* default xsize is 5in - use the current size as relative value */ + tmp_float[0] *= CONTEXT_XSIZE_VALUE; + tmp_unit[0] = CONTEXT_XSIZE_UNIT; + } else { + tmp_float[1] = tmp_float[0]*CONTEXT_YSIZE_VALUE; + tmp_float[0] = tmp_float[0]*CONTEXT_XSIZE_VALUE; + tmp_unit[0] = CONTEXT_XSIZE_UNIT; + tmp_unit[1] = CONTEXT_YSIZE_UNIT; + i++; + } + /* second run: if no unit is specified, proceed to next command + * and interpret the expression as being relative to the default size */ + } else { + tmp_float[1] *= CONTEXT_YSIZE_VALUE; + tmp_unit[1] = CONTEXT_YSIZE_UNIT; + } + break; + } + } + CONTEXT_params.xsize.value = tmp_float[0]; + CONTEXT_params.ysize.value = tmp_float[1]; + CONTEXT_params.xsize.unit = tmp_unit[0]; + CONTEXT_params.ysize.unit = tmp_unit[1]; + break; + /*---------------------------------------------------*/ + case CONTEXT_OPT_SIZE_DEFAULT: + c_token++; + CONTEXT_params.xsize.value = CONTEXT_XSIZE_VALUE; + CONTEXT_params.ysize.value = CONTEXT_YSIZE_VALUE; + CONTEXT_params.xsize.unit = CONTEXT_XSIZE_UNIT; + CONTEXT_params.ysize.unit = CONTEXT_YSIZE_UNIT; + break; + case CONTEXT_OPT_INPUT: + c_token++; + CONTEXT_params.standalone = FALSE; + break; + case CONTEXT_OPT_STANDALONE: + c_token++; + CONTEXT_params.standalone = TRUE; + break; + + case CONTEXT_OPT_HEADER: + c_token++; + /* parse the string */ + tmp_string = try_to_get_string(); + if (!tmp_string) { + int_error(c_token,"String containing header information expected"); + /* only touch the options if the string is OK */ + } else { + /* remove the old header if any */ + if (CONTEXT_params.header) { + free(CONTEXT_params.header); + CONTEXT_params.header = NULL; + } + /* and set the new one if nonempty; + * empty header will be treated as 'noheader' */ + if (strlen(tmp_string) > 0) { + CONTEXT_params.header = tmp_string; + } else { + free(tmp_string); + } + } + break; + case CONTEXT_OPT_NOHEADER: + c_token++; + /* delete the header if it exists */ + if (CONTEXT_params.header) { + free(CONTEXT_params.header); + CONTEXT_params.header = NULL; + } + break; + case CONTEXT_OPT_COLOR: + c_token++; + CONTEXT_params.color = TRUE; + break; + case CONTEXT_OPT_MONOCHROME: + c_token++; + CONTEXT_params.color = FALSE; + break; + case CONTEXT_OPT_DASHED: + c_token++; + CONTEXT_params.dashed = TRUE; + break; + case CONTEXT_OPT_SOLID: + c_token++; + CONTEXT_params.dashed = FALSE; + break; + case CONTEXT_OPT_LINEJOIN_MITERED: + c_token++; + CONTEXT_params.linejoin = LINEJOIN_MITER; + break; + case CONTEXT_OPT_LINEJOIN_ROUNDED: + c_token++; + CONTEXT_params.linejoin = LINEJOIN_ROUND; + break; + case CONTEXT_OPT_LINEJOIN_BEVELED: + c_token++; + CONTEXT_params.linejoin = LINEJOIN_BEVEL; + break; + case CONTEXT_OPT_LINECAP_BUTT: + c_token++; + CONTEXT_params.linecap = LINECAP_BUTT; + break; + case CONTEXT_OPT_LINECAP_ROUNDED: + c_token++; + CONTEXT_params.linecap = LINECAP_ROUND; + break; + case CONTEXT_OPT_LINECAP_SQUARED: + c_token++; + CONTEXT_params.linecap = LINECAP_SQUARE; + break; + case CONTEXT_OPT_SCALE_DASHLENGTH: + c_token++; + CONTEXT_params.scale_dashlength = real(const_express(&a)); + break; + case CONTEXT_OPT_SCALE_LINEWIDTH: + c_token++; + CONTEXT_params.scale_linewidth = real(const_express(&a)); + break; + case CONTEXT_OPT_SCALE_TEXT: + c_token++; + CONTEXT_params.scale_text = real(const_express(&a)); + break; + case CONTEXT_OPT_DEFAULTFONT: + c_token++; + /* CONTEXT_params.font should be an empty string */ + CONTEXT_params.font[0] = 0; + /* default fontsize is 12pt */ + CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; + CONTEXT_params.fontsize.unit = UNIT_PT; + break; + /* since the other two options are disabled, it makes no sense any more - will disappear */ + case CONTEXT_OPT_TEXT_SOMETXT: + c_token++; + CONTEXT_params.text = CONTEXT_TEXT_SOMETXT; + break; + /* not supported by metapost macros any more - will disappear */ + case CONTEXT_OPT_TEXT_TEXTEXT: + c_token++; + /* CONTEXT_params.text = CONTEXT_TEXT_TEXTEXT; */ + int_warn(c_token,"textext is now deprecated: sometxt will be used instead!"); + break; +/* + * so old and deprecated that we don't want users + * to complain about its inefficiency + * + * but if this terminal is going to support plain metapost in the future, + * we'll need it again + * + case CONTEXT_OPT_BTEX: + c_token++; + CONTEXT_params.text = CONTEXT_TEXT_BTEX; + break; +*/ + case CONTEXT_OPT_POINTS_WITH_METAPOST: + c_token++; + CONTEXT_params.points = CONTEXT_POINTS_WITH_METAPOST; + break; + case CONTEXT_OPT_POINTS_WITH_TEX: + c_token++; + CONTEXT_params.points = CONTEXT_POINTS_WITH_TEX; + break; + /* + * The preferred way to set the font is to set it in a document itself, + * labels in gnuplot in graphs will then inherit that font. + * + * However, it is polite to tell gnuplot which size is going to be used, + * so that it can estimate size of text labels. + * + * Whenever you specify + * set term context font "fontname,14pt" (or "fontname,14") + * + * there are two possibilities: + * - if STANDALONE mode is on, then the whole string will be used as + * \setupbodyfont[fontname,14pt] + * somewhere on top of the document (you still have to make sure that you + * included the proper typescript, so that "fontname" will be recognised + * - in the other case (INPUT mode) only the font size will be used + * internally to estimate sizes of labels, but the font name + * won't be written anywhere + */ + case CONTEXT_OPT_FONT: + c_token++; + if ((tmp_string = try_to_get_string())) { + /* if string with font name (and size) has been provided, parse it, + * otherwise expect font size (as an integer {+units}) */ + if (tmp_string) { + /* + * copies font name from tmp_string to tmp_font, + * but leaves out font size and saves it to tmp_dimension instead + * + * if no font size is specified, then tmp_dimension.value is set to 0 + */ + CONTEXT_fontstring_parse(tmp_string, tmp_font, sizeof(tmp_font), &tmp_dimension); + /* copies font name to parameters */ + strncpy(CONTEXT_params.font, tmp_font, sizeof(CONTEXT_params.font)); + /* free allocated memmory */ + free(tmp_string); + + /* (TODO: this code repeats three times!!!) + * save font size: + * + * - if size > 0, copy + * - if size < 0, fix the size to default value (12pt) + * - if size = 0, ignore + */ + if (tmp_dimension.value > 0 && tmp_dimension.unit != UNIT_OTHER) { + CONTEXT_params.fontsize.value = tmp_dimension.value; + CONTEXT_params.fontsize.unit = tmp_dimension.unit; + } else if (tmp_dimension.value < 0 || tmp_dimension.unit == UNIT_OTHER) { + CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; + CONTEXT_params.fontsize.unit = UNIT_PT; + } + /* optional font size at the end */ + /* + * XXX: this code repeats twice, which is a bad programming habit! + * (plese be polite and provide a function instead) + */ + if (isanumber(c_token)) { + /* fontsize */ + tmp_dimension.value = real(const_express(&a)); + tmp_dimension.unit = UNIT_PT; + /* optional dimension of font size */ + switch (lookup_table(&units[0], c_token)) { + case UNIT_IN: + case UNIT_CM: + case UNIT_MM: + case UNIT_PT: + case UNIT_PC: + case UNIT_BP: + case UNIT_DD: + case UNIT_CC: + tmp_dimension.unit = lookup_table(&units[0], c_token); + c_token++; + break; + case UNIT_PX: + int_error(c_token,"Fontsize cannot be specified in pixels. Use other unit or leave dimension out."); + break; + default: + /* apparently no unit has been specified */ + break; + } + } + /* + * save font size: + * + * - if size > 0, copy + * - if size < 0, fix the size to default value (12pt) + * - if size = 0, ignore + */ + if (tmp_dimension.value > 0 && tmp_dimension.unit != UNIT_OTHER) { + CONTEXT_params.fontsize.value = tmp_dimension.value; + CONTEXT_params.fontsize.unit = tmp_dimension.unit; + } else if (tmp_dimension.value < 0 || tmp_dimension.unit == UNIT_OTHER) { + CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; + CONTEXT_params.fontsize.unit = UNIT_PT; + } + } + /* font size (and possibly dimension) */ + } else { + /* used to be: + * int_error(c_token,"String containing font name and/or size expected"); + */ + /* fontsize */ + tmp_dimension.value = real(const_express(&a)); + tmp_dimension.unit = UNIT_PT; + /* optional dimension of font size */ + switch (lookup_table(&units[0], c_token)) { + case UNIT_IN: + case UNIT_CM: + case UNIT_MM: + case UNIT_PT: + case UNIT_PC: + case UNIT_BP: + case UNIT_DD: + case UNIT_CC: + tmp_dimension.unit = lookup_table(&units[0], c_token); + c_token++; + break; + case UNIT_PX: + int_error(c_token,"Fontsize cannot be specified in pixels. Use other unit or leave dimension out."); + break; + default: + /* apparently no unit has been specified */ + break; + } + /* + * save font size (same as above): + * + * - if size > 0, copy + * - if size < 0, fix the size to default value (12pt) + * - if size = 0, ignore + */ + if (tmp_dimension.value > 0) { + CONTEXT_params.fontsize.value = tmp_dimension.value; + CONTEXT_params.fontsize.unit = tmp_dimension.unit; + } else if (tmp_dimension.value < 0) { + CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; + CONTEXT_params.fontsize.unit = UNIT_PT; + } + } + break; + case CONTEXT_OPT_OTHER: + default: + /* error */ + int_error(c_token, "extraneous argument in set terminal %s",term->name); + break; + } + } + + /* current font size in pt (to be used in CONTEXT_adjust_dimensions) */ + CONTEXT_fontsize.value = CONTEXT_params.fontsize.value; + CONTEXT_fontsize.unit = CONTEXT_params.fontsize.unit; + + /* sets term->xmax, ymax, vchar, hchar */ + CONTEXT_adjust_dimensions(); + + snprintf(term_options, sizeof(term_options), + "size %g%s,%g%s %s %s", + CONTEXT_params.xsize.value, + unit_name(CONTEXT_params.xsize.unit), + CONTEXT_params.ysize.value, + unit_name(CONTEXT_params.ysize.unit), + CONTEXT_params.standalone ? "standalone" : "input", + CONTEXT_params.header == NULL ? "noheader \\\n " : "\\\n header "); + + if (CONTEXT_params.header != NULL) { + strncat(term_options,"\"", sizeof(term_options)-strlen(term_options)); + strncat(term_options,CONTEXT_params.header, sizeof(term_options)-strlen(term_options)); + strncat(term_options,"\" \\\n ", sizeof(term_options)-strlen(term_options)); + } + + strncat(term_options, + CONTEXT_params.color ? "color " : "monochrome ", + sizeof(term_options)-strlen(term_options)); + + switch(CONTEXT_params.linejoin) { + case LINEJOIN_MITER: + strncat(term_options, "mitered ", + sizeof(term_options)-strlen(term_options)); break; + case LINEJOIN_ROUND: + strncat(term_options, "rounded ", + sizeof(term_options)-strlen(term_options)); break; + case LINEJOIN_BEVEL: + strncat(term_options, "beveled ", + sizeof(term_options)-strlen(term_options)); break; + } + switch(CONTEXT_params.linecap) { + case LINECAP_BUTT : + strncat(term_options, "butt", + sizeof(term_options)-strlen(term_options)); break; + case LINECAP_ROUND : + strncat(term_options, "round", + sizeof(term_options)-strlen(term_options)); break; + case LINECAP_SQUARE: + strncat(term_options, "squared", + sizeof(term_options)-strlen(term_options)); break; + } + + snprintf(tmp_term_options, sizeof(tmp_term_options), + " %s dashlength %g linewidth %g textscale %g \\\n ", + CONTEXT_params.dashed ? "dashed" : "solid", + CONTEXT_params.scale_dashlength, + CONTEXT_params.scale_linewidth, + CONTEXT_params.scale_text + ); + strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); + +/* + * this is undocumented on purpose: + * - users are not supposed to use textext("...") any more, + * but it's left here if one decides to create a plain metapost terminal out of it + * + * switch(CONTEXT_params.text) { + * case CONTEXT_TEXT_BTEX : + * strncat(term_options, "btex ", + * sizeof(term_options)-strlen(term_options)); break; + * case CONTEXT_TEXT_SOMETXT : + * strncat(term_options, "sometxt ", + * sizeof(term_options)-strlen(term_options)); break; + * case CONTEXT_TEXT_TEXTEXT : + * strncat(term_options, "textext ", + * sizeof(term_options)-strlen(term_options)); break; + * } + */ + switch (CONTEXT_params.points) { + case CONTEXT_POINTS_WITH_TEX : + strncat(term_options, "pointswithtex ", + sizeof(term_options)-strlen(term_options)); break; + case CONTEXT_POINTS_WITH_METAPOST : + strncat(term_options, "pointswithmetapost ", + sizeof(term_options)-strlen(term_options)); break; + } + strncat(term_options,"font \"", sizeof(term_options)-strlen(term_options)); + strncat(term_options,CONTEXT_params.font, sizeof(term_options)-strlen(term_options)); + strncat(term_options,"\" ", sizeof(term_options)-strlen(term_options)); + + snprintf(tmp_term_options, sizeof(tmp_term_options), + "%g%s", CONTEXT_params.fontsize.value, unit_name(CONTEXT_params.fontsize.unit) + ); + strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); + snprintf(tmp_term_options, sizeof(tmp_term_options), + "(%d)", sizeof(term_options) + ); + strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); +} + +/* ************* + * CONTEXT_init* + * ************* + * + * Starts a new file. + * + * XXX: "set term context" multiple times will only include those graphics + * that were create before issuing a new "set term context options" + * this should be fixed in the core by reopening the file + * (removing previously written content). + * - PDF & binary terminals start a new file + * - PS & TeX-based terminals continue + */ +TERM_PUBLIC void +CONTEXT_init() +{ + time_t now; + char *timedate; + + CONTEXT_posx = CONTEXT_posy = 0; + CONTEXT_path_count = 0; + CONTEXT_path_is_dot = 0; + + CONTEXT_counter=0; + + /* + * XXX: Timoth\'ee warned me that this is not safe to use (portable); + * but PostScript terminal uses the same set of commands. + * Is there a way to write the timestamp in a compatible way? + */ + time(&now); + timedate=asctime(localtime(&now)); + timedate[strlen(timedate)-1]='\0'; + + fprintf(gpoutfile, "%% Written by ConTeXt terminal for GNUPLOT on: %s", asctime(localtime(&now))); + fprintf(gpoutfile, "%% GNUPLOT version %s patchlevel %s\n", gnuplot_version, gnuplot_patchlevel); + fprintf(gpoutfile, "%% See also http://wiki.contextgarden.net/Gnuplot\n%%\n"); + + /* place the header first if this is a standalone graphic */ + if(CONTEXT_params.standalone) { + /* load the gnuplot module */ + fputs("\\usemodule\n [gnuplot]\n", gpoutfile); + /* enable color (the only place where "color" is indeed used so far) */ + if (CONTEXT_params.color) { + fputs("\\setupcolors\n [state=start]\n", gpoutfile); + } + /* additional user-provided header information (if available) */ + if(CONTEXT_params.header) { + fprintf(gpoutfile, "%s\n", CONTEXT_params.header); + } + /* for some reason setting \bodyfontenvironment is needed, + * otherwise \switchtobodyfont[name] doesn't work OK */ + if (!(CONTEXT_params.fontsize.value == CONTEXT_FONTSIZE && CONTEXT_params.fontsize.unit == UNIT_PT)) { + fprintf(gpoutfile, "\\definebodyfontenvironment\n [%g%s]\n", + CONTEXT_params.fontsize.value, + unit_name(CONTEXT_params.fontsize.unit)); + } + /* set the proper font: \setupbodyfont[{fontname,}fontsize sizeunit] */ + fprintf(gpoutfile, "\\setupbodyfont\n [%s%s%g%s]\n", + CONTEXT_params.font, + /* write a comma only if the last string was non-empty */ + ((strlen(CONTEXT_params.font)>0) ? "," : ""), + CONTEXT_params.fontsize.value, + unit_name(CONTEXT_params.fontsize.unit)); + + /*---------* + * options * + *---------*/ + fprintf(gpoutfile, "\\setupGNUPLOTterminal\n [context]\n ["); + + /* color (gp_use_color): yes/no (true/false) + * default: yes + * - doesn't do anything useful yet; + * and besides that, it's already set in \setupcolors[state=start] */ + /* fprintf(gpoutfile, " color=%s, %% *yes* | no\n", CONTEXT_params.color ? "yes" : "no"); */ + + /* linejoin: mitered/rounded/beveled + * default: mitered */ + fprintf(gpoutfile, "linejoin="); + switch(CONTEXT_params.linejoin) { + case LINEJOIN_MITER: fprintf(gpoutfile, "mitered"); break; + case LINEJOIN_ROUND: fprintf(gpoutfile, "rounded"); break; + case LINEJOIN_BEVEL: fprintf(gpoutfile, "beveled"); break; + } + fprintf(gpoutfile, ", %% *mitered* | rounded | beveled\n"); + + /* linecap: butt/rounded/squared + * default: butt */ + fprintf(gpoutfile, " linecap="); + switch(CONTEXT_params.linecap) { + case LINECAP_BUTT : fprintf(gpoutfile, "butt"); break; + case LINECAP_ROUND : fprintf(gpoutfile, "rounded"); break; + case LINECAP_SQUARE: fprintf(gpoutfile, "squared"); break; + } + fprintf(gpoutfile, ", %% *butt* | rounded | squared\n"); + + /* dashed (gp_use_dashed): yes/no (true/false) + * default: yes */ + fprintf(gpoutfile, " dashed=%s, %% *yes* | no\n", CONTEXT_params.dashed ? "yes" : "no"); + /* dashlength (gp_scale_dashlength): 1.0 */ + fprintf(gpoutfile, " dashlength=%g, %% scaling factor for dash lengths\n", CONTEXT_params.scale_dashlength); + + /* linewidth (gp_scale_linewidth): 1.0 */ + fprintf(gpoutfile, " linewidth=%g, %% scaling factor for line widths (1.0 means 0.4bp)\n", CONTEXT_params.scale_linewidth); + + /* textscale (gp_scale_text): 1.0 */ + /* written out just for reference - it's commented out since it needs to be part of graphic + and affects estimation of label sizes */ + fprintf(gpoutfile, " %%textscale=%g, %% scaling factor for text labels\n", CONTEXT_params.scale_text); + + /* points (gp_points_with): metapost/tex (gp_points_with_metapost/gp_points_with_tex) + * default: metapost */ + fprintf(gpoutfile, " points=%s] %% *metapost* | tex (Should points be drawn with MetaPost or TeX?)\n", + CONTEXT_params.points == CONTEXT_POINTS_WITH_METAPOST ? "metapost" : "tex"); + + /*----------------* + * end of options * + *----------------*/ + + fputs("\n\\starttext\n\n", gpoutfile); + } else { + /* Sorry, nothing! In non-standalone graphic, parameters make no sense. + * Setup everything in the file which includes such a graphic instead. + */ + } +} + +/* *************** + * CONTEXT_reset * + * *************** + * + * finish writing the file + */ +TERM_PUBLIC void +CONTEXT_reset() +{ + /* we only have to end the document if this is a stand-alone graphic */ + if(CONTEXT_params.standalone) { + fputs("\\stoptext\n", gpoutfile); + } else { + /* This means that any subsequent plots to the same file will be ignored. + * I don't like that - gnuplot should delete the old contents instead, + * just as it does in case of PNG or PDF - + * but it will be at least consistent with standalone graphics that way + */ + fputs("\\endinput\n", gpoutfile); + } +} + +/* ************** + * CONTEXT_text * + * ************** + * + * Ends the current graphic. + */ +TERM_PUBLIC void +CONTEXT_text() +{ + /* close and draw the current path first */ + if (CONTEXT_path_count > 0) + CONTEXT_endpath(); + + fprintf(gpoutfile, "setbounds currentpicture to unitsquare xyscaled (w,h); %% %g %g %g %g\n", xoffset, yoffset, xsize, ysize); + + /* standalone graphic is a whole-page graphic */ + if(CONTEXT_params.standalone) { + fputs("\\stopGNUPLOTpage\n", gpoutfile); + /* otherwise we define a MPgraphic to be included later */ + } else { + fputs("\\stopGNUPLOTgraphic\n", gpoutfile); + } +} + +/* ****************** + * CONTEXT_graphics * + * ****************** + * + * Starts a new graphic. + */ +TERM_PUBLIC void +CONTEXT_graphics() +{ + /* standalone graphic is a whole-page graphic */ + if(CONTEXT_params.standalone) { + fprintf(gpoutfile, "\\startGNUPLOTpage %% Graphic Nr. %d\n", ++CONTEXT_counter); + /* otherwise we define a MPgraphic to be included later */ + } else { + /* the first parameter holds the graphic number */ + fprintf(gpoutfile, "\\startGNUPLOTgraphic[%d]\n", ++CONTEXT_counter); + } + + fprintf(gpoutfile, "gnuplotversion := \"%s\";\n", gnuplot_version); + /* + * MetaPost can only handle numbers up to 4096. Too high resolution + * would thus result in number overflow, that's why we scale down all the + * integers from gnuplot by 100 and multiply those numbers later by + * appropriate scaling factors 'a' and 'b' to get the proper dimensions. + */ + fprintf(gpoutfile, "%% width and height of the figure\nw:=%g%s; h:=%g%s;\n", + CONTEXT_params.xsize.value, unit_name(CONTEXT_params.xsize.unit), + CONTEXT_params.ysize.value, unit_name(CONTEXT_params.ysize.unit)); + fprintf(gpoutfile, "%% horizontal and vertical scaling factors\na:=w/%.2f; b:=h/%.2f;\n", + (term->xmax)/100.0, (term->ymax)/100.0); + fprintf(gpoutfile, "%% temporary variable for storing the path\nsave p; path p;\n%%\n"); + fprintf(gpoutfile, "%% -------------------------\n"); + fprintf(gpoutfile, "%% Different initialisations\n"); + fprintf(gpoutfile, "%% -------------------------\n"); + fprintf(gpoutfile, "%% for additional user-defined settings\ngp_setup_before;\n"); + /* needed (depends on terminal settings & needs to be passed) */ + fprintf(gpoutfile, "%% text scaling factor for the whole figure\n"); + fprintf(gpoutfile, "gp_scale_text := %g;\n", CONTEXT_params.scale_text); + fprintf(gpoutfile, "%% pointsize scaling factor\n"); + fprintf(gpoutfile, "gp_set_pointsize(%g);\n", CONTEXT_old_pointsize); + /* needed (provided by terminal) */ + fprintf(gpoutfile, "%% linewidth scaling factor for individual lines\n"); + fprintf(gpoutfile, "gp_set_linewidth(%g);\n", CONTEXT_old_linewidth); + fprintf(gpoutfile, "%% for additional user-defined settings\ngp_setup_after;\n"); + fprintf(gpoutfile, "%% -------------------------\n"); + + /* since palette is initialized only once, subsequent plots wouldn't see it + * unless we write it on the top of relevant plots explicitely */ + if(is_plot_with_palette()) { + CONTEXT_write_palette(CONTEXT_old_palette); + } + + /* needed, otherwise the first linetype(-2) would be ignored */ + CONTEXT_old_linetype = -3; + /* different initializations - not really needed, but they cannot hurt */ + CONTEXT_posx = CONTEXT_posy = 0; + CONTEXT_path_count = 0; + CONTEXT_path_is_dot = 0; +} + +/* --------------- + * CONTEXT_endpath + * --------------- + * + * Closes and strokes (draws) the current path. + * + * It the path ends where it started, it ends it with --cycle (we get cyclic path), + * otherwise just with a semicolon. + */ +static void +CONTEXT_endpath() +{ + /* if we have a dot, draw only the dot */ + if(CONTEXT_path_is_dot) { + fprintf(gpoutfile, "gp_dot(%.2fa,%.2fb);\n", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); + CONTEXT_path_is_dot = 0; + + /* cyclic path, so let's end it with "--cycle" */ + } else if((CONTEXT_posx == CONTEXT_path_start_x) && (CONTEXT_posy == CONTEXT_path_start_y)) { + fputs("--cycle;\ngp_draw(p);\n", gpoutfile); + + /* regular non-cyclic path */ + } else { + fputs(";\ngp_draw(p);\n", gpoutfile); + } + + /* we're not inside path any more */ + CONTEXT_path_count = 0; +} + +/* ************** + * CONTEXT_move * + * ************** + * + * Remembers the new location for a path. + * + * If it doesn't differ from the current location, this is simply ignored. + * If we're still in the middle of path construction and location differs, + * it strokes the old path. + * + */ +TERM_PUBLIC void +CONTEXT_move(unsigned int x, unsigned int y) +{ + /* we seem to be there already */ + if((x == CONTEXT_posx) && (y == CONTEXT_posy)) + return; + + /* close and draw the current path before the move */ + if(CONTEXT_path_count > 0) + CONTEXT_endpath(); + + CONTEXT_posx = x; + CONTEXT_posy = y; +} + +/* ----------------- + * CONTEXT_startpath + * ----------------- + * + * Starts ([but not yet] drawing) a new path. + */ +static void +CONTEXT_startpath() +{ + CONTEXT_path_start_x = CONTEXT_posx; + CONTEXT_path_start_y = CONTEXT_posy; + CONTEXT_path_count = 2; + + fprintf(gpoutfile, "p := (%.2fa,%.2fb)", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); +} + +/* **************** + * CONTEXT_vector * + * **************** + * + * Prolongs the current path for an additional line (from the last point to (x,y)) + * unless it has a zero-length. + * + * Points have to be treated as a special case, since gnuplot sometimes tries + * to draw them as moveto(a,b)--lineto(a,b) - without the proper linecap, + * that wouldn't draw anything in MetaPost or PS. + * (I hope that I got that part right, but I'm not completely sure.) + */ +TERM_PUBLIC void +CONTEXT_vector(unsigned int x, unsigned int y) +{ + /* this is zero-length line (or a dot) + * + * stupid background, has to handle + * - move(0,0), vector(0,0), whatever: draw a dot + * - move(0,0), vector(0,0), vector (1,1): draw a line + */ + if((x == CONTEXT_posx) && (y==CONTEXT_posy)) { + /* as long as this is still a dot candidate, mark it so + * however - further vector() commands may set this back to 0 */ + if(CONTEXT_path_count == 0) { + CONTEXT_path_is_dot = 1; + } + /* if some path was already drawn up to this place, ignore (it's not a dot) */ + return; + } + + /* start the path if none is started yet */ + if(CONTEXT_path_count == 0) { + /* the path is not a dot */ + CONTEXT_path_is_dot = 0; + CONTEXT_startpath(); + } else { + /* or prevent too long lines if you're in the middle of a path */ + if((CONTEXT_path_count % CONTEXT_LINEMAX) == 1) { + fputs("\n ", gpoutfile); + CONTEXT_path_count = 1; + } + } + + CONTEXT_posx = x; + CONTEXT_posy = y; + fprintf(gpoutfile, "--(%.2fa,%.2fb)", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); + CONTEXT_path_count++; +} + +/* ****************** + * CONTEXT_linetype * + * ****************** + * + * - If only color has been changed (recently), but not the linetype, + * it resets only the color. + * - If linetype was changed, it sets the new linetype + */ +TERM_PUBLIC void +CONTEXT_linetype(int lt) +{ + /* reset the color in case it has been changed in CONTEXT_set_color() */ + if (CONTEXT_old_linetype != lt || CONTEXT_color_changed) { + /* close and draw the current path first */ + if (CONTEXT_path_count > 0) + CONTEXT_endpath(); + + fprintf(gpoutfile, "gp_set_linetype(%d);\n", lt); + + CONTEXT_old_linetype = lt; + CONTEXT_color_changed = FALSE; + } +} + +/* ****************** + * CONTEXT_put_text * + * ****************** + * + * Places text labels. + */ +TERM_PUBLIC void +CONTEXT_put_text(unsigned int x, unsigned int y, const char str[]) +{ + const char *s; /* pointer to string */ + int alignment = 0; + char alignments[3][10] = {"center", "left", "right"}; + + /* ignore empty strings */ + if (!str || !strlen(str)) + return; + + /* close and draw the current path first */ + if (CONTEXT_path_count > 0) + CONTEXT_endpath(); + + /* see the values of "alignments" above */ + switch (CONTEXT_justify) { + case CENTRE: + alignment = 0; + break; + case LEFT: + alignment = 1; + break; + case RIGHT: + alignment = 2; + break; + } + + /* remove whitespaces at the beginning of string + * (usually problem in positive numbers) + * They interfere with positioning: whitespace at the beginning takes + * some place although it's invisible, so left-aligned and centered labels + * are posioned wrong otherwise. + * Example: + * -1 0 1 2: centered labels on x axis + * if we had a space in front, positive numbers would be "centered" in a weird way*/ + for(s=str; s[0] == ' '; s++); + + /* label position */ + fprintf(gpoutfile, "gp_put_text((%.2fa, %.2fb), ", x/100.0, y/100.0); + /* angle of rotation - optional and needed only if it's different from 0 */ + if (CONTEXT_ang != 0) { + fprintf(gpoutfile, "angle(%d), ", CONTEXT_ang); + } + /* alignment - "center" is optional, but we'll add it anyway */ + /* + \sometxt */ + fprintf(gpoutfile, "align(%s), \\sometxt[gp]", alignments[alignment]); + /* fontface/fontsize - optional second argument */ + if(CONTEXT_font_explicit[0] != 0) + fprintf(gpoutfile, "[%s]", CONTEXT_font_explicit); + /* finally the text label itself */ + fprintf(gpoutfile, "{%s});\n", s); +} + + +/* ******************** + * CONTEXT_text_angle * + * ******************** + * + * Saves text angle to be used for text labels. + */ +TERM_PUBLIC int +CONTEXT_text_angle(int ang) +{ + CONTEXT_ang = ang; + return TRUE; +} + +/* ********************** + * CONTEXT_justify_text * + * ********************** + * + * Saves horizontal text justification (left/middle/right) to be used for text labels. + */ +TERM_PUBLIC int +CONTEXT_justify_text(enum JUSTIFY mode) +{ + CONTEXT_justify = mode; + return TRUE; +} + +/* *************** + * CONTEXT_point * + * *************** + * + * There are two/three possible ways of drawing points in ConTeXt: + * - let the gnuplot draw the points with standard move/vector commands + * (points_default): not enabled + * - use the points predefined in mp-gnuplot.mp (drawn with metapost) + * which can easily be redefined + * (points_with_metapost) + * - use symbols from a symbol set predefined in m-gnuplot.tex (drawn with TeX) + * (points_with_tex) + * + * At first this routine took care of that, but now it's up to the high-level + * user interface to switch between the last two options. + */ +TERM_PUBLIC void +CONTEXT_point (unsigned int x, unsigned int y, int number) +{ + /* finish the current line first before the move */ + if(CONTEXT_path_count > 0) + CONTEXT_endpath(); + + fprintf(gpoutfile, "gp_point(%.2fa,%.2fb,%d);\n", x/100.0, y/100.0, number); + + /* + * This is how the points were draw at the beginning when macros + * for enclusion of TeX portions weren't as advanced as they're now. + * Currently there is some trickery involved which enabled simpler + * definitions of points that have to be typeset with TeX. + * + * See m-gnuplot.tex for details. + * + * This portion of code is left here for historical reasons. + * It might be needed again if clean metapost code will ever be produced + * with this terminal. + * + // use the points defined in the module (drawn with metapost) + if(CONTEXT_params.points == CONTEXT_POINTS_WITH_METAPOST) { + fprintf(gpoutfile, "gp_point(%.2fa,%.2fb,%d);\n", x/100.0, y/100.0, number); + // use the points defined in the module (typeset with TeX) + } else if(CONTEXT_params.points == CONTEXT_POINTS_WITH_TEX) { + // change \sometxt{\GPpoint{N}} to \GPpoint{N} + fprintf(gpoutfile, "gp_point(%.2fa,%.2fb,\\sometxt{\\GPpoint{%d}});\n", x/100.0, y/100.0, number); + } else { + // should not happen unless there's an error in this terminal + // or, if one will ever want the old "btex ... etex" functionality, + // it might fit in here + } + */ +} + +/* *************** + * CONTEXT_arrow * + * *************** + * ConTeXt could draw nice arrows on its own and in such a way that user could + * simply redefine arrow heads in the template file. + * + * This function is left here just in case that anyoune would find it useful + * to define his own arrow-drawing commands. + * + * Currently it just calls the default gnuplot function for drawing arrows. + */ +TERM_PUBLIC void +CONTEXT_arrow (unsigned int sx, unsigned int sy, unsigned int ex, unsigned int ey, int head) +{ + do_arrow(sx, sy, ex, ey, head); +} + +/* ------------------------ + * CONTEXT_fontstring_parse + * ------------------------ + * + * Parses from_string, which is divided by commas (,) + * and copies the whole content to to_string except for the part + * which starts with a numeric value. + * + * That value is interpreted separately, saved into dimension + * and used for setting the proper font size. + * + * Only the latest valid dimension provided is stored. + * If dimension->value is 0, it means that no font size has been provided in the string + * + * to_size is the size of to_string which may not be exceeded while copying the string + */ +static void +CONTEXT_fontstring_parse(char *from_string, char *to_string, int to_size, dimension_t *dimension) +{ + int i; + char c; + char tmp_fontsize[MAX_ID_LEN+1], unit[MAX_ID_LEN+1]; + /* temporary place for storing dimenstion - + * we will only store the latest valid dimension + * and ignore any other fields starting with a number and not containing a valid dimension */ + dimension_t tmp_dimension; + float size; + + /* positions in input string between two commas (or start/stop) */ + int from_start=0, from_stop=0; + /* last used position in output string */ + int to_stop=0; + + /* font size zero is forbidden anyway; + * the output value zero means: no size has been set, so this field will be ignored */ + dimension->value = 0; + dimension->unit = UNIT_PT; + + /* let's walk from comma to comma (or from start to stop) + * and analyse content of each string separately */ + /* as long as we don't hit the end of the string */ + for (; (from_stop < strlen(from_string)) && (to_stop < to_size); from_start = ++from_stop) { + /* find next specification of font name/family/size/... between two commas */ + while (from_string[from_stop] != 0 && from_string[from_stop] != ',') from_stop++; + + /* if this looks like a real number (+ optional unit), parse interpret it as font size */ + if (from_string[from_start] == '.' || (from_string[from_start]+1 > '0' && from_string[from_start]-1 < '9')) { + /* copy the content to tmp_string first */ + for (i=0; i < sizeof(tmp_fontsize) && i < (from_stop-from_start); i++) { + tmp_fontsize[i] = from_string[from_start+i]; + } + tmp_fontsize[i] = 0; + /* parse the value & unit */ + unit[0] = 0; /* if no unit is given, we should get an empty string */ + sscanf(tmp_fontsize, "%f%s", &size, unit); + tmp_dimension.value = size; + /* if no unit given - default to pt */ + if (unit == NULL || strlen(unit)==0) { + tmp_dimension.unit = UNIT_PT; + /* otherwise determine the unit first */ + } else { + tmp_dimension.unit = string_to_unit(unit); + } + /* pixel is not a valid unit for a font size */ + if (tmp_dimension.unit == UNIT_PX) + tmp_dimension.unit = UNIT_OTHER; + + /* + * store the size, but only if the size is valid and makes sense + * + * we want the font to be: + * - at least 3pt + * - at most half the height of the plot + */ + if(tmp_dimension.unit != UNIT_OTHER) { + if(convert_to_unit(tmp_dimension,UNIT_PT)*CONTEXT_params.scale_text > 3 && + convert_to_unit(tmp_dimension,UNIT_PT)*CONTEXT_params.scale_text < 0.5*convert_to_unit(CONTEXT_params.ysize,UNIT_PT)) { + dimension->value = tmp_dimension.value; + dimension->unit = tmp_dimension.unit; + } + } + + /* otherwise copy the content (almost) literally + * from_stop can be a comma or 0 - in either case we simply copy that one as well */ + } else { + for (i=from_start; i < from_stop+1; i++) { + c = from_string[i]; + /* TODO: improve this filter - remove any other dangerous characters + * Do we need to remove {} ? */ + if (!(c==']' || c=='[' || c=='$' || c=='#' || + c=='%' || c=='^' || c=='_' || c=='`' || + c=='\\' || c=='\n' || c=='\t' || c=='\r')) { + /* we may only write up to the pre-last character of to_string */ + if (to_stop < to_size) { + to_string[to_stop++] = c; + } + } + } + } + } + + /* it might have happened that the last character we wrote was a comma instead of 0, + * so we have to make sure that we finish the string */ + to_string[to_stop-1] = 0; +} + +/* ------------------------- + * CONTEXT_adjust_dimensions + * ------------------------- + * + * - sets the v_char and h_char based on current font size (approximation only) + * using CONTEXT_fontsize in points (from CONTEXT_params.fontsize) and CONTEXT_params.scale_text + * - sets xmax and ymax based on CONTEXT_params.xsize and CONTEXT_params.ysize + */ +static void +CONTEXT_adjust_dimensions() +{ + /* sets vertical dimension of characters based on current fontsize in pt */ + term->v_char = (unsigned int)((double)CONTEXT_DPI * + convert_to_unit(CONTEXT_fontsize,UNIT_IN) * CONTEXT_params.scale_text); + /* based on proportions of LM digits at 12pt */ + term->h_char = (unsigned int)(CONTEXT_LM_H_TO_V_RATIO*term->v_char); + + /* we might want to fix CONTEXT_DPI in case that the figure becomes too big */ + term->xmax = (unsigned int)((double)CONTEXT_DPI * convert_to_unit(CONTEXT_params.xsize,UNIT_IN) ); + term->ymax = (unsigned int)((double)CONTEXT_DPI * convert_to_unit(CONTEXT_params.ysize,UNIT_IN) ); +} + +/* ***************** + * CONTEXT_set_font* + * ***************** + * + * Official description: + * - empty string restores the terminal's default font + * - fonts are selected as strings "name,size", + * where size should be a floating point number by default, interpreted as "pt" (point) + * + * It's the user's own responsibility to make sure that the proper typescripts + * are included in the header, else the selected font won't work out-of-the-box + * + * The ConTeXt terminal should support things such as + * "iwona,12pt,ss" (sans serif) or + * "palatino,10dd" (10 didot points) or + * "tt" (typewriter) or + * as well + * + * The routine saves font name to CONTEXT_font + * and fontsize to CONTEXT_fontsize. + * + * The two are joined in CONTEXT_font_explicit for the usage in + * \sometxt[gp][fontname,fontsize]{label} + */ +TERM_PUBLIC int +CONTEXT_set_font(const char *font) +{ + char tmp_fontstring[MAX_ID_LEN+1] = ""; + + /* saves font name & family to CONTEXT_font */ + CONTEXT_fontstring_parse((char *)font, CONTEXT_font, sizeof(CONTEXT_font), &CONTEXT_fontsize_explicit); + strncpy(CONTEXT_font_explicit, CONTEXT_font, sizeof(CONTEXT_font_explicit)); + + /* valid fontsize has been provided */ + if (CONTEXT_fontsize_explicit.value > 0) { /* XXX: if valid */ + + CONTEXT_fontsize.value = CONTEXT_fontsize_explicit.value; + CONTEXT_fontsize.unit = CONTEXT_fontsize_explicit.unit; + + snprintf(tmp_fontstring, sizeof(tmp_fontstring), + ",%g%s", CONTEXT_fontsize_explicit.value, unit_name(CONTEXT_fontsize_explicit.unit) + ); + strncat(CONTEXT_font_explicit, tmp_fontstring, sizeof(CONTEXT_font_explicit)-strlen(CONTEXT_font_explicit)); + + /* no fontsize has been provided: switch back to default terminal fontsize */ + } else if (CONTEXT_fontsize_explicit.value == 0) { + CONTEXT_fontsize.value = CONTEXT_params.fontsize.value; + CONTEXT_fontsize.unit = CONTEXT_params.fontsize.unit; + } + + /* tell to gnuplot how big the fonts in labels are */ + CONTEXT_adjust_dimensions(); + + return TRUE; +} + +/* ******************* + * CONTEXT_pointsize * + * ******************* + * + * Sets the (relative) point size for subsequent points + * + * The base point size is defined "somewhere else": + * - depends on the font[size] used when "pointswithtex" option is on + */ +TERM_PUBLIC void +CONTEXT_pointsize(double pointsize) +{ + /* + * my first thought was not to allow negative sizes of points, + * but I see no reason why one shouldn't be able to play with + * inverted point shapes, so finally I commented this out again + * + * if(pointsize < 0) + * pointsize = 1; + */ + + if(CONTEXT_old_pointsize != pointsize) { + /* close and draw the current path first */ + if (CONTEXT_path_count>0) + CONTEXT_endpath(); + fprintf(gpoutfile, "gp_set_pointsize(%.3lf);\n", pointsize); + CONTEXT_old_pointsize = pointsize; + } +} + +/* ***************** + * CONTEXT_fillbox * + * ***************** + * + * Creates the path for the rectangle and calls the CONTEXT_fill(style) + * routine (shared with CONTEXT_filled_polygon) to actually fill that shape + */ +TERM_PUBLIC void +CONTEXT_fillbox(int style, unsigned int x1, unsigned int y1, unsigned int width, unsigned int height) +{ + /* close and draw the current path first */ + if (CONTEXT_path_count > 0) + CONTEXT_endpath(); + + /* create a new path */ + fprintf(gpoutfile, "p := unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", width/100.0, height/100.0, x1/100.0, y1/100.0); + + /* fills the box according to the "style" + * the code went out of this routine because of undocumented behaviour + * that also the filled_polygon should paint with patterns ... */ + CONTEXT_fill(style); +} + +/* ************** + * CONTEXT_fill * + * ************** + * + * Filling routine, yet another undocumented feature of Gnuplot. + * The code was mostly cloned from the PostScript terminal, + * so that you know whom to accuse if it doesn't do what you would expect + * it to do. (In case of cloning errors please inform the author of this terminal.) + * + * The lowest 4 bits of "style" seem to represent the style of filling + * (whether it is solid or pattern-based or whatever that might appear + * in that place in the future). + * + * The rest of the bits of "style" represent either the density + * (ranging from 0 to 100) or the number of the pattern that the polygon + * should be filled with. + * + * Used by CONTEXT_fillbox(...) and CONTEXT_filled_polygon(...) + */ +TERM_PUBLIC void +CONTEXT_fill(int style) +{ + int density; + int pattern; + + /* used in FS_[TRANSPARENT_]SOLID --> fill with intensity according to filldensity + it extracts a percentage out of "style" */ + density = (style >> 4); + if(density < 0) + density = 0; + if(density > 100) + density = 100; + + fputs("gp_fill(p", gpoutfile); + + /* do some strange trickery */ + switch(style & 0xf) { + + case FS_SOLID: + /* fill the box with density "density": if no parameter density is specified, + it implies 100% density by default */ + if (density < 100) { + fprintf(gpoutfile, ",density(%.2f)", density/100.0); + } + break; + case FS_PATTERN: + pattern = (style >> 4); + fprintf(gpoutfile, ",pattern(%d)", pattern); + break; + + default: /* style == 0 (FS_EMPTY) or unknown --> fill with background color */ + fprintf(gpoutfile, ",density(0)"); + } + /* TODO: FS_DEFAULT is missing; what should that one do? */ + + /* gp_fill(p,...); */ + fputs(");\n", gpoutfile); +} + +/* ******************* + * CONTEXT_linewidth * + * ******************* + * + * scale line width (similar to pointsize) + * remembers the values locally (no serious need for that actually) and writes them into file + */ +TERM_PUBLIC void +CONTEXT_linewidth(double linewidth) +{ + if(linewidth < 0) + linewidth = 1.0; + + if (CONTEXT_old_linewidth != linewidth) { + /* close and draw the current path first */ + if (CONTEXT_path_count>0) + CONTEXT_endpath(); + fprintf(gpoutfile, "gp_set_linewidth(%.3lf);\n", linewidth); + CONTEXT_old_linewidth = linewidth; + } +} + +/* ------------------------------ + * CONTEXT_write_palette_gradient + * ------------------------------ + * + * Writes the colors and positions for a gradient pallete. + */ +static void +CONTEXT_write_palette_gradient(gradient_struct *gradient, int cnt) +{ + int i; + + /* i-th color */ + fprintf(gpoutfile, "colors("); + for (i=0; i0) { fprintf(gpoutfile, ","); } + fprintf(gpoutfile, "(%.3g,%.3g,%.3g)", gradient[i].col.r, gradient[i].col.g, gradient[i].col.b); + } + + /* position of the i-th color */ + fprintf(gpoutfile, ");positions("); + for (i=0; i0) { fprintf(gpoutfile, ","); } + fprintf(gpoutfile, "%.4g", gradient[i].pos); + } + fprintf(gpoutfile, ")"); +} + +/* --------------------- + * CONTEXT_write_palette + * --------------------- + * + */ +static void +CONTEXT_write_palette (t_sm_palette *palette) +{ + if (palette == NULL) { + return; + } + +/* TODO + // Color models: RGB, HSV, CMY, YIQ, XYZ + // + // RGB: Red, Green, Blue + // HSV: Hue, Saturation, Value + // CMY + // + // two models that gnuplot uses, but which probably won't be supported by this terminal: + // YIQ: U.S. Commercial Television Briadcasting (almost RGB) + // XYZ: three primary colors of the color model defined by the 'Commission Internationale de l'Eclairage' (CIE) + // http://www.cs.rit.edu/~ncs/color/glossary.htm + // http://cs.fit.edu/wds/classes/cse5255/cse5255/davis/index.html +*/ + fprintf(gpoutfile, "gp_make_palette("); + switch(sm_palette.colorMode) { + /* grayscale only */ + case SMPAL_COLOR_MODE_GRAY: + fprintf(gpoutfile, "color_mode(gray)"); + // XXX: I'm not sure if something has to be done here or not + break; + /* one of several fixed transformations */ + case SMPAL_COLOR_MODE_RGB: + fprintf(gpoutfile, "color_mode(rgb);formulae(%d,%d,%d)", + sm_palette.formulaR, + sm_palette.formulaG, + sm_palette.formulaB); + break; + /* user defined transforms */ + case SMPAL_COLOR_MODE_FUNCTIONS: + fprintf(gpoutfile, "color_mode(functions)"); + break; + /* interpolated table: explicitely defined or read from file */ + case SMPAL_COLOR_MODE_GRADIENT: + fprintf(gpoutfile, "color_mode(gradient);"); + CONTEXT_write_palette_gradient(palette->gradient, palette->gradient_num); + break; + case SMPAL_COLOR_MODE_NONE: + break; + default: + break; + } + fprintf(gpoutfile, ");\n"); +} + +/* ********************* + * CONTEXT_make_palette* + * ********************* + * + * 1. if palette==NULL, then return nice/suitable + * maximal number of colours supported by this terminal. + * Returns 0 if it can make colours without palette (like + * postscript). + * 2. if palette!=NULL, then allocate its own palette + * return value is undefined + * 3. available: some negative values of max_colors for whatever + * can be useful + */ +TERM_PUBLIC int +CONTEXT_make_palette (t_sm_palette *palette) +{ + if (palette == NULL) { + return 0; /* ConTeXt can do continuous colors */ + } + + /* save the palette */ + CONTEXT_old_palette = palette; + + return 0; +} + + +/* + * most probably this one is not needed + * + * TERM_PUBLIC void + * CONTEXT_previous_palette(){} + * + */ + +/* ******************* + * CONTEXT_set_color * + * ******************* + * + * typedef struct t_colorspec { + * int type; // TC_ DEFAULT, LT, LINESTYLE, RGB, CB, FRAC, Z + * int lt; // used for TC_LT, TC_LINESTYLE and TC_RGB + * double value; // used for TC_CB and TC_FRAC + * } t_colorspec; + */ +TERM_PUBLIC void +CONTEXT_set_color (t_colorspec *colorspec) +{ + double gray, r, g, b; + +/* If (colorspec->type == TC_FRAC): + Set current color according to colorspec->value, where 0 <= value <= 1. + If using a palette, first map value to an integer i in the interval + [0...num_colors-1], then set to the ith color in the palette. + If (colorspec->type == TC_RGB): + Set current color to the rgb triple given in colorspec->lt. */ + + /* close and draw the current path first */ + if(CONTEXT_path_count > 0) + CONTEXT_endpath(); + + switch (colorspec->type) { + + /* TC_DEFAULT, TC_CB, TC_Z: probably unused; what about linestyle? */ + + /* color equal as that of linetype in colorspec->lt */ + case TC_LT: + fprintf(gpoutfile, "gp_set_color(lt(%d));\n", colorspec->lt); + CONTEXT_color_changed = TRUE; + break; + + /* rgb color */ + case TC_RGB: + r = (double)((colorspec->lt >> 16 ) & 255) / 255.; + g = (double)((colorspec->lt >> 8 ) & 255) / 255.; + b = (double)(colorspec->lt & 255) / 255.; + + fprintf(gpoutfile, "gp_set_color(rgb(%3.2f,%3.2f,%3.2f));\n", r, g, b); + CONTEXT_color_changed = TRUE; + break; + + /* map [0:1] to gray colors or to the corresponding color from the palette */ + case TC_FRAC: + gray = colorspec->value; + + /* limit negative and >1 values to [0:1] first */ + if(gray < 0) gray = 0; + if(gray > 1) gray = 1; + + fprintf(gpoutfile, "gp_set_color(frac(%.4f));\n", gray); + CONTEXT_color_changed = TRUE; + break; + default: + int_warn(NO_CARET,"context.trm set_color unknown colorspec->type %i", colorspec->type); + break; + } +} + + +/* ************************ + * CONTEXT_filled_polygon * + * ************************ + * + * Draws a polygon with the fill color set by set_color, and no border. + */ +TERM_PUBLIC void +CONTEXT_filled_polygon (int points, gpiPoint *corners) +{ + int i; + + /* nothing to be filled if less than 3 points */ + if(points < 3) + return; + + /* close and draw the current path first */ + if(CONTEXT_path_count > 0) { + CONTEXT_endpath(); + } + + /* create new path with corners */ + fputs("p := ", gpoutfile); + fprintf(gpoutfile, "(%.2fa,%.2fb)", corners[0].x/100.0, corners[0].y/100.0); + for (i=1; istyle); +} + +/* + * TODO: figure out first how this can be done/ + * additional support has to be implemented in pdfTeX and/or ConTeX to support + * efficient inclusion of bitmap images + * (this is a placeholder and a reminder that it has to me finished) + +#ifdef WITH_IMAGE + +TERM_PUBLIC void +CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imagecolor color_mode) +{ +} + +#endif +*/ + +/* + * TODO: Implement this function for smooth shading + * + * you need to fix draw_color_smooth_box(MODE_SPLOT) in graph3d.c -> color.c + * +static void +CONTEXT_draw_inside_color_smooth_box() +{ +} + */ + +#endif /* TERM_BODY */ + +#ifdef TERM_TABLE + +TERM_TABLE_START(context_driver) + "context", "ConTeXt with MetaFun (for PDF documents)", + CONTEXT_XMAX, CONTEXT_YMAX, CONTEXT_VCHAR, CONTEXT_HCHAR, + CONTEXT_VTIC, CONTEXT_HTIC, CONTEXT_options, CONTEXT_init, + CONTEXT_reset, CONTEXT_text, null_scale, CONTEXT_graphics, + CONTEXT_move, CONTEXT_vector, + CONTEXT_linetype, CONTEXT_put_text, CONTEXT_text_angle, + CONTEXT_justify_text, CONTEXT_point, CONTEXT_arrow, CONTEXT_set_font, + CONTEXT_pointsize, + TERM_BINARY /* flags */, 0 /* suspend */, 0 /* resume*/, /* XXX: why did I put binary here? */ + CONTEXT_fillbox, CONTEXT_linewidth +#ifdef USE_MOUSE + , 0, 0, 0, 0, 0 /* no mouse support */ +#endif /* USE_MOUSE */ + , CONTEXT_make_palette, + 0, /* XXX: CONTEXT_previous_palette: not sure if we need it at all (PS does "grestore") */ + CONTEXT_set_color, + CONTEXT_filled_polygon +#ifdef WITH_IMAGE + , 0 /* XXX: will be CONTEXT_image */ +#endif /* WITH_IMAGE */ +TERM_TABLE_END(context_driver) +#undef LAST_TERM +#define LAST_TERM context_driver + +#endif /* TERM_TABLE */ +#endif /* TERM_PROTO_ONLY */ + +#ifdef TERM_HELP +START_HELP(context) +"1 ConTeXt", +"?commands set terminal context", +"?set terminal context", +"?set term context", +"?terminal context", +"?term context", +"?context", +" ConTeXt is a macro package for TeX, highly integrated with Metapost", +" (for drawing figures) and intended for creation of high-quality PDF documents.", +" The terminal outputs Metafun source, which can be edited manually,", +" but you should be able to configure most things from outside.", +"", +" For an average user of ConTeXt + gnuplot module it's recommended to refer to", +" `Using ConTeXt` rather than reading this page", +" or to read the manual of the gnuplot module for ConTeXt.", +"", +" The `context` terminal supports the following options:", +" ", +" Syntax:", +" set term context {default}", +" {defaultsize | size |", +" size {in|cm}, {in|cm}}", +" {input | standalone}", +" {noheader | header \"
\"}", +" {color | colour | monochrome}", +" {mitered | rounded | beveled}", +" {butt | round | squared}", +" {dashed | solid}", +" {dashlength | dl
}", +" {linewidth | lw }", +" {textscale }", +" {pointswithmetapost | pointswithmp | pointswithtex}", +" {defaultfont | font {} |", +" font \"{,}\" {fontsize}}", +"", +" In non-standalone (`input`) graphic only parameters `size` to select graphic", +" size, `textscale` to scale all the labels for a factor ", +" and font size, make sense, the rest is silently", +" ignored and should be configured in the .tex file which inputs the graphic." +" It's highly recommended to set the proper fontsize if document font differs from", +" 12pt, so that gnuplot will know how much space to reserve for labels.", +"", +" `default` resets all the options to their default values.", +"", +" `defaultsize` sets the plot size to 5in,3in.\n", +" `size` sets the plot size to times .", +" If two arguments are given (separated with ','), the first one sets", +" the horizontal size and the second one the vertical size.", +" Size may be given without units (in which case it means relative to the default", +" value), with inches ('in') or centimeters ('cm').", +"", +" `input` (default) creates a graphic that can be included into another ConTeXt", +" document.\n", +" `standalone` adds some lines, so that the document might be compiled as-is.", +" You might also want to add `header` in that case.", +"", +" Use `header` for any additional settings/definitions/macros", +" that you might want to include in a standalone graphic. `noheader` is the default.", +"", +" `color` to make color plots is the default, but `monochrome` doesn't do anything special yet.", +" If you have any good ideas how the behaviour should differ to suit the monochrome printers better,", +" your suggestions are welcome.", +"", +" `mitered` (default), `rounded` and `beveled` control the shape of line joins.", +" `butt` (default), `round` and `squared` control the shape of line caps.", +" See PostScript or PDF Reference Manual for explanation. For wild-behaving functions", +" and thick lines", +" you might want to use `rounded` and `round` to prevent sharp corners in line joins.", +" (Some general support for this should be added to Gnuplot, so that the same options", +" could be set for each line (style) separately).", +"", +" `dashed` (default) uses different dash patterns for different line types," +" `solid` draws all plots with solid lines.", +"", +" `dashlength` or `dl` scales the length of the dashed-line segments by
.", +" `linewidth` or `lw` scales all linewidths by ." +" (lw 1 stands for 0.4bp, which is the default line width when drawing with Metapost.)", +" `textscale` scales text labels for factor relative to default document font.", +"", +" `pointswithmetapost` uses predefined point shapes, drawn in Metapost.", +" `pointswithtex` uses easily configurable set of symbols, defined with ConTeXt", +" in the following way:", +" \\defineconversion[my own points][+,{\\ss x},\\mathematics{\\circ}]", +" \\setupGNUPLOTterminal[context][points=tex,pointset=my own points]", +"", +" With `font` you can set font name and size in standalone graphics.", +" In non-standalone (`input`) mode only the font size is important", +" to reserve enough space for text labels.", +" set term context font \"myfont,ss\" 10pt", +" will result in", +" \\setupbodyfont[myfont,ss,10pt]", +" If you additionaly set `textscale` to 0.8 for example,", +" then the resulting font will be 8pt big and", +" set label ... font \"myfont,12pt\"", +" will come out as 9.6pt.\n", +" For compatibility with other terminals font size may be specified without units,", +" in which case the default unit \"pt\" will be appended to it, but you can also", +" use any other supported unit, for example \"11dd\".\n", +" However, it's your own responsibility to use the proper typescript,", +" otherwise switching the font will have no effect.\n", +" For an already supported font you can use", +" set terminal context standalone header '\\usetypescript[iwona][ec]' \\", +" font \"iwona,ss,11pt\"", +" If you also need your own typescripts, then you could write", +" set terminal context standalone \\", +" header '\\usetypescriptfile[type-myfont] \\usetypescript[myfont][uc]' \\", +" font \"myfont\" 10pt", +"", +" Note: The examples with typescripts above might become obsolete", +" with new versions of (pdf)TeX and already differ in XeTeX. They should be regarded", +" only as examples of the usage inside gnuplot.", +" Please take a look into ConTeXt documentation, wiki or mailing list (archives)", +" for any up-to-date information about font usage." +"", +" Examples:", +" set terminal context size 1.5 # 7.5in, 4.5in", +" set terminal context size 10cm, 5cm # 10cm, 5cm", +" set terminal context size 4/5, 2.54*4cm # 4in, 4in", +" For standalone (whole-page) plots with labels in UTF-8 encoding:", +" set terminal context standalone header '\\enableregime[utf-8]'", +"", +/* TODO: LaTeX formatting */ +"2 Requirements", +" - files from http://modules.contextgarden.net/gnuplot", +"", +" - please use the latest version of ConTeXt if possible (but at least one from 2007 or newer)", +"", +" - texmfstart has to be in PATH - teTeX is too old to process your files", +"", +" - if you want to call gnuplot on-the-fly, you need to have write18 enabled", +" (on most TeX distributionts this can be set with shell_escape=t in texmf.cnf)", +"", +" See http://wiki.contextgarden.net/Gnuplot for details about this terminal", +" and for more exhaustive help & examples.", +"2 Calling gnuplot from ConTeXt", +" The easiest way to make plots in ConTeXt documents is", +" \\usemodule[gnuplot]", +" \\starttext", +" \\section{How to make nice plots with {\\sc gnuplot}}", +" \\startGNUPLOTscript[sin]", +" plot sin(x)", +" \\stopGNUPLOTscript", +" \\useGNUPLOTgraphic[sin]", +" \\stoptext", +" This will run gnuplot automatically and include the resulting figure in the document." +END_HELP(context) +#endif /* TERM_HELP */ + From 39751a6c304eafe303fd2797aaa02b6237d5ce9e Mon Sep 17 00:00:00 2001 From: mojca Date: Mon, 22 Dec 2008 16:45:44 +0100 Subject: [PATCH 02/54] Fix (a)--(b)--(c)--(d)--(a)--cycle - the last point is not needed. --- term/context.trm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/term/context.trm b/term/context.trm index f06ec17e6..4742568bb 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2105,6 +2105,11 @@ CONTEXT_filled_polygon (int points, gpiPoint *corners) CONTEXT_endpath(); } + /* if the first point equals the last one, skip drawing the last point; --cycle does that already */ + if((corners[0].x == corners[points-1].x) && (corners[0].y == corners[points-1].y)) { + points--; + } + /* create new path with corners */ fputs("p := ", gpoutfile); fprintf(gpoutfile, "(%.2fa,%.2fb)", corners[0].x/100.0, corners[0].y/100.0); From a4dc0e0b33ea77f46b3d8da9be3d7a79db4dd24d Mon Sep 17 00:00:00 2001 From: mojca Date: Mon, 22 Dec 2008 16:47:33 +0100 Subject: [PATCH 03/54] just comment the last change --- term/context.trm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/term/context.trm b/term/context.trm index 4742568bb..ad7a41154 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2105,7 +2105,8 @@ CONTEXT_filled_polygon (int points, gpiPoint *corners) CONTEXT_endpath(); } - /* if the first point equals the last one, skip drawing the last point; --cycle does that already */ + /* if the first point equals the last one, skip the last point; --cycle does that already + * this condition is probably always true in gnuplot, so the if condition may not be needed */ if((corners[0].x == corners[points-1].x) && (corners[0].y == corners[points-1].y)) { points--; } From da02b5672eaf21f3936a98d79228657149dc846e Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Thu, 9 Jul 2009 19:32:38 +0200 Subject: [PATCH 04/54] added fallback in order to make palettes work until we start supporting them on metapost (low-level) --- term/context.trm | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/term/context.trm b/term/context.trm index ad7a41154..a5369571c 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1044,10 +1044,12 @@ CONTEXT_options() "%g%s", CONTEXT_params.fontsize.value, unit_name(CONTEXT_params.fontsize.unit) ); strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); + /* just for debugging: snprintf(tmp_term_options, sizeof(tmp_term_options), "(%d)", sizeof(term_options) ); strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); + */ } /* ************* @@ -2035,6 +2037,12 @@ TERM_PUBLIC void CONTEXT_set_color (t_colorspec *colorspec) { double gray, r, g, b; + /* ConTeXt doesn't offer full support for palettes yet + (I don't know how to trick metapost to accept the full palette specification) + so we convert the colors from palettes to RGB instead. + If terminal starts supporting palettes, this behaviour will change. + */ + rgb_color rgb1; /* If (colorspec->type == TC_FRAC): Set current color according to colorspec->value, where 0 <= value <= 1. @@ -2062,20 +2070,25 @@ CONTEXT_set_color (t_colorspec *colorspec) r = (double)((colorspec->lt >> 16 ) & 255) / 255.; g = (double)((colorspec->lt >> 8 ) & 255) / 255.; b = (double)(colorspec->lt & 255) / 255.; - + fprintf(gpoutfile, "gp_set_color(rgb(%3.2f,%3.2f,%3.2f));\n", r, g, b); CONTEXT_color_changed = TRUE; break; - + /* map [0:1] to gray colors or to the corresponding color from the palette */ case TC_FRAC: gray = colorspec->value; - + /* limit negative and >1 values to [0:1] first */ if(gray < 0) gray = 0; if(gray > 1) gray = 1; - - fprintf(gpoutfile, "gp_set_color(frac(%.4f));\n", gray); + + /* TODO: if ConTeXt start supporting palettes, we'll uncomment the following: */ + fprintf(gpoutfile, "%%gp_set_color(frac(%.4f));\n", gray); + /* but now it doesn't, so let's use the fallback instead: */ + rgb1maxcolors_from_gray(gray, &rgb1); + fprintf(gpoutfile, "gp_set_color(rgb(%3.2f,%3.2f,%3.2f));\n", rgb1.r, rgb1.g, rgb1.b); + CONTEXT_color_changed = TRUE; break; default: @@ -2104,13 +2117,13 @@ CONTEXT_filled_polygon (int points, gpiPoint *corners) if(CONTEXT_path_count > 0) { CONTEXT_endpath(); } - + /* if the first point equals the last one, skip the last point; --cycle does that already * this condition is probably always true in gnuplot, so the if condition may not be needed */ if((corners[0].x == corners[points-1].x) && (corners[0].y == corners[points-1].y)) { points--; } - + /* create new path with corners */ fputs("p := ", gpoutfile); fprintf(gpoutfile, "(%.2fa,%.2fb)", corners[0].x/100.0, corners[0].y/100.0); From 3e74d7f0a7ce47bec7f5a748eaea41c0ddc200d9 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Thu, 9 Jul 2009 20:56:28 +0200 Subject: [PATCH 05/54] add .gitignore --- .gitignore | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..9bc81f158 --- /dev/null +++ b/.gitignore @@ -0,0 +1,126 @@ +Makefile +Makefile.in +aclocal.m4 +autom4te.cache/ +config.h +config.hin +config.log +config.status +config/Makefile +config/Makefile.am +config/Makefile.in +configure +demo/Makefile +demo/Makefile.am +demo/Makefile.in +demo/binary1 +demo/binary2 +demo/binary3 +docs/Makefile +docs/doc2gih +docs/doc2gih.o +docs/gnuplot.gih +docs/termdoc.o +lisp/Makefile +lisp/Makefile.in +lisp/aclocal.m4 +lisp/autom4te.cache/ +lisp/config.log +lisp/config.status +lisp/configure +lisp/gnuplot-gui.elc +lisp/gnuplot.elc +m4/Makefile +m4/Makefile.am +m4/Makefile.in +man/Makefile +man/Makefile.in +share/Gnuplot +share/LaTeX/Makefile +share/LaTeX/Makefile.in +share/Makefile +share/Makefile.in +src/.deps/ +src/Makefile +src/Makefile.in +src/alloc.o +src/axis.o +src/beos/Makefile +src/bf_test +src/bf_test.o +src/binary.o +src/bitmap.o +src/breaders.o +src/color.o +src/command.o +src/contour.o +src/datafile.o +src/dynarray.o +src/eval.o +src/fit.o +src/gadgets.o +src/getcolor.o +src/getcolor_x11.o +src/gnuplot +src/gnuplot.lua +src/gnuplot_x11 +src/gp_cairo.o +src/gp_cairo_helpers.o +src/gpexecute.o +src/gplt_x11.o +src/graph3d.o +src/graphics.o +src/help.o +src/hidden3d.o +src/history.o +src/internal.o +src/interpol.o +src/matrix.o +src/misc.o +src/mouse.o +src/parse.o +src/plot.o +src/plot2d.o +src/plot3d.o +src/pm3d.o +src/readline.o +src/save.o +src/scanner.o +src/set.o +src/show.o +src/specfun.o +src/standard.o +src/stdfn.o +src/tables.o +src/tabulate.o +src/term.o +src/time.o +src/unset.o +src/util.o +src/util3d.o +src/variable.o +src/version.o +src/wxt_gui.o +src/wxterminal/Makefile +src/wxterminal/Makefile.in +stamp-h +stamp-h1 +term/Makefile +term/Makefile.am +term/Makefile.in +tutorial/Makefile +tutorial/Makefile.am +tutorial/Makefile.in +tutorial/eg1.tex +tutorial/eg2.tex +tutorial/eg3.tex +tutorial/eg4.tex +tutorial/eg5.tex +tutorial/eg6.tex +tutorial/eg7.eps +tutorial/eg7.tex +tutorial/test.tex +tutorial/tutorial.aux +tutorial/tutorial.dvi +tutorial/tutorial.log +tutorial/tutorial.toc From 8c54309e2ed8e6da6ba5e33ddce3be59d004bc25 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Thu, 9 Jul 2009 20:58:01 +0200 Subject: [PATCH 06/54] modify gitignore a bit --- .gitignore | 54 +----------------------------------------------------- 1 file changed, 1 insertion(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index 9bc81f158..ca736e749 100644 --- a/.gitignore +++ b/.gitignore @@ -43,64 +43,12 @@ share/Makefile.in src/.deps/ src/Makefile src/Makefile.in -src/alloc.o -src/axis.o +src/*.o src/beos/Makefile src/bf_test -src/bf_test.o -src/binary.o -src/bitmap.o -src/breaders.o -src/color.o -src/command.o -src/contour.o -src/datafile.o -src/dynarray.o -src/eval.o -src/fit.o -src/gadgets.o -src/getcolor.o -src/getcolor_x11.o src/gnuplot src/gnuplot.lua src/gnuplot_x11 -src/gp_cairo.o -src/gp_cairo_helpers.o -src/gpexecute.o -src/gplt_x11.o -src/graph3d.o -src/graphics.o -src/help.o -src/hidden3d.o -src/history.o -src/internal.o -src/interpol.o -src/matrix.o -src/misc.o -src/mouse.o -src/parse.o -src/plot.o -src/plot2d.o -src/plot3d.o -src/pm3d.o -src/readline.o -src/save.o -src/scanner.o -src/set.o -src/show.o -src/specfun.o -src/standard.o -src/stdfn.o -src/tables.o -src/tabulate.o -src/term.o -src/time.o -src/unset.o -src/util.o -src/util3d.o -src/variable.o -src/version.o -src/wxt_gui.o src/wxterminal/Makefile src/wxterminal/Makefile.in stamp-h From baa76e102c1edbfbbc259aea6e08a337e73794a4 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 12 Jul 2009 02:56:31 +0200 Subject: [PATCH 07/54] add support for trasparent fill --- term/context.trm | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/term/context.trm b/term/context.trm index a5369571c..ffcb592e9 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1862,13 +1862,20 @@ CONTEXT_fill(int style) /* do some strange trickery */ switch(style & 0xf) { + case FS_TRANSPARENT_SOLID: + /* just a flag to tell the terminal that density() should be used to interpret transparency */ + fprintf(gpoutfile, ",transparent"); case FS_SOLID: /* fill the box with density "density": if no parameter density is specified, it implies 100% density by default */ if (density < 100) { - fprintf(gpoutfile, ",density(%.2f)", density/100.0); + fprintf(gpoutfile, ",density(%.2f)", density*0.01); } break; + + case FS_TRANSPARENT_PATTERN: + /* just a flag that should be interpreted in metapost, bun no idea what transparent pattern means */ + fprintf(gpoutfile, ",transparent"); case FS_PATTERN: pattern = (style >> 4); fprintf(gpoutfile, ",pattern(%d)", pattern); From a3774990b9cf123be507398f9ac36a6485fefa7c Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 12 Jul 2009 17:27:48 +0200 Subject: [PATCH 08/54] WITH_IMAGE has been removed from terminals, so the terminal had to adapt to that --- term/context.trm | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/term/context.trm b/term/context.trm index ffcb592e9..88212cfd9 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2196,10 +2196,11 @@ TERM_TABLE_START(context_driver) , CONTEXT_make_palette, 0, /* XXX: CONTEXT_previous_palette: not sure if we need it at all (PS does "grestore") */ CONTEXT_set_color, - CONTEXT_filled_polygon -#ifdef WITH_IMAGE - , 0 /* XXX: will be CONTEXT_image */ -#endif /* WITH_IMAGE */ + CONTEXT_filled_polygon, + 0, /* image; TODO: implement it (CONTEXT_image) */ + 0, 0, 0, /* No enhanced text mode because this is TeX */ + 0, /* layer (used to signal front/back text, used in canvas, PS, epslatex, ...); XXX: figure out what this has to do */ + 0 /* path (Path control for end-joins of closed polygons on PostScript-like devices); TODO: implement it (CONTEXT_path) */ TERM_TABLE_END(context_driver) #undef LAST_TERM #define LAST_TERM context_driver From 5a9c9fe2d5205e3d340d0ee72ce0ce36c5d1cefe Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 12 Jul 2009 17:41:35 +0200 Subject: [PATCH 09/54] add TERM_MONOCHROME (for whatever the reason other terminals have added it) --- term/context.trm | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/term/context.trm b/term/context.trm index 88212cfd9..486b66285 100644 --- a/term/context.trm +++ b/term/context.trm @@ -717,10 +717,14 @@ CONTEXT_options() case CONTEXT_OPT_COLOR: c_token++; CONTEXT_params.color = TRUE; + /* just mimick other terminals; no idea what it does; + at the moment monochrome is not fully implemented either */ + term->flags &= ~TERM_MONOCHROME; break; case CONTEXT_OPT_MONOCHROME: c_token++; CONTEXT_params.color = FALSE; + term->flags |= TERM_MONOCHROME; break; case CONTEXT_OPT_DASHED: c_token++; From 0cf720d13bd1d1f886a517b51a33b780af468bed Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Mon, 13 Jul 2009 13:20:51 +0200 Subject: [PATCH 10/54] minor edits in comments --- term/context.trm | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/term/context.trm b/term/context.trm index 486b66285..4f035daa4 100644 --- a/term/context.trm +++ b/term/context.trm @@ -99,14 +99,11 @@ * Future plans: * - add missing functionality: * - improved support for palettes - * - binary images + * - support for binary images * - smooth shading in color bars - * - transparent colors and other color spaces + * - other color spaces * - gouraud shading (once implemented in 4.3) * - derive a better metapost terminal out of this one (to replace the old one) - * - a new terminal based on TukZ - * - compatible with LaTeX/ConTeX/plain TeX - * - works with e-TeX/pdfTeX/XeTeX/luaTeX/etc. */ #include "driver.h" From 63b521bc8572c561f7a3aa6a1970327786a44dae Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Wed, 26 Jan 2011 18:14:35 +0100 Subject: [PATCH 11/54] changes in lua.trm --- term/lua.trm | 91 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 38 deletions(-) diff --git a/term/lua.trm b/term/lua.trm index d49c9a051..d96ab284e 100644 --- a/term/lua.trm +++ b/term/lua.trm @@ -1,7 +1,7 @@ /* * $Id: lua.trm,v 1.11 2010/10/09 21:01:50 sfeam Exp $ * $Date: 2010/10/09 21:01:50 $ - * $Rev: 90 $ + * $Rev: 98 $ */ /* @@ -43,7 +43,7 @@ #include "driver.h" -#define LUA_TERM_REVISON "$Rev: 97 $" +#define LUA_TERM_REVISON "$Rev: 98 $" #ifdef TERM_REGISTER register_term(lua) @@ -346,11 +346,13 @@ LUA_GP_get_all_variables(lua_State *L) { lua_pushstring(L, "real"); lua_rawseti(L, -2, 2); } - +#ifdef HAVE_ISNAN if (isnan(val->v.cmplx_val.real)) { lua_pushnil(L); lua_rawseti(L, -2, 3); - } else { + } else +#endif + { lua_pushnumber(L, val->v.cmplx_val.real); lua_rawseti(L, -2, 3); } @@ -441,8 +443,6 @@ LUA_get_term_vars(void) { static void LUA_set_term_vars() { - extern const char lua_ident[]; - /* set term.version */ lua_pushstring(L, gnuplot_version); lua_setfield(L, luaterm, "gp_version"); @@ -453,12 +453,10 @@ LUA_set_term_vars() { lua_pushstring(L, LUA_TERM_REVISON); lua_setfield(L, luaterm, "lua_term_revision"); /* set term.lua_ident */ - lua_pushstring(L, lua_ident); + lua_pushstring(L, LUA_RELEASE); lua_setfield(L, luaterm, "lua_ident"); - lua_pushboolean(L, 1); - lua_setfield(L, luaterm, "IS_GNUPLOT_43"); - - /* some static definitions */ + + /* some static definitions from term_api.h */ lua_pushinteger(L, TERM_CAN_MULTIPLOT); /* tested if stdout not redirected */ lua_setfield(L, luaterm, "TERM_CAN_MULTIPLOT"); lua_pushinteger(L, TERM_CANNOT_MULTIPLOT); /* tested if stdout is redirected */ @@ -479,6 +477,16 @@ LUA_set_term_vars() { lua_setfield(L, luaterm, "TERM_CAN_CLIP"); lua_pushinteger(L, TERM_CAN_DASH); /* terminal knows dashed lines */ lua_setfield(L, luaterm, "TERM_CAN_DASH"); + lua_pushinteger(L, TERM_ALPHA_CHANNEL); /* alpha channel transparency */ + lua_setfield(L, luaterm, "TERM_ALPHA_CHANNEL"); + lua_pushinteger(L, TERM_MONOCHROME); /* term is running in mono mode */ + lua_setfield(L, luaterm, "TERM_MONOCHROME"); + lua_pushinteger(L, TERM_LINEWIDTH); /* support for set term linewidth */ + lua_setfield(L, luaterm, "TERM_LINEWIDTH"); +#ifdef TERM_FONTSCALE + lua_pushinteger(L, TERM_FONTSCALE); /* terminal supports fontscale */ + lua_setfield(L, luaterm, "TERM_FONTSCALE"); +#endif } static int @@ -676,17 +684,21 @@ LUA_options() int tc_off = c_token+1; /* token counter offset */ int need_init = 1; - /* 'set term tikz' is short for 'set term lua tikz' */ - if (c_token == 3) { - if (almost_equals(2,"termop$tions")) - need_init = 0; - else if (equals(2,"tikz")) + if (c_token == 3 && equals(2,"tikz")) { c_token--; + } else if (c_token == 2) { /* 'set termoptions' */ + if (!LUA_script) { + int_error(NO_CARET, "No Lua context for setting terminal options!"); + return; + } + need_init = 0; } - if (!END_OF_COMMAND) { opt_str = gp_input_line + token[c_token].start_index; + + if (need_init) { + if (!END_OF_COMMAND) { if (*opt_str == '"' || *opt_str == '\'') { s = try_to_get_string(); gp_expand_tilde(&s); @@ -710,21 +722,24 @@ LUA_options() LUA_script = s; } opt_str = gp_input_line + token[c_token].start_index; + /* FIXME: added by Ethan, but i think "c_token = num_tokens;" below does virtually the same here while (!END_OF_COMMAND) - c_token++; - - } else { + c_token++; + */ + } else { LUA_close(); int_error(NO_CARET, "No Lua driver name or file name given!"); } /* init lua when opening the terminal or on script change */ - if(need_init) { if(!LUA_init_lua()) { return; } } + /* since options are tokenized again in the script we always "finish" this here */ + c_token = num_tokens; + if(LUA_init_luaterm_function("options")) { /* isolate the "set term ...;" part of the command line */ opt_str = gp_strdup(opt_str); @@ -796,8 +811,8 @@ TERM_PUBLIC void LUA_move(unsigned int x, unsigned int y) { if(LUA_init_luaterm_function("move")) { - lua_pushinteger(L, x); - lua_pushinteger(L, y); + lua_pushinteger(L, (int)x); + lua_pushinteger(L, (int)y); LUA_call_report(lua_pcall(L, 2, 1, tb)); lua_term_result = (int)lua_tointeger(L, -1); lua_pop(L, 1); @@ -812,8 +827,8 @@ LUA_point(unsigned int x, unsigned int y, int number) lua_term_result = 0; if(LUA_init_luaterm_function("point")) { - lua_pushinteger(L, x); - lua_pushinteger(L, y); + lua_pushinteger(L, (int)x); + lua_pushinteger(L, (int)y); lua_pushinteger(L, number); LUA_call_report(lua_pcall(L, 3, 1, tb)); lua_term_result = (int)lua_tointeger(L, -1); @@ -839,8 +854,8 @@ TERM_PUBLIC void LUA_vector(unsigned int ux, unsigned int uy) { if(LUA_init_luaterm_function("vector")) { - lua_pushinteger(L, ux); - lua_pushinteger(L, uy); + lua_pushinteger(L, (int)ux); + lua_pushinteger(L, (int)uy); LUA_call_report(lua_pcall(L, 2, 1, tb)); lua_term_result = (int)lua_tointeger(L, -1); lua_pop(L, 1); @@ -861,10 +876,10 @@ LUA_arrow( lua_term_result = 0; if(LUA_init_luaterm_function("arrow")) { - lua_pushinteger(L, sx); - lua_pushinteger(L, sy); - lua_pushinteger(L, ex); - lua_pushinteger(L, ey); + lua_pushinteger(L, (int)sx); + lua_pushinteger(L, (int)sy); + lua_pushinteger(L, (int)ex); + lua_pushinteger(L, (int)ey); lua_pushinteger(L, head); /* additional vars */ lua_pushinteger(L, curr_arrow_headlength); /* access head length + angle (int) */ @@ -885,8 +900,8 @@ TERM_PUBLIC void LUA_put_text(unsigned int x, unsigned int y, const char str[]) { if(LUA_init_luaterm_function("put_text")) { - lua_pushinteger(L, x); - lua_pushinteger(L, y); + lua_pushinteger(L, (int)x); + lua_pushinteger(L, (int)y); lua_pushstring(L, str); LUA_call_report(lua_pcall(L, 3, 1, tb)); lua_term_result = (int)lua_tointeger(L, -1); @@ -956,10 +971,10 @@ LUA_boxfill (int style, unsigned int x1, if(LUA_init_luaterm_function("boxfill")) { lua_pushstring(L, LUA_get_fillstyle(style)); lua_pushinteger(L, style >> 4); - lua_pushinteger(L, x1); - lua_pushinteger(L, y1); - lua_pushinteger(L, width); - lua_pushinteger(L, height); + lua_pushinteger(L, (int)x1); + lua_pushinteger(L, (int)y1); + lua_pushinteger(L, (int)width); + lua_pushinteger(L, (int)height); LUA_call_report(lua_pcall(L, 6, 1, tb)); lua_term_result = (int)lua_tointeger(L, -1); lua_pop(L, 1); @@ -1216,7 +1231,7 @@ TERM_TABLE_START(tikz_driver) LUA_text, null_scale, LUA_graphics, LUA_move, LUA_vector, LUA_linetype, LUA_put_text, LUA_text_angle, LUA_justify_text, LUA_point, LUA_arrow, LUA_set_font, LUA_pointsize, - TERM_BINARY|TERM_IS_LATEX /*flags*/, 0 /*suspend*/, 0 /*resume*/, + TERM_BINARY /*flags*/, 0 /*suspend*/, 0 /*resume*/, LUA_boxfill, LUA_linewidth #ifdef USE_MOUSE , 0, 0, 0, 0, 0 From 185bdef8c596380b46b37bb04b101b3bda524e7e Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 28 Jan 2011 04:30:47 +0100 Subject: [PATCH 12/54] the latest version of lua/tikz terminal --- term/lua.trm | 10 +- term/lua/NEWS | 8 + term/lua/README | 44 +++- term/lua/TODO | 17 +- term/lua/gnuplot-tikz.lua | 541 ++++++++++++++++++++++++++++---------- 5 files changed, 453 insertions(+), 167 deletions(-) diff --git a/term/lua.trm b/term/lua.trm index d96ab284e..5e5b8ad43 100644 --- a/term/lua.trm +++ b/term/lua.trm @@ -1,7 +1,7 @@ /* * $Id: lua.trm,v 1.11 2010/10/09 21:01:50 sfeam Exp $ * $Date: 2010/10/09 21:01:50 $ - * $Rev: 98 $ + * $Rev: 99 $ */ /* @@ -43,7 +43,7 @@ #include "driver.h" -#define LUA_TERM_REVISON "$Rev: 98 $" +#define LUA_TERM_REVISON "$Rev: 99 $" #ifdef TERM_REGISTER register_term(lua) @@ -455,7 +455,7 @@ LUA_set_term_vars() { /* set term.lua_ident */ lua_pushstring(L, LUA_RELEASE); lua_setfield(L, luaterm, "lua_ident"); - + /* some static definitions from term_api.h */ lua_pushinteger(L, TERM_CAN_MULTIPLOT); /* tested if stdout not redirected */ lua_setfield(L, luaterm, "TERM_CAN_MULTIPLOT"); @@ -724,9 +724,9 @@ LUA_options() opt_str = gp_input_line + token[c_token].start_index; /* FIXME: added by Ethan, but i think "c_token = num_tokens;" below does virtually the same here while (!END_OF_COMMAND) - c_token++; + c_token++; */ - } else { + } else { LUA_close(); int_error(NO_CARET, "No Lua driver name or file name given!"); } diff --git a/term/lua/NEWS b/term/lua/NEWS index 2457ae1c3..5bb4b5010 100644 --- a/term/lua/NEWS +++ b/term/lua/NEWS @@ -1,3 +1,11 @@ +2011-01-23 + + * support for ConTeXt and plain TeX + * removed dependencies for LaTeX packages other than TikZ + * needs TikZ Version >= 2.0 + * better support for saving terminal options with gnuplot 'save' command + * fixed "overflow" on negative coordinates that are outside of the crop box + 2009-03-29 * Providing a script or target name is now mandatory. The new syntax: diff --git a/term/lua/README b/term/lua/README index 656c8823c..d15621e30 100644 --- a/term/lua/README +++ b/term/lua/README @@ -8,20 +8,11 @@ terminals in Lua instead of C. The first (and currently only) implemented `backend' is the PGF/TikZ terminal for the use with TeX/LaTeX. The actual terminal functionality is a provided by a separate Lua script -`gnuplot.lua'. The LaTeX style file is usually derived from PGF/TikZ terminal +`gnuplot-tikz.lua'. The LaTeX style file is usually derived from PGF/TikZ terminal script to keep things consistent. -Disclaimer -========== - -This terminal is tested with Lua 5.1 and gnuplot 4.2 and 4.3csv, -and since I am exclusively using Linux it is also mainly tested on -this platform. - - - Why all that hassle? ==================== @@ -57,21 +48,48 @@ Here is a brief list of reasons: For more reasons please have a look at the PGF/TikZ manual ;-) + +Generating style and help file +============================== + +To generate the style files and wrapper for the various TeX flavors enter + + lua gnuplot-tikz.lua style + +on the command line. The files generated should be + + t-gnuplot-lua-tikz.tex (Context wrapper) + gnuplot-lua-tikz.tex (plain TeX wrapper) + gnuplot-lua-tikz.sty (LaTeX wrapper) + gnuplot-lua-tikz-common.tex (common definitions) + +and can be copied to the appropriate places. + + lua gnuplot-tikz.lua help + +prints the terminal help on stdout and + + lua gnuplot-tikz.lua termhelp > gnuplot-tikz.help + +generates the version to be included in gnuplot help system. + + + Usage ===== The usage is quite similar to the other terminals with the difference that most terminal options are processed by the external Lua script. To see the list of additional script options just type - gnuplot> set term lua help + gnuplot> set term lua tikz help or - # lua gnuplot.lua + # lua gnuplot-tikz.lua on the command line. A minimalist gnuplot session (or script) may look like this: -> set term lua # activate the Lua terminal and load "gnuplot.lua" +> set term lua tikz # activate the Lua terminal and load "gnuplot.lua" > set out 'test.tex' # write output to file 'test.tex' > plot x*x # plot a nice parabola diff --git a/term/lua/TODO b/term/lua/TODO index 834451b6e..aba2bd2d3 100644 --- a/term/lua/TODO +++ b/term/lua/TODO @@ -1,25 +1,20 @@ TODO ==== -Autoconf - - * liblua is not always named liblua; can we autodetect its name? - EAM: Yes, via the AC_SEARCH_LIBS macro in configure.in - * where to install gnuplot-lua-tikz.sty? Versioning? - EAM: I moved this file to .../share/LaTeX, where there is already an - installation script for latex support files - Lua generic terminal * support color palettes (low priority, not really needed for TikZ?) * support enhanced text mode (low priority, not really needed for TikZ) * alpha channel support for image data (need to have a look)? EAM: Seems to work already. Did you have an example that fails? + PH: See http://gnuplot.sourceforge.net/demo_4.4/rgbalpha.html TikZ backend * alpha channel support for image data (need to have a look)? EAM: The demos for transparency seem to work already - * automatic detection of font sizes (via LaTeX test call?! very low priority) - * ConTeXt and plain TeX support - * switch to pgf 2.0 style system + PH: See http://gnuplot.sourceforge.net/demo_4.4/rgbalpha.html + * automatic detection of font sizes (via LaTeX test call?! very low + priority. It works with the gnuplottex package and the charsize option, + see style file for an example) + \ No newline at end of file diff --git a/term/lua/gnuplot-tikz.lua b/term/lua/gnuplot-tikz.lua index 058443539..e53bf21e9 100644 --- a/term/lua/gnuplot-tikz.lua +++ b/term/lua/gnuplot-tikz.lua @@ -37,9 +37,9 @@ - $Date: 2009/10/29 03:42:14 $ + $Date: 2009/06/05 05:37:04 $ $Author: sfeam $ - $Rev: 96 $ + $Rev: 97 $ ]]-- @@ -71,11 +71,17 @@ pgf.DEFAULT_TIC_SIZE = 0.18 pgf.DEFAULT_RESOLUTION = 1000 -- default font size in TeX pt pgf.DEFAULT_FONT_SIZE = 10 +-- default sizes for CM@10pt and default resolution +-- there is no need to adapt these values when changing +-- pgf.DEFAULT_FONT_SIZE or pgf.DEFAULT_RESOLUTION ! +pgf.DEFAULT_FONT_H_CHAR = 184 +pgf.DEFAULT_FONT_V_CHAR = 308 -pgf.LATEX_STYLE_FILE = "gnuplot-lua-tikz" -- \usepackage{gnuplot-lua-tikz} -pgf.REVISION = string.sub("$Rev: 96a $",7,-3) -pgf.REVISION_DATE = string.gsub("$Date: 2009/10/29 03:42:14 $", +pgf.STYLE_FILE_BASENAME = "gnuplot-lua-tikz" -- \usepackage{gnuplot-lua-tikz} + +pgf.REVISION = string.sub("$Rev: 97 $",7,-3) +pgf.REVISION_DATE = string.gsub("$Date: 2011/01/22 05:37:04 $", "$Date: ([0-9]+).([0-9]+).([0-9]+) .*","%1/%2/%3") pgf.styles = {} @@ -117,7 +123,7 @@ pgf.styles.plotstyles = { } pgf.styles.linetypes_axes = { - [1] = {"gp lt axes", "dashed"}, -- An lt of -1 is used for the X and Y axes. + [1] = {"gp lt axes", "dotted"}, -- An lt of -1 is used for the X and Y axes. [2] = {"gp lt border", "solid"}, -- An lt of -2 is used for the border of the plot. } @@ -134,19 +140,19 @@ pgf.styles.linetypes = { -- corresponds to pgf.styles.linetypes pgf.styles.lt_colors_axes = { - [1] = {"gp lt color axes", "black"}, + [1] = {"gp lt color axes", "black!30"}, [2] = {"gp lt color border", "black"}, } pgf.styles.lt_colors = { [1] = {"gp lt color 0", "red"}, - [2] = {"gp lt color 1", "green!60!black"}, + [2] = {"gp lt color 1", "green"}, [3] = {"gp lt color 2", "blue"}, [4] = {"gp lt color 3", "magenta"}, [5] = {"gp lt color 4", "cyan"}, - [6] = {"gp lt color 5", "orange"}, - [7] = {"gp lt color 6", "yellow!80!red"}, - [8] = {"gp lt color 7", "blue!80!black"} + [6] = {"gp lt color 5", "yellow"}, + [7] = {"gp lt color 6", "orange"}, + [8] = {"gp lt color 7", "purple"} } pgf.styles.patterns = { @@ -226,36 +232,28 @@ pgf.format_coord = function(xc, yc) end pgf.write_doc_begin = function(preamble) - gp.write("\\documentclass["..pgf.DEFAULT_FONT_SIZE.."pt]{article}\n" - .."\\usepackage[T1]{fontenc}\n" - .."\\usepackage{textcomp}\n\n" - .."\\usepackage[utf8x]{inputenc}\n\n" - .."\\usepackage{"..pgf.LATEX_STYLE_FILE.."}\n" - .."\\pagestyle{empty}\n" - .."\\usepackage[active,tightpage]{preview}\n" - .."\\PreviewEnvironment{tikzpicture}\n" - .."\\setlength\\PreviewBorder{2mm}\n" - ..preamble.."\n\n" - .."\\begin{document}\n") + gp.write(gfx.format[gfx.opt.tex_format].docheader) + gp.write(preamble) + gp.write(gfx.format[gfx.opt.tex_format].begindocument) end pgf.write_doc_end = function() - gp.write("\\end{document}\n") + gp.write(gfx.format[gfx.opt.tex_format].enddocument) end pgf.write_graph_begin = function (font, noenv) local global_opt = "" -- unused + if gfx.opt.full_doc then + gp.write(gfx.format[gfx.opt.tex_format].beforetikzpicture) + end if noenv then gp.write("%% ") -- comment out end - gp.write(string.format("\\begin{tikzpicture}[gnuplot%s]\n",global_opt)) + gp.write(string.format("%s[gnuplot%s]\n", gfx.format[gfx.opt.tex_format].begintikzpicture, global_opt)) gp.write(string.format("%%%% generated with GNUPLOT %sp%s (%s; terminal rev. %s, script rev. %s)\n%%%% %s\n", - term.gp_version, term.gp_patchlevel, - string.match(term.lua_ident, "Lua [0-9\.]+"), - string.sub(term.lua_term_revision,7,-3), - pgf.REVISION,os.date())) + term.gp_version, term.gp_patchlevel, _VERSION, string.sub(term.lua_term_revision,7,-3), pgf.REVISION,os.date())) if font ~= "" then - gp.write(string.format("\\tikzstyle{every node}+=[font=%s]\n", font)) + gp.write(string.format("\\tikzset{every node/.append style={font=%s}}\n", font)) end if not gfx.opt.lines_dashed then gp.write("\\gpsolidlines\n") @@ -269,7 +267,13 @@ pgf.write_graph_end = function(noenv) if noenv then gp.write("%% ") -- comment out end - gp.write("\\end{tikzpicture}\n") + if gfx.opt.full_doc then + gp.write(gfx.format[gfx.opt.tex_format].beforeendtikzpicture) + end + gp.write(gfx.format[gfx.opt.tex_format].endtikzpicture .. "\n") + if gfx.opt.full_doc then + gp.write(gfx.format[gfx.opt.tex_format].aftertikzpicture) + end end pgf.draw_path = function(t) @@ -380,10 +384,10 @@ end pgf.draw_fill = function(t, pattern, color, saturation, opacity) local fill_path = '' - local fill_style = '' + local fill_style = color if saturation < 100 then - gp.write("\\begin{colormixin}{"..saturation.."!white}\n") + fill_style = fill_style .. ",color=.!"..saturation; end fill_path = fill_path .. '('..pgf.format_coord(t[1][1], t[1][2])..')' @@ -410,7 +414,7 @@ pgf.draw_fill = function(t, pattern, color, saturation, opacity) if pattern == '' then -- solid fills - fill_style = 'color='..color +-- fill_style = 'color='..color if opacity < 100 then fill_style = fill_style..string.format(",opacity=%.2f", opacity/100) else @@ -418,7 +422,7 @@ pgf.draw_fill = function(t, pattern, color, saturation, opacity) end else -- pattern fills - fill_style = pattern..',pattern color='..color + fill_style = fill_style..','..pattern..',pattern color=.' end local out = '' if (pattern ~= '') and (opacity == 100) then @@ -429,10 +433,6 @@ pgf.draw_fill = function(t, pattern, color, saturation, opacity) else gp.write("\\gpfill{"..fill_style.."} "..fill_path..";\n") end - - if saturation < 100 then - gp.write("\\end{colormixin}\n") - end end pgf.draw_raw_rgb_image = function(t, m, n, ll, ur) @@ -487,13 +487,13 @@ pgf.draw_raw_cmyk_image = function(t, m, n, ll, ur) end pgf.write_clipbox_begin = function (ll, ur) - gp.write("\\begin{scope}\n") + gp.write(gfx.format[gfx.opt.tex_format].beginscope.."\n") gp.write(string.format("\\clip (%s) rectangle (%s);\n", pgf.format_coord(ll[1],ll[2]),pgf.format_coord(ur[1],ur[2]))) end pgf.write_clipbox_end = function() - gp.write("\\end{scope}\n") + gp.write(gfx.format[gfx.opt.tex_format].endscope.."\n") end pgf.write_boundingbox = function(t, num) @@ -511,28 +511,142 @@ pgf.write_variables = function(t) end -- write style to seperate file, or whatever... -pgf.create_style = function(f) -f:write([[ -%% -%% This is the style file for the gnuplot PGF/TikZ terminal +pgf.create_style = function() + local name_common = pgf.STYLE_FILE_BASENAME.."-common.tex" + local name_latex = pgf.STYLE_FILE_BASENAME..".sty" + local name_tex = pgf.STYLE_FILE_BASENAME..".tex" + local name_context = "t-"..pgf.STYLE_FILE_BASENAME..".tex" + +-- LaTeX + +local f_latex = io.open(name_latex, "w+") +f_latex:write([[ %% -%% It is associated with the 'gnuplot.lua' script, and usually generated -%% automatically. So take care whenever you make any changes! +%% LaTeX wrapper for gnuplot-tikz style file %% \NeedsTeXFormat{LaTeX2e} ]]) -f:write("\\ProvidesPackage{"..pgf.LATEX_STYLE_FILE.."}%\n") -f:write(" ["..pgf.REVISION_DATE.." (rev. "..pgf.REVISION..") GNUPLOT Lua terminal style]\n\n") -f:write([[ -\RequirePackage{tikz,xxcolor,ifpdf,ifxetex} +f_latex:write("\\ProvidesPackage{"..pgf.STYLE_FILE_BASENAME.."}%\n") +f_latex:write(" ["..pgf.REVISION_DATE.." (rev. "..pgf.REVISION..") GNUPLOT Lua terminal style]\n\n") +f_latex:write([[ +\RequirePackage{tikz} + +\usetikzlibrary{arrows,patterns,plotmarks} +]]) +f_latex:write("\\input "..name_common.."\n") +f_latex:write([[ + +\endinput +]]) +f_latex:close() + +-- ConTeXt + +local f_context = io.open(name_context, "w+") +f_context:write([[ +%% +%% ConTeXt wrapper for gnuplot-tikz style file +%% +\usemodule[tikz] + +\usetikzlibrary[arrows,patterns,plotmarks] + +\edef\tikzatcode{\the\catcode`\@} +\edef\tikzbarcode{\the\catcode`\|} +\edef\tikzexclaimcode{\the\catcode`\!} +\catcode`\@=11 +\catcode`\|=12 +\catcode`\!=12 + +]]) +f_context:write("\\input "..name_common.."\n") +f_context:write([[ + +\catcode`\@=\tikzatcode +\catcode`\|=\tikzbarcode +\catcode`\!=\tikzexclaimcode + +\endinput +]]) +f_context:close() + +-- plain TeX + +local f_tex = io.open(name_tex, "w+") +f_tex:write([[ +%% +%% plain TeX wrapper for gnuplot-tikz style file +%% +\input tikz.tex \usetikzlibrary{arrows,patterns,plotmarks} +\edef\tikzatcode{\the\catcode`\@} +\catcode`\@=11 + +]]) +f_tex:write("\\input "..name_common.."\n\n") +f_tex:write([[ + +\catcode`\@=\tikzatcode + +\endinput +]]) +f_tex:close() + +-- common + +local f = io.open(name_common, "w+") +f:write([[ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% +%% Common style file for TeX, LaTeX and ConTeXt %% +%% It is associated with the 'gnuplot.lua' script, and usually generated +%% automatically. So take care whenever you make any changes! %% +% FIXME: is there a more elegant way to determine the output format? + +\def\pgfsysdriver@a{pgfsys-dvi.def} % ps +\def\pgfsysdriver@b{pgfsys-dvipdfm.def} % pdf +\def\pgfsysdriver@c{pgfsys-dvipdfmx.def} % pdf +\def\pgfsysdriver@d{pgfsys-dvips.def} % ps +\def\pgfsysdriver@e{pgfsys-pdftex.def} % pdf +\def\pgfsysdriver@f{pgfsys-tex4ht.def} % html +\def\pgfsysdriver@g{pgfsys-textures.def} % ps +\def\pgfsysdriver@h{pgfsys-vtex.def} % ps +\def\pgfsysdriver@i{pgfsys-xetex.def} % pdf + +\newif\ifgppdfout\gppdfoutfalse +\newif\ifgppsout\gppsoutfalse + +\ifx\pgfsysdriver\pgfsysdriver@a + \gppsouttrue +\else\ifx\pgfsysdriver\pgfsysdriver@b + \gppdfouttrue +\else\ifx\pgfsysdriver\pgfsysdriver@c + \gppdfouttrue +\else\ifx\pgfsysdriver\pgfsysdriver@d + \gppsouttrue +\else\ifx\pgfsysdriver\pgfsysdriver@e + \gppdfouttrue +\else\ifx\pgfsysdriver\pgfsysdriver@f + % tex4ht +\else\ifx\pgfsysdriver\pgfsysdriver@g + \gppsouttrue +\else\ifx\pgfsysdriver\pgfsysdriver@h + \gppsouttrue +\else\ifx\pgfsysdriver\pgfsysdriver@i + \gppdfouttrue +\fi\fi\fi\fi\fi\fi\fi\fi\fi + +% uncomment the following lines to make font values "appendable" +% and if you are really sure about that ;-) +% \pgfkeyslet{/tikz/font/.@cmd}{\undefined} +% \tikzset{font/.initial={}} +% \def\tikz@textfont{\pgfkeysvalueof{/tikz/font}} + % % image related stuff % @@ -565,15 +679,17 @@ f:write([[ \pgfsysprotocol@literalbuffered{false \gp@temp\space colorimage}% \pgfsysprotocol@literal{#6 >}% } +\def\gp@rawimage@html#1#2#3#4#5#6{% +% FIXME: print a warning message here +} - -\ifpdf +\ifgppdfout \def\gp@rawimage{\gp@rawimage@pdf} \else - \ifxetex - \def\gp@rawimage{\gp@rawimage@pdf} - \else + \ifgppsout \def\gp@rawimage{\gp@rawimage@ps} + \else + \def\gp@rawimage{\gp@rawimage@html} \fi \fi @@ -599,6 +715,7 @@ f:write([[ % \def\gnuplottexextension@lua{\string tex} +\def\gnuplottexextension@tikz{\string tex} % % gnuplot variables getter and setter @@ -616,41 +733,36 @@ f:write([[ % some wrapper code % -% short for the lengthy xcolor rgb definition -\def\gprgb#1#2#3{rgb,1000:red,#1;green,#2;blue,#3} - % short for a filled path \def\gpfill#1{\path[fill,#1]} -% short for changing the linewidth +% short for changing the line width \def\gpsetlinewidth#1{\pgfsetlinewidth{#1\gpbaselw}} -\def\gpsetlinetype#1{\tikzstyle{gp path}=[#1,#1 add]} +% short for changing the line type +\def\gpsetlinetype#1{\tikzset{gp path/.style={#1,#1 add}}} -% short for changing the pointsize -\def\gpsetpointsize#1{\tikzstyle{gp point}=[mark size=#1\gpbasems]} +% short for changing the point size +\def\gpsetpointsize#1{\tikzset{gp point/.style={mark size=#1\gpbasems}}} % wrapper for color settings -\def\gpcolor#1{\pgfsetcolor{#1}} +\def\gpcolor#1{\tikzset{global #1}} +\tikzset{rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\tikzset{color=.}}} +\tikzset{global rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\pgfsetcolor{.}}} +\tikzset{global color/.code={\pgfsetcolor{#1}}} % prevent plot mark distortions due to changes in the PGF transformation matrix % use `\gpscalepointstrue' and `\gpscalepointsfalse' for enabling and disabling % point scaling % \newif\ifgpscalepoints -\tikzoption{gp shift only}[]{% - \ifgpscalepoints% - \else% - % this is actually the same definition as used by "shift only" (seen - % in pgf-1.18 and later) - \tikz@addtransform{\pgftransformresetnontranslations}% - \fi% -} +\tikzset{gp shift only/.style={% + \ifgpscalepoints\else shift only\fi% +}} \def\gppoint#1#2{% \path[solid] plot[only marks,gp point,#1,mark options={gp shift only}] coordinates {#2};% } -\def\gpfontsize#1#2{\fontsize{#1}{#2}\selectfont} % % char size calculation, that might be used with gnuplottex @@ -690,7 +802,6 @@ f:write([[ % % define a rectangular node in tikz e.g. for the plot area -% FIXME: this is done globally to work with gnuplottex.sty % % #1 node name % #2 coordinate of "south west" @@ -718,15 +829,15 @@ f:write([[ % % style for every plot % -\tikzstyle{gnuplot}=[% +\tikzset{gnuplot/.style={% >=stealth',% cap=round,% join=round,% -] +}} -\tikzstyle{gp node left}=[anchor=mid west,yshift=-.12ex] -\tikzstyle{gp node center}=[anchor=mid,yshift=-.12ex] -\tikzstyle{gp node right}=[anchor=mid east,yshift=-.12ex] +\tikzset{gp node left/.style={anchor=mid west,yshift=-.12ex}} +\tikzset{gp node center/.style={anchor=mid,yshift=-.12ex}} +\tikzset{gp node right/.style={anchor=mid east,yshift=-.12ex}} % basic plot mark size (points) \newdimen\gpbasems @@ -742,7 +853,7 @@ f:write([[ % this should reverse the normal text node presets, for the % later referencing as described below -\tikzstyle{gp refnode}=[coordinate,yshift=.12ex] +\tikzset{gp refnode/.style={coordinate,yshift=.12ex}} % to add an empty label with the referenceable name "my node" % to the plot, just add the following line to your gnuplot @@ -751,6 +862,9 @@ f:write([[ % set label "" at 1,1 font ",gp refnode,name=my node" % +% enlargement of the bounding box in standalone mode (only used by LaTeX/ConTeXt) +\def\gpbboxborder{2mm} + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% The following TikZ-styles are derived from the 'pgf.styles.*' tables @@ -762,35 +876,35 @@ f:write([[ ]]) f:write("% arrow styles settings\n") for i = 1, #pgf.styles.arrows do - f:write("\\tikzstyle{"..pgf.styles.arrows[i][1].."} = ["..pgf.styles.arrows[i][2].."]\n") + f:write("\\tikzset{"..pgf.styles.arrows[i][1].."/.style={"..pgf.styles.arrows[i][2].."}}\n") end f:write("\n% plotmark settings\n") for i = 1, #pgf.styles.plotmarks do - f:write("\\tikzstyle{"..pgf.styles.plotmarks[i][1].."} = ["..pgf.styles.plotmarks[i][2].."]\n") + f:write("\\tikzset{"..pgf.styles.plotmarks[i][1].."/.style={"..pgf.styles.plotmarks[i][2].."}}\n") end f:write("\n% pattern settings\n") for i = 1, #pgf.styles.patterns do - f:write("\\tikzstyle{"..pgf.styles.patterns[i][1].."} = ["..pgf.styles.patterns[i][2].."]\n") + f:write("\\tikzset{"..pgf.styles.patterns[i][1].."/.style={"..pgf.styles.patterns[i][2].."}}\n") end f:write("\n% if the 'tikzplot' option is used the corresponding lines will be smoothed by default\n") for i = 1, #pgf.styles.plotstyles_axes do - f:write("\\tikzstyle{"..pgf.styles.plotstyles_axes[i][1].."} = ["..pgf.styles.plotstyles_axes[i][2].."]%\n") + f:write("\\tikzset{"..pgf.styles.plotstyles_axes[i][1].."/.style="..pgf.styles.plotstyles_axes[i][2].."}\n") end for i = 1, #pgf.styles.plotstyles do - f:write("\\tikzstyle{"..pgf.styles.plotstyles[i][1].."} = ["..pgf.styles.plotstyles[i][2].."]%\n") + f:write("\\tikzset{"..pgf.styles.plotstyles[i][1].."/.style="..pgf.styles.plotstyles[i][2].."}\n") end -- line styles for borders etc ... f:write("\n% linestyle settings\n") for i = 1, #pgf.styles.linetypes_axes do - f:write("\\tikzstyle{"..pgf.styles.linetypes_axes[i][1].."} = ["..pgf.styles.linetypes_axes[i][2].."]\n") + f:write("\\tikzset{"..pgf.styles.linetypes_axes[i][1].."/.style="..pgf.styles.linetypes_axes[i][2].."}\n") end f:write("\n% linestyle \"addon\" settings for overwriting a default linestyle within the\n") - f:write("% TeX document via eg. \\tikzstyle{gp lt plot 1 add}=[fill=black,draw=none] etc.\n") + f:write("% TeX document via eg. \\tikzset{gp lt plot 1 add}=[fill=black,draw=none] etc.\n") for i = 1, #pgf.styles.linetypes_axes do - f:write("\\tikzstyle{"..pgf.styles.linetypes_axes[i][1].." add} = []\n") + f:write("\\tikzset{"..pgf.styles.linetypes_axes[i][1].." add/.style={}}\n") end for i = 1, #pgf.styles.linetypes do - f:write("\\tikzstyle{"..pgf.styles.linetypes[i][1].." add} = []\n") + f:write("\\tikzset{"..pgf.styles.linetypes[i][1].." add/.style={}}\n") end f:write("\n% linestyle color settings\n") for i = 1, #pgf.styles.lt_colors_axes do @@ -800,7 +914,7 @@ f:write([[ f:write("\n% command for switching to dashed lines\n") f:write("\\def\\gpdashedlines{%\n") for i = 1, #pgf.styles.linetypes do - f:write(" \\tikzstyle{"..pgf.styles.linetypes[i][1].."} = ["..pgf.styles.linetypes[i][2].."]%\n") + f:write(" \\tikzset{"..pgf.styles.linetypes[i][1].."/.style={"..pgf.styles.linetypes[i][2].."}}\n") end f:write("}\n") f:write("\n% command for switching to colored lines\n") @@ -812,7 +926,7 @@ f:write([[ f:write("\n% command for switching to solid lines\n") f:write("\\def\\gpsolidlines{%\n") for i = 1, #pgf.styles.linetypes do - f:write(" \\tikzstyle{"..pgf.styles.linetypes[i][1].."} = [solid]%\n") + f:write(" \\tikzset{"..pgf.styles.linetypes[i][1].."/.style=solid}%\n") end f:write("}\n") f:write("\n% command for switching to monochrome (black) lines\n") @@ -840,26 +954,28 @@ end pgf.print_help = function(fwrite) fwrite([[ - {help} - {monochrome} - {solid} - {originreset} - {gparrows} - {gppoints} - {nopicenvironment} + {latex | tex | context} + {color | monochrome} + {dashed | solid} + {nooriginreset | originreset} + {nogparrows | gparrows} + {nogppoints | gppoints} + {picenvironment | nopicenvironment} {size {unit},{unit}} {scale ,} {plotsize {unit},{unit}} {charsize {unit},{unit}} {font ""} - {createstyle} - {fulldoc|standalone} - {{preamble|header} ""} + {nofulldoc | nostandalone | fulldoc | standalone} + {{preamble | header} ""} {tikzplot ,...} - {tikzarrows} - {cmykimages} - {nobitmap} + {notikzarrows | tikzarrows} + {rgbimages | cmykimages} + {bitmap | nobitmap} + {noclip | clip} {providevars ,...} + {createstyle} + {help} For all options that expect lengths as their arguments they will default to 'cm' if no unit is specified. For all lengths @@ -903,8 +1019,15 @@ pgf.print_help = function(fwrite) size of the used font. Look at the generated style file for an example of how to use it from within your TeX document. - 'createstyle' derives the LaTeX style file from the script and - writes it to the file ']]..pgf.LATEX_STYLE_FILE..'.sty'..[['. + The options 'tex', 'latex' and 'context' choose the TeX output format. + LaTeX is the default. To load the style file put the according line + at the beginning of your document: + \input ]]..pgf.STYLE_FILE_BASENAME..[[.tex % (for plain TeX) + \usepackage{]]..pgf.STYLE_FILE_BASENAME..[[} % (for LaTeX) + \usemodule[]]..pgf.STYLE_FILE_BASENAME..[[] % (for ConTeXt) + + 'createstyle' derives the TeX/LaTeX/ConTeXt styles from the script + and writes them to the appropriate files. 'fulldoc' or 'standalone' produces a full LaTeX document for direct compilation. @@ -940,9 +1063,9 @@ pgf.print_help = function(fwrite) script. Use gnuplot's 'show variables all' command to see the list of valid variables. - The string may contain any valid LaTeX font commands like - e.g. '\small'. It is passed directly as a node parameter in form of - "font=". This can be 'misused' to add further code to a node, + The string may contain any valid TeX/LaTeX/ConTeXt font commands + like e.g. '\small'. It is passed directly as a node parameter in form of + "font={}". This can be 'misused' to add further code to a node, e.g. '\small,yshift=1ex' or ',yshift=1ex' are also valid while the latter does not change the current font settings. One exception is the second argument of the list. If it is a number of the form @@ -951,6 +1074,11 @@ pgf.print_help = function(fwrite) omitted the value is interpreted as 'pt'. As an example the string '\sffamily,12,fill=red' sets the font to LaTeX's sans serif font at a size of 12pt and red background color. + The same applies to ConTeXt, e.g. '\switchtobodyfont[iwona],10' changes the + font to Iwona at a size of 10pt. + Plain TeX users have to change the font size explicitly within the first + argument. The second should be set to the same value to get proper scaling + of text boxes. Strings have to be put in single or double quotes. Double quoted strings may contain special characters like newlines '\n' etc. @@ -1041,7 +1169,63 @@ gfx.opt = { -- style. tikzarrows = false, -- if true, cmyk image model will be used for bitmap images - cmykimage = false + cmykimage = false, + -- output TeX flavor, default is LaTeX + tex_format = 'latex' +} + +-- Formats for the various TeX flavors +gfx.format = {} + +gfx.format.tex = { + docheader = "\\input "..pgf.STYLE_FILE_BASENAME..".tex\n", + begindocument = "", + enddocument = "\\bye\n", + beforetikzpicture= "", -- standalone only + aftertikzpicture = "", -- standalone only + begintikzpicture = "\\tikzpicture", + endtikzpicture = "\\endtikzpicture", + beginscope = "\\scope", + endscope = "\\endscope", + beforeendtikzpicture = "", -- standalone only + fontsize = "" +} + +gfx.format.latex = { + docheader = "\\documentclass["..pgf.DEFAULT_FONT_SIZE.."pt]{article}\n" + .."\\usepackage[T1]{fontenc}\n" + .."\\usepackage{textcomp}\n\n" + .."\\usepackage[utf8x]{inputenc}\n\n" + .."\\usepackage{"..pgf.STYLE_FILE_BASENAME.."}\n" + .."\\pagestyle{empty}\n" + .."\\usepackage[active,tightpage]{preview}\n" + .."\\PreviewEnvironment{tikzpicture}\n" + .."\\setlength\\PreviewBorder{\\gpbboxborder}\n", + begindocument = "\\begin{document}\n", + enddocument = "\\end{document}\n", + beforetikzpicture= "", -- standalone only + aftertikzpicture = "", -- standalone only + begintikzpicture = "\\begin{tikzpicture}", + endtikzpicture = "\\end{tikzpicture}", + beginscope = "\\begin{scope}", + endscope = "\\end{scope}", + beforeendtikzpicture = "", -- standalone only + fontsize = "\\fontsize{%spt}{%spt}\\selectfont" +} + +gfx.format.context = { + docheader = "\\usemodule["..pgf.STYLE_FILE_BASENAME.."]\n", + begindocument = "\\starttext\n", + enddocument = "\\stoptext\n", + beforetikzpicture= "\\startTEXpage\n", -- standalone only + aftertikzpicture = "\\stopTEXpage\n", -- standalone only + begintikzpicture = "\\starttikzpicture", + endtikzpicture = "\\stoptikzpicture", + beginscope = "\\startscope", + endscope = "\\stopscope", + beforeendtikzpicture = "\\path[use as bounding box] ([shift={(-\\gpbboxborder,-\\gpbboxborder)}]current bounding box.south west)" + .." rectangle ([shift={(\\gpbboxborder,\\gpbboxborder)}]current bounding box.north east);\n", -- standalone only + fontsize = "\\switchtobodyfont[%spt]" } -- within tikzpicture environment or not @@ -1111,12 +1295,20 @@ gfx.parse_font_string = function (str) size, _ = gfx.parse_number_unit(toks[1],'pt','pt') if (size) then table.remove(toks,1) - rets = rets .. string.format('\\gpfontsize{%.2fpt}{%.2fpt}',size,size*1.2) + rets = rets .. string.format(gfx.format[gfx.opt.tex_format].fontsize,size,size*1.2) + end + -- add grouping braces for the font settings + if #rets > 0 then + rets = "{" .. rets .. "}" end -- add remaining parts for k,v in ipairs(toks) do rets = rets .. ',' .. v end + else + if #rets > 0 then + rets = "{" .. rets .. "}" + end end return rets, size end @@ -1260,19 +1452,18 @@ gfx.format_color = function(ctype, val) if ctype == 'LT' then if val[1] < 0 then if val[1] < -2 then -- LT_NODRAW, LT_BACKGROUND, LT_UNDEFINED - c = 'gpbgfillcolor' + c = 'color=gpbgfillcolor' else - c = pgf.styles.lt_colors_axes[math.abs(val[1])][1] + c = 'color='..pgf.styles.lt_colors_axes[math.abs(val[1])][1] end else - c = pgf.styles.lt_colors[(val[1] % #pgf.styles.lt_colors)+1][1] + c = 'color='..pgf.styles.lt_colors[(val[1] % #pgf.styles.lt_colors)+1][1] end -- c = pgf.styles.lt_colors[((val[1]+3) % #pgf.styles.lt_colors) + 1][1] elseif ctype == 'RGB' then - c = string.format("\\gprgb{%i}{%i}{%i}", - 1000*val[1]+0.5, 1000*val[2]+0.5, 1000*val[3]+0.5) + c = string.format("rgb color={%.3g,%.3g,%.3g}", val[1], val[2], val[3]) elseif ctype == 'GRAY' then - c = string.format("black!%i", 100*val[1]+0.5) + c = string.format("color=black!%i", 100*val[1]+0.5) end return c end @@ -1305,12 +1496,12 @@ else term.h_tic = pgf.DEFAULT_RESOLUTION * pgf.DEFAULT_TIC_SIZE term.v_tic = pgf.DEFAULT_RESOLUTION * pgf.DEFAULT_TIC_SIZE -- default size for CM@10pt - term.h_char = 184 * math.floor((pgf.DEFAULT_FONT_SIZE/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) - term.v_char = 308 * math.floor((pgf.DEFAULT_FONT_SIZE/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) + term.h_char = math.floor(pgf.DEFAULT_FONT_H_CHAR * (pgf.DEFAULT_FONT_SIZE/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) + term.v_char = math.floor(pgf.DEFAULT_FONT_V_CHAR * (pgf.DEFAULT_FONT_SIZE/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) term.description = "Lua PGF/TikZ terminal for LaTeX2e" - term.flags = term.TERM_BINARY + term.TERM_CAN_CLIP - + term.TERM_IS_POSTSCRIPT + term.TERM_CAN_MULTIPLOT - term.flags = term.flags + term.TERM_CAN_DASH + term_default_flags = term.TERM_BINARY + term.TERM_IS_POSTSCRIPT + term.TERM_CAN_MULTIPLOT + + term.TERM_CAN_DASH + term.TERM_ALPHA_CHANNEL + term.TERM_LINEWIDTH + term.TERM_IS_LATEX + term.flags = term_default_flags + term.TERM_CAN_CLIP end @@ -1326,6 +1517,10 @@ term.options = function(opt_str, initial, t_count) local o_next = "" local o_type = nil local s_start, s_end = 1, 1 + local term_opt = "" + local term_opt_font, term_opt_size, term_opt_scale, term_opt_preamble = "", "", "", "" + + -- gfx.opt.latex_preamble = "" -- trim spaces opt_str = opt_str:gsub("^%s*(.-)%s*$", "%1") @@ -1436,6 +1631,7 @@ term.options = function(opt_str, initial, t_count) end local print_help = false + local do_clip = false while true do get_next_token() @@ -1444,6 +1640,9 @@ term.options = function(opt_str, initial, t_count) print_help = true elseif almost_equals(o_next, "mono$chrome") then -- no colored lines + -- Setting `term.TERM_MONOCHROME' would internally disable colors for all drawings. + -- We do it the `soft' way by redefining all colors via a TeX command. + -- Maybe an additional terminal option is useful here... gfx.opt.lines_colored = false elseif almost_equals(o_next, "c$olor") or almost_equals(o_next, "c$olour") then -- colored lines @@ -1457,15 +1656,27 @@ term.options = function(opt_str, initial, t_count) elseif almost_equals(o_next, "gparr$ows") then -- use gnuplot arrows instead of TikZ gfx.opt.gp_arrows = true + elseif almost_equals(o_next, "nogparr$ows") then + -- use gnuplot arrows instead of TikZ + gfx.opt.gp_arrows = false elseif almost_equals(o_next, "gppoint$s") then -- use gnuplot points instead of TikZ gfx.opt.gp_points = true + elseif almost_equals(o_next, "nogppoint$s") then + -- use gnuplot points instead of TikZ + gfx.opt.gp_points = false elseif almost_equals(o_next, "nopic$environment") then -- omit the 'tikzpicture' environment gfx.opt.nopicenv = true + elseif almost_equals(o_next, "pic$environment") then + -- omit the 'tikzpicture' environment + gfx.opt.nopicenv = false elseif almost_equals(o_next, "origin$reset") then -- moves the origin of the TikZ picture to the lower left corner of the plot gfx.opt.set_origin = true + elseif almost_equals(o_next, "noorigin$reset") then + -- moves the origin of the TikZ picture to the lower left corner of the plot + gfx.opt.set_origin = false elseif almost_equals(o_next, "plot$size") then get_next_token() gfx.opt.plotsize_x, gfx.opt.plotsize_y = get_two_sizes(o_next) @@ -1473,8 +1684,9 @@ term.options = function(opt_str, initial, t_count) gp.int_error(t_count, string.format("error: two comma seperated lengths expected, got `%s'.", o_next)) end gfx.opt.set_plotsize = true + term_opt_size = string.format("plotsize %s,%s ", gfx.opt.plotsize_x, gfx.opt.plotsize_y) -- we set the canvas size to the plotsize to keep the aspect ratio as good as possible - -- and rescale later once we know the actual plotsize... + -- and rescale later once we know the actual plot size... term.xmax = gfx.opt.plotsize_x*pgf.DEFAULT_RESOLUTION term.ymax = gfx.opt.plotsize_y*pgf.DEFAULT_RESOLUTION elseif almost_equals(o_next, "si$ze") then @@ -1483,6 +1695,8 @@ term.options = function(opt_str, initial, t_count) if not plotsize_x then gp.int_error(t_count, string.format("error: two comma seperated lengths expected, got `%s'.", o_next)) end + gfx.opt.set_plotsize = false + term_opt_size = string.format("size %s,%s ", plotsize_x, plotsize_y) term.xmax = plotsize_x*pgf.DEFAULT_RESOLUTION term.ymax = plotsize_y*pgf.DEFAULT_RESOLUTION elseif almost_equals(o_next, "char$size") then @@ -1499,6 +1713,7 @@ term.options = function(opt_str, initial, t_count) if not xscale then gp.int_error(t_count, string.format("error: two comma seperated numbers expected, got `%s'.", o_next)) end + term_opt_scale = string.format("scale %s,%s ",xscale, yscale) term.xmax = term.xmax * xscale term.ymax = term.ymax * yscale elseif almost_equals(o_next, "tikzpl$ot") then @@ -1521,20 +1736,31 @@ term.options = function(opt_str, initial, t_count) elseif almost_equals(o_next, "tikzar$rows") then -- map the arrow angles to TikZ arrow styles gfx.opt.tikzarrows = true + elseif almost_equals(o_next, "notikzar$rows") then + -- don't map the arrow angles to TikZ arrow styles + gfx.opt.tikzarrows = false elseif almost_equals(o_next, "nobit$map") then -- render images as filled rectangles instead of the nativ -- PS or PDF image format gfx.opt.direct_image = false + elseif almost_equals(o_next, "bit$map") then + -- render images as nativ PS or PDF image + gfx.opt.direct_image = true elseif almost_equals(o_next, "cmyk$image") then -- use cmyk color model for images gfx.opt.cmykimage = true + elseif almost_equals(o_next, "rgb$image") then + -- use cmyk color model for images + gfx.opt.cmykimage = false elseif almost_equals(o_next, "full$doc") or almost_equals(o_next, "stand$alone") then -- produce full tex document gfx.opt.full_doc = true + elseif almost_equals(o_next, "nofull$doc") or almost_equals(o_next, "nostand$alone") then + -- produce full tex document + gfx.opt.full_doc = true elseif almost_equals(o_next, "create$style") then -- creates the coresponding LaTeX style from the script - local f = io.open(pgf.LATEX_STYLE_FILE..".sty" , "w+") - pgf.create_style(f) + pgf.create_style() elseif almost_equals(o_next, "fo$nt") then local fsize get_next_token() @@ -1543,17 +1769,37 @@ term.options = function(opt_str, initial, t_count) else gp.int_error(t_count, string.format("error: string expected, got `%s'.", o_next)) end + term_opt_font = string.format("font %q ", o_next) if fsize then - term.h_char = math.floor(term.h_char * (fsize/pgf.DEFAULT_FONT_SIZE) + .5) - term.v_char = math.floor(term.v_char * (fsize/pgf.DEFAULT_FONT_SIZE) + .5) + term.h_char = math.floor(pgf.DEFAULT_FONT_H_CHAR * (fsize/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) + term.v_char = math.floor(pgf.DEFAULT_FONT_V_CHAR * (fsize/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) end elseif almost_equals(o_next, "pre$amble") or almost_equals(o_next, "header") then get_next_token() if o_type == 'string' then + term_opt_preamble = term_opt_preamble .. string.format("preamble %q ", o_next) gfx.opt.latex_preamble = gfx.opt.latex_preamble .. o_next .. "\n" else gp.int_error(t_count, string.format("error: string expected, got `%s'.", o_next)) end + elseif almost_equals(o_next, "nopre$amble") or almost_equals(o_next, "noheader") then + gfx.opt.latex_preamble = '' + elseif almost_equals(o_next, "con$text") then + gfx.opt.tex_format = "context" + local fsize = 12 -- ConTeXt has a default of 12pt + term.h_char = math.floor(pgf.DEFAULT_FONT_H_CHAR * (fsize/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) + term.v_char = math.floor(pgf.DEFAULT_FONT_V_CHAR * (fsize/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) + elseif almost_equals(o_next, "tex") then + gfx.opt.tex_format = "tex" + elseif almost_equals(o_next, "latex") then + gfx.opt.tex_format = "latex" + elseif almost_equals(o_next, "clip") then + -- FIXME: needs more testing, maybe better use TikZ for clipping? + do_clip = true + term.flags = term_default_flags + elseif almost_equals(o_next, "noclip") then + do_clip = false + term.flags = term_default_flags + term.TERM_CAN_CLIP else gp.int_warn(t_count, string.format("unknown option `%s'.", o_next)) end @@ -1564,18 +1810,34 @@ term.options = function(opt_str, initial, t_count) end local tf = function(b,y,n) + local addopt = '' if b then - return(y) + addopt = y else - return(n) + addopt = n + end + if (string.len(addopt) > 0) then + term_opt = term_opt .. addopt .. ' ' end end - local opt_str = string.format("%s %s", - tf(gfx.opt.lines_colored, 'color', 'monochrome'), - tf(gfx.opt.lines_dashed, 'dashed', 'solid')) - - gp.term_options(opt_str) + tf(true, gfx.opt.tex_format, nil) + tf(true, term_opt_font, nil) + tf(true, term_opt_size, nil) + tf((#gfx.opt.latex_preamble>0), term_opt_preamble, 'nopreamble') + tf(gfx.opt.lines_colored, 'color', 'monochrome') + tf(gfx.opt.full_doc, 'standalone', 'nostandalone') + tf(gfx.opt.lines_dashed, 'dashed', 'solid') + tf(gfx.opt.gp_arrows, 'gparrows', 'nogparrows') + tf(gfx.opt.tikzarrows, 'tikzarrows', 'notikzarrows') + tf(gfx.opt.gp_points, 'gppoints', 'nogppoints') + tf(gfx.opt.nopicenv, 'nopicenvironment', 'picenvironment') + tf(gfx.opt.set_origin, 'originreset', 'nooriginreset') + tf(gfx.opt.direct_image, 'bitmap', 'nobitmap') + tf(gfx.opt.cmykimage, 'cmykimage', 'rgbimage') + tf(do_clip, 'clip', 'noclip') + + gp.term_options(term_opt) return 1 end @@ -1636,6 +1898,11 @@ term.linetype = function(ltype) gfx.check_in_path() gfx.set_color('LT', {ltype}) + + if (ltype < -2) then -- LT_NODRAW, LT_BACKGROUND, LT_UNDEFINED + ltype = -2 + end + gfx.linetype_idx = ltype return 1 @@ -1907,8 +2174,7 @@ end if arg then -- called from the command line! if #arg > 0 and arg[1] == 'style' then -- write style file - local f = io.open(pgf.LATEX_STYLE_FILE..".sty" , "w+") - pgf.create_style(f) + pgf.create_style() elseif arg[1] == 'termhelp' then io.write([["2 tikz", "?set terminal lua tikz", @@ -1928,9 +2194,8 @@ if arg then -- called from the command line! io.write([[ This script is intended to be called from GNUPLOT. - For generating the associated LaTeX style file - (']] .. pgf.LATEX_STYLE_FILE..".sty')" .. [[ just call this script - with the additional option 'style': + For generating the associated TeX/LaTeX/ConTeXt style files + just call this script with the additional option 'style': # lua gnuplot.lua style From 0baa45d2a259e2bf4050e07a46ab4ce797d078c7 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 11 Feb 2011 21:59:29 +0100 Subject: [PATCH 13/54] bugfix: terminal didn't reset the font on empty input --- term/context.trm | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/term/context.trm b/term/context.trm index 4f035daa4..a64814304 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1626,6 +1626,11 @@ CONTEXT_fontstring_parse(char *from_string, char *to_string, int to_size, dimens dimension->value = 0; dimension->unit = UNIT_PT; + /* on empty input just reset the font */ + if(from_string[0] == 0) { + to_string[0] = 0; + return; + } /* let's walk from comma to comma (or from start to stop) * and analyse content of each string separately */ /* as long as we don't hit the end of the string */ @@ -1688,7 +1693,7 @@ CONTEXT_fontstring_parse(char *from_string, char *to_string, int to_size, dimens } } } - + /* it might have happened that the last character we wrote was a comma instead of 0, * so we have to make sure that we finish the string */ to_string[to_stop-1] = 0; @@ -1748,7 +1753,7 @@ CONTEXT_set_font(const char *font) /* saves font name & family to CONTEXT_font */ CONTEXT_fontstring_parse((char *)font, CONTEXT_font, sizeof(CONTEXT_font), &CONTEXT_fontsize_explicit); strncpy(CONTEXT_font_explicit, CONTEXT_font, sizeof(CONTEXT_font_explicit)); - + /* valid fontsize has been provided */ if (CONTEXT_fontsize_explicit.value > 0) { /* XXX: if valid */ From 54bed4450563dc202940f8462f7468058e5553fc Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 11 Feb 2011 23:14:49 +0100 Subject: [PATCH 14/54] accept 'fontscale' in addition to 'textscale' and add a few binary flags --- term/context.trm | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/term/context.trm b/term/context.trm index a64814304..c889e2f92 100644 --- a/term/context.trm +++ b/term/context.trm @@ -515,7 +515,8 @@ static struct gen_table CONTEXT_opts[] = { { "dl", CONTEXT_OPT_SCALE_DASHLENGTH }, { "linew$idth", CONTEXT_OPT_SCALE_LINEWIDTH }, { "lw", CONTEXT_OPT_SCALE_LINEWIDTH }, - { "textscale", CONTEXT_OPT_SCALE_TEXT }, + { "fontscale", CONTEXT_OPT_SCALE_TEXT }, + { "textscale", CONTEXT_OPT_SCALE_TEXT }, /* backward compatibility */ { "textext", CONTEXT_OPT_TEXT_TEXTEXT }, { "sometxt", CONTEXT_OPT_TEXT_SOMETXT }, { "pointswithmp", CONTEXT_OPT_POINTS_WITH_METAPOST}, @@ -1004,7 +1005,7 @@ CONTEXT_options() } snprintf(tmp_term_options, sizeof(tmp_term_options), - " %s dashlength %g linewidth %g textscale %g \\\n ", + " %s dashlength %g linewidth %g fontscale %g \\\n ", CONTEXT_params.dashed ? "dashed" : "solid", CONTEXT_params.scale_dashlength, CONTEXT_params.scale_linewidth, @@ -2194,7 +2195,9 @@ TERM_TABLE_START(context_driver) CONTEXT_linetype, CONTEXT_put_text, CONTEXT_text_angle, CONTEXT_justify_text, CONTEXT_point, CONTEXT_arrow, CONTEXT_set_font, CONTEXT_pointsize, - TERM_BINARY /* flags */, 0 /* suspend */, 0 /* resume*/, /* XXX: why did I put binary here? */ + /* also add TERM_CAN_CLIP once you understand what it does; Why binary? Because of UTF-8? */ + TERM_BINARY | TERM_CAN_DASH | TERM_ALPHA_CHANNEL | TERM_LINEWIDTH | TERM_FONTSCALE | TERM_IS_LATEX /* flags */, + 0 /* suspend */, 0 /* resume*/, CONTEXT_fillbox, CONTEXT_linewidth #ifdef USE_MOUSE , 0, 0, 0, 0, 0 /* no mouse support */ @@ -2246,13 +2249,13 @@ START_HELP(context) " {dashed | solid}", " {dashlength | dl
}", " {linewidth | lw }", -" {textscale }", +" {fontscale }", " {pointswithmetapost | pointswithmp | pointswithtex}", " {defaultfont | font {} |", " font \"{,}\" {fontsize}}", "", " In non-standalone (`input`) graphic only parameters `size` to select graphic", -" size, `textscale` to scale all the labels for a factor ", +" size, `fontscale` to scale all the labels for a factor ", " and font size, make sense, the rest is silently", " ignored and should be configured in the .tex file which inputs the graphic." " It's highly recommended to set the proper fontsize if document font differs from", @@ -2293,7 +2296,7 @@ START_HELP(context) " `dashlength` or `dl` scales the length of the dashed-line segments by
.", " `linewidth` or `lw` scales all linewidths by ." " (lw 1 stands for 0.4bp, which is the default line width when drawing with Metapost.)", -" `textscale` scales text labels for factor relative to default document font.", +" `fontscale` scales text labels for factor relative to default document font.", "", " `pointswithmetapost` uses predefined point shapes, drawn in Metapost.", " `pointswithtex` uses easily configurable set of symbols, defined with ConTeXt", @@ -2307,7 +2310,7 @@ START_HELP(context) " set term context font \"myfont,ss\" 10pt", " will result in", " \\setupbodyfont[myfont,ss,10pt]", -" If you additionaly set `textscale` to 0.8 for example,", +" If you additionaly set `fontscale` to 0.8 for example,", " then the resulting font will be 8pt big and", " set label ... font \"myfont,12pt\"", " will come out as 9.6pt.\n", From 35f0359fe2861df2c498cc61107c07707b2e5e6e Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 12 Feb 2011 10:41:42 +0100 Subject: [PATCH 15/54] bugfix: FS_DEFAULT should paint with current color, not with density 0 --- term/context.trm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/term/context.trm b/term/context.trm index c889e2f92..1cce45905 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1869,6 +1869,9 @@ CONTEXT_fill(int style) /* do some strange trickery */ switch(style & 0xf) { + case FS_DEFAULT: + break; + case FS_TRANSPARENT_SOLID: /* just a flag to tell the terminal that density() should be used to interpret transparency */ fprintf(gpoutfile, ",transparent"); From 2100ab657f5d3a5dcf2c320112466f88f03fc197 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 12 Feb 2011 19:38:15 +0100 Subject: [PATCH 16/54] add (no)timestamp --- term/context.trm | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/term/context.trm b/term/context.trm index 1cce45905..2df521d36 100644 --- a/term/context.trm +++ b/term/context.trm @@ -440,6 +440,7 @@ typedef struct CONTEXT_params_t { dimension_t xsize; /* 5in */ dimension_t ysize; /* 3in */ TBOOLEAN standalone; /* false */ + TBOOLEAN timestamp; /* true */ char *header; /* "" */ TBOOLEAN color; /* true */ TBOOLEAN dashed; /* true */ @@ -459,6 +460,7 @@ typedef struct CONTEXT_params_t { {CONTEXT_XSIZE_VALUE, CONTEXT_XSIZE_UNIT},\ {CONTEXT_YSIZE_VALUE, CONTEXT_YSIZE_UNIT},\ FALSE,\ + TRUE,\ NULL,\ TRUE,\ TRUE,\ @@ -480,6 +482,7 @@ enum CONTEXT_id { CONTEXT_OPT_SIZE, CONTEXT_OPT_SIZE_DEFAULT, CONTEXT_OPT_INPUT, CONTEXT_OPT_STANDALONE, + CONTEXT_OPT_TIMESTAMP, CONTEXT_OPT_NOTIMESTAMP, CONTEXT_OPT_HEADER, CONTEXT_OPT_NOHEADER, CONTEXT_OPT_COLOR, CONTEXT_OPT_MONOCHROME, CONTEXT_OPT_DASHED, CONTEXT_OPT_SOLID, @@ -498,6 +501,8 @@ static struct gen_table CONTEXT_opts[] = { { "defaultsize", CONTEXT_OPT_SIZE_DEFAULT }, { "inp$ut", CONTEXT_OPT_INPUT }, { "stand$alone", CONTEXT_OPT_STANDALONE }, + { "time$stamp", CONTEXT_OPT_TIMESTAMP }, + { "notime$stamp", CONTEXT_OPT_NOTIMESTAMP }, { "header", CONTEXT_OPT_HEADER }, { "noheader", CONTEXT_OPT_NOHEADER }, { "col$or", CONTEXT_OPT_COLOR }, @@ -547,6 +552,7 @@ CONTEXT_params_reset() CONTEXT_params.ysize.unit = CONTEXT_YSIZE_UNIT; CONTEXT_params.standalone = FALSE; + CONTEXT_params.timestamp = TRUE; if (CONTEXT_params.header) { free(CONTEXT_params.header); CONTEXT_params.header = NULL; @@ -681,6 +687,14 @@ CONTEXT_options() c_token++; CONTEXT_params.standalone = TRUE; break; + case CONTEXT_OPT_TIMESTAMP: + c_token++; + CONTEXT_params.timestamp = TRUE; + break; + case CONTEXT_OPT_NOTIMESTAMP: + c_token++; + CONTEXT_params.timestamp = FALSE; + break; case CONTEXT_OPT_HEADER: c_token++; @@ -963,12 +977,13 @@ CONTEXT_options() CONTEXT_adjust_dimensions(); snprintf(term_options, sizeof(term_options), - "size %g%s,%g%s %s %s", + "size %g%s,%g%s %s %s %s", CONTEXT_params.xsize.value, unit_name(CONTEXT_params.xsize.unit), CONTEXT_params.ysize.value, unit_name(CONTEXT_params.ysize.unit), CONTEXT_params.standalone ? "standalone" : "input", + CONTEXT_params.timestamp ? "timestamp" : "notimestamp", CONTEXT_params.header == NULL ? "noheader \\\n " : "\\\n header "); if (CONTEXT_params.header != NULL) { @@ -1088,7 +1103,11 @@ CONTEXT_init() timedate=asctime(localtime(&now)); timedate[strlen(timedate)-1]='\0'; - fprintf(gpoutfile, "%% Written by ConTeXt terminal for GNUPLOT on: %s", asctime(localtime(&now))); + fprintf(gpoutfile, "%% Written by ConTeXt terminal for GNUPLOT"); + if(CONTEXT_params.timestamp) { + fprintf(gpoutfile, " on: %s", asctime(localtime(&now))); + } + fprintf(gpoutfile, "\n"); fprintf(gpoutfile, "%% GNUPLOT version %s patchlevel %s\n", gnuplot_version, gnuplot_patchlevel); fprintf(gpoutfile, "%% See also http://wiki.contextgarden.net/Gnuplot\n%%\n"); @@ -1823,7 +1842,8 @@ CONTEXT_fillbox(int style, unsigned int x1, unsigned int y1, unsigned int width, CONTEXT_endpath(); /* create a new path */ - fprintf(gpoutfile, "p := unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", width/100.0, height/100.0, x1/100.0, y1/100.0); + /*fprintf(gpoutfile, "p := unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", width/100.0, height/100.0, x1/100.0, y1/100.0);*/ + fprintf(gpoutfile, "p := gp_rect ((%.2fa,%.2fb),(%.2fa,%.2fb));\n", x1/100.0, y1/100.0, width/100.0, height/100.0); /* fills the box according to the "style" * the code went out of this routine because of undocumented behaviour @@ -2245,6 +2265,7 @@ START_HELP(context) " {defaultsize | size |", " size {in|cm}, {in|cm}}", " {input | standalone}", +" {timestamp | notimestamp}", " {noheader | header \"
\"}", " {color | colour | monochrome}", " {mitered | rounded | beveled}", @@ -2281,6 +2302,9 @@ START_HELP(context) " Use `header` for any additional settings/definitions/macros", " that you might want to include in a standalone graphic. `noheader` is the default.", "", +" `notimestamp` prevents printing creation time in comments", +" (if version control is used, one may prefer not to commit new version when only date changes).", +"", " `color` to make color plots is the default, but `monochrome` doesn't do anything special yet.", " If you have any good ideas how the behaviour should differ to suit the monochrome printers better,", " your suggestions are welcome.", From b4f19a145aac28093878a37f226c3cbb2e57c594 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 12 Feb 2011 19:39:14 +0100 Subject: [PATCH 17/54] new version of lua/tikz terminal --- term/lua.trm | 124 ++++++++++++++++++++------------------ term/lua/gnuplot-tikz.lua | 46 +++++++++----- 2 files changed, 97 insertions(+), 73 deletions(-) diff --git a/term/lua.trm b/term/lua.trm index 5e5b8ad43..9670d224e 100644 --- a/term/lua.trm +++ b/term/lua.trm @@ -256,10 +256,10 @@ LUA_GP_int_warn(lua_State *L) { static int LUA_GP_term_out(lua_State *L) { char c; /* dummy input char */ - char *line; - int pagelines = 0; + char *line, *last; + int pagelines = 0; const char *msg = ""; - + int n = lua_gettop(L); /* Number of arguments */ switch (n) { case 1: @@ -270,10 +270,9 @@ LUA_GP_term_out(lua_State *L) { break; } - line = strtok((char*)msg, "\n"); - while(line != NULL) { - /* built-in dumb pager code stolen from help.c - maybe someone has a fancy old ATARI :-) */ + last = (char *)msg; + while (line = strchr(last, '\n')) { + *line = '\0'; if (pagelines >= 22) { fputs("Press return for more: ", stderr); #if defined(ATARI) || defined(MTOS) @@ -287,11 +286,14 @@ LUA_GP_term_out(lua_State *L) { #endif pagelines = 0; } - fputs(line, stderr); + fputs(last, stderr); fputs("\n", stderr); pagelines++; - line = strtok(NULL, "\n"); + last = line+1; } + if (*last) + fputs(last, stderr); + return(0); } @@ -455,7 +457,7 @@ LUA_set_term_vars() { /* set term.lua_ident */ lua_pushstring(L, LUA_RELEASE); lua_setfield(L, luaterm, "lua_ident"); - + /* some static definitions from term_api.h */ lua_pushinteger(L, TERM_CAN_MULTIPLOT); /* tested if stdout not redirected */ lua_setfield(L, luaterm, "TERM_CAN_MULTIPLOT"); @@ -695,50 +697,47 @@ LUA_options() need_init = 0; } - opt_str = gp_input_line + token[c_token].start_index; + opt_str = gp_input_line + token[c_token].start_index; if (need_init) { if (!END_OF_COMMAND) { - if (*opt_str == '"' || *opt_str == '\'') { - s = try_to_get_string(); - gp_expand_tilde(&s); - } else { - s = gp_alloc(token_len(c_token)+strlen("gnuplot-.lua")+1, "LUA_script"); - memcpy(s, "gnuplot-", 8); - memcpy(s+8, opt_str , token_len(c_token)); - memcpy(s+8+token_len(c_token), ".lua\0", 5); - c_token++; - } - if (LUA_script) { - if (strcmp(LUA_script, s)) { - free(LUA_script); - LUA_script = s; - need_init = 1; + if (*opt_str == '"' || *opt_str == '\'') { + s = try_to_get_string(); + gp_expand_tilde(&s); } else { - free(s); - need_init = 0; + s = gp_alloc(token_len(c_token)+strlen("gnuplot-.lua")+1, "LUA_script"); + memcpy(s, "gnuplot-", 8); + memcpy(s+8, opt_str , token_len(c_token)); + memcpy(s+8+token_len(c_token), ".lua\0", 5); + c_token++; } + if (LUA_script) { + if (strcmp(LUA_script, s)) { + free(LUA_script); + LUA_script = s; + need_init = 1; + } else { + free(s); + need_init = 0; + } + } else { + LUA_script = s; + } + opt_str = gp_input_line + token[c_token].start_index; } else { - LUA_script = s; + LUA_close(); + int_error(NO_CARET, "No Lua driver name or file name given!"); } - opt_str = gp_input_line + token[c_token].start_index; - /* FIXME: added by Ethan, but i think "c_token = num_tokens;" below does virtually the same here - while (!END_OF_COMMAND) - c_token++; - */ - } else { - LUA_close(); - int_error(NO_CARET, "No Lua driver name or file name given!"); - } - /* init lua when opening the terminal or on script change */ + /* init lua when opening the terminal or on script change */ if(!LUA_init_lua()) { return; } } /* since options are tokenized again in the script we always "finish" this here */ - c_token = num_tokens; + while (!END_OF_COMMAND) + c_token++; if(LUA_init_luaterm_function("options")) { /* isolate the "set term ...;" part of the command line */ @@ -762,8 +761,9 @@ TERM_PUBLIC void LUA_init() { fseek(gpoutfile, 0, SEEK_SET); - ftruncate(fileno(gpoutfile),0); - + /* ignore compiler warnings here, because `gpoutfile' is already open */ + if (ftruncate(fileno(gpoutfile), 0)); + LUA_linetype(-1); if(LUA_init_luaterm_function("init")) { LUA_call_report(lua_pcall(L, 0, 1, tb)); @@ -1031,8 +1031,7 @@ LUA_set_color (t_colorspec *colorspec) { double gray = colorspec->value; - rgb_color color; - + rgb_color color = {0.,0.,0.}; if(LUA_init_luaterm_function("set_color")) { if (colorspec->type == TC_FRAC) { @@ -1149,21 +1148,23 @@ LUA_path (int path) TERM_PUBLIC void LUA_image (unsigned m, unsigned n, coordval *image, gpiPoint *corner, t_imagecolor color_mode) { if(LUA_init_luaterm_function("image")) { - int i, is_rgb; + int i; rgb_color rgb1; - + coordval alpha; + lua_pushinteger(L, m); lua_pushinteger(L, n); - is_rgb = (color_mode == IC_RGB) ? 1 : 0; lua_newtable(L); /* pixel table */ for (i = 0; i < m*n; i++) { - if (is_rgb) { + if (color_mode == IC_PALETTE) { + /* FIXME: Is this correct? Needs a testcase. Would be nice to map it correctly to RGB. */ + rgb1maxcolors_from_gray(*image++, &rgb1); + } else { /* IC_RGB and IC_RGBA*/ rgb1.r = *image++; rgb1.g = *image++; rgb1.b = *image++; - } else { - rgb1maxcolors_from_gray(*image++, &rgb1); + if (color_mode == IC_RGBA) alpha = (*image++)/255.; } lua_newtable(L); /* pixel color */ lua_pushnumber(L, rgb1.r); @@ -1172,22 +1173,31 @@ LUA_image (unsigned m, unsigned n, coordval *image, gpiPoint *corner, t_imagecol lua_rawseti(L, -2, 2); lua_pushnumber(L, rgb1.b); lua_rawseti(L, -2, 3); + if (color_mode == IC_RGBA) { + lua_pushnumber(L, alpha); + lua_rawseti(L, -2, 4); + } lua_rawseti(L, -2, i+1); /* add "pixel" */ } - + lua_newtable(L); /* "corner" table */ for (i = 0; i < 4; i++) { lua_newtable(L); - lua_pushinteger(L, corner[i].x); + lua_pushinteger(L, (int)corner[i].x); lua_rawseti(L, -2, 1); - lua_pushinteger(L, corner[i].y); + lua_pushinteger(L, (int)corner[i].y); lua_rawseti(L, -2, 2); lua_rawseti(L, -2 , i+1); /* add "subtable" */ } - if (is_rgb) - lua_pushstring(L, "RGB"); - else - lua_pushstring(L, "GRAY"); + switch (color_mode) { + case IC_PALETTE: + case IC_RGB: + lua_pushstring(L, "RGB"); + break; + case IC_RGBA: + lua_pushstring(L, "RGBA"); + break; + } LUA_call_report(lua_pcall(L, 5, 1, tb)); lua_term_result = (int)lua_tointeger(L, -1); lua_pop(L, 1); diff --git a/term/lua/gnuplot-tikz.lua b/term/lua/gnuplot-tikz.lua index e53bf21e9..81906ffb2 100644 --- a/term/lua/gnuplot-tikz.lua +++ b/term/lua/gnuplot-tikz.lua @@ -250,8 +250,11 @@ pgf.write_graph_begin = function (font, noenv) gp.write("%% ") -- comment out end gp.write(string.format("%s[gnuplot%s]\n", gfx.format[gfx.opt.tex_format].begintikzpicture, global_opt)) - gp.write(string.format("%%%% generated with GNUPLOT %sp%s (%s; terminal rev. %s, script rev. %s)\n%%%% %s\n", - term.gp_version, term.gp_patchlevel, _VERSION, string.sub(term.lua_term_revision,7,-3), pgf.REVISION,os.date())) + gp.write(string.format("%%%% generated with GNUPLOT %sp%s (%s; terminal rev. %s, script rev. %s)\n", + term.gp_version, term.gp_patchlevel, _VERSION, string.sub(term.lua_term_revision,7,-3), pgf.REVISION)) + if not gfx.opt.notimestamp then + gp.write(string.format("%%%% %s\n", os.date())) + end if font ~= "" then gp.write(string.format("\\tikzset{every node/.append style={font=%s}}\n", font)) end @@ -521,7 +524,7 @@ pgf.create_style = function() local f_latex = io.open(name_latex, "w+") f_latex:write([[ -%% +%% %% LaTeX wrapper for gnuplot-tikz style file %% \NeedsTeXFormat{LaTeX2e} @@ -601,11 +604,18 @@ f:write([[ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% Common style file for TeX, LaTeX and ConTeXt -%% +%% %% It is associated with the 'gnuplot.lua' script, and usually generated %% automatically. So take care whenever you make any changes! %% +% check for the correct TikZ version +\def\gpchecktikzversion#1.#2\relax{% +\ifnum#1<2% + \PackageError{gnuplot-lua-tikz}{PGF/TikZ version >= 2.0 is required, but version \pgfversion\space was found}{}% +\fi} +\expandafter\gpchecktikzversion\pgfversion\relax + % FIXME: is there a more elegant way to determine the output format? \def\pgfsysdriver@a{pgfsys-dvi.def} % ps @@ -747,9 +757,9 @@ f:write([[ % wrapper for color settings \def\gpcolor#1{\tikzset{global #1}} -\tikzset{rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\tikzset{color=.}}} -\tikzset{global rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\pgfsetcolor{.}}} -\tikzset{global color/.code={\pgfsetcolor{#1}}} +\tikzset{rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\color{.}}} +\tikzset{global rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\color{.}}} +\tikzset{global color/.code={\color{#1}}} % prevent plot mark distortions due to changes in the PGF transformation matrix % use `\gpscalepointstrue' and `\gpscalepointsfalse' for enabling and disabling @@ -972,7 +982,6 @@ pgf.print_help = function(fwrite) {notikzarrows | tikzarrows} {rgbimages | cmykimages} {bitmap | nobitmap} - {noclip | clip} {providevars ,...} {createstyle} {help} @@ -1171,7 +1180,9 @@ gfx.opt = { -- if true, cmyk image model will be used for bitmap images cmykimage = false, -- output TeX flavor, default is LaTeX - tex_format = 'latex' + tex_format = 'latex', + -- for regression tests etc. we can turn off the timestamp + notimestamp = false } -- Formats for the various TeX flavors @@ -1267,7 +1278,7 @@ gfx.units = { gfx.parse_number_unit = function (str, from, to) to = to or 'cm' from = from or 'cm' - local num, unit = string.match(str, '([%d%.]+)([a-z]*)') + local num, unit = string.match(str, '^([%d%.]+)([a-z]*)$') if unit and (string.len(unit) > 0) then from = unit else @@ -1462,6 +1473,8 @@ gfx.format_color = function(ctype, val) -- c = pgf.styles.lt_colors[((val[1]+3) % #pgf.styles.lt_colors) + 1][1] elseif ctype == 'RGB' then c = string.format("rgb color={%.3g,%.3g,%.3g}", val[1], val[2], val[3]) + elseif ctype == 'RGBA' then + c = string.format("rgb color={%.3g,%.3g,%.3g},opacity=%.3g", val[1], val[2], val[3], val[4]) elseif ctype == 'GRAY' then c = string.format("color=black!%i", 100*val[1]+0.5) end @@ -1498,9 +1511,9 @@ else -- default size for CM@10pt term.h_char = math.floor(pgf.DEFAULT_FONT_H_CHAR * (pgf.DEFAULT_FONT_SIZE/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) term.v_char = math.floor(pgf.DEFAULT_FONT_V_CHAR * (pgf.DEFAULT_FONT_SIZE/10) * (pgf.DEFAULT_RESOLUTION/1000) + .5) - term.description = "Lua PGF/TikZ terminal for LaTeX2e" + term.description = "Lua PGF/TikZ terminal for TeX and friends" term_default_flags = term.TERM_BINARY + term.TERM_IS_POSTSCRIPT + term.TERM_CAN_MULTIPLOT - + term.TERM_CAN_DASH + term.TERM_ALPHA_CHANNEL + term.TERM_LINEWIDTH + term.TERM_IS_LATEX + + term.TERM_CAN_DASH + term.TERM_ALPHA_CHANNEL + term.TERM_LINEWIDTH + term.TERM_IS_LATEX term.flags = term_default_flags + term.TERM_CAN_CLIP end @@ -1520,8 +1533,6 @@ term.options = function(opt_str, initial, t_count) local term_opt = "" local term_opt_font, term_opt_size, term_opt_scale, term_opt_preamble = "", "", "", "" - -- gfx.opt.latex_preamble = "" - -- trim spaces opt_str = opt_str:gsub("^%s*(.-)%s*$", "%1") local opt_len = string.len(opt_str) @@ -1650,6 +1661,9 @@ term.options = function(opt_str, initial, t_count) elseif almost_equals(o_next, "so$lid") then -- no dashed and dotted etc. lines gfx.opt.lines_dashed = false + elseif almost_equals(o_next, "notime$stamp") then + -- omit output of the timestamp + gfx.opt.notimestamp = true elseif almost_equals(o_next, "da$shed") then -- dashed and dotted etc. lines gfx.opt.lines_dashed = true @@ -2082,7 +2096,7 @@ end -- points[row][column] -- m: #cols, n: #rows -- corners: clip box and draw box coordinates --- ctype: "RGB" or "GRAY" (unused since we allways use RGB to keep things simple) +-- ctype: "RGB" or "RGBA" or "PALETTE" term.image = function(m, n, points, corners, ctype) gfx.check_in_path() @@ -2106,7 +2120,7 @@ term.image = function(m, n, points, corners, ctype) yy = corners[1][2]-math.floor(cnt/m)*h yyy = yy-h xxx = xx+w - pgf.draw_fill({{xx, yy}, {xxx, yy}, {xxx, yyy}, {xx, yyy}}, '', gfx.format_color('RGB', points[cnt]) , 100, 100) + pgf.draw_fill({{xx, yy}, {xxx, yy}, {xxx, yyy}, {xx, yyy}}, '', gfx.format_color(ctype, points[cnt]) , 100, 100) end end pgf.write_clipbox_end() From 1b32330874672a1f355ca04d2ae174295dc9265f Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Thu, 18 Aug 2011 23:19:05 +0200 Subject: [PATCH 18/54] change executable bit --- config/djconfig.sh | 0 missing | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 config/djconfig.sh mode change 100644 => 100755 missing diff --git a/config/djconfig.sh b/config/djconfig.sh old mode 100644 new mode 100755 diff --git a/missing b/missing old mode 100644 new mode 100755 From 7fdee73fb6f5e5490b769a82770f1c9e3c5d236e Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 19 Aug 2011 00:06:31 +0200 Subject: [PATCH 19/54] fix some changes that got lost during merge --- term/lua.trm | 12 +- term/lua/ChangeLog | 322 ++++++++++++++++++++++++++++++++++++++ term/lua/INSTALL | 187 ++++++++++++++++++++++ term/lua/gnuplot-tikz.lua | 52 ------ 4 files changed, 515 insertions(+), 58 deletions(-) create mode 100644 term/lua/ChangeLog create mode 100644 term/lua/INSTALL diff --git a/term/lua.trm b/term/lua.trm index aa6772aec..b6cb33502 100644 --- a/term/lua.trm +++ b/term/lua.trm @@ -269,7 +269,7 @@ LUA_GP_term_out(lua_State *L) { char *line, *last; int pagelines = 0; const char *msg = ""; - + int n = lua_gettop(L); /* Number of arguments */ switch (n) { case 1: @@ -738,6 +738,7 @@ LUA_options() int tc_off = c_token+1; /* token counter offset */ int need_init = 1; + /* 'set term tikz' is short for 'set term lua tikz' */ c_token--; /* see how we got here */ if (!equals(c_token, "tikz")) { @@ -772,12 +773,11 @@ LUA_options() LUA_script = s; need_init = 1; } else { - LUA_script = s; + free(s); + need_init = 0; } - opt_str = gp_input_line + token[c_token].start_index; } else { - LUA_close(); - int_error(NO_CARET, "No Lua driver name or file name given!"); + LUA_script = s; } opt_str = gp_input_line + token[c_token].start_index; } else { @@ -1252,7 +1252,7 @@ LUA_image (unsigned m, unsigned n, coordval *image, gpiPoint *corner, t_imagecol } lua_rawseti(L, -2, i+1); /* add "pixel" */ } - + lua_newtable(L); /* "corner" table */ for (i = 0; i < 4; i++) { lua_newtable(L); diff --git a/term/lua/ChangeLog b/term/lua/ChangeLog new file mode 100644 index 000000000..dac7ae597 --- /dev/null +++ b/term/lua/ChangeLog @@ -0,0 +1,322 @@ +2009-03-29 peter + + * [r96] /gnuplot-tikz.lua, /lua.trm: removed duplicate line in docs + minor changes to keep pace with recent the cvs version + +2009-03-11 peter + + * [r95] /gnuplot-tikz.lua: fixed revision date parsing again to + work with CVS and SVN + * [r94] /gnuplot-tikz.lua: * make plot nodes globally available to + work with gnuplottex + * dummy node for character size calculation was changing the + bounding box + +2009-03-02 peter + + * [r93] /INSTALL, /README, /gnuplot-tikz.lua, /lua.trm: + Documentation update. + The TikZ driver help is now included as a subsection of the Lua + terminal. + The help file is generated via 'lua gnuplot-tikz.lua termhelp > + gnuplot-tikz.help'. + +2009-02-22 peter + + * [r92] /gnuplot-tikz.lua, /gnuplot.lua, /lua.trm: improved error + handling + gnuplot.lua renamed to gnuplot-tikz.lua + removed option "script" from gnuplot-tikz.lua + +2009-02-20 peter + + * [r91] /lua.trm: * Providing a script name is now mandatory, like + set term lua tikz {} + * Support for script names via the environmental variable + GNUPLOT_LUA_SCRIPT is dropped + * The environmental variable GNUPLOT_DRIVER_DIR can now be + used to change the default search dir + +2008-12-20 peter + + * [r90] /Copyright, /gnuplot.lua, /lua.trm: style and color name + changes and small fixes + plot box coordinates are now accessible via a special node + new interface function gp.term_options() + +2008-12-09 peter + + * [r89] /COPYING, /Copyright, /INSTALL, /README, /TODO, + /gnuplot.lua, /lua.trm: Changed the license from GPL to gnuplot + license + +2008-11-23 peter + + * [r88] /gnuplot.lua: fixed a typo in charsize example code + * [r87] /gnuplot.lua: added some example code for charsize + small bugfix on charsize/canvas calculation + +2008-11-22 peter + + * [r86] /gnuplot.lua: Straightened the canvas calculation to make + it more easy to change the default sizes. + Changed the default canvas size to 12.5cm x 8.75cm (suggested by + Juergen Wieferink). + In "fulldoc" mode the preview package is now used to clip the + plot (suggested by Juergen Wieferink). + Added north, south, west and east coordinates of the plot area. + +2008-02-24 peter + + * [r85] /gnuplot.lua: new option `charsize' + +2008-02-02 peter + + * [r84] /test.2.gnu, /test.tex: only minor changes in the demo + files + +2008-01-29 peter + + * [r83] /gnuplot.lua: minor "pretty printing" changes + +2008-01-27 peter + + * [r82] /gnuplot.lua: some internal renaming + * [r81] /INSTALL, /gnuplot.lua, /lua.trm: keeping pace with gnuplot + 4.3 development + +2008-01-24 peter + + * [r80] /INSTALL, /README, /gnuplot.lua: documentation update + * [r79] /gnuplot.lua, /lua.trm: added pager functionality for help + output + +2008-01-17 peter + + * [r78] /gnuplot.lua: added options for CMYK support and for + disabling bitmap rendering + * [r77] /gnuplot.lua: fixed "zero length path" bug + +2008-01-12 peter + + * [r76] /INSTALL, /lua.trm: added the environment variable + GNUPLOT_LUA_SCRIPT + +2008-01-03 peter + + * [r75] /lua.trm: fixing the latest changes + * [r74] /lua.trm: Initialize term->tscale for gnuplot 4.3 + +2007-12-22 peter + + * [r73] /INSTALL, /gnuplot.lua, /lua.trm: small adaptions to the + most recent CVS version + +2007-11-03 peter + + * [r72] /gnuplot.lua: gnuplottex support + some minor changes + +2007-10-11 peter + + * [r71] /gnuplot.lua: fixed the option parser fix and improved + error reporting + +2007-10-10 peter + + * [r70] /README, /TODO, /gnuplot.lua: gnuplot arrow styles to TikZ + mapping + cmyk support for image data + some minor bugfixes (parser, "path redundencies", color value + rounding) + +2007-09-29 peter + + * [r69] /TODO, /gnuplot.lua, /lua.trm, /test.tex: new option + 'providevars' + new color scheme and dash styles + small option parser fixes + bounding box coordinates name change + +2007-09-22 peter + + * [r68] /TODO, /test.2.gnu, /test.gnu, /test.tex: examples update + * [r67] /gnuplot.lua: the 'smooth' option is now named 'tikzplot' + +2007-09-10 peter + + * [r66] /gnuplot.lua: image code now works with tex (dvi/ps), + pdftex and xetex + +2007-09-06 peter + + * [r65] /gnuplot.lua: even more work on images + +2007-09-04 peter + + * [r64] /gnuplot.lua, /lua.trm: more work on images + * [r63] /TODO, /gnuplot.lua, /lua.trm: First support for plotting + images. + API change: Color and fill styles are now passed as strings + instead of integers to the script. + +2007-08-31 peter + + * [r62] /TODO: + +2007-08-28 peter + + * [r61] /gnuplot.lua: added \gpscalepointstrue \gpscalepointsfalse + cleanups + * [r60] /test.2.gnu, /test.tex: only small changes + * [r59] /gnuplot.lua: lengths now accept units + more individual gnuplot TikZ-styles + experimental option 'smooth' + +2007-08-26 peter + + * [r58] /lua.trm: little clean up + added gp.is_multiplot() + +2007-08-25 peter + + * [r57] /TODO, /gnuplot.lua: fixed a compatibility issue with pgf + 1.10 + * [r56] /gnuplot.lua: option parser is more robust + * [r55] /gnuplot.lua: debug code removed + * [r54] /gnuplot.lua: Fixed rectangle scaling with option + 'plotsize' + +2007-08-24 peter + + * [r53] /gnuplot.lua: New option 'size'. + Removed 'xscale' and 'yscale'. + Fixed scaling and resizing behavior. + Made styles a little more flexible. + * [r52] /INSTALL, /test.2.gnu, /test.tex: more updates + * [r51] /README, /TODO: docs update + * [r50] /lua.trm: new ouput function for console messages + +2007-08-20 peter + + * [r49] /gnuplot.lua, /lua.trm, /test.tex: new option 'createstyle' + fixed a node placement bug in the lua script and tikz style + +2007-08-13 peter + + * [r48] /gnuplot.lua, /test.tex: fixed scaling and aspect ratio of + tic marks + +2007-08-12 peter + + * [r47] /test.tex: + * [r46] /test.tex: + * [r45] /demo.gnu, /demo.tex, /test.gnu, /test.tex: new test file + * [r44] /demo.gnu, /demo.tex, /gnuplot.lua, /test.2.gnu, /test.gnu: + new options 'originreset' and 'plotsize x,y' + +2007-08-08 peter + + * [r43] /INSTALL, /README, /TODO, /gnuplot.lua: new terminal option + 'nopicenvironment' + fixed gfx.format_color() call in term.filled_polygon() + +2007-07-09 peter + + * [r42] /gnuplot.lua, /lua.trm: lots of internal api changes + new options gparrows and gppoints + debugging code removed + +2007-07-03 peter + + * [r41] /README.terminal, /TODO, /gnuplot.lua, /lua.trm: linetype + now set via \gpsetlinetype; + changed bounding box determination; + mapping for function 'layer' + +2007-07-01 peter + + * [r40] /INSTALL, /gnuplot.lua: direkte Erzeugung des Style-Files, + Hilfe auf der Kommandozeile + * [r39] /gnuplot.lua: Bugfix aus vorangegangener Revision + * [r38] /gnuplot.lua: mehr Infos im Style-File + * [r37] /README, /gnuplot.lua: neue Hilfsmakros \gpsetlinewidth, + \gpsetpointsize und \gppoint + +2007-06-28 peter + + * [r36] /INSTALL, /README, /TODO, /gnuplot.lua: Tippfehler und + Doku, plotmarks haben nun eine eigene Basisgröße + +2007-06-27 peter + + * [r35] /INSTALL, /gnuplot.lua: filldraw zurückgenommen + +2007-06-26 peter + + * [r34] /TODO, /gnuplot.lua, /lua.trm, /test.gnu: bugfixes, + kompaktere Ausgabe + +2007-06-25 peter + + * [r33] /README, /TODO, /gnuplot.lua, /test.gnu: erzeugter Code ist + etwas kompakter + +2007-06-24 peter + + * [r32] /gnuplot.lua, /lua.trm: + ein paar Tippfehler gefixed + + Initialisierung der Terminalvariablen überarbeitet + * [r31] /gnuplot.lua, /lua.trm, /test.gnu: neue Optionen 'xscale' + und 'yscale' + single und double quote strings in Optionen + Koordinaten der Bounding-Box des Plots zugänglich gemacht + Hilfe erweitert + +2007-06-23 peter + + * [r30] /gnuplot.lua, /lua.trm: Koordinaten der bounding box des + Plots sind nun verfügbar + * [r29] /gnuplot.lua, /lua.trm: Aufräumarbeiten + +2007-06-21 peter + + * [r28] /gnuplot.lua, /lua.trm: filled_polygon API geändert + +2007-06-20 peter + + * [r27] /gnuplot.lua, /lua.trm: Umbenennungen + ein kleiner bugfix + "Auflösung" reduziert + * [r26] /INSTALL, /TODO, /gnuplot.lua, /lua.trm, /test.gnu: einige + Konstanten nach lua.trm verschoben + Initialisierung des lua-Skriptes geändert + +2007-06-18 peter + + * [r25] /gnuplot.lua, /test.gnu: typo gefixed + option preamble mehrfach möglich + * [r24] /gnuplot.lua, /lua.trm, /test.gnu: hilfe erweitert + * [r23] /README, /gnuplot.lua, /test.gnu: neue optionen font (für + default font), scale + set_font unterstützung + * [r22] /gnuplot.lua, /lua.trm: lua terminal revision zugänglich + gemacht + * [r21] /gnuplot.lua, /lua.trm, /test.gnu: gnuplot version nun + direkt via variablen zugänglich + * [r20] /gnuplot.lua, /lua.trm: besseres skript handling + neue funktion gp.version() + bugfixes + +2007-06-17 peter + + * [r19] /gnuplot.lua, /lua.trm, /test.gnu: lua optionen parser + kennt nun auch abkürzungen wie in gnuplot + * [r18] /gnuplot.lua, /lua.trm, /test.gnu: verbesserte + Fehlerbehandlung + bugfixes + * [r17] /TODO, /gnuplot.lua, /lua.trm, /test.gnu: Optionen Parser + im Skript + bugfixes + * [r16] /lua.trm: verbesserte fehlerbehandlung + terminal wertet die option "script" aus :-) + diff --git a/term/lua/INSTALL b/term/lua/INSTALL new file mode 100644 index 000000000..42ccd9856 --- /dev/null +++ b/term/lua/INSTALL @@ -0,0 +1,187 @@ + + +This document consists of brief installation instructions of the GNUPLOT Lua +terminal for + + * Unix like systems + * MS Windows + * Mac OS X Leopard + + + + +Requirements +============ + +Since this software comes as a patch to the gnuplot sources and +also uses Lua you need to get the following software before +installation: + + 1. gnuplot 4.2 or 4.3cvs sources from http://gnuplot.sourceforge.net + + 2. a working Lua 5.1 installation e.g. from http://www.lua.org + or http://lua-users.org/wiki/LuaBinaries + + 3. since the only supported backend is PGF/TikZ you may also + want to install the PGF/TikZ packages from + http://www.ctan.org/tex-archive/help/Catalogue/entries/pgf.html + or http://sourceforge.net/projects/pgf/ + + + +Installation on Unix-like systems +================================= + + 1. compile gnuplot + + 2. copy lua.trm to "/term/" + + 3. add the line + + #include "lua.trm" + + at the bottom of /src/term.h + + 4. modify /src/Makefile, so that gnuplot + links against lua by adding e.g. `-llua' to LIBS: + + LIBS = -lm -llua + + You may also have to adapt the include path in regard + to your Lua installation. + + 5. run "make" and "make install" again + + 6. call gnuplot.lua with the parameter 'style' to + generate the LaTeX style file: + + # lua gnuplot.lua style + + Or from the gnuplot console: + + > set term lua createstyle + + The basename of the LaTeX style file is defined in the + 'pgf.LATEX_STYLE_FILE' variable (currently set to + 'gnuplot-lua-tikz'). + + 7. try e.g. the example file: + + # gnuplot test.gnu + # pdflatex test.tex + + + +Installation on Windows systems +=============================== + + 1. see above + + 2. see above + + 3. see above + + 4. edit the makefile, used for the initial build process, similar to + the above, so that the Lua library and headers will be found + + 4b. in 'lua.trm' the line + + #define GNUPLOT_LUA_DIR X11_DRIVER_DIR + + has to be changed, so that GNUPLOT_LUA_DIR points to a directory + for the 'gnuplot.lua' script, e.g.: + + #define GNUPLOT_LUA_DIR "share\luaterm" + + + 5. see above + + 6. see above + + 7. see above + + + +Installation on Mac OS X Leopard (Intel) +(thanks to Federico Maggi) +======================================== + + 0. Update the port base (if needed) and get Gnuplot TikZ terminal: + + $ sudo port sync + $ cd + $ wget http://peter.affenbande.org/gnuplot/gnuplot_lua_terminal.tgz + $ tar zxf gnuplot_lua_terminal.tgz + + + 1. Install lua 5.1 + + $ sudo port install lua + + 2. Fetch GNUPLOT source (if GNUPLOT is alread installed, uninstall + it using $ sudo port uninstall gnuplot) + + $ sudo port -d fetch gnuplot + + + 3. Extract GNUPLOT and configure it + + $ sudo port -d extract gnuplot + $ sudo port -d configure gnuplot + + + 4. Patch the source code + + $ cd {ports_prefix}/var/macports/build/${portbuildpath}/gnuplot-4.2.3/ + (e.g. /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_math_gnuplot/work/gnuplot-4.2.3/ ) + $ cp ~/gnuplot_lua_terminal/lua.trm term/ + $ $EDITOR src/term.h + + and add #include "lua.trm" to the bottom of the file. + Open the Makefile + + $ $EDITOR src/Makefile + + and locate the LIBS variable, append -llua to the other values of LIBS + (aquaterm should be there). + + + 5. Build the port + + $ sudo port build gnuplot + + + 6. Install it + + $ sudo port install gnuplot + + + 7. Copy gnuplot.lua + + $ sudo cp ~/gnuplot_lua_terminal/gnuplot.lua ${ports_prefix}/libexec/gnuplot/4.2/ + + or define the env variable GNUPLOT_LUA_SCRIPT to place it wherever you want. + + + +Additional notes +================ + + Please take care, that the gnuplot.lua script is in the search path + of gnuplot. This is usually the working directory or the directory + defined in X11_DRIVER_DIR/GNUPLOT_LUA_DIR. It is also possible to refer + to a script by using the 'script' terminal option or the environment + variable GNUPLOT_LUA_SCRIPT. + + For more informations about the Lua terminal options just type + > help term lua + and + > set term lua help + from the gnuplot console. + + + Now use the source :-) + + +-Peter Hedwig + diff --git a/term/lua/gnuplot-tikz.lua b/term/lua/gnuplot-tikz.lua index 36287b37a..e8a1801ae 100644 --- a/term/lua/gnuplot-tikz.lua +++ b/term/lua/gnuplot-tikz.lua @@ -622,57 +622,6 @@ f:write([[ %% It is associated with the 'gnuplot.lua' script, and usually generated %% automatically. So take care whenever you make any changes! %% -%% It is associated with the 'gnuplot.lua' script, and usually generated -%% automatically. So take care whenever you make any changes! -%% - -% check for the correct TikZ version -\def\gpchecktikzversion#1.#2\relax{% -\ifnum#1<2% - \PackageError{gnuplot-lua-tikz}{PGF/TikZ version >= 2.0 is required, but version \pgfversion\space was found}{}% -\fi} -\expandafter\gpchecktikzversion\pgfversion\relax - -% FIXME: is there a more elegant way to determine the output format? - -\def\pgfsysdriver@a{pgfsys-dvi.def} % ps -\def\pgfsysdriver@b{pgfsys-dvipdfm.def} % pdf -\def\pgfsysdriver@c{pgfsys-dvipdfmx.def} % pdf -\def\pgfsysdriver@d{pgfsys-dvips.def} % ps -\def\pgfsysdriver@e{pgfsys-pdftex.def} % pdf -\def\pgfsysdriver@f{pgfsys-tex4ht.def} % html -\def\pgfsysdriver@g{pgfsys-textures.def} % ps -\def\pgfsysdriver@h{pgfsys-vtex.def} % ps -\def\pgfsysdriver@i{pgfsys-xetex.def} % pdf - -\newif\ifgppdfout\gppdfoutfalse -\newif\ifgppsout\gppsoutfalse - -\ifx\pgfsysdriver\pgfsysdriver@a - \gppsouttrue -\else\ifx\pgfsysdriver\pgfsysdriver@b - \gppdfouttrue -\else\ifx\pgfsysdriver\pgfsysdriver@c - \gppdfouttrue -\else\ifx\pgfsysdriver\pgfsysdriver@d - \gppsouttrue -\else\ifx\pgfsysdriver\pgfsysdriver@e - \gppdfouttrue -\else\ifx\pgfsysdriver\pgfsysdriver@f - % tex4ht -\else\ifx\pgfsysdriver\pgfsysdriver@g - \gppsouttrue -\else\ifx\pgfsysdriver\pgfsysdriver@h - \gppsouttrue -\else\ifx\pgfsysdriver\pgfsysdriver@i - \gppdfouttrue -\fi\fi\fi\fi\fi\fi\fi\fi\fi - -% uncomment the following lines to make font values "appendable" -% and if you are really sure about that ;-) -% \pgfkeyslet{/tikz/font/.@cmd}{\undefined} -% \tikzset{font/.initial={}} -% \def\tikz@textfont{\pgfkeysvalueof{/tikz/font}} % check for the correct TikZ version \def\gpchecktikzversion#1.#2\relax{% @@ -1769,7 +1718,6 @@ term.options = function(opt_str, initial, t_count) end local print_help = false - local do_clip = false while true do get_next_token() From 2efbb7a0a6b82860425215e5cd1e1fa7c659604e Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Tue, 30 Aug 2011 23:31:25 +0200 Subject: [PATCH 20/54] I forgot to commit mp-gnuplot with definition of gp_rect; temporary switch back to usage of unitsquare --- term/context.trm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/term/context.trm b/term/context.trm index 2df521d36..a85398d7c 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1842,8 +1842,8 @@ CONTEXT_fillbox(int style, unsigned int x1, unsigned int y1, unsigned int width, CONTEXT_endpath(); /* create a new path */ - /*fprintf(gpoutfile, "p := unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", width/100.0, height/100.0, x1/100.0, y1/100.0);*/ - fprintf(gpoutfile, "p := gp_rect ((%.2fa,%.2fb),(%.2fa,%.2fb));\n", x1/100.0, y1/100.0, width/100.0, height/100.0); + fprintf(gpoutfile, "p := unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", width/100.0, height/100.0, x1/100.0, y1/100.0); + /* fprintf(gpoutfile, "p := gp_rect ((%.2fa,%.2fb),(%.2fa,%.2fb));\n", x1/100.0, y1/100.0, width/100.0, height/100.0); */ /* fills the box according to the "style" * the code went out of this routine because of undocumented behaviour From 2a0aa4040f9d16bcf8478379a487f4cfe88542cf Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Wed, 31 Aug 2011 01:23:51 +0200 Subject: [PATCH 21/54] respect 'set encoding utf8' in standalone plots --- term/context.trm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/term/context.trm b/term/context.trm index a85398d7c..77750c329 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1113,6 +1113,22 @@ CONTEXT_init() /* place the header first if this is a standalone graphic */ if(CONTEXT_params.standalone) { + /* If encoding is explicitely set to UTF-8 by gnuplot, use that setting. + * \enableregime only makes a difference for pdfTeX; in LuaTeX and XeTeX UTF-8 is already default, + * so this line will be ignored. + * + * There is no extra support for other encodings on purpose: + * - ConTeXt doesn't support all encodings supported by Gnuplot. + * - In LuaTeX and XeTeX one should not use any other encoding anyway. + * - pdfTeX users are free to use "header '\enableregime[...]'" */ + switch(encoding) { + case S_ENC_UTF8: + fputs("\\enableregime\n [utf-8]\n", gpoutfile); + break; + default: + /* do nothing */ + break; + } /* load the gnuplot module */ fputs("\\usemodule\n [gnuplot]\n", gpoutfile); /* enable color (the only place where "color" is indeed used so far) */ From d7c1ae15ace22312991ebe504ae6c4f744289088 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Wed, 31 Aug 2011 02:22:56 +0200 Subject: [PATCH 22/54] some cleanup in comments (needs a native speaker to check anyway) --- term/context.trm | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/term/context.trm b/term/context.trm index 77750c329..96931a97f 100644 --- a/term/context.trm +++ b/term/context.trm @@ -5,7 +5,7 @@ /* GNUPLOT - context.trm */ /*[ - * Copyleft 2006 Mojca Miklavec + * Copyleft 2006-2011 Mojca Miklavec * * Permission to use, copy, and distribute this software and its * documentation for any purpose with or without fee is hereby granted, @@ -50,7 +50,7 @@ * - author of the module m-gnuplot.tex to support inclusion of Gnuplot * graphics into ConTeXt * - constant support on the most tricky issues - * Taco Hoekwater (developer of Metapost & pdfTeX) + * Taco Hoekwater (developer of MetaPost, pdfTeX & LuaTeX) * * - some code of this file, many tricks, lots of bug fixes, * suggestions and testing @@ -82,9 +82,9 @@ * \usemodule[gnuplot] * * \starttext - * \section{Drawing nice graphs with \sc gnuplot} + * \title{Drawing nice graphs with \sc gnuplot} * \startGNUPLOTscript{sin} - * pot sin(x) + * plot sin(x) t '$\sin(x)$' * \stopGNUPLOTscript * \useGNUPLOTgraphic[sin] * \stoptext @@ -2350,6 +2350,7 @@ START_HELP(context) " With `font` you can set font name and size in standalone graphics.", " In non-standalone (`input`) mode only the font size is important", " to reserve enough space for text labels.", +" The command" " set term context font \"myfont,ss\" 10pt", " will result in", " \\setupbodyfont[myfont,ss,10pt]", @@ -2360,19 +2361,11 @@ START_HELP(context) " For compatibility with other terminals font size may be specified without units,", " in which case the default unit \"pt\" will be appended to it, but you can also", " use any other supported unit, for example \"11dd\".\n", -" However, it's your own responsibility to use the proper typescript,", +" It is your own responsibility to provide proper typescripts (and header),", " otherwise switching the font will have no effect.\n", -" For an already supported font you can use", +" For a standard font in ConTeXt MKII (pdfTeX) you could use:", " set terminal context standalone header '\\usetypescript[iwona][ec]' \\", " font \"iwona,ss,11pt\"", -" If you also need your own typescripts, then you could write", -" set terminal context standalone \\", -" header '\\usetypescriptfile[type-myfont] \\usetypescript[myfont][uc]' \\", -" font \"myfont\" 10pt", -"", -" Note: The examples with typescripts above might become obsolete", -" with new versions of (pdf)TeX and already differ in XeTeX. They should be regarded", -" only as examples of the usage inside gnuplot.", " Please take a look into ConTeXt documentation, wiki or mailing list (archives)", " for any up-to-date information about font usage." "", @@ -2382,27 +2375,25 @@ START_HELP(context) " set terminal context size 4/5, 2.54*4cm # 4in, 4in", " For standalone (whole-page) plots with labels in UTF-8 encoding:", " set terminal context standalone header '\\enableregime[utf-8]'", -"", -/* TODO: LaTeX formatting */ +"", /* TODO: LaTeX formatting */ "2 Requirements", -" - files from http://modules.contextgarden.net/gnuplot", -"", -" - please use the latest version of ConTeXt if possible (but at least one from 2007 or newer)", +" Requirements:", "", -" - texmfstart has to be in PATH - teTeX is too old to process your files", -"", -" - if you want to call gnuplot on-the-fly, you need to have write18 enabled", -" (on most TeX distributionts this can be set with shell_escape=t in texmf.cnf)", +" - gnuplot module for ConTeXt and a recent version of ConTeXt", +" - If you want to call gnuplot on-the-fly, you need to have write18 enabled.", +" (In most TeX distributionts this can be set with shell_escape=t in texmf.cnf.)", "", " See http://wiki.contextgarden.net/Gnuplot for details about this terminal", " and for more exhaustive help & examples.", +"", "2 Calling gnuplot from ConTeXt", " The easiest way to make plots in ConTeXt documents is", " \\usemodule[gnuplot]", " \\starttext", -" \\section{How to make nice plots with {\\sc gnuplot}}", +" \\title{How to draw nice plots with {\\sc gnuplot}?}", " \\startGNUPLOTscript[sin]", -" plot sin(x)", +" set format y \"%.1f\"", +" plot sin(x) t '$\\sin(x)$'", " \\stopGNUPLOTscript", " \\useGNUPLOTgraphic[sin]", " \\stoptext", From b38c3bf1503b658e3c80c60a1e3ba10684ffc720 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 2 Sep 2011 22:40:56 +0200 Subject: [PATCH 23/54] add some files to .gitignore and remove docs/pdffigures (it constantly complains about deleted file) --- .gitignore | 7 +++++++ docs/pdffigures.tex | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 docs/pdffigures.tex diff --git a/.gitignore b/.gitignore index ca736e749..4613a32f2 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,10 @@ tutorial/tutorial.aux tutorial/tutorial.dvi tutorial/tutorial.log tutorial/tutorial.toc +docs/gnuplot.info +docs/gnuplot.texi +docs/pdffigures.tex +share/LaTeX/gnuplot-lua-tikz-common.tex +share/LaTeX/gnuplot-lua-tikz.sty +share/LaTeX/gnuplot-lua-tikz.tex +share/LaTeX/t-gnuplot-lua-tikz.tex diff --git a/docs/pdffigures.tex b/docs/pdffigures.tex deleted file mode 100644 index e48ea902d..000000000 --- a/docs/pdffigures.tex +++ /dev/null @@ -1,7 +0,0 @@ -% -% $Id: pdffigures.tex,v 1.1 2010/03/08 23:41:15 sfeam Exp $ -% -% This file is modified dynamically by "make" depending on whether or not -% figures are to be included in the documentation -% \usepackage{graphicx} -% \usepackage{picins} From a41c380440c13ebd54f8178ea1d12a44d0e069c7 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 2 Sep 2011 23:10:04 +0200 Subject: [PATCH 24/54] remove TERM_BINARY flag --- term/context.trm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/term/context.trm b/term/context.trm index 96931a97f..6a260fefe 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2234,8 +2234,8 @@ TERM_TABLE_START(context_driver) CONTEXT_linetype, CONTEXT_put_text, CONTEXT_text_angle, CONTEXT_justify_text, CONTEXT_point, CONTEXT_arrow, CONTEXT_set_font, CONTEXT_pointsize, - /* also add TERM_CAN_CLIP once you understand what it does; Why binary? Because of UTF-8? */ - TERM_BINARY | TERM_CAN_DASH | TERM_ALPHA_CHANNEL | TERM_LINEWIDTH | TERM_FONTSCALE | TERM_IS_LATEX /* flags */, + /* also add TERM_CAN_CLIP once you understand what it does */ + TERM_CAN_DASH | TERM_ALPHA_CHANNEL | TERM_LINEWIDTH | TERM_FONTSCALE | TERM_IS_LATEX /* flags */, 0 /* suspend */, 0 /* resume*/, CONTEXT_fillbox, CONTEXT_linewidth #ifdef USE_MOUSE From c3a09f1ae70a62139133b72bed448f718bcd668e Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 01:44:10 +0200 Subject: [PATCH 25/54] fix tikz definitions --- term/lua/gnuplot-tikz.help | 6 +++++- term/lua/gnuplot-tikz.lua | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/term/lua/gnuplot-tikz.help b/term/lua/gnuplot-tikz.help index c773a3a09..7c2768fa2 100644 --- a/term/lua/gnuplot-tikz.help +++ b/term/lua/gnuplot-tikz.help @@ -15,6 +15,7 @@ " {nogppoints | gppoints}", " {picenvironment | nopicenvironment}", " {noclip | clip}", +" {notightboundingbox | tightboundingbox}", " {background \"\"}", " {size {unit},{unit}}", " {scale ,}", @@ -62,7 +63,10 @@ " 'clip' crops the plot at the defined canvas size. Default is", " 'noclip' by which only a minimum bounding box of the canvas size", " is set. Neither a fixed bounding box nor a crop box is set if the", -" 'plotsize' option is used.", +" 'plotsize' or 'tightboundingbox' option is used.", +"", +" If 'tightboundingbox' is set the 'clip' option is ignored and the", +" final bounding box is the natural bounding box calculated by tikz.", "", " 'background' sets the background color to the value specified in", " the argument. must be a valid color name or", diff --git a/term/lua/gnuplot-tikz.lua b/term/lua/gnuplot-tikz.lua index e8a1801ae..6f30114b5 100644 --- a/term/lua/gnuplot-tikz.lua +++ b/term/lua/gnuplot-tikz.lua @@ -789,9 +789,9 @@ f:write([[ % wrapper for color settings \def\gpcolor#1{\tikzset{global #1}} -\tikzset{rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\color{.}}} -\tikzset{global rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\color{.}}} -\tikzset{global color/.code={\color{#1}}} +\tikzset{rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\tikzset{color=.}}} +\tikzset{global rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\pgfsetcolor{.}}} +\tikzset{global color/.code={\pgfsetcolor{#1}}} % prevent plot mark distortions due to changes in the PGF transformation matrix % use `\gpscalepointstrue' and `\gpscalepointsfalse' for enabling and disabling From 8cfbf90f27363898b9ee8ad7544c0eef142d2b2e Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 02:00:48 +0200 Subject: [PATCH 26/54] remove traces of btex/textext/sometxt and old comments --- term/context.trm | 97 ------------------------------------------------ 1 file changed, 97 deletions(-) diff --git a/term/context.trm b/term/context.trm index 6a260fefe..524f849b6 100644 --- a/term/context.trm +++ b/term/context.trm @@ -409,30 +409,6 @@ static t_sm_palette *CONTEXT_old_palette; /* global parameters */ /*********************/ -/* btex ... etex is hopefully not needed any more (and not supported either) - * it may be removed, but it was left here for historical reasons - * and if I decide to extend this terminal to support plain metapost, - * it will be needed again. - * - * textext("...") is not needed and supported any more either, - * but was left here for historical reasons & (hidden) testing purposes - * It will be removed. - * - * At the beginning textext("...") has been used, but: - * - it was extremely inefficient - * - it caused only troubles (different settings etc.) - * Because of the second, btex ... etex has been provided as a slightly - * more reliable alternative. The first one hasn't been solved until - * a new command for text placement showed up. - * - * After Hans Hagen has patched it, he implemented a new command \sometxt{...}, - * which was one of the most crucial steps towards usability of this terminal. - * Without it the terminal was close-to-useless because of unbearably slow speed. - * - * See also "\sometxt" MyWay on contextgarden.net about that. - * Hans & Taco: thanks a lot for this wonderful macro!!! - */ -enum CONTEXT_TEXT { CONTEXT_TEXT_TEXTEXT, CONTEXT_TEXT_SOMETXT, CONTEXT_TEXT_BTEX }; /* whether points are drawn with metapost or typeset with TeX (easy configurable) */ enum CONTEXT_POINTS { CONTEXT_POINTS_WITH_METAPOST, CONTEXT_POINTS_WITH_TEX }; @@ -449,7 +425,6 @@ typedef struct CONTEXT_params_t { float scale_dashlength; /* 1.0 */ float scale_linewidth; /* 1.0 */ float scale_text; /* 1.0 */ - enum CONTEXT_TEXT text; /* CONTEXT_TEXT_SOMETXT */ enum CONTEXT_POINTS points; /* CONTEXT_POINTS_WITH_METAPOST */ char font[MAX_ID_LEN+1]; /* "" */ dimension_t fontsize; /* 12pt */ @@ -469,7 +444,6 @@ typedef struct CONTEXT_params_t { 1.0,\ 1.0,\ 1.0,\ - CONTEXT_TEXT_SOMETXT,\ CONTEXT_POINTS_WITH_METAPOST,\ "",\ {CONTEXT_FONTSIZE, UNIT_PT}\ @@ -489,7 +463,6 @@ enum CONTEXT_id { CONTEXT_OPT_LINEJOIN_MITERED, CONTEXT_OPT_LINEJOIN_ROUNDED, CONTEXT_OPT_LINEJOIN_BEVELED, CONTEXT_OPT_LINECAP_BUTT, CONTEXT_OPT_LINECAP_ROUNDED, CONTEXT_OPT_LINECAP_SQUARED, CONTEXT_OPT_SCALE_DASHLENGTH, CONTEXT_OPT_SCALE_LINEWIDTH, CONTEXT_OPT_SCALE_TEXT, - CONTEXT_OPT_TEXT_SOMETXT, CONTEXT_OPT_TEXT_TEXTEXT, CONTEXT_OPT_POINTS_WITH_METAPOST, CONTEXT_OPT_POINTS_WITH_TEX, CONTEXT_OPT_DEFAULTFONT, CONTEXT_OPT_FONT, @@ -522,8 +495,6 @@ static struct gen_table CONTEXT_opts[] = { { "lw", CONTEXT_OPT_SCALE_LINEWIDTH }, { "fontscale", CONTEXT_OPT_SCALE_TEXT }, { "textscale", CONTEXT_OPT_SCALE_TEXT }, /* backward compatibility */ - { "textext", CONTEXT_OPT_TEXT_TEXTEXT }, - { "sometxt", CONTEXT_OPT_TEXT_SOMETXT }, { "pointswithmp", CONTEXT_OPT_POINTS_WITH_METAPOST}, { "pointswithmetapost", CONTEXT_OPT_POINTS_WITH_METAPOST}, { "pointswithtex", CONTEXT_OPT_POINTS_WITH_TEX}, @@ -564,7 +535,6 @@ CONTEXT_params_reset() CONTEXT_params.scale_dashlength = 1.0; CONTEXT_params.scale_linewidth = 1.0; CONTEXT_params.scale_text = 1.0; - CONTEXT_params.text = CONTEXT_TEXT_SOMETXT; CONTEXT_params.points = CONTEXT_POINTS_WITH_METAPOST; CONTEXT_params.font[0] = 0; CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; @@ -790,29 +760,6 @@ CONTEXT_options() CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; CONTEXT_params.fontsize.unit = UNIT_PT; break; - /* since the other two options are disabled, it makes no sense any more - will disappear */ - case CONTEXT_OPT_TEXT_SOMETXT: - c_token++; - CONTEXT_params.text = CONTEXT_TEXT_SOMETXT; - break; - /* not supported by metapost macros any more - will disappear */ - case CONTEXT_OPT_TEXT_TEXTEXT: - c_token++; - /* CONTEXT_params.text = CONTEXT_TEXT_TEXTEXT; */ - int_warn(c_token,"textext is now deprecated: sometxt will be used instead!"); - break; -/* - * so old and deprecated that we don't want users - * to complain about its inefficiency - * - * but if this terminal is going to support plain metapost in the future, - * we'll need it again - * - case CONTEXT_OPT_BTEX: - c_token++; - CONTEXT_params.text = CONTEXT_TEXT_BTEX; - break; -*/ case CONTEXT_OPT_POINTS_WITH_METAPOST: c_token++; CONTEXT_params.points = CONTEXT_POINTS_WITH_METAPOST; @@ -1028,23 +975,6 @@ CONTEXT_options() ); strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); -/* - * this is undocumented on purpose: - * - users are not supposed to use textext("...") any more, - * but it's left here if one decides to create a plain metapost terminal out of it - * - * switch(CONTEXT_params.text) { - * case CONTEXT_TEXT_BTEX : - * strncat(term_options, "btex ", - * sizeof(term_options)-strlen(term_options)); break; - * case CONTEXT_TEXT_SOMETXT : - * strncat(term_options, "sometxt ", - * sizeof(term_options)-strlen(term_options)); break; - * case CONTEXT_TEXT_TEXTEXT : - * strncat(term_options, "textext ", - * sizeof(term_options)-strlen(term_options)); break; - * } - */ switch (CONTEXT_params.points) { case CONTEXT_POINTS_WITH_TEX : strncat(term_options, "pointswithtex ", @@ -1519,7 +1449,6 @@ CONTEXT_put_text(unsigned int x, unsigned int y, const char str[]) fprintf(gpoutfile, "angle(%d), ", CONTEXT_ang); } /* alignment - "center" is optional, but we'll add it anyway */ - /* + \sometxt */ fprintf(gpoutfile, "align(%s), \\sometxt[gp]", alignments[alignment]); /* fontface/fontsize - optional second argument */ if(CONTEXT_font_explicit[0] != 0) @@ -1579,32 +1508,6 @@ CONTEXT_point (unsigned int x, unsigned int y, int number) CONTEXT_endpath(); fprintf(gpoutfile, "gp_point(%.2fa,%.2fb,%d);\n", x/100.0, y/100.0, number); - - /* - * This is how the points were draw at the beginning when macros - * for enclusion of TeX portions weren't as advanced as they're now. - * Currently there is some trickery involved which enabled simpler - * definitions of points that have to be typeset with TeX. - * - * See m-gnuplot.tex for details. - * - * This portion of code is left here for historical reasons. - * It might be needed again if clean metapost code will ever be produced - * with this terminal. - * - // use the points defined in the module (drawn with metapost) - if(CONTEXT_params.points == CONTEXT_POINTS_WITH_METAPOST) { - fprintf(gpoutfile, "gp_point(%.2fa,%.2fb,%d);\n", x/100.0, y/100.0, number); - // use the points defined in the module (typeset with TeX) - } else if(CONTEXT_params.points == CONTEXT_POINTS_WITH_TEX) { - // change \sometxt{\GPpoint{N}} to \GPpoint{N} - fprintf(gpoutfile, "gp_point(%.2fa,%.2fb,\\sometxt{\\GPpoint{%d}});\n", x/100.0, y/100.0, number); - } else { - // should not happen unless there's an error in this terminal - // or, if one will ever want the old "btex ... etex" functionality, - // it might fit in here - } - */ } /* *************** From d1c6649f03cbef0ecdf9abb1ec09206a2e4542f6 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 02:26:14 +0200 Subject: [PATCH 27/54] do not repeat the first point when drawing a cyclic path (delay drawing) --- term/context.trm | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/term/context.trm b/term/context.trm index 524f849b6..9b41066b5 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1275,7 +1275,7 @@ CONTEXT_endpath() /* regular non-cyclic path */ } else { - fputs(";\ngp_draw(p);\n", gpoutfile); + fprintf(gpoutfile, "--(%.2fa,%.2fb);\ngp_draw(p);\n", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); } /* we're not inside path any more */ @@ -1362,15 +1362,16 @@ CONTEXT_vector(unsigned int x, unsigned int y) CONTEXT_startpath(); } else { /* or prevent too long lines if you're in the middle of a path */ - if((CONTEXT_path_count % CONTEXT_LINEMAX) == 1) { + if((CONTEXT_path_count % CONTEXT_LINEMAX) == 2) { fputs("\n ", gpoutfile); - CONTEXT_path_count = 1; + CONTEXT_path_count = 2; } + /* and output the previous point */ + fprintf(gpoutfile, "--(%.2fa,%.2fb)", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); } CONTEXT_posx = x; CONTEXT_posy = y; - fprintf(gpoutfile, "--(%.2fa,%.2fb)", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); CONTEXT_path_count++; } From 2a5109764506af4a4e9df42aa5cba13bd8533730 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 02:52:53 +0200 Subject: [PATCH 28/54] replace textscale with fontscale --- term/context.trm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/term/context.trm b/term/context.trm index 9b41066b5..b40b7f9ad 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1122,12 +1122,12 @@ CONTEXT_init() fprintf(gpoutfile, " dashlength=%g, %% scaling factor for dash lengths\n", CONTEXT_params.scale_dashlength); /* linewidth (gp_scale_linewidth): 1.0 */ - fprintf(gpoutfile, " linewidth=%g, %% scaling factor for line widths (1.0 means 0.4bp)\n", CONTEXT_params.scale_linewidth); + fprintf(gpoutfile, " linewidth=%g, %% scaling factor for line widths (1.0 means 0.4bp)\n", CONTEXT_params.scale_linewidth); - /* textscale (gp_scale_text): 1.0 */ + /* fontscale (gp_scale_text): 1.0 */ /* written out just for reference - it's commented out since it needs to be part of graphic and affects estimation of label sizes */ - fprintf(gpoutfile, " %%textscale=%g, %% scaling factor for text labels\n", CONTEXT_params.scale_text); + fprintf(gpoutfile, " %%fontscale=%g, %% scaling factor for text labels\n", CONTEXT_params.scale_text); /* points (gp_points_with): metapost/tex (gp_points_with_metapost/gp_points_with_tex) * default: metapost */ From b1055cd31b8775aeb0b49cc75ebbfc6e647c5197 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 02:54:01 +0200 Subject: [PATCH 29/54] default line width is 0.5bp, not 0.4bp --- term/context.trm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/term/context.trm b/term/context.trm index b40b7f9ad..93b771d1b 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1122,7 +1122,7 @@ CONTEXT_init() fprintf(gpoutfile, " dashlength=%g, %% scaling factor for dash lengths\n", CONTEXT_params.scale_dashlength); /* linewidth (gp_scale_linewidth): 1.0 */ - fprintf(gpoutfile, " linewidth=%g, %% scaling factor for line widths (1.0 means 0.4bp)\n", CONTEXT_params.scale_linewidth); + fprintf(gpoutfile, " linewidth=%g, %% scaling factor for line widths (1.0 means 0.5bp)\n", CONTEXT_params.scale_linewidth); /* fontscale (gp_scale_text): 1.0 */ /* written out just for reference - it's commented out since it needs to be part of graphic @@ -2242,7 +2242,7 @@ START_HELP(context) "", " `dashlength` or `dl` scales the length of the dashed-line segments by
.", " `linewidth` or `lw` scales all linewidths by ." -" (lw 1 stands for 0.4bp, which is the default line width when drawing with Metapost.)", +" (lw 1 stands for 0.5bp, which is the default line width when drawing with Metapost.)", " `fontscale` scales text labels for factor relative to default document font.", "", " `pointswithmetapost` uses predefined point shapes, drawn in Metapost.", From 5b1995ad686f07e035db8ada37f80b6e192681cb Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 03:05:31 +0200 Subject: [PATCH 30/54] replace pointswithtex->texpoints, pointswithmetapost->mppoints --- term/context.trm | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/term/context.trm b/term/context.trm index 93b771d1b..41925741f 100644 --- a/term/context.trm +++ b/term/context.trm @@ -494,10 +494,12 @@ static struct gen_table CONTEXT_opts[] = { { "linew$idth", CONTEXT_OPT_SCALE_LINEWIDTH }, { "lw", CONTEXT_OPT_SCALE_LINEWIDTH }, { "fontscale", CONTEXT_OPT_SCALE_TEXT }, - { "textscale", CONTEXT_OPT_SCALE_TEXT }, /* backward compatibility */ - { "pointswithmp", CONTEXT_OPT_POINTS_WITH_METAPOST}, - { "pointswithmetapost", CONTEXT_OPT_POINTS_WITH_METAPOST}, - { "pointswithtex", CONTEXT_OPT_POINTS_WITH_TEX}, + { "textscale", CONTEXT_OPT_SCALE_TEXT }, /* backward compatibility */ + { "mp$points", CONTEXT_OPT_POINTS_WITH_METAPOST}, + { "tex$points", CONTEXT_OPT_POINTS_WITH_TEX}, + { "pointswithmp", CONTEXT_OPT_POINTS_WITH_METAPOST}, /* (removable) backward compatibility */ + { "pointswithmetapost", CONTEXT_OPT_POINTS_WITH_METAPOST}, /* (removable) backward compatibility */ + { "pointswithtex", CONTEXT_OPT_POINTS_WITH_TEX}, /* (removable) backward compatibility */ { "font", CONTEXT_OPT_FONT }, { "defaultfont", CONTEXT_OPT_DEFAULTFONT }, { NULL, CONTEXT_OPT_OTHER } @@ -977,10 +979,10 @@ CONTEXT_options() switch (CONTEXT_params.points) { case CONTEXT_POINTS_WITH_TEX : - strncat(term_options, "pointswithtex ", + strncat(term_options, "texpoints ", sizeof(term_options)-strlen(term_options)); break; case CONTEXT_POINTS_WITH_METAPOST : - strncat(term_options, "pointswithmetapost ", + strncat(term_options, "mppoints ", sizeof(term_options)-strlen(term_options)); break; } strncat(term_options,"font \"", sizeof(term_options)-strlen(term_options)); @@ -1724,7 +1726,7 @@ CONTEXT_set_font(const char *font) * Sets the (relative) point size for subsequent points * * The base point size is defined "somewhere else": - * - depends on the font[size] used when "pointswithtex" option is on + * - depends on the font[size] used when "texpoints" option is on */ TERM_PUBLIC void CONTEXT_pointsize(double pointsize) @@ -2194,7 +2196,7 @@ START_HELP(context) " {dashlength | dl
}", " {linewidth | lw }", " {fontscale }", -" {pointswithmetapost | pointswithmp | pointswithtex}", +" {mppoints | texpoints}", " {defaultfont | font {} |", " font \"{,}\" {fontsize}}", "", @@ -2245,8 +2247,8 @@ START_HELP(context) " (lw 1 stands for 0.5bp, which is the default line width when drawing with Metapost.)", " `fontscale` scales text labels for factor relative to default document font.", "", -" `pointswithmetapost` uses predefined point shapes, drawn in Metapost.", -" `pointswithtex` uses easily configurable set of symbols, defined with ConTeXt", +" `mppoints` uses predefined point shapes, drawn in Metapost.", +" `texpoints` uses easily configurable set of symbols, defined with ConTeXt", " in the following way:", " \\defineconversion[my own points][+,{\\ss x},\\mathematics{\\circ}]", " \\setupGNUPLOTterminal[context][points=tex,pointset=my own points]", From 79b25c9af7f4a781eb337cab61fb84168513bae3 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 05:08:20 +0200 Subject: [PATCH 31/54] add support for images (not yet enabled) --- term/context.trm | 127 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 112 insertions(+), 15 deletions(-) diff --git a/term/context.trm b/term/context.trm index 41925741f..4b9fd7cd2 100644 --- a/term/context.trm +++ b/term/context.trm @@ -137,9 +137,7 @@ TERM_PUBLIC int CONTEXT_make_palette __PROTO((t_sm_palette *palette)); /* TERM_PUBLIC void CONTEXT_previous_palette __PROTO((void)); do we need it? */ TERM_PUBLIC void CONTEXT_set_color __PROTO((t_colorspec *colorspec)); TERM_PUBLIC void CONTEXT_filled_polygon __PROTO((int points, gpiPoint *corners)); -#ifdef WITH_IMAGE -/* TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint *, t_imagecolor)); TODO */ -#endif /* WITH_IMAGE */ +TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint *, t_imagecolor)); /* Metapost can only deal with numbers between 2^{-16} and 2^12=4069 */ /* the values will be set later */ @@ -1224,7 +1222,7 @@ CONTEXT_graphics() CONTEXT_params.ysize.value, unit_name(CONTEXT_params.ysize.unit)); fprintf(gpoutfile, "%% horizontal and vertical scaling factors\na:=w/%.2f; b:=h/%.2f;\n", (term->xmax)/100.0, (term->ymax)/100.0); - fprintf(gpoutfile, "%% temporary variable for storing the path\nsave p; path p;\n%%\n"); + fprintf(gpoutfile, "%% temporary variable for storing the path and images\nsave p, img, ima; path p; string img, ima;\n%%\n"); fprintf(gpoutfile, "%% -------------------------\n"); fprintf(gpoutfile, "%% Different initialisations\n"); fprintf(gpoutfile, "%% -------------------------\n"); @@ -2100,21 +2098,120 @@ CONTEXT_filled_polygon (int points, gpiPoint *corners) CONTEXT_fill(corners->style); } -/* - * TODO: figure out first how this can be done/ - * additional support has to be implemented in pdfTeX and/or ConTeX to support - * efficient inclusion of bitmap images - * (this is a placeholder and a reminder that it has to me finished) - -#ifdef WITH_IMAGE +/* *************** + * CONTEXT_image * + * *************** + * + Plot a pixel-based image on the display device. + 'M' is the number of pixels along the y-dimension of the image and + 'N' is the number of pixels along the x-dimension of the image. The + coordval pointer 'image' is the pixel values normalized to the range + [0:1]. These values should be scaled appropriately for the output + device. The 'image' data starts in the upper left corner and scans + along rows finishing in the lower right corner. If 'color_mode' is + IC_PALETTE, the terminal is to use palette lookup to generate color + information. In this scenario the size of 'image' is M*N. If + 'color_mode' is IC_RGB, successive bytes of the data structure are + interpreted as RGB components of a single pixel. In this scenario + the size of 'image' is 3*M*N. The data appears in RGB triples, i.e., + image[0] = R(1,1), image[1] = G(1,1), image[2] = B(1,1), + image[3] = R(1,2), image[4] = G(1,2), ..., image[3*M*N-1] = B(M,N). + The mode IC_RGBA is similar except that four values red, green, blue, + alpha per pixel are passed to the terminal in the image structure. + The 'image' is actually an "input" image in the sense that + it must also be properly resampled for the output device. Many output + media, e.g., PostScript, do this work via various driver functions. + To determine the appropriate rescaling, the 'corner' information + should be used. There are four entries in the gpiPoint data array. + 'corner[0]' is the upper left corner (in terms of plot location) of + the outer edge of the image. Similarly, 'corner[1]' is the lower + right corner of the outer edge of the image. (Outer edge means the + outer extent of the corner pixels, not the middle of the corner + pixels.) 'corner[2]' is the upper left corner of the visible part + of the image, and 'corner[3]' is the lower right corner of the visible + part of the image. The information is provided in this way because + often it is necessary to clip a portion of the outer pixels of the + image. + */ +/* This code is working in ConTeXt MKIV, + * but it needs some thinking and additional support in ConTeXt core before it is enabled. + * + * ConTeXt MKII needs a different strategy + * (maybe writing out a PNG and including it as an image?). + */ TERM_PUBLIC void CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imagecolor color_mode) { -} + int i, j, k, line_length, components_per_color; + unsigned char byte; + rgb_color color; + + /* Palette colors have to be converted into RGB first */ + if (color_mode == IC_PALETTE) { + /* write the image data */ + fprintf(gpoutfile, "img := \"%%\n"); + line_length = 0; + for(i=0; i= 16) { + line_length = 1; + fprintf(gpoutfile, "%%\n"); + } + rgb1maxcolors_from_gray(image[i], &color); + fprintf(gpoutfile, "%02x%02x%02x", (unsigned char)(255*color.r), (unsigned char)(255*color.g), (unsigned char)(255*color.b)); + } + fprintf(gpoutfile, "\";\n"); -#endif -*/ + /* IC_RGB or IC_RGBA */ + } else { + if (color_mode == IC_RGBA) { + components_per_color = 4; + } else { /* IC_RGB */ + components_per_color = 3; + } + + /* write the image data */ + fprintf(gpoutfile, "img := \"%%\n"); + line_length = 0; + for(i=0; i= 16) { + line_length = 1; + fprintf(gpoutfile, "%%\n"); + } + for(k=0; k<3; k++) { + fprintf(gpoutfile, "%02x", (unsigned char)(image[i*components_per_color+k]*255)); + } + } + fprintf(gpoutfile, "\";\n"); + + /* transparency mask */ + if (color_mode == IC_RGBA) { + fprintf(gpoutfile, "ima := \"%%\n"); + line_length = 0; + for(i=0; i= 3*16) { + line_length = 1; + fprintf(gpoutfile, "%%\n"); + }; + fprintf(gpoutfile, "%02x", (unsigned char)(image[i*components_per_color+3]*255)); + } + fprintf(gpoutfile, "\";\n"); + } + } + + /* TODO: clipping and transparency handling is not yet completely ready */ + /* (width, height) in pixels; + * (x,y) lower left + * (x,y) upper right + */ + fprintf(gpoutfile, "gp_image_rgb"); + if (color_mode == IC_RGB) + fprintf(gpoutfile, "_alpha"); + fprintf(gpoutfile, "((%u,%u),(%.2fa,%.2fb),(%.2fa,%.2fb));\n", + N, M, corner[0].x/100.0, corner[1].y/100.0, corner[1].x/100.0, corner[0].y/100.0); + fprintf(gpoutfile, "%% I also want to clip to ((%.2fa,%.2fb), (%.2fa,%.2fb))\n", + corner[2].x/100.0, corner[3].y/100.0, corner[3].x/100.0, corner[2].y/100.0); +} /* * TODO: Implement this function for smooth shading @@ -2151,7 +2248,7 @@ TERM_TABLE_START(context_driver) 0, /* XXX: CONTEXT_previous_palette: not sure if we need it at all (PS does "grestore") */ CONTEXT_set_color, CONTEXT_filled_polygon, - 0, /* image; TODO: implement it (CONTEXT_image) */ + 0, /* TODO: CONTEXT_image */ 0, 0, 0, /* No enhanced text mode because this is TeX */ 0, /* layer (used to signal front/back text, used in canvas, PS, epslatex, ...); XXX: figure out what this has to do */ 0 /* path (Path control for end-joins of closed polygons on PostScript-like devices); TODO: implement it (CONTEXT_path) */ From 2c7a096794c07b6a2a43f29ae5f43c90b735686a Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 12:39:48 +0200 Subject: [PATCH 32/54] printf for double should be %f, not %lf --- term/context.trm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/term/context.trm b/term/context.trm index 4b9fd7cd2..f2890b699 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1742,7 +1742,7 @@ CONTEXT_pointsize(double pointsize) /* close and draw the current path first */ if (CONTEXT_path_count>0) CONTEXT_endpath(); - fprintf(gpoutfile, "gp_set_pointsize(%.3lf);\n", pointsize); + fprintf(gpoutfile, "gp_set_pointsize(%.3f);\n", pointsize); CONTEXT_old_pointsize = pointsize; } } @@ -1857,7 +1857,7 @@ CONTEXT_linewidth(double linewidth) /* close and draw the current path first */ if (CONTEXT_path_count>0) CONTEXT_endpath(); - fprintf(gpoutfile, "gp_set_linewidth(%.3lf);\n", linewidth); + fprintf(gpoutfile, "gp_set_linewidth(%.3f);\n", linewidth); CONTEXT_old_linewidth = linewidth; } } From 9bb51c719a10e0282546760c6ca4e13486591e89 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 12:42:32 +0200 Subject: [PATCH 33/54] change some floats to doubles --- term/context.trm | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/term/context.trm b/term/context.trm index f2890b699..c9049a79a 100644 --- a/term/context.trm +++ b/term/context.trm @@ -221,7 +221,7 @@ static struct gen_table units[] = { }; typedef struct dimension_t { - float value; + double value; size_unit unit; } dimension_t; @@ -420,9 +420,9 @@ typedef struct CONTEXT_params_t { TBOOLEAN dashed; /* true */ enum LINEJOIN linejoin; /* MITER */ enum LINECAP linecap; /* BUTT */ - float scale_dashlength; /* 1.0 */ - float scale_linewidth; /* 1.0 */ - float scale_text; /* 1.0 */ + double scale_dashlength; /* 1.0 */ + double scale_linewidth; /* 1.0 */ + double scale_text; /* 1.0 */ enum CONTEXT_POINTS points; /* CONTEXT_POINTS_WITH_METAPOST */ char font[MAX_ID_LEN+1]; /* "" */ dimension_t fontsize; /* 12pt */ @@ -557,8 +557,8 @@ CONTEXT_options() char tmp_font[MAX_ID_LEN+1] = ""; char tmp_term_options[MAX_LINE_LEN+1] = ""; - enum size_unit tmp_unit[2]; - float tmp_float[2]; + enum size_unit tmp_unit[2]; + double tmp_double[2]; dimension_t tmp_dimension; while (!END_OF_COMMAND) { @@ -579,9 +579,9 @@ CONTEXT_options() * in output routine as well */ for (i=0; i<2; i++) { - tmp_float[i] = real(const_express(&a)); + tmp_double[i] = real(const_express(&a)); /* check if the size is positive (minimal requirement) */ - if (tmp_float[i] <= 0) + if (tmp_double[i] <= 0) int_error(c_token-1,"Size has to be positive."); /* if unit (inch or cm) is used, use that one, otherwise interpret that value * as if it was a relative to the default value (5in); ie: 1.5 means 7.5 inches */ @@ -618,26 +618,26 @@ CONTEXT_options() if (equals(c_token, ",")) { c_token++; /* default xsize is 5in - use the current size as relative value */ - tmp_float[0] *= CONTEXT_XSIZE_VALUE; - tmp_unit[0] = CONTEXT_XSIZE_UNIT; + tmp_double[0] *= CONTEXT_XSIZE_VALUE; + tmp_unit[0] = CONTEXT_XSIZE_UNIT; } else { - tmp_float[1] = tmp_float[0]*CONTEXT_YSIZE_VALUE; - tmp_float[0] = tmp_float[0]*CONTEXT_XSIZE_VALUE; - tmp_unit[0] = CONTEXT_XSIZE_UNIT; - tmp_unit[1] = CONTEXT_YSIZE_UNIT; + tmp_double[1] = tmp_double[0]*CONTEXT_YSIZE_VALUE; + tmp_double[0] = tmp_double[0]*CONTEXT_XSIZE_VALUE; + tmp_unit[0] = CONTEXT_XSIZE_UNIT; + tmp_unit[1] = CONTEXT_YSIZE_UNIT; i++; } /* second run: if no unit is specified, proceed to next command * and interpret the expression as being relative to the default size */ } else { - tmp_float[1] *= CONTEXT_YSIZE_VALUE; - tmp_unit[1] = CONTEXT_YSIZE_UNIT; + tmp_double[1] *= CONTEXT_YSIZE_VALUE; + tmp_unit[1] = CONTEXT_YSIZE_UNIT; } break; } } - CONTEXT_params.xsize.value = tmp_float[0]; - CONTEXT_params.ysize.value = tmp_float[1]; + CONTEXT_params.xsize.value = tmp_double[0]; + CONTEXT_params.ysize.value = tmp_double[1]; CONTEXT_params.xsize.unit = tmp_unit[0]; CONTEXT_params.ysize.unit = tmp_unit[1]; break; @@ -1554,7 +1554,7 @@ CONTEXT_fontstring_parse(char *from_string, char *to_string, int to_size, dimens * we will only store the latest valid dimension * and ignore any other fields starting with a number and not containing a valid dimension */ dimension_t tmp_dimension; - float size; + double size; /* positions in input string between two commas (or start/stop) */ int from_start=0, from_stop=0; From c68d1eba67ddf0c443bba563fd2fc13adf7d73a4 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 3 Sep 2011 14:00:39 +0200 Subject: [PATCH 34/54] add %lf for double in scanf --- term/context.trm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/term/context.trm b/term/context.trm index c9049a79a..77a150c3a 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1587,7 +1587,7 @@ CONTEXT_fontstring_parse(char *from_string, char *to_string, int to_size, dimens tmp_fontsize[i] = 0; /* parse the value & unit */ unit[0] = 0; /* if no unit is given, we should get an empty string */ - sscanf(tmp_fontsize, "%f%s", &size, unit); + sscanf(tmp_fontsize, "%lf%s", &size, unit); tmp_dimension.value = size; /* if no unit given - default to pt */ if (unit == NULL || strlen(unit)==0) { From 96166f60416f8cff56768319ead055fc560f59af Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 4 Sep 2011 10:55:03 +0200 Subject: [PATCH 35/54] reset now uses defaults instead of duplicating code --- term/context.trm | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/term/context.trm b/term/context.trm index 77a150c3a..c1425a851 100644 --- a/term/context.trm +++ b/term/context.trm @@ -507,38 +507,19 @@ static struct gen_table CONTEXT_opts[] = { * CONTEXT_options* * **************** * - * The routine resets all the parameters of the terminal to their default - * value; the routine should be better implemented if it would use the values - * from "#define CONTEXT_PARAMS_DEFAULT", but I don't know how to do this; - * There must be a cleaner way, I'm sure. I double-checked that the values here - * and there agree with each other. + * Resets all parameters of the terminal to their default value. */ static void CONTEXT_params_reset() { - /* default size is 5x3 inches */ - CONTEXT_params.xsize.value = CONTEXT_XSIZE_VALUE; - CONTEXT_params.ysize.value = CONTEXT_YSIZE_VALUE; - CONTEXT_params.xsize.unit = CONTEXT_XSIZE_UNIT; - CONTEXT_params.ysize.unit = CONTEXT_YSIZE_UNIT; - - CONTEXT_params.standalone = FALSE; - CONTEXT_params.timestamp = TRUE; + const CONTEXT_params_t CONTEXT_params_default = CONTEXT_PARAMS_DEFAULT; + + /* free the memory with header first */ if (CONTEXT_params.header) { free(CONTEXT_params.header); - CONTEXT_params.header = NULL; } - CONTEXT_params.color = TRUE; - CONTEXT_params.dashed = TRUE; - CONTEXT_params.linejoin = LINEJOIN_MITER; - CONTEXT_params.linecap = LINECAP_BUTT; - CONTEXT_params.scale_dashlength = 1.0; - CONTEXT_params.scale_linewidth = 1.0; - CONTEXT_params.scale_text = 1.0; - CONTEXT_params.points = CONTEXT_POINTS_WITH_METAPOST; - CONTEXT_params.font[0] = 0; - CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; - CONTEXT_params.fontsize.unit = UNIT_PT; + + memcpy(&CONTEXT_params, &CONTEXT_params_default, sizeof(CONTEXT_params_t)); } From d92914c2c446b2199002fca7f15016fd30fed246 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 4 Sep 2011 13:25:32 +0200 Subject: [PATCH 36/54] make default parameters a static variable --- term/context.trm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/term/context.trm b/term/context.trm index c1425a851..856b3c997 100644 --- a/term/context.trm +++ b/term/context.trm @@ -512,7 +512,7 @@ static struct gen_table CONTEXT_opts[] = { static void CONTEXT_params_reset() { - const CONTEXT_params_t CONTEXT_params_default = CONTEXT_PARAMS_DEFAULT; + static const CONTEXT_params_t CONTEXT_params_default = CONTEXT_PARAMS_DEFAULT; /* free the memory with header first */ if (CONTEXT_params.header) { From 77ad3824184eb632b8e8e20cbf8f73bbdc32507d Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 4 Sep 2011 14:41:03 +0200 Subject: [PATCH 37/54] change time format (hopefully that is compatible enough) --- term/context.trm | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/term/context.trm b/term/context.trm index 856b3c997..169fb9645 100644 --- a/term/context.trm +++ b/term/context.trm @@ -997,7 +997,9 @@ TERM_PUBLIC void CONTEXT_init() { time_t now; - char *timedate; + char timebuffer[100]; + + time(&now); CONTEXT_posx = CONTEXT_posy = 0; CONTEXT_path_count = 0; @@ -1005,18 +1007,11 @@ CONTEXT_init() CONTEXT_counter=0; - /* - * XXX: Timoth\'ee warned me that this is not safe to use (portable); - * but PostScript terminal uses the same set of commands. - * Is there a way to write the timestamp in a compatible way? - */ - time(&now); - timedate=asctime(localtime(&now)); - timedate[strlen(timedate)-1]='\0'; - fprintf(gpoutfile, "%% Written by ConTeXt terminal for GNUPLOT"); if(CONTEXT_params.timestamp) { - fprintf(gpoutfile, " on: %s", asctime(localtime(&now))); + if(strftime(timebuffer, 100, "%Y-%m-%d %H:%M %Z", localtime(&now)) != 0) { + fprintf(gpoutfile, " on: %s", timebuffer); + } } fprintf(gpoutfile, "\n"); fprintf(gpoutfile, "%% GNUPLOT version %s patchlevel %s\n", gnuplot_version, gnuplot_patchlevel); From e15716e6e4feed891ee2ec2632ab3aaed57158a4 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 4 Sep 2011 16:31:19 +0200 Subject: [PATCH 38/54] minor comments --- term/context.trm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/term/context.trm b/term/context.trm index 169fb9645..a1b462341 100644 --- a/term/context.trm +++ b/term/context.trm @@ -99,10 +99,10 @@ * Future plans: * - add missing functionality: * - improved support for palettes - * - support for binary images - * - smooth shading in color bars * - other color spaces - * - gouraud shading (once implemented in 4.3) + * - enable support for binary images + * - smooth shading in color bars + * - gouraud shading (if it will be implemented in gnuplot) * - derive a better metapost terminal out of this one (to replace the old one) */ @@ -141,7 +141,7 @@ TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint /* Metapost can only deal with numbers between 2^{-16} and 2^12=4069 */ /* the values will be set later */ -/* scale is 1pt = 100 units */ +/* scale is 1bp = 100 units, resolution is 2400 'big points per inch' */ #define CONTEXT_DPI (2400) /* default plot size will be 5in x 3in*/ From 42cf609020a2aa26799313f7c07cb1dcff5bddfb Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 10 Sep 2011 23:08:21 +0200 Subject: [PATCH 39/54] tic sizes in other related terminals (comments only) --- term/context.trm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/term/context.trm b/term/context.trm index a1b462341..95ce3b185 100644 --- a/term/context.trm +++ b/term/context.trm @@ -161,8 +161,10 @@ TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint #define CONTEXT_LM_H_TO_V_RATIO 0.4895 #define CONTEXT_HCHAR (CONTEXT_LM_H_TO_V_RATIO*CONTEXT_VCHAR) /* default tic size: 3.5bp (chosen to suit the size of plot approximately) - * - in LaTeXt it's 5bp - * - in PostScript it's 3.15pt; + * - in LaTeX it is 5bp + * - in TikZ it is 0.18cm (approximately 5.1bp) + * - in PostScript it is 3.15pt + * - MetaPost uses 5pt or 5bp */ #define CONTEXT_HTIC (3.5*CONTEXT_DPI/72) #define CONTEXT_VTIC CONTEXT_HTIC From a1eccc761a2c9667b86df3ae78936f8802b449b7 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 10 Sep 2011 23:08:55 +0200 Subject: [PATCH 40/54] remove redundant help keys --- term/context.trm | 2 -- 1 file changed, 2 deletions(-) diff --git a/term/context.trm b/term/context.trm index 95ce3b185..ab6e15c6b 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2242,9 +2242,7 @@ START_HELP(context) "1 ConTeXt", "?commands set terminal context", "?set terminal context", -"?set term context", "?terminal context", -"?term context", "?context", " ConTeXt is a macro package for TeX, highly integrated with Metapost", " (for drawing figures) and intended for creation of high-quality PDF documents.", From b00303a5194e1d672f00c261fd7aa9b0d7a93776 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 11 Sep 2011 12:32:50 +0200 Subject: [PATCH 41/54] add whitespace to help --- term/context.trm | 91 +++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/term/context.trm b/term/context.trm index ab6e15c6b..7b73895d0 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2256,22 +2256,22 @@ START_HELP(context) " The `context` terminal supports the following options:", " ", " Syntax:", -" set term context {default}", -" {defaultsize | size |", -" size {in|cm}, {in|cm}}", -" {input | standalone}", -" {timestamp | notimestamp}", -" {noheader | header \"
\"}", -" {color | colour | monochrome}", -" {mitered | rounded | beveled}", -" {butt | round | squared}", -" {dashed | solid}", -" {dashlength | dl
}", -" {linewidth | lw }", -" {fontscale }", -" {mppoints | texpoints}", -" {defaultfont | font {} |", -" font \"{,}\" {fontsize}}", +" set term context {default}", +" {defaultsize | size |", +" size {in|cm}, {in|cm}}", +" {input | standalone}", +" {timestamp | notimestamp}", +" {noheader | header \"
\"}", +" {color | colour | monochrome}", +" {mitered | rounded | beveled}", +" {butt | round | squared}", +" {dashed | solid}", +" {dashlength | dl
}", +" {linewidth | lw }", +" {fontscale }", +" {mppoints | texpoints}", +" {defaultfont | font {} |", +" font \"{,}\" {fontsize}}", "", " In non-standalone (`input`) graphic only parameters `size` to select graphic", " size, `fontscale` to scale all the labels for a factor ", @@ -2323,19 +2323,19 @@ START_HELP(context) " `mppoints` uses predefined point shapes, drawn in Metapost.", " `texpoints` uses easily configurable set of symbols, defined with ConTeXt", " in the following way:", -" \\defineconversion[my own points][+,{\\ss x},\\mathematics{\\circ}]", -" \\setupGNUPLOTterminal[context][points=tex,pointset=my own points]", +" \\defineconversion[my own points][+,{\\ss x},\\mathematics{\\circ}]", +" \\setupGNUPLOTterminal[context][points=tex,pointset=my own points]", "", " With `font` you can set font name and size in standalone graphics.", " In non-standalone (`input`) mode only the font size is important", " to reserve enough space for text labels.", " The command" -" set term context font \"myfont,ss\" 10pt", +" set term context font \"myfont,ss\" 10pt", " will result in", -" \\setupbodyfont[myfont,ss,10pt]", +" \\setupbodyfont[myfont,ss,10pt]", " If you additionaly set `fontscale` to 0.8 for example,", " then the resulting font will be 8pt big and", -" set label ... font \"myfont,12pt\"", +" set label ... font \"myfont,12pt\"", " will come out as 9.6pt.\n", " For compatibility with other terminals font size may be specified without units,", " in which case the default unit \"pt\" will be appended to it, but you can also", @@ -2343,39 +2343,44 @@ START_HELP(context) " It is your own responsibility to provide proper typescripts (and header),", " otherwise switching the font will have no effect.\n", " For a standard font in ConTeXt MKII (pdfTeX) you could use:", -" set terminal context standalone header '\\usetypescript[iwona][ec]' \\", -" font \"iwona,ss,11pt\"", +" set terminal context standalone header '\\usetypescript[iwona][ec]' \\", +" font \"iwona,ss,11pt\"", " Please take a look into ConTeXt documentation, wiki or mailing list (archives)", " for any up-to-date information about font usage." "", " Examples:", -" set terminal context size 1.5 # 7.5in, 4.5in", -" set terminal context size 10cm, 5cm # 10cm, 5cm", -" set terminal context size 4/5, 2.54*4cm # 4in, 4in", +" set terminal context size 1.5 # 7.5in, 4.5in", +" set terminal context size 10cm, 5cm # 10cm, 5cm", +" set terminal context size 4/5, 2.54*4cm # 4in, 4in", " For standalone (whole-page) plots with labels in UTF-8 encoding:", -" set terminal context standalone header '\\enableregime[utf-8]'", +" set terminal context standalone header '\\enableregime[utf-8]'", "", /* TODO: LaTeX formatting */ "2 Requirements", -" Requirements:", +" You need gnuplot module for ConTeXt", +"^ ", +" http://ctan.org/pkg/context-gnuplot", +"^ ", +" and a recent version of ConTeXt.", +" If you want to call gnuplot on-the-fly, you also need write18 enabled.", +" In most TeX distributionts this can be set with shell_escape=t in texmf.cnf.", "", -" - gnuplot module for ConTeXt and a recent version of ConTeXt", -" - If you want to call gnuplot on-the-fly, you need to have write18 enabled.", -" (In most TeX distributionts this can be set with shell_escape=t in texmf.cnf.)", -"", -" See http://wiki.contextgarden.net/Gnuplot for details about this terminal", -" and for more exhaustive help & examples.", +" See", +"^ ", +" http://wiki.contextgarden.net/Gnuplot", +"^ ", +" for details about this terminal and for more exhaustive help & examples.", "", "2 Calling gnuplot from ConTeXt", " The easiest way to make plots in ConTeXt documents is", -" \\usemodule[gnuplot]", -" \\starttext", -" \\title{How to draw nice plots with {\\sc gnuplot}?}", -" \\startGNUPLOTscript[sin]", -" set format y \"%.1f\"", -" plot sin(x) t '$\\sin(x)$'", -" \\stopGNUPLOTscript", -" \\useGNUPLOTgraphic[sin]", -" \\stoptext", +" \\usemodule[gnuplot]", +" \\starttext", +" \\title{How to draw nice plots with {\\sc gnuplot}?}", +" \\startGNUPLOTscript[sin]", +" set format y \"%.1f\"", +" plot sin(x) t '$\\sin(x)$'", +" \\stopGNUPLOTscript", +" \\useGNUPLOTgraphic[sin]", +" \\stoptext", " This will run gnuplot automatically and include the resulting figure in the document." END_HELP(context) #endif /* TERM_HELP */ From ba9ec4ee19252d9f03143515260b0a49e74dcf21 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sun, 11 Sep 2011 15:26:23 +0200 Subject: [PATCH 42/54] use low level commands for images & add proper clipping --- term/context.trm | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/term/context.trm b/term/context.trm index 7b73895d0..692ff0fd6 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2124,6 +2124,7 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag int i, j, k, line_length, components_per_color; unsigned char byte; rgb_color color; + TBOOLEAN is_clipped = FALSE; /* Palette colors have to be converted into RGB first */ if (color_mode == IC_PALETTE) { @@ -2177,18 +2178,28 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag } } - /* TODO: clipping and transparency handling is not yet completely ready */ - /* (width, height) in pixels; - * (x,y) lower left - * (x,y) upper right - */ + /* TODO: transparency handling is not yet supported in ConTeXt */ + if((corner[2].x > corner[0].x) || (corner[0].y > corner[2].y) || + (corner[1].x > corner[3].x) || (corner[3].y > corner[1].y)) { + is_clipped = TRUE; + } + if(is_clipped) { + fprintf(gpoutfile, "draw image(\n "); + } + fprintf(gpoutfile, "draw bitmapimage (%u,%u,img) xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", + N, M, corner[1].x/100.0-corner[0].x/100.0, corner[0].y/100.0-corner[1].y/100.0, corner[0].x/100.0, corner[1].y/100.0); + if(is_clipped) { + fprintf(gpoutfile, " clip currentpicture to unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb););\n", + corner[3].x/100.0-corner[2].x/100.0, corner[2].y/100.0-corner[3].y/100.0, corner[2].x/100.0, corner[3].y/100.0); + } + /* (alternative implementation) * + fprintf(gpoutfile, "gp_image_rgb"); if (color_mode == IC_RGB) fprintf(gpoutfile, "_alpha"); fprintf(gpoutfile, "((%u,%u),(%.2fa,%.2fb),(%.2fa,%.2fb));\n", N, M, corner[0].x/100.0, corner[1].y/100.0, corner[1].x/100.0, corner[0].y/100.0); - fprintf(gpoutfile, "%% I also want to clip to ((%.2fa,%.2fb), (%.2fa,%.2fb))\n", - corner[2].x/100.0, corner[3].y/100.0, corner[3].x/100.0, corner[2].y/100.0); + */ } /* @@ -2226,7 +2237,7 @@ TERM_TABLE_START(context_driver) 0, /* XXX: CONTEXT_previous_palette: not sure if we need it at all (PS does "grestore") */ CONTEXT_set_color, CONTEXT_filled_polygon, - 0, /* TODO: CONTEXT_image */ + CONTEXT_image, 0, 0, 0, /* No enhanced text mode because this is TeX */ 0, /* layer (used to signal front/back text, used in canvas, PS, epslatex, ...); XXX: figure out what this has to do */ 0 /* path (Path control for end-joins of closed polygons on PostScript-like devices); TODO: implement it (CONTEXT_path) */ From 26844e501d476b481909670096bd3a844ed382fa Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Wed, 14 Sep 2011 08:22:21 +0200 Subject: [PATCH 43/54] add terminal version --- term/context.trm | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/term/context.trm b/term/context.trm index 692ff0fd6..0b6559350 100644 --- a/term/context.trm +++ b/term/context.trm @@ -175,6 +175,17 @@ TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint #ifndef TERM_PROTO_ONLY #ifdef TERM_BODY +/* + * we will start counting when the code is included into CVS + * + * major number should change only in case of a complete rewrite or major incompatibility + * minor number should change for every new functionality + * patch number & date should change in every commit + */ +static const char CONTEXT_term_version[] = "1.0"; +static const char CONTEXT_term_patch[] = "0"; +static const char CONTEXT_term_date[] = "2011-09-14"; + /********* * UNITS * ********* @@ -1016,7 +1027,8 @@ CONTEXT_init() } } fprintf(gpoutfile, "\n"); - fprintf(gpoutfile, "%% GNUPLOT version %s patchlevel %s\n", gnuplot_version, gnuplot_patchlevel); + fprintf(gpoutfile, "%% GNUPLOT version: %s.%s, terminal version: %s.%s (%s)\n", + gnuplot_version, gnuplot_patchlevel, CONTEXT_term_version, CONTEXT_term_patch, CONTEXT_term_date); fprintf(gpoutfile, "%% See also http://wiki.contextgarden.net/Gnuplot\n%%\n"); /* place the header first if this is a standalone graphic */ @@ -1188,7 +1200,8 @@ CONTEXT_graphics() fprintf(gpoutfile, "\\startGNUPLOTgraphic[%d]\n", ++CONTEXT_counter); } - fprintf(gpoutfile, "gnuplotversion := \"%s\";\n", gnuplot_version); + fprintf(gpoutfile, "gnuplotversion := \"%s\"; termversion := \"%s\";\n", + gnuplot_version, CONTEXT_term_version); /* * MetaPost can only handle numbers up to 4096. Too high resolution * would thus result in number overflow, that's why we scale down all the @@ -1200,7 +1213,7 @@ CONTEXT_graphics() CONTEXT_params.ysize.value, unit_name(CONTEXT_params.ysize.unit)); fprintf(gpoutfile, "%% horizontal and vertical scaling factors\na:=w/%.2f; b:=h/%.2f;\n", (term->xmax)/100.0, (term->ymax)/100.0); - fprintf(gpoutfile, "%% temporary variable for storing the path and images\nsave p, img, ima; path p; string img, ima;\n%%\n"); + fprintf(gpoutfile, "%% temporary variable for storing the path and images\nsave p, img, ima; path p; string img, ima;\n"); fprintf(gpoutfile, "%% -------------------------\n"); fprintf(gpoutfile, "%% Different initialisations\n"); fprintf(gpoutfile, "%% -------------------------\n"); @@ -2186,7 +2199,7 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag if(is_clipped) { fprintf(gpoutfile, "draw image(\n "); } - fprintf(gpoutfile, "draw bitmapimage (%u,%u,img) xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", + fprintf(gpoutfile, "draw bitmapimage (%u,%u,img) xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", N, M, corner[1].x/100.0-corner[0].x/100.0, corner[0].y/100.0-corner[1].y/100.0, corner[0].x/100.0, corner[1].y/100.0); if(is_clipped) { fprintf(gpoutfile, " clip currentpicture to unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb););\n", From 9e10cb25be03806926d884c0279b82c7de4ee98a Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Wed, 14 Sep 2011 08:28:37 +0200 Subject: [PATCH 44/54] downcase DL/LW --- term/context.trm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/term/context.trm b/term/context.trm index 0b6559350..e9a79b195 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2290,8 +2290,8 @@ START_HELP(context) " {mitered | rounded | beveled}", " {butt | round | squared}", " {dashed | solid}", -" {dashlength | dl
}", -" {linewidth | lw }", +" {dashlength | dl
}", +" {linewidth | lw }", " {fontscale }", " {mppoints | texpoints}", " {defaultfont | font {} |", @@ -2339,8 +2339,8 @@ START_HELP(context) " `dashed` (default) uses different dash patterns for different line types," " `solid` draws all plots with solid lines.", "", -" `dashlength` or `dl` scales the length of the dashed-line segments by
.", -" `linewidth` or `lw` scales all linewidths by ." +" `dashlength` or `dl` scales the length of the dashed-line segments by
.", +" `linewidth` or `lw` scales all linewidths by ." " (lw 1 stands for 0.5bp, which is the default line width when drawing with Metapost.)", " `fontscale` scales text labels for factor relative to default document font.", "", From b41146d56fcb1637a473b2072dc4e85e15316ddd Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Wed, 14 Sep 2011 08:32:28 +0200 Subject: [PATCH 45/54] change round/rounded vs butt/mitered which is now the default --- term/context.trm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/term/context.trm b/term/context.trm index e9a79b195..748c9a20e 100644 --- a/term/context.trm +++ b/term/context.trm @@ -2287,8 +2287,8 @@ START_HELP(context) " {timestamp | notimestamp}", " {noheader | header \"
\"}", " {color | colour | monochrome}", -" {mitered | rounded | beveled}", -" {butt | round | squared}", +" {rounded | mitered | beveled}", +" {round | butt | squared}", " {dashed | solid}", " {dashlength | dl
}", " {linewidth | lw }", @@ -2328,11 +2328,11 @@ START_HELP(context) " If you have any good ideas how the behaviour should differ to suit the monochrome printers better,", " your suggestions are welcome.", "", -" `mitered` (default), `rounded` and `beveled` control the shape of line joins.", -" `butt` (default), `round` and `squared` control the shape of line caps.", +" `rounded` (default), `mitered` and `beveled` control the shape of line joins.", +" `round` (default), `butt` and `squared` control the shape of line caps.", " See PostScript or PDF Reference Manual for explanation. For wild-behaving functions", " and thick lines", -" you might want to use `rounded` and `round` to prevent sharp corners in line joins.", +" it is better to use `rounded` and `round` to prevent sharp corners in line joins.", " (Some general support for this should be added to Gnuplot, so that the same options", " could be set for each line (style) separately).", "", From f1e90ee160d18e2af341842bdaac9b2edab48ff9 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 16 Sep 2011 15:45:48 +0200 Subject: [PATCH 46/54] add support for writing external bitmap images --- term/context.trm | 263 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 193 insertions(+), 70 deletions(-) diff --git a/term/context.trm b/term/context.trm index 748c9a20e..ae0a8dda4 100644 --- a/term/context.trm +++ b/term/context.trm @@ -358,7 +358,6 @@ static void CONTEXT_endpath __PROTO((void)); static void CONTEXT_write_palette __PROTO((t_sm_palette *palette)); static void CONTEXT_write_palette_gradient __PROTO((gradient_struct *gradient, int cnt)); - /* Each number is divided by 100 (1/100th of a point is drawn) */ static int CONTEXT_posx; @@ -392,9 +391,6 @@ static int CONTEXT_old_linetype = -3; /* was the color changed explicitly? */ static TBOOLEAN CONTEXT_color_changed = FALSE; -/* counts the number of graphics */ -static int CONTEXT_counter = 0; - /* the number of path nodes before a newline (doesn't really matter, * could be set to 1000; check if any editors have problems with that) */ #define CONTEXT_LINEMAX 4 @@ -413,6 +409,21 @@ static enum JUSTIFY CONTEXT_justify = LEFT; enum LINEJOIN { LINEJOIN_MITER, LINEJOIN_ROUND, LINEJOIN_BEVEL }; enum LINECAP { LINECAP_BUTT, LINECAP_ROUND, LINECAP_SQUARE }; +/* whether points are drawn with metapost or typeset with TeX (easy configurable) */ +enum CONTEXT_POINTS { CONTEXT_POINTS_WITH_METAPOST, CONTEXT_POINTS_WITH_TEX }; +/* whether images are inline or written out as PNGs and included (in MKII only external work) */ +enum CONTEXT_IMAGES { CONTEXT_IMAGES_INLINE, CONTEXT_IMAGES_EXTERNAL }; + +/* counts the number of graphics */ +static int CONTEXT_counter = 0; +/* counts the number of external PNG images */ +static int CONTEXT_image_counter = 0; +static char *CONTEXT_image_filename = NULL; +static int CONTEXT_image_filename_length = 0; +/* whether images will be external or not */ +enum CONTEXT_IMAGES CONTEXT_images = CONTEXT_IMAGES_INLINE; +static char CONTEXT_image_name[MAX_ID_LEN+1] = ""; + /* Pallete has to be stored for the usage in later plots */ static t_sm_palette *CONTEXT_old_palette; @@ -420,9 +431,6 @@ static t_sm_palette *CONTEXT_old_palette; /* global parameters */ /*********************/ -/* whether points are drawn with metapost or typeset with TeX (easy configurable) */ -enum CONTEXT_POINTS { CONTEXT_POINTS_WITH_METAPOST, CONTEXT_POINTS_WITH_TEX }; - typedef struct CONTEXT_params_t { dimension_t xsize; /* 5in */ dimension_t ysize; /* 3in */ @@ -437,6 +445,7 @@ typedef struct CONTEXT_params_t { double scale_linewidth; /* 1.0 */ double scale_text; /* 1.0 */ enum CONTEXT_POINTS points; /* CONTEXT_POINTS_WITH_METAPOST */ + enum CONTEXT_IMAGES images; /* CONTEXT_IMAGES_INLINE */ char font[MAX_ID_LEN+1]; /* "" */ dimension_t fontsize; /* 12pt */ } CONTEXT_params_t; @@ -456,6 +465,7 @@ typedef struct CONTEXT_params_t { 1.0,\ 1.0,\ CONTEXT_POINTS_WITH_METAPOST,\ + CONTEXT_IMAGES_INLINE,\ "",\ {CONTEXT_FONTSIZE, UNIT_PT}\ } @@ -475,6 +485,7 @@ enum CONTEXT_id { CONTEXT_OPT_LINECAP_BUTT, CONTEXT_OPT_LINECAP_ROUNDED, CONTEXT_OPT_LINECAP_SQUARED, CONTEXT_OPT_SCALE_DASHLENGTH, CONTEXT_OPT_SCALE_LINEWIDTH, CONTEXT_OPT_SCALE_TEXT, CONTEXT_OPT_POINTS_WITH_METAPOST, CONTEXT_OPT_POINTS_WITH_TEX, + CONTEXT_OPT_IMAGES_INLINE, CONTEXT_OPT_IMAGES_EXTERNAL, CONTEXT_OPT_DEFAULTFONT, CONTEXT_OPT_FONT, CONTEXT_OPT_OTHER @@ -511,6 +522,8 @@ static struct gen_table CONTEXT_opts[] = { { "pointswithmp", CONTEXT_OPT_POINTS_WITH_METAPOST}, /* (removable) backward compatibility */ { "pointswithmetapost", CONTEXT_OPT_POINTS_WITH_METAPOST}, /* (removable) backward compatibility */ { "pointswithtex", CONTEXT_OPT_POINTS_WITH_TEX}, /* (removable) backward compatibility */ + { "inline$images", CONTEXT_OPT_IMAGES_INLINE}, + { "external$images", CONTEXT_OPT_IMAGES_EXTERNAL}, { "font", CONTEXT_OPT_FONT }, { "defaultfont", CONTEXT_OPT_DEFAULTFONT }, { NULL, CONTEXT_OPT_OTHER } @@ -530,6 +543,7 @@ CONTEXT_params_reset() /* free the memory with header first */ if (CONTEXT_params.header) { free(CONTEXT_params.header); + CONTEXT_params.header = NULL; } memcpy(&CONTEXT_params, &CONTEXT_params_default, sizeof(CONTEXT_params_t)); @@ -762,6 +776,18 @@ CONTEXT_options() c_token++; CONTEXT_params.points = CONTEXT_POINTS_WITH_TEX; break; + case CONTEXT_OPT_IMAGES_INLINE: + c_token++; + CONTEXT_params.images = CONTEXT_IMAGES_INLINE; + break; + case CONTEXT_OPT_IMAGES_EXTERNAL: +#ifdef WRITE_PNG_IMAGE + c_token++; + CONTEXT_params.images = CONTEXT_IMAGES_EXTERNAL; +#else + int_warn(c_token, "Gnuplot was built without support for PNG images. You cannot use this option unless you rebuild gnuplot."); +#endif + break; /* * The preferred way to set the font is to set it in a document itself, * labels in gnuplot in graphs will then inherit that font. @@ -970,13 +996,23 @@ CONTEXT_options() strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); switch (CONTEXT_params.points) { - case CONTEXT_POINTS_WITH_TEX : + case CONTEXT_POINTS_WITH_TEX : strncat(term_options, "texpoints ", sizeof(term_options)-strlen(term_options)); break; case CONTEXT_POINTS_WITH_METAPOST : strncat(term_options, "mppoints ", sizeof(term_options)-strlen(term_options)); break; } +#ifdef WRITE_PNG_IMAGE + switch (CONTEXT_params.images) { + case CONTEXT_IMAGES_INLINE : + strncat(term_options, "inlineimages ", + sizeof(term_options)-strlen(term_options)); break; + case CONTEXT_IMAGES_EXTERNAL : + strncat(term_options, "externalimages ", + sizeof(term_options)-strlen(term_options)); break; + } +#endif strncat(term_options,"font \"", sizeof(term_options)-strlen(term_options)); strncat(term_options,CONTEXT_params.font, sizeof(term_options)-strlen(term_options)); strncat(term_options,"\" ", sizeof(term_options)-strlen(term_options)); @@ -1011,6 +1047,7 @@ CONTEXT_init() { time_t now; char timebuffer[100]; + int i; time(&now); @@ -1018,7 +1055,48 @@ CONTEXT_init() CONTEXT_path_count = 0; CONTEXT_path_is_dot = 0; - CONTEXT_counter=0; + CONTEXT_counter = 0; + + /* setup bitmap images */ + CONTEXT_image_counter = 0; + /* the default is to use inline images */ + CONTEXT_images = CONTEXT_IMAGES_INLINE; + + /* only if external images are both requested and supported, we switch to them (double paranoia) */ + /* delete the stored filename first */ + if(CONTEXT_image_filename) { + free(CONTEXT_image_filename); + CONTEXT_image_filename = NULL; + CONTEXT_image_filename_length = 0; + }; +#ifdef WRITE_PNG_IMAGE + if(CONTEXT_params.images == CONTEXT_IMAGES_EXTERNAL) { + CONTEXT_images = CONTEXT_IMAGES_EXTERNAL; + + /* but only if 'set output' was set because we use that string as base for image names */ + if(outstr) { + CONTEXT_image_filename_length = strlen(outstr); + /* we will cut off the last .tex ending if present */ + /* find the last dot if present */ + for(i=CONTEXT_image_filename_length-1; i>=0 && outstr[i] != '.'; i--); + if(outstr[i] == '.') { + CONTEXT_image_filename_length = i; + } + /* it would also be very nice to do some sanity checks on filenames */ + + /* .xx.png; must be at least 7 characters long */ + CONTEXT_image_filename = (char *)gp_alloc(CONTEXT_image_filename_length + 10, "ConTeXt image filename"); + strncpy(CONTEXT_image_filename, outstr, CONTEXT_image_filename_length); + CONTEXT_image_filename[CONTEXT_image_filename_length] = 0; + } else { + CONTEXT_image_filename_length = strlen("gp_image"); + /* .xx.png; must be at least 7 characters long */ + CONTEXT_image_filename = (char *)gp_alloc(CONTEXT_image_filename_length + 10, "ConTeXt image filename"); + strncpy(CONTEXT_image_filename, "gp_image", CONTEXT_image_filename_length); + CONTEXT_image_filename[CONTEXT_image_filename_length] = 0; + } + } +#endif fprintf(gpoutfile, "%% Written by ConTeXt terminal for GNUPLOT"); if(CONTEXT_params.timestamp) { @@ -1121,9 +1199,14 @@ CONTEXT_init() /* points (gp_points_with): metapost/tex (gp_points_with_metapost/gp_points_with_tex) * default: metapost */ - fprintf(gpoutfile, " points=%s] %% *metapost* | tex (Should points be drawn with MetaPost or TeX?)\n", + fprintf(gpoutfile, " points=%s, %% *metapost* | tex (Should points be drawn with MetaPost or TeX?)\n", CONTEXT_params.points == CONTEXT_POINTS_WITH_METAPOST ? "metapost" : "tex"); + /* images + * default: inline */ + fprintf(gpoutfile, " images=%s] %% *inline* | external (inline only works in MKIV, external requires png support in gnuplot)\n", + CONTEXT_images == CONTEXT_IMAGES_INLINE ? "inline" : "external"); + /*----------------* * end of options * *----------------*/ @@ -1156,6 +1239,12 @@ CONTEXT_reset() */ fputs("\\endinput\n", gpoutfile); } + /* deallocate image name if present */ + if(CONTEXT_image_filename) { + free(CONTEXT_image_filename); + CONTEXT_image_filename = NULL; + CONTEXT_image_filename_length = 0; + }; } /* ************** @@ -1213,7 +1302,12 @@ CONTEXT_graphics() CONTEXT_params.ysize.value, unit_name(CONTEXT_params.ysize.unit)); fprintf(gpoutfile, "%% horizontal and vertical scaling factors\na:=w/%.2f; b:=h/%.2f;\n", (term->xmax)/100.0, (term->ymax)/100.0); - fprintf(gpoutfile, "%% temporary variable for storing the path and images\nsave p, img, ima; path p; string img, ima;\n"); + /* TODO: the following if-else could be slightly nicer */ + if(CONTEXT_images == CONTEXT_IMAGES_INLINE) { + fprintf(gpoutfile, "%% temporary variable for storing the path and images\nsave p, img, ima; path p; string img, ima;\n"); + } else { + fprintf(gpoutfile, "%% temporary variable for storing the path\nsave p; path p;\n"); + } fprintf(gpoutfile, "%% -------------------------\n"); fprintf(gpoutfile, "%% Different initialisations\n"); fprintf(gpoutfile, "%% -------------------------\n"); @@ -2139,80 +2233,100 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag rgb_color color; TBOOLEAN is_clipped = FALSE; - /* Palette colors have to be converted into RGB first */ - if (color_mode == IC_PALETTE) { - /* write the image data */ - fprintf(gpoutfile, "img := \"%%\n"); - line_length = 0; - for(i=0; i= 16) { - line_length = 1; - fprintf(gpoutfile, "%%\n"); - } - rgb1maxcolors_from_gray(image[i], &color); - fprintf(gpoutfile, "%02x%02x%02x", (unsigned char)(255*color.r), (unsigned char)(255*color.g), (unsigned char)(255*color.b)); - } - fprintf(gpoutfile, "\";\n"); + if((corner[2].x > corner[0].x) || (corner[0].y > corner[2].y) || + (corner[1].x > corner[3].x) || (corner[3].y > corner[1].y)) { + is_clipped = TRUE; + } - /* IC_RGB or IC_RGBA */ - } else { - if (color_mode == IC_RGBA) { - components_per_color = 4; - } else { /* IC_RGB */ - components_per_color = 3; + if(CONTEXT_images == CONTEXT_IMAGES_EXTERNAL) { +#ifdef WRITE_PNG_IMAGE + /* we reserved 10 extra bytes, so we can afford images up to 9999 */ + snprintf(CONTEXT_image_filename + CONTEXT_image_filename_length, 9, "_%02d.png", ++CONTEXT_image_counter); + write_png_image (M, N, image, color_mode, CONTEXT_image_filename); + + if(is_clipped) { + fprintf(gpoutfile, "draw image(\n "); } + fprintf(gpoutfile, "externalfigure \"%s\" xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", + CONTEXT_image_filename, corner[1].x/100.0-corner[0].x/100.0, corner[0].y/100.0-corner[1].y/100.0, corner[0].x/100.0, corner[1].y/100.0); + if(is_clipped) { + fprintf(gpoutfile, " clip currentpicture to unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb););\n", + corner[3].x/100.0-corner[2].x/100.0, corner[2].y/100.0-corner[3].y/100.0, corner[2].x/100.0, corner[3].y/100.0); + } +#endif + } else { - /* write the image data */ - fprintf(gpoutfile, "img := \"%%\n"); - line_length = 0; - for(i=0; i= 16) { - line_length = 1; - fprintf(gpoutfile, "%%\n"); + /* Palette colors have to be converted into RGB first */ + if (color_mode == IC_PALETTE) { + /* write the image data */ + fprintf(gpoutfile, "img := \"%%\n"); + line_length = 0; + for(i=0; i= 16) { + line_length = 1; + fprintf(gpoutfile, "%%\n"); + } + rgb1maxcolors_from_gray(image[i], &color); + fprintf(gpoutfile, "%02x%02x%02x", (unsigned char)(255*color.r), (unsigned char)(255*color.g), (unsigned char)(255*color.b)); } - for(k=0; k<3; k++) { - fprintf(gpoutfile, "%02x", (unsigned char)(image[i*components_per_color+k]*255)); + fprintf(gpoutfile, "\";\n"); + + /* IC_RGB or IC_RGBA */ + } else { + if (color_mode == IC_RGBA) { + components_per_color = 4; + } else { /* IC_RGB */ + components_per_color = 3; } - } - fprintf(gpoutfile, "\";\n"); - /* transparency mask */ - if (color_mode == IC_RGBA) { - fprintf(gpoutfile, "ima := \"%%\n"); + /* write the image data */ + fprintf(gpoutfile, "img := \"%%\n"); line_length = 0; for(i=0; i= 3*16) { + if(line_length++ >= 16) { line_length = 1; fprintf(gpoutfile, "%%\n"); - }; - fprintf(gpoutfile, "%02x", (unsigned char)(image[i*components_per_color+3]*255)); + } + for(k=0; k<3; k++) { + fprintf(gpoutfile, "%02x", (unsigned char)(image[i*components_per_color+k]*255)); + } } fprintf(gpoutfile, "\";\n"); + + /* transparency mask */ + if (color_mode == IC_RGBA) { + fprintf(gpoutfile, "ima := \"%%\n"); + line_length = 0; + for(i=0; i= 3*16) { + line_length = 1; + fprintf(gpoutfile, "%%\n"); + }; + fprintf(gpoutfile, "%02x", (unsigned char)(image[i*components_per_color+3]*255)); + } + fprintf(gpoutfile, "\";\n"); + } } - } - /* TODO: transparency handling is not yet supported in ConTeXt */ - if((corner[2].x > corner[0].x) || (corner[0].y > corner[2].y) || - (corner[1].x > corner[3].x) || (corner[3].y > corner[1].y)) { - is_clipped = TRUE; - } - if(is_clipped) { - fprintf(gpoutfile, "draw image(\n "); - } - fprintf(gpoutfile, "draw bitmapimage (%u,%u,img) xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", - N, M, corner[1].x/100.0-corner[0].x/100.0, corner[0].y/100.0-corner[1].y/100.0, corner[0].x/100.0, corner[1].y/100.0); - if(is_clipped) { - fprintf(gpoutfile, " clip currentpicture to unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb););\n", - corner[3].x/100.0-corner[2].x/100.0, corner[2].y/100.0-corner[3].y/100.0, corner[2].x/100.0, corner[3].y/100.0); - } - /* (alternative implementation) * + /* TODO: transparency handling is not yet supported in ConTeXt */ + if(is_clipped) { + fprintf(gpoutfile, "draw image(\n "); + } + fprintf(gpoutfile, "draw bitmapimage (%u,%u,img) xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", + N, M, corner[1].x/100.0-corner[0].x/100.0, corner[0].y/100.0-corner[1].y/100.0, corner[0].x/100.0, corner[1].y/100.0); + if(is_clipped) { + fprintf(gpoutfile, " clip currentpicture to unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb););\n", + corner[3].x/100.0-corner[2].x/100.0, corner[2].y/100.0-corner[3].y/100.0, corner[2].x/100.0, corner[3].y/100.0); + } - fprintf(gpoutfile, "gp_image_rgb"); - if (color_mode == IC_RGB) - fprintf(gpoutfile, "_alpha"); - fprintf(gpoutfile, "((%u,%u),(%.2fa,%.2fb),(%.2fa,%.2fb));\n", - N, M, corner[0].x/100.0, corner[1].y/100.0, corner[1].x/100.0, corner[0].y/100.0); - */ + /* (alternative implementation) * + fprintf(gpoutfile, "gp_image_rgb"); + if (color_mode == IC_RGB) + fprintf(gpoutfile, "_alpha"); + fprintf(gpoutfile, "((%u,%u),(%.2fa,%.2fb),(%.2fa,%.2fb));\n", + N, M, corner[0].x/100.0, corner[1].y/100.0, corner[1].x/100.0, corner[0].y/100.0); + */ + } } /* @@ -2294,6 +2408,9 @@ START_HELP(context) " {linewidth | lw }", " {fontscale }", " {mppoints | texpoints}", +#ifdef WRITE_PNG_IMAGE +" {inlineimages | externalimages}", +#endif " {defaultfont | font {} |", " font \"{,}\" {fontsize}}", "", @@ -2350,6 +2467,12 @@ START_HELP(context) " \\defineconversion[my own points][+,{\\ss x},\\mathematics{\\circ}]", " \\setupGNUPLOTterminal[context][points=tex,pointset=my own points]", "", +#ifdef WRITE_PNG_IMAGE +" `inlineimages` writes binary images to a string and only works in ConTeXt MKIV.", +" `externalimages` writes PNG files to disk and also works with ConTeXt MKII.", +" Gnuplot needs to have support for PNG images built in for this to work." +"", +#endif " With `font` you can set font name and size in standalone graphics.", " In non-standalone (`input`) mode only the font size is important", " to reserve enough space for text labels.", From 8a5f8f164a19f3fb9bb1d9519956ed76306f0270 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 16 Sep 2011 16:27:17 +0200 Subject: [PATCH 47/54] declare gnuplot termversion to be a string (it would be enough to do it in terminal, but this should enable older support files to work properly) --- term/context.trm | 1 + 1 file changed, 1 insertion(+) diff --git a/term/context.trm b/term/context.trm index ae0a8dda4..86fe7ecde 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1289,6 +1289,7 @@ CONTEXT_graphics() fprintf(gpoutfile, "\\startGNUPLOTgraphic[%d]\n", ++CONTEXT_counter); } + fprintf(gpoutfile, "string gnuplotversion, termversion;\n"); fprintf(gpoutfile, "gnuplotversion := \"%s\"; termversion := \"%s\";\n", gnuplot_version, CONTEXT_term_version); /* From 7813310065d3b8f45456c9b4c3b7c9e9f5325179 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Fri, 16 Sep 2011 17:50:00 +0200 Subject: [PATCH 48/54] switch from 2400 dpi to 1000 units per cm and only keep a single scaling factor --- term/context.trm | 60 +++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/term/context.trm b/term/context.trm index 86fe7ecde..6b7b7cf6f 100644 --- a/term/context.trm +++ b/term/context.trm @@ -141,9 +141,11 @@ TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint /* Metapost can only deal with numbers between 2^{-16} and 2^12=4069 */ /* the values will be set later */ -/* scale is 1bp = 100 units, resolution is 2400 'big points per inch' */ +/* scale is 1cm = 1000 units */ -#define CONTEXT_DPI (2400) +// #define CONTEXT_DPI (2400) +#define CONTEXT_DPCM 1000 +#define CONTEXT_DPI (2.54*CONTEXT_DPCM) /* default plot size will be 5in x 3in*/ #define CONTEXT_XSIZE_VALUE 5 #define CONTEXT_YSIZE_VALUE 3 @@ -184,7 +186,7 @@ TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint */ static const char CONTEXT_term_version[] = "1.0"; static const char CONTEXT_term_patch[] = "0"; -static const char CONTEXT_term_date[] = "2011-09-14"; +static const char CONTEXT_term_date[] = "2011-09-16"; /********* * UNITS * @@ -1295,14 +1297,14 @@ CONTEXT_graphics() /* * MetaPost can only handle numbers up to 4096. Too high resolution * would thus result in number overflow, that's why we scale down all the - * integers from gnuplot by 100 and multiply those numbers later by - * appropriate scaling factors 'a' and 'b' to get the proper dimensions. + * integers from gnuplot by 1000 and multiply those numbers later by + * appropriate scaling factor 'a' to get the proper dimensions. */ - fprintf(gpoutfile, "%% width and height of the figure\nw:=%g%s; h:=%g%s;\n", + fprintf(gpoutfile, "%% scaling factor\na := 1cm;\n"); + fprintf(gpoutfile, "%% width and height of the figure\nw := %.3fa; h := %.3fa; %% (%g%s, %g%s)\n", + 0.001*(term->xmax), 0.001*(term->ymax), CONTEXT_params.xsize.value, unit_name(CONTEXT_params.xsize.unit), CONTEXT_params.ysize.value, unit_name(CONTEXT_params.ysize.unit)); - fprintf(gpoutfile, "%% horizontal and vertical scaling factors\na:=w/%.2f; b:=h/%.2f;\n", - (term->xmax)/100.0, (term->ymax)/100.0); /* TODO: the following if-else could be slightly nicer */ if(CONTEXT_images == CONTEXT_IMAGES_INLINE) { fprintf(gpoutfile, "%% temporary variable for storing the path and images\nsave p, img, ima; path p; string img, ima;\n"); @@ -1352,7 +1354,7 @@ CONTEXT_endpath() { /* if we have a dot, draw only the dot */ if(CONTEXT_path_is_dot) { - fprintf(gpoutfile, "gp_dot(%.2fa,%.2fb);\n", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); + fprintf(gpoutfile, "gp_dot(%.3fa,%.3fa);\n", 0.001*CONTEXT_posx, 0.001*CONTEXT_posy); CONTEXT_path_is_dot = 0; /* cyclic path, so let's end it with "--cycle" */ @@ -1361,7 +1363,7 @@ CONTEXT_endpath() /* regular non-cyclic path */ } else { - fprintf(gpoutfile, "--(%.2fa,%.2fb);\ngp_draw(p);\n", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); + fprintf(gpoutfile, "--(%.3fa,%.3fa);\ngp_draw(p);\n", 0.001*CONTEXT_posx, 0.001*CONTEXT_posy); } /* we're not inside path any more */ @@ -1407,7 +1409,7 @@ CONTEXT_startpath() CONTEXT_path_start_y = CONTEXT_posy; CONTEXT_path_count = 2; - fprintf(gpoutfile, "p := (%.2fa,%.2fb)", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); + fprintf(gpoutfile, "p := (%.3fa,%.3fa)", 0.001*CONTEXT_posx, 0.001*CONTEXT_posy); } /* **************** @@ -1453,7 +1455,7 @@ CONTEXT_vector(unsigned int x, unsigned int y) CONTEXT_path_count = 2; } /* and output the previous point */ - fprintf(gpoutfile, "--(%.2fa,%.2fb)", CONTEXT_posx / 100.0, CONTEXT_posy / 100.0); + fprintf(gpoutfile, "--(%.3fa,%.3fa)", 0.001*CONTEXT_posx, 0.001*CONTEXT_posy); } CONTEXT_posx = x; @@ -1530,7 +1532,7 @@ CONTEXT_put_text(unsigned int x, unsigned int y, const char str[]) for(s=str; s[0] == ' '; s++); /* label position */ - fprintf(gpoutfile, "gp_put_text((%.2fa, %.2fb), ", x/100.0, y/100.0); + fprintf(gpoutfile, "gp_put_text((%.3fa, %.3fa), ", 0.001*x, 0.001*y); /* angle of rotation - optional and needed only if it's different from 0 */ if (CONTEXT_ang != 0) { fprintf(gpoutfile, "angle(%d), ", CONTEXT_ang); @@ -1594,7 +1596,7 @@ CONTEXT_point (unsigned int x, unsigned int y, int number) if(CONTEXT_path_count > 0) CONTEXT_endpath(); - fprintf(gpoutfile, "gp_point(%.2fa,%.2fb,%d);\n", x/100.0, y/100.0, number); + fprintf(gpoutfile, "gp_point(%.3fa,%.3fa,%d);\n", 0.001*x, 0.001*y, number); } /* *************** @@ -1848,8 +1850,8 @@ CONTEXT_fillbox(int style, unsigned int x1, unsigned int y1, unsigned int width, CONTEXT_endpath(); /* create a new path */ - fprintf(gpoutfile, "p := unitsquare xyscaled (%.2fa,%.2fb) shifted (%.2fa,%.2fb);\n", width/100.0, height/100.0, x1/100.0, y1/100.0); - /* fprintf(gpoutfile, "p := gp_rect ((%.2fa,%.2fb),(%.2fa,%.2fb));\n", x1/100.0, y1/100.0, width/100.0, height/100.0); */ + fprintf(gpoutfile, "p := unitsquare xyscaled (%.3fa,%.3fa) shifted (%.3fa,%.3fa);\n", 0.001*width, 0.001*height, 0.001*x1, 0.001*y1); + /* fprintf(gpoutfile, "p := gp_rect ((%.3fa,%.3fa),(%.3fa,%.3fa));\n", 0.001*x1, 0.001*y1, 0.001*width, 0.001*height); */ /* fills the box according to the "style" * the code went out of this routine because of undocumented behaviour @@ -1905,7 +1907,7 @@ CONTEXT_fill(int style) /* fill the box with density "density": if no parameter density is specified, it implies 100% density by default */ if (density < 100) { - fprintf(gpoutfile, ",density(%.2f)", density*0.01); + fprintf(gpoutfile, ",density(%.2f)", 0.01*density); } break; @@ -2169,11 +2171,11 @@ CONTEXT_filled_polygon (int points, gpiPoint *corners) /* create new path with corners */ fputs("p := ", gpoutfile); - fprintf(gpoutfile, "(%.2fa,%.2fb)", corners[0].x/100.0, corners[0].y/100.0); + fprintf(gpoutfile, "(%.3fa,%.3fa)", 0.001*corners[0].x, 0.001*corners[0].y); for (i=1; i Date: Fri, 16 Sep 2011 18:05:21 +0200 Subject: [PATCH 49/54] cosmetics of output --- term/context.trm | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/term/context.trm b/term/context.trm index 6b7b7cf6f..b1ee2b8b1 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1291,17 +1291,15 @@ CONTEXT_graphics() fprintf(gpoutfile, "\\startGNUPLOTgraphic[%d]\n", ++CONTEXT_counter); } - fprintf(gpoutfile, "string gnuplotversion, termversion;\n"); - fprintf(gpoutfile, "gnuplotversion := \"%s\"; termversion := \"%s\";\n", - gnuplot_version, CONTEXT_term_version); + fprintf(gpoutfile, "string gnuplotversion; gnuplotversion := \"%s\";\n", gnuplot_version); + fprintf(gpoutfile, "string termversion; termversion := \"%s\";\n", CONTEXT_term_version); /* * MetaPost can only handle numbers up to 4096. Too high resolution * would thus result in number overflow, that's why we scale down all the * integers from gnuplot by 1000 and multiply those numbers later by * appropriate scaling factor 'a' to get the proper dimensions. */ - fprintf(gpoutfile, "%% scaling factor\na := 1cm;\n"); - fprintf(gpoutfile, "%% width and height of the figure\nw := %.3fa; h := %.3fa; %% (%g%s, %g%s)\n", + fprintf(gpoutfile, "%% scaling factor, width and height of the figure\na := 1cm; w := %.3fa; h := %.3fa; %% (%g%s, %g%s)\n", 0.001*(term->xmax), 0.001*(term->ymax), CONTEXT_params.xsize.value, unit_name(CONTEXT_params.xsize.unit), CONTEXT_params.ysize.value, unit_name(CONTEXT_params.ysize.unit)); From 6b93b4bc0cd4e8abde16f99d0b6642cacc08d427 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Tue, 27 Sep 2011 12:39:30 +0200 Subject: [PATCH 50/54] use gp_basename to remove directory from filename when image is written out --- term/context.trm | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/term/context.trm b/term/context.trm index b1ee2b8b1..2e291b3f4 100644 --- a/term/context.trm +++ b/term/context.trm @@ -417,14 +417,16 @@ enum CONTEXT_POINTS { CONTEXT_POINTS_WITH_METAPOST, CONTEXT_POINTS_WITH_TEX }; enum CONTEXT_IMAGES { CONTEXT_IMAGES_INLINE, CONTEXT_IMAGES_EXTERNAL }; /* counts the number of graphics */ -static int CONTEXT_counter = 0; +static int CONTEXT_counter = 0; /* counts the number of external PNG images */ -static int CONTEXT_image_counter = 0; +static int CONTEXT_image_counter = 0; +/* length of basename for storing image name (including path, excluding extension) */ +static int CONTEXT_image_filename_length = 0; +/* if name is a path, remember at which index basename starts */ +static int CONTEXT_image_filename_start = 0; static char *CONTEXT_image_filename = NULL; -static int CONTEXT_image_filename_length = 0; /* whether images will be external or not */ enum CONTEXT_IMAGES CONTEXT_images = CONTEXT_IMAGES_INLINE; -static char CONTEXT_image_name[MAX_ID_LEN+1] = ""; /* Pallete has to be stored for the usage in later plots */ static t_sm_palette *CONTEXT_old_palette; @@ -1070,6 +1072,7 @@ CONTEXT_init() free(CONTEXT_image_filename); CONTEXT_image_filename = NULL; CONTEXT_image_filename_length = 0; + CONTEXT_image_filename_start = 0; }; #ifdef WRITE_PNG_IMAGE if(CONTEXT_params.images == CONTEXT_IMAGES_EXTERNAL) { @@ -1078,6 +1081,7 @@ CONTEXT_init() /* but only if 'set output' was set because we use that string as base for image names */ if(outstr) { CONTEXT_image_filename_length = strlen(outstr); + CONTEXT_image_filename_start = strlen(outstr) - strlen(gp_basename(outstr)); /* we will cut off the last .tex ending if present */ /* find the last dot if present */ for(i=CONTEXT_image_filename_length-1; i>=0 && outstr[i] != '.'; i--); @@ -1092,6 +1096,7 @@ CONTEXT_init() CONTEXT_image_filename[CONTEXT_image_filename_length] = 0; } else { CONTEXT_image_filename_length = strlen("gp_image"); + CONTEXT_image_filename_start = 0; /* .xx.png; must be at least 7 characters long */ CONTEXT_image_filename = (char *)gp_alloc(CONTEXT_image_filename_length + 10, "ConTeXt image filename"); strncpy(CONTEXT_image_filename, "gp_image", CONTEXT_image_filename_length); @@ -1246,6 +1251,7 @@ CONTEXT_reset() free(CONTEXT_image_filename); CONTEXT_image_filename = NULL; CONTEXT_image_filename_length = 0; + CONTEXT_image_filename_start = 0; }; } @@ -2249,7 +2255,7 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag fprintf(gpoutfile, "draw image(\n "); } fprintf(gpoutfile, "externalfigure \"%s\" xyscaled (%.3fa,%.3fa) shifted (%.3fa,%.3fa);\n", - CONTEXT_image_filename, 0.001*(corner[1].x-corner[0].x), 0.001*(corner[0].y-corner[1].y), 0.001*corner[0].x, 0.001*corner[1].y); + CONTEXT_image_filename + CONTEXT_image_filename_start, 0.001*(corner[1].x-corner[0].x), 0.001*(corner[0].y-corner[1].y), 0.001*corner[0].x, 0.001*corner[1].y); if(is_clipped) { fprintf(gpoutfile, " clip currentpicture to unitsquare xyscaled (%.3fa,%.3fa) shifted (%.3fa,%.3fa););\n", 0.001*(corner[3].x-corner[2].x), 0.001*(corner[2].y-corner[3].y), 0.001*corner[2].x, 0.001*corner[3].y); From d7c13c31240b1c23e9a6de5d79a62c080606a162 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Tue, 27 Sep 2011 12:41:35 +0200 Subject: [PATCH 51/54] remove ifdef WRITE_PNG_IMAGE from TERM_HELP as it is not possible to use it there --- term/context.trm | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/term/context.trm b/term/context.trm index 2e291b3f4..5d771bf66 100644 --- a/term/context.trm +++ b/term/context.trm @@ -186,7 +186,7 @@ TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint */ static const char CONTEXT_term_version[] = "1.0"; static const char CONTEXT_term_patch[] = "0"; -static const char CONTEXT_term_date[] = "2011-09-16"; +static const char CONTEXT_term_date[] = "2011-09-27"; /********* * UNITS * @@ -2415,9 +2415,7 @@ START_HELP(context) " {linewidth | lw }", " {fontscale }", " {mppoints | texpoints}", -#ifdef WRITE_PNG_IMAGE " {inlineimages | externalimages}", -#endif " {defaultfont | font {} |", " font \"{,}\" {fontsize}}", "", @@ -2474,12 +2472,10 @@ START_HELP(context) " \\defineconversion[my own points][+,{\\ss x},\\mathematics{\\circ}]", " \\setupGNUPLOTterminal[context][points=tex,pointset=my own points]", "", -#ifdef WRITE_PNG_IMAGE " `inlineimages` writes binary images to a string and only works in ConTeXt MKIV.", " `externalimages` writes PNG files to disk and also works with ConTeXt MKII.", " Gnuplot needs to have support for PNG images built in for this to work." "", -#endif " With `font` you can set font name and size in standalone graphics.", " In non-standalone (`input`) mode only the font size is important", " to reserve enough space for text labels.", From 4aaf1bca96103310a49767c948d57b4f1d67041d Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Tue, 27 Sep 2011 12:50:46 +0200 Subject: [PATCH 52/54] fix a comment --- term/context.trm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/term/context.trm b/term/context.trm index 5d771bf66..6a11ec2ad 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1916,7 +1916,8 @@ CONTEXT_fill(int style) break; case FS_TRANSPARENT_PATTERN: - /* just a flag that should be interpreted in metapost, bun no idea what transparent pattern means */ + /* just a flag that should be interpreted in metapost, + non-transparent patterns have to fill with background color before drawing the pattern */ fprintf(gpoutfile, ",transparent"); case FS_PATTERN: pattern = (style >> 4); From 3789e905bfb4d232fcdfb93ffe2ac4862ac05d65 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Tue, 27 Sep 2011 21:33:11 +0200 Subject: [PATCH 53/54] remove support for font sizes outside the string 'font,size' --- term/context.trm | 103 +++++------------------------------------------ 1 file changed, 10 insertions(+), 93 deletions(-) diff --git a/term/context.trm b/term/context.trm index 6a11ec2ad..58e641f63 100644 --- a/term/context.trm +++ b/term/context.trm @@ -814,8 +814,7 @@ CONTEXT_options() case CONTEXT_OPT_FONT: c_token++; if ((tmp_string = try_to_get_string())) { - /* if string with font name (and size) has been provided, parse it, - * otherwise expect font size (as an integer {+units}) */ + /* if string with font name (and size) has been provided, parse it */ if (tmp_string) { /* * copies font name from tmp_string to tmp_font, @@ -829,50 +828,6 @@ CONTEXT_options() /* free allocated memmory */ free(tmp_string); - /* (TODO: this code repeats three times!!!) - * save font size: - * - * - if size > 0, copy - * - if size < 0, fix the size to default value (12pt) - * - if size = 0, ignore - */ - if (tmp_dimension.value > 0 && tmp_dimension.unit != UNIT_OTHER) { - CONTEXT_params.fontsize.value = tmp_dimension.value; - CONTEXT_params.fontsize.unit = tmp_dimension.unit; - } else if (tmp_dimension.value < 0 || tmp_dimension.unit == UNIT_OTHER) { - CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; - CONTEXT_params.fontsize.unit = UNIT_PT; - } - /* optional font size at the end */ - /* - * XXX: this code repeats twice, which is a bad programming habit! - * (plese be polite and provide a function instead) - */ - if (isanumber(c_token)) { - /* fontsize */ - tmp_dimension.value = real(const_express(&a)); - tmp_dimension.unit = UNIT_PT; - /* optional dimension of font size */ - switch (lookup_table(&units[0], c_token)) { - case UNIT_IN: - case UNIT_CM: - case UNIT_MM: - case UNIT_PT: - case UNIT_PC: - case UNIT_BP: - case UNIT_DD: - case UNIT_CC: - tmp_dimension.unit = lookup_table(&units[0], c_token); - c_token++; - break; - case UNIT_PX: - int_error(c_token,"Fontsize cannot be specified in pixels. Use other unit or leave dimension out."); - break; - default: - /* apparently no unit has been specified */ - break; - } - } /* * save font size: * @@ -887,49 +842,12 @@ CONTEXT_options() CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; CONTEXT_params.fontsize.unit = UNIT_PT; } + } else { + int_error(c_token,"String containing font name and/or size expected"); } /* font size (and possibly dimension) */ } else { - /* used to be: - * int_error(c_token,"String containing font name and/or size expected"); - */ - /* fontsize */ - tmp_dimension.value = real(const_express(&a)); - tmp_dimension.unit = UNIT_PT; - /* optional dimension of font size */ - switch (lookup_table(&units[0], c_token)) { - case UNIT_IN: - case UNIT_CM: - case UNIT_MM: - case UNIT_PT: - case UNIT_PC: - case UNIT_BP: - case UNIT_DD: - case UNIT_CC: - tmp_dimension.unit = lookup_table(&units[0], c_token); - c_token++; - break; - case UNIT_PX: - int_error(c_token,"Fontsize cannot be specified in pixels. Use other unit or leave dimension out."); - break; - default: - /* apparently no unit has been specified */ - break; - } - /* - * save font size (same as above): - * - * - if size > 0, copy - * - if size < 0, fix the size to default value (12pt) - * - if size = 0, ignore - */ - if (tmp_dimension.value > 0) { - CONTEXT_params.fontsize.value = tmp_dimension.value; - CONTEXT_params.fontsize.unit = tmp_dimension.unit; - } else if (tmp_dimension.value < 0) { - CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; - CONTEXT_params.fontsize.unit = UNIT_PT; - } + int_error(c_token,"String containing font name and/or size expected"); } break; case CONTEXT_OPT_OTHER: @@ -1019,12 +937,12 @@ CONTEXT_options() #endif strncat(term_options,"font \"", sizeof(term_options)-strlen(term_options)); strncat(term_options,CONTEXT_params.font, sizeof(term_options)-strlen(term_options)); - strncat(term_options,"\" ", sizeof(term_options)-strlen(term_options)); - snprintf(tmp_term_options, sizeof(tmp_term_options), - "%g%s", CONTEXT_params.fontsize.value, unit_name(CONTEXT_params.fontsize.unit) + ",%g%s", CONTEXT_params.fontsize.value, unit_name(CONTEXT_params.fontsize.unit) ); - strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); + strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); + strncat(term_options,"\"", sizeof(term_options)-strlen(term_options)); + /* just for debugging: snprintf(tmp_term_options, sizeof(tmp_term_options), "(%d)", sizeof(term_options) @@ -2417,8 +2335,7 @@ START_HELP(context) " {fontscale }", " {mppoints | texpoints}", " {inlineimages | externalimages}", -" {defaultfont | font {} |", -" font \"{,}\" {fontsize}}", +" {defaultfont | font \"{,}\"}", "", " In non-standalone (`input`) graphic only parameters `size` to select graphic", " size, `fontscale` to scale all the labels for a factor ", @@ -2481,7 +2398,7 @@ START_HELP(context) " In non-standalone (`input`) mode only the font size is important", " to reserve enough space for text labels.", " The command" -" set term context font \"myfont,ss\" 10pt", +" set term context font \"myfont,ss,10pt\"", " will result in", " \\setupbodyfont[myfont,ss,10pt]", " If you additionaly set `fontscale` to 0.8 for example,", From c6eaee1b7a9cc5f26ad868a071299baa7cbf87a8 Mon Sep 17 00:00:00 2001 From: Mojca Miklavec Date: Sat, 5 Nov 2011 22:13:09 +0100 Subject: [PATCH 54/54] remove units completely (by Bastian), typos, minor cosmetic changes and code cleanups, licence change to BSD --- term/context.trm | 910 +++++++++++++++-------------------------------- 1 file changed, 284 insertions(+), 626 deletions(-) diff --git a/term/context.trm b/term/context.trm index 58e641f63..d0ac14d3b 100644 --- a/term/context.trm +++ b/term/context.trm @@ -1,37 +1,33 @@ -/* Hello, Emacs, this is -*-C-*- +/* * $Id:$ */ /* GNUPLOT - context.trm */ /*[ - * Copyleft 2006-2011 Mojca Miklavec - * - * Permission to use, copy, and distribute this software and its - * documentation for any purpose with or without fee is hereby granted, - * provided that the above copyright notice appear in all copies and - * that both that copyright notice and this permission notice appear - * in supporting documentation. - * - * Permission to modify the software is granted, but not the right to - * distribute the complete modified source code. Modifications are to - * be distributed as patches to the released version. Permission to - * distribute binaries produced by compiling modified sources is granted, - * provided you - * 1. distribute the corresponding source modifications from the - * released version in the form of a patch file along with the binaries, - * 2. add special version identification to distinguish your version - * in addition to the base release version number, - * 3. provide your name and address as the primary contact for the - * support of your modified version, and - * 4. retain our contact information in regard to use of the base - * software. - * Permission to distribute the released version of the source code along - * with corresponding source modifications in the form of a patch file is - * granted with same provisions 2 through 4 for binary distributions. - * - * This software is provided "as is" without express or implied warranty - * to the extent permitted by applicable law. + * Copyright (c) 2006-2011, Mojca Miklavec + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. ]*/ /* @@ -63,10 +59,11 @@ * - http://wiki.contextgarden.net/Gnuplot * * With special thanks to: - * - Peter M\"unster for the initiative (Peter, sory for \") - * - Renaud Aubin for maintaining the darcs repository - * - Aditya Mahajan for some tricky parts in m-gnuplot.tex - * - Timoth\'ee Lecomte for many suggestion about improving this terminal + * - Peter Münster (ctx) for the initiative + * - Renaud Aubin (ctx) for maintaining the first source code repository + * - Aditya Mahajan (ctx) for some tricky parts in m-gnuplot.tex + * - Timothée Lecomte & Ethan A Merritt (gnuplot) for many suggestion about improving this terminal + * - Bastian Märkisch (gnuplot) for code improvements * */ @@ -97,12 +94,14 @@ * - advanced functionality with reference to PostScript terminal * * Future plans: + * - most important + * - different syntax for font switching + * - improve support for (transparent) binary images * - add missing functionality: * - improved support for palettes - * - other color spaces - * - enable support for binary images * - smooth shading in color bars * - gouraud shading (if it will be implemented in gnuplot) + * - other color spaces * - derive a better metapost terminal out of this one (to replace the old one) */ @@ -139,36 +138,33 @@ TERM_PUBLIC void CONTEXT_set_color __PROTO((t_colorspec *colorspec)); TERM_PUBLIC void CONTEXT_filled_polygon __PROTO((int points, gpiPoint *corners)); TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint *, t_imagecolor)); -/* Metapost can only deal with numbers between 2^{-16} and 2^12=4069 */ -/* the values will be set later */ +/* Metapost < 1.750 can only deal with numbers between 2^{-16} and 2^12=4069 */ /* scale is 1cm = 1000 units */ -// #define CONTEXT_DPI (2400) #define CONTEXT_DPCM 1000 -#define CONTEXT_DPI (2.54*CONTEXT_DPCM) -/* default plot size will be 5in x 3in*/ +#define CONTEXT_DPI (2.54 * CONTEXT_DPCM) +/* default plot size will be 5in x 3in */ #define CONTEXT_XSIZE_VALUE 5 #define CONTEXT_YSIZE_VALUE 3 -#define CONTEXT_XSIZE_UNIT UNIT_IN -#define CONTEXT_YSIZE_UNIT UNIT_IN -#define CONTEXT_XMAX (CONTEXT_XSIZE_VALUE*CONTEXT_DPI) -#define CONTEXT_YMAX (CONTEXT_YSIZE_VALUE*CONTEXT_DPI) +#define CONTEXT_SIZE_UNIT INCHES +#define CONTEXT_XMAX (CONTEXT_XSIZE_VALUE * CONTEXT_DPI) +#define CONTEXT_YMAX (CONTEXT_YSIZE_VALUE * CONTEXT_DPI) /* default fontsize: 12pt */ #define CONTEXT_FONTSIZE 12.0 /* default height of char: 12pt by default */ -#define CONTEXT_VCHAR (CONTEXT_DPI*CONTEXT_FONTSIZE/72.27) +#define CONTEXT_VCHAR (CONTEXT_DPI * CONTEXT_FONTSIZE / 72.27) /* in ConTeXt 12pt LM font is the default; * at that size digits are 5.8749847pt wide * in LaTeX, which assumes 11pt, the ratio is 5.3/11, which is similar */ #define CONTEXT_LM_H_TO_V_RATIO 0.4895 -#define CONTEXT_HCHAR (CONTEXT_LM_H_TO_V_RATIO*CONTEXT_VCHAR) +#define CONTEXT_HCHAR (CONTEXT_LM_H_TO_V_RATIO * CONTEXT_VCHAR) /* default tic size: 3.5bp (chosen to suit the size of plot approximately) * - in LaTeX it is 5bp * - in TikZ it is 0.18cm (approximately 5.1bp) * - in PostScript it is 3.15pt * - MetaPost uses 5pt or 5bp */ -#define CONTEXT_HTIC (3.5*CONTEXT_DPI/72) +#define CONTEXT_HTIC (3.5 * CONTEXT_DPI / 72) #define CONTEXT_VTIC CONTEXT_HTIC #endif /* TERM_PROTO */ @@ -178,7 +174,7 @@ TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint #ifdef TERM_BODY /* - * we will start counting when the code is included into CVS + * started counting when the code was included into CVS * * major number should change only in case of a complete rewrite or major incompatibility * minor number should change for every new functionality @@ -186,173 +182,12 @@ TERM_PUBLIC void CONTEXT_image __PROTO((unsigned, unsigned, coordval *, gpiPoint */ static const char CONTEXT_term_version[] = "1.0"; static const char CONTEXT_term_patch[] = "0"; -static const char CONTEXT_term_date[] = "2011-09-27"; - -/********* - * UNITS * - ********* - * - * This part of code is meant for parsing the units in - * set term context size 5in,3in - * or even - * set term context size (5+3/2) cm, 8-2**2in - * which will make the picture 6.5 centimeters wide and 4 inches high. - * - * It is also used for (rather dirty) parsing of font sizes, - * which is needed for estimating character width & height. - * - * This code may be taken out of the terminal if anyone needs it. - * - * Similar functionality is provided for some drivers (such as PostScript), - * to parse terminal size, but is more limited and supports less units. - */ - -typedef enum size_unit { - UNIT_PT, /* point */ - UNIT_PC, /* pica: 1pc = 12pt */ - UNIT_IN, /* inch: 1in = 72.27pt */ - UNIT_BP, /* big point: 72bp = 1in */ - UNIT_CM, /* centimeter: 2.54cm = 1in */ - UNIT_MM, /* millimeter: 10mm = 1cm */ - UNIT_DD, /* didot point: 1157dd = 1238pt */ - UNIT_CC, /* cicero: 1cc = 12dd */ - /* ----- // --- [not absolute units] */ - UNIT_PX, /* pixel: [dpi resolution] * 1px = 1in */ - UNIT_RELATIVE, /* relative: no specific unit */ - UNIT_OTHER -} size_unit; - -static struct gen_table units[] = { - { "pt", UNIT_PT }, /* point */ - { "pc", UNIT_PC }, /* pica */ - { "in$ch", UNIT_IN }, /* inch */ - { "bp", UNIT_BP }, /* big point */ - { "cm", UNIT_CM }, /* centimeter */ - { "mm", UNIT_MM }, /* millimeter */ - { "dd", UNIT_DD }, /* didot point */ - { "cc", UNIT_CC }, /* cicero */ - { "px", UNIT_PX }, /* pixel */ - { NULL, UNIT_OTHER } -}; - -typedef struct dimension_t { - double value; - size_unit unit; -} dimension_t; - -static double convert_to_unit __PROTO((dimension_t from, size_unit to_unit)); -static char* unit_name __PROTO((size_unit unit)); -/* needs a better name; lookup_table should be OK for it, but only if unit is a token, - * otherwise the work has to be done "manually" */ -static size_unit string_to_unit __PROTO((char *unit_name)); - -/* -------------- - * string_to_unit - * -------------- - * - * Returns two-letter name for the unit - * (it can be used in TeX or metapost directly) - */ -static char* -unit_name(size_unit unit) -{ - switch(unit) { - case UNIT_PT: return "pt"; - case UNIT_PC: return "pc"; - case UNIT_IN: return "in"; - case UNIT_BP: return "bp"; - case UNIT_CM: return "cm"; - case UNIT_MM: return "mm"; - case UNIT_DD: return "dd"; - case UNIT_CC: return "cc"; - case UNIT_PX: return "px"; - default: return NULL; /* XXX: do we need some error handling here? */ - } -} - -/* -------------- - * string_to_unit - * -------------- - * - * Accepts a two-letter name for an unit - * and returns the unit type - */ -static size_unit -string_to_unit(char *name) -{ - /* invalid unit for no string or for a string of any other size than two */ - if (name == NULL) return UNIT_OTHER; - if (strlen(name) != 2) return UNIT_OTHER; - - switch (name[0]) { - case 'b': - if (name[1] == 'p') return UNIT_BP; - case 'c': - if (name[1] == 'c') return UNIT_CC; - if (name[1] == 'm') return UNIT_CM; - case 'd': - if (name[1] == 'd') return UNIT_DD; - case 'i': - if (name[1] == 'n') return UNIT_IN; - case 'm': - if (name[1] == 'm') return UNIT_MM; - case 'p': - if (name[1] == 'c') return UNIT_PC; - if (name[1] == 't') return UNIT_PT; - if (name[1] == 'x') return UNIT_PX; - default: - return UNIT_OTHER; - } -} - -/* --------------- - * convert_to_unit - * --------------- - * - * Converts the value (from.value) from one unit (from.unit) - * to another (to_unit). - * - * This "stupid" converter should be rewritten completely for better efficiency, - * although it currently works OK. It should be initilized only once. - */ -static double -convert_to_unit(dimension_t from, size_unit to_unit) -{ - /* conversion is done via inches - * conversion[UNIT_CM] returns how much inches is one cm (ie. 1/2.54) */ - double conversion[20]; /* XXX: shoud be sizeof(size_unit) */ - - if(from.unit == to_unit) { - return (double)from.value; - /* XXX: this is wrong, but I have no other idea what to do with it. - It's not yet used anywhere so far, so let's care about it - when the need arises. */ - } else if(to_unit == UNIT_RELATIVE || to_unit == UNIT_OTHER) { - return (double)from.value; - } - - conversion[UNIT_PT] = 1.0/72.27; - conversion[UNIT_PC] = 12.0*conversion[UNIT_PT]; - conversion[UNIT_IN] = 1.0; - conversion[UNIT_BP] = 1.0/72.0; - conversion[UNIT_CM] = 1.0/2.54; - conversion[UNIT_MM] = conversion[UNIT_CM]/10.0; - conversion[UNIT_DD] = 1238.0/1157.0*conversion[UNIT_PT]; - conversion[UNIT_CC] = 12.0*conversion[UNIT_DD]; - /* for pixel some resolution would be needed */ - conversion[UNIT_PX] = 1/72.0; /* XXX: wrong! (but not used anywhere yet) */ - - return from.value*conversion[from.unit]/conversion[to_unit]; -} - -/**************** - * End of UNITS * - ****************/ +static const char CONTEXT_term_date[] = "2011-11-05"; static void CONTEXT_params_reset __PROTO((void)); static void CONTEXT_adjust_dimensions __PROTO((void)); -static void CONTEXT_fontstring_parse __PROTO((char *from_string, char *to_string, int to_size, dimension_t *dimension)); +static void CONTEXT_fontstring_parse __PROTO((char *from_string, char *to_string, int to_size, double *fontsize)); static void CONTEXT_startpath __PROTO((void)); static void CONTEXT_endpath __PROTO((void)); @@ -367,7 +202,7 @@ static int CONTEXT_posy; /* remembers where we started the path: * if we finish it in the same point, the path is closed with --cycle * - * After I impplemented this, the functionality has been added to gnuplot core + * After I implemented this, the functionality has been added to gnuplot core * and PostScript terminal uses it, but to be on the safe side, * I prefer not to add last-minute patches which could potentially break something. * (the code written here proved to be rather safe & working so far) @@ -377,12 +212,12 @@ static int CONTEXT_path_start_x; static int CONTEXT_path_start_y; /* fontname, fontsize */ static char CONTEXT_font[MAX_ID_LEN+1] = ""; -static dimension_t CONTEXT_fontsize = {CONTEXT_FONTSIZE, UNIT_PT}; +static double CONTEXT_fontsize = CONTEXT_FONTSIZE; /* fontname,fontsize to be put next to font labels if needed */ static char CONTEXT_font_explicit[2*MAX_ID_LEN+1] = ""; -/* this is only set to >0 if asked for explicitely (for example with set_font(",15pt")) */ -static dimension_t CONTEXT_fontsize_explicit = {0, UNIT_PT}; +/* this is only set to >0 if asked for explicitely (for example with set_font(",15")) */ +static double CONTEXT_fontsize_explicit = 0.0; /* the last pointsize used (it will only be changed if it becomes different) */ static double CONTEXT_old_pointsize = 1.0; @@ -436,28 +271,30 @@ static t_sm_palette *CONTEXT_old_palette; /*********************/ typedef struct CONTEXT_params_t { - dimension_t xsize; /* 5in */ - dimension_t ysize; /* 3in */ - TBOOLEAN standalone; /* false */ - TBOOLEAN timestamp; /* true */ - char *header; /* "" */ - TBOOLEAN color; /* true */ - TBOOLEAN dashed; /* true */ - enum LINEJOIN linejoin; /* MITER */ - enum LINECAP linecap; /* BUTT */ - double scale_dashlength; /* 1.0 */ - double scale_linewidth; /* 1.0 */ - double scale_text; /* 1.0 */ + double xsize; /* 5in */ + double ysize; /* 3in */ + size_units unit; /* INCHES */ + TBOOLEAN standalone; /* false */ + TBOOLEAN timestamp; /* true */ + char *header; /* "" */ + TBOOLEAN color; /* true */ + TBOOLEAN dashed; /* true */ + enum LINEJOIN linejoin; /* MITER */ + enum LINECAP linecap; /* BUTT */ + double scale_dashlength; /* 1.0 */ + double scale_linewidth; /* 1.0 */ + double scale_text; /* 1.0 */ enum CONTEXT_POINTS points; /* CONTEXT_POINTS_WITH_METAPOST */ enum CONTEXT_IMAGES images; /* CONTEXT_IMAGES_INLINE */ - char font[MAX_ID_LEN+1]; /* "" */ - dimension_t fontsize; /* 12pt */ + char font[MAX_ID_LEN+1]; /* "" */ + double fontsize; /* 12pt */ } CONTEXT_params_t; #define CONTEXT_PARAMS_DEFAULT { \ - {CONTEXT_XSIZE_VALUE, CONTEXT_XSIZE_UNIT},\ - {CONTEXT_YSIZE_VALUE, CONTEXT_YSIZE_UNIT},\ + CONTEXT_XSIZE_VALUE,\ + CONTEXT_YSIZE_VALUE,\ + INCHES,\ FALSE,\ TRUE,\ NULL,\ @@ -471,7 +308,7 @@ typedef struct CONTEXT_params_t { CONTEXT_POINTS_WITH_METAPOST,\ CONTEXT_IMAGES_INLINE,\ "",\ - {CONTEXT_FONTSIZE, UNIT_PT}\ + CONTEXT_FONTSIZE\ } static CONTEXT_params_t CONTEXT_params = CONTEXT_PARAMS_DEFAULT; @@ -533,9 +370,9 @@ static struct gen_table CONTEXT_opts[] = { { NULL, CONTEXT_OPT_OTHER } }; -/* **************** - * CONTEXT_options* - * **************** +/* ********************** + * CONTEXT_params_reset * + * ********************** * * Resets all parameters of the terminal to their default value. */ @@ -554,25 +391,19 @@ CONTEXT_params_reset() } -/* **************** - * CONTEXT_options* - * **************** +/* ***************** + * CONTEXT_options * + * ***************** * * Parses "set term context [options]". */ TERM_PUBLIC void CONTEXT_options() { - int i; struct value a; char *tmp_string; - char tmp_font[MAX_ID_LEN+1] = ""; char tmp_term_options[MAX_LINE_LEN+1] = ""; - enum size_unit tmp_unit[2]; - double tmp_double[2]; - dimension_t tmp_dimension; - while (!END_OF_COMMAND) { switch (lookup_table(&CONTEXT_opts[0], c_token)) { case CONTEXT_OPT_DEFAULT: @@ -580,86 +411,27 @@ CONTEXT_options() /* there should be a better way to do it, but I don't know how */ CONTEXT_params_reset(); break; - /*---------------------------------------------------*/ - case CONTEXT_OPT_SIZE: + case CONTEXT_OPT_SIZE: { + float xmax_t, ymax_t; + size_units unit; c_token++; - /* size {cm|in}, {cm|in} - * - * value of the xsize - * XXX: perhaps the values shouldn't be saved right away - * just take care that all the values supported here are supported in conversion and - * in output routine as well - */ - for (i=0; i<2; i++) { - tmp_double[i] = real(const_express(&a)); - /* check if the size is positive (minimal requirement) */ - if (tmp_double[i] <= 0) - int_error(c_token-1,"Size has to be positive."); - /* if unit (inch or cm) is used, use that one, otherwise interpret that value - * as if it was a relative to the default value (5in); ie: 1.5 means 7.5 inches */ - switch (lookup_table(&units[0], c_token)) { - case UNIT_IN: - case UNIT_CM: - case UNIT_MM: - tmp_unit[i] = lookup_table(&units[0], c_token); - c_token++; - /* read the comma after the first number */ - if (i==0) { - if (equals(c_token, ",")) { - c_token++; - } else { - int_error(c_token,"',' expected.\nSize has to be specified as\n\tsize {in|cm}, {in|cm}\nor as\n\tsize "); - } - } - break; - case UNIT_PT: - case UNIT_PC: - case UNIT_BP: - case UNIT_DD: - case UNIT_CC: - int_error(c_token,"Sorry, this unit is not supported. Use cm or in."); - break; - case UNIT_PX: - int_error(c_token,"Size for this terminal cannot be specified in pixels. Use cm or in."); - break; - /* if comma follows, interpret as relative unit - * if some other unit follows */ - default: - /* first run: if no unit is specified, we expect at least a comma */ - if (i==0) { - if (equals(c_token, ",")) { - c_token++; - /* default xsize is 5in - use the current size as relative value */ - tmp_double[0] *= CONTEXT_XSIZE_VALUE; - tmp_unit[0] = CONTEXT_XSIZE_UNIT; - } else { - tmp_double[1] = tmp_double[0]*CONTEXT_YSIZE_VALUE; - tmp_double[0] = tmp_double[0]*CONTEXT_XSIZE_VALUE; - tmp_unit[0] = CONTEXT_XSIZE_UNIT; - tmp_unit[1] = CONTEXT_YSIZE_UNIT; - i++; - } - /* second run: if no unit is specified, proceed to next command - * and interpret the expression as being relative to the default size */ - } else { - tmp_double[1] *= CONTEXT_YSIZE_VALUE; - tmp_unit[1] = CONTEXT_YSIZE_UNIT; - } - break; - } + + /* size {cm|in}, {cm|in} */ + unit = parse_term_size(&xmax_t, &ymax_t, CM); + CONTEXT_params.xsize = (double)xmax_t / gp_resolution; + CONTEXT_params.ysize = (double)ymax_t / gp_resolution; + CONTEXT_params.unit = unit; + if (unit == CM) { + CONTEXT_params.xsize *= 2.54; + CONTEXT_params.ysize *= 2.54; } - CONTEXT_params.xsize.value = tmp_double[0]; - CONTEXT_params.ysize.value = tmp_double[1]; - CONTEXT_params.xsize.unit = tmp_unit[0]; - CONTEXT_params.ysize.unit = tmp_unit[1]; break; - /*---------------------------------------------------*/ + } case CONTEXT_OPT_SIZE_DEFAULT: c_token++; - CONTEXT_params.xsize.value = CONTEXT_XSIZE_VALUE; - CONTEXT_params.ysize.value = CONTEXT_YSIZE_VALUE; - CONTEXT_params.xsize.unit = CONTEXT_XSIZE_UNIT; - CONTEXT_params.ysize.unit = CONTEXT_YSIZE_UNIT; + CONTEXT_params.xsize = CONTEXT_XSIZE_VALUE; + CONTEXT_params.ysize = CONTEXT_YSIZE_VALUE; + CONTEXT_params.unit = CONTEXT_SIZE_UNIT; break; case CONTEXT_OPT_INPUT: c_token++; @@ -769,8 +541,7 @@ CONTEXT_options() /* CONTEXT_params.font should be an empty string */ CONTEXT_params.font[0] = 0; /* default fontsize is 12pt */ - CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; - CONTEXT_params.fontsize.unit = UNIT_PT; + CONTEXT_params.fontsize = CONTEXT_FONTSIZE; break; case CONTEXT_OPT_POINTS_WITH_METAPOST: c_token++; @@ -796,11 +567,11 @@ CONTEXT_options() * The preferred way to set the font is to set it in a document itself, * labels in gnuplot in graphs will then inherit that font. * - * However, it is polite to tell gnuplot which size is going to be used, + * However, it is important to tell gnuplot which size is going to be used, * so that it can estimate size of text labels. * * Whenever you specify - * set term context font "fontname,14pt" (or "fontname,14") + * set term context font "fontname,14" * * there are two possibilities: * - if STANDALONE mode is on, then the whole string will be used as @@ -811,66 +582,49 @@ CONTEXT_options() * internally to estimate sizes of labels, but the font name * won't be written anywhere */ - case CONTEXT_OPT_FONT: + case CONTEXT_OPT_FONT: { + double tmp_fontsize; + char tmp_font[MAX_ID_LEN+1] = ""; c_token++; - if ((tmp_string = try_to_get_string())) { - /* if string with font name (and size) has been provided, parse it */ - if (tmp_string) { - /* - * copies font name from tmp_string to tmp_font, - * but leaves out font size and saves it to tmp_dimension instead - * - * if no font size is specified, then tmp_dimension.value is set to 0 - */ - CONTEXT_fontstring_parse(tmp_string, tmp_font, sizeof(tmp_font), &tmp_dimension); - /* copies font name to parameters */ - strncpy(CONTEXT_params.font, tmp_font, sizeof(CONTEXT_params.font)); - /* free allocated memmory */ - free(tmp_string); - - /* - * save font size: - * - * - if size > 0, copy - * - if size < 0, fix the size to default value (12pt) - * - if size = 0, ignore - */ - if (tmp_dimension.value > 0 && tmp_dimension.unit != UNIT_OTHER) { - CONTEXT_params.fontsize.value = tmp_dimension.value; - CONTEXT_params.fontsize.unit = tmp_dimension.unit; - } else if (tmp_dimension.value < 0 || tmp_dimension.unit == UNIT_OTHER) { - CONTEXT_params.fontsize.value = CONTEXT_FONTSIZE; - CONTEXT_params.fontsize.unit = UNIT_PT; - } - } else { - int_error(c_token,"String containing font name and/or size expected"); - } - /* font size (and possibly dimension) */ - } else { - int_error(c_token,"String containing font name and/or size expected"); + if ((tmp_string = try_to_get_string()) && (tmp_string != NULL)) { + CONTEXT_fontstring_parse(tmp_string, tmp_font, MAX_ID_LEN+1, &tmp_fontsize); + /* copies font name to parameters */ + strncpy(CONTEXT_params.font, tmp_font, sizeof(CONTEXT_params.font)); + tmp_font[MAX_ID_LEN] = NUL; + free(tmp_string); + /* save font size: + * + * - if size > 0, copy + * - if size < 0, fix the size to default value (12pt) + * - if size = 0, ignore + */ + if (tmp_fontsize > 0) + CONTEXT_params.fontsize = tmp_fontsize; + else if (tmp_fontsize < 0) + CONTEXT_params.fontsize = CONTEXT_FONTSIZE; } break; + } case CONTEXT_OPT_OTHER: default: /* error */ - int_error(c_token, "extraneous argument in set terminal %s",term->name); + int_error(c_token, "extraneous argument in set terminal %s",term->name); break; } } /* current font size in pt (to be used in CONTEXT_adjust_dimensions) */ - CONTEXT_fontsize.value = CONTEXT_params.fontsize.value; - CONTEXT_fontsize.unit = CONTEXT_params.fontsize.unit; + CONTEXT_fontsize = CONTEXT_params.fontsize; /* sets term->xmax, ymax, vchar, hchar */ CONTEXT_adjust_dimensions(); snprintf(term_options, sizeof(term_options), "size %g%s,%g%s %s %s %s", - CONTEXT_params.xsize.value, - unit_name(CONTEXT_params.xsize.unit), - CONTEXT_params.ysize.value, - unit_name(CONTEXT_params.ysize.unit), + CONTEXT_params.xsize, + (CONTEXT_params.unit == INCHES) ? "in" : "cm", + CONTEXT_params.ysize, + (CONTEXT_params.unit == INCHES) ? "in" : "cm", CONTEXT_params.standalone ? "standalone" : "input", CONTEXT_params.timestamp ? "timestamp" : "notimestamp", CONTEXT_params.header == NULL ? "noheader \\\n " : "\\\n header "); @@ -885,7 +639,7 @@ CONTEXT_options() CONTEXT_params.color ? "color " : "monochrome ", sizeof(term_options)-strlen(term_options)); - switch(CONTEXT_params.linejoin) { + switch (CONTEXT_params.linejoin) { case LINEJOIN_MITER: strncat(term_options, "mitered ", sizeof(term_options)-strlen(term_options)); break; @@ -896,7 +650,7 @@ CONTEXT_options() strncat(term_options, "beveled ", sizeof(term_options)-strlen(term_options)); break; } - switch(CONTEXT_params.linecap) { + switch (CONTEXT_params.linecap) { case LINECAP_BUTT : strncat(term_options, "butt", sizeof(term_options)-strlen(term_options)); break; @@ -935,25 +689,14 @@ CONTEXT_options() sizeof(term_options)-strlen(term_options)); break; } #endif - strncat(term_options,"font \"", sizeof(term_options)-strlen(term_options)); - strncat(term_options,CONTEXT_params.font, sizeof(term_options)-strlen(term_options)); - snprintf(tmp_term_options, sizeof(tmp_term_options), - ",%g%s", CONTEXT_params.fontsize.value, unit_name(CONTEXT_params.fontsize.unit) - ); - strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); - strncat(term_options,"\"", sizeof(term_options)-strlen(term_options)); - - /* just for debugging: - snprintf(tmp_term_options, sizeof(tmp_term_options), - "(%d)", sizeof(term_options) - ); - strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)); - */ + snprintf(tmp_term_options, sizeof(tmp_term_options), "font \"%s,%g\"", + CONTEXT_params.font, CONTEXT_params.fontsize); + strncat(term_options, tmp_term_options, sizeof(term_options) - strlen(term_options)); } -/* ************* - * CONTEXT_init* - * ************* +/* ************** + * CONTEXT_init * + * ************** * * Starts a new file. * @@ -986,26 +729,25 @@ CONTEXT_init() /* only if external images are both requested and supported, we switch to them (double paranoia) */ /* delete the stored filename first */ - if(CONTEXT_image_filename) { + if (CONTEXT_image_filename) { free(CONTEXT_image_filename); CONTEXT_image_filename = NULL; CONTEXT_image_filename_length = 0; CONTEXT_image_filename_start = 0; }; #ifdef WRITE_PNG_IMAGE - if(CONTEXT_params.images == CONTEXT_IMAGES_EXTERNAL) { + if (CONTEXT_params.images == CONTEXT_IMAGES_EXTERNAL) { CONTEXT_images = CONTEXT_IMAGES_EXTERNAL; /* but only if 'set output' was set because we use that string as base for image names */ - if(outstr) { + if (outstr) { CONTEXT_image_filename_length = strlen(outstr); CONTEXT_image_filename_start = strlen(outstr) - strlen(gp_basename(outstr)); /* we will cut off the last .tex ending if present */ /* find the last dot if present */ - for(i=CONTEXT_image_filename_length-1; i>=0 && outstr[i] != '.'; i--); - if(outstr[i] == '.') { + for (i = CONTEXT_image_filename_length - 1; i >= 0 && outstr[i] != '.'; i--); + if (outstr[i] == '.') CONTEXT_image_filename_length = i; - } /* it would also be very nice to do some sanity checks on filenames */ /* .xx.png; must be at least 7 characters long */ @@ -1024,10 +766,9 @@ CONTEXT_init() #endif fprintf(gpoutfile, "%% Written by ConTeXt terminal for GNUPLOT"); - if(CONTEXT_params.timestamp) { - if(strftime(timebuffer, 100, "%Y-%m-%d %H:%M %Z", localtime(&now)) != 0) { + if (CONTEXT_params.timestamp) { + if (strftime(timebuffer, 100, "%Y-%m-%d %H:%M %Z", localtime(&now)) != 0) fprintf(gpoutfile, " on: %s", timebuffer); - } } fprintf(gpoutfile, "\n"); fprintf(gpoutfile, "%% GNUPLOT version: %s.%s, terminal version: %s.%s (%s)\n", @@ -1035,7 +776,7 @@ CONTEXT_init() fprintf(gpoutfile, "%% See also http://wiki.contextgarden.net/Gnuplot\n%%\n"); /* place the header first if this is a standalone graphic */ - if(CONTEXT_params.standalone) { + if (CONTEXT_params.standalone) { /* If encoding is explicitely set to UTF-8 by gnuplot, use that setting. * \enableregime only makes a difference for pdfTeX; in LuaTeX and XeTeX UTF-8 is already default, * so this line will be ignored. @@ -1044,7 +785,7 @@ CONTEXT_init() * - ConTeXt doesn't support all encodings supported by Gnuplot. * - In LuaTeX and XeTeX one should not use any other encoding anyway. * - pdfTeX users are free to use "header '\enableregime[...]'" */ - switch(encoding) { + switch (encoding) { case S_ENC_UTF8: fputs("\\enableregime\n [utf-8]\n", gpoutfile); break; @@ -1055,27 +796,22 @@ CONTEXT_init() /* load the gnuplot module */ fputs("\\usemodule\n [gnuplot]\n", gpoutfile); /* enable color (the only place where "color" is indeed used so far) */ - if (CONTEXT_params.color) { + if (CONTEXT_params.color) fputs("\\setupcolors\n [state=start]\n", gpoutfile); - } /* additional user-provided header information (if available) */ - if(CONTEXT_params.header) { + if (CONTEXT_params.header) fprintf(gpoutfile, "%s\n", CONTEXT_params.header); - } /* for some reason setting \bodyfontenvironment is needed, * otherwise \switchtobodyfont[name] doesn't work OK */ - if (!(CONTEXT_params.fontsize.value == CONTEXT_FONTSIZE && CONTEXT_params.fontsize.unit == UNIT_PT)) { - fprintf(gpoutfile, "\\definebodyfontenvironment\n [%g%s]\n", - CONTEXT_params.fontsize.value, - unit_name(CONTEXT_params.fontsize.unit)); - } + if (!(CONTEXT_params.fontsize == CONTEXT_FONTSIZE)) + fprintf(gpoutfile, "\\definebodyfontenvironment\n [%gpt]\n", + CONTEXT_params.fontsize); /* set the proper font: \setupbodyfont[{fontname,}fontsize sizeunit] */ - fprintf(gpoutfile, "\\setupbodyfont\n [%s%s%g%s]\n", + fprintf(gpoutfile, "\\setupbodyfont\n [%s%s%gpt]\n", CONTEXT_params.font, /* write a comma only if the last string was non-empty */ ((strlen(CONTEXT_params.font)>0) ? "," : ""), - CONTEXT_params.fontsize.value, - unit_name(CONTEXT_params.fontsize.unit)); + CONTEXT_params.fontsize); /*---------* * options * @@ -1091,7 +827,7 @@ CONTEXT_init() /* linejoin: mitered/rounded/beveled * default: mitered */ fprintf(gpoutfile, "linejoin="); - switch(CONTEXT_params.linejoin) { + switch (CONTEXT_params.linejoin) { case LINEJOIN_MITER: fprintf(gpoutfile, "mitered"); break; case LINEJOIN_ROUND: fprintf(gpoutfile, "rounded"); break; case LINEJOIN_BEVEL: fprintf(gpoutfile, "beveled"); break; @@ -1101,7 +837,7 @@ CONTEXT_init() /* linecap: butt/rounded/squared * default: butt */ fprintf(gpoutfile, " linecap="); - switch(CONTEXT_params.linecap) { + switch (CONTEXT_params.linecap) { case LINECAP_BUTT : fprintf(gpoutfile, "butt"); break; case LINECAP_ROUND : fprintf(gpoutfile, "rounded"); break; case LINECAP_SQUARE: fprintf(gpoutfile, "squared"); break; @@ -1154,7 +890,7 @@ TERM_PUBLIC void CONTEXT_reset() { /* we only have to end the document if this is a stand-alone graphic */ - if(CONTEXT_params.standalone) { + if (CONTEXT_params.standalone) { fputs("\\stoptext\n", gpoutfile); } else { /* This means that any subsequent plots to the same file will be ignored. @@ -1165,7 +901,7 @@ CONTEXT_reset() fputs("\\endinput\n", gpoutfile); } /* deallocate image name if present */ - if(CONTEXT_image_filename) { + if (CONTEXT_image_filename) { free(CONTEXT_image_filename); CONTEXT_image_filename = NULL; CONTEXT_image_filename_length = 0; @@ -1186,10 +922,10 @@ CONTEXT_text() if (CONTEXT_path_count > 0) CONTEXT_endpath(); - fprintf(gpoutfile, "setbounds currentpicture to unitsquare xyscaled (w,h); %% %g %g %g %g\n", xoffset, yoffset, xsize, ysize); + fprintf(gpoutfile, "setbounds currentpicture to unitsquare xyscaled (w,h);\n"); /* standalone graphic is a whole-page graphic */ - if(CONTEXT_params.standalone) { + if (CONTEXT_params.standalone) { fputs("\\stopGNUPLOTpage\n", gpoutfile); /* otherwise we define a MPgraphic to be included later */ } else { @@ -1207,7 +943,7 @@ TERM_PUBLIC void CONTEXT_graphics() { /* standalone graphic is a whole-page graphic */ - if(CONTEXT_params.standalone) { + if (CONTEXT_params.standalone) { fprintf(gpoutfile, "\\startGNUPLOTpage %% Graphic Nr. %d\n", ++CONTEXT_counter); /* otherwise we define a MPgraphic to be included later */ } else { @@ -1224,11 +960,12 @@ CONTEXT_graphics() * appropriate scaling factor 'a' to get the proper dimensions. */ fprintf(gpoutfile, "%% scaling factor, width and height of the figure\na := 1cm; w := %.3fa; h := %.3fa; %% (%g%s, %g%s)\n", - 0.001*(term->xmax), 0.001*(term->ymax), - CONTEXT_params.xsize.value, unit_name(CONTEXT_params.xsize.unit), - CONTEXT_params.ysize.value, unit_name(CONTEXT_params.ysize.unit)); + CONTEXT_params.xsize * ((CONTEXT_params.unit == INCHES) ? 2.54 : 1), /* cm */ + CONTEXT_params.ysize * ((CONTEXT_params.unit == INCHES) ? 2.54 : 1), /* cm */ + CONTEXT_params.xsize, (CONTEXT_params.unit == INCHES) ? "in" : "cm", + CONTEXT_params.ysize, (CONTEXT_params.unit == INCHES) ? "in" : "cm"); /* TODO: the following if-else could be slightly nicer */ - if(CONTEXT_images == CONTEXT_IMAGES_INLINE) { + if (CONTEXT_images == CONTEXT_IMAGES_INLINE) { fprintf(gpoutfile, "%% temporary variable for storing the path and images\nsave p, img, ima; path p; string img, ima;\n"); } else { fprintf(gpoutfile, "%% temporary variable for storing the path\nsave p; path p;\n"); @@ -1250,7 +987,7 @@ CONTEXT_graphics() /* since palette is initialized only once, subsequent plots wouldn't see it * unless we write it on the top of relevant plots explicitely */ - if(is_plot_with_palette()) { + if (is_plot_with_palette()) { CONTEXT_write_palette(CONTEXT_old_palette); } @@ -1275,12 +1012,12 @@ static void CONTEXT_endpath() { /* if we have a dot, draw only the dot */ - if(CONTEXT_path_is_dot) { + if (CONTEXT_path_is_dot) { fprintf(gpoutfile, "gp_dot(%.3fa,%.3fa);\n", 0.001*CONTEXT_posx, 0.001*CONTEXT_posy); CONTEXT_path_is_dot = 0; /* cyclic path, so let's end it with "--cycle" */ - } else if((CONTEXT_posx == CONTEXT_path_start_x) && (CONTEXT_posy == CONTEXT_path_start_y)) { + } else if ((CONTEXT_posx == CONTEXT_path_start_x) && (CONTEXT_posy == CONTEXT_path_start_y)) { fputs("--cycle;\ngp_draw(p);\n", gpoutfile); /* regular non-cyclic path */ @@ -1307,11 +1044,11 @@ TERM_PUBLIC void CONTEXT_move(unsigned int x, unsigned int y) { /* we seem to be there already */ - if((x == CONTEXT_posx) && (y == CONTEXT_posy)) + if ((x == CONTEXT_posx) && (y == CONTEXT_posy)) return; /* close and draw the current path before the move */ - if(CONTEXT_path_count > 0) + if (CONTEXT_path_count > 0) CONTEXT_endpath(); CONTEXT_posx = x; @@ -1355,24 +1092,23 @@ CONTEXT_vector(unsigned int x, unsigned int y) * - move(0,0), vector(0,0), whatever: draw a dot * - move(0,0), vector(0,0), vector (1,1): draw a line */ - if((x == CONTEXT_posx) && (y==CONTEXT_posy)) { + if ((x == CONTEXT_posx) && (y==CONTEXT_posy)) { /* as long as this is still a dot candidate, mark it so * however - further vector() commands may set this back to 0 */ - if(CONTEXT_path_count == 0) { + if (CONTEXT_path_count == 0) CONTEXT_path_is_dot = 1; - } /* if some path was already drawn up to this place, ignore (it's not a dot) */ return; } /* start the path if none is started yet */ - if(CONTEXT_path_count == 0) { + if (CONTEXT_path_count == 0) { /* the path is not a dot */ CONTEXT_path_is_dot = 0; CONTEXT_startpath(); } else { /* or prevent too long lines if you're in the middle of a path */ - if((CONTEXT_path_count % CONTEXT_LINEMAX) == 2) { + if ((CONTEXT_path_count % CONTEXT_LINEMAX) == 2) { fputs("\n ", gpoutfile); CONTEXT_path_count = 2; } @@ -1451,18 +1187,18 @@ CONTEXT_put_text(unsigned int x, unsigned int y, const char str[]) * Example: * -1 0 1 2: centered labels on x axis * if we had a space in front, positive numbers would be "centered" in a weird way*/ - for(s=str; s[0] == ' '; s++); + for (s = str; s[0] == ' '; s++); /* label position */ fprintf(gpoutfile, "gp_put_text((%.3fa, %.3fa), ", 0.001*x, 0.001*y); /* angle of rotation - optional and needed only if it's different from 0 */ - if (CONTEXT_ang != 0) { + if (CONTEXT_ang != 0) fprintf(gpoutfile, "angle(%d), ", CONTEXT_ang); - } + /* alignment - "center" is optional, but we'll add it anyway */ fprintf(gpoutfile, "align(%s), \\sometxt[gp]", alignments[alignment]); /* fontface/fontsize - optional second argument */ - if(CONTEXT_font_explicit[0] != 0) + if (CONTEXT_font_explicit[0] != 0) fprintf(gpoutfile, "[%s]", CONTEXT_font_explicit); /* finally the text label itself */ fprintf(gpoutfile, "{%s});\n", s); @@ -1478,8 +1214,8 @@ CONTEXT_put_text(unsigned int x, unsigned int y, const char str[]) TERM_PUBLIC int CONTEXT_text_angle(int ang) { - CONTEXT_ang = ang; - return TRUE; + CONTEXT_ang = ang; + return TRUE; } /* ********************** @@ -1491,8 +1227,8 @@ CONTEXT_text_angle(int ang) TERM_PUBLIC int CONTEXT_justify_text(enum JUSTIFY mode) { - CONTEXT_justify = mode; - return TRUE; + CONTEXT_justify = mode; + return TRUE; } /* *************** @@ -1515,7 +1251,7 @@ TERM_PUBLIC void CONTEXT_point (unsigned int x, unsigned int y, int number) { /* finish the current line first before the move */ - if(CONTEXT_path_count > 0) + if (CONTEXT_path_count > 0) CONTEXT_endpath(); fprintf(gpoutfile, "gp_point(%.3fa,%.3fa,%d);\n", 0.001*x, 0.001*y, number); @@ -1538,6 +1274,7 @@ CONTEXT_arrow (unsigned int sx, unsigned int sy, unsigned int ex, unsigned int e do_arrow(sx, sy, ex, ey, head); } + /* ------------------------ * CONTEXT_fontstring_parse * ------------------------ @@ -1546,109 +1283,32 @@ CONTEXT_arrow (unsigned int sx, unsigned int sy, unsigned int ex, unsigned int e * and copies the whole content to to_string except for the part * which starts with a numeric value. * - * That value is interpreted separately, saved into dimension - * and used for setting the proper font size. - * - * Only the latest valid dimension provided is stored. - * If dimension->value is 0, it means that no font size has been provided in the string + * That value is interpreted separately and saved to fontsize. * * to_size is the size of to_string which may not be exceeded while copying the string */ static void -CONTEXT_fontstring_parse(char *from_string, char *to_string, int to_size, dimension_t *dimension) +CONTEXT_fontstring_parse(char *from_string, char *to_string, int to_size, double *fontsize) { - int i; - char c; - char tmp_fontsize[MAX_ID_LEN+1], unit[MAX_ID_LEN+1]; - /* temporary place for storing dimenstion - - * we will only store the latest valid dimension - * and ignore any other fields starting with a number and not containing a valid dimension */ - dimension_t tmp_dimension; - double size; - - /* positions in input string between two commas (or start/stop) */ - int from_start=0, from_stop=0; - /* last used position in output string */ - int to_stop=0; - - /* font size zero is forbidden anyway; - * the output value zero means: no size has been set, so this field will be ignored */ - dimension->value = 0; - dimension->unit = UNIT_PT; - - /* on empty input just reset the font */ - if(from_string[0] == 0) { - to_string[0] = 0; - return; - } - /* let's walk from comma to comma (or from start to stop) - * and analyse content of each string separately */ - /* as long as we don't hit the end of the string */ - for (; (from_stop < strlen(from_string)) && (to_stop < to_size); from_start = ++from_stop) { - /* find next specification of font name/family/size/... between two commas */ - while (from_string[from_stop] != 0 && from_string[from_stop] != ',') from_stop++; - - /* if this looks like a real number (+ optional unit), parse interpret it as font size */ - if (from_string[from_start] == '.' || (from_string[from_start]+1 > '0' && from_string[from_start]-1 < '9')) { - /* copy the content to tmp_string first */ - for (i=0; i < sizeof(tmp_fontsize) && i < (from_stop-from_start); i++) { - tmp_fontsize[i] = from_string[from_start+i]; - } - tmp_fontsize[i] = 0; - /* parse the value & unit */ - unit[0] = 0; /* if no unit is given, we should get an empty string */ - sscanf(tmp_fontsize, "%lf%s", &size, unit); - tmp_dimension.value = size; - /* if no unit given - default to pt */ - if (unit == NULL || strlen(unit)==0) { - tmp_dimension.unit = UNIT_PT; - /* otherwise determine the unit first */ - } else { - tmp_dimension.unit = string_to_unit(unit); - } - /* pixel is not a valid unit for a font size */ - if (tmp_dimension.unit == UNIT_PX) - tmp_dimension.unit = UNIT_OTHER; - - /* - * store the size, but only if the size is valid and makes sense - * - * we want the font to be: - * - at least 3pt - * - at most half the height of the plot - */ - if(tmp_dimension.unit != UNIT_OTHER) { - if(convert_to_unit(tmp_dimension,UNIT_PT)*CONTEXT_params.scale_text > 3 && - convert_to_unit(tmp_dimension,UNIT_PT)*CONTEXT_params.scale_text < 0.5*convert_to_unit(CONTEXT_params.ysize,UNIT_PT)) { - dimension->value = tmp_dimension.value; - dimension->unit = tmp_dimension.unit; - } - } - - /* otherwise copy the content (almost) literally - * from_stop can be a comma or 0 - in either case we simply copy that one as well */ - } else { - for (i=from_start; i < from_stop+1; i++) { - c = from_string[i]; - /* TODO: improve this filter - remove any other dangerous characters - * Do we need to remove {} ? */ - if (!(c==']' || c=='[' || c=='$' || c=='#' || - c=='%' || c=='^' || c=='_' || c=='`' || - c=='\\' || c=='\n' || c=='\t' || c=='\r')) { - /* we may only write up to the pre-last character of to_string */ - if (to_stop < to_size) { - to_string[to_stop++] = c; - } - } - } - } + double tmp_fontsize = 0.; + char * comma; + size_t n; + + comma = strrchr(from_string, ','); + if (comma) { + sscanf(comma + 1, "%lf", &tmp_fontsize); + n = comma - from_string; + } else { + n = strlen(from_string); } - - /* it might have happened that the last character we wrote was a comma instead of 0, - * so we have to make sure that we finish the string */ - to_string[to_stop-1] = 0; + *fontsize = tmp_fontsize; + if (n >= to_size) + n = to_size - 1; + memcpy(to_string, from_string, n); + to_string[n] = NUL; } + /* ------------------------- * CONTEXT_adjust_dimensions * ------------------------- @@ -1661,14 +1321,19 @@ static void CONTEXT_adjust_dimensions() { /* sets vertical dimension of characters based on current fontsize in pt */ - term->v_char = (unsigned int)((double)CONTEXT_DPI * - convert_to_unit(CONTEXT_fontsize,UNIT_IN) * CONTEXT_params.scale_text); + term->v_char = (unsigned int)round((double)CONTEXT_DPI * + CONTEXT_fontsize / 72.27 * CONTEXT_params.scale_text); /* based on proportions of LM digits at 12pt */ - term->h_char = (unsigned int)(CONTEXT_LM_H_TO_V_RATIO*term->v_char); + term->h_char = (unsigned int)round(CONTEXT_LM_H_TO_V_RATIO * term->v_char); /* we might want to fix CONTEXT_DPI in case that the figure becomes too big */ - term->xmax = (unsigned int)((double)CONTEXT_DPI * convert_to_unit(CONTEXT_params.xsize,UNIT_IN) ); - term->ymax = (unsigned int)((double)CONTEXT_DPI * convert_to_unit(CONTEXT_params.ysize,UNIT_IN) ); + if (CONTEXT_params.unit == INCHES) { + term->xmax = (unsigned int)round((double)CONTEXT_DPI * CONTEXT_params.xsize); + term->ymax = (unsigned int)round((double)CONTEXT_DPI * CONTEXT_params.ysize); + } else { + term->xmax = (unsigned int)round((double)CONTEXT_DPCM * CONTEXT_params.xsize); + term->ymax = (unsigned int)round((double)CONTEXT_DPCM * CONTEXT_params.ysize); + } } /* ***************** @@ -1678,16 +1343,15 @@ CONTEXT_adjust_dimensions() * Official description: * - empty string restores the terminal's default font * - fonts are selected as strings "name,size", - * where size should be a floating point number by default, interpreted as "pt" (point) + * where size should be a floating point number interpreted as "pt" (point) * * It's the user's own responsibility to make sure that the proper typescripts * are included in the header, else the selected font won't work out-of-the-box * * The ConTeXt terminal should support things such as - * "iwona,12pt,ss" (sans serif) or - * "palatino,10dd" (10 didot points) or - * "tt" (typewriter) or - * as well + * "iwona,ss,12" (iwona sans serif) or + * ",10" (10 points) or + * "tt" (typewriter) * * The routine saves font name to CONTEXT_font * and fontsize to CONTEXT_fontsize. @@ -1705,20 +1369,17 @@ CONTEXT_set_font(const char *font) strncpy(CONTEXT_font_explicit, CONTEXT_font, sizeof(CONTEXT_font_explicit)); /* valid fontsize has been provided */ - if (CONTEXT_fontsize_explicit.value > 0) { /* XXX: if valid */ + if (CONTEXT_fontsize_explicit > 0.) { /* XXX: if valid */ - CONTEXT_fontsize.value = CONTEXT_fontsize_explicit.value; - CONTEXT_fontsize.unit = CONTEXT_fontsize_explicit.unit; + CONTEXT_fontsize = CONTEXT_fontsize_explicit; - snprintf(tmp_fontstring, sizeof(tmp_fontstring), - ",%g%s", CONTEXT_fontsize_explicit.value, unit_name(CONTEXT_fontsize_explicit.unit) - ); - strncat(CONTEXT_font_explicit, tmp_fontstring, sizeof(CONTEXT_font_explicit)-strlen(CONTEXT_font_explicit)); + snprintf(tmp_fontstring, sizeof(tmp_fontstring), ",%gpt", CONTEXT_fontsize_explicit); + strncat(CONTEXT_font_explicit, tmp_fontstring, sizeof(CONTEXT_font_explicit) - strlen(CONTEXT_font_explicit)); + tmp_fontstring[MAX_ID_LEN] = NUL; /* no fontsize has been provided: switch back to default terminal fontsize */ - } else if (CONTEXT_fontsize_explicit.value == 0) { - CONTEXT_fontsize.value = CONTEXT_params.fontsize.value; - CONTEXT_fontsize.unit = CONTEXT_params.fontsize.unit; + } else if (CONTEXT_fontsize_explicit == 0) { + CONTEXT_fontsize = CONTEXT_params.fontsize; } /* tell to gnuplot how big the fonts in labels are */ @@ -1744,13 +1405,13 @@ CONTEXT_pointsize(double pointsize) * but I see no reason why one shouldn't be able to play with * inverted point shapes, so finally I commented this out again * - * if(pointsize < 0) + * if (pointsize < 0) * pointsize = 1; */ - if(CONTEXT_old_pointsize != pointsize) { + if (CONTEXT_old_pointsize != pointsize) { /* close and draw the current path first */ - if (CONTEXT_path_count>0) + if (CONTEXT_path_count > 0) CONTEXT_endpath(); fprintf(gpoutfile, "gp_set_pointsize(%.3f);\n", pointsize); CONTEXT_old_pointsize = pointsize; @@ -1809,15 +1470,15 @@ CONTEXT_fill(int style) /* used in FS_[TRANSPARENT_]SOLID --> fill with intensity according to filldensity it extracts a percentage out of "style" */ density = (style >> 4); - if(density < 0) + if (density < 0) density = 0; - if(density > 100) + if (density > 100) density = 100; fputs("gp_fill(p", gpoutfile); /* do some strange trickery */ - switch(style & 0xf) { + switch (style & 0xf) { case FS_DEFAULT: break; @@ -1828,9 +1489,8 @@ CONTEXT_fill(int style) case FS_SOLID: /* fill the box with density "density": if no parameter density is specified, it implies 100% density by default */ - if (density < 100) { + if (density < 100) fprintf(gpoutfile, ",density(%.2f)", 0.01*density); - } break; case FS_TRANSPARENT_PATTERN: @@ -1861,12 +1521,12 @@ CONTEXT_fill(int style) TERM_PUBLIC void CONTEXT_linewidth(double linewidth) { - if(linewidth < 0) + if (linewidth < 0) linewidth = 1.0; if (CONTEXT_old_linewidth != linewidth) { /* close and draw the current path first */ - if (CONTEXT_path_count>0) + if (CONTEXT_path_count > 0) CONTEXT_endpath(); fprintf(gpoutfile, "gp_set_linewidth(%.3f);\n", linewidth); CONTEXT_old_linewidth = linewidth; @@ -1886,15 +1546,17 @@ CONTEXT_write_palette_gradient(gradient_struct *gradient, int cnt) /* i-th color */ fprintf(gpoutfile, "colors("); - for (i=0; i0) { fprintf(gpoutfile, ","); } + for (i = 0; i < cnt; i++) { + if (i > 0) + fprintf(gpoutfile, ","); fprintf(gpoutfile, "(%.3g,%.3g,%.3g)", gradient[i].col.r, gradient[i].col.g, gradient[i].col.b); } /* position of the i-th color */ fprintf(gpoutfile, ");positions("); - for (i=0; i0) { fprintf(gpoutfile, ","); } + for (i = 0; i < cnt; i++) { + if (i > 0) + fprintf(gpoutfile, ","); fprintf(gpoutfile, "%.4g", gradient[i].pos); } fprintf(gpoutfile, ")"); @@ -1906,11 +1568,10 @@ CONTEXT_write_palette_gradient(gradient_struct *gradient, int cnt) * */ static void -CONTEXT_write_palette (t_sm_palette *palette) +CONTEXT_write_palette(t_sm_palette *palette) { - if (palette == NULL) { + if (palette == NULL) return; - } /* TODO // Color models: RGB, HSV, CMY, YIQ, XYZ @@ -1926,7 +1587,7 @@ CONTEXT_write_palette (t_sm_palette *palette) // http://cs.fit.edu/wds/classes/cse5255/cse5255/davis/index.html */ fprintf(gpoutfile, "gp_make_palette("); - switch(sm_palette.colorMode) { + switch (sm_palette.colorMode) { /* grayscale only */ case SMPAL_COLOR_MODE_GRAY: fprintf(gpoutfile, "color_mode(gray)"); @@ -1961,21 +1622,20 @@ CONTEXT_write_palette (t_sm_palette *palette) * ********************* * * 1. if palette==NULL, then return nice/suitable - * maximal number of colours supported by this terminal. - * Returns 0 if it can make colours without palette (like - * postscript). + * maximal number of colours supported by this terminal. + * Returns 0 if it can make colours without palette (like + * postscript). * 2. if palette!=NULL, then allocate its own palette - * return value is undefined + * return value is undefined * 3. available: some negative values of max_colors for whatever - * can be useful + * can be useful */ TERM_PUBLIC int -CONTEXT_make_palette (t_sm_palette *palette) +CONTEXT_make_palette(t_sm_palette *palette) { - if (palette == NULL) { + if (palette == NULL) return 0; /* ConTeXt can do continuous colors */ - } - + /* save the palette */ CONTEXT_old_palette = palette; @@ -2002,7 +1662,7 @@ CONTEXT_make_palette (t_sm_palette *palette) * } t_colorspec; */ TERM_PUBLIC void -CONTEXT_set_color (t_colorspec *colorspec) +CONTEXT_set_color(t_colorspec *colorspec) { double gray, r, g, b; /* ConTeXt doesn't offer full support for palettes yet @@ -2020,7 +1680,7 @@ CONTEXT_set_color (t_colorspec *colorspec) Set current color to the rgb triple given in colorspec->lt. */ /* close and draw the current path first */ - if(CONTEXT_path_count > 0) + if (CONTEXT_path_count > 0) CONTEXT_endpath(); switch (colorspec->type) { @@ -2048,8 +1708,8 @@ CONTEXT_set_color (t_colorspec *colorspec) gray = colorspec->value; /* limit negative and >1 values to [0:1] first */ - if(gray < 0) gray = 0; - if(gray > 1) gray = 1; + if (gray < 0) gray = 0; + if (gray > 1) gray = 1; /* TODO: if ConTeXt start supporting palettes, we'll uncomment the following: */ fprintf(gpoutfile, "%%gp_set_color(frac(%.4f));\n", gray); @@ -2073,29 +1733,27 @@ CONTEXT_set_color (t_colorspec *colorspec) * Draws a polygon with the fill color set by set_color, and no border. */ TERM_PUBLIC void -CONTEXT_filled_polygon (int points, gpiPoint *corners) +CONTEXT_filled_polygon(int points, gpiPoint *corners) { int i; /* nothing to be filled if less than 3 points */ - if(points < 3) + if (points < 3) return; /* close and draw the current path first */ - if(CONTEXT_path_count > 0) { + if (CONTEXT_path_count > 0) CONTEXT_endpath(); - } /* if the first point equals the last one, skip the last point; --cycle does that already * this condition is probably always true in gnuplot, so the if condition may not be needed */ - if((corners[0].x == corners[points-1].x) && (corners[0].y == corners[points-1].y)) { + if ((corners[0].x == corners[points-1].x) && (corners[0].y == corners[points-1].y)) points--; - } /* create new path with corners */ fputs("p := ", gpoutfile); fprintf(gpoutfile, "(%.3fa,%.3fa)", 0.001*corners[0].x, 0.001*corners[0].y); - for (i=1; i 0.750 and is not implemented at all + * two possible approaches: drawing rectangles & creating proper image + * it might require one additional level of abstraction like gp_image(...) */ TERM_PUBLIC void -CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imagecolor color_mode) +CONTEXT_image(unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imagecolor color_mode) { - int i, j, k, line_length, components_per_color; - unsigned char byte; + int i, k, line_length, components_per_color; rgb_color color; TBOOLEAN is_clipped = FALSE; - if((corner[2].x > corner[0].x) || (corner[0].y > corner[2].y) || - (corner[1].x > corner[3].x) || (corner[3].y > corner[1].y)) { + if ((corner[2].x > corner[0].x) || (corner[0].y > corner[2].y) || + (corner[1].x > corner[3].x) || (corner[3].y > corner[1].y)) is_clipped = TRUE; - } - if(CONTEXT_images == CONTEXT_IMAGES_EXTERNAL) { + if (CONTEXT_images == CONTEXT_IMAGES_EXTERNAL) { #ifdef WRITE_PNG_IMAGE /* we reserved 10 extra bytes, so we can afford images up to 9999 */ snprintf(CONTEXT_image_filename + CONTEXT_image_filename_length, 9, "_%02d.png", ++CONTEXT_image_counter); write_png_image (M, N, image, color_mode, CONTEXT_image_filename); - if(is_clipped) { + if (is_clipped) fprintf(gpoutfile, "draw image(\n "); - } fprintf(gpoutfile, "externalfigure \"%s\" xyscaled (%.3fa,%.3fa) shifted (%.3fa,%.3fa);\n", CONTEXT_image_filename + CONTEXT_image_filename_start, 0.001*(corner[1].x-corner[0].x), 0.001*(corner[0].y-corner[1].y), 0.001*corner[0].x, 0.001*corner[1].y); - if(is_clipped) { + if (is_clipped) { fprintf(gpoutfile, " clip currentpicture to unitsquare xyscaled (%.3fa,%.3fa) shifted (%.3fa,%.3fa););\n", 0.001*(corner[3].x-corner[2].x), 0.001*(corner[2].y-corner[3].y), 0.001*corner[2].x, 0.001*corner[3].y); } @@ -2187,8 +1850,8 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag /* write the image data */ fprintf(gpoutfile, "img := \"%%\n"); line_length = 0; - for(i=0; i= 16) { + for (i = 0; i < M * N; i++) { + if (line_length++ >= 16) { line_length = 1; fprintf(gpoutfile, "%%\n"); } @@ -2208,12 +1871,12 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag /* write the image data */ fprintf(gpoutfile, "img := \"%%\n"); line_length = 0; - for(i=0; i= 16) { + for (i = 0; i < M * N; i++) { + if (line_length++ >= 16) { line_length = 1; fprintf(gpoutfile, "%%\n"); } - for(k=0; k<3; k++) { + for (k = 0; k < 3; k++) { fprintf(gpoutfile, "%02x", (unsigned char)(image[i*components_per_color+k]*255)); } } @@ -2223,8 +1886,8 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag if (color_mode == IC_RGBA) { fprintf(gpoutfile, "ima := \"%%\n"); line_length = 0; - for(i=0; i= 3*16) { + for (i = 0; i < M * N; i++) { + if (line_length++ >= 3*16) { line_length = 1; fprintf(gpoutfile, "%%\n"); }; @@ -2235,12 +1898,11 @@ CONTEXT_image (unsigned M, unsigned N, coordval *image, gpiPoint *corner, t_imag } /* TODO: transparency handling is not yet supported in ConTeXt */ - if(is_clipped) { + if (is_clipped) fprintf(gpoutfile, "draw image(\n "); - } fprintf(gpoutfile, "draw bitmapimage (%u,%u,img) xyscaled (%.3fa,%.3fa) shifted (%.3fa,%.3fa);\n", N, M, 0.001*(corner[1].x-corner[0].x), 0.001*(corner[0].y-corner[1].y), 0.001*corner[0].x, 0.001*corner[1].y); - if(is_clipped) { + if (is_clipped) { fprintf(gpoutfile, " clip currentpicture to unitsquare xyscaled (%.3fa,%.3fa) shifted (%.3fa,%.3fa););\n", 0.001*(corner[3].x-corner[2].x), 0.001*(corner[2].y-corner[3].y), 0.001*corner[2].x, 0.001*corner[3].y); } @@ -2303,7 +1965,7 @@ TERM_TABLE_END(context_driver) #ifdef TERM_HELP START_HELP(context) -"1 ConTeXt", +"1 context", "?commands set terminal context", "?set terminal context", "?terminal context", @@ -2322,7 +1984,7 @@ START_HELP(context) " Syntax:", " set term context {default}", " {defaultsize | size |", -" size {in|cm}, {in|cm}}", +" size {in|cm}, {in|cm}}", " {input | standalone}", " {timestamp | notimestamp}", " {noheader | header \"
\"}", @@ -2335,7 +1997,7 @@ START_HELP(context) " {fontscale }", " {mppoints | texpoints}", " {inlineimages | externalimages}", -" {defaultfont | font \"{,}\"}", +" {defaultfont | font \"{}{,}\"}", "", " In non-standalone (`input`) graphic only parameters `size` to select graphic", " size, `fontscale` to scale all the labels for a factor ", @@ -2398,28 +2060,24 @@ START_HELP(context) " In non-standalone (`input`) mode only the font size is important", " to reserve enough space for text labels.", " The command" -" set term context font \"myfont,ss,10pt\"", +" set term context font \"myfont,ss,10\"", " will result in", " \\setupbodyfont[myfont,ss,10pt]", " If you additionaly set `fontscale` to 0.8 for example,", " then the resulting font will be 8pt big and", -" set label ... font \"myfont,12pt\"", +" set label ... font \"myfont,12\"", " will come out as 9.6pt.\n", -" For compatibility with other terminals font size may be specified without units,", -" in which case the default unit \"pt\" will be appended to it, but you can also", -" use any other supported unit, for example \"11dd\".\n", " It is your own responsibility to provide proper typescripts (and header),", " otherwise switching the font will have no effect.\n", " For a standard font in ConTeXt MKII (pdfTeX) you could use:", " set terminal context standalone header '\\usetypescript[iwona][ec]' \\", -" font \"iwona,ss,11pt\"", +" font \"iwona,ss,11\"", " Please take a look into ConTeXt documentation, wiki or mailing list (archives)", " for any up-to-date information about font usage." "", " Examples:", -" set terminal context size 1.5 # 7.5in, 4.5in", " set terminal context size 10cm, 5cm # 10cm, 5cm", -" set terminal context size 4/5, 2.54*4cm # 4in, 4in", +" set terminal context size 4in, 3in # 4in, 3in", " For standalone (whole-page) plots with labels in UTF-8 encoding:", " set terminal context standalone header '\\enableregime[utf-8]'", "", /* TODO: LaTeX formatting */ @@ -2430,7 +2088,7 @@ START_HELP(context) "^ ", " and a recent version of ConTeXt.", " If you want to call gnuplot on-the-fly, you also need write18 enabled.", -" In most TeX distributionts this can be set with shell_escape=t in texmf.cnf.", +" In most TeX distributions this can be set with shell_escape=t in texmf.cnf.", "", " See", "^ ",