Merge pull request #5765 from harfbuzz/hb-vector-svg-improvements

Hb vector svg improvements
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-02-26 13:54:56 -07:00
commit 648c75b112
24 files changed
+610 -120

No files matched your search

+2
View File
@@ -1089,6 +1089,8 @@ hb_vector_paint_get_extents
hb_vector_paint_set_glyph_extents
hb_vector_paint_set_foreground
hb_vector_paint_set_palette
hb_vector_paint_set_custom_palette_color
hb_vector_paint_clear_custom_palette_colors
hb_vector_paint_get_funcs
hb_vector_paint_glyph
hb_vector_svg_paint_set_flat
+11 -7
View File
@@ -1855,9 +1855,12 @@ hb_raster_paint_set_foreground (hb_raster_paint_t *paint,
/**
* hb_raster_paint_clear_custom_palette_colors:
* @paint: a paint context
* @paint: a paint context.
*
* Clears all custom palette color overrides.
* Clears all custom palette color overrides previously set on @paint.
*
* After this call, palette lookups use the selected font palette without
* custom override entries.
*
* XSince: REPLACEME
**/
@@ -1870,12 +1873,13 @@ hb_raster_paint_clear_custom_palette_colors (hb_raster_paint_t *paint)
/**
* hb_raster_paint_set_custom_palette_color:
* @paint: a paint context
* @color_index: palette color index to override
* @color: replacement color for @color_index
* @paint: a paint context.
* @color_index: color index to override.
* @color: replacement color.
*
* Sets a custom palette color override. During paint operations,
* when the font requests @color_index from CPAL, @color is used.
* Overrides one font palette color entry for subsequent paint operations.
* Overrides are keyed by @color_index and persist on @paint until cleared
* (or replaced for the same index).
*
* Return value: `true` if the override was set; `false` on allocation failure.
*
+35
View File
@@ -385,6 +385,7 @@ struct hb_vector_paint_t
hb_color_t foreground = HB_COLOR (0, 0, 0, 255);
int palette = 0;
hb_hashmap_t<unsigned, hb_color_t> custom_palette_colors;
unsigned precision = 2;
bool flat = false;
@@ -969,6 +970,7 @@ static void hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, void *, hb_color
static void hb_vector_paint_push_group (hb_paint_funcs_t *, void *, void *);
static void hb_vector_paint_pop_group (hb_paint_funcs_t *, void *, hb_paint_composite_mode_t, void *);
static hb_bool_t hb_vector_paint_color_glyph (hb_paint_funcs_t *, void *, hb_codepoint_t, hb_font_t *, void *);
static hb_bool_t hb_vector_paint_custom_palette_color (hb_paint_funcs_t *, void *, unsigned, hb_color_t *, void *);
static inline void free_static_vector_paint_funcs ();
static struct hb_vector_paint_funcs_lazy_loader_t
@@ -990,6 +992,7 @@ static struct hb_vector_paint_funcs_lazy_loader_t
hb_paint_funcs_set_push_group_func (funcs, (hb_paint_push_group_func_t) hb_vector_paint_push_group, nullptr, nullptr);
hb_paint_funcs_set_pop_group_func (funcs, (hb_paint_pop_group_func_t) hb_vector_paint_pop_group, nullptr, nullptr);
hb_paint_funcs_set_color_glyph_func (funcs, (hb_paint_color_glyph_func_t) hb_vector_paint_color_glyph, nullptr, nullptr);
hb_paint_funcs_set_custom_palette_color_func (funcs, (hb_paint_custom_palette_color_func_t) hb_vector_paint_custom_palette_color, nullptr, nullptr);
hb_paint_funcs_make_immutable (funcs);
hb_atexit (free_static_vector_paint_funcs);
return funcs;
@@ -1656,12 +1659,44 @@ hb_vector_paint_set_palette (hb_vector_paint_t *paint,
paint->palette = palette;
}
void
hb_vector_paint_set_custom_palette_color (hb_vector_paint_t *paint,
unsigned color_index,
hb_color_t color)
{
paint->custom_palette_colors.set (color_index, color);
}
void
hb_vector_paint_clear_custom_palette_colors (hb_vector_paint_t *paint)
{
paint->custom_palette_colors.clear ();
}
hb_paint_funcs_t *
hb_vector_paint_get_funcs (void)
{
return hb_vector_paint_funcs_get ();
}
static hb_bool_t
hb_vector_paint_custom_palette_color (hb_paint_funcs_t *pfuncs HB_UNUSED,
void *paint_data,
unsigned color_index,
hb_color_t *color,
void *user_data HB_UNUSED)
{
hb_vector_paint_t *paint = (hb_vector_paint_t *) paint_data;
if (!paint || !color)
return false;
hb_color_t *value = nullptr;
if (!paint->custom_palette_colors.has (color_index, &value) || !value)
return false;
*color = *value;
return true;
}
hb_bool_t
hb_vector_paint_glyph (hb_vector_paint_t *paint,
hb_font_t *font,
+34
View File
@@ -524,6 +524,40 @@ HB_EXTERN void
hb_vector_paint_set_palette (hb_vector_paint_t *paint,
int palette);
/**
* hb_vector_paint_set_custom_palette_color:
* @paint: a paint context.
* @color_index: color index to override.
* @color: replacement color.
*
* Overrides one font palette color entry for subsequent paint operations.
* Overrides are keyed by @color_index and persist on @paint until cleared
* (or replaced for the same index).
*
* These overrides are consulted by paint operations that resolve CPAL
* entries, including SVG glyph content using `var(--colorN)`.
*
* XSince: REPLACEME
*/
HB_EXTERN void
hb_vector_paint_set_custom_palette_color (hb_vector_paint_t *paint,
unsigned color_index,
hb_color_t color);
/**
* hb_vector_paint_clear_custom_palette_colors:
* @paint: a paint context.
*
* Clears all custom palette color overrides previously set on @paint.
*
* After this call, palette lookups use the selected font palette without
* custom override entries.
*
* XSince: REPLACEME
*/
HB_EXTERN void
hb_vector_paint_clear_custom_palette_colors (hb_vector_paint_t *paint);
/**
* hb_vector_paint_get_funcs:
*
+3 -3
View File
@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -984 1513 1257" width="1513" height="1257">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1000 1545 1289" width="1545" height="1289">
<defs>
<path id="p1" d="M194,-12C255,-12 309,20 355,58L358,58L365,0L433,0L433,298C433,419 383,498 264,498C186,498 118,464 73,435L105,378C143,404 194,430 250,430C330,430 350,370 350,308C143,285 52,232 52,126C52,39 113,-12 194,-12ZM218,54C170,54 132,77 132,132C132,194 188,234 350,254L350,119C303,77 265,54 218,54Z"/>
@@ -7,6 +7,6 @@
</defs>
<use href="#p1" transform="translate(0,0) scale(1,-1)"/>
<use href="#p2" transform="translate(504,0) scale(1,-1)"/>
<use href="#p3" transform="translate(1057,0) scale(1,-1)"/>
<use href="#p2" transform="translate(504.03,0) scale(1,-1)"/>
<use href="#p3" transform="translate(1057.01,0) scale(1,-1)"/>
</svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -918 1696 1253" width="1696" height="1253">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -934 1727.98 1285" width="1727.98" height="1285">
<defs>
<path id="p1" d="M25,105C25,32 76,-16 155,-16C232,-16 263,15 306,64L353,117L344,125L296,80C276,62 262,56 249,56C217,56 197,79 197,129C197,201 233,243 279,263C295,270 343,282 391,295L391,320C345,308 271,289 230,278C71,235 25,191 25,105ZM291,72C304,19 341,-16 414,-16C485,-16 524,8 548,69L530,79C513,48 504,41 493,41C482,41 475,48 475,65L475,308C475,445 424,503 286,503C125,503 42,444 42,358C52,322 79,304 118,304C161,304 200,334 200,415L200,478L120,456C166,466 190,469 215,469C278,469 291,441 291,356L291,72Z"/>
@@ -7,6 +7,6 @@
</defs>
<use href="#p1" transform="translate(0,0) scale(1,-1)"/>
<use href="#p2" transform="translate(540,0) scale(1,-1)"/>
<use href="#p3" transform="translate(1173,0) scale(1,-1)"/>
<use href="#p2" transform="translate(539.98,0) scale(1,-1)"/>
<use href="#p3" transform="translate(1172.97,0) scale(1,-1)"/>
</svg>

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -1,10 +1,10 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -984 1800 1257" width="1800" height="1257">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1000 1831.93 1289" width="1831.93" height="1289">
<defs>
<path id="p0" d="M24,0L576,0L576,660L24,660L24,0ZM236,104L262,160L298,256L302,256L338,160L364,104L236,104ZM136,172L136,508L218,340L136,172ZM298,424L258,520L240,556L360,556L342,520L302,424L298,424ZM382,340L464,508L464,172L382,340Z"/>
</defs>
<use href="#p0" transform="translate(0,0) scale(1,-1)"/>
<use href="#p0" transform="translate(600,0) scale(1,-1)"/>
<use href="#p0" transform="translate(1200,0) scale(1,-1)"/>
<use href="#p0" transform="translate(599.98,0) scale(1,-1)"/>
<use href="#p0" transform="translate(1199.95,0) scale(1,-1)"/>
</svg>

Before

Width:  |  Height:  |  Size: 528 B

After

Width:  |  Height:  |  Size: 543 B

+1 -1
View File
@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -1900 3335 2400" width="3335" height="2400">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1916 3367 2432" width="3367" height="2432">
<defs>
<path id="p1" d="M808,0Q792,32 782,114Q653,-20 474,-20Q314,-20 211.5,70.5Q109,161 109,300Q109,469 237.5,562.5Q366,656 599,656L779,656L779,741Q779,838 721,895.5Q663,953 550,953Q451,953 384,903Q317,853 317,782L131,782Q131,863 188.5,938.5Q246,1014 344.5,1058Q443,1102 561,1102Q748,1102 854,1008.5Q960,915 964,751L964,253Q964,104 1002,16L1002,0L808,0ZM501,141Q588,141 666,186Q744,231 779,303L779,525L634,525Q294,525 294,326Q294,239 352,190Q410,141 501,141Z"/>

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -1,6 +1,6 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -984 1513 1257" width="1513" height="1257">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1000 1545 1289" width="1545" height="1289">
<path d="M194,-12C255,-12 309,20 355,58L358,58L365,0L433,0L433,298C433,419 383,498 264,498C186,498 118,464 73,435L105,378C143,404 194,430 250,430C330,430 350,370 350,308C143,285 52,232 52,126C52,39 113,-12 194,-12ZM218,54C170,54 132,77 132,132C132,194 188,234 350,254L350,119C303,77 265,54 218,54Z" transform="translate(0,0) scale(1,-1)"/>
<path d="M297,-12C408,-12 507,85 507,251C507,401 440,498 314,498C260,498 206,469 162,430L164,518L164,712L82,712L82,0L148,0L155,50L158,50C201,11 252,-12 297,-12ZM283,57C251,57 207,71 164,108L164,363C210,406 253,429 294,429C386,429 422,357 422,250C422,130 363,57 283,57Z" transform="translate(504,0) scale(1,-1)"/>
<path d="M274,-12C332,-12 387,11 431,50L395,105C365,78 326,56 281,56C192,56 131,131 131,242C131,354 195,430 283,430C322,430 352,412 380,387L422,441C388,472 344,498 280,498C155,498 46,405 46,242C46,81 145,-12 274,-12Z" transform="translate(1057,0) scale(1,-1)"/>
<path d="M297,-12C408,-12 507,85 507,251C507,401 440,498 314,498C260,498 206,469 162,430L164,518L164,712L82,712L82,0L148,0L155,50L158,50C201,11 252,-12 297,-12ZM283,57C251,57 207,71 164,108L164,363C210,406 253,429 294,429C386,429 422,357 422,250C422,130 363,57 283,57Z" transform="translate(504.03,0) scale(1,-1)"/>
<path d="M274,-12C332,-12 387,11 431,50L395,105C365,78 326,56 281,56C192,56 131,131 131,242C131,354 195,430 283,430C322,430 352,412 380,387L422,441C388,472 344,498 280,498C155,498 46,405 46,242C46,81 145,-12 274,-12Z" transform="translate(1057.01,0) scale(1,-1)"/>
</svg>

Before

Width:  |  Height:  |  Size: 1018 B

After

Width:  |  Height:  |  Size: 1.0 KiB

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -1900 3335 2400" width="3335" height="2400">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1916 3367 2432" width="3367" height="2432">
<path d="M808,0Q792,32 782,114Q653,-20 474,-20Q314,-20 212,70Q109,161 109,300Q109,469 238,562Q366,656 599,656L779,656L779,741Q779,838 721,896Q663,953 550,953Q451,953 384,903Q317,853 317,782L131,782Q131,863 188,938Q246,1014 344,1058Q443,1102 561,1102Q748,1102 854,1008Q960,915 964,751L964,253Q964,104 1002,16L1002,0L808,0ZM501,141Q588,141 666,186Q744,231 779,303L779,525L634,525Q294,525 294,326Q294,239 352,190Q410,141 501,141Z" transform="translate(0,0) scale(1,-1)"/>
<path d="M1056,529Q1056,281 942,130Q828,-20 636,-20Q431,-20 319,125L310,0L140,0L140,1536L325,1536L325,963Q437,1102 634,1102Q831,1102 944,953Q1056,804 1056,545L1056,529ZM871,550Q871,739 798,842Q725,945 588,945Q405,945 325,775L325,307Q410,137 590,137Q723,137 797,240Q871,343 871,550Z" transform="translate(1114,0) scale(1,-1)"/>

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -1900 3335 2400" width="3335" height="2400">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1916 3367 2432" width="3367" height="2432">
<path d="M808,0Q792,32 782,114Q653,-20 474,-20Q314,-20 211.5,70.5Q109,161 109,300Q109,469 237.5,562.5Q366,656 599,656L779,656L779,741Q779,838 721,895.5Q663,953 550,953Q451,953 384,903Q317,853 317,782L131,782Q131,863 188.5,938.5Q246,1014 344.5,1058Q443,1102 561,1102Q748,1102 854,1008.5Q960,915 964,751L964,253Q964,104 1002,16L1002,0L808,0ZM501,141Q588,141 666,186Q744,231 779,303L779,525L634,525Q294,525 294,326Q294,239 352,190Q410,141 501,141Z" transform="translate(0,0) scale(1,-1)"/>
<path d="M1056,529Q1056,281 942,130.5Q828,-20 636,-20Q431,-20 319,125L310,0L140,0L140,1536L325,1536L325,963Q437,1102 634,1102Q831,1102 943.5,953Q1056,804 1056,545L1056,529ZM871,550Q871,739 798,842Q725,945 588,945Q405,945 325,775L325,307Q410,137 590,137Q723,137 797,240Q871,343 871,550Z" transform="translate(1114,0) scale(1,-1)"/>

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

+1 -1
View File
@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -1900 3335 2400" width="3335" height="2400">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1916 3367 2432" width="3367" height="2432">
<path d="M808,0Q792,32 782,114Q653,-20 474,-20Q314,-20 211.5,70.5Q109,161 109,300Q109,469 237.5,562.5Q366,656 599,656L779,656L779,741Q779,838 721,895.5Q663,953 550,953Q451,953 384,903Q317,853 317,782L131,782Q131,863 188.5,938.5Q246,1014 344.5,1058Q443,1102 561,1102Q748,1102 854,1008.5Q960,915 964,751L964,253Q964,104 1002,16L1002,0L808,0ZM501,141Q588,141 666,186Q744,231 779,303L779,525L634,525Q294,525 294,326Q294,239 352,190Q410,141 501,141Z" transform="translate(0,0) scale(1,-1)"/>
<path d="M1056,529Q1056,281 942,130.5Q828,-20 636,-20Q431,-20 319,125L310,0L140,0L140,1536L325,1536L325,963Q437,1102 634,1102Q831,1102 943.5,953Q1056,804 1056,545L1056,529ZM871,550Q871,739 798,842Q725,945 588,945Q405,945 325,775L325,307Q410,137 590,137Q723,137 797,240Q871,343 871,550Z" transform="translate(1114,0) scale(1,-1)"/>

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -1900 3306 2400" width="3306" height="2400">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1916 3338 2432" width="3338" height="2432">
<defs>
<path id="p1" d="M657,272L657,718Q657,764 644,797Q631,830 603,848.5Q575,867 528,867Q490,867 463,854Q436,841 422,815Q408,789 408,749L71,749Q71,824 104.5,888.5Q138,953 200.5,1001Q263,1049 350.5,1075.5Q438,1102 546,1102Q675,1102 776.5,1059Q878,1016 937,930.5Q996,845 996,716L996,273Q996,167 1008,112.5Q1020,58 1042,17L1042,0L704,0Q680,50 668.5,122.5Q657,195 657,272ZM698,661L700,481L551,481Q504,481 471.5,468Q439,455 419.5,432.5Q400,410 391.5,382Q383,354 383,323Q383,293 397.5,269.5Q412,246 437,233Q462,220 493,220Q549,220 588.5,241.5Q628,263 649,294Q670,325 670,352L744,214Q724,174 698.5,133Q673,92 635.5,57.5Q598,23 544,1.5Q490,-20 413,-20Q313,-20 229.5,21Q146,62 96,135.5Q46,209 46,305Q46,389 76.5,455Q107,521 169,567Q231,613 327,637Q423,661 553,661L698,661Z"/>

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-500 0 1000 3063" width="1000" height="3063">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-516 -16 1032 3094.99" width="1032" height="3094.99">
<defs>
<path id="p1" d="M25,113Q25,152 42.5,182.5Q60,213 104.5,238.5Q149,264 230,285Q250,290 279,298Q308,306 338,313.5Q368,321 391,327L391,283Q355,273 319.5,263.5Q284,254 262,243Q248,236 233,223.5Q218,211 207.5,191Q197,171 197,144Q197,107 212,89Q227,71 249,71Q256,71 263,73Q270,75 278,79.5Q286,84 296,92L344,128L353,116L306,64Q284,40 264,22Q244,4 219,-6Q194,-16 155,-16Q96,-16 60.5,18Q25,52 25,113ZM414,-16Q359,-16 329.5,8.5Q300,33 291,78L291,337Q291,380 285,404Q279,428 262.5,438.5Q246,449 215,449Q196,449 175,446.5Q154,444 120,436L200,478L200,415Q200,354 175,328.5Q150,303 118,303Q57,303 42,357Q42,422 103.5,462.5Q165,503 286,503Q390,503 432.5,457.5Q475,412 475,309L475,80Q475,72 479.5,67.5Q484,63 492,63Q498,63 504,67.5Q510,72 519,85L548,69Q530,23 498.5,3.5Q467,-16 414,-16Z"/>
@@ -6,7 +6,7 @@
<path id="p3" d="M331,449Q308,449 282.5,431.5Q257,414 239.5,373.5Q222,333 222,264Q222,208 238,170Q254,132 283.5,112Q313,92 353,92Q386,92 410,105.5Q434,119 455,139L494,101Q462,40 407,12Q352,-16 281,-16Q213,-16 154,12Q95,40 58.5,97.5Q22,155 22,242Q22,325 60,383.5Q98,442 163,472.5Q228,503 308,503Q364,503 406,482Q448,461 472,426.5Q496,392 499,351Q480,293 423,293Q392,293 366,317Q340,341 337,407L334,472L417,429Q394,439 375,444Q356,449 331,449Z"/>
</defs>
<use href="#p1" transform="translate(-270,-618) scale(1,-1)"/>
<use href="#p2" transform="translate(-311.5,-1853) scale(1,-1)"/>
<use href="#p3" transform="translate(-261.5,-2652) scale(1,-1)"/>
<use href="#p1" transform="translate(-269.96,-617.98) scale(1,-1)"/>
<use href="#p2" transform="translate(-311.46,-1853.03) scale(1,-1)"/>
<use href="#p3" transform="translate(-261.47,-2652.04) scale(1,-1)"/>
</svg>

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -1900 2221 4800" width="2221" height="4800">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1916 2253 4832" width="2253" height="4832">
<defs>
<path id="p1" d="M808,0Q792,32 782,114Q653,-20 474,-20Q314,-20 211.5,70.5Q109,161 109,300Q109,469 237.5,562.5Q366,656 599,656L779,656L779,741Q779,838 721,895.5Q663,953 550,953Q451,953 384,903Q317,853 317,782L131,782Q131,863 188.5,938.5Q246,1014 344.5,1058Q443,1102 561,1102Q748,1102 854,1008.5Q960,915 964,751L964,253Q964,104 1002,16L1002,0L808,0ZM501,141Q588,141 666,186Q744,231 779,303L779,525L634,525Q294,525 294,326Q294,239 352,190Q410,141 501,141Z"/>

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

+1 -1
View File
@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-1024 0 2048 7200" width="2048" height="7200">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-1040 -16 2080 7232" width="2080" height="7232">
<defs>
<path id="p1" d="M808,0Q792,32 782,114Q653,-20 474,-20Q314,-20 211.5,70.5Q109,161 109,300Q109,469 237.5,562.5Q366,656 599,656L779,656L779,741Q779,838 721,895.5Q663,953 550,953Q451,953 384,903Q317,853 317,782L131,782Q131,863 188.5,938.5Q246,1014 344.5,1058Q443,1102 561,1102Q748,1102 854,1008.5Q960,915 964,751L964,253Q964,104 1002,16L1002,0L808,0ZM501,141Q588,141 666,186Q744,231 779,303L779,525L634,525Q294,525 294,326Q294,239 352,190Q410,141 501,141Z"/>

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -1900 3335 2400" width="3335" height="2400">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -1916 3367 2432" width="3367" height="2432">
<defs>
<path id="p1" d="M808,0Q792,32 782,114Q653,-20 474,-20Q314,-20 211.5,70.5Q109,161 109,300Q109,469 237.5,562.5Q366,656 599,656L779,656L779,741Q779,838 721,895.5Q663,953 550,953Q451,953 384,903Q317,853 317,782L131,782Q131,863 188.5,938.5Q246,1014 344.5,1058Q443,1102 561,1102Q748,1102 854,1008.5Q960,915 964,751L964,253Q964,104 1002,16L1002,0L808,0ZM501,141Q588,141 666,186Q744,231 779,303L779,525L634,525Q294,525 294,326Q294,239 352,190Q410,141 501,141Z"/>

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -800 920 1000" width="920" height="1000">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -816 951.98 1032" width="951.98" height="1032">
<defs>
<path id="p1" d="M85.86,284.12Q191.24,304.99 270.31,341.96Q349.37,378.92 402.13,432.04Q454.88,485.16 481.39,554.48Q507.9,623.79 507.9,709.05L507.9,746L80,746L80,676L463.82,676L426.44,688Q425.94,625 403.09,571.44Q380.24,517.89 334.68,474.9Q289.13,431.9 220.17,399.75Q151.2,367.6 58.78,346.97L85.86,284.12ZM749.14,-93.48L749.14,186L749.14,236L196,236L196,166L712.14,166L674.69,182L674.69,-93.48L749.14,-93.48Z"/>

Before

Width:  |  Height:  |  Size: 585 B

After

Width:  |  Height:  |  Size: 593 B

@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -800 920 1000" width="920" height="1000">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -816 951.98 1032" width="951.98" height="1032">
<defs>
<path id="p1" d="M78.31,273.3Q191.35,293.88 275.6,331.46Q359.86,369.05 415.9,423.76Q471.94,478.48 499.93,549.95Q527.91,621.41 527.91,709.77L527.91,766L64.49,766L64.49,637L455.4,637L376.23,687.31Q375.97,633.07 354.68,587.36Q333.39,541.65 290.83,504.92Q248.27,468.19 183.62,440.29Q118.96,412.38 31.55,393.24L78.31,273.3ZM709,487.99L914,487.99L914,617.99L709,617.99L709,487.99ZM623,292.63L769,292.63L769,823L623,823L623,292.63ZM769.14,-113.07L769.14,176L769.14,246L176,246L176,120L667.41,120L624.73,130L624.73,-113.07L769.14,-113.07Z"/>

Before

Width:  |  Height:  |  Size: 708 B

After

Width:  |  Height:  |  Size: 716 B

+2 -2
View File
@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -800 1840 1000" width="1840" height="1000">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -816 1871.97 1032" width="1871.97" height="1032">
<defs>
<path id="p1" d="M83.45,118.5Q186.08,148.14 265.18,198.36Q344.27,248.59 398.25,318.95Q452.23,389.31 480.07,479.28Q507.91,569.25 507.91,678.17L507.91,722L80,722L80,652L463.43,652L428.29,664Q428.05,575.16 404.72,499.38Q381.39,423.61 334.68,362.23Q287.98,300.85 216.95,254.48Q145.93,208.12 50.21,177.42L83.45,118.5ZM702,400L887,400L887,470L702,470L702,400ZM666,-82L742,-82L742,803L666,803L666,-82Z"/>
@@ -6,5 +6,5 @@
</defs>
<use href="#p1" transform="translate(0,0) scale(1,-1)"/>
<use href="#p2" transform="translate(920,0) scale(1,-1)"/>
<use href="#p2" transform="translate(919.98,0) scale(1,-1)"/>
</svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

+2 -2
View File
@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -800 1840 1000" width="1840" height="1000">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-16 -816 1871.97 1032" width="1871.97" height="1032">
<defs>
<path id="p1" d="M80.52,98.57Q189.32,128.45 272.64,180.18Q355.95,231.91 412.77,304.91Q469.6,377.92 498.76,471.4Q527.91,564.88 527.91,678.3L527.91,742L67.45,742L67.45,608L449.12,608L378.24,656.24Q378.12,575.07 356.22,506.68Q334.32,438.3 290.03,383.05Q245.74,327.8 178.16,284.99Q110.57,242.18 18.37,211.67L80.52,98.57ZM702,370L907,370L907,500L702,500L702,370ZM616,-102L762,-102L762,823L616,823L616,-102Z"/>
@@ -6,5 +6,5 @@
</defs>
<use href="#p1" transform="translate(0,0) scale(1,-1)"/>
<use href="#p2" transform="translate(920,0) scale(1,-1)"/>
<use href="#p2" transform="translate(919.98,0) scale(1,-1)"/>
</svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

+1 -1
View File
@@ -31,7 +31,7 @@
#include "shape-consumer.hh"
#include "text-options.hh"
const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM;
const unsigned DEFAULT_FONT_SIZE = 256;
const unsigned SUBPIXEL_BITS = 6;
int
+1 -1
View File
@@ -29,7 +29,7 @@
#include "shape-consumer.hh"
#include "text-options.hh"
const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM;
const unsigned DEFAULT_FONT_SIZE = 256;
const unsigned SUBPIXEL_BITS = 6;
int
+496 -81
View File
@@ -25,51 +25,25 @@
#ifndef HB_VECTOR_OUTPUT_HH
#define HB_VECTOR_OUTPUT_HH
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
#include "output-options.hh"
#include "view-options.hh"
#include "hb-vector.h"
#include "hb-ot.h"
struct vector_output_t : output_options_t<>
struct vector_output_t : output_options_t<>, view_options_t
{
static const bool repeat_shape = false;
struct margin_t {
float t, r, b, l;
};
static gboolean
parse_margin (const char *name G_GNUC_UNUSED,
const char *arg,
gpointer data,
GError **error)
{
vector_output_t *opts = (vector_output_t *) data;
margin_t &m = opts->margin;
double t, r, b, l;
if (parse_1to4_doubles (arg, &t, &r, &b, &l))
{
m.t = (float) t;
m.r = (float) r;
m.b = (float) b;
m.l = (float) l;
return true;
}
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"%s argument should be one to four space-separated numbers",
name);
return false;
}
~vector_output_t ()
{
g_free (background_str);
hb_font_destroy (font);
hb_font_destroy (upem_font);
g_free (foreground_str);
}
void add_options (option_parser_t *parser)
@@ -78,17 +52,12 @@ struct vector_output_t : output_options_t<>
parser->set_description ("Shows shaped glyph outlines as SVG.");
output_options_t::add_options (parser);
view_options_t::add_options (parser);
GOptionEntry entries[] =
{
{"logical", 0, 0, G_OPTION_ARG_NONE, &this->logical, "Use logical extents for bounding box (default)", nullptr},
{"ink", 0, 0, G_OPTION_ARG_NONE, &this->ink, "Use ink extents for bounding box", nullptr},
{"flat", 0, 0, G_OPTION_ARG_NONE, &this->flat, "Flatten geometry and disable reuse", nullptr},
{"precision", 0, 0, G_OPTION_ARG_INT, &this->precision, "Decimal precision (default: 2)", "N"},
{"palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Color palette index (default: 0)", "N"},
{"background", 0, 0, G_OPTION_ARG_STRING, &this->background_str,"Background color", "rrggbb[aa]"},
{"margin", 0, 0, G_OPTION_ARG_CALLBACK,(gpointer) &parse_margin, "Margin around output (default: 0)","one to four numbers"},
{"foreground", 0, 0, G_OPTION_ARG_STRING, &this->foreground_str,"Foreground color (default: 000000)", "rrggbb[aa]"},
{nullptr}
};
parser->add_group (entries,
@@ -100,6 +69,10 @@ struct vector_output_t : output_options_t<>
void post_parse (GError **error)
{
view_options_t::post_parse (error);
if (error && *error)
return;
if (precision < 0)
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
@@ -107,30 +80,36 @@ struct vector_output_t : output_options_t<>
return;
}
if (foreground_str)
foreground = HB_COLOR (0, 0, 0, 255);
if (foreground_use_palette && foreground_palette && foreground_palette->len)
{
const rgba_color_t &c = g_array_index (foreground_palette, rgba_color_t, 0);
foreground = HB_COLOR (c.b, c.g, c.r, c.a);
}
else if (fore && *fore)
{
unsigned r, g, b, a;
if (!parse_color (foreground_str, r, g, b, a))
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Invalid foreground color: %s", foreground_str);
if (!parse_color (fore, r, g, b, a))
return;
}
foreground = HB_COLOR (b, g, r, a);
}
if (background_str)
has_background = false;
if (back && *back)
{
unsigned r, g, b, a;
if (!parse_color (background_str, r, g, b, a))
if (!parse_color (back, r, g, b, a))
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Invalid background color: %s", background_str);
"Invalid background color: %s", back);
return;
}
background = HB_COLOR (b, g, r, a);
has_background = true;
}
if (!parse_custom_palette_overrides (error))
return;
}
template <typename app_t>
@@ -230,7 +209,9 @@ struct vector_output_t : output_options_t<>
hb_vector_paint_set_scale_factor (paint, 1.f, 1.f);
hb_vector_paint_set_extents (paint, &extents);
hb_vector_paint_set_foreground (paint, foreground);
hb_vector_paint_set_palette (paint, palette);
hb_vector_paint_set_palette (paint, this->palette);
apply_custom_palette (paint);
init_palette_color_cache ();
hb_vector_svg_paint_set_precision (paint, precision);
hb_vector_svg_paint_set_flat (paint, flat);
@@ -238,15 +219,18 @@ struct vector_output_t : output_options_t<>
bool had_paint = false;
hb_vector_extents_mode_t extents_mode = ink ? HB_VECTOR_EXTENTS_MODE_EXPAND
: HB_VECTOR_EXTENTS_MODE_NONE;
const bool use_foreground_palette =
foreground_use_palette && foreground_palette && foreground_palette->len;
unsigned palette_glyph_index = 0;
hb_direction_t dir = direction;
if (dir == HB_DIRECTION_INVALID)
dir = HB_DIRECTION_LTR;
bool vertical = HB_DIRECTION_IS_VERTICAL (dir);
hb_font_extents_t fext = {0, 0, 0};
hb_font_get_extents_for_direction (upem_font, dir, &fext);
float step = fabsf ((float) (fext.ascender - fext.descender + fext.line_gap));
float asc = 0.f, desc = 0.f, gap = 0.f;
get_line_metrics (dir, &asc, &desc, &gap);
float step = fabsf (asc - desc + gap + (float) line_space);
if (!(step > 0.f))
step = (float) upem;
@@ -260,6 +244,14 @@ struct vector_output_t : output_options_t<>
float pen_x = g.x + off_x;
float pen_y = g.y + off_y;
if (use_foreground_palette)
{
const rgba_color_t &c =
g_array_index (foreground_palette, rgba_color_t,
palette_glyph_index++ % foreground_palette->len);
hb_vector_paint_set_foreground (paint, HB_COLOR (c.b, c.g, c.r, c.a));
}
hb_vector_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f);
if (hb_vector_paint_glyph (paint, upem_font, g.gid, pen_x, pen_y,
extents_mode))
@@ -288,6 +280,9 @@ struct vector_output_t : output_options_t<>
else if (draw_blob && draw_blob != hb_blob_get_empty ())
write_blob (draw_blob, true);
if (show_extents)
emit_extents_overlay (dir, step);
hb_blob_destroy (draw_blob);
hb_blob_destroy (paint_blob);
hb_vector_draw_destroy (draw);
@@ -340,10 +335,9 @@ struct vector_output_t : output_options_t<>
dir = HB_DIRECTION_LTR;
bool vertical = HB_DIRECTION_IS_VERTICAL (dir);
hb_font_extents_t fext = {0, 0, 0};
hb_font_get_extents_for_direction (upem_font, dir, &fext);
float step = fabsf ((float) (fext.ascender - fext.descender + fext.line_gap));
float asc = 0.f, desc = 0.f, gap = 0.f;
get_line_metrics (dir, &asc, &desc, &gap);
float step = fabsf (asc - desc + gap + (float) line_space);
if (!(step > 0.f))
step = (float) upem;
@@ -377,8 +371,8 @@ struct vector_output_t : output_options_t<>
if (include_logical)
{
float log_min = hb_min ((float) fext.descender, (float) fext.ascender);
float log_max = hb_max ((float) fext.descender, (float) fext.ascender);
float log_min = hb_min (desc, asc);
float log_max = hb_max (desc, asc);
if (vertical)
{
include_point (off_x + log_min, hb_min (0.f, line.advance_y));
@@ -518,27 +512,308 @@ struct vector_output_t : output_options_t<>
void emit_fill_group_open ()
{
if (!foreground_str)
const bool has_fill_override = fore || foreground_use_palette;
const bool has_stroke = stroke_enabled && stroke_width > 0.;
if (!has_fill_override && !has_stroke)
return;
unsigned r = hb_color_get_red (foreground);
unsigned g = hb_color_get_green (foreground);
unsigned b = hb_color_get_blue (foreground);
unsigned a = hb_color_get_alpha (foreground);
fputs ("<g", out_fp);
if (has_fill_override)
{
unsigned r = hb_color_get_red (foreground);
unsigned g = hb_color_get_green (foreground);
unsigned b = hb_color_get_blue (foreground);
unsigned a = hb_color_get_alpha (foreground);
fprintf (out_fp, " fill=\"#%02X%02X%02X\"", r, g, b);
if (a != 255)
fprintf (out_fp, " fill-opacity=\"%.3f\"", (double) a / 255.);
}
else
fputs (" fill=\"none\"", out_fp);
if (has_stroke)
emit_stroke_attrs ();
fprintf (out_fp, "<g fill=\"#%02X%02X%02X\"", r, g, b);
if (a != 255)
fprintf (out_fp, " fill-opacity=\"%.3f\"", (double) a / 255.);
fprintf (out_fp, ">\n");
}
void emit_fill_group_close ()
{
if (!foreground_str)
if (!(fore || foreground_use_palette || (stroke_enabled && stroke_width > 0.)))
return;
fputs ("</g>\n", out_fp);
}
void emit_fill_stroke_attrs_for_color (hb_color_t color)
{
unsigned r = hb_color_get_red (color);
unsigned g = hb_color_get_green (color);
unsigned b = hb_color_get_blue (color);
unsigned a = hb_color_get_alpha (color);
fprintf (out_fp, " fill=\"#%02X%02X%02X\"", r, g, b);
if (a != 255)
fprintf (out_fp, " fill-opacity=\"%.3f\"", (double) a / 255.);
}
void emit_draw_body_with_palette (slice_t body)
{
if (!foreground_palette || !foreground_palette->len || !body.p || !body.len)
return;
const char *p = body.p;
const char *end = body.p + body.len;
unsigned palette_i = 0;
const bool has_stroke = stroke_enabled && stroke_width > 0.;
if (has_stroke)
{
fputs ("<g", out_fp);
emit_stroke_attrs ();
fputs (">\n", out_fp);
}
while (p < end)
{
const char *line_end = (const char *) memchr (p, '\n', (size_t) (end - p));
if (!line_end)
line_end = end;
const char *s = p;
while (s < line_end && (*s == ' ' || *s == '\t' || *s == '\r'))
s++;
bool is_elem = s < line_end && *s == '<' && (s + 1 < line_end && s[1] != '/');
if (is_elem)
{
const rgba_color_t &c =
g_array_index (foreground_palette, rgba_color_t,
palette_i++ % foreground_palette->len);
hb_color_t col = HB_COLOR (c.b, c.g, c.r, c.a);
fputs ("<g", out_fp);
emit_fill_stroke_attrs_for_color (col);
fputs (">", out_fp);
fwrite (p, 1, (size_t) (line_end - p), out_fp);
fputs ("</g>\n", out_fp);
}
else
{
fwrite (p, 1, (size_t) (line_end - p), out_fp);
fputc ('\n', out_fp);
}
p = line_end;
if (p < end && *p == '\n')
p++;
}
if (has_stroke)
fputs ("</g>\n", out_fp);
}
void emit_stroke_attrs ()
{
fprintf (out_fp, " stroke=\"#%02X%02X%02X\"",
stroke_color.r, stroke_color.g, stroke_color.b);
if (stroke_color.a != 255)
fprintf (out_fp, " stroke-opacity=\"%.3f\"", (double) stroke_color.a / 255.);
fprintf (out_fp, " stroke-width=\"%.*g\" stroke-linecap=\"round\" stroke-linejoin=\"round\"",
precision + 4, stroke_width);
}
bool lookup_palette_color (unsigned idx, hb_color_t *color) const
{
if (idx < custom_palette_has_value.size () && custom_palette_has_value[idx])
{
*color = custom_palette_values[idx];
return true;
}
if (!palette_lookup_enabled)
return false;
if (palette_cache_state.size () <= idx)
{
palette_cache_state.resize (idx + 1, 0);
palette_cache_values.resize (idx + 1, HB_COLOR (0, 0, 0, 255));
}
if (palette_cache_state[idx] == 1)
{
*color = palette_cache_values[idx];
return true;
}
if (palette_cache_state[idx] == 2)
return false;
hb_color_t fetched = HB_COLOR (0, 0, 0, 255);
unsigned n = 1;
if (hb_ot_color_palette_get_colors (palette_face, palette_lookup_index,
idx, &n, &fetched) && n)
{
palette_cache_values[idx] = fetched;
palette_cache_state[idx] = 1;
*color = fetched;
return true;
}
palette_cache_state[idx] = 2;
return false;
}
static inline const char *
skip_ascii_ws (const char *p, const char *end)
{
while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'))
p++;
return p;
}
static inline const char *
trim_ascii_ws_right (const char *begin, const char *end)
{
while (end > begin &&
(end[-1] == ' ' || end[-1] == '\t' || end[-1] == '\n' || end[-1] == '\r'))
end--;
return end;
}
struct parsed_var_color_expr_t
{
const char *expr_end = nullptr;
const char *fallback_begin = nullptr;
const char *fallback_end = nullptr;
unsigned idx = 0;
bool parse_ok = false;
bool have_digits = false;
bool has_fallback = false;
};
static inline const char *
find_next_var_expr (const char *p, const char *end)
{
for (const char *q = p; q + 4 <= end; q++)
if (memcmp (q, "var(", 4) == 0)
return q;
return nullptr;
}
static inline const char *
find_matching_paren (const char *inside, const char *end)
{
unsigned depth = 1;
for (const char *q = inside; q < end; q++)
{
if (*q == '(') depth++;
else if (*q == ')')
{
depth--;
if (!depth)
return q;
}
}
return nullptr;
}
static inline parsed_var_color_expr_t
parse_var_color_expr (const char *match, const char *end)
{
parsed_var_color_expr_t parsed;
const char *expr_close = find_matching_paren (match + 4, end);
if (!expr_close)
return parsed;
parsed.expr_end = expr_close + 1;
const char *inside = match + 4;
const char *inside_end = expr_close;
const char *q = skip_ascii_ws (inside, inside_end);
parsed.parse_ok = true;
if (!(q + 7 <= inside_end && memcmp (q, "--color", 7) == 0))
parsed.parse_ok = false;
if (parsed.parse_ok)
q += 7;
while (parsed.parse_ok && q < inside_end && *q >= '0' && *q <= '9')
{
parsed.have_digits = true;
parsed.idx = parsed.idx * 10 + (unsigned) (*q - '0');
q++;
}
q = skip_ascii_ws (q, inside_end);
if (parsed.parse_ok && q < inside_end && *q == ',')
{
parsed.has_fallback = true;
q++;
parsed.fallback_begin = skip_ascii_ws (q, inside_end);
parsed.fallback_end = trim_ascii_ws_right (parsed.fallback_begin, inside_end);
q = inside_end;
}
if (parsed.parse_ok && q != inside_end)
parsed.parse_ok = false;
return parsed;
}
void emit_css_color (hb_color_t color)
{
unsigned r = hb_color_get_red (color);
unsigned g = hb_color_get_green (color);
unsigned b = hb_color_get_blue (color);
unsigned a = hb_color_get_alpha (color);
if (a == 255)
fprintf (out_fp, "#%02X%02X%02X", r, g, b);
else
fprintf (out_fp, "rgba(%u,%u,%u,%.6g)", r, g, b, (double) a / 255.);
}
void write_slice_resolving_palette_vars (slice_t s)
{
if (!s.p || !s.len)
return;
const char *p = s.p;
const char *end = s.p + s.len;
while (p < end)
{
const char *match = find_next_var_expr (p, end);
if (!match)
{
fwrite (p, 1, (size_t) (end - p), out_fp);
return;
}
if (match > p)
fwrite (p, 1, (size_t) (match - p), out_fp);
parsed_var_color_expr_t parsed = parse_var_color_expr (match, end);
if (!parsed.expr_end)
{
fwrite (match, 1, (size_t) (end - match), out_fp);
return;
}
hb_color_t color;
if (parsed.parse_ok && parsed.have_digits && lookup_palette_color (parsed.idx, &color))
{
emit_css_color (color);
}
else if (parsed.has_fallback && parsed.fallback_begin &&
parsed.fallback_end >= parsed.fallback_begin)
{
fwrite (parsed.fallback_begin, 1,
(size_t) (parsed.fallback_end - parsed.fallback_begin), out_fp);
}
else
{
fwrite (match, 1, (size_t) (parsed.expr_end - match), out_fp);
}
p = parsed.expr_end;
}
}
void write_blob (hb_blob_t *blob,
hb_bool_t is_draw_blob)
{
@@ -561,19 +836,21 @@ struct vector_output_t : output_options_t<>
if (defs.len)
{
fputs ("<defs>\n", out_fp);
fwrite (defs.p, 1, defs.len, out_fp);
write_slice_resolving_palette_vars (defs);
fputs ("</defs>\n", out_fp);
}
emit_background_rect ();
if (is_draw_blob)
if (is_draw_blob && foreground_use_palette && foreground_palette && foreground_palette->len)
emit_draw_body_with_palette (body);
else if (is_draw_blob)
emit_fill_group_open ();
if (body.len)
fwrite (body.p, 1, body.len, out_fp);
if (body.len && !(is_draw_blob && foreground_use_palette && foreground_palette && foreground_palette->len))
write_slice_resolving_palette_vars (body);
if (is_draw_blob)
if (is_draw_blob && !(foreground_use_palette && foreground_palette && foreground_palette->len))
emit_fill_group_close ();
fputs ("</svg>\n", out_fp);
@@ -605,20 +882,25 @@ struct vector_output_t : output_options_t<>
{
fputs ("<defs>\n", out_fp);
if (draw_defs.len)
fwrite (draw_defs.p, 1, draw_defs.len, out_fp);
write_slice_resolving_palette_vars (draw_defs);
if (paint_defs.len)
fwrite (paint_defs.p, 1, paint_defs.len, out_fp);
write_slice_resolving_palette_vars (paint_defs);
fputs ("</defs>\n", out_fp);
}
emit_background_rect ();
emit_fill_group_open ();
if (draw_body.len)
fwrite (draw_body.p, 1, draw_body.len, out_fp);
emit_fill_group_close ();
if (foreground_use_palette && foreground_palette && foreground_palette->len)
emit_draw_body_with_palette (draw_body);
else
{
emit_fill_group_open ();
if (draw_body.len)
fwrite (draw_body.p, 1, draw_body.len, out_fp);
emit_fill_group_close ();
}
if (paint_body.len)
fwrite (paint_body.p, 1, paint_body.len, out_fp);
write_slice_resolving_palette_vars (paint_body);
fputs ("</svg>\n", out_fp);
}
@@ -642,14 +924,140 @@ struct vector_output_t : output_options_t<>
fputs ("/>\n", out_fp);
}
void apply_custom_palette (hb_vector_paint_t *paint)
{
hb_vector_paint_clear_custom_palette_colors (paint);
for (unsigned idx = 0; idx < custom_palette_values.size (); idx++)
if (idx < custom_palette_has_value.size () && custom_palette_has_value[idx])
hb_vector_paint_set_custom_palette_color (paint, idx, custom_palette_values[idx]);
}
void init_palette_color_cache ()
{
palette_cache_values.clear ();
palette_cache_state.clear ();
palette_face = nullptr;
palette_lookup_index = 0;
palette_lookup_enabled = false;
hb_face_t *face = hb_font_get_face (upem_font ? upem_font : font);
if (!face)
return;
unsigned palette_count = hb_ot_color_palette_get_count (face);
if (!palette_count)
return;
unsigned palette_index = palette >= 0 ? (unsigned) palette : 0u;
if (palette_index >= palette_count)
palette_index = 0;
palette_face = face;
palette_lookup_index = palette_index;
palette_lookup_enabled = true;
}
bool parse_custom_palette_overrides (GError **error)
{
custom_palette_values.clear ();
custom_palette_has_value.clear ();
if (!custom_palette || !*custom_palette)
return true;
char **entries = g_strsplit (custom_palette, ",", -1);
for (unsigned idx = 0; entries && entries[idx]; idx++)
{
char *entry = g_strstrip (entries[idx]);
if (!*entry)
continue;
unsigned r = 0, g = 0, b = 0, a = 255;
if (!parse_color (entry, r, g, b, a))
{
g_strfreev (entries);
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Invalid --custom-palette entry; expected rrggbb or rrggbbaa");
return false;
}
if (custom_palette_values.size () <= idx)
{
custom_palette_values.resize (idx + 1, HB_COLOR (0, 0, 0, 255));
custom_palette_has_value.resize (idx + 1, false);
}
custom_palette_values[idx] = HB_COLOR (b, g, r, a);
custom_palette_has_value[idx] = true;
}
g_strfreev (entries);
return true;
}
void emit_extents_overlay (hb_direction_t dir, float step)
{
const bool vertical = HB_DIRECTION_IS_VERTICAL (dir);
const float dot_r = hb_max (1.f, step * 0.01f);
fputs ("<g fill=\"#FF000080\" stroke=\"none\">\n", out_fp);
for (unsigned li = 0; li < lines.size (); li++)
{
float off_x = vertical ? -(step * (float) li) : 0.f;
float off_y = vertical ? 0.f : -(step * (float) li);
const line_t &line = lines[li];
for (const auto &g : line.glyphs)
{
fprintf (out_fp, "<circle cx=\"%.*g\" cy=\"%.*g\" r=\"%.*g\"/>\n",
precision + 4, (double) (g.x + off_x),
precision + 4, (double) (-(g.y + off_y)),
precision + 4, (double) dot_r);
}
}
fputs ("</g>\n", out_fp);
fputs ("<g fill=\"none\" stroke=\"#FF00FF80\" stroke-width=\"1\">\n", out_fp);
for (unsigned li = 0; li < lines.size (); li++)
{
float off_x = vertical ? -(step * (float) li) : 0.f;
float off_y = vertical ? 0.f : -(step * (float) li);
const line_t &line = lines[li];
for (const auto &g : line.glyphs)
{
hb_glyph_extents_t ge;
if (!hb_font_get_glyph_extents (upem_font, g.gid, &ge))
continue;
float x = g.x + off_x + ge.x_bearing;
float y = g.y + off_y + ge.y_bearing;
float w = ge.width;
float h = ge.height;
if (!w || !h)
continue;
fprintf (out_fp, "<rect x=\"%.*g\" y=\"%.*g\" width=\"%.*g\" height=\"%.*g\"/>\n",
precision + 4, (double) x,
precision + 4, (double) (-y),
precision + 4, (double) w,
precision + 4, (double) (-h));
}
}
fputs ("</g>\n", out_fp);
}
void get_line_metrics (hb_direction_t dir, float *asc, float *desc, float *gap) const
{
if (have_font_extents)
{
if (asc) *asc = (float) font_extents.ascent;
if (desc) *desc = (float) (-font_extents.descent);
if (gap) *gap = (float) font_extents.line_gap;
return;
}
hb_font_extents_t fext = {0, 0, 0};
hb_font_get_extents_for_direction (upem_font, dir, &fext);
if (asc) *asc = (float) fext.ascender;
if (desc) *desc = (float) fext.descender;
if (gap) *gap = (float) fext.line_gap;
}
hb_bool_t flat = false;
hb_bool_t logical = false;
hb_bool_t ink = false;
int precision = 2;
int palette = 0;
margin_t margin = {0.f, 0.f, 0.f, 0.f};
char *background_str = nullptr;
char *foreground_str = nullptr;
hb_color_t background = HB_COLOR (255, 255, 255, 255);
hb_color_t foreground = HB_COLOR (0, 0, 0, 255);
hb_bool_t has_background = false;
@@ -664,6 +1072,13 @@ struct vector_output_t : output_options_t<>
unsigned upem = 0;
unsigned subpixel_bits = 0;
hb_vector_extents_t final_extents = {0, 0, 1, 1};
std::vector<hb_color_t> custom_palette_values;
std::vector<bool> custom_palette_has_value;
mutable std::vector<hb_color_t> palette_cache_values;
mutable std::vector<unsigned char> palette_cache_state;
hb_face_t *palette_face = nullptr;
unsigned palette_lookup_index = 0;
bool palette_lookup_enabled = false;
};
#endif