Skip to content

Commit

Permalink
use the new unit system for font sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed May 17, 2020
1 parent 619edf0 commit dcafb51
Show file tree
Hide file tree
Showing 36 changed files with 246 additions and 301 deletions.
2 changes: 1 addition & 1 deletion src/draw/text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ ReturnCode text_draw(
{"color", std::bind(&color_read, ctx, _1, &style.color)},
{"text", std::bind(&expr_to_string, _1, &text)},
{"font", expr_call_string_fn(std::bind(&font_load_best, _1, &style.font))},
{"font-size", std::bind(&measure_read, _1, &style.font_size)},
{"font-size", std::bind(&expr_to_font_size, _1, *layer, &style.font_size)},
});

if (!config_rc) {
Expand Down
16 changes: 8 additions & 8 deletions src/figure/legend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ LegendConfig::LegendConfig() :
void legend_normalize(
Context* ctx,
LegendConfig* config) {
for (auto& m : config->margins) {
convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &m);
}
//for (auto& m : config->margins) {
// convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &m);
//}

for (auto& m : config->padding) {
convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &m);
}
//for (auto& m : config->padding) {
// convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &m);
//}

convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &config->item_row_padding);
convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &config->item_column_padding);
//convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &config->item_row_padding);
//convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &config->item_column_padding);
}

