Commit 31645c90 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

graphics: add Measure, MeasureTable

parent a839b92b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ add_library(plotfxlib STATIC
    common/graphics/image.cc
    common/graphics/layer.cc
    common/graphics/layout.cc
    common/graphics/measure.cc
    common/graphics/text.cc
    common/graphics/text_layout.cc
    common/graphics/text_shaper.cc
+3 −2
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include <stdint.h>
#include "colour.h"
#include "path.h"
#include "measure.h"

namespace plotfx {
class Layer;
@@ -42,12 +43,12 @@ enum class StrokeLineCap { BUTT, SQUARE, ROUND};

struct StrokeStyle {
  StrokeStyle() :
    line_width(1.0),
    line_width(Unit::PT, 1.0),
    line_join(StrokeLineJoin::MITER),
    line_cap(StrokeLineCap::SQUARE),
    colour(Colour::fromRGB(0, 0, 0)) {}

  float line_width;
  Measure line_width;
  StrokeLineJoin line_join;
  StrokeLineCap line_cap;
  Colour colour;
+7 −8
Original line number Diff line number Diff line
@@ -37,15 +37,14 @@ namespace plotfx {
Layer::Layer(
    double w,
    double h,
    double rem_ /* = 12 */,
    double dpi_ /* = 96 */) :
    double rem /* = 12 */,
    double dpi /* = 96 */) :
    width(w),
    height(h),
    dpi(dpi_),
    rem(rem_),
    measures{.dpi = dpi, .rem = rem},
    //pixmap(PixelFormat::RGBA8, w, h),
    text_shaper(dpi),
    rasterizer(w, h, dpi) {}
    rasterizer(w, h, measures) {}

Layer::~Layer() {}

@@ -78,7 +77,7 @@ void Layer::clear(const Colour& c) {
}

double from_rem(const Layer& l, double v) {
  return from_pt(l, l.rem) * v;
  return from_pt(l, l.measures.rem) * v;
}

double from_px(const Layer& l, double v) {
@@ -86,11 +85,11 @@ double from_px(const Layer& l, double v) {
}

double from_pt(const Layer& l, double v) {
  return (v / 72.0) * l.dpi;
  return (v / 72.0) * l.measures.dpi;
}

double to_pt(const Layer& l, double v) {
  return (v / l.dpi) * 72;
  return (v / l.measures.dpi) * 72;
}

} // namespace plotfx
+2 −2
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#include "text_shaper.h"
#include "rasterize.h"
#include "image.h"
#include "measure.h"

namespace plotfx {

@@ -55,8 +56,7 @@ struct Layer {

  double width;
  double height;
  double rem;
  double dpi;
  MeasureTable measures;
  //Image pixmap;
  text::TextShaper text_shaper;
  Rasterizer rasterizer;
+66 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "measure.h"

namespace plotfx {

Measure::Measure() : Measure(Unit::UNIT, 0.0f) {}

Measure::Measure(
    Unit _unit,
    double _value) :
    unit(_unit),
    value(_value) {}

Measure::operator double() const {
  return value;
}

Measure to_px(const MeasureTable& t, const Measure& v) {
  double v_px;

  switch (v.unit) {
    case Unit::UNIT:
    case Unit::PX:
      v_px = v.value;
      break;
    case Unit::PT:
      v_px = (v.value / 72.0) * t.dpi;
      break;
    case Unit::REM:
      v_px = ((v.value * t.rem) / 72.0) * t.dpi;
      break;
  }

  return Measure{.unit = Unit::PX, .value = v_px};
}

} // namespace plotfx
Loading