about summary refs log tree commit diff stats
path: root/html/080display.cc.html
Commit message (Collapse)AuthorAgeFilesLines
* 3878Kartik K. Agaram2017-05-261-105/+108
|
* 3877Kartik K. Agaram2017-05-261-2/+2
|
* 3867Kartik K. Agaram2017-05-191-495/+405
|
* 3855Kartik K. Agaram2017-05-131-411/+498
|
* 3849Kartik K. Agaram2017-05-101-356/+356
|
* 3845Kartik K. Agaram2017-05-061-438/+409
|
* 3825Kartik K. Agaram2017-04-161-485/+428
|
* 3764 - better colors for cross-linksKartik K. Agaram2017-03-081-4/+5
|
* 3761Kartik K. Agaram2017-03-071-106/+107
|
* 3750Kartik K. Agaram2017-03-021-13/+13
|
* 3749Kartik K. Agaram2017-03-021-13/+13
|
* 3716Kartik K. Agaram2016-12-261-0/+2
| | | | Make hyperlinks less salient in the rendered html since there's so many of them.
* 3713 - cross-link calls with definitions in htmlKartik K. Agaram2016-12-261-62/+62
|
* 3710Kartik K. Agaram2016-12-261-556/+556
| | | | | Turns out we don't need to explicitly add anchors for each line. Vim's TOhtml has magic for that out of the box.
* 3709 - line numbers in htmlKartik K. Agaram2016-12-261-559/+583
| | | | | | Each line number also gets an anchor name, but I'm not hyperlinking them for now because I don't want to encourage bookmarking these links just yet. They aren't permalinks because every revision may change what's at any given line number.
* 3604Kartik K. Agaram2016-10-271-4/+4
|
* 3561Kartik K. Agaram2016-10-221-3/+3
|
* 3544Kartik K. Agaram2016-10-221-1/+1
|
* 3543Kartik K. Agaram2016-10-221-1/+1
|
* 3524Kartik K. Agaram2016-10-201-3/+3
|
* 3315Kartik K. Agaram2016-09-101-1/+41
|
* 3266Kartik K. Agaram2016-08-271-5/+5
|
* 3174Kartik K. Agaram2016-08-121-3/+19
|
* 3158Kartik K. Agaram2016-07-271-2/+2
|
* 2996Kartik K. Agaram2016-05-211-7/+7
|
* 2812Kartik K. Agaram2016-03-271-21/+30
|
* 2744Kartik K. Agaram2016-03-091-3/+3
| | | | Tweak colors and font-sizes in generated html.
* 2743Kartik K. Agaram2016-03-091-135/+127
| | | | | Looks like "TOhtml | <other command>" doesn't work on Mac OS X for some reason..
* 2706 - update htmlKartik K. Agaram2016-02-251-2/+2
|
* 2605Kartik K. Agaram2016-01-261-2/+0
|
* 2545Kartik K. Agaram2015-12-151-1/+6
| | | | update html
* 2430 - make room for more transformsKartik K. Agaram2015-11-131-0/+534
06 04:14:58 +0200 Refactor bitmap, add png color modes' href='/ahoang/chawan/commit/src/img/painter.nim?id=f94439ca8d5025252e9207cf831ec1e5d0e9a674'>f94439ca ^
a0ea4c5a ^

f94439ca ^























66b9574b ^
f94439ca ^






911c2dd1 ^
f94439ca ^









66b9574b ^


f94439ca ^




911c2dd1 ^
f94439ca ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

                    
 
                    






                   
                           

                        
 
                                                                    
                                                                        
















                                                                           
                                                                         

















                                                                           
                                                                     










                                             
                                                              

                                                                     
                                                          

                                       
                                                             


                             
                                                                   
                      

                                         

                                 
                                                         














                                                          
                                                




                               
                                        





                                                                             

                            







                                           
                                                                       



                             
                                                                         






                            
                                                      



















                                                                      

                                          























                                                        
                                            






                                                                
                                                                          









                                     


                                           




                                                   
                                                                            


                                            
import std/algorithm
import std/unicode

import css/cssvalues
import img/bitmap
import img/path
import img/png
import types/color
import types/line
import types/vector

