From 48f2471f29615c0f7025eaf4aab44f7e47d395ac Mon Sep 17 00:00:00 2001 From: jro Date: Sat, 4 Aug 2018 18:31:31 -0700 Subject: [PATCH] Adding options to manually set text rendering mode and stroke width. Also adding a text building mode which removes the BT and ET tags, specifically to allow building text clipping paths out of large blocks of text, also attempting to remove the inverted coordinate plane for text drawing as to avoid needing to save and restore graphic state, so that graphic state can be set and then persist after drawing text. --- lib/mixins/text.coffee | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/mixins/text.coffee b/lib/mixins/text.coffee index 7d564bb5c..6851b4ed1 100644 --- a/lib/mixins/text.coffee +++ b/lib/mixins/text.coffee @@ -189,6 +189,9 @@ module.exports = align = options.align or 'left' wordSpacing = options.wordSpacing or 0 characterSpacing = options.characterSpacing or 0 + characterStrokeWidth = options.characterStrokeWidth or 0 + mode = options.mode or 0 + textBuildMode = options.textBuildMode or 0 # text alignments if options.width @@ -254,8 +257,6 @@ module.exports = @stroke() @restore() - @save() - # oblique (angle in degrees or boolean) if options.oblique if typeof options.oblique is 'number' @@ -266,26 +267,26 @@ module.exports = @transform 1, 0, skew, 1, -skew * dy, 0 @transform 1, 0, 0, 1, -x, -y - # flip coordinate system - @transform 1, 0, 0, -1, 0, @page.height - y = @page.height - y - dy - # add current font to page if necessary @page.fonts[@_font.id] ?= @_font.ref() # begin the text object - @addContent "BT" + @addContent "BT" if not textBuildMode # text position - @addContent "1 0 0 1 #{number(x)} #{number(y)} Tm" + @addContent "1 0 0 -1 #{number(x)} #{number(y)} Tm" # font and font size @addContent "/#{@_font.id} #{number(@_fontSize)} Tf" # rendering mode - mode = if options.fill and options.stroke then 2 else if options.stroke then 1 else 0 + if not mode + mode = if options.fill and options.stroke then 2 else if options.stroke then 1 else 0 @addContent "#{mode} Tr" if mode + # character stroke width if applicable + @addContent "#{number(characterStrokeWidth)} w" if characterStrokeWidth + # Character spacing @addContent "#{number(characterSpacing)} Tc" if characterSpacing @@ -344,14 +345,14 @@ module.exports = flush i # Move the text position and flush just the current character - @addContent "1 0 0 1 #{number(x + pos.xOffset * scale)} #{number(y + pos.yOffset * scale)} Tm" + @addContent "1 0 0 -1 #{number(x + pos.xOffset * scale)} #{number(y + pos.yOffset * scale)} Tm" flush i + 1 hadOffset = yes else # If the last character had an offset, reset the text position if hadOffset - @addContent "1 0 0 1 #{number(x)} #{number(y)} Tm" + @addContent "1 0 0 -1 #{number(x)} #{number(y)} Tm" hadOffset = no # Group segments that don't have any advance adjustments @@ -364,7 +365,4 @@ module.exports = flush i # end the text object - @addContent "ET" - - # restore flipped coordinate system - @restore() + @addContent "ET" if not textBuildMode