]> gitweb.factorcode.org Git - factor.git/commitdiff
Fix text rendering for fonts where the image bounds exceed the metric bounds, such...
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 21 Feb 2009 02:53:51 +0000 (20:53 -0600)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 21 Feb 2009 02:53:51 +0000 (20:53 -0600)
basis/core-graphics/core-graphics-tests.factor
basis/core-graphics/core-graphics.factor
basis/core-graphics/types/types.factor
basis/core-text/core-text.factor
basis/opengl/textures/textures.factor
basis/ui/gadgets/editors/editors.factor
basis/ui/gadgets/line-support/line-support.factor
basis/ui/gadgets/panes/panes.factor
basis/ui/images/images.factor
basis/ui/text/core-text/core-text.factor

index c54dc0a98b7b3d8036631e67c1da632b0429279f..b032a7763a869f606890ea059cb93f8e244dae32 100644 (file)
@@ -3,4 +3,6 @@
 USING: tools.test core-graphics kernel images ;
 IN: core-graphics.tests
 
-[ t ] [ { 100 200 } [ drop ] make-bitmap-image image? ] unit-test
\ No newline at end of file
+[ t ] [ { 100 200 } [ drop ] make-bitmap-image image? ] unit-test
+
+[ ] [ dummy-context drop ] unit-test
\ No newline at end of file
index 19d8b8116afec0dfc38b6573eada275b67de8dd2..358e784e333ebc96a4f213bfd8bda1fec16bc2d9 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien.c-types alien.destructors alien.syntax accessors
+USING: alien alien.c-types alien.destructors alien.syntax accessors
 destructors fry kernel math math.bitwise sequences libc colors
 images core-graphics.types core-foundation.utilities ;
 IN: core-graphics
@@ -117,8 +117,8 @@ FUNCTION: void* CGBitmapContextGetData ( CGContextRef c ) ;
 : bitmap-color-space ( -- color-space )
     CGColorSpaceCreateDeviceRGB &CGColorSpaceRelease ;
 
-: <CGBitmapContext> ( dim -- context )
-    [ malloc-bitmap-data ] [ first2 8 ] [ first 4 * ] tri
+: <CGBitmapContext> ( data dim -- context )
+    [ first2 8 ] [ first 4 * ] bi
     bitmap-color-space bitmap-flags CGBitmapContextCreate
     [ "CGBitmapContextCreate failed" throw ] unless* ;
 
@@ -134,8 +134,13 @@ FUNCTION: void* CGBitmapContextGetData ( CGContextRef c ) ;
 
 PRIVATE>
 
+: dummy-context ( -- context )
+    \ dummy-context [
+        [ 4 malloc { 1 1 } <CGBitmapContext> ] with-destructors
+    ] initialize-alien ;
+
 : make-bitmap-image ( dim quot -- image )
     [
-        [ [ <CGBitmapContext> &CGContextRelease ] keep ] dip
+        [ [ [ malloc-bitmap-data ] keep <CGBitmapContext> &CGContextRelease ] keep ] dip
         [ nip call ] [ drop [ bitmap-data ] keep <bitmap-image> ] 3bi
     ] with-destructors ; inline
index c7d86896c777e9c02b5a50f795988930564c5b8b..13e4285ea1770ddb603798ac3ee393aac34f93b9 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien.c-types alien.syntax kernel layouts math ;
+USING: alien.c-types alien.syntax kernel layouts
+math math.rectangles arrays ;
 IN: core-graphics.types
 
 << cell 4 = "float" "double" ? "CGFloat" typedef >>
@@ -33,6 +34,17 @@ C-STRUCT: CGRect
     { "CGPoint" "origin" }
     { "CGSize"  "size"   } ;
 
