-- primitives for editing drawings Drawing = {} require 'drawing_tests' -- All drawings span 100% of some conceptual 'page width' and divide it up -- into 256 parts. function Drawing.draw(State, line_index, y) local line = State.lines[line_index] local line_cache = State.line_cache[line_index] line_cache.starty = y local pmx,pmy = App.mouse_x(), App.mouse_y() if pmx < State.right and pmy > line_cache.starty and pmy < line_cache.starty+Drawing.pixels(line.h, State.width) then App.color(Icon_color) love.graphics.rectangle('line', State.left,line_cache.starty, State.width,Drawing.pixels(line.h, State.width)) if icon[State.current_drawing_mode] then icon[State.current_drawing_mode](State.right-22, line_cache.starty+4) else icon[State.previous_drawing_mode](State.right-22, line_cache.starty+4) end if App.mouse_down(1) and love.keyboard.isDown('h') then draw_help_with_mouse_pressed(State, line_index) return end end if line.show_help then draw_help_without_mouse_pressed(State, line_index) return end local mx = Drawing.coord(pmx-State.left, State.width) local my = Drawing.coord(pmy-line_cache.starty, State.width) for _,shape in ipairs(line.shapes) do assert(shape) if geom.on_shape(mx,my, line, shape) then App.color(Focus_stroke_color) else App.color(Stroke_color) end Drawing.draw_shape(line, shape, line_cache.starty, State.left,State.right) end local function px(x) return Drawing.pixels(x, State.width)+State.left end local function py(y) return Drawing.pixels(y, State.width)+line_cache.starty end for i,p in ipairs(line.points) do if p.deleted == nil then if Drawing.near(p, mx,my, State.width) then App.color(Focus_stroke_color) love.graphics.circle('line', px(p.x),py(p.y), Same_point_distance) else App.color(Stroke_color) love.graphics.circle('fill', px(p.x),py(p.y), 2) end if p.name then -- TODO: clip local x,y = px(p.x)+5, py(p.y)+5 love.graphics.print(p.name, x,y) if State.current_drawing_mode == 'name' and i == line.pending.target_point then -- create a faint red box for the name App.color(Current_name_background_color) local name_text -- TODO: avoid computing name width on every repaint if p.name == '' then name_text = State.em else name_text = App.newText(love.graphics.getFont(), p.name) end love.graphics.rectangle('fill', x,y, App.width(name_text), State.line_height) end end end end App.color(Current_stroke_color) Drawing.draw_pending_shape(line, line_cache.starty, State.left,State.right) end function Drawing.draw_shape(drawing, shape, top, left,right) local width = right-left local function px(x) return Drawing.pixels(x, width)+left end local function py(y) return Drawing.pixels(y, width)+top end if shape.mode == 'freehand' then local prev = nil for _,point in ipairs(shape.points) do if prev then love.graphics.line(px(prev.x),py(prev.y), px(point.x),py(point.y)) end prev = point end elseif shape.mode == 'line' or shape.mode == 'manhattan' then local p1 = drawing.points[shape.p1] local p2 = drawing.points[shape.p2] love.graphics.line(px(p1.x),py(p1.y), px(p2.x),py(p2.y)) elseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' then local prev = nil for _,point in ipairs(shape.vertices) do local curr = drawing.points[point] if prev then love.graphics.line(px(prev.x),py(prev.y), px(curr.x),py(curr.y)) end prev = curr end -- close the loop local curr = drawing.points[shape.vertices[1]] love.graphics.line(px(prev.x),py(prev.y), px(curr.x),py(curr.y)) elseif shape.mode == 'circle' then -- TODO: clip local center = drawing.points[shape.center] love.graphics.circle('line', px(center.x),py(center.y), Drawing.pixels(shape.radius, width)) elseif shape.mode == 'arc' then local center = drawing.points[shape.center] love.graphics.arc('line', 'open', px(center.x),py(center.y), Drawing.pixels(shape.radius, width), shape.start_angle, shape.end_angle, 360) elseif shape.mode == 'deleted' then -- ignore else print(shape.mode) assert(false) end end function Drawing.draw_pending_shape(drawing, top, left,right) local width = right-left local pmx,pmy = App.mouse_x(), App.mouse_y() local function px(x) return Drawing.pixels(x, width)+left end local function py(y) return Drawing.pixels(y, width)+top end local mx = Drawing.coord(pmx-left, width) local my = Drawing.coord(pmy-top, width) -- recreate pixels from coords to precisely mimic how the drawing will look -- after mouse_release pmx,pmy = px(mx), py(my) local shape = drawing.pending if shape.mode == nil then -- nothing pending elseif shape.mode == 'freehand' then local shape_copy = deepcopy(shape) Drawing.smoothen(shape_copy) Drawing.draw_shape(drawing, shape_copy, top, left,right) elseif shape.mode == 'line' then if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then return end local p1 = drawing.points[shape.p1] love.graphics.line(px(p1.x),py(p1.y), pmx,pmy) elseif shape.mode == 'manhattan' then if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then return end local p1 = drawing.points[shape.p1] if math.abs(mx-p1.x) > math.abs(my-p1.y) then love.graphics.line(px(p1.x),py(p1.y), pmx, py(p1.y)) else love.graphics.line(px(p1.x),py(p1.y), px(p1.x),pmy) end elseif shape.mode == 'polygon' then -- don't close the loop on a pending polygon local prev = nil for _,point in ipairs(shape.vertices) do local curr = drawing.points[point] if prev then love.graphics.line(px(prev.x),py(prev.y), px(curr.x),py(curr.y)) end prev = curr end love.graphics.line(px(prev.x),py(prev.y), pmx,pmy) elseif shape.mode == 'rectangle' then local first = drawing.points[shape.vertices[1]] if #shape.vertices == 1 then love.graphics.line(px(first.x),py(first.y), pmx,pmy) return end local second = drawing.points[shape.vertices[2]] local thirdx,thirdy, fourthx,fourthy = Drawing.complete_rectangle(first.x,first.y, second.x,second.y, mx,my) love.graphics.line(px(first.x),py(first.y), px(second.x),py(second.y)) love.graphics.line(px(second.x),py(second.y), px(thirdx),py(thirdy)) love.graphics.line(px(thirdx),py(thirdy), px(fourthx),py(fourthy)) love.graphics.line(px(fourthx),py(fourthy), px(first.x),py(first.y)) elseif shape.mode == 'square' then local first = drawing.points[shape.vertices[1]] if #shape.vertices == 1 then love.graphics.
#
#
#              Nim's Runtime Library
#        (c) Copyright 2021 Nim Contributors
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module provides some high performance string operations.
##
## Experimental API, subject to change.

