Blame gfx/src/nsBoundingMetrics.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#ifndef __nsBoundingMetrics_h
Packit f0b94e
#define __nsBoundingMetrics_h
Packit f0b94e
Packit f0b94e
#include "nsCoord.h"
Packit f0b94e
#include <algorithm>
Packit f0b94e
Packit f0b94e
/* Struct used for accurate measurements of a string, in order to
Packit f0b94e
 * allow precise positioning when processing MathML.  This is in its
Packit f0b94e
 * own header file because some very-widely-included headers need it
Packit f0b94e
 * but not the rest of nsFontMetrics, or vice versa.
Packit f0b94e
 */
Packit f0b94e
Packit f0b94e
struct nsBoundingMetrics {
Packit f0b94e
  ///////////
Packit f0b94e
  // Metrics that _exactly_ enclose the text:
Packit f0b94e
Packit f0b94e
  // The character coordinate system is the one used on X Windows:
Packit f0b94e
  // 1. The origin is located at the intersection of the baseline
Packit f0b94e
  //    with the left of the character's cell.
Packit f0b94e
  // 2. All horizontal bearings are oriented from left to right.
Packit f0b94e
  // 2. All horizontal bearings are oriented from left to right.
Packit f0b94e
  // 3. The ascent is oriented from bottom to top (being 0 at the orgin).
Packit f0b94e
  // 4. The descent is oriented from top to bottom (being 0 at the origin).
Packit f0b94e
Packit f0b94e
  // Note that Win32/Mac/PostScript use a different convention for
Packit f0b94e
  // the descent (all vertical measurements are oriented from bottom
Packit f0b94e
  // to top on these palatforms). Make sure to flip the sign of the
Packit f0b94e
  // descent on these platforms for cross-platform compatibility.
Packit f0b94e
Packit f0b94e
  // Any of the following member variables listed here can have
Packit f0b94e
  // positive or negative value.
Packit f0b94e
Packit f0b94e
  nscoord leftBearing;
Packit f0b94e
  /* The horizontal distance from the origin of the drawing
Packit f0b94e
     operation to the left-most part of the drawn string. */
Packit f0b94e
Packit f0b94e
  nscoord rightBearing;
Packit f0b94e
  /* The horizontal distance from the origin of the drawing
Packit f0b94e
     operation to the right-most part of the drawn string.
Packit f0b94e
     The _exact_ width of the string is therefore:
Packit f0b94e
     rightBearing - leftBearing */
Packit f0b94e
Packit f0b94e
  nscoord ascent;
Packit f0b94e
  /* The vertical distance from the origin of the drawing
Packit f0b94e
     operation to the top-most part of the drawn string. */
Packit f0b94e
Packit f0b94e
  nscoord descent;
Packit f0b94e
  /* The vertical distance from the origin of the drawing
Packit f0b94e
     operation to the bottom-most part of the drawn string.
Packit f0b94e
     The _exact_ height of the string is therefore:
Packit f0b94e
     ascent + descent */
Packit f0b94e
Packit f0b94e
  nscoord width;
Packit f0b94e
  /* The horizontal distance from the origin of the drawing
Packit f0b94e
     operation to the correct origin for drawing another string
Packit f0b94e
     to follow the current one. Depending on the font, this
Packit f0b94e
     could be greater than or less than the right bearing. */
Packit f0b94e
Packit f0b94e
  nsBoundingMetrics()
Packit f0b94e
      : leftBearing(0), rightBearing(0), ascent(0), descent(0), width(0) {}
Packit f0b94e
Packit f0b94e
  void operator+=(const nsBoundingMetrics& bm) {
Packit f0b94e
    if (ascent + descent == 0 && rightBearing - leftBearing == 0) {
Packit f0b94e
      ascent = bm.ascent;
Packit f0b94e
      descent = bm.descent;
Packit f0b94e
      leftBearing = width + bm.leftBearing;
Packit f0b94e
      rightBearing = width + bm.rightBearing;
Packit f0b94e
    } else {
Packit f0b94e
      if (ascent < bm.ascent) ascent = bm.ascent;
Packit f0b94e
      if (descent < bm.descent) descent = bm.descent;
Packit f0b94e
      leftBearing = std::min(leftBearing, width + bm.leftBearing);
Packit f0b94e
      rightBearing = std::max(rightBearing, width + bm.rightBearing);
Packit f0b94e
    }
Packit f0b94e
    width += bm.width;
Packit f0b94e
  }
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
#endif  // __nsBoundingMetrics_h