diff mde/font/font.d @ 100:0ea4a3e651ae

There is now a position marker for text editing. Changed the way fonts are configured. Actually, not much of the new way exists yet.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 15 Nov 2008 17:39:14 +0000
parents 49e7cfed4b34
children 71f0f1f83620
line wrap: on
line diff
--- a/mde/font/font.d	Fri Nov 14 12:44:32 2008 +0000
+++ b/mde/font/font.d	Sat Nov 15 17:39:14 2008 +0000
@@ -21,14 +21,10 @@
 import mde.font.FontTexture;
 import mde.font.exception;
 
-import mde.file.mergetag.Reader;
-import mde.file.mergetag.DataSet;
-import mde.setup.paths;
 import mde.setup.exception;     // InitStage stuff
 
 import derelict.freetype.ft;
 
-import mde.file.deserialize;
 import tango.stdc.stringz;
 import Util = tango.text.Util;
 import tango.util.log.Log : Log, Logger;
@@ -46,7 +42,7 @@
  * Particular to a font and size, and any other effects like bold/italic if ever implemented.
  * 
  * Note: it is not currently intended to be thread-safe. */
-class FontStyle : IDataSection
+class FontStyle
 {
     //BEGIN Static: manager
     static {
@@ -55,8 +51,7 @@
                 fontTex.drawTexture;
         }
         
-        /** Load the freetype library with settings from the file fileName. */
-        private const fileName = "fonts";
+        /** Load the freetype library with settings from options. */
         StageState initialize () {
             if (FT_Init_FreeType (&library))
                 throw new fontException ("error initialising the FreeType library");
@@ -66,7 +61,7 @@
             FT_Library_Version (library, &maj, &min, &patch);
             if (maj != 2 || min != 3) {
                 logger.warn ("Using an untested FreeType version: {}.{}.{}", maj, min, patch);
-                logger.info ("The only tested version of freetype is 2.3.5");
+                logger.info ("The only tested version of freetype is 2.3");
             }
             
             // Set LCD filtering method if LCD rendering is enabled.
@@ -86,51 +81,19 @@
                 fontOpts.renderMode = FT_LOAD_TARGET_NORMAL;
             }
             
-            /* Load font settings
-             *
-             * Each mergetag section corresponds to a font; each is loaded whether used or not
-             * (however the actual font files are only loaded on use). A fallback id must be
-             * provided in the header which must match a loaded font name; if a non-existant font
-             * is requested a warning will be logged and this font returned. */
-            char[] fallbackName;
-            try {
-                IReader reader;
-                reader = confDir.makeMTReader (fileName, PRIORITY.LOW_HIGH, null, true);
-                reader.dataSecCreator = delegate IDataSection(ID id) {
-                    auto f = new FontStyle;
-                    fonts[id] = f;
-                    return f;
-                };
-                reader.read;
-                
-                // get fallback name
-                char[]* p = "fallback" in reader.dataset.header._charA;
-                if (p is null)
-                    throw new fontException ("No fallback font style specified");
-                fallbackName = *p;
-            } catch (NoFileException) {
-                throw new fontException ("No font settings file (fonts.[mtt|mtb])");
-            } catch (Exception e) {
-                throw new fontException ("Reading font settings failed: "~e.msg);
-            }
-            
-            // Find the fallback
-            FontStyle* p = fallbackName in fonts;
-            if (p is null)
-                throw new fontException ("Fallback font style specified is not found");
-            fallback = *p;
-            // Load the fallback now, to ensure it's available.
-            // Also note that get() doesn't make sure the fallback is loaded before returning it.
-            fallback.load;
-            
+            // Load the fallback font; if it throws let exception abort program
+	    fallback = new FontStyle (fontOpts.defaultFont(), fontOpts.defaultSize());
+	    
             return StageState.ACTIVE;
         }
         
         /** Cleanup: delete all fonts. */
         StageState cleanup () {
             // Clear loaded fonts (each has an FT_Face object needing to be freed):
-            foreach (fs; fonts)
+            /* FIXME
+	    foreach (fs; fonts)
                 fs.freeFace;
+	    */
             
             FT_Done_FreeType (library); // free the library
             
@@ -144,55 +107,25 @@
           * Uses fallback font-style if the desired style isn't known about or fails to load, so
           * this function should never fail or throw, in theory (unless out of memory). The
           * fallback should already be loaded. */
-        FontStyle get(char[] name) {
-            FontStyle* p = name in fonts;
-            if (p is null) {
-                logger.warn ("Font style "~name~" requested but not found; reverting to the fallback style.");
-                fonts[name] = fallback;	// set to prevent another warning getting logged
-                return fallback;
-            }
-            // Got it, but we need to make sure it's loaded:
-            try {
-                p.load;
-            } catch (Exception e) {
-                logger.warn ("Font style "~name~" failed to load; reverting to the fallback style.");
-                return fallback;
-            }
-            return *p;
+        FontStyle getDefault () {
+	    //FIXME: Ddoc, new purpose; rename
+            return fallback;
         }
         
     private:
         FT_Library	library;
         FontTexture	fontTex;
-        FontStyle[ID]	fonts;		// all font styles known about; not necessarily loaded
-        FontStyle	fallback;	// used when requested font isn't in fonts
+        //FontStyle[char[]]	fonts;	// loaded fonts, by file name	 FIXME: use hash of struct { char[] path; int size; }?
+        FontStyle	fallback;	// default & used when requested font can't be loaded
     }
     //END Static
     
-    this() {}
-    
-    //BEGIN Mergetag code
-    //NOTE: would it be better not to use a new mergetag file for this?
-    //FIXME: revise when gui can set options
-    void addTag (char[] tp, ID id, char[] dt) {
-        if (tp == "char[]") {
-            if (id == "path")
-                path = parseTo!(char[]) (dt);
-        }
-        else if (tp == "int") {
-            if (id == "size")
-                size = parseTo!(int) (dt);
-        }
-    }
-    void writeAll (ItemDelg) {}		// no writing the config for now
-    //END Mergetag code
-    
     /** Load the font file.
      *
      * Even if the same font is used at multiple sizes, multiple copies of FT_Face are used.
      * Sharing an FT_Face would require calling FT_Set_Pixel_Sizes each time a glyph is rendered or
      * swapping the size information (face.size)? */
-    void load ()
+    this (char[] path, int size)
     in {
         assert (library !is null, "font: library is null");
     } body {
@@ -206,12 +139,18 @@
          *	Use of face.size.metrics.height property.
          */
         
-        if (FT_Set_Pixel_Sizes (face, 0,size))
+        if (FT_Set_Pixel_Sizes (face, 0, size))
             throw new fontLoadException ("Unable to set pixel size");
         
         // Create if necessary:
         if (fontTex is null)
             fontTex = new FontTexture;
+	
+	return this;
+    }
+    
+    this (FontStyle font, int size) {
+	//FIXME: copy font's face and set new size?
     }
     
     /** Update a TextBlock cache, as used by the textBlock function.
@@ -234,8 +173,16 @@
     
     /** Draw a block of text (may inlcude new-lines).
      *
+     * Params:
+     *	x =	Top left x-coordinate of text block
+     *	y =	Top left y-coordinate of text block
+     *	str =	Text to render
+     *	cache =	An (optional) TextBlock used for rendering this text, to save some CPU work. See updateBlock
+     *	col =	Colour to render the text; see mde.types.Colour
+     *	index =	Either the index of the character to draw with an edit cursor or size_t.max. Not
+     *		the index within str, but the number of characters, excluding new-lines.
      * The text block is drawn with top-left corner at x,y. To put the text's baseline at a given
-     * y coordinate would require some changes. Line height is fixed based on largest glyph.
+     * y coordinate would require some changes. Line height is fixed (see getLineSeparation).
      * Due to hinter, glyphs are not guaranteed to lie within the "bounding box" defined by cache.
      * Can be changed to test size of each glyph if necessary.
      *
@@ -247,7 +194,7 @@
      * ---------------------------------
      * char[] str;
      * TextBlock strCache;
-     * textBlock (x, y, str, strCache);
+     * textBlock (x, y, str, strCache, Colour.WHITE);
      * ---------------------------------
      * The TextBlock cache will be updated as necessary. Besides the initial update, this will only
      * be if the font changes, or it is manually invalidated. This can be done by setting the
@@ -257,18 +204,18 @@
      * than this cache only serves as a small optimisation. However, the only way to get the size
      * of a text block is to use a TextBlock cache and update it, either with this function or with
      * the updateBlock function. */
-    void textBlock (int x, int y, char[] str, ref TextBlock cache, Colour col)
+    void textBlock (int x, int y, char[] str, ref TextBlock cache, Colour col, size_t index = size_t.max)
     in {
         assert (face, "FontStyle: face is null");
     } body {
         try {
-            fontTex.drawCache (face, str, cache, x, y, col);
+            fontTex.drawCache (face, str, cache, x, y, col, index);
         } catch (Exception e) {
             logger.warn ("Exception while drawing text: "~e.msg);
         }
     }
     /** ditto */
-    void textBlock (int x, int y, char[] str, Colour col)
+    void textBlock (int x, int y, char[] str, Colour col, size_t index = size_t.max)
     in {
         assert (face, "FontStyle: face is null");
     } body {
@@ -277,7 +224,7 @@
             // isn't really recommended anyway (and maintaining two versions of fontTex.drawText
             // would be horrible).
             TextBlock cache;
-            fontTex.drawCache (face, str, cache, x, y, col);
+            fontTex.drawCache (face, str, cache, x, y, col, index);
         } catch (Exception e) {
             logger.warn ("Exception while drawing text: "~e.msg);
         }
@@ -287,12 +234,12 @@
      *
      * Set the alpha by calling glColor*() first. See FontTexture.drawCacheA()'s documentation for
      * details. */
-    void textBlockA (int x, int y, char[] str, ref TextBlock cache, Colour col) 
+    void textBlockA (int x, int y, char[] str, ref TextBlock cache, Colour col, size_t index = size_t.max) 
     in {
         assert (face, "FontStyle: face is null");
     } body {
         try {
-            fontTex.drawCacheA (face, str, cache, x, y, col);
+	    fontTex.drawCacheA (face, str, cache, x, y, col, index);
         } catch (Exception e) {
             logger.warn ("Exception while drawing text: "~e.msg);
         }
@@ -312,16 +259,5 @@
     }
     
 private:
-    char[]	path;	// path to font file
-    int		size;	// font size
-    
     FT_Face	face;
-    
-    debug(mdeUnitTest) unittest {
-        // Don't do a unittest since font relies on loading the freetype library dynamically,
-        // normally done by Init. Also font is mostly visual and many problems will be obvious.
-    }
 }
-
-/+class OptionsFont : Options {
-    alias store!(+/
\ No newline at end of file