Typesetting/Attributed Text

From J Wiki
Jump to: navigation, search
Attr.png

Attributed Text script is an extension to gl2 graphics commands to allow to drawing of text in mixed assortment of fonts and colors, with corresponding adjustments of glyphs ascent and horizontal stride.

Attributed text can be used in composit controls, such as tree view, to represent items with different types of information. For example, tree views in IDE.

attrtext script

attrtext.ijs defines attributed text commands as gl2 extension. [{{#file: ""}} Download script: ]

require 'gl2 strings'
cocurrent 'jgl2'

«glattr»

«glattrxy»

«glattrtext»

hexcol=: ({&'0123456789abcdef')@:((6#16)&#:)
glrgbx=: glrgb@(256 256 256&#:)

glattr defines a list of semicolon-separated text attributes, each a fontspec with optional hex color #rrggbb. For example,
  glattr '"Arial" 10;"Arial" 14 italic #ff0000' [{{#file: ""}} Download script: ]

glattr=: 3 : 0
  'ATTRFONT_jgl2_ ATTRCOL_jgl2_'=: |:2{."1 '#' cut every ';' cut y
  a=. ''
  for_i. ATTRFONT do.
    glfont >i
    a=. a, 1 { glqtextmetrics ''
  end.
  ATTRASC_jgl2_=: (>./ - ]) a
)

glattrxy is replacement for gltextxy except that it remembers the value set, necessary to maintain ascent offsets and horizontal stride. For example,
  glattrxy 36 10 [{{#file: ""}} Download script: ]

glattrxy=: 3 : 0
  gltextxy ATTRXY_jgl2_=: y
)

glattrtext writes prefix-separated text fragments, each prepended with glattr index and space, starting at glattrxy; all spaces except one after index are part of the text; replacement for gltext. For example,
  glattrtext '\0 Normal \1 Italic' [{{#file: ""}} Download script: ]

glattrtext=: 3 : 0
  'dx dy'=. ATTRXY
  for_t. <;._1 y do.
    a=. 0". ' ' taketo >t
    t=. ' ' takeafter >t
    glfont a {:: ATTRFONT
    gltextcolor'' [ glrgbx  hexcol^:_1 a {:: ATTRCOL
    gltextxy dx,dy + a {:: ATTRASC
    gltext t
    dx=. dx + {.glqextent t
  end.
)

Example

The following example generates a few sample attributed text fragments, as shown in the screenshot above. [{{#file: ""}} Download script: ]

require '~user/attrtext.ijs'
coinsert 'jgl2'

F=: 0 : 0
pc f; pn "Attributed Text";
xywh 6 6 100 60;cc g isigraph;
pas 6 6;pcenter;
rem form end;
)

f_run=: 3 : 0
  wd F
  wd 'pshow;'
)

f_close=: 3 : 0
  wd'pclose'
)

f_g_paint=: 3 : 0
  glclear''

  glattr '"Arial" 10;"Arial" 14 italic #ff0000;"Arial" 12 bold #0000ff'
  glattrxy 10 10
  glattrtext '\0 Normal \1 Italic \2 Bold'

  glattr '"Arial" 10;"Arial" 10 bold;"Arial" 10 #808080'
  glattrxy 10 36
  glattrtext '|1 Message |0 sayHello |2 text="Hello"'

  f=. ';"Book Antiqua" 36 bold '
  glattr f,'#3364c3',f,'#f31903',f,'#f7d727',f,'#44c406'
  glattrxy 10 50
  glattrtext '\0 G\1 o\2 o\0 g\3 l\1 e'

  glpaint''
)

f_run''

See Also

Contributed by Oleg Kobchenko