+: CGPoint>loc ( CGPoint -- loc )
+    [ CGPoint-x ] [ CGPoint-y ] bi 2array ;
+
+: CGSize>dim ( CGSize -- dim )
+    [ CGSize-w ] [ CGSize-h ] bi 2array ;
+
+: CGRect>rect ( CGRect -- rect )
+    [ CGRect-origin CGPoint>loc ]
+    [ CGRect-size CGSize>dim ]
+    bi <rect> ; inline
+
 : CGRect-x ( CGRect -- x )
     CGRect-origin CGPoint-x ; inline
 : CGRect-y ( CGRect -- y )
index 39d298476ddcd1cb9036c1c8f83812de1abddfad..0648128d116755fcddc977b1c2972cf34fba4fb0 100644 (file)
@@ -2,10 +2,10 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: arrays alien alien.c-types alien.syntax kernel destructors
 accessors fry words hashtables strings sequences memoize assocs math
-math.functions locals init namespaces combinators fonts colors cache
-core-foundation core-foundation.strings core-foundation.attributed-strings
-core-foundation.utilities core-graphics core-graphics.types
-core-text.fonts core-text.utilities ;
+math.vectors math.rectangles math.functions locals init namespaces
+combinators fonts colors cache core-foundation core-foundation.strings
+core-foundation.attributed-strings core-foundation.utilities
+core-graphics core-graphics.types core-text.fonts core-text.utilities ;
 IN: core-text
 
 TYPEDEF: void* CTLineRef
@@ -46,7 +46,7 @@ ERROR: not-a-string object ;
         CTLineCreateWithAttributedString
     ] with-destructors ;
 
-TUPLE: line font line metrics image disposed ;
+TUPLE: line font line metrics image loc dim disposed ;
 
 : compute-line-metrics ( open-font line -- line-metrics )
     [
@@ -65,7 +65,7 @@ TUPLE: line font line metrics image disposed ;
     } spread
     dup compute-height ;
 
-: bounds>dim ( bounds -- dim )
+: metrics>dim ( bounds -- dim )
     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
     [ ceiling >integer ]
     bi@ 2array ;
