PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/text.sc

https://bitbucket.org/bunny351/ezd
Scala | 156 lines | 142 code | 14 blank | 0 comment | 6 complexity | 274facb6b29809eec0cce341e25ad999 MD5 | raw file
  1. ;;; ezd - easy drawing for X11 displays.
  2. ;;;
  3. ;;; The procedures in this module generate the GRAPHIC objects representing
  4. ;;; text.
  5. ;* Copyright 1990-1993 Digital Equipment Corporation
  6. ;* All Rights Reserved
  7. ;*
  8. ;* Permission to use, copy, and modify this software and its documentation is
  9. ;* hereby granted only under the following terms and conditions. Both the
  10. ;* above copyright notice and this permission notice must appear in all copies
  11. ;* of the software, derivative works or modified versions, and any portions
  12. ;* thereof, and both notices must appear in supporting documentation.
  13. ;*
  14. ;* Users of this software agree to the terms and conditions set forth herein,
  15. ;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  16. ;* right and license under any changes, enhancements or extensions made to the
  17. ;* core functions of the software, including but not limited to those affording
  18. ;* compatibility with other hardware or software environments, but excluding
  19. ;* applications which incorporate this software. Users further agree to use
  20. ;* their best efforts to return to Digital any such changes, enhancements or
  21. ;* extensions that they make and inform Digital of noteworthy uses of this
  22. ;* software. Correspondence should be provided to Digital at:
  23. ;*
  24. ;* Director of Licensing
  25. ;* Western Research Laboratory
  26. ;* Digital Equipment Corporation
  27. ;* 250 University Avenue
  28. ;* Palo Alto, California 94301
  29. ;*
  30. ;* This software may be distributed (but not offered for sale or transferred
  31. ;* for compensation) to third parties, provided such third parties agree to
  32. ;* abide by the terms and conditions of this notice.
  33. ;*
  34. ;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  35. ;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  36. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  37. ;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  38. ;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  39. ;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  40. ;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  41. ;* SOFTWARE.
  42. (module text)
  43. (include "struct.sch")
  44. (include "commands.sch")
  45. (include "ginfo.sch")
  46. (include "display.sch")
  47. (include "view.sch")
  48. (include "psdraw.sch")
  49. (include "graphic.sch")
  50. (include "xternal.sch")
  51. ;;; Text objects are created by the following procedure where x and y
  52. ;;; specify the minimum x,y coordinate of a rectangle containing the text.
  53. ;;;
  54. ;;; If width, height, and positioning information are supplied, then that
  55. ;;; information describes the size of the rectangle and how the text is
  56. ;;; to be placed in it. If the drawing is scaled such that the text won't
  57. ;;; fit in the box, then the text is not displayed.
  58. (define (TEXT x y bbox words color stipple font)
  59. (let* ((width (if bbox (car bbox) #f))
  60. (height (if bbox (cadr bbox) #f))
  61. (center-x (if bbox (caddr bbox) #f))
  62. (center-y (if bbox (cadddr bbox) #f))
  63. (dxfs (display-font->xfontstruct *display* font))
  64. (extent (xtextextents dxfs words (string-length words)))
  65. (dim (cadddr extent))
  66. (lbearing (xcharstruct-lbearing dim))
  67. (rbearing (xcharstruct-rbearing dim))
  68. (text-width (xcharstruct-width dim))
  69. (ascent (xfontstruct-max_bounds-ascent dxfs))
  70. (descent (xfontstruct-max_bounds-descent dxfs))
  71. (text-height (+ ascent descent)))
  72. (define (BB-X)
  73. (+ (min (user->x x)
  74. (user->x (+ x (if bbox
  75. width
  76. (width->user text-width)))))
  77. (case center-x
  78. ((center) (quotient (- (user->width width)
  79. text-width)
  80. 2))
  81. ((right) (- (user->width width) text-width))
  82. (else 0))))
  83. (define (BB-Y)
  84. (+ (min (user->y y)
  85. (user->y (+ y (if bbox
  86. height
  87. (height->user text-height)))))
  88. (case center-y
  89. ((center) (quotient (- (user->height height)
  90. text-height)
  91. 2))
  92. ((down) (- (user->height height) text-height))
  93. (else 0))))
  94. (define (INSIDE-BBOX)
  95. (or (not bbox)
  96. (and (<= (width->user text-width) width)
  97. (<= (height->user text-height) height))))
  98. (make-graphic
  99. #f
  100. (lambda ()
  101. (let ((x (bb-x))
  102. (y (bb-y)))
  103. (list (- x 1) y
  104. (+ x text-width) (+ y text-height))))
  105. (if (eq? color 'clear)
  106. draw-clear
  107. (lambda ()
  108. (if (inside-bbox)
  109. (xdrawstring *dpy* *xwindow*
  110. (cv-gc #f color stipple #f font #f)
  111. (- (bb-x) lbearing) (+ (bb-y) ascent)
  112. words (string-length words)))))
  113. (if (eq? color 'clear)
  114. draw-clear
  115. (lambda ()
  116. (when (inside-bbox)
  117. (pscolor color)
  118. (psfont font)
  119. (pscommand "xwindowmatrix" "setmatrix"
  120. (- (bb-x) lbearing) (+ (bb-y) ascent)
  121. "moveto" (psstring words) "show"
  122. "viewmatrix" "setmatrix"))))
  123. (lambda (minx miny maxx maxy) #t))))
  124. (define (CENTER-X? x) (memq x '(left center right)))
  125. (define (CENTER-Y? x) (memq x '(up center down)))
  126. (define-ezd-command
  127. `(text ,number? ,number? (optional ,number? ,number? ,center-x? ,center-y?)
  128. ,string? (optional ,color?) (optional ,stipple?) (optional ,string?))
  129. "(text x y [width height center-x center-y] [\"<text string>\" [<color>] [<stipple>] [\"fontname\"])"
  130. text)
  131. ;;; Utility function to compute the height and width in pixels of a text string
  132. ;;; for a given font (or #f for the default font). Note that the height is
  133. ;;; always specified as the maximum height for the font.
  134. (define (TEXT->HEIGHT-WIDTH words font)
  135. (let* ((dxfs (display-font->xfontstruct *display* font))
  136. (extent (xtextextents dxfs words (string-length words)))
  137. (dim (cadddr extent))
  138. (text-width (xcharstruct-width dim))
  139. (ascent (xfontstruct-max_bounds-ascent dxfs))
  140. (descent (xfontstruct-max_bounds-descent dxfs))
  141. (text-height (+ ascent descent)))
  142. (list text-height text-width)))