home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / Topware / gimp / gimp-setup-20001226.exe / Main / asc2img.scm < prev    next >
Encoding:
GIMP Script-Fu Script  |  2000-12-27  |  6.8 KB  |  241 lines

  1. ; Chris Gutteridge / ECS Dept. University of Southampton, England
  2. ; "ASCII 2 Image" script for the Gimp.
  3. ;
  4. ; 8th April 1998
  5. ; Takes a filename of an ASCII file and converts it to a gimp image.
  6. ; Does sensible things to preserve indents (gimp-text strips them)
  7. ;
  8. ; cjg@ecs.soton.ac.uk
  9.  
  10. ; This program is free software; you can redistribute it and/or modify
  11. ; it under the terms of the GNU General Public License as published by
  12. ; the Free Software Foundation; either version 2 of the License, or
  13. ; (at your option) any later version.
  14. ; This program is distributed in the hope that it will be useful,
  15. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ; GNU General Public License for more details.
  18. ; You should have received a copy of the GNU General Public License
  19. ; along with this program; if not, write to the Free Software
  20. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ; Define the function:
  23.  
  24. (define (script-fu-asc-2-img inFile
  25.                  inFont
  26.                  inFontSize
  27.                  inTextColor
  28.                              inTrans
  29.                              inBackColor
  30.                  inBufferAmount)
  31.     
  32.   (set! old-bg (car (gimp-palette-get-background)))
  33.  
  34.   (set! theImage (car (gimp-image-new 10 10 RGB) ) )
  35.   
  36.   (set! theLayer (car (gimp-layer-new theImage
  37.                       10
  38.                       10
  39.                       RGBA_IMAGE
  40.                       "layer 1"
  41.                       100
  42.                       NORMAL) ) )
  43.  
  44.   (gimp-palette-set-background inBackColor)
  45.   (gimp-layer-set-name theLayer "Background")
  46.   (gimp-image-add-layer theImage theLayer 0)
  47.  
  48.   (script-fu-asc-2-img-layer theImage theLayer inFile inFont inFontSize
  49.                  inTextColor inBufferAmount)
  50.   
  51.   (set! theBuffer (* inFontSize (/ inBufferAmount 100) ) )
  52.   (set! theImageWidth (+ theImageWidth theBuffer theBuffer ))
  53.   (set! theImageHeight (+ theImageHeight theBuffer theBuffer ))
  54.   
  55.   (gimp-image-resize theImage
  56.              theImageWidth
  57.              theImageHeight
  58.              theBuffer
  59.              theBuffer)
  60.   (gimp-layer-resize theLayer
  61.              theImageWidth
  62.              theImageHeight
  63.              theBuffer
  64.              theBuffer)
  65.   (gimp-selection-all theImage)
  66.   (if (= inTrans TRUE)
  67.       (gimp-edit-clear theLayer)
  68.       (gimp-edit-fill theLayer BG-IMAGE-FILL)
  69.       )
  70.   (gimp-selection-none theImage)
  71.   
  72.   (gimp-palette-set-background old-bg)
  73.   (gimp-display-new theImage)
  74.   
  75.   (gimp-image-clean-all theImage)
  76.   
  77.   (gimp-displays-flush)
  78.   (cons theImage  ()   )
  79. )
  80.  
  81. (define (script-fu-asc-2-img-layer inImage
  82.                    inLayer
  83.                    inFile
  84.                        inFont
  85.                        inFontSize
  86.                    inTextColor)
  87.   
  88.   (set! old-fg (car (gimp-palette-get-foreground)))
  89.   (set! theImage inImage)
  90.   (set! theLayer inLayer)
  91.   (set! theFile (fopen inFile))
  92.   
  93.   (set! otherLayers (cadr (gimp-image-get-layers theImage)))
  94.   (set! nLayers (car (gimp-image-get-layers theImage)))
  95.   (set! n nLayers)
  96.   
  97.   (gimp-palette-set-foreground inTextColor)
  98.   (gimp-selection-none theImage)
  99.   (set! theData ())
  100.   (set! theIndentList ())
  101.   (set! theChar "X")
  102.   (while (not (equal? () theChar))
  103.      (set! allspaces TRUE)
  104.      (set! theIndent 0)
  105.      (set! theLine "")
  106.      (while     (begin     (set! theChar (fread 1 theFile))
  107.                 (and     (not (equal? "\n" theChar))
  108.                     (not (equal? () theChar))
  109.                     )
  110.                 )
  111.             (cond     (    (equal? theChar "\t")
  112.                     (set! theChar "        ")
  113.                     (if (= allspaces TRUE)
  114.                         (set! theIndent (+ theIndent 8))
  115.                         ())
  116.                     )
  117.                 (    (equal? theChar " ")
  118.                     (if (= allspaces TRUE)
  119.                         (set! theIndent (+ theIndent 1))
  120.                         ())
  121.                     )
  122.                 (TRUE (set! allspaces FALSE))
  123.                 )
  124.             (set! theLine (string-append theLine theChar))
  125.             )
  126.      (if     (= allspaces TRUE)
  127.         (set! theLine "")
  128.         ()
  129.         )
  130.      (if     (and     (equal? () theChar)
  131.             (equal? "" theLine)
  132.             )
  133.         ()
  134.         (begin     (set! theData (cons theLine theData))
  135.             (set! theIndentList
  136.                   (cons theIndent theIndentList))
  137.             )
  138.         )
  139.      )
  140.   
  141.   (set! theText (car (gimp-text-fontname theImage
  142.                      -1
  143.                      0
  144.                      0
  145.                      "X"
  146.                      0
  147.                      TRUE
  148.                      inFontSize
  149.                      PIXELS
  150.                      inFont)))
  151.   (set! theCharWidth (car (gimp-drawable-width  theText) ))
  152.   (gimp-edit-cut theText)
  153.   
  154.   (set! theImageHeight 0)
  155.   (set! theImageWidth 0)
  156.   (cjg-add-text (reverse theData)
  157.         (reverse theIndentList)
  158.         inFont
  159.         inFontSize)
  160.   (set! n nLayers)
  161.   (gimp-palette-set-foreground old-fg)
  162.   (gimp-displays-flush)
  163.   )
  164.  
  165. (define (cjg-add-text inData inIndentList inFont inFontSize)
  166.   (if     (equal? () inData)
  167.     ()
  168.     (let     ((theLine (car inData)) (theIndent (car inIndentList)))
  169.       (if     (equal? "" theLine)
  170.         ()
  171.         (begin
  172.           (set! theText (car (gimp-text-fontname theImage
  173.                              -1
  174.                              0
  175.                              0
  176.                              (string-append
  177.                               " " theLine)
  178.                              0
  179.                              TRUE
  180.                              inFontSize
  181.                              PIXELS
  182.                              inFont)))
  183.           (set! theLineHeight (car (gimp-drawable-height  theText) ) )
  184.           (gimp-layer-set-offsets theText
  185.                       (* theCharWidth theIndent)
  186.                       (+ theImageHeight
  187.                          (- inFontSize theLineHeight)))
  188.           (set! theImageWidth (max
  189.                        (+ (car (gimp-drawable-width  theText) )
  190.                       (* theCharWidth theIndent))
  191.                        theImageWidth ))
  192.           (if (= (car (gimp-layer-is-floating-sel theText)) TRUE)
  193.                       (gimp-floating-sel-anchor theText)
  194.               ()
  195.               )
  196.                   (gimp-layer-set-name theText theLine)
  197.           )
  198.         )
  199.       (set! theImageHeight
  200.         (+ theImageHeight inFontSize))
  201.       (cjg-add-text (cdr inData) (cdr inIndentList) inFont inFontSize)))
  202.   )
  203.  
  204. ; Register the function with the GIMP:
  205.  
  206. (script-fu-register
  207.  "script-fu-asc-2-img"
  208.  _"<Toolbox>/Xtns/Script-Fu/Utils/ASCII to Image..."
  209.  "Create a new image containing text from a simple text file"
  210.  "Chris Gutteridge: cjg@ecs.soton.ac.uk"
  211.  "8th April 1998"
  212.  "Chris Gutteridge / ECS @ University of Southampton, England"
  213.  ""
  214.  SF-FILENAME   _"Filename"               "afile"
  215.  SF-FONT       _"Font"                   "-*-Charter-*-r-*-*-24-*-*-*-p-*-*-*"
  216.  SF-ADJUSTMENT _"Font Size (pixels)"     '(45 2 1000 1 10 0 1)
  217.  SF-COLOR      _"Text Color"             '(0 0 0)
  218.  SF-TOGGLE     _"Transparent Background" FALSE
  219.  SF-COLOR      _"Background Color"       '(255 255 255)
  220.  SF-ADJUSTMENT _"Buffer Amount (% Height of Text)" '(35 0 100 1 10 0 0)
  221. )
  222.  
  223. (script-fu-register
  224.  "script-fu-asc-2-img-layer"
  225.  _"<Image>/Script-Fu/Utils/ASCII to Image Layer..."
  226.  "Create a new layer of text from a simple text file"
  227.  "Chris Gutteridge: cjg@ecs.soton.ac.uk"
  228.  "30th April 1998"
  229.  "Chris Gutteridge / ECS @ University of Southampton, England"
  230.  "*"
  231.  SF-IMAGE "Image" 0
  232.  SF-DRAWABLE "Layer" 0
  233.  SF-FILENAME _"Filename"             "afile"
  234.  SF-FONT     _"Font"                 "-*-Charter-*-r-*-*-24-*-*-*-p-*-*-*"
  235.  SF-ADJUSTMENT _"Font Size (pixels)" '(45 2 1000 1 10 0 1)
  236.  SF-COLOR    _"Text Color"           '(0 0 0)
  237. )
  238.