@@ -80,30 +80,48 @@ TUPLE: line font line metrics image disposed ;
     [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
 
-:: fill-selection-background ( context dim line string -- )
+:: fill-selection-background ( context loc dim line string -- )
     string selection? [
         context string color>> >rgba-components CGContextSetRGBFillColor
-        context dim line string selection-rect CGContextFillRect
+        context dim line string selection-rect
+        dup CGRect-x loc first - over set-CGRect-x
+        CGContextFillRect
     ] when ;
 
-: set-text-position ( context metrics -- )
-    [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
+: line-rect ( line -- rect )
+    dummy-context CTLineGetImageBounds ;
+
+: set-text-position ( context loc -- )
+    first2 [ neg ] bi@ CGContextSetTextPosition ;
+
+:: line-loc ( metrics loc dim -- loc )
+    loc first
+    metrics ascent>> ceiling dim second loc second + - 2array ;
 
 :: <line> ( font string -- line )
     [
         [let* | open-font [ font cache-font CFRetain |CFRelease ]
                 line [ string open-font font foreground>> <CTLine> |CFRelease ]
-                metrics [ open-font line compute-line-metrics ]
-                dim [ metrics bounds>dim ] |
+
+                rect [ line line-rect ]
+                (loc) [ rect CGRect-origin CGPoint>loc ]
+                (dim) [ rect CGRect-size CGSize>dim ]
+                (ext) [ (loc) (dim) v+ ]
+                loc [ (loc) [ floor ] map ]
+                ext [ (loc) (dim) [ + ceiling ] 2map ]
+                dim [ ext loc [ - >integer ] 2map ]
+                metrics [ open-font line compute-line-metrics ] |
             open-font line metrics
             dim [
                 {
                     [ font dim fill-background ]
-                    [ dim line string fill-selection-background ]
-                    [ metrics set-text-position ]
+                    [ loc dim line string fill-selection-background ]
+                    [ loc set-text-position ]
                     [ [ line ] dip CTLineDraw ]
                 } cleave
             ] make-bitmap-image
+            metrics loc dim line-loc
+            metrics metrics>dim
         ]
         f line boa
     ] with-destructors ;
index 02e818d5018c56312e325db71957e8bac75a9516..ee25be1d8ce60a0961e40037e7d8cbe65dd80575 100644 (file)
@@ -9,7 +9,7 @@ IN: opengl.textures
 
 : delete-texture ( id -- ) [ glDeleteTextures ] (delete-gl-object) ;
 
-TUPLE: texture texture-coords texture display-list disposed ;
+TUPLE: texture loc dim texture-coords texture display-list disposed ;
 
 <PRIVATE
 
@@ -72,10 +72,12 @@ M: BGRA component-order>format drop GL_BGRA_EXT GL_UNSIGNED_INT_8_8_8_8 ;
         GL_TEXTURE_BIT [
             GL_TEXTURE_COORD_ARRAY [
                 COLOR: white gl-color
-                [ [ GL_TEXTURE_2D ] dip texture>> glBindTexture ]
-                [ init-texture texture-coords>> gl-texture-coord-pointer ] bi
-                fill-rect-vertices (gl-fill-rect)
-                GL_TEXTURE_2D 0 glBindTexture
+                dup loc>> [
+                    [ [ GL_TEXTURE_2D ] dip texture>> glBindTexture ]
+                    [ init-texture texture-coords>> gl-texture-coord-pointer ] bi
+                    fill-rect-vertices (gl-fill-rect)
+                    GL_TEXTURE_2D 0 glBindTexture
+                ] with-translation
             ] do-enabled-client-state
         ] do-attribs
     ] do-enabled ;
@@ -85,19 +87,21 @@ M: BGRA component-order>format drop GL_BGRA_EXT GL_UNSIGNED_INT_8_8_8_8 ;
     { { 0 0 } { 1 0 } { 1 1 } { 0 1 } } [ v* ] with map
     float-array{ } join ;
 
-: make-texture-display-list ( dim texture -- dlist )
-    GL_COMPILE [ draw-textured-rect ] make-dlist ;
+: make-texture-display-list ( texture -- dlist )
+    GL_COMPILE [ [ dim>> ] keep draw-textured-rect ] make-dlist ;
 
 PRIVATE>
 
-: <texture> ( image -- texture )
-    dup dim>> { 0 0 } = [ drop texture new ] [
-        [ dim>> ]
-        [ dim>> texture-coords ]
-        [ power-of-2-image make-texture ] tri
-        f f texture boa
-        [ nip ] [ make-texture-display-list ] 2bi >>display-list
-    ] if ;
+: <texture> ( image loc -- texture )
+    texture new swap >>loc
+    swap
+    [ dim>> >>dim ] keep
+    [ dim>> { 0 0 } = ] keep '[
+        _
+        [ dim>> texture-coords >>texture-coords ]
+        [ power-of-2-image make-texture >>texture ] bi
+        dup make-texture-display-list >>display-list
+    ] unless ;
 
 M: texture dispose*
     [ texture>> [ delete-texture ] when* ]
index f3de3493620d530f8bec7da5af6a12c06c3a0ca6..1d129b39b89b6b7e3190a36fa1ec456449f686c5 100755 (executable)
@@ -169,11 +169,12 @@ TUPLE: selected-line start end first? last? ;
         '[ [ _ _ ] keep _ start/end-on-line 2array ] H{ } map>assoc
     ] [ drop f ] if ;
 
