home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 July / CHIP-1999-07.iso / software / gimp / GIMP-SET.EXE / file0379_39f462_39fccc.bin < prev    next >
Encoding:
Text File  |  1999-02-28  |  7.0 KB  |  261 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.                              inFlatten)
  32.     
  33.   (set! theImage (car (gimp-image-new 10 10 RGB) ) )
  34.   
  35.     
  36.  
  37.  
  38.   (set! theLayer (car (gimp-layer-new theImage
  39.                       10
  40.                       10
  41.                       RGBA_IMAGE
  42.                       "layer 1"
  43.                       100
  44.                       NORMAL) ) )
  45.  
  46.   (gimp-palette-set-background inBackColor)
  47.   (gimp-layer-set-name theLayer "Background")
  48.   (gimp-image-add-layer theImage theLayer 0)
  49.  
  50. (script-fu-asc-2-img-layer theImage theLayer inFile inFont inFontSize
  51.                  inTextColor inBufferAmount inFlatten)
  52.  
  53.     (set! theBuffer (* inFontSize (/ inBufferAmount 100) ) )
  54.     (set! theImageWidth (+ theImageWidth theBuffer theBuffer ))
  55.     (set! theImageHeight (+ theImageHeight theBuffer theBuffer ))
  56.  
  57.         (gimp-image-resize theImage
  58.                theImageWidth
  59.                theImageHeight
  60.                theBuffer
  61.                theBuffer)
  62.         (gimp-layer-resize theLayer
  63.                theImageWidth
  64.                theImageHeight
  65.                theBuffer
  66.                theBuffer)
  67.       (gimp-selection-all theImage)
  68.         (if (= inTrans TRUE)
  69.           (gimp-edit-clear theLayer)
  70.           (gimp-edit-fill theLayer)
  71.         )
  72.       (gimp-selection-none theImage)
  73.  
  74.         (if (= inFlatten TRUE)
  75.         (gimp-image-merge-visible-layers theImage 0)
  76.             ()
  77.         )
  78.  
  79.  
  80.     (gimp-display-new theImage)
  81.     
  82.     (gimp-image-clean-all theImage)
  83.     
  84.     (gimp-displays-flush)
  85.     (cons theImage  ()   )
  86. )
  87.  
  88. (define (script-fu-asc-2-img-layer inImage
  89.                    inLayer
  90.                 inFile
  91.                        inFont
  92.                        inFontSize
  93.                  inTextColor
  94.                              inFlatten)
  95.     
  96.   (set! theImage inImage)
  97.   (set! theLayer inLayer)
  98.   (set! theFile (fopen inFile))
  99.  
  100.   (set! otherLayers (cadr (gimp-image-get-layers theImage)))
  101.   (set! nLayers (car (gimp-image-get-layers theImage)))
  102.   (set! n nLayers)
  103.   (if (= inFlatten TRUE)
  104.       (while (> n 0) (set! n (- n 1)) (gimp-layer-set-visible (aref otherLayers n) FALSE) )
  105.       ()
  106.   )
  107.  
  108.  
  109.   (gimp-palette-set-foreground inTextColor)
  110.   (gimp-selection-none theImage)
  111.   (set! theData ())
  112.   (set! theIndentList ())
  113.   (set! theChar "X")
  114.   (while (not (equal? () theChar))
  115.      (set! allspaces TRUE)
  116.      (set! theIndent 0)
  117.      (set! theLine "")
  118.      (while     (begin     (set! theChar (fread 1 theFile))
  119.                 (and     (not (equal? "\n" theChar))
  120.                     (not (equal? () theChar))
  121.                     )
  122.                 )
  123.             (cond     (    (equal? theChar "\t")
  124.                     (set! theChar "        ")
  125.                     (if (= allspaces TRUE)
  126.                         (set! theIndent (+ theIndent 8))
  127.                         ())
  128.                 )
  129.                 (    (equal? theChar " ")
  130.                     (if (= allspaces TRUE)
  131.                         (set! theIndent (+ theIndent 1))
  132.                         ())
  133.                 )
  134.                 (TRUE (set! allspaces FALSE))
  135.             )
  136.             (set! theLine (string-append theLine theChar))
  137.         )
  138.         (if     (= allspaces TRUE)
  139.             (set! theLine "")
  140.             ()
  141.         )
  142.         (if     (and     (equal? () theChar)
  143.                 (equal? "" theLine)
  144.             )
  145.             ()
  146.             (begin     (set! theData (cons theLine theData))
  147.                 (set! theIndentList
  148.                       (cons theIndent theIndentList))
  149.                 )
  150.             )
  151.         )
  152.  
  153.   (set! theText (car (gimp-text-fontname theImage
  154.                 -1
  155.                 0
  156.                 0
  157.                 "X"
  158.                 0
  159.                 TRUE
  160.                 inFontSize
  161.                 PIXELS
  162.                 inFont)))
  163.      (set! theCharWidth (car (gimp-drawable-width  theText) ))
  164.     (gimp-edit-cut theText)
  165.  
  166.     (set! theImageHeight 0)
  167.     (set! theImageWidth 0)
  168.     (cjg-add-text (reverse theData)
  169.               (reverse theIndentList)
  170.               inFont
  171.               inFontSize)
  172.       (if (= inFlatten TRUE)
  173.         (gimp-image-merge-visible-layers theImage 0)
  174.               ()
  175.       )
  176.       (set! n nLayers)
  177.       (if (= inFlatten TRUE)
  178.               (while (> n 0) (set! n (- n 1)) (gimp-layer-set-visible (aref otherLayers n) TRUE) )
  179.               ()
  180.       )
  181.     (gimp-displays-flush)
  182. )
  183.  
  184. (define (cjg-add-text inData inIndentList inFont inFontSize)
  185.   (if     (equal? () inData)
  186.     ()
  187.     (let     ((theLine (car inData)) (theIndent (car inIndentList)))
  188.       (if     (equal? "" theLine)
  189.         ()
  190.         (begin
  191.           (set! theText (car (gimp-text-fontname theImage
  192.                         -1
  193.                         0
  194.                         0
  195.                         (string-append " " theLine)
  196.                         0
  197.                         TRUE
  198.                         inFontSize
  199.                         PIXELS
  200.                         inFont)))
  201.           (set! theLineHeight (car (gimp-drawable-height  theText) ) )
  202.           (gimp-layer-set-offsets theText
  203.                       (* theCharWidth theIndent)
  204.                       (+ theImageHeight
  205.                          (- inFontSize theLineHeight)))
  206.           (set! theImageWidth (max
  207.                        (+ (car (gimp-drawable-width  theText) )
  208.                       (* theCharWidth theIndent))
  209.                        theImageWidth ))
  210.           (if (= (car (gimp-layer-is-floating-sel theText)) TRUE)
  211.                       (gimp-floating-sel-anchor theText)
  212.               ()
  213.                   )
  214.                   (gimp-layer-set-name theText theLine)
  215.           )
  216.         )
  217.       (set! theImageHeight
  218.         (+ theImageHeight inFontSize))
  219.       (cjg-add-text (cdr inData) (cdr inIndentList) inFont inFontSize)))
  220. )
  221.  
  222. ; Register the function with the GIMP:
  223.  
  224. (script-fu-register
  225.  "script-fu-asc-2-img"
  226.  "<Toolbox>/Xtns/Script-Fu/Utils/ASCII 2 Image"
  227.  "foo"
  228.  "Chris Gutteridge: cjg@ecs.soton.ac.uk"
  229.  "8th April 1998"
  230.  "Chris Gutteridge / ECS @ University of Southampton, England"
  231.  "bar"
  232.  SF-FILENAME "File Name"       "afile"
  233.  SF-FONT "Font"       "-*-Charter-*-r-*-*-24-*-*-*-p-*-*-*"
  234.  SF-ADJUSTMENT "Font Size (pixels)" '(45 2 1000 1 10 0 1)
  235.  SF-COLOR "Text Color"      '(0 0 0)
  236.  SF-TOGGLE "Transparent BG?" FALSE
  237.  SF-COLOR "Background Color"      '(255 255 255)
  238.  SF-ADJUSTMENT "Buffer amount (% height of text)" '(35 0 100 1 10 0 0)
  239.  SF-TOGGLE "Flatten Image?" TRUE
  240. )
  241.  
  242. (script-fu-register
  243.  "script-fu-asc-2-img-layer"
  244.  "<Image>/Script-Fu/Utils/ASCII 2 Image Layer"
  245.  "foo"
  246.  "Chris Gutteridge: cjg@ecs.soton.ac.uk"
  247.  "30th April 1998"
  248.  "Chris Gutteridge / ECS @ University of Southampton, England"
  249.  "bar"
  250.  SF-IMAGE "Image" 0
  251.  SF-DRAWABLE "Layer" 0
  252.  SF-FILENAME "File Name"       "afile"
  253.  SF-FONT "Font"       "-*-Charter-*-r-*-*-24-*-*-*-p-*-*-*"
  254.  SF-ADJUSTMENT "Font Size (pixels)" '(45 2 1000 1 10 0 1)
  255.  SF-COLOR "Text Color"      '(0 0 0)
  256.  SF-TOGGLE "Flatten Image?" TRUE
  257. )
  258.