gfx/src/nsBoundingMetrics.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/src/nsBoundingMetrics.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef __nsBoundingMetrics_h
    1.10 +#define __nsBoundingMetrics_h
    1.11 +
    1.12 +#include "nsCoord.h"
    1.13 +#include <algorithm>
    1.14 +
    1.15 +/* Struct used for accurate measurements of a string, in order to
    1.16 + * allow precise positioning when processing MathML.  This is in its
    1.17 + * own header file because some very-widely-included headers need it
    1.18 + * but not the rest of nsFontMetrics, or vice versa.
    1.19 + */
    1.20 +
    1.21 +struct nsBoundingMetrics {
    1.22 +
    1.23 +    ///////////
    1.24 +    // Metrics that _exactly_ enclose the text:
    1.25 +
    1.26 +    // The character coordinate system is the one used on X Windows:
    1.27 +    // 1. The origin is located at the intersection of the baseline
    1.28 +    //    with the left of the character's cell.
    1.29 +    // 2. All horizontal bearings are oriented from left to right.
    1.30 +    // 2. All horizontal bearings are oriented from left to right.
    1.31 +    // 3. The ascent is oriented from bottom to top (being 0 at the orgin).
    1.32 +    // 4. The descent is oriented from top to bottom (being 0 at the origin).
    1.33 +
    1.34 +    // Note that Win32/Mac/PostScript use a different convention for
    1.35 +    // the descent (all vertical measurements are oriented from bottom
    1.36 +    // to top on these palatforms). Make sure to flip the sign of the
    1.37 +    // descent on these platforms for cross-platform compatibility.
    1.38 +
    1.39 +    // Any of the following member variables listed here can have
    1.40 +    // positive or negative value.
    1.41 +
    1.42 +    nscoord leftBearing;
    1.43 +    /* The horizontal distance from the origin of the drawing
    1.44 +       operation to the left-most part of the drawn string. */
    1.45 +
    1.46 +    nscoord rightBearing;
    1.47 +    /* The horizontal distance from the origin of the drawing
    1.48 +       operation to the right-most part of the drawn string.
    1.49 +       The _exact_ width of the string is therefore:
    1.50 +       rightBearing - leftBearing */
    1.51 +
    1.52 +    nscoord ascent;
    1.53 +    /* The vertical distance from the origin of the drawing
    1.54 +       operation to the top-most part of the drawn string. */
    1.55 +
    1.56 +    nscoord descent;
    1.57 +    /* The vertical distance from the origin of the drawing
    1.58 +       operation to the bottom-most part of the drawn string.
    1.59 +       The _exact_ height of the string is therefore:
    1.60 +       ascent + descent */
    1.61 +
    1.62 +    nscoord width;
    1.63 +    /* The horizontal distance from the origin of the drawing
    1.64 +       operation to the correct origin for drawing another string
    1.65 +       to follow the current one. Depending on the font, this
    1.66 +       could be greater than or less than the right bearing. */
    1.67 +
    1.68 +    nsBoundingMetrics() : leftBearing(0), rightBearing(0),
    1.69 +                          ascent(0), descent(0), width(0)
    1.70 +    {}
    1.71 +
    1.72 +    void
    1.73 +    operator += (const nsBoundingMetrics& bm) {
    1.74 +        if (ascent + descent == 0 && rightBearing - leftBearing == 0) {
    1.75 +            ascent = bm.ascent;
    1.76 +            descent = bm.descent;
    1.77 +            leftBearing = width + bm.leftBearing;
    1.78 +            rightBearing = width + bm.rightBearing;
    1.79 +        }
    1.80 +        else {
    1.81 +            if (ascent < bm.ascent) ascent = bm.ascent;
    1.82 +            if (descent < bm.descent) descent = bm.descent;
    1.83 +            leftBearing = std::min(leftBearing, width + bm.leftBearing);
    1.84 +            rightBearing = std::max(rightBearing, width + bm.rightBearing);
    1.85 +        }
    1.86 +        width += bm.width;
    1.87 +    }
    1.88 +};
    1.89 +
    1.90 +#endif // __nsBoundingMetrics_h

mercurial