Skip to content

Commit

Permalink
Added support for returning text metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Feb 27, 2018
1 parent 046b6c9 commit 845d2b3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 69 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ The `settings` table can contain the following values:

**RETURNS**
* `nodes` (table) - A table with all the gui text nodes used to create the text
* `metrics` (table) - A table with text metrics. Contains the keys `width` and `height`.
65 changes: 5 additions & 60 deletions example/example.gui
Original file line number Diff line number Diff line change
Expand Up @@ -59,74 +59,19 @@ nodes {
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
x: 0.16470589
y: 0.16470589
z: 0.2
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "parent"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 400.0
y: 1000.0
z: 0.0
w: 1.0
}
color {
x: 0.4
y: 0.6
z: 0.6
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "box"
id: "bg"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_NW
adjust_mode: ADJUST_MODE_FIT
parent: "parent"
layer: ""
inherit_alpha: true
slice9 {
Expand All @@ -138,7 +83,7 @@ nodes {
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 0.38
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
Expand Down
10 changes: 7 additions & 3 deletions example/example.gui_script
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local richtext = require "richtext.richtext"

local TEXT = "<size=3>RichText</size>Lorem <color=0,0.5,0,1>ipsum </color>dolor <color=red>sit </color><color=#ff00ffff>amet, </color><size=1.15><font=Nanum>consectetur </font></size>adipiscing elit. <b>Nunc </b>tincidunt <b><i>mattis</i> libero</b> <i>non viverra</i>.\n\nNullam ornare accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa rutrum."
local TEXT = "<size=3>RichText</size>Lorem <color=0,0.5,0,1>ipsum </color>dolor <color=red>sit </color><color=#ff00ffff>amet, </color><size=1.15><font=Nanum>consectetur </font></size>adipiscing elit. <b>Nunc </b>tincidunt <b><i>mattis</i> libero</b> <i>non viverra</i>.\n\nNullam ornare accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa <size=2>rutrum.</size>"

function init(self)
local settings = {
Expand All @@ -17,9 +17,13 @@ function init(self)
},
width = 400,
position = vmath.vector3(0, 0, 0),
parent = gui.get_node("parent"),
parent = gui.get_node("bg"),
color = vmath.vector4(0.95, 0.95, 1.0, 1.0),
}

print(TEXT)
self.nodes = richtext.create(TEXT, "Roboto", settings)
local nodes, metrics = richtext.create(TEXT, "Roboto", settings)

-- adjust background to cover text
gui.set_size(settings.parent, vmath.vector3(metrics.width, metrics.height, 0))
end
25 changes: 19 additions & 6 deletions richtext/richtext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ function M.create(text, font, settings)

local words = parser.parse(text)
local highest_word = 0
local text_metrics = {
width = 0,
height = 0,
}
local nodes = {}
for _,word in pairs(words) do
-- assign defaults if needed
Expand All @@ -73,32 +77,40 @@ function M.create(text, font, settings)
gui.set_color(node, word.color)
gui.set_scale(node, vmath.vector3(1) * (word.size or 1))

-- measure width of a single space for current font
-- cache some font measurements for the current font
if not font_sizes[font] then
font_sizes[font] = {
space = gui.get_text_metrics(font, " _").width - gui.get_text_metrics(font, "_").width,
height = gui.get_text_metrics(font, "Ij").height,
}
end

-- get metrics of node
-- get metrics of node with and without trailing whitespace
word.metrics = gui.get_text_metrics_from_node(node)
word.metrics.total_width = (word.metrics.width + #get_trailing_whitespace(word.text) * font_sizes[font].space) * word.size
word.metrics.width = word.metrics.width * word.size
word.metrics.height = word.metrics.height * word.size
highest_word = math.max(highest_word, word.metrics.height)

-- update text metrics width
local current_width = position.x - settings.position.x
text_metrics.width = math.max(text_metrics.width, current_width)

-- move word to next row if it overflows the width
local width = position.x + word.metrics.width - settings.position.x
local width = current_width + word.metrics.width
if settings.width and width > settings.width then
position.y = position.y - highest_word
position.x = settings.position.x
highest_word = font_sizes[font].height
highest_word = word.metrics.height
else
highest_word = math.max(highest_word, word.metrics.height)
end

-- position word
gui.set_position(node, position)

-- update text metrics height
text_metrics.height = (settings.position.y - (position.y - highest_word))

-- handle linebreak or advance on same line
if word.linebreak then
position.y = position.y - highest_word
Expand All @@ -107,9 +119,10 @@ function M.create(text, font, settings)
else
position.x = position.x + word.metrics.total_width
end

end

return nodes
return nodes, text_metrics
end


Expand Down

0 comments on commit 845d2b3

Please sign in to comment.