Changeset 49:bca7e2342d77
- Timestamp:
- 05/31/08 08:10:06
(7 months ago)
- Author:
- Diggory Hardy <diggory.hardy@gmail.com>
- branch:
- default
- convert_revision:
- ff010240373bc41302d578ef711f9723a8bfbcba
- Message:
Enabled getting the size of a text block (before or after rendering).
committer: Diggory Hardy <diggory.hardy@gmail.com>
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r48 |
r49 |
|
| 52 | 52 | |
|---|
| 53 | 53 | Done (for git log message): |
|---|
| 54 | | Implemented font rendering (grayscale only; i.e. non-LCD). |
|---|
| 55 | | FontTexture creates a texture and caches glyphs. |
|---|
| 56 | | Font supports multiple styles/faces, loaded from config file (should probably be loaded via Options instead). |
|---|
| 57 | | TextBlock cache for glyph placement within a string. |
|---|
| | 54 | Enabled getting the size of a text block (before or after rendering). |
|---|
| r48 |
r49 |
|
| 39 | 39 | |
|---|
| 40 | 40 | gui.draw (); |
|---|
| 41 | | FontStyle.drawTexture; |
|---|
| | 41 | debug FontStyle.drawTexture; |
|---|
| 42 | 42 | |
|---|
| 43 | 43 | GLenum err = glGetError(); |
|---|
| r48 |
r49 |
|
| 108 | 108 | this (IWindow wind, int[] data) { |
|---|
| 109 | 109 | if (data.length != 1) throw new WidgetDataException; |
|---|
| 110 | | mw = 100; //FIXME: set properly |
|---|
| 111 | | mh = 50; |
|---|
| | 110 | if (font is null) font = FontStyle.get("default"); |
|---|
| | 111 | font.updateBlock (str, textCache); |
|---|
| | 112 | mw = textCache.w; |
|---|
| | 113 | mh = textCache.h; |
|---|
| 112 | 114 | super (wind,data); |
|---|
| 113 | 115 | } |
|---|
| … | … | |
| 115 | 117 | void draw () { |
|---|
| 116 | 118 | super.draw(); |
|---|
| 117 | | if (font is null) font = FontStyle.get("default"); |
|---|
| 118 | | font.textBlock (x,y, "|â|â| .,.,.", textCache); // test new-lines and unicode characters |
|---|
| 119 | | //old string: "Text Widget\nαβγ â ÎÎÎ" |
|---|
| | 119 | font.textBlock (x,y, str, textCache); // test new-lines and unicode characters |
|---|
| 120 | 120 | } |
|---|
| 121 | 121 | |
|---|
| 122 | 122 | protected: |
|---|
| | 123 | const str = "Text Widget\nαβγ â ÎÎÎ"; |
|---|
| 123 | 124 | TextBlock textCache; |
|---|
| 124 | 125 | static FontStyle font; |
|---|
| r48 |
r49 |
|
| 29 | 29 | import Utf = tango.text.convert.Utf; |
|---|
| 30 | 30 | import tango.util.log.Log : Log, Logger; |
|---|
| 31 | | import tango.io.Stdout; |
|---|
| 32 | 31 | |
|---|
| 33 | 32 | private Logger logger; |
|---|
| … | … | |
| 98 | 97 | for (size_t i = 0; i < chrs.length; ++i) |
|---|
| 99 | 98 | { |
|---|
| 100 | | // First, get maxmimal yMax for the current line. |
|---|
| | 99 | // First, find yMax for the current line. |
|---|
| 101 | 100 | int yMax = 0; // Maximal glyph height above baseline. |
|---|
| 102 | 101 | for (size_t j = i; j < chrs.length; ++j) |
|---|
| … | … | |
| 148 | 147 | |
|---|
| 149 | 148 | cache.chars ~= cc; |
|---|
| | 149 | |
|---|
| | 150 | // Update rect total size. Top and left coords should be zero, so make width and |
|---|
| | 151 | // height maximal x and y coordinates. |
|---|
| | 152 | if (cc.xPos + cc.ga.w > cache.w) |
|---|
| | 153 | cache.w = cc.xPos + cc.ga.w; |
|---|
| | 154 | if (cc.yPos + cc.ga.h > cache.h) |
|---|
| | 155 | cache.h = cc.yPos + cc.ga.h; |
|---|
| 150 | 156 | } |
|---|
| 151 | 157 | // Now increment i and continue with the next line if there is one. |
|---|
| … | … | |
| 192 | 198 | debug scope (failure) |
|---|
| 193 | 199 | logger.warn ("FontTexture.addGlyph failed!"); |
|---|
| 194 | | //Stdout ("Adding glyph ")(chr).newline; |
|---|
| | 200 | |
|---|
| 195 | 201 | auto gi = FT_Get_Char_Index (face, chr); |
|---|
| 196 | 202 | auto g = face.glyph; |
|---|
| … | … | |
| 215 | 221 | ga.advanceX = g.advance.x >> 6; |
|---|
| 216 | 222 | ga.index = gi; |
|---|
| 217 | | Stdout ("Glyph left is: ")(ga.left).newline; |
|---|
| 218 | 223 | |
|---|
| 219 | 224 | foreach (ref t; tex) { |
|---|
| r48 |
r49 |
|
| 205 | 205 | /** Draw a block of text (may inlcude new-lines). |
|---|
| 206 | 206 | * |
|---|
| | 207 | * The text block is drawn with top-left corner at x,y. To put the text's baseline at a given |
|---|
| | 208 | * y coordinate would require some changes. Line height is currently variable, depending on the |
|---|
| | 209 | * highest glyph in the line. |
|---|
| | 210 | * |
|---|
| 207 | 211 | * As a CPU-side code optimisation, store a TextBlock (unique to str) and pass a reference as |
|---|
| 208 | 212 | * the cache argument. This is the recommended method, although for one-time calls when you |
|---|
| r48 |
r49 |
|
| 26 | 26 | * Currently some external modules depend on InitFunctions, while some are set up from here. Once |
|---|
| 27 | 27 | * all are set up from here, the Init* modules can be rearranged. */ |
|---|
| | 28 | /* Idea: go back to init being controlled from elsewhere. Add a function to wait for another init |
|---|
| | 29 | * function to complete (threaded; might need to be done differently for non-threaded). */ |
|---|
| 28 | 30 | module mde.scheduler.init2; |
|---|
| 29 | 31 | |
|---|
| … | … | |
| 46 | 48 | init.addFunc (&initInput, "initInput"); |
|---|
| 47 | 49 | init.addFunc (&guiLoad, "guiLoad"); |
|---|
| 48 | | init.addFunc (&initFreeType, "initFreeType"); |
|---|
| 49 | 50 | } |
|---|
| 50 | 51 | |
|---|
| 51 | 52 | void guiLoad () { // init func |
|---|
| 52 | 53 | try { |
|---|
| | 54 | font.FontStyle.initialize; |
|---|
| 53 | 55 | gui.load (GUI); |
|---|
| 54 | 56 | cleanup.addFunc (&guiSave, "guiSave"); |
|---|
| … | … | |
| 85 | 87 | } |
|---|
| 86 | 88 | |
|---|
| 87 | | void initFreeType () { // init func |
|---|
| 88 | | try { |
|---|
| 89 | | font.FontStyle.initialize; |
|---|
| 90 | | } catch (Exception e) { |
|---|
| 91 | | logger.fatal ("initFreeType failed: " ~ e.msg); |
|---|
| 92 | | setInitFailure; |
|---|
| 93 | | } |
|---|
| 94 | | } |
|---|
| 95 | | |
|---|
| 96 | 89 | /+ Potential wrapper function: |
|---|
| 97 | 90 | // Template to call function, catching exceptions: |
|---|