split/Tioga/lib/TeX_Text.rb in tioga-1.6 vs split/Tioga/lib/TeX_Text.rb in tioga-1.7

- old
+ new

@@ -59,10 +59,14 @@ for the two special cases of \' and \\), so TeX commands using backslashes don't cause trouble. With double quotes, Ruby uses backslash for a variety of escape characters such as newline (\n) and tab (\t), so the backslashes for TeX need to be entered as \\ pairs to be safe. Compare '$\nu\sim\tau$' to the equivalent form "$\\\\nu\\\\sim\\\\tau$" and the incorrect form "$\nu\sim\tau$". +Starting from Tioga 1.7, Tioga can measure the TeX text it typesets. For +that purpose, provide #show_text with a 'measure' dictionnary entry that +serves as a key to retrieve the information using #get_text_size. + Dictionary Entries 'text' => a_string # to be processed by TeX 'side' => a_side # TOP, BOTTOM, LEFT, or RIGHT 'loc' # alias for 'side' 'position' => a_float # fractional distance along side from bottom left @@ -75,11 +79,12 @@ 'color' => a_color # default is to omit color specification 'scale' => a_float # scale relative to default_text_scale. default 1 'angle' => a_float # degrees to rotate. default 0 'alignment' => an_alignment # see discussion of alignment 'justification' => a_justification # see discussion of justification - + 'measure' => a_string # the name of the measure + Examples def math_typesetting centerx = t.bounds_xmin + 0.5 * t.bounds_width equation = @@ -200,9 +205,82 @@ link:images/Framebox.png =end def show_text(dict) + end + +=begin rdoc +Returns information about the text measure named _name_. It returns a hash +with the following information: + + * _[xy][tb][lr]_ the X/Y coordinates of the Top/Bottom Left/Right point of the + box around the text (top, left, right and bottom are relative to the text + natural orientation) + * _just_ the text justification + * _angle_ the text's angle, in degrees + * _tex_measured_depth_, _tex_measured_height_, tex_measured_width_, the + text's depth, width and height as measured by TeX, + (does not take scaling into accound) + * _xanchor_, _yanchor_: the page coordinates of the position of the text + (the ones that were given to #show_text) + * _points_ an array of (x,y) coordinates of the points that make the box, + in the order bottom left, bottom right, top right, top left. + * _depth_, _width_, _height_ : the size of the text. + * _align_ the alignment of the text + +All the measures are given in postscript points, which means you need +to multiply by 10 before using the with +Page_Frame_Bounds#convert_output_to_figure_x +Page_Frame_Bounds#convert_output_to_figure_y. See the example + +Please make sure you test the presence of one key before using it, as +any code using measures has to run *twice*: one first time to typeset the +text and ask LaTeX for information about its size, and the second time to +actually use the information. You don't need to call the code twice as it +is done automatically, but keep in mind that in the first run, the returned +hash will be empty. + + def text_size_with_rotation + t.stroke_rect(0,0,1,1) + t.rescale(0.5) + + equation = '\int_{-\infty}^{\infty} \! e^{-x^{2}}\, \! dx = \sqrt{\pi}' + text = "\\fbox{$\\displaystyle #{equation}$}" + + nb = 5 + nb.times do |i| + scale = 0.5 + i * 0.2 + angle = i * 37 + x = (i+1)/(nb+1.0) + y = x + color = [1.0 - i * 0.2, i*0.2, 0] + t.show_text('text' => text, 'color' => color, 'x' => x, + 'y' => x, + 'alignment' => ALIGNED_AT_MIDHEIGHT, + 'scale' => scale , 'measure' => "box#{i}", + 'angle' => angle ) + size = t.get_text_size("box#{i}") + if size.key? 'points' + xs = Dvector.new + ys = Dvector.new + for x,y in size['points'] + xs << t.convert_output_to_figure_x(x) + ys << t.convert_output_to_figure_y(y) + end + t.stroke_color = color + t.line_type = Line_Type_Dashes + t.stroke_rect(xs.min, ys.min, + (xs.max - xs.min),(ys.max - ys.min)) + end + end + end + +link:images/Text_size_with_rotation.png + +=end + + def get_text_size(name) end # Changes the default_text_scale attribute by multiplying it times _scale_. # This also updates the default_text_height_dx and default_text_height_dy # attributes to match the new setting for default_text_scale.