Skip to content

Commit

Permalink
added dashes and fix rule bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed Jan 26, 2019
1 parent 1a07696 commit bad73fd
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 76 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Changelog

## [v1.1.5] - future release
## [v1.1.5] - 2019-01-26

### Added

- a few more box functions take vertices=true/false
- more box functions take vertices=true/false
- added setdash(dashes)

### Changed

-
- tried to fix odd bug in `rule(..., π/2)`

### Removed

Expand Down
3 changes: 0 additions & 3 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Luxor = "ae8d54c2-7ccd-5906-9d76-62fc9837b5bc"

[compat]
Documenter = "~0.20"
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Documenter, Luxor
makedocs(
modules = [Luxor],
sitename = "Luxor",
html_prettyurls = get(ENV, "CI", nothing) == "true",
format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true"),
pages = Any[
"Introduction to Luxor" => "index.md",
"A few examples" => "examples.md",
Expand Down
31 changes: 0 additions & 31 deletions docs/src/assets/examples/luxor-logo.jl

This file was deleted.

55 changes: 43 additions & 12 deletions docs/src/colors-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,49 @@ nothing # hide

![dashes](assets/figures/dashes.png)

To define more complicated dash patterns in Luxor, supply a vector to `setdash()`.

```julia
dashes = [50.0, # ink
10.0, # skip
10.0, # ink
10.0 # skip
]
setdash(dashes)
```

```@example
using Luxor # hide
Drawing(600, 180, "assets/figures/moredashes.svg") # hide
background("white") # hide
origin() # hide
function dashing()
fontsize(12) # hide
sethue("black") # hide
setline(8)
setlinecap("butt")
patterns = [10, 4, 50, 25, 14, 100]
table = Table(fill(20, length(patterns)), [40, 325])
for p in 1:length(patterns)
setdash(patterns)
pt = table[p, 2]
text(string(patterns), table[p, 1], halign=:right, valign=:middle)
line(pt - (150, 0), pt + (200, 0), :stroke)
patterns = circshift(patterns, 1)
pop!(patterns)
end
end
dashing()
finish() # hide
nothing # hide
```

![more dashes](assets/figures/moredashes.svg)

Notice that odd-numbered patterns flip the ink and skip numbers each time through.

```@docs
setline
setlinecap
Expand All @@ -128,18 +171,6 @@ paint
do_action
```

Soon you'll be able to define dash patterns in Luxor. For now:

```julia
dashes = [50.0, # ink
10.0, # skip
10.0, # ink
10.0 # skip
]
offset = -50.0
Cairo.set_dash(get_current_cr()(), dashes, offset)
```

## Blends

A blend is a color gradient. Use `setblend()` to select a blend in the same way that you'd use `setcolor()` and `sethue()` to select a solid color.
Expand Down
11 changes: 5 additions & 6 deletions src/Luxor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ module Luxor

using Juno, Cairo, Colors, FileIO, Dates

#= from Cairo use: CairoARGBSurface, CairoEPSSurface, CairoMatrix, CairoPDFSurface,
CairoPattern, CairoPatternMesh, CairoSurface, CairoSVGSurface,
#= from Cairo use: CairoARGBSurface, CairoEPSSurface, CairoMatrix, CairoPDFSurface, CairoPattern, CairoPatternMesh, CairoSurface, CairoSVGSurface,
CairoContext, arc, arc_negative, circle, clip, clip_preserve, close_path,
convert_cairo_path_data, copy_path, copy_path_flat, curve_to, destroy, fill,
fill_preserve, finish, get_matrix, get_operator, height, image, line_to,
Expand All @@ -16,10 +15,10 @@ move_to, new_path, new_sub_path,paint, paint_with_alpha,
pattern_add_color_stop_rgba, pattern_create_linear, pattern_create_radial,
read_from_png, rectangle, rel_line_to, rel_move_to, reset_clip, restore, rotate,
save, scale, select_font_face, set_antialias, set_font_face, set_font_size,
set_line_cap, set_line_join, set_line_type, set_line_width, set_matrix,
set_operator, set_source, set_source_rgba, set_source_surface, show_text,
status, stroke, stroke_preserve, text, text_extents, text_path, translate,
width, write_to_png =#
set_line_cap, set_line_join, set_dash, set_line_type, set_line_width,
set_matrix, set_operator, set_source, set_source_rgba, set_source_surface,
show_text, status, stroke, stroke_preserve, text, text_extents, text_path,
translate, width, write_to_png =#

include("drawings.jl")
include("point.jl")
Expand Down
53 changes: 33 additions & 20 deletions src/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ newpath() = Cairo.new_path(get_current_cr())
"""
newsubpath()
Add a new subpath to the current path. This is Cairo's `new_sub_path()` function. It can
be used for example to make holes in shapes.
Add a new subpath to the current path. This is Cairo's `new_sub_path()`
function. It can be used for example to make holes in shapes.
"""
newsubpath() = Cairo.new_sub_path(get_current_cr())

Expand All @@ -152,8 +152,8 @@ closepath() = Cairo.close_path(get_current_cr())
"""
strokepath()
Stroke the current path with the current line width, line join, line cap, and dash settings.
The current path is then cleared.
Stroke the current path with the current line width, line join, line cap, and
dash settings. The current path is then cleared.
"""
strokepath() = Cairo.stroke(get_current_cr())

Expand Down Expand Up @@ -284,15 +284,32 @@ function setlinejoin(str="miter")
end

"""
setlinedash("dot")
setdash("dot")
Set the dash pattern to one of: "solid", "dotted", "dot", "dotdashed", "longdashed",
"shortdashed", "dash", "dashed", "dotdotdashed", "dotdotdotdashed"
Set the dash pattern to one of: "solid", "dotted", "dot", "dotdashed",
"longdashed", "shortdashed", "dash", "dashed", "dotdotdashed",
"dotdotdotdashed".
Use `setdash(dashes::Vector)` to specify the pattern numerically.
"""
function setdash(dashing)
function setdash(dashing::AbstractString)
Cairo.set_line_type(get_current_cr(), dashing)
end

"""
setdash(dashes::Vector, offset=0.0)
Set the dash pattern to the values in `dashes`. The first number is the length of the ink, the second the gap, and so on.
The `offset` specifies an offset into the pattern at which the stroke begins. So an offset of 10 means that the stroke starts at `dashes[1] + 10` into the pattern.
Or use `setdash("dot")` etc.
"""
function setdash(dashes::Vector, offset=0.0)
# no negative dashes
Cairo.set_dash(get_current_cr(), abs.(Float64.(dashes)), offset)
end

"""
move(pt)
Expand Down Expand Up @@ -357,14 +374,10 @@ draws a line that spans a bounding box half the width and height of the drawing.
function rule(pos, theta=0.0;
boundingbox=BoundingBox())
bbox = box(boundingbox, vertices=true)
topside = bbox[1:2]
rightside = bbox[2:3]
bottomside = bbox[3:4]
leftside = vcat(bbox[4], bbox[1])

#if !isinside(pos, bbox, allowonedge=true)
# #@warn "position is not inside bounding box"
#end
topside = bbox[2:3]
rightside = bbox[3:4]
bottomside = vcat(bbox[4], bbox[1])
leftside = bbox[1:2]

# ruled line could be as long as the diagonal so add a bit extra
r = boxdiagonal(boundingbox)/2 + 10
Expand All @@ -376,31 +389,31 @@ function rule(pos, theta=0.0;
interpoints = Set{Point}()

# check for intersection with top of bounding box
flag, ip = intersection(ruledline[1], ruledline[2], topside[1], topside[2])
flag, ip = intersection(ruledline[1], ruledline[2], topside[1], topside[2], crossingonly=true)
if flag
if !(ip.x > topside[2].x || ip.x < topside[1].x)
push!(interpoints, ip)
end
end

# check for right intersection
flag, ip = intersection(ruledline[1], ruledline[2], rightside[1], rightside[2])
flag, ip = intersection(ruledline[1], ruledline[2], rightside[1], rightside[2], crossingonly=true)
if flag
if !(ip.y > rightside[2].y || ip.y < rightside[1].y)
push!(interpoints, ip)
end
end

# check for bottom intersection
flag, ip = intersection(ruledline[1], ruledline[2], bottomside[1], bottomside[2])
flag, ip = intersection(ruledline[1], ruledline[2], bottomside[1], bottomside[2], crossingonly=true)
if flag
if !(ip.x < bottomside[2].x || ip.x > bottomside[1].x)
push!(interpoints, ip)
end
end

# check for left intersection
flag, ip = intersection(ruledline[1], ruledline[2], leftside[1], leftside[2])
flag, ip = intersection(ruledline[1], ruledline[2], leftside[1], leftside[2], crossingonly=true)
if flag
if !(ip.y > leftside[1].y || ip.y < leftside[2].y)
push!(interpoints, ip)
Expand Down
66 changes: 66 additions & 0 deletions test/dashtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env julia

using Luxor

using Test

using Random
Random.seed!(42)

function annotatedash(pos, dashes, offset)
@layer begin
translate(pos)
@layer begin
translate(0, 15)
fontsize(3)
sethue("black")
x = 0
for n in dashes
text(string(n), O + (-offset + x + n/2, 0), halign=:center)
x += n
end
fontsize(6)
text("offset $offset", O + (-10, -10), halign=:right)
end
@layer begin
translate(0, 2.5)
setdash(dashes, offset)
setline(0.5)
sethue("grey60")
line(O + (0, 6), O + (3 * sum(dashes), 6), :stroke)
sethue("red")
setline(5)
setdash(dashes, offset)
line(O, O + (3 * sum(dashes), 0), :stroke)
end
end
end

function dash_tests(fname)
pagewidth, pageheight = 800, 600
Drawing(pagewidth, pageheight, fname)
origin()
background("ivory")
setline(3)
dashes = [30.0, # ink
10.0, # skip
5.0, # ink
15.0, # skip
20.0, # ink
4. # skip
]
offset = -50.0
sethue("black")
text("Pattern is $(dashes)", boxtopcenter(BoundingBox() * 0.9), halign=:center)
sethue("blue")
for y in -pageheight/2 + 50:30:pageheight/2 - 50
randomhue()
annotatedash(O + (-200, y), dashes, offset)
offset += 10
end
@test finish() == true
end

fname = "dash-tests.svg"
dash_tests(fname)
println("...finished dash tests: output in $(fname)")
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function run_all_tests()
include("cropmarkstest.jl")
include("boxmaptest.jl")
include("noise-test.jl")
include("dashtests.jl")
end
end

Expand Down

0 comments on commit bad73fd

Please sign in to comment.