From c72b50927c6d3345fc886ee3ed97ccd1a960bb19 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Sat, 29 Mar 2014 20:22:21 -0700 Subject: [PATCH] Add heightOfString method that uses the line wrapper without actually adding content to the document. Very useful for layout purposes --- lib/mixins/text.coffee | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/mixins/text.coffee b/lib/mixins/text.coffee index 56203eb..19a43a3 100644 --- a/lib/mixins/text.coffee +++ b/lib/mixins/text.coffee @@ -24,7 +24,7 @@ module.exports = @y -= @currentLineHeight(true) * lines + @_lineGap return this - text: (text, x, y, options) -> + _text: (text, x, y, options, lineCallback) -> options = @_initOptions(x, y, options) # Convert text to a string @@ -39,7 +39,7 @@ module.exports = wrapper = @_wrapper unless wrapper wrapper = new LineWrapper(this, options) - wrapper.on 'line', @_line.bind(this) + wrapper.on 'line', lineCallback @_wrapper = if options.continued then wrapper else null @_textOptions = if options.continued then options else null @@ -47,10 +47,29 @@ module.exports = # render paragraphs as single lines else - @_line line, options for line in text.split '\n' + lineCallback line, options for line in text.split '\n' return this + text: (text, x, y, options) -> + @_text text, x, y, options, @_line.bind(this) + + widthOfString: (string, options = {}) -> + @_font.widthOfString(string, @_fontSize) + (options.characterSpacing or 0) * (string.length - 1) + + heightOfString: (text, options) -> + {x,y} = this + lineGap = options.lineGap or @_lineGap or 0 + + @_text text, 0, 0, options, (line, options) => + @y += @currentLineHeight(true) + lineGap + + height = @y # we started at y=0 + @x = x + @y = y + + return height + list: (list, x, y, options, wrapper) -> options = @_initOptions(x, y, options) @@ -135,10 +154,7 @@ module.exports = options.columnGap ?= 18 # 1/4 inch return options - - widthOfString: (string, options = {}) -> - @_font.widthOfString(string, @_fontSize) + (options.characterSpacing or 0) * (string.length - 1) - + _line: (text, options = {}, wrapper) -> @_fragment text, @x, @y, options lineGap = options.lineGap or @_lineGap or 0