-:: draw-empty-selection ( line pair editor -- )
-    editor font>> :> font
-    pair first font line offset>x 0 2array [
+:: draw-selection ( line pair editor -- )
+    pair [ editor font>> line offset>x ] map :> pair
+    pair first 0 2array [
         editor selection-color>> gl-color
-        1 font font-metrics height>> 2array gl-fill-rect
+        pair second pair first - round 1 max
+        editor line-height 2array gl-fill-rect
     ] with-translation ;
 
 : draw-unselected-line ( line editor -- )
@@ -181,10 +182,13 @@ TUPLE: selected-line start end first? last? ;
 
 : draw-selected-line ( line pair editor -- )
     over all-equal? [
-        [ nip draw-unselected-line ] [ draw-empty-selection ] 3bi
+        [ nip draw-unselected-line ] [ draw-selection ] 3bi
     ] [
-        [ [ first2 ] [ selection-color>> ] bi* <selection> ] keep
-        draw-unselected-line
+        [ draw-selection ]
+        [
+            [ [ first2 ] [ selection-color>> ] bi* <selection> ] keep
+            draw-unselected-line
+        ] 3bi
     ] if ;
 
 M: editor draw-line ( line index editor -- )
@@ -197,7 +201,8 @@ M: editor draw-gadget*
     ] with-variable ;
 
 M: editor pref-dim*
-    [ font>> ] [ control-value ] bi text-dim ;
+    ! Add some space for the caret.
+    [ font>> ] [ control-value ] bi text-dim { 1 0 } v+ ;
 
 M: editor baseline font>> font-metrics ascent>> ;
 
index 87155fa018d4d1500f5e234c67ee0ca369a2e347..80feb31ad2215f83d177491c59a77c2c0b80d839 100644 (file)
@@ -21,7 +21,7 @@ M: line-gadget line-leading font>> font-metrics leading>> ;
 
 GENERIC: line-height ( gadget -- n )
 
-M: line-gadget line-height font>> font-metrics height>> ;
+M: line-gadget line-height font>> font-metrics height>> ceiling ;
 
 : y>line ( y gadget -- n ) line-height /i ;
 
index c4ac015a8a7709a3d8185ccac403b51432b2ecb1..7337d992d8c80f02b55954927199ab2ccb6029b7 100644 (file)
@@ -213,8 +213,12 @@ MEMO: specified-font ( assoc -- font )
 : apply-image-style ( style gadget -- style gadget )
     image [ nip <image-name> <icon> ] apply-style ;
 
+: apply-background-style ( style gadget -- style gadget )
+    background [ <solid> >>interior ] apply-style ;
+
 : style-label ( style gadget -- gadget )
     apply-font-style
+    apply-background-style
     apply-presentation-style
     apply-image-style
     nip ; inline
index fae809b009fb7a31b440d41dec891c30e26ede37..2b1caa8ab9d3fc95bbfcdfc7ec5ccf0738fb8efa 100644 (file)
@@ -19,7 +19,8 @@ MEMO: cached-image ( image-name -- image ) path>> load-image ;
 PRIVATE>
 
 : rendered-image ( path -- texture )
-    world get image-texture-cache [ cached-image <texture> ] cache ;
+    world get image-texture-cache
+    [ cached-image { 0 0 } <texture> ] cache ;
 
 : draw-image ( image-name -- )
     rendered-image draw-texture ;
index 482e95fd95a4245a3ad1fe6c6ab4604b49c4670b..785a9366cb63c8ed3f6a2abb02ccd1b355bea1f7 100644 (file)
@@ -15,7 +15,7 @@ M: core-text-renderer init-text-rendering
 
 M: core-text-renderer string-dim
     [ " " string-dim { 0 1 } v* ]
-    [ cached-line image>> dim>> ]
+    [ cached-line dim>> ]
     if-empty ;
 
 M: core-text-renderer finish-text-rendering
@@ -24,7 +24,7 @@ M: core-text-renderer finish-text-rendering
 
 : rendered-line ( font string -- texture )
     world get text-handle>>
-    [ cached-line image>> <texture> ]
+    [ cached-line [ image>> ] [ loc>> ] bi <texture> ]
     2cache ;
 
 M: core-text-renderer draw-string ( font string -- )