Text Metrics And Wrapping
Laying text into a fixed-width panel cell means knowing roughly how wide it will render, and MQL4 cannot ask the platform for that - there is no function anywhere in the language that measures a rendered string's pixel width in a given font and size. This library fills that gap with an honest, clearly documented approximation rather than pretending to be exact: every ASCII character is assigned a relative width CLASS (narrow, normal or wide - a handful of thin glyphs like "iIl.,:;'!|" and a handful of wide ones like "MWmw@%" against everything else as "normal"), the classes are summed for a string, and the total is scaled by one tunable pixels-per-unit-per-point constant. This is deliberately not presented as pixel-exact - it drifts from the true rendered width as font size, font family or the terminal's own DPI scaling change, and it treats every non-ASCII character as normal width (under-estimating double-width scripts). What it is good for is the actual dashboard question: "does this roughly fit, and if not, where should it wrap or truncate" - which is what Wrap() and Ellipsize() answer, including a hard-break fallback in Wrap() for a single word wider than the box on its own, so no line silently overflows a fixed-width layout. Notes and limitations - This is a heuristic approximation, not a measured value - see the file header for the exact width-class assignment and the single tunable constant it is built on. Do not rely on it for pixel-perfect alignment. - Non-ASCII characters (code point 128 and above) are treated as normal width, which under-estimates most double-width scripts. - Wrap() only splits on plain spaces - it has no tab or newline handling, since panel text is single-line UI copy, not a document. - No money is at risk in the demo. It only measures and prints strings and never sends an order.
Script · MT4 · v1.0 · Free
About Text Metrics And Wrapping
Text Metrics And Wrapping is a script for MetaTrader 4. MQL source code included.
Files in the download: TextMetricsWrapping.mqh, TextMetricsWrapping.txt, TextMetricsWrappingDemo.mq4.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Dashboards & Scanners · MT4 Script · MQL4 Source Code