Skip to content

Commit 6279dd6

Browse files
committed
set font size using "scale", method for changing font size
1 parent 9a6ac6c commit 6279dd6

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

examples/previewer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Gtk4, GtkMarkdownTextView, Markdown
22

33
w = GtkWindow("GtkMarkdownTextView example")
4-
Gtk4.default_size(w, 400, 600)
4+
Gtk4.default_size(w, 800, 400)
55
p = GtkPaned(:h)
66
w[] = p
77

src/GtkMarkdownTextView.jl

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module GtkMarkdownTextView
33
using Gtk4
44
import Gtk4: create_tag, apply_tag
55
import Gtk4.GLib: gobject_move_ref, GObject
6+
import Gtk4.Pango
67

78
using Markdown
89

@@ -20,47 +21,37 @@ module GtkMarkdownTextView
2021

2122
mutable struct MarkdownTextView <: GtkTextView
2223
handle::Ptr{GObject}
23-
view::GtkTextViewLeaf
2424
buffer::GtkTextBufferLeaf
2525

2626
function MarkdownTextView(m::Markdown.MD, prelude::String, mc::MarkdownColors = MarkdownColors(); kwargs...)
2727

2828
buffer = GtkTextBuffer()
2929
buffer.text = prelude
3030
view = GtkTextView(buffer; kwargs...)
31-
32-
style_css(view,
33-
"window, view, textview, buffer, text {
34-
background-color: $(mc.background);
35-
color: $(mc.color);
36-
font-family: Monaco, Consolas, Courier, monospace;
37-
font-size: $(mc.font_size)pt;
38-
margin:0px;
39-
}"
40-
)
4131

42-
#set_gtk_property!(view, :margin_left, 1)
4332
view.monospace = true
44-
view.wrap_mode = true
33+
view.wrap_mode = Pango.WrapMode_WORD_CHAR
4534

4635
fs = mc.font_size
4736

48-
create_tag(buffer, "normal", font = "$fs")
49-
create_tag(buffer, "h1", font = "bold $(fs+3)")
50-
create_tag(buffer, "h2", font = "bold $(fs+2)")
51-
create_tag(buffer, "h3", font = "bold $(fs+1)")
52-
create_tag(buffer, "h4", font = "bold $(fs)")
53-
create_tag(buffer, "h5", font = "$(fs)")
54-
create_tag(buffer, "h6", font = "$(fs-1)")
55-
create_tag(buffer, "bold", font = "bold $(fs)")
56-
create_tag(buffer, "italic", font = "italic $fs")
57-
create_tag(buffer, "code", font = "bold $fs",
37+
create_tag(buffer, "normal")
38+
create_tag(buffer, "h1", scale = 2.0, weight = Pango.Weight_BOLD)
39+
create_tag(buffer, "h2", scale = 1.5, weight = Pango.Weight_BOLD)
40+
create_tag(buffer, "h3", scale = 1.25, weight = Pango.Weight_BOLD)
41+
create_tag(buffer, "h4", scale = 1.0, weight = Pango.Weight_BOLD)
42+
create_tag(buffer, "h5", scale = 0.9, weight = Pango.Weight_BOLD)
43+
create_tag(buffer, "h6", scale = 0.8, weight = Pango.Weight_BOLD)
44+
create_tag(buffer, "bold", weight = Pango.Weight_BOLD)
45+
create_tag(buffer, "italic", style = Pango.Style_ITALIC)
46+
create_tag(buffer, "code", weight = Pango.Weight_BOLD,
5847
foreground=mc.highlight_color, background=mc.highlight_background)
5948

6049
insert_MD!(buffer, m)
6150

62-
n = new(view.handle, view, buffer)
63-
gobject_move_ref(n, view)
51+
n = new(view.handle, buffer)
52+
mtv = gobject_move_ref(n, view)
53+
style!(mtv, MarkdownColors())
54+
mtv
6455
end
6556

6657
MarkdownTextView(m::String) = MarkdownTextView(Markdown.parse(m), "")
@@ -88,7 +79,7 @@ module GtkMarkdownTextView
8879
for el in m.text
8980
i = insert_MD!(buffer, el, i)
9081
end
91-
tag(buffer, "h$(min(N,4))", ip, i)
82+
tag(buffer, "h$(min(N,6))", ip, i)
9283
i
9384
end
9485

@@ -186,6 +177,22 @@ module GtkMarkdownTextView
186177
end
187178
end
188179

180+
# this sets the colors to the default too, which is not ideal
181+
function fontsize!(m::MarkdownTextView, fs)
182+
mc = MarkdownColors(fs, "#000", "#fff", "#111", "#eee")
183+
style!(m, mc)
184+
end
189185

186+
function style!(m::MarkdownTextView, mc::MarkdownColors)
187+
style_css(m,
188+
"window, view, textview, buffer, text {
189+
background-color: $(mc.background);
190+
color: $(mc.color);
191+
font-family: Monaco, Consolas, Courier, monospace;
192+
font-size: $(mc.font_size)pt;
193+
margin:0px;
194+
}"
195+
)
196+
end
190197
end
191198

0 commit comments

Comments
 (0)