when defined(nimPreviewSlimSystem):
  import std/assertions


const whitespaces = {' ', '\t', '\v', '\r', '\l', '\f'}

proc add*(x: var string, y: openArray[char]) =
  ## Concatenates `x` and `y` in place. `y` must not overlap with `x` to
  ## allow future `memcpy` optimizations.
  # Use `{.noalias.}` ?
  let n = x.len
  x.setLen n + y.len
    # pending https://github.com/nim-lang/Nim/issues/14655#issuecomment-643671397
    # use x.setLen(n + y.len, isInit = false)
  var i = 0
  while i < y.len:
    x[n + i] = y[i]
    i.inc
  # xxx use `nimCopyMem(x[n].addr, y[0].addr, y.len)` after some refactoring

func stripSlice(s: openArray[char], leading = true, trailing = true, chars: set[char] = whitespaces): Slice[int] =
  ## Returns the slice range of `s` which is stripped `chars`.
  runnableExamples:
    assert stripSlice(" abc  ") == 1 .. 3
  var
    first = 0
    last = high(s)
  if leading:
    while first <= last and s[first] in chars: inc(first)
  if trailing:
    while last >= first and s[last] in chars: dec(last)
  result = first .. last

func setSlice*(s: var string, slice: Slice[int]) =
  ## Inplace version of `substr`.
  runnableExamples:
    import std/sugar

    var a = "Hello, Nim!"
    doAssert a.dup(setSlice(7 .. 9)) == "Nim"
    doAssert a.dup(setSlice(0 .. 0)) == "H"
    doAssert a.dup(setSlice(0 .. 1)) == "He"
    doAssert a.dup(setSlice(0 .. 10)) == a
    doAssert a.dup(setSlice(1 .. 0)).len == 0
    doAssert a.dup(setSlice(20 .. -1)).len == 0


    doAssertRaises(AssertionDefect):
      discard a.dup(setSlice(-1 .. 1))

    doAssertRaises(AssertionDefect):
      discard a.dup(setSlice(1 .. 11))


  let first = slice.a
  let last = slice.b

  assert first >= 0
  assert last <= s.high

  if first > last:
    s.setLen(0)
    return
  template impl =
    for index in first .. last:
      s[index - first] = s[index]
  if first > 0:
    when nimvm: impl()
    else:
      # not JS and not Nimscript
      when not declared(moveMem):
        impl()
      else:
        when defined(nimSeqsV2):
          prepareMutation(s)
        moveMem(addr s[0], addr s[first], last - first + 1)
  s.setLen(last - first + 1)

func strip*(a: var string, leading = true, trailing = true, chars: set[char] = whitespaces) {.inline.} =
  ## Inplace version of `strip`. Strips leading or
  ## trailing `chars` (default: whitespace characters).
  ##
  ## If `leading` is true (default), leading `chars` are stripped.
  ## If `trailing` is true (default), trailing `chars` are stripped.