ReturnCode legend_layout_item_rows(
Expand Down
46 changes: 24 additions & 22 deletions src/figure/legend_item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ namespace clip::plotgen {
void legend_item_normalize(
Context* ctx,
LegendItem* config) {
convert_unit_typographic(
layer_get_dpi(ctx),
layer_get_font_size(ctx),
&config->label_font_size);

convert_unit_typographic(
layer_get_dpi(ctx),
config->label_font_size,
&config->label_margin);

convert_unit_typographic(
layer_get_dpi(ctx),
layer_get_font_size(ctx),
&config->marker_size);

convert_unit_typographic(
layer_get_dpi(ctx),
config->marker_size,
&config->marker_margin);
//convert_unit_typographic(
// layer_get_dpi(ctx),
// layer_get_font_size(ctx),
// &config->label_font_size);

//convert_unit_typographic(
// layer_get_dpi(ctx),
// config->label_font_size,
// &config->label_margin);

//convert_unit_typographic(
// layer_get_dpi(ctx),
// layer_get_font_size(ctx),
// &config->marker_size);

//convert_unit_typographic(
// layer_get_dpi(ctx),
// config->marker_size,
// &config->marker_margin);
}

ReturnCode legend_item_calculate_size(
Expand Down Expand Up @@ -172,11 +172,13 @@ ReturnCode legend_item_configure(
Context* ctx,
const Expr* expr,
LegendItem* config) {
const auto& layer = *layer_get(ctx);

/* inherit defaults */
config->label_align = HAlign::LEFT;
config->label_margin = from_em(1.1);
config->label_font = layer_get_font(ctx);
config->label_font_size = from_em(1);
config->label_font_size = layer_get_font_size(ctx);
config->label_color = layer_get(ctx)->text_color;
config->marker = marker_create_disk();
config->marker_align = HAlign::LEFT;
Expand All @@ -199,7 +201,7 @@ ReturnCode legend_item_configure(
{"label-margin", std::bind(&measure_read, _1, &config->label_margin)},
{"label-color", std::bind(&color_read, ctx, _1, &config->label_color)},
{"label-font", expr_call_string_fn(std::bind(&font_load_best, _1, &config->label_font))},
{"label-font-size", std::bind(&measure_read, _1, &config->label_font_size)},
{"label-font-size", std::bind(&expr_to_font_size, _1, layer, &config->label_font_size)},
{"marker-shape", std::bind(&marker_configure, _1, &config->marker)},
{
"marker-align",
Expand All @@ -210,7 +212,7 @@ ReturnCode legend_item_configure(
},
{"marker-margin", std::bind(&measure_read, _1, &config->marker_margin)},
{"marker-color", std::bind(&color_read, ctx, _1, &config->marker_color)},
{"marker-size", std::bind(&measure_read, _1, &config->marker_size)},
{"marker-size", std::bind(&expr_to_font_size, _1, layer, &config->marker_size)},
{"color", std::bind(&color_read, ctx, _1, &config->marker_color)},
});

Expand Down
4 changes: 2 additions & 2 deletions src/figure/legend_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ struct LegendItem {
Measure label_margin;
Color label_color;
FontInfo label_font;
Measure label_font_size;
Number label_font_size;
Marker marker;
HAlign marker_align;
Measure marker_margin;
Color marker_color;
Measure marker_size;
Number marker_size;
};

ReturnCode legend_item_configure(
Expand Down
5 changes: 0 additions & 5 deletions src/graphics/draw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ void draw_polygon(
const Poly2& poly,
StrokeStyle stroke_style,
FillStyle fill_style) {
convert_unit_typographic(
layer_get_dpi(ctx),
layer_get_font_size(ctx),
&stroke_style.line_width);

DrawCommand elem;
elem.path = path_from_poly2(poly);
elem.stroke_style = stroke_style;
Expand Down
9 changes: 5 additions & 4 deletions src/graphics/draw_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ void draw_line(

void draw_text(Context* ctx, const TextInfo& elem) {
const auto& style = elem.style;
const auto& layer = layer_get(ctx);
auto dpi = layer_get_dpi(layer);
auto font_size_pt = unit_to_pt(elem.style.font_size, dpi);

for (const auto& gg : elem.glyphs) {
for (const auto& g : gg.glyphs) {
Expand All @@ -61,8 +64,8 @@ void draw_text(Context* ctx, const TextInfo& elem) {
Path gp;
auto rc = font_get_glyph_path(
g.font,
elem.style.font_size,
layer_get_dpi(ctx),
font_size_pt,
dpi,
g.codepoint,
&gp);

Expand All @@ -89,8 +92,6 @@ ReturnCode draw_text(
TextStyle style) {
const auto layer = layer_get(ctx);

convert_unit_typographic(layer_get_dpi(ctx), layer_get_rem(ctx), &style.font_size);

text::TextSpan span;
span.text_direction = style.direction;
span.text = text;
Expand Down
8 changes: 4 additions & 4 deletions src/graphics/export_svg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ Status svg_add_text_elem_native(
<< svg_attr("x", origin.x)
<< svg_attr("y", origin.y)
<< svg_attr("fill", style.color.to_hex_str(4))
<< svg_attr("font-size", style.font_size)
<< svg_attr("font-size", style.font_size.value)
<< svg_attr("font-family", style.font.font_family_css)
<< svg_attr("font-weight", style.font.font_weight_css)
<< transform
Expand Down Expand Up @@ -283,7 +283,7 @@ Status svg_add_text_elem_embed(
Path gp;
auto rc = font_get_glyph_path(
g.font,
elem.style.font_size,
unit_to_pt(elem.style.font_size, dpi),
dpi,
g.codepoint,
&gp);
Expand Down Expand Up @@ -324,8 +324,8 @@ struct SVGDrawOp {
ReturnCode export_svg(
const Layer* layer,
std::string* buffer) {
auto width = convert_unit(*layer, layer->width);
auto height = convert_unit(*layer, layer->height);
auto width = layer_get_width(*layer).value;
auto height = layer_get_height(*layer).value;

auto svg = std::make_shared<SVGData>();
svg->width = width;
Expand Down
5 changes: 2 additions & 3 deletions src/graphics/font_lookup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ struct GlyphContour {

ReturnCode font_get_glyph_path(
FontRef font,
double font_size,
NumberPT font_size,
double dpi,
uint32_t codepoint,
Path* path) {
*path = Path{};

// load the glyph using freetype
auto font_size_ft = font_size * (72.0 / dpi) * 64;
if (FT_Set_Char_Size(font->ft_font, 0, font_size_ft, dpi, dpi)) {
if (FT_Set_Char_Size(font->ft_font, 0, font_size.value * 64, dpi, dpi)) {
return ERROR;
}

Expand Down
3 changes: 2 additions & 1 deletion src/graphics/font_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <memory>
#include "return_code.h"
#include "path.h"
#include "units.h"

namespace clip {

Expand All @@ -39,7 +40,7 @@ ReturnCode font_load_best(

ReturnCode font_get_glyph_path(
FontRef font,
double font_size,
NumberPT font_size,
double dpi,
uint32_t codepoint,
Path* path);
Expand Down
1 change: 0 additions & 1 deletion src/graphics/measure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ Measure measure_or(const Measure& primary, const Measure& fallback) {
void convert_unit(
const Layer& layer,
Measure* measure) {
convert_unit_typographic(layer.dpi, layer.font_size, measure);
}

Measure convert_unit(
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/rasterize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ Status Rasterizer::drawText(
}

for (const auto& gg : glyphs) {

auto ft_font = static_cast<FT_Face>(font_get_freetype(gg.font));
auto font_size_ft = style.font_size * (72.0 / dpi) * 64;
auto font_size_pt = unit_to_pt(style.font_size, dpi);
auto font_size_ft = font_size_pt.value * (72.0 / dpi) * 64;
if (FT_Set_Char_Size(ft_font, 0, font_size_ft, dpi, dpi)) {
FT_Done_Face(ft_font);
return ERROR;
Expand All @@ -165,7 +165,7 @@ Status Rasterizer::drawText(

auto cairo_face = cairo_ft_font_face_create_for_ft_face(ft_font, 0);
cairo_set_font_face(cr_ctx, cairo_face);
cairo_set_font_size(cr_ctx, style.font_size);
cairo_set_font_size(cr_ctx, font_size_pt.value);

auto glyph_count = gg.glyphs.size();
auto cairo_glyphs = cairo_glyph_allocate(glyph_count);
Expand Down
18 changes: 17 additions & 1 deletion src/graphics/text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Status text_measure_label(
const std::string& text,
TextDirection text_direction_base,
const FontInfo& font,
double font_size,
Number font_size,
double dpi,
Rectangle* bbox) {
text::TextSpan span;
Expand All @@ -44,5 +44,21 @@ Status text_measure_label(
return text_layout_line(line, dpi, nullptr, bbox);
}

Status text_measure_label(
const std::string& text,
TextDirection text_direction_base,
const FontInfo& font,
double font_size,
double dpi,
Rectangle* bbox) {
return text_measure_label(
text,
text_direction_base,
font,
Number(font_size),
dpi,
bbox);
}

} // namespace clip

10 changes: 9 additions & 1 deletion src/graphics/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct TextStyle {
TextStyle();
TextDirection direction;
FontInfo font;
Measure font_size;
Number font_size;
Color color;
std::string default_script;
std::string default_language;
Expand All @@ -48,6 +48,14 @@ Status text_measure_label(
double dpi,
Rectangle* bbox);

Status text_measure_label(
const std::string& text,
TextDirection text_direction_base,
const FontInfo& font,
Number font_size,
double dpi,
Rectangle* bbox);


} // namespace clip

2 changes: 1 addition & 1 deletion src/graphics/text_layout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Status text_layout_span(
span.language,
span.script,
span.font,
span.font_size,
unit_to_pt(span.font_size, dpi),
dpi,
&glyph_list);

Expand Down
2 changes: 1 addition & 1 deletion src/graphics/text_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct TextSpan {
std::string language;
std::string script;
FontInfo font;
double font_size;
Number font_size;
uint32_t span_id;
};

Expand Down
7 changes: 3 additions & 4 deletions src/graphics/text_shaper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ Status text_shape_run(
const std::string& language,
const std::string& script,
FontRef font,
double font_size,
NumberPT font_size,
double dpi,
std::vector<GlyphInfo>* glyphs) {
/* get freetype font */
auto ft_font = static_cast<FT_Face>(font_get_freetype(font));
auto font_size_ft = font_size * (72.0 / dpi) * 64;
if (FT_Set_Char_Size(ft_font, 0, font_size_ft, dpi, dpi)) {
if (FT_Set_Char_Size(ft_font, 0, font_size.value * 64, dpi, dpi)) {
return ERROR;
}

Expand Down Expand Up @@ -109,7 +108,7 @@ Status text_shape_run_with_font_fallback(
const std::string& text_language,
const std::string& text_script,
const FontInfo& font_info,
double font_size,
NumberPT font_size,
double dpi,
std::vector<GlyphInfo>* glyphs) {

Expand Down
4 changes: 2 additions & 2 deletions src/graphics/text_shaper.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Status text_shape_run(
const std::string& text_language,
const std::string& text_script,
FontRef font,
double font_size,
NumberPT font_size,
double dpi,
std::vector<GlyphInfo>* glyphs);

Expand All @@ -53,7 +53,7 @@ Status text_shape_run_with_font_fallback(
const std::string& text_language,
const std::string& text_script,
const FontInfo& font_info,
double font_size,
NumberPT font_size,
double dpi,
std::vector<GlyphInfo>* glyphs);

Expand Down
Loading

0 comments on commit dcafb51

Please sign in to comment.