-
Notifications
You must be signed in to change notification settings - Fork 42
/
drawing-area.js
138 lines (112 loc) · 3.55 KB
/
drawing-area.js
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
/*
* drawing-area.js
*/
const gi = require('../')
const Gtk = gi.require('Gtk', '3.0')
const Cairo = gi.require('cairo')
const Pango = gi.require('Pango')
const PangoCairo = gi.require('PangoCairo')
Gtk.init()
// Main program window
const window = new Gtk.Window({
type : Gtk.WindowType.TOPLEVEL
})
// Button
const button = Gtk.ToolButton.newFromStock(Gtk.STOCK_GO_BACK)
// Draw area
const drawingArea = new Gtk.DrawingArea()
drawingArea.on('draw', (_) => {
const width = drawingArea.getAllocatedWidth()
const height = drawingArea.getAllocatedHeight()
console.log(['draw', { width, height }])
// Cairo in GJS uses camelCase function names
_.setSourceRgba(1, 0.0, 0.0, 1)
_.arc(16, 16, 16, 0, 2 * Math.PI);
_.fill()
_.selectFontFace('Fantasque Sans Mono', Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL)
_.setFontSize(12)
const extents = _.textExtents('Disziplin ist Macht.')
console.log({
xAdvance: extents.xAdvance,
yAdvance: extents.yAdvance,
width: extents.width,
height: extents.height,
xBearing: extents.xBearing,
yBearing: extents.yBearing,
})
const fontExtents = _.fontExtents()
console.log({
ascent: fontExtents.ascent,
descent: fontExtents.descent,
height: fontExtents.height,
maxXAdvance: fontExtents.maxXAdvance,
maxYAdvance: fontExtents.maxYAdvance,
})
_.setSourceRgba(0.1, 0.1, 0.1, 1)
_.rectangle(10, 40, extents.xAdvance, extents.height - extents.yBearing)
_.fill()
_.moveTo(10, 50)
_.setSourceRgba(1, 0.0, 0.0, 1)
_.showText('Disziplin ist Macht.')
/* Lines */
{
_.setSourceRgb(1, 1, 1)
_.setLineWidth(1)
_.moveTo(250, 100)
_.lineTo(250, 200)
_.moveTo(200, 150)
_.lineTo(300, 150)
_.stroke()
}
{
_.setSourceRgb(1, 1, 1)
_.setLineWidth(1)
_.moveTo(400.5, 100.5)
_.lineTo(400.5, 200.5)
_.moveTo(350.5, 150.5)
_.lineTo(450.5, 150.5)
_.stroke()
}
_.setSourceRgb(0, 0, 0)
const layout = PangoCairo.createLayout(_)
const fontDescription = Pango.fontDescriptionFromString('Fantasque Sans Mono 9')
layout.setText('text', -1)
layout.setFontDescription(fontDescription)
PangoCairo.showLayout(_, layout)
// Draw glyphs
// const fontFace = Cairo.FontFace.create('Arial', Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL)
// const scaledFont = Cairo.ScaledFont.create(fontFace, new Cairo.Matrix(), new Cairo.Matrix(), new Cairo.FontOptions())
const scaledFont = _.getScaledFont()
const text = 'Here are some glyphs'
const [glyphs] = scaledFont.textToGlyphs(10, 100, text, true)
// put paths for current cluster to _
// _.setScaledFont(scaledFont)
_.glyphPath(glyphs, glyphs.length)
const glyphExtents = _.glyphExtents(glyphs, glyphs.length)
// draw black text with green stroke
_.setSourceRgba(1, 1, 0.2, 1)
_.fillPreserve()
// _.setLineWidth(0.5)
// _.stroke()
const path = _.copyPath()
console.log(path, path.status)
return true
})
// Containing box
const vbox = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL })
vbox.packStart(button, false, true, 0)
vbox.packStart(drawingArea, true, true, 0)
// configure main window
window.setDefaultSize(600, 400)
window.setResizable(true)
window.add(vbox)
// window show event
window.on('show', () => {
Gtk.main()
})
// window after-close event
window.on('destroy', () => Gtk.mainQuit())
// window close event: returning true has the semantic of preventing the default behavior:
// in this case, it would prevent the user from closing the window if we would return `true`
window.on('delete-event', () => false)
window.showAll()