type CanvasFillRule* = enum
  cfrNonZero = "nonzero"
  cfrEvenOdd = "evenodd"

# https://en.wikipedia.org/wiki/Bresenham's_line_algorithm#All_cases
proc plotLineLow(bmp: Bitmap; x0, y0, x1, y1: int64; color: ARGBColor) =
  var dx = x1 - x0
  var dy = y1 - y0
  var yi = 1
  if dy < 0:
    yi = -1
    dy = -dy
  var D = 2 * dy - dx;
  var y = y0;
  for x in x0 ..< x1:
    if x < 0 or y < 0 or uint64(x) >= bmp.width or uint64(y) >= bmp.height:
      break
    bmp.setpxb(uint64(x), uint64(y), color)
    if D > 0:
       y = y + yi;
       D = D - 2 * dx;
    D = D + 2 * dy;

proc plotLineHigh(bmp: Bitmap; x0, y0, x1, y1: int64; color: ARGBColor) =
  var dx = x1 - x0
  var dy = y1 - y0
  var xi = 1
  if dx < 0:
    xi = -1
    dx = -dx
  var D = 2 * dx - dy
  var x = x0
  for y in y0 ..< y1:
    if x < 0 or y < 0 or uint64(x) >= bmp.width or uint64(y) >= bmp.height:
      break
    bmp.setpxb(uint64(x), uint64(y), color)
    if D > 0:
       x = x + xi
       D = D - 2 * dy
    D = D + 2 * dx

#TODO should be uint64...
proc plotLine(bmp: Bitmap; x0, y0, x1, y1: int64; color: ARGBColor) =
  if abs(y1 - y0) < abs(x1 - x0):
    if x0 > x1:
      bmp.plotLineLow(x1, y1, x0, y0, color)
    else:
      bmp.plotLineLow(x0, y0, x1, y1, color)
  else:
    if y0 > y1:
      bmp.plotLineHigh(x1, y1, x0, y0, color)
    else:
      bmp.plotLineHigh(x0, y0, x1, y1, color)

proc plotLine(bmp: Bitmap; a, b: Vector2D; color: ARGBColor) =
  bmp.plotLine(int64(a.x), int64(a.y), int64(b.x), int64(b.y), color)

proc plotLine(bmp: Bitmap; line: Line; color: ARGBColor) =
  bmp.plotLine(line.p0, line.p1, color)

proc strokePath*(bmp: Bitmap; path: Path; color: ARGBColor) =
  for line in path.lines:
    bmp.plotLine(line, color)

func isInside(windingNumber: int; fillRule: CanvasFillRule): bool =
  return case fillRule
  of cfrNonZero: windingNumber != 0
  of cfrEvenOdd: windingNumber mod 2 == 0

# Mainly adapted from SerenityOS.
proc fillPath*(bmp: Bitmap; path: Path; color: ARGBColor;
    fillRule: CanvasFillRule) =
  let lines = path.getLineSegments()
  var i = 0
  var ylines: seq[LineSegment]
  for y in int64(lines.miny) .. int64(lines.maxy):
    for k in countdown(ylines.high, 0):
      if ylines[k].maxy < float64(y):
        ylines.del(k) # we'll sort anyways, so del is fine
    for j in i ..< lines.len:
      if lines[j].miny > float64(y):
        break
      if lines[j].maxy > float64(y):
        ylines.add(lines[j])
      inc i
    ylines.sort(cmpLineSegmentX)
    var w = if fillRule == cfrNonZero: 1 else: 0
    for k in 0 ..< ylines.high:
      let a = ylines[k]
      let b = ylines[k + 1]
      let sx = int64(a.minyx)
      let ex = int64(b.minyx)
      if w.isInside(fillRule) and y > 0:
        for x in sx .. ex:
          if x > 0:
            bmp.setpxb(uint64(x), uint64(y), color)
      if int64(a.p0.y) != y and int64(a.p1.y) != y and int64(b.p0.y) != y and
          int64(b.p1.y) != y and sx != ex or a.islope * b.islope < 0:
        case fillRule
        of cfrEvenOdd: inc w
        of cfrNonZero:
          if a.p0.y < a.p1.y:
            inc w
          else:
            dec w
      ylines[k].minyx += ylines[k].islope
    if ylines.len > 0:
      ylines[^1].minyx += ylines[^1].islope

