ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Jens-Uwe Morawski <morawski@gmx.net>
Subject: Re: Re[4]: MetaTricks?
Date: Fri, 7 Feb 2003 17:42:29 +0100	[thread overview]
Message-ID: <20030207174229.4a406678.morawski@gmx.net> (raw)
In-Reply-To: <592730426.20030206233514@iol.it>

[-- Attachment #1: Type: text/plain, Size: 1075 bytes --]

On Thu, 6 Feb 2003 23:35:14 +0100
Giuseppe Bilotta <gip.bilotta@iol.it> wrote:

> Thursday, February 6, 2003 Jens-Uwe Morawski wrote:
> 
> JUM> as you know, PSTricks is a huge package, so where to start?
> 
 
> In particular, there are a few things that I currently see as
> "missing" in (plain) MetaPost (and which I couldn't find in
> MetaFun either); for example, there is no macro that allows to
> "add" drawoptions; you can only replace them altogether. PSTricks,
> OTOH (at least for what I could see) has a default color, pensize
> and line style which can be changed singularly (MetaPost seems to
> only do this for the pen --currentpen; it doesn't have a
> currentcolor or currentstyle).

Do you mean something like in the attached files?
keyvalmp.mp : package for key-value parameters in MP; used in mpt-conf.mp
              (BTW, if anybody knows a better way to implement this, please
               let me know)
mptricks.mp : base MPTricks package
mpt-conf.mp : MPTricks module that implements the requested feature
mpt-test.mp : the example file

Best,
  Jens

[-- Attachment #2: keyvalmp.mp --]
[-- Type: application/octet-stream, Size: 5794 bytes --]

%H  package name: keyvalMP
%H  description:  support for key-value style parameters
%H  file:         keyvalmp.mp
%H  author:       Jens-Uwe Morawski <morawski@gmx.net>
%H  version:      0.0.1 2002/10/19

%L  Copyright 2002, Jens-Uwe Morawski
%L  License: LPPL 1.0, see LEGAL

%I basic macros:
%I   - declareBooleanKeys_kv, declareNumericKeys_kv,
%I     declarePairKeys_kv, declareStringKeys_kv,
%I     declarePathKeys_kv, declarePictureKeys_kv,
%I     declareColorKeys_kv, declarePenKeys_kv 
%I   - declareBooleanSyns_kv, declareNumericSyns_kv,
%I     declarePairSyns_kv, declareStringSyns_kv,
%I     declarePathSyns_kv, declarePictureSyns_kv,
%I     declareColorSyns_kv
%I   - processKeyvals_kv
%I   - ifSetKey_kv, ifUnsetKey_kv, ifValidValue_kv
%I additional macros:
%I   - forRangeOfNumerics_kv

%D initialisation
if known keyval_module: endinput ; fi ;
boolean keyval_module ; keyval_module := true ;

%D
%D declaration of keyval variables
%D

def declareBooleanKeys_kv(text _keys_) =
	forsuffixes $=_keys_: save $ ; boolean $ ; endfor;
enddef;

def declareNumericKeys_kv(text _keys_) =
	forsuffixes $=_keys_: save $ ; numeric $ ; endfor;
enddef;

def declarePairKeys_kv(text _keys_) =
	forsuffixes $=_keys_: save $ ; pair $ ; endfor;
enddef;

def declarePenKeys_kv(text _keys_) =
	forsuffixes $=_keys_: save $ ; pen $ ; endfor;
enddef;

def declarePathKeys_kv(text _keys_) =
	forsuffixes $=_keys_: save $ ; path $ ; endfor;
enddef;

def declareColorKeys_kv(text _keys_) =
	forsuffixes $=_keys_: save $ ; color $ ; endfor;
enddef;

def declareStringKeys_kv(text _keys_) =
	forsuffixes $=_keys_: save $ ; string $ ; endfor;
enddef;

%D
%D declaration of keyval synonyms
%D

def declareBooleanSyns_kv(text _syns_)(text _vals_) =
	forsuffixes $=_syns_: save $ ; boolean $ ; endfor;
	processKeyvals_kv(_vals_) ;
enddef;

def declareNumericSyns_kv(text _syns_)(text _vals_) =
	forsuffixes $=_syns_: save $ ; numeric $ ; endfor;
	processKeyvals_kv(_vals_) ;
enddef;

def declarePairSyns_kv(text _syns_)(text _vals_) =
	forsuffixes $=_syns_: save $ ; pair $ ; endfor;
	processKeyvals_kv(_vals_) ;
enddef;

def declarePathSyns_kv(text _syns_)(text _vals_) =
	forsuffixes $=_syns_: save $ ; path $ ; endfor;
	processKeyvals_kv(_vals_) ;
enddef;

def declareColorSyns_kv(text _syns_)(text _vals_) =
	forsuffixes $=_syns_: save $ ; color $ ; endfor;
	processKeyvals_kv(_vals_) ;
enddef;


def declareStringSyns_kv(text _syns_)(text _vals_) =
	forsuffixes $=_syns_: save $ ; string $ ; endfor;
	processKeyvals_kv(_vals_) ;
enddef;


%D
%D the macros above do not contain commands for
%D variables of type 'picture'. The problem is
%D that the command that process the keyval-parameters
%D changes the definition of symbols ',','(' and ')',
%D but they might be used in a picture definition in
%D unexpected way.
%D Therefore it is possible to pass variable-names
%D but no picture-expression:
%D
%D picture pic ; pic:=image(...) and the usage as
%D keyvalFunc(logo=pic)
%D
%D will work. But
%D keyvalFunc(logo=image(...)) doesn't work as well as
%D keyvalFunc(logo=btex hallo etex)
%D
%D Thus, use keys of type picture only to get a picture
%D by variable-name. Since pictures are not really useful
%D as keys, i hope this is not a big lack in this package.

def declarePictureKeys_kv(text _keys_) =
	forsuffixes $=_keys_: save $ ; picture $ ; endfor;
enddef;

def declarePictureSyns_kv(text _syns_)(text _vals_) =
	forsuffixes $=_syns_: save $ ; picture $ ; endfor;
	processKeyvals_kv(_vals_) ;
enddef;

%D keyval processing
%D 
%D the following command could be really short and simple:
%D
%D def processKeyvals_kv (text kvps)=
%D	begingroup;
%D	save , ; let ,=; ;
%D	kvps ;
%D	endgroup;
%D enddef;
%D
%D this makes a list like:    'bla=1,bloob=2,foo="hey"'
%D work when executed like:   'bla=1;bloob=2;foo="hey"'
%D
%D This looks like MetaPost. The problem are values of 
%D type pair and color, since they include commas (1,2)
%D or (0.1,0.4,0.2).
%D
%D so the macro became a little bit longer and more complex

def processKeyvals_kv (text _kvps_)=
	begingroup;
	
	save _co ; let _co=, ;		
	save _lb ; let _lb=( ;
	save _rb ; let _rb=) ;

	save _lbrace_kv, _rbrace_kv, _comma_kv ;
	
	primarydef l _comma_kv r =
		begingroup
		save , ; let ,=_co ;
		save ) ; let )=_rb ;
		save c ;
		if (pair l):
			((xpart l),(ypart l), r) endgroup
		elseif (numeric l):
			(l,r) endgroup
		else
			errmessage "keyvalMP: don't use macros on key-value parameters"
		fi
	enddef;
	
	def _lbrace_kv =
		begingroup
		save , ; let ,=_comma_kv ;
		save ( ; let (=_lb ;
	enddef;
	
	def _rbrace_kv = endgroup enddef;
	
	save ( ; let (=_lbrace_kv ;
	save ) ; let )=_rbrace_kv ;
	
	save , ; let ,=; ;
	
	_kvps_ ;
	endgroup;
enddef;


%D
%D evaluate keyval settings
%D

def ifSetKey_kv(suffix _key_)(text _commands_)=
	if known _key_: _commands_ ; fi;
enddef;

def ifUnsetKey_kv(suffix _key_)(text _commands_)=
	if unknown _key_: _commands_ ; fi;
enddef;

def ifValidValue_kv(text _validvals_)(suffix _key_)(text _commands_) =
	begingroup;
	save _isvalid_ ; boolean _isvalid_ ; _isvalid_ := false ;
	if known _key_:
		for v=_validvals_:
			if _key_=v: _isvalid_:= true ; fi ;
		endfor;
		
		if _isvalid_:
			_commands_ ;
		else:
			errmessage ("No valid parameter given for " & (str _key_)) ;
		fi;
	fi ;
	endgroup;
enddef;


%D
%D  additional macros
%D

%D
%D expands numeric list like 1,3-6,8 to 1,3,4,5,6,8
%D

def forRangeOfNumerics_kv(text _nl_)(text _commands_) =
	begingroup;
	save kv_minus_, _minus_kv ; let kv_minus_=- ;
	
	primarydef l _minus_kv r =
		if l<r:
			for i=l upto r:
				i,
			endfor
		else:
			for i=l step kv_minus_1 until r:
				i,
			endfor		
		fi
	enddef;
	
	save - ; let -=_minus_kv ; 
	for i=_nl_:
		begingroup;
		save - ; let -=kv_minus_ ;
		_commands_ ;
		endgroup;
	endfor;
	endgroup;
enddef;

[-- Attachment #3: mpt-conf.mp --]
[-- Type: application/octet-stream, Size: 2470 bytes --]

% MPTricks - configuration macros



%%% init
if known mptricks_conf_module: endinput ; fi ;

boolean mptricks_conf_module ; mptricks_conf_module:=true ;
string mptricks_conf_version ; mptricks_conf_version:="0.0.1" ;

%%% dependencies
message_mpt("Loading keyval package") ; input keyvalmp ;


%%% attribute based drawoptions

boolean mpt_drawattributeState, mpt_drawattributeDashedState ;
picture mpt_drawattributeDashPattern ;
pen     mpt_drawattributePen ;
color   mpt_drawattributeColor ;

mpt_drawattributeState       := false ;
mpt_drawattributeDashedState := false ;
mpt_drawattributeDashPattern := evenly ;
mpt_drawattributePen         := currentpen ;
mpt_drawattributeColor       := black ;


def definePen_mpt(suffix p)(text t)=
  begingroup;
    save currentpen ; pen currentpen ;
    save pen_lft, pen_rt, pen_top, pen_bot, currentpen_path ;
    numeric pen_lft, pen_rt, pen_top, pen_bot ;
    path currentpen_path ;
    
    pen p ;
    pickup t ;
    p:=currentpen ;
  endgroup ;
enddef;

def defineDashPattern_mpt(suffix p)(text t)=
  picture p ;
  p:= t ;
enddef;

def setupDrawingAttributes_mpt(text kvps) =
  begingroup;

    save state_has_been_changed ; boolean state_has_been_changed ;
    state_has_been_changed := false ;

    save exec_drawoptions ; string exec_drawoptions ;
    exec_drawoptions:="drawoptions(" ;
    
    declareBooleanKeys_kv(State,Style) ;
    declareBooleanSyns_kv(Start,Stop)(Start=true,Stop=false) ;
    declareBooleanSyns_kv(Stroked,Dashed)(Stroked=false,Dashed=true) ;
    declareColorKeys_kv(Color) ;
    declarePictureKeys_kv(Pattern) ;
    declarePenKeys_kv(Pen) ;

    processKeyvals_kv(kvps) ;

    ifValidValue_kv(Start,Stop)(State)(
      mpt_drawattributeState:=State; state_has_been_changed:=true) ;
    ifValidValue_kv(Stroked,Dashed)(Style)(
      mpt_drawattributeDashedState:=Style) ;

    ifSetKey_kv(Color)(mpt_drawattributeColor:=Color) ;
    ifSetKey_kv(Pattern)(mpt_drawattributeDashPattern:=Pattern) ;
    ifSetKey_kv(Pen)(mpt_drawattributePen:=Pen) ;

    if state_has_been_changed and (not mpt_drawattributeState):
      drawoptions();
    fi;

    if mpt_drawattributeState:
      if mpt_drawattributeDashedState:
	exec_drawoptions:= exec_drawoptions &
	  "dashed mpt_drawattributeDashPattern ";
      fi;
      exec_drawoptions:= exec_drawoptions &
          "withpen mpt_drawattributePen withcolor mpt_drawattributeColor )";
      scantokens exec_drawoptions ;
    fi;
    
  endgroup;
enddef;


[-- Attachment #4: mpt-test.mp --]
[-- Type: application/octet-stream, Size: 736 bytes --]



input mptricks ;

definePen_mpt(MyPen)(pencircle scaled 8pt)

definePen_mpt(SquarePen)(pensquare scaled 6pt rotated 45)

defineDashPattern_mpt(MyPattern)(withdots) ;


path p ; p := origin--(8cm,0) ;

beginfig(1);

  draw p;
  
  setupDrawingAttributes_mpt(
    State=Start,
    Style=Dashed,
    Pen=MyPen,
    Pattern=MyPattern,
    Color=red) ;
  
  draw  p shifted (0,1cm) ;

  setupDrawingAttributes_mpt(Pen=SquarePen,Color=green) ;

  draw p shifted (0,2cm) ;

  setupDrawingAttributes_mpt(State=Stop,Pen=MyPen) ;
  
  draw p shifted (0,3cm) ;

  setupDrawingAttributes_mpt(State=Start,Style=Stroked) ;

  draw p shifted (0,4cm) ;

  setupDrawingAttributes_mpt(Style=Dashed,Color=blue) ;

  draw p shifted (0,5cm) ;
endfig;
end

[-- Attachment #5: mptricks.mp --]
[-- Type: application/octet-stream, Size: 339 bytes --]

% MPTricks

%%% init
if known mptricks_module: endinput ; fi ;
boolean mptricks_module ; mptricks_module:=true ;
string mptricks_version ; mptricks_version:="0.0.1" ;

string mpt_messagePrefix ; mpt_messagePrefix:="MPTricks: " ;
def message_mpt(expr msg) =
  message(mpt_messagePrefix & msg) ;
enddef;

%%% dependencies
input mpt-conf ;



  parent reply	other threads:[~2003-02-07 16:42 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-05 21:38 MetaTricks? Giuseppe Bilotta
2003-02-06 13:16 ` MetaTricks? Jens-Uwe Morawski
2003-02-06 14:29   ` Re[2]: MetaTricks? Giuseppe Bilotta
2003-02-06 16:49     ` Jens-Uwe Morawski
2003-02-06 22:35       ` Re[4]: MetaTricks? Giuseppe Bilotta
2003-02-06 23:03         ` MetaTricks? Denis Roegel
2003-02-06 23:14           ` Re[2]: MetaTricks? Giuseppe Bilotta
2003-02-07 16:42         ` Jens-Uwe Morawski [this message]
2003-02-07 20:34           ` Re[6]: MetaTricks? Giuseppe Bilotta
2003-02-08  2:20             ` Jens-Uwe Morawski
2003-02-08 16:21               ` Denis Roegel
2003-02-08 19:23                 ` Hans Hagen
2003-02-08 23:18                   ` Jens-Uwe Morawski
2003-02-09 18:18                     ` Hans Hagen
2003-02-10 22:36                       ` Jens-Uwe Morawski
2003-02-12 12:25                         ` Hans Hagen
2003-02-12 14:25                           ` Jens-Uwe Morawski
2003-02-12 18:28                           ` Denis Roegel
2003-02-13  0:22                             ` Jens-Uwe Morawski
2003-02-13  0:58                               ` Denis Roegel
2003-02-13 10:51                                 ` Jens-Uwe Morawski
2003-02-13 10:52                                 ` Hans Hagen
2003-02-13 10:53                               ` Hans Hagen
2003-02-08 20:07                 ` Jens-Uwe Morawski
2003-02-08 21:10                   ` Denis Roegel
2003-02-09 13:14                     ` Jens-Uwe Morawski
2003-02-09 18:30                       ` Denis Roegel
2003-02-10 15:05                         ` Jens-Uwe Morawski
2003-02-08 20:00           ` Re[4]: MetaTricks? Hans Hagen
2003-02-10 15:36             ` Jens-Uwe Morawski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030207174229.4a406678.morawski@gmx.net \
    --to=morawski@gmx.net \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).