port remainder of the the placement algorithm

This commit is contained in:
Konstantin Käfer
2014-03-19 18:58:50 +01:00
parent 98bb797a4a
commit 4ee0cf1791
26 changed files with 872 additions and 290 deletions

View File

@@ -2,23 +2,17 @@
#define LLMR_GEOMETRY_BINPACK
#include <llmr/util/noncopyable.hpp>
#include <llmr/util/rect.hpp>
#include <cstdint>
#include <list>
namespace llmr {
template <typename T>
struct Rect {
Rect(T x, T y, T w, T h) : x(x), y(y), w(w), h(h) {}
T x = 0, y = 0;
T w = 0, h = 0;
};
template <typename T>
class BinPack : private util::noncopyable {
public:
BinPack(T width, T height)
: free(1, { 0, 0, width, height }) {}
: free(1, Rect<uint16_t>{ 0, 0, width, height }) {}
public:
Rect<T> allocate(T width, T height) {
// Find the smallest free rect angle
@@ -35,7 +29,7 @@ public:
if (smallest == free.end()) {
// There's no space left for this char.
return { 0, 0, 0, 0 };
return Rect<uint16_t>{ 0, 0, 0, 0 };
} else {
Rect<T> rect = *smallest;
free.erase(smallest);
@@ -60,7 +54,7 @@ public:
if (rect.h > height) free.emplace_back(rect.x, rect.y + height, width, rect.h - height);
}
return { rect.x, rect.y, width, height };
return Rect<uint16_t>{ rect.x, rect.y, width, height };
}
}

View File

@@ -47,7 +47,7 @@ public:
protected:
// increase the buffer size by at least /required/ bytes.
void *addElement() {
inline void *addElement() {
assert("Buffer is already bound to GPU" && buffer == 0);
if (length < pos + itemSize) {
while (length < pos + itemSize) length += defaultLength;

View File

@@ -6,49 +6,8 @@
namespace llmr {
template <typename Vertices>
Anchors interpolate(const Vertices& vertices, float spacing, float minScale = 0.0f, int start = 0) {
float distance = 0.0f;
float markedDistance = 0.0f;
int added = start;
Anchors points;
auto end = vertices.end() - 1;
int i = 0;
for (auto it = vertices.begin(); it != end; it++, i++) {
const auto& a = *(it),
b = *(it + 1);
float segmentDist = util::dist(a, b);
float angle = util::angle_to(b, a);
while (markedDistance + spacing < distance + segmentDist) {
markedDistance += spacing;
float t = (markedDistance - distance) / segmentDist,
x = util::interp(a.x, b.x, t),
y = util::interp(a.y, b.y, t),
s = added % 8 == 0 ? minScale :
added % 4 == 0 ? 2 :
added % 2 == 0 ? 4 :
8;
if (x >= 0 && x < 4096 && y >= 0 && y < 4096) {
points.emplace_back(x, y, angle, s, i);
}
added++;
}
distance += segmentDist;
}
return points;
}
Anchors interpolate(const std::vector<Coordinate> &vertices, float spacing,
float minScale = 0.0f, int start = 0);
}
#endif

View File

@@ -2,21 +2,10 @@
#define LLMR_GEOMETRY_TEXT_BUFFER
#include "buffer.hpp"
#include <array>
namespace llmr {
class GlyphPosition {
public:
int16_t x = 0, y = 0;
float ox = 0.0f, oy = 0.0f;
uint16_t tx = 0, ty = 0;
float angle = 0.0f;
float minzoom = 0.0f;
float rangefrom = 0.0f, rangeto = 0.0f;
float maxzoom = 25.0f;
float labelminzoom = 0.0f;
};
class TextVertexBuffer : public Buffer <
16,
GL_ARRAY_BUFFER,
@@ -27,7 +16,7 @@ public:
static const double angleFactor;
size_t add(const GlyphPosition& pos);
size_t add(int16_t x, int16_t y, float ox, float oy, uint16_t tx, uint16_t ty, float angle, float minzoom, std::array<float, 2> range, float maxzoom, float labelminzoom);
};

View File

@@ -2,12 +2,10 @@
#define LLMR_MAP_TILE_PARSER
#include <llmr/map/tile_data.hpp>
#include <llmr/text/placement.hpp>
namespace llmr {
typedef std::map<uint32_t, Rect<uint16_t>> GlyphPositions;
typedef std::map<std::string, GlyphPositions> FaceGlyphPositions;
class TileParser {
public:
TileParser(const std::string& data, TileData& tile, const Style& style, GlyphAtlas& glyphAtlas);
@@ -28,7 +26,8 @@ private:
TileData& tile;
const Style& style;
GlyphAtlas& glyphAtlas;
FaceGlyphPositions rects;
FaceGlyphPositions faces;
Placement placement;
};
}

View File

@@ -4,6 +4,7 @@
#include <llmr/util/pbf.hpp>
#include <llmr/util/vec.hpp>
#include <llmr/style/value.hpp>
#include <llmr/text/glyph.hpp>
#include <vector>
#include <map>
#include <set>
@@ -71,18 +72,7 @@ private:
std::set<uint32_t> values;
};
class VectorTilePlacement {
public:
VectorTilePlacement(uint32_t face, uint32_t glyph, uint32_t x, uint32_t y);
uint32_t face = 0;
uint32_t glyph = 0;
uint32_t x = 0;
uint32_t y = 0;
};
std::ostream& operator<<(std::ostream&, const VectorTilePlacement& placement);
std::ostream& operator<<(std::ostream&, const GlyphPlacement& placement);
class VectorTileLayer {
public:
@@ -94,11 +84,12 @@ public:
std::vector<std::string> keys;
std::vector<Value> values;
std::vector<std::string> faces;
std::map<std::string, std::map<Value, std::vector<VectorTilePlacement>>> shaping;
std::map<std::string, std::map<Value, Shaping>> shaping;
};
class VectorTileGlyph {
public:
VectorTileGlyph();
VectorTileGlyph(pbf data);
uint32_t id = 0;
@@ -106,12 +97,8 @@ public:
// A signed distance field of the glyph with a border of 3 pixels.
std::string bitmap;
// Glyph metrics.
uint32_t width = 0;
uint32_t height = 0;
int32_t left = 0;
int32_t top = 0;
uint32_t advance = 0;
// Glyph metrics
GlyphMetrics metrics;
};
std::ostream& operator<<(std::ostream&, const VectorTileGlyph& glyph);

View File

@@ -5,6 +5,8 @@
#include <llmr/style/bucket_description.hpp>
#include <llmr/geometry/vao.hpp>
#include <llmr/geometry/elements_buffer.hpp>
#include <llmr/map/vector_tile.hpp>
#include <llmr/text/types.hpp>
#include <memory>
#include <map>
#include <vector>
@@ -15,46 +17,45 @@ class Style;
class TextVertexBuffer;
class TriangleElementsBuffer;
class TextShader;
class VectorTileFeature;
class VectorTileFace;
class VectorTilePlacement;
class Placement;
struct pbf;
class TextBucket : public Bucket {
typedef ElementGroup triangle_group_type;
public:
TextBucket(const std::shared_ptr<TextVertexBuffer>& vertexBuffer,
const std::shared_ptr<TriangleElementsBuffer>& triangleElementsBuffer,
const BucketDescription& bucket_desc);
TextBucket(
const std::shared_ptr<TextVertexBuffer> &vertexBuffer,
const std::shared_ptr<TriangleElementsBuffer> &triangleElementsBuffer,
const BucketDescription &bucket_desc, Placement &placement);
virtual void render(Painter& painter, const std::string& layer_name, const Tile::ID& id);
virtual void render(Painter &painter, const std::string &layer_name,
const Tile::ID &id);
void addFeature(const VectorTileFeature& feature,
const std::vector<const VectorTileFace *>& faces,
const std::map<Value, std::vector<VectorTilePlacement>>& shaping);
void addGlyphs(const PlacedGlyphs &glyphs, float placementZoom,
PlacementRange placementRange, float zoom);
void addLabel(const std::vector<Coordinate>& line,
const std::vector<const VectorTileFace *>& faces,
const std::vector<VectorTilePlacement>& placements);
void addFeature(const VectorTileFeature &feature,
const IndexedFaceGlyphPositions &faces,
const std::map<Value, Shaping> &shapings);
bool empty() const;
void drawGlyphs(TextShader& shader);
void drawGlyphs(TextShader &shader);
public:
const BucketGeometryDescription geometry;
const BucketGeometryDescription &geom_desc;
private:
std::shared_ptr<TextVertexBuffer> vertexBuffer;
std::shared_ptr<TriangleElementsBuffer> triangleElementsBuffer;
Placement &placement;
const size_t vertex_start;
const size_t triangle_elements_start;
std::vector<triangle_group_type> triangleGroups;
};
}
#endif

View File

@@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include <cmath>
#include "value.hpp"
@@ -30,6 +31,11 @@ enum class JoinType {
Round = 3
};
enum class TextPathType {
Horizontal = 0,
Curve = 1
};
inline BucketType bucketType(const std::string& type) {
if (type == "fill") return BucketType::Fill;
@@ -54,6 +60,12 @@ inline JoinType joinType(const std::string& join) {
else return JoinType::None;
}
inline TextPathType textPathType(const std::string& path) {
if (path == "horizontal") return TextPathType::Horizontal;
else if (path == "curve") return TextPathType::Curve;
else return TextPathType::Horizontal;
};
class BucketGeometryDescription {
public:
CapType cap = CapType::None;
@@ -63,6 +75,12 @@ public:
float font_size = 0.0f;
float miter_limit = 2.0f;
float round_limit = 1.0f;
TextPathType path = TextPathType::Horizontal;
float padding = 2.0f;
float textMinDistance = 250.0f;
float rotate = 0.0f; // what is this?
float maxAngleDelta = M_PI;
bool alwaysVisible = false;
};
class BucketDescription {

View File

@@ -6,7 +6,7 @@
namespace llmr {
typedef ::util::variant<bool, int64_t, uint64_t, double, std::string> Value;
typedef util::variant<bool, int64_t, uint64_t, double, std::string> Value;
Value parseValue(pbf data);

View File

@@ -1,65 +1,17 @@
#ifndef LLMR_TEXT_COLLISION
#define LLMR_TEXT_COLLISION
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <llmr/text/rotation_range.hpp>
#include <llmr/text/types.hpp>
namespace llmr {
// These are the glyph boxes that we want to have placed.
struct GlyphBox {
explicit GlyphBox() {}
explicit GlyphBox(const CollisionRect &bbox, const CollisionRect &box,
float minScale)
: bbox(bbox), box(box), minScale(minScale) {}
CollisionAnchor anchor;
CollisionRect bbox;
CollisionRect box;
bool rotate = false;
float minScale = 0.0f;
float maxScale = std::numeric_limits<float>::infinity();
};
typedef std::vector<GlyphBox> GlyphBoxes;
// These are the placed boxes contained in the rtree.
struct PlacementBox {
CollisionAnchor anchor;
CollisionRect bbox;
CollisionRect box;
bool rotate = false;
PlacementRange placementRange = {{0.0f, 0.0f}};
float placementScale = 0.0f;
float maxScale = std::numeric_limits<float>::infinity();
float padding = 0.0f;
};
struct PlacementProperty {
explicit PlacementProperty() {}
explicit PlacementProperty(float zoom, const PlacementRange &rotationRange)
: zoom(zoom), rotationRange(rotationRange) {}
float zoom = -1.0f;
PlacementRange rotationRange = {{0.0f, 0.0f}};
};
class Collision {
typedef boost::geometry::model::point<float, 2,
boost::geometry::cs::cartesian> Point;
typedef boost::geometry::model::box<Point> Box;
typedef std::pair<Box, PlacementBox> PlacementValue;
typedef boost::geometry::index::rtree<
PlacementValue, boost::geometry::index::quadratic<16>> Tree;
public:
Collision();
~Collision();
PlacementProperty place(const GlyphBoxes &boxes,
PlacementProperty place(const PlacedGlyphs &boxes,
const CollisionAnchor &anchor,
float minPlacementScale, float maxPlacementScale,
float padding, bool horizontal);
@@ -72,7 +24,7 @@ public:
bool horizontal, float padding);
private:
Tree tree;
void *tree;
};
}

View File

@@ -0,0 +1,58 @@
#ifndef LLMR_TEXT_GLYPH
#define LLMR_TEXT_GLYPH
#include <llmr/util/rect.hpp>
#include <cstdint>
#include <vector>
#include <map>
namespace llmr {
struct GlyphMetrics {
operator bool() const {
return width == 0 && height == 0 && advance == 0;
}
// Glyph metrics.
uint32_t width = 0;
uint32_t height = 0;
int32_t left = 0;
int32_t top = 0;
uint32_t advance = 0;
};
struct Glyph {
inline explicit Glyph() : rect(0, 0, 0, 0), metrics() {}
inline explicit Glyph(const Rect<uint16_t> &rect,
const GlyphMetrics &metrics)
: rect(rect), metrics(metrics) {}
operator bool() const {
return !metrics;
}
const Rect<uint16_t> rect;
const GlyphMetrics metrics;
};
typedef std::vector<Glyph> GlyphPositions;
typedef std::map<std::string, GlyphPositions> FaceGlyphPositions;
typedef std::vector<const GlyphPositions *> IndexedFaceGlyphPositions;
class GlyphPlacement {
public:
inline explicit GlyphPlacement(uint32_t face, uint32_t glyph, uint32_t x, uint32_t y)
: face(face), glyph(glyph), x(x), y(y) {}
uint32_t face = 0;
uint32_t glyph = 0;
uint32_t x = 0;
uint32_t y = 0;
};
typedef std::vector<GlyphPlacement> Shaping;
}
#endif

View File

@@ -0,0 +1,39 @@
#ifndef LLMR_TEXT_PLACEMENT
#define LLMR_TEXT_PLACEMENT
#include <llmr/text/collision.hpp>
#include <llmr/geometry/geometry.hpp>
#include <llmr/util/vec.hpp>
#include <llmr/text/glyph.hpp>
#include <vector>
namespace llmr {
class TextBucket;
class BucketGeometryDescription;
class Placement {
public:
Placement(int8_t zoom);
void addFeature(TextBucket &bucket, const std::vector<Coordinate> &line,
const BucketGeometryDescription &info,
const IndexedFaceGlyphPositions &faces,
const Shaping &shaping);
float measureText(const IndexedFaceGlyphPositions &faces,
const Shaping &shaping);
private:
const int8_t zoom;
Collision collision;
const float zOffset;
const float maxPlacementScale;
public:
static const int tileExtent;
static const int glyphSize;
static const float minScale;
};
}
#endif

View File

@@ -1,39 +1,14 @@
#ifndef LLMR_TEXT_ROTATION_RANGE
#define LLMR_TEXT_ROTATION_RANGE
#include <llmr/util/vec.hpp>
#include <llmr/util/math.hpp>
#include <llmr/text/types.hpp>
#include <array>
#include <vector>
#include <cassert>
namespace llmr {
typedef vec2<float> CollisionPoint;
typedef vec2<float> CollisionAnchor;
typedef std::array<float, 2> PlacementRange;
typedef float CollisionAngle;
typedef std::vector<CollisionAngle> CollisionAngles;
typedef std::array<CollisionAngle, 2> CollisionRange;
typedef std::vector<CollisionRange> CollisionList;
typedef std::array<CollisionPoint, 4> CollisionCorners;
struct CollisionRect {
CollisionPoint tl;
CollisionPoint br;
inline explicit CollisionRect() {}
inline explicit CollisionRect(CollisionPoint::Type ax,
CollisionPoint::Type ay,
CollisionPoint::Type bx,
CollisionPoint::Type by)
: tl(ax, ay), br(bx, by) {}
inline explicit CollisionRect(const CollisionPoint &tl,
const CollisionPoint &br)
: tl(tl), br(br) {}
};
/*
* Combine an array of collision ranges to form a continuous
* range that includes 0. Collisions within the ignoreRange are ignored
@@ -72,9 +47,8 @@ CollisionList rotatingFixedCollisions(const CollisionRect &rotating,
/*
* Calculate the range a box conflicts with a second box
*/
template <typename InsertingBox, typename BlockingBox>
CollisionRange rotationRange(const InsertingBox &inserting,
const BlockingBox &blocker, float scale);
CollisionRange rotationRange(const GlyphBox &inserting,
const PlacementBox &blocker, float scale);
}
#endif

108
include/llmr/text/types.hpp Normal file
View File

@@ -0,0 +1,108 @@
#ifndef LLMR_TEXT_TYPES
#define LLMR_TEXT_TYPES
#include <llmr/util/vec.hpp>
#include <llmr/util/rect.hpp>
#include <array>
#include <vector>
namespace llmr {
typedef vec2<float> CollisionPoint;
typedef vec2<float> CollisionAnchor;
typedef std::array<float, 2> PlacementRange;
typedef float CollisionAngle;
typedef std::vector<CollisionAngle> CollisionAngles;
typedef std::array<CollisionAngle, 2> CollisionRange;
typedef std::vector<CollisionRange> CollisionList;
typedef std::array<CollisionPoint, 4> CollisionCorners;
struct CollisionRect {
CollisionPoint tl;
CollisionPoint br;
inline explicit CollisionRect() {}
inline explicit CollisionRect(CollisionPoint::Type ax,
CollisionPoint::Type ay,
CollisionPoint::Type bx,
CollisionPoint::Type by)
: tl(ax, ay), br(bx, by) {}
inline explicit CollisionRect(const CollisionPoint &tl,
const CollisionPoint &br)
: tl(tl), br(br) {}
};
// These are the glyph boxes that we want to have placed.
struct GlyphBox {
explicit GlyphBox() {}
explicit GlyphBox(const CollisionRect &bbox, const CollisionRect &box,
float minScale)
: bbox(bbox), box(box), minScale(minScale) {}
explicit GlyphBox(const CollisionRect &box, float minScale, float maxScale,
const CollisionAnchor &anchor, bool rotate)
: anchor(anchor),
box(box),
rotate(rotate),
minScale(minScale),
maxScale(maxScale) {}
CollisionAnchor anchor;
CollisionRect bbox;
CollisionRect box;
bool rotate = false;
float minScale = 0.0f;
float maxScale = std::numeric_limits<float>::infinity();
};
typedef std::vector<GlyphBox> GlyphBoxes;
// TODO: Transform the vec2<float>s to vec2<int16_t> to save bytes
struct PlacedGlyph {
explicit PlacedGlyph(const vec2<float> &tl, const vec2<float> &tr,
const vec2<float> &bl, const vec2<float> &br,
const Rect<uint16_t> &tex, uint32_t width,
uint32_t height, float angle, const GlyphBox &glyphBox)
: tl(tl),
tr(tr),
bl(bl),
br(br),
tex(tex),
width(width),
height(height),
angle(angle),
glyphBox(glyphBox) {}
vec2<float> tl, tr, bl, br;
Rect<uint16_t> tex;
uint32_t width, height;
float angle;
GlyphBox glyphBox;
};
typedef std::vector<PlacedGlyph> PlacedGlyphs;
// These are the placed boxes contained in the rtree.
struct PlacementBox {
CollisionAnchor anchor;
CollisionRect bbox;
CollisionRect box;
bool rotate = false;
PlacementRange placementRange = {{0.0f, 0.0f}};
float placementScale = 0.0f;
float maxScale = std::numeric_limits<float>::infinity();
float padding = 0.0f;
};
struct PlacementProperty {
explicit PlacementProperty() {}
explicit PlacementProperty(float zoom, const PlacementRange &rotationRange)
: zoom(zoom), rotationRange(rotationRange) {}
float zoom = -1.0f;
PlacementRange rotationRange = {{0.0f, 0.0f}};
};
}
#endif

View File

@@ -0,0 +1,16 @@
#ifndef LLMR_UTIL_RECT
#define LLMR_UTIL_RECT
namespace llmr {
template <typename T>
struct Rect {
explicit Rect(T x, T y, T w, T h) : x(x), y(y), w(w), h(h) {}
T x = 0, y = 0;
T w = 0, h = 0;
operator bool() const { return w == 0 && h == 0; }
};
}
#endif

View File

@@ -16,6 +16,8 @@
#define VARIANT_INLINE __attribute__((noinline))
#endif
namespace llmr {
namespace util {
namespace detail {
@@ -377,4 +379,6 @@ operator<< (std::basic_ostream<charT,traits>& out, Variant const& rhs)
}
}
#endif // UTIL_VARIANT_HPP

View File

@@ -25,12 +25,45 @@ struct vec2 {
inline vec2(const vec2& o) : x(o.x), y(o.y) {}
template<typename U>
inline vec2(const U& u) : x(u.x), y(u.y) {}
inline vec2(T x, T y) : x(x), y(y) {}
inline bool operator==(const vec2& rhs) const {
return x == rhs.x && y == rhs.y;
}
template <typename O>
inline typename std::enable_if<std::is_arithmetic<O>::value, vec2>::type
operator*(O o) const {
return {x * o, y * o};
}
template <typename O>
inline typename std::enable_if<std::is_arithmetic<O>::value, vec2>::type &
operator*=(O o) {
x *= o;
y *= o;
}
template <typename O>
inline typename std::enable_if<std::is_arithmetic<O>::value, vec2>::type
operator-(O o) const {
return {x - o, y - o};
}
template <typename O>
inline typename std::enable_if<!std::is_arithmetic<O>::value, vec2>::type
operator-(const O &o) const {
return {x - o.x, y - o.y};
}
template <typename M>
inline vec2 matMul(const M &m) const {
return {m[0] * x + m[1] * y, m[2] * x + m[3] * y};
}
template<typename U = T, typename std::enable_if<std::numeric_limits<U>::has_quiet_NaN, int>::type = 0>
inline operator bool() const {
return !isnan(x) && !isnan(y);

View File

@@ -39,12 +39,12 @@ Rect<uint16_t> GlyphAtlas::addGlyph(uint64_t tile_id, const std::string& face_na
}
// The glyph bitmap has zero width.
if (!glyph.width) {
return { 0, 0, 0, 0 };
if (!glyph.metrics.width) {
return Rect<uint16_t>{ 0, 0, 0, 0 };
}
uint16_t buffered_width = glyph.width + buffer * 2;
uint16_t buffered_height = glyph.height + buffer * 2;
uint16_t buffered_width = glyph.metrics.width + buffer * 2;
uint16_t buffered_height = glyph.metrics.height + buffer * 2;
// Add a 1px border around every image.
uint16_t pack_width = buffered_width;

View File

@@ -0,0 +1,42 @@
#include <llmr/geometry/interpolate.hpp>
namespace llmr {
Anchors interpolate(const std::vector<Coordinate> &vertices, float spacing,
float minScale, int start) {
float distance = 0.0f;
float markedDistance = 0.0f;
int added = start;
Anchors points;
auto end = vertices.end() - 1;
int i = 0;
for (auto it = vertices.begin(); it != end; it++, i++) {
const Coordinate &a = *(it), b = *(it + 1);
float segmentDist = util::dist<float>(a, b);
float angle = util::angle_to(b, a);
while (markedDistance + spacing < distance + segmentDist) {
markedDistance += spacing;
float t = (markedDistance - distance) / segmentDist,
x = util::interp(a.x, b.x, t), y = util::interp(a.y, b.y, t),
s = added % 8 == 0 ? minScale : added % 4 == 0
? 2
: added % 2 == 0 ? 4 : 8;
if (x >= 0 && x < 4096 && y >= 0 && y < 4096) {
points.emplace_back(x, y, angle, s, i);
}
added++;
}
distance += segmentDist;
}
return points;
}
}

View File

@@ -6,28 +6,27 @@
using namespace llmr;
const double TextVertexBuffer::angleFactor = 128 / M_PI;
const double TextVertexBuffer::angleFactor = 128.0 / M_PI;
size_t TextVertexBuffer::add(const GlyphPosition& pos) {
size_t TextVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, uint16_t tx, uint16_t ty, float angle, float minzoom, std::array<float, 2> range, float maxzoom, float labelminzoom) {
size_t idx = index();
void *data = addElement();
int16_t *shorts = static_cast<int16_t *>(data);
shorts[0] = pos.x;
shorts[1] = pos.y;
shorts[2] = round(pos.ox * 64); // use 1/64 pixels for placement
shorts[3] = round(pos.oy * 64);
shorts[0] = x;
shorts[1] = y;
shorts[2] = round(ox * 64); // use 1/64 pixels for placement
shorts[3] = round(oy * 64);
uint8_t *ubytes = static_cast<uint8_t *>(data);
ubytes[8] = pos.tx / 4;
ubytes[9] = pos.ty / 4;
ubytes[10] = pos.labelminzoom * 10;
ubytes[11] = pos.minzoom * 10; // 1/10 zoom levels: z16 == 160.
ubytes[12] = fmin(pos.maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160.
ubytes[13] = (int16_t)round(pos.angle * angleFactor) % 256;
ubytes[14] = util::max((int16_t)round(pos.rangefrom * angleFactor), (int16_t)0) % 256;
ubytes[15] = util::min((int16_t)round(pos.rangeto * angleFactor), (int16_t)255) % 256;
ubytes[8] = tx / 4;
ubytes[9] = ty / 4;
ubytes[10] = labelminzoom * 10;
ubytes[11] = minzoom * 10; // 1/10 zoom levels: z16 == 160.
ubytes[12] = fmin(maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160.
ubytes[13] = (int16_t)round(angle * angleFactor) % 256;
ubytes[14] = util::max((int16_t)round(range[0] * angleFactor), (int16_t)0) % 256;
ubytes[15] = util::min((int16_t)round(range[1] * angleFactor), (int16_t)255) % 256;
return idx;
}

View File

@@ -13,7 +13,8 @@ TileParser::TileParser(const std::string& data, TileData& tile, const Style& sty
: data(pbf((const uint8_t *)data.data(), data.size())),
tile(tile),
style(style),
glyphAtlas(glyphAtlas) {
glyphAtlas(glyphAtlas),
placement(tile.id.z) {
parseGlyphs();
parseStyleLayers(style.layers);
}
@@ -24,13 +25,14 @@ bool TileParser::obsolete() const {
void TileParser::parseGlyphs() {
for (const std::pair<std::string, const VectorTileFace> pair : data.faces) {
const std::string& name = pair.first;
const VectorTileFace& face = pair.second;
const std::string &name = pair.first;
const VectorTileFace &face = pair.second;
GlyphPositions& rect = rects[name];
for (const VectorTileGlyph& glyph : face.glyphs) {
rect.emplace(glyph.id, glyphAtlas.addGlyph((uint64_t)tile.id, name, glyph));
GlyphPositions &glyphs = faces[name];
for (const VectorTileGlyph &glyph : face.glyphs) {
const Rect<uint16_t> rect =
glyphAtlas.addGlyph((uint64_t)tile.id, name, glyph);
glyphs.emplace_back(Glyph{rect, glyph.metrics});
}
}
}
@@ -137,12 +139,14 @@ std::shared_ptr<Bucket> TileParser::createTextBucket(const VectorTileLayer& laye
}
// TODO: currently hardcoded to use the first font stack.
const std::map<Value, std::vector<VectorTilePlacement>>& shaping = layer.shaping.begin()->second;
const std::map<Value, Shaping>& shaping = layer.shaping.begin()->second;
std::vector<const VectorTileFace *> faces;
const FaceGlyphPositions& const_faces = faces;
IndexedFaceGlyphPositions faces;
for (const std::string& face : layer.faces) {
auto it = data.faces.find(face);
if (it == data.faces.end()) {
auto it = const_faces.find(face);
if (it == const_faces.end()) {
// This layer references an unknown face.
return nullptr;
}
@@ -150,7 +154,7 @@ std::shared_ptr<Bucket> TileParser::createTextBucket(const VectorTileLayer& laye
}
std::shared_ptr<TextBucket> bucket = std::make_shared<TextBucket>(
tile.textVertexBuffer, tile.triangleElementsBuffer, bucket_desc);
tile.textVertexBuffer, tile.triangleElementsBuffer, bucket_desc, placement);
FilteredVectorTileLayer filtered_layer(layer, bucket_desc);
for (const pbf& feature_pbf : filtered_layer) {

View File

@@ -117,6 +117,9 @@ std::ostream& llmr::operator<<(std::ostream& os, const VectorTileFace& face) {
}
// -1 overflows to maximum value
VectorTileGlyph::VectorTileGlyph() {}
VectorTileGlyph::VectorTileGlyph(pbf glyph) {
while (glyph.next()) {
if (glyph.tag == 1) { // id
@@ -124,15 +127,15 @@ VectorTileGlyph::VectorTileGlyph(pbf glyph) {
} else if (glyph.tag == 2) { // bitmap
bitmap = glyph.string();
} else if (glyph.tag == 3) { // width
width = glyph.varint();
metrics.width = glyph.varint();
} else if (glyph.tag == 4) { // height
height = glyph.varint();
metrics.height = glyph.varint();
} else if (glyph.tag == 5) { // left
left = glyph.svarint();
metrics.left = glyph.svarint();
} else if (glyph.tag == 6) { // top
top = glyph.svarint();
metrics.top = glyph.svarint();
} else if (glyph.tag == 7) { // advance
advance = glyph.varint();
metrics.advance = glyph.varint();
} else {
glyph.skip();
}
@@ -140,8 +143,8 @@ VectorTileGlyph::VectorTileGlyph(pbf glyph) {
}
std::ostream& llmr::operator<<(std::ostream& os, const VectorTileGlyph& glyph) {
os << "Glyph " << glyph.id << ": " << glyph.width << "x" << glyph.height <<
" (" << glyph.left << "/" << glyph.top << ") +" << glyph.advance << std::endl;
os << "Glyph " << glyph.id << ": " << glyph.metrics.width << "x" << glyph.metrics.height <<
" (" << glyph.metrics.left << "/" << glyph.metrics.top << ") +" << glyph.metrics.advance << std::endl;
return os;
}
@@ -151,7 +154,7 @@ public:
uint32_t text = 0;
uint32_t stack = 0;
std::vector<VectorTilePlacement> placements;
std::vector<GlyphPlacement> placements;
};
VectorTileLabel::VectorTileLabel(pbf label) {
@@ -218,14 +221,7 @@ VectorTileLayer::VectorTileLayer(pbf layer) : data(layer) {
}
}
VectorTilePlacement::VectorTilePlacement(uint32_t face, uint32_t glyph, uint32_t x, uint32_t y)
: face(face),
glyph(glyph),
x(x),
y(y) {
}
std::ostream& llmr::operator<<(std::ostream& os, const VectorTilePlacement& placement) {
std::ostream& llmr::operator<<(std::ostream& os, const GlyphPlacement& placement) {
os << "Glyph " << placement.face << "/" << placement.glyph << ": " << placement.x << "/" << placement.y << std::endl;
return os;
}

View File

@@ -7,45 +7,116 @@
#include <llmr/shader/text_shader.hpp>
#include <llmr/style/style.hpp>
#include <llmr/map/vector_tile.hpp>
#include <llmr/text/placement.hpp>
#include <llmr/util/math.hpp>
#include <llmr/platform/gl.hpp>
#include <iostream>
#define BUFFER_OFFSET(i) ((char *)nullptr + (i))
#define BUFFER_OFFSET(i) ((char *) nullptr + (i))
#include <cassert>
using namespace llmr;
TextBucket::TextBucket(const std::shared_ptr<TextVertexBuffer>& vertexBuffer,
const std::shared_ptr<TriangleElementsBuffer>& triangleElementsBuffer,
const BucketDescription& bucket_desc)
: geometry(bucket_desc.geometry),
TextBucket::TextBucket(
const std::shared_ptr<TextVertexBuffer> &vertexBuffer,
const std::shared_ptr<TriangleElementsBuffer> &triangleElementsBuffer,
const BucketDescription &bucket_desc, Placement &placement)
: geom_desc(bucket_desc.geometry),
vertexBuffer(vertexBuffer),
triangleElementsBuffer(triangleElementsBuffer),
placement(placement),
vertex_start(vertexBuffer->index()),
triangle_elements_start(triangleElementsBuffer->index()) {
}
triangle_elements_start(triangleElementsBuffer->index()) {}
void TextBucket::addFeature(const VectorTileFeature& feature,
const std::vector<const VectorTileFace *>& faces,
const std::map<Value, std::vector<VectorTilePlacement>>& shaping) {
auto it_prop = feature.properties.find(geometry.text_field);
void TextBucket::addGlyphs(const PlacedGlyphs &glyphs, float placementZoom,
PlacementRange placementRange, float zoom) {
placementZoom += zoom;
for (const PlacedGlyph &glyph : glyphs) {
const auto &tl = glyph.tl;
const auto &tr = glyph.tr;
const auto &bl = glyph.bl;
const auto &br = glyph.br;
const auto &tex = glyph.tex;
const auto &width = glyph.width;
const auto &height = glyph.height;
const auto &angle = glyph.angle;
float minZoom = util::max(
static_cast<float>(zoom + log(glyph.glyphBox.minScale) / log(2)),
placementZoom);
float maxZoom = util::min(
static_cast<float>(zoom + log(glyph.glyphBox.maxScale) / log(2)),
25.0f);
const auto &glyphAnchor = glyph.glyphBox.anchor;
if (maxZoom <= minZoom)
continue;
// Lower min zoom so that while fading out the label
// it can be shown outside of collision-free zoom levels
if (minZoom == placementZoom) {
minZoom = 0;
}
const int glyph_vertex_length = 4;
if (!triangleGroups.size() ||
(triangleGroups.back().vertex_length + glyph_vertex_length >
65535)) {
// Move to a new group because the old one can't hold the geometry.
triangleGroups.emplace_back();
}
// We're generating triangle fans, so we always start with the first
// coordinate in this polygon.
triangle_group_type &triangleGroup = triangleGroups.back();
uint32_t triangleIndex = triangleGroup.vertex_length;
// coordinates (2 triangles)
vertexBuffer->add(glyphAnchor.x, glyphAnchor.y, tl.x, tl.y, tex.x,
tex.y, angle, minZoom, placementRange, maxZoom,
placementZoom);
vertexBuffer->add(glyphAnchor.x, glyphAnchor.y, tr.x, tr.y,
tex.x + width, tex.y, angle, minZoom, placementRange,
maxZoom, placementZoom);
vertexBuffer->add(glyphAnchor.x, glyphAnchor.y, bl.x, bl.y, tex.x,
tex.y + height, angle, minZoom, placementRange,
maxZoom, placementZoom);
vertexBuffer->add(glyphAnchor.x, glyphAnchor.y, br.x, br.y,
tex.x + width, tex.y + height, angle, minZoom,
placementRange, maxZoom, placementZoom);
// add the two triangles, referencing the four coordinates we just
// inserted.
triangleElementsBuffer->add(triangleIndex + 0, triangleIndex + 1,
triangleIndex + 1);
triangleElementsBuffer->add(triangleIndex + 1, triangleIndex + 2,
triangleIndex + 3);
triangleGroup.vertex_length += glyph_vertex_length;
}
};
void TextBucket::addFeature(const VectorTileFeature &feature,
const IndexedFaceGlyphPositions &faces,
const std::map<Value, Shaping> &shapings) {
auto it_prop = feature.properties.find(geom_desc.text_field);
if (it_prop == feature.properties.end()) {
// feature does not have the correct property
return;
}
const Value& value = it_prop->second;
const Value &value = it_prop->second;
auto it_shaping = shaping.find(value);
if (it_shaping == shaping.end()) {
auto it_shaping = shapings.find(value);
if (it_shaping == shapings.end()) {
// we lack shaping information for this label
return;
}
const std::vector<VectorTilePlacement>& placements = it_shaping->second;
const Shaping &shaping = it_shaping->second;
// std::cerr << "we have shaping for " << value << std::endl;
@@ -60,31 +131,24 @@ void TextBucket::addFeature(const VectorTileFeature& feature,
while ((cmd = geometry.next(x, y)) != Geometry::end) {
if (cmd == Geometry::move_to) {
if (!line.empty()) {
addLabel(line, faces, placements);
placement.addFeature(*this, line, geom_desc, faces, shaping);
line.clear();
}
}
line.emplace_back(x, y);
}
if (line.size()) {
addLabel(line, faces, placements);
placement.addFeature(*this, line, geom_desc, faces, shaping);
}
}
void TextBucket::addLabel(const std::vector<Coordinate>& line,
const std::vector<const VectorTileFace *>& faces,
const std::vector<VectorTilePlacement>& placements) {
// TODO
}
void TextBucket::render(Painter& painter, const std::string& layer_name, const Tile::ID& id) {
void TextBucket::render(Painter &painter, const std::string &layer_name,
const Tile::ID &id) {
painter.renderText(*this, layer_name, id);
}
bool TextBucket::empty() const {
return triangleGroups.empty();
}
bool TextBucket::empty() const { return triangleGroups.empty(); }
void TextBucket::drawGlyphs(TextShader& shader) {
void TextBucket::drawGlyphs(TextShader &shader) {
// noop
}

View File

@@ -1,9 +1,29 @@
#include <llmr/text/collision.hpp>
#include <llmr/text/rotation_range.hpp>
#include <llmr/util/math.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/index/rtree.hpp>
namespace llmr {
namespace bg = boost::geometry;
namespace bgm = bg::model;
namespace bgi = bg::index;
typedef bgm::point<float, 2, bg::cs::cartesian> Point;
typedef bgm::box<Point> Box;
typedef std::pair<Box, PlacementBox> PlacementValue;
typedef bgi::rtree<PlacementValue, bgi::quadratic<16>> Tree;
}
using namespace llmr;
Collision::Collision() {
Collision::~Collision() {
delete ((Tree *)tree);
}
Collision::Collision() : tree(new Tree()) {
const float m = 4096;
// Hack to prevent cross-tile labels
@@ -28,7 +48,7 @@ Collision::Collision() {
CollisionAnchor{m, m}, 1, {{M_PI * 2, 0}}, false, 2);
}
GlyphBox getMergedGlyphs(const GlyphBoxes &glyphs, bool horizontal,
GlyphBox getMergedGlyphs(const PlacedGlyphs &placed_glyphs, bool horizontal,
const CollisionAnchor &anchor) {
GlyphBox mergedGlyphs;
const float inf = std::numeric_limits<float>::infinity();
@@ -37,28 +57,28 @@ GlyphBox getMergedGlyphs(const GlyphBoxes &glyphs, bool horizontal,
mergedGlyphs.anchor = anchor;
CollisionRect &box = mergedGlyphs.box;
for (const GlyphBox &glyph : glyphs) {
const CollisionRect &gbox = glyph.box;
for (const PlacedGlyph &placed_glyph : placed_glyphs) {
const CollisionRect &gbox = placed_glyph.glyphBox.box;
box.tl.x = util::min(box.tl.x, gbox.tl.x);
box.tl.y = util::min(box.tl.y, gbox.tl.y);
box.br.x = util::max(box.br.x, gbox.br.x);
box.br.y = util::max(box.br.y, gbox.br.y);
mergedGlyphs.minScale =
util::max(mergedGlyphs.minScale, glyph.minScale);
util::max(mergedGlyphs.minScale, placed_glyph.glyphBox.minScale);
}
return mergedGlyphs;
}
PlacementProperty Collision::place(const GlyphBoxes &boxes,
PlacementProperty Collision::place(const PlacedGlyphs &placed_glyphs,
const CollisionAnchor &anchor,
float minPlacementScale,
float maxPlacementScale, float padding,
bool horizontal) {
float minScale = std::numeric_limits<float>::infinity();
for (const GlyphBox &box : boxes) {
minScale = util::min(minScale, box.minScale);
for (const PlacedGlyph &placed_glyph : placed_glyphs) {
minScale = util::min(minScale, placed_glyph.glyphBox.minScale);
}
minPlacementScale = util::max(minPlacementScale, minScale);
@@ -67,9 +87,11 @@ PlacementProperty Collision::place(const GlyphBoxes &boxes,
// for horizontal labels.
GlyphBoxes glyphs;
if (horizontal) {
glyphs.push_back(getMergedGlyphs(boxes, horizontal, anchor));
glyphs.push_back(getMergedGlyphs(placed_glyphs, horizontal, anchor));
} else {
glyphs = boxes;
for (const PlacedGlyph &placed_glyph : placed_glyphs) {
glyphs.push_back(placed_glyph.glyphBox);
}
}
// Calculate bboxes for all the glyphs
@@ -89,11 +111,11 @@ PlacementProperty Collision::place(const GlyphBoxes &boxes,
// Calculate the minimum scale the entire label can be shown without
// collisions
float scale =
getPlacementScale(boxes, minPlacementScale, maxPlacementScale, padding);
float scale = getPlacementScale(glyphs, minPlacementScale,
maxPlacementScale, padding);
// Return if the label can never be placed without collision
if (scale < 0) {
if (scale < 0 || scale == std::numeric_limits<float>::infinity()) {
return PlacementProperty{};
}
@@ -137,8 +159,8 @@ float Collision::getPlacementScale(const GlyphBoxes &glyphs,
Point{maxPlacedX, maxPlacedY}};
std::vector<PlacementValue> blocking;
tree.query(boost::geometry::index::intersects(query_box),
std::back_inserter(blocking));
((Tree *)tree)
->query(bgi::intersects(query_box), std::back_inserter(blocking));
if (blocking.size()) {
const CollisionAnchor &na = anchor; // new anchor
@@ -218,8 +240,8 @@ PlacementRange Collision::getPlacementRange(const GlyphBoxes &glyphs,
Point{maxPlacedX, maxPlacedY}};
std::vector<PlacementValue> blocking;
tree.query(boost::geometry::index::intersects(query_box),
std::back_inserter(blocking));
((Tree *)tree)
->query(bgi::intersects(query_box), std::back_inserter(blocking));
for (const PlacementValue &value : blocking) {
const Box &s = value.first;
@@ -267,6 +289,8 @@ void Collision::insert(const GlyphBoxes &glyphs, const CollisionAnchor &anchor,
float placementScale,
const PlacementRange &placementRange, bool horizontal,
float padding) {
assert(placementScale != std::numeric_limits<float>::infinity());
for (const GlyphBox &glyph : glyphs) {
const CollisionRect &bbox = glyph.bbox;
const CollisionRect &box = glyph.box;
@@ -288,6 +312,9 @@ void Collision::insert(const GlyphBoxes &glyphs, const CollisionAnchor &anchor,
placement.maxScale = glyph.maxScale;
placement.padding = padding;
tree.insert({bounds, placement});
assert(!isnan(bounds.min_corner().get<0>()) && !isnan(bounds.min_corner().get<1>()));
assert(!isnan(bounds.max_corner().get<0>()) && !isnan(bounds.max_corner().get<1>()));
((Tree *)tree)->insert({bounds, placement});
}
}

320
src/text/placement.cpp Normal file
View File

@@ -0,0 +1,320 @@
#include <llmr/text/placement.hpp>
#include <llmr/map/vector_tile.hpp>
#include <llmr/geometry/interpolate.hpp>
#include <llmr/style/bucket_description.hpp>
#include <llmr/renderer/text_bucket.hpp>
#include <llmr/util/math.hpp>
#include <llmr/util/constants.hpp>
using namespace llmr;
const int Placement::tileExtent = 4096;
const int Placement::glyphSize =
24; // size in pixels of this glyphs in the tile
const float Placement::minScale = 0.125; // underscale by 3 zoom levels
Placement::Placement(int8_t zoom)
: zoom(zoom),
zOffset(log(256.0 / util::tileSize) / log(2)),
// Calculate the maximum scale we can go down in our fake-3d rtree so that
// placement still makes sense. This is calculated so that the minimum
// placement zoom can be at most 25.5 (we use an unsigned integer x10 to
// store the minimum zoom).
//
// We don't want to place labels all the way to 25.5. This lets too many
// glyphs be placed, slowing down collision checking. Only place labels if
// they will show up within the intended zoom range of the tile.
// TODO make this not hardcoded to 3
maxPlacementScale(exp(log(2) * util::min((25.5 - zoom), 3.0))) {}
bool byScale(const Anchor &a, const Anchor &b) { return a.scale < b.scale; }
static const Glyph null_glyph;
inline const Glyph &getGlyph(const GlyphPlacement &placed,
const IndexedFaceGlyphPositions &faces) {
if (placed.face < faces.size()) {
const GlyphPositions &face = *faces[placed.face];
if (&face) {
if (placed.glyph < face.size()) {
return face[placed.glyph];
}
}
}
return null_glyph;
}
struct GlyphInstance {
explicit GlyphInstance(const vec2<float> &anchor) : anchor(anchor) {}
explicit GlyphInstance(const vec2<float> &anchor, float offset,
float minScale, float maxScale, float angle)
: anchor(anchor),
offset(offset),
minScale(minScale),
maxScale(maxScale),
angle(angle) {}
const vec2<float> anchor;
const float offset = 0.0f;
const float minScale = Placement::minScale;
const float maxScale = std::numeric_limits<float>::infinity();
const float angle = 0.0f;
};
typedef std::vector<GlyphInstance> GlyphInstances;
void getSegmentGlyphs(std::back_insert_iterator<GlyphInstances> glyphs,
Anchor &anchor, float offset,
const std::vector<Coordinate> &line, int segment,
int8_t direction, float maxAngleDelta) {
const bool upsideDown = direction < 0;
if (offset < 0)
direction *= -1;
if (direction > 0)
segment++;
vec2<float> newAnchor = anchor;
if (line.size() <= segment) {
return;
}
vec2<float> end = line[segment];
float prevscale = std::numeric_limits<float>::infinity();
float prevAngle = 0.0f;
offset = fabs(offset);
const float placementScale = anchor.scale;
while (true) {
const float dist = util::dist<float>(newAnchor, end);
const float scale = offset / dist;
float angle = -atan2(end.x - newAnchor.x, end.y - newAnchor.y) +
direction * M_PI / 2.0f;
if (upsideDown)
angle += M_PI;
// Don't place around sharp corners
float angleDiff = fmod((angle - prevAngle), (2.0f * M_PI));
if (prevAngle && angleDiff > maxAngleDelta) {
anchor.scale = prevscale;
break;
}
glyphs = GlyphInstance{
/* anchor */ newAnchor,
/* offset */ static_cast<float>(upsideDown ? M_PI : 0.0),
/* minScale */ scale,
/* maxScale */ prevscale,
/* angle */ static_cast<float>(
fmod((angle + 2.0 * M_PI), (2.0 * M_PI)))};
if (scale <= placementScale)
break;
newAnchor = end;
// skip duplicate nodes
while (newAnchor == end) {
segment += direction;
if (line.size() <= segment) {
anchor.scale = scale;
return;
}
end = line[segment];
}
vec2<float> normal = util::normal<float>(newAnchor, end) * dist;
newAnchor = newAnchor - normal;
prevscale = scale;
prevAngle = angle;
}
}
PlacedGlyphs getGlyphs(Anchor &anchor, float advance, const Shaping &shaping,
const IndexedFaceGlyphPositions &faces, float fontScale,
bool horizontal, const std::vector<Coordinate> &line,
float maxAngleDelta, float rotate) {
// The total text advance is the width of this label.
// TODO: figure out correct ascender height.
vec2<float> origin{-advance / 2, -17};
// TODO: allow setting an alignment
// var alignment = 'center';
// if (alignment == 'center') {
// origin.x -= advance / 2;
// } else if (alignment == 'right') {
// origin.x -= advance;
// }
PlacedGlyphs glyphs;
const uint32_t buffer = 3;
for (const GlyphPlacement &placed : shaping) {
const Glyph &glyph = getGlyph(placed, faces);
if (!glyph) {
// This glyph is empty and doesn't have any pixels that we'd need to
// show.
continue;
}
uint32_t width = glyph.metrics.width;
uint32_t height = glyph.metrics.height;
width += buffer * 2;
height += buffer * 2;
// Increase to next number divisible by 4, but at least 1.
// This is so we can scale down the texture coordinates and pack them
// into 2 bytes rather than 4 bytes.
width += (4 - width % 4);
height += (4 - height % 4);
float x =
(origin.x + placed.x + glyph.metrics.left - buffer + width / 2) *
fontScale;
GlyphInstances glyphInstances;
if (anchor.segment >= 0) {
getSegmentGlyphs(std::back_inserter(glyphInstances), anchor, x,
line, anchor.segment, 1, maxAngleDelta);
getSegmentGlyphs(std::back_inserter(glyphInstances), anchor, x,
line, anchor.segment, -1, maxAngleDelta);
} else {
glyphInstances.emplace_back(GlyphInstance{anchor});
}
const float x1 = origin.x + placed.x + glyph.metrics.left - buffer;
const float y1 = origin.y + placed.y - glyph.metrics.top - buffer;
const float x2 = x1 + width;
const float y2 = y1 + height;
const vec2<float> otl{x1, y1};
const vec2<float> otr{x2, y1};
const vec2<float> obl{x1, y2};
const vec2<float> obr{x2, y2};
const CollisionRect obox{fontScale *x1, fontScale *y1,
fontScale *x2, fontScale *y2};
for (const GlyphInstance &instance : glyphInstances) {
vec2<float> tl = otl;
vec2<float> tr = otr;
vec2<float> bl = obl;
vec2<float> br = obr;
CollisionRect box = obox;
// Clamp to -90/+90 degrees
const float angle = instance.angle + rotate;
if (angle) {
// Compute the transformation matrix.
float angle_sin = sin(angle);
float angle_cos = cos(angle);
std::array<float, 4> matrix = {
{angle_cos, -angle_sin, angle_sin, angle_cos}};
tl = tl.matMul(matrix);
tr = tr.matMul(matrix);
bl = bl.matMul(matrix);
br = br.matMul(matrix);
// Calculate the rotated glyph's bounding box offsets from the
// anchor point.
box = CollisionRect{
fontScale * util::min(tl.x, tr.x, bl.x, br.x),
fontScale * util::min(tl.y, tr.y, bl.y, br.y),
fontScale * util::max(tl.x, tr.x, bl.x, br.x),
fontScale * util::max(tl.y, tr.y, bl.y, br.y)};
}
// Remember the glyph for later insertion.
glyphs.emplace_back(PlacedGlyph{
tl, tr, bl, br, glyph.rect, width, height,
static_cast<float>(
fmod((anchor.angle + rotate + instance.offset + 2 * M_PI),
(2 * M_PI))),
GlyphBox{box, instance.minScale, instance.maxScale,
instance.anchor, horizontal}});
}
}
// Prevent label from extending past the end of the line
for (PlacedGlyph &g : glyphs) {
g.glyphBox.minScale = util::max(g.glyphBox.minScale, anchor.scale);
}
return glyphs;
}
void Placement::addFeature(TextBucket& bucket,
const std::vector<Coordinate> &line,
const BucketGeometryDescription &info,
const IndexedFaceGlyphPositions &faces,
const Shaping &shaping) {
const bool horizontal = info.path == TextPathType::Horizontal;
const float padding = info.padding;
const float maxAngleDelta = info.maxAngleDelta;
const float textMinDistance = info.textMinDistance;
const float rotate = info.rotate;
const float fontScale =
(tileExtent / util::tileSize) / (glyphSize / info.font_size);
const float advance = measureText(faces, shaping);
Anchors anchors;
// fprintf(stderr, "adding feature with advance %f\n", advance);
if (line.size() == 1) {
// Point labels
anchors = Anchors{{Anchor{
static_cast<float>(line[0].x), static_cast<float>(line[0].y),
0, minScale}}};
} else {
// Line labels
anchors = interpolate(line, textMinDistance, minScale);
// Sort anchors by segment so that we can start placement with
// the anchors that can be shown at the lowest zoom levels.
std::sort(anchors.begin(), anchors.end(), byScale);
}
for (Anchor anchor : anchors) {
PlacedGlyphs glyphs =
getGlyphs(anchor, advance, shaping, faces, fontScale, horizontal,
line, maxAngleDelta, rotate);
PlacementProperty place =
collision.place(glyphs, anchor, anchor.scale, maxPlacementScale,
padding, horizontal);
if (place.zoom >= 0.0f) {
bucket.addGlyphs(glyphs, place.zoom, place.rotationRange,
zoom - zOffset);
}
}
}
float Placement::measureText(const IndexedFaceGlyphPositions &faces,
const Shaping &shaping) {
float advance = 0;
// TODO: advance is not calculated correctly. we should instead use the
// bounding box of the glyph placement.
for (const GlyphPlacement &shape : shaping) {
advance += getGlyph(shape, faces).metrics.advance;
}
return advance;
}

View File

@@ -225,13 +225,12 @@ CollisionList rotatingFixedCollisions(const CollisionRect &rotating,
/*
* Calculate the range a box conflicts with a second box
*/
template <typename InsertingBox, typename BlockingBox>
CollisionRange rotationRange(const InsertingBox &inserting,
const BlockingBox &blocker, float scale) {
CollisionRange rotationRange(const GlyphBox &inserting,
const PlacementBox &blocker, float scale) {
CollisionList collisions;
const InsertingBox &a = inserting;
const BlockingBox &b = blocker;
const GlyphBox &a = inserting;
const PlacementBox &b = blocker;
// Instead of scaling the boxes, we move the anchors
CollisionAnchor relativeAnchor{
@@ -242,12 +241,12 @@ CollisionRange rotationRange(const InsertingBox &inserting,
if (a.rotate && b.rotate) {
collisions = rotatingRotatingCollisions(a.box, b.box, relativeAnchor);
} else if (a.rotate) {
const CollisionRect box = {
const CollisionRect box {
b.box.tl.x + relativeAnchor.x, b.box.tl.y + relativeAnchor.y,
b.box.br.x + relativeAnchor.x, b.box.br.y + relativeAnchor.y};
collisions = rotatingFixedCollisions(a.box, box);
} else if (b.rotate) {
const CollisionRect box = {
const CollisionRect box {
a.box.tl.x - relativeAnchor.x, a.box.tl.y - relativeAnchor.y,
a.box.br.x - relativeAnchor.x, a.box.br.y - relativeAnchor.y};
collisions = rotatingFixedCollisions(b.box, box);