proc fillRect*(bmp: Bitmap; x0, x1, y0, y1: uint64, color: ARGBColor) =
  for y in y0 ..< y1:
    for x in x0 ..< x1:
      bmp.setpxb(x, y, color)

proc strokeRect*(bmp: Bitmap; x0, x1, y0, y1: uint64, color: ARGBColor) =
  for x in x0 ..< x1:
    bmp.setpxb(x, y0, color)
    bmp.setpxb(x, y1, color)
  for y in y0 ..< y1:
    bmp.setpxb(x0, y, color)
    bmp.setpxb(x1, y, color)

proc clearRect*(bmp: Bitmap; x0, x1, y0, y1: uint64) =
  for y in y0 ..< y1:
    for x in x0 ..< x1:
      bmp.setpx(x, y, rgba(0, 0, 0, 0))

proc clear*(bmp: Bitmap) =
  bmp.clearRect(0, bmp.width, 0, bmp.height)

const unifont = readFile"res/unifont_jp-15.0.05.png"
var unifontBitmap: Bitmap
var glyphCache: seq[tuple[u: uint32, bmp: Bitmap]]
var glyphCacheI = 0
proc getCharBmp(u: uint32): Bitmap =
  # We only have the BMP.
  let u = if u <= 0xFFFF: u else: 0xFFFD
  if unifontBitmap == nil:
    unifontBitmap = fromPNG(toOpenArrayByte(unifont, 0, unifont.high))
  for (cu, bmp) in glyphCache:
    if cu == u:
      return bmp
  # Unifont glyphs start at x: 32, y: 64, and are of 8x16/16x16 size
  let gx = uint64(32 + 16 * (u mod 0x100))
  let gy = uint64(64 + 16 * (u div 0x100))
  var fullwidth = false
  const white = rgba(255, 255, 255, 255)
  block loop:
    # hack to recognize full width characters
    for y in 0 ..< 16u64:
      for x in 8 ..< 16u64:
        if unifontBitmap.getpx(gx + x, gy + y) != white:
          fullwidth = true
          break loop
  let bmp = newBitmap(if fullwidth: 16 else: 8, 16)
  for y in 0 ..< bmp.height:
    for x in 0 ..< bmp.width:
      let c = unifontBitmap.getpx(gx + x, gy + y)
      if c != white:
        bmp.setpx(x, y, c)
  if glyphCache.len < 256:
    glyphCache.add((u, bmp))
  else:
    glyphCache[glyphCacheI] = (u, bmp)
    inc glyphCacheI
    if glyphCacheI >= glyphCache.len:
      glyphCacheI = 0
  return bmp

proc drawBitmap(a, b: Bitmap; p: Vector2D) =
  for y in 0 ..< b.height:
    for x in 0 ..< b.width:
      let ax = uint64(p.x) + x
      let ay = uint64(p.y) + y
      if ax >= 0 and ay >= y and ax < a.width and ay < a.height:
        a.setpxb(ax, ay, b.getpx(x, y))

proc fillText*(bmp: Bitmap; text: string; x, y: float64; color: ARGBColor;
    textAlign: CSSTextAlign) =
  var w = 0f64
  var glyphs: seq[Bitmap]
  for r in text.runes:
    let glyph = getCharBmp(uint32(r))
    glyphs.add(glyph)
    w += float64(glyph.width)
  var x = x
  #TODO rtl
  case textAlign
  of TextAlignLeft, TextAlignStart: discard
  of TextAlignRight, TextAlignEnd: x -= w
  of TextAlignCenter: x -= w / 2
  else: doAssert false
  for glyph in glyphs:
    bmp.drawBitmap(glyph, Vector2D(x: x, y: y - 8))
    x += float64(glyph.width)

proc strokeText*(bmp: Bitmap; text: string; x, y: float64; color: ARGBColor;
    textAlign: CSSTextAlign) =
  #TODO
  bmp.fillText(text, x, y, color, textAlign)