| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
6.3 Pens
In Asymptote, pens provide a context for the four basic drawing
commands (see section Drawing commands). They are used to specify the
following drawing attributes: color, line type, line width, line cap,
line join, fill rule, text alignment, font, font size, pattern,
overwrite mode, and calligraphic transforms on the pen nib. The
default pen used by the drawing routines is called
currentpen. This provides the same functionality as the
MetaPost command pickup.
The implicit initializer for pens is defaultpen.
Pens may be added together with the nonassociative binary
operator +. This will add the colors of the two pens.
All other non-default attributes of the rightmost pen will
override those of the leftmost pen. Thus, one can obtain a yellow
dashed pen by saying dashed+red+green or red+green+dashed
or red+dashed+green. The binary operator *
can be used to scale the color of a pen by a real number, until it
saturates with one or more color components equal to 1.
- Colors are specified using one of the following colorspaces:
pen gray(real g);-
This produces a grayscale color, where the intensity
glies in the interval [0,1], with 0.0 denoting black and 1.0 denoting white. pen rgb(real r, real g, real b);-
This produces an RGB color, where each of the red, green, and blue intensities
r,g,b, lies in the interval [0,1]. pen cmyk(real c, real m, real y, real k);-
This produces a CMYK color, where each of the cyan, magenta, yellow, and black intensities
c,m,y,k, lies in the interval [0,1]. pen invisible;-
This special pen writes in invisible ink, but adjusts the bounding box as if something had been drawn (like the
\phantomcommand in TeX). The functionbool invisible(pen)can be used to test whether a pen is invisible.
The default color is
black; this may be changed with the routinedefaultpen(pen). The functioncolorspace(pen p)returns the colorspace of penpas a string ("gray","rgb","cmyk", or"").The function
real[] colors(pen)returns the color components of a pen. The functionspen gray(pen),pen rgb(pen), andpen cmyk(pen)return new pens obtained by converting their arguments to the respective color spaces. The functioncolorless(pen=currentpen)returns a copy of its argument with the color attributes stripped (to avoid color mixing).A 6-character RGB hexidecimal string can be converted to a pen with the routine
pen rgb(string s);
- string hex(pen p);
Various shades and mixtures of the grayscale primary colors
blackandwhite, RGB primary colorsred,green, andblue, and RGB secondary colorscyan,magenta, andyelloware defined as named colors, along with the CMYK primary colorsCyan,Magenta,Yellow, andBlack, in the moduleplain:
The standard 140 RGB
X11colors can be imported with the commandimport x11colors;
and the standard 68 CMYK TeX colors can be imported with the command
import texcolors;
Note that there is some overlap between these two standards and the definitions of some colors (e.g.
Green) actually disagree.Asymptotealso comes with aasycolors.styLaTeXpackage that defines toLaTeXCMYK versions ofAsymptote’s predefined colors, so that they can be used directly withinLaTeXstrings. Normally, such colors are passed toLaTeXvia a pen argument; however, to change the color of only a portion of a string, say for a slide presentation, (see sectionslide) it may be desirable to specify the color directly toLaTeX. This file can be passed toLaTeXwith theAsymptotecommandusepackage("asycolors");The structure
hsvdefined inplain_pens.asymay be used to convert between HSV and RGB spaces, where the huehis an angle in [0,360) and the saturationsand valuevlie in[0,1]:pen p=hsv(180,0.5,0.75); write(p); // ([default], red=0.375, green=0.75, blue=0.75) hsv q=p; write(q.h,q.s,q.v); // 180 0.5 0.75
- Line types are specified with the function
pen linetype(real[] a, real offset=0, bool scale=true, bool adjust=true), whereais an array of real array numbers. The optional parameteroffsetspecifies where in the pattern to begin. The first number specifies how far (ifscaleistrue, in units of the pen line width; otherwise inPostScriptunits) to draw with the pen on, the second number specifies how far to draw with the pen off, and so on. Ifadjustistrue, these spacings are automatically adjusted byAsymptoteto fit the arclength of the path. Here are the predefined line types:pen solid=linetype(new real[]); pen dotted=linetype(new real[] {0,4}); pen dashed=linetype(new real[] {8,8}); pen longdashed=linetype(new real[] {24,8}); pen dashdotted=linetype(new real[] {8,8,0,8}); pen longdashdotted=linetype(new real[] {24,8,0,8}); pen Dotted(pen p=currentpen) {return linetype(new real[] {0,3})+2*linewidth(p);} pen Dotted=Dotted();
The default line type is
solid; this may be changed withdefaultpen(pen). The line type of a pen can be determined with the functionsreal[] linetype(pen p=currentpen),real offset(pen p),bool scale(pen p), andbool adjust(pen p). - The pen line width is specified in
PostScriptunits withpen linewidth(real). The default line width is 0.5 bp; this value may be changed withdefaultpen(pen). The line width of a pen is returned byreal linewidth(pen p=currentpen). For convenience, in the moduleplain_penswe definevoid defaultpen(real w) {defaultpen(linewidth(w));} pen operator +(pen p, real w) {return p+linewidth(w);} pen operator +(real w, pen p) {return linewidth(w)+p;}so that one may set the line width like this:
defaultpen(2); pen p=red+0.5;
- A pen with a specific
PostScriptline cap is returned on callinglinecapwith an integer argument:pen squarecap=linecap(0); pen roundcap=linecap(1); pen extendcap=linecap(2);
The default line cap,
roundcap, may be changed withdefaultpen(pen). The line cap of a pen is returned byint linecap(pen p=currentpen). - A pen with a specific
PostScriptjoin style is returned on callinglinejoinwith an integer argument:pen miterjoin=linejoin(0); pen roundjoin=linejoin(1); pen beveljoin=linejoin(2);
The default join style,
roundjoin, may be changed withdefaultpen(pen).The join style of a pen is returned byint linejoin(pen p=currentpen). - A pen with a specific
PostScriptmiter limit is returned by callingmiterlimit(real). The default miterlimit,10.0, may be changed withdefaultpen(pen). The miter limit of a pen is returned byreal miterlimit(pen p=currentpen). - A pen with a specific
PostScriptfill rule is returned on callingfillrulewith an integer argument:pen zerowinding=fillrule(0); pen evenodd=fillrule(1);
The fill rule, which identifies the algorithm used to determine the insideness of a path or array of paths, only affects the
clip,fill, andinsidefunctions. For thezerowindingfill rule, a pointzis outside the region bounded by a path if the number of upward intersections of the path with the horizontal linez--z+infinityminus the number of downward intersections is zero. For theevenoddfill rule,zis considered to be outside the region if the total number of such intersections is even. The default fill rule,zerowinding, may be changed withdefaultpen(pen). The fill rule of a pen is returned byint fillrule(pen p=currentpen). - A pen with a specific text alignment setting is returned on
calling
basealignwith an integer argument:pen nobasealign=basealign(0); pen basealign=basealign(1);
The default setting,
nobasealign,which may be changed withdefaultpen(pen), causes the label alignment routines to use the full label bounding box for alignment. In contrast,basealignrequests that the TeX baseline be respected. The base align setting of a pen is returned byint basealigin(pen p=currentpen). - The font size is specified in TeX points (1 pt = 1/72.27 inches) with
the function
pen fontsize(real size, real lineskip=1.2*size). The default font size, 12pt, may be changed withdefaultpen(pen). Nonstandard font sizes may require insertingimport fontsize;
at the beginning of the file (this requires the
type1cmpackage available fromand included in recent
LaTeXdistributions). The font size and line skip of a pen can be examined with the routinesreal fontsize(pen p=currentpen)andreal lineskip(pen p=currentpen), respectively. - A pen using a specific
LaTeXNFSSfont is returned by calling the functionpen font(string encoding, string family, string series, string shape). The default setting,font("OT1","cmr","m","n"), corresponds to 12pt Computer Modern Roman; this may be changed withdefaultpen(pen). The font setting of a pen is returned bystring font(pen p=currentpen). Support for standardized international characters is provided by theunicodepackage (see sectionunicode).Alternatively, one may select a fixed-size TeX font (on which
fontsizehas no effect) like"cmr12"(12pt Computer Modern Roman) or"pcrr"(Courier) using the functionpen font(string name). An optional size argument can also be given to scale the font to the requested size:pen font(string name, real size).A nonstandard font command can be generated with
pen fontcommand(string).A convenient interface to the following standard
PostScriptfonts is also provided:pen AvantGarde(string series="m", string shape="n"); pen Bookman(string series="m", string shape="n"); pen Courier(string series="m", string shape="n"); pen Helvetica(string series="m", string shape="n"); pen NewCenturySchoolBook(string series="m", string shape="n"); pen Palatino(string series="m", string shape="n"); pen TimesRoman(string series="m", string shape="n"); pen ZapfChancery(string series="m", string shape="n"); pen Symbol(string series="m", string shape="n"); pen ZapfDingbats(string series="m", string shape="n");
- The transparency of a pen can be changed with the command:
pen opacity(real opacity=1, string blend="Compatible");
The opacity can be varied from
0(fully transparent) to the default value of1(opaque), andblendspecifies one of the following foreground–background blending operations:"Compatible","Normal","Multiply","Screen","Overlay","SoftLight", "HardLight","ColorDodge","ColorBurn","Darken","Lighten","Difference", "Exclusion","Hue","Saturation","Color","Luminosity",
as described in
http://partners.adobe.com/public/developer/en/pdf/PDFReference16.pdf. Since
PostScriptdoes not support transparency, this feature is only effective with the-f pdfoutput format option; other formats can be produced from the resulting PDF file with theImageMagickconvertprogram. Labels are always drawn with anopacityof 1. A simple example of transparent filling is provided in the example filetransparency.asy. -
PostScriptcommands within apicturemay be used to create a tiling pattern, identified by the stringname, forfillanddrawoperations by adding it to the globalPostScriptframecurrentpatterns, with optional left-bottom marginlband right-top marginrt.import patterns; void add(string name, picture pic, pair lb=0, pair rt=0);
To
fillordrawusing patternname, use the penpattern("name"). For example, rectangular tilings can be constructed using the routinespicture tile(real Hx=5mm, real Hy=0, pen p=currentpen, filltype filltype=NoFill),picture checker(real Hx=5mm, real Hy=0, pen p=currentpen), andpicture brick(real Hx=5mm, real Hy=0, pen p=currentpen)defined inpatterns.asy:size(0,90); import patterns; add("tile",tile()); add("filledtilewithmargin",tile(6mm,4mm,red,Fill),(1mm,1mm),(1mm,1mm)); add("checker",checker()); add("brick",brick()); real s=2.5; filldraw(unitcircle,pattern("tile")); filldraw(shift(s,0)*unitcircle,pattern("filledtilewithmargin")); filldraw(shift(2s,0)*unitcircle,pattern("checker")); filldraw(shift(3s,0)*unitcircle,pattern("brick"));
Hatch patterns can be generated with the routines
picture hatch(real H=5mm, pair dir=NE, pen p=currentpen),picture crosshatch(real H=5mm, pen p=currentpen):size(0,100); import patterns; add("hatch",hatch()); add("hatchback",hatch(NW)); add("crosshatch",crosshatch(3mm)); real s=1.25; filldraw(unitsquare,pattern("hatch")); filldraw(shift(s,0)*unitsquare,pattern("hatchback")); filldraw(shift(2s,0)*unitsquare,pattern("crosshatch"));
You may need to turn off aliasing in your
PostScriptviewer for patterns to appear correctly. Custom patterns can easily be constructed, following the examples inpatterns.asy. The tiled pattern can even incorporate shading (see gradient shading), as illustrated in this example (not included in the manual because not all printers supportPostScript3):size(0,100); import patterns; real d=4mm; picture tiling; path square=scale(d)*unitsquare; axialshade(tiling,square,white,(0,0),black,(d,d)); fill(tiling,shift(d,d)*square,blue); add("shadedtiling",tiling); filldraw(unitcircle,pattern("shadedtiling")); - One can specify a custom pen nib as an arbitrary polygonal path
with
pen makepen(path); this path represents the mark to be drawn for paths containing a single point. This pen nib path can be recovered from a pen withpath nib(pen). Unlike inMetaPost, the path need not be convex:size(200); pen convex=makepen(scale(10)*polygon(8))+grey; draw((1,0.4),convex); draw((0,0)---(1,1)..(2,0)--cycle,convex); pen nonconvex=scale(10)* makepen((0,0)--(0.25,-1)--(0.5,0.25)--(1,0)--(0.5,1.25)--cycle)+red; draw((0.5,-1.5),nonconvex); draw((0,-1.5)..(1,-0.5)..(2,-1.5),nonconvex);
The value
nullpathrepresents a circular pen nib (the default); an elliptical pen can be achieved simply by multiplying the pen by a transform:yscale(2)*currentpen. - One can prevent labels from overwriting one another by using
the pen attribute
overwrite, which takes a single argument:AllowAllow labels to overwrite one another. This is the default behaviour (unless overridden with
defaultpen(pen).SuppressSuppress, with a warning, each label that would overwrite another label.
SuppressQuietSuppress, without warning, each label that would overwrite another label.
MoveMove a label that would overwrite another out of the way and issue a warning. As this adjustment is during the final output phase (in
PostScriptcoordinates) it could result in a larger figure than requested.MoveQuietMove a label that would overwrite another out of the way, without warning. As this adjustment is during the final output phase (in
PostScriptcoordinates) it could result in a larger figure than requested.
The routine defaultpen() returns the current default pen attributes.
Calling the routine resetdefaultpen() resets all pen default
attributes to their initial values.
| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on May 24, 2014 using texi2html 5.0.
