1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
/*
 * This file is part of CtkSourceView
 *
 * Copyright (C) 2008, 2011, 2016 - Paolo Borelli <pborelli@gnome.org>
 * Copyright (C) 2008, 2010 - Ignacio Casal Quinteiro <icq@gnome.org>
 * Copyright (C) 2010 - Garret Regier
 * Copyright (C) 2013 - Arpad Borsos <arpad.borsos@googlemail.com>
 * Copyright (C) 2015, 2016 - Sébastien Wilmet <swilmet@gnome.org>
 * Copyright (C) 2016 - Christian Hergert <christian@hergert.me>
 *
 * CtkSourceView is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * CtkSourceView is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ctksourcespacedrawer.h"
#include "ctksourcespacedrawer-private.h"
#include "ctksourcebuffer.h"
#include "ctksourcebuffer-private.h"
#include "ctksourceiter.h"
#include "ctksourcestylescheme.h"
#include "ctksourcetag.h"
#include "ctksourceview.h"

/**
 * SECTION:spacedrawer
 * @Short_description: Represent white space characters with symbols
 * @Title: CtkSourceSpaceDrawer
 * @See_also: #CtkSourceView
 *
 * #CtkSourceSpaceDrawer provides a way to visualize white spaces, by drawing
 * symbols.
 *
 * Call ctk_source_view_get_space_drawer() to get the #CtkSourceSpaceDrawer
 * instance of a certain #CtkSourceView.
 *
 * By default, no white spaces are drawn because the
 * #CtkSourceSpaceDrawer:enable-matrix is %FALSE.
 *
 * To draw white spaces, ctk_source_space_drawer_set_types_for_locations() can
 * be called to set the #CtkSourceSpaceDrawer:matrix property (by default all
 * space types are enabled at all locations). Then call
 * ctk_source_space_drawer_set_enable_matrix().
 *
 * For a finer-grained method, there is also the CtkSourceTag's
 * #CtkSourceTag:draw-spaces property.
 *
 * # Example
 *
 * To draw non-breaking spaces everywhere and draw all types of trailing spaces
 * except newlines:
 * |[
 * ctk_source_space_drawer_set_types_for_locations (space_drawer,
 *                                                  CTK_SOURCE_SPACE_LOCATION_ALL,
 *                                                  CTK_SOURCE_SPACE_TYPE_NBSP);
 *
 * ctk_source_space_drawer_set_types_for_locations (space_drawer,
 *                                                  CTK_SOURCE_SPACE_LOCATION_TRAILING,
 *                                                  CTK_SOURCE_SPACE_TYPE_ALL &
 *                                                  ~CTK_SOURCE_SPACE_TYPE_NEWLINE);
 *
 * ctk_source_space_drawer_set_enable_matrix (space_drawer, TRUE);
 * ]|
 *
 * # Use-case: draw unwanted white spaces
 *
 * A possible use-case is to draw only unwanted white spaces. Examples:
 * - Draw all trailing spaces.
 * - If the indentation and alignment must be done with spaces, draw tabs.
 *
 * And non-breaking spaces can always be drawn, everywhere, to distinguish them
 * from normal spaces.
 */

/* A drawer specially designed for the International Space Station. It comes by
 * default with a DVD of Matrix, in case the astronauts are bored.
 */

/*
#define ENABLE_PROFILE
*/
#undef ENABLE_PROFILE

struct _CtkSourceSpaceDrawerPrivate
{
	CtkSourceSpaceTypeFlags *matrix;
	CdkRGBA *color;
	guint enable_matrix : 1;
};

enum
{
	PROP_0,
	PROP_ENABLE_MATRIX,
	PROP_MATRIX,
	N_PROPERTIES
};

static GParamSpec *properties[N_PROPERTIES];

G_DEFINE_TYPE_WITH_PRIVATE (CtkSourceSpaceDrawer, ctk_source_space_drawer, G_TYPE_OBJECT)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.

static gint
get_number_of_locations (void)
{
	gint num;
	gint flags;

	num = 0;
	flags = CTK_SOURCE_SPACE_LOCATION_ALL;

	while (flags != 0)
	{
		flags >>= 1;
		num++;
	}

	return num;
}

static GVariant *
get_default_matrix (void)
{
	GVariantBuilder builder;
	gint num_locations;
	gint i;

	g_variant_builder_init (&builder, G_VARIANT_TYPE ("au"));

	num_locations = get_number_of_locations ();

	for (i = 0; i < num_locations; i++)
	{
		GVariant *space_types;

		space_types = g_variant_new_uint32 (CTK_SOURCE_SPACE_TYPE_ALL);

		g_variant_builder_add_value (&builder, space_types);
	}

	return g_variant_builder_end (&builder);
}

static gboolean
is_zero_matrix (CtkSourceSpaceDrawer *drawer)
{
	gint num_locations;
	gint i;

	num_locations = get_number_of_locations ();

	for (i = 0; i < num_locations; i++)
	{
		if (drawer->priv->matrix[i] != 0)
		{
			return FALSE;
		}
	}

	return TRUE;
}

static void
set_zero_matrix (CtkSourceSpaceDrawer *drawer)
{
	gint num_locations;
	gint i;
	gboolean changed = FALSE;

	num_locations = get_number_of_locations ();

	for (i = 0; i < num_locations; i++)
	{
		if (drawer->priv->matrix[i] != 0)
		{
			drawer->priv->matrix[i] = 0;
			changed = TRUE;
		}
	}

	if (changed)
	{
		g_object_notify_by_pspec (G_OBJECT (drawer), properties[PROP_MATRIX]);
	}
}

/* AND */
static CtkSourceSpaceTypeFlags
get_types_at_all_locations (CtkSourceSpaceDrawer        *drawer,
			    CtkSourceSpaceLocationFlags  locations)
{
	CtkSourceSpaceTypeFlags ret = CTK_SOURCE_SPACE_TYPE_ALL;
	gint index;
	gint num_locations;
	gboolean found;

	index = 0;
	num_locations = get_number_of_locations ();
	found = FALSE;

	while (locations != 0 && index < num_locations)
	{
		if ((locations & 1) == 1)
		{
			ret &= drawer->priv->matrix[index];
			found = TRUE;
		}

		locations >>= 1;
		index++;
	}

	return found ? ret : CTK_SOURCE_SPACE_TYPE_NONE;
}

/* OR */
static CtkSourceSpaceTypeFlags
get_types_at_any_locations (CtkSourceSpaceDrawer        *drawer,
			    CtkSourceSpaceLocationFlags  locations)
{
	CtkSourceSpaceTypeFlags ret = CTK_SOURCE_SPACE_TYPE_NONE;
	gint index;
	gint num_locations;

	index = 0;
	num_locations = get_number_of_locations ();

	while (locations != 0 && index < num_locations)
	{
		if ((locations & 1) == 1)
		{
			ret |= drawer->priv->matrix[index];
		}

		locations >>= 1;
		index++;
	}

	return ret;
}

static void
ctk_source_space_drawer_get_property (GObject    *object,
				      guint       prop_id,
				      GValue     *value,
				      GParamSpec *pspec)
{
	CtkSourceSpaceDrawer *drawer = CTK_SOURCE_SPACE_DRAWER (object);

	switch (prop_id)
	{
		case PROP_ENABLE_MATRIX:
			g_value_set_boolean (value, ctk_source_space_drawer_get_enable_matrix (drawer));
			break;

		case PROP_MATRIX:
			g_value_set_variant (value, ctk_source_space_drawer_get_matrix (drawer));
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
ctk_source_space_drawer_set_property (GObject      *object,
				      guint         prop_id,
				      const GValue *value,
				      GParamSpec   *pspec)
{
	CtkSourceSpaceDrawer *drawer = CTK_SOURCE_SPACE_DRAWER (object);

	switch (prop_id)
	{
		case PROP_ENABLE_MATRIX:
			ctk_source_space_drawer_set_enable_matrix (drawer, g_value_get_boolean (value));
			break;

		case PROP_MATRIX:
			ctk_source_space_drawer_set_matrix (drawer, g_value_get_variant (value));
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
ctk_source_space_drawer_finalize (GObject *object)
{
	CtkSourceSpaceDrawer *drawer = CTK_SOURCE_SPACE_DRAWER (object);

	g_free (drawer->priv->matrix);

	if (drawer->priv->color != NULL)
	{
		cdk_rgba_free (drawer->priv->color);
	}

	G_OBJECT_CLASS (ctk_source_space_drawer_parent_class)->finalize (object);
}

static void
ctk_source_space_drawer_class_init (CtkSourceSpaceDrawerClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->get_property = ctk_source_space_drawer_get_property;
	object_class->set_property = ctk_source_space_drawer_set_property;
	object_class->finalize = ctk_source_space_drawer_finalize;

	/**
	 * CtkSourceSpaceDrawer:enable-matrix:
	 *
	 * Whether the #CtkSourceSpaceDrawer:matrix property is enabled.
	 *
	 * Since: 3.24
	 */
	properties[PROP_ENABLE_MATRIX] =
		g_param_spec_boolean ("enable-matrix",
				      "Enable Matrix",
				      "",
				      FALSE,
				      G_PARAM_READWRITE |
				      G_PARAM_CONSTRUCT |
				      G_PARAM_STATIC_STRINGS);

	/**
	 * CtkSourceSpaceDrawer:matrix:
	 *
	 * The :matrix property is a #GVariant property to specify where and
	 * what kind of white spaces to draw.
	 *
	 * The #GVariant is of type `"au"`, an array of unsigned integers. Each
	 * integer is a combination of #CtkSourceSpaceTypeFlags. There is one
	 * integer for each #CtkSourceSpaceLocationFlags, in the same order as
	 * they are defined in the enum (%CTK_SOURCE_SPACE_LOCATION_NONE and
	 * %CTK_SOURCE_SPACE_LOCATION_ALL are not taken into account).
	 *
	 * If the array is shorter than the number of locations, then the value
	 * for the missing locations will be %CTK_SOURCE_SPACE_TYPE_NONE.
	 *
	 * By default, %CTK_SOURCE_SPACE_TYPE_ALL is set for all locations.
	 *
	 * Since: 3.24
	 */
	properties[PROP_MATRIX] =
		g_param_spec_variant ("matrix",
				      "Matrix",
				      "",
				      G_VARIANT_TYPE ("au"),
				      get_default_matrix (),
				      G_PARAM_READWRITE |
				      G_PARAM_CONSTRUCT |
				      G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties (object_class, N_PROPERTIES, properties);
}

static void
ctk_source_space_drawer_init (CtkSourceSpaceDrawer *drawer)
{
	drawer->priv = ctk_source_space_drawer_get_instance_private (drawer);

	drawer->priv->matrix = g_new0 (CtkSourceSpaceTypeFlags, get_number_of_locations ());
}

/**
 * ctk_source_space_drawer_new:
 *
 * Creates a new #CtkSourceSpaceDrawer object. Useful for storing space drawing
 * settings independently of a #CtkSourceView.
 *
 * Returns: a new #CtkSourceSpaceDrawer.
 * Since: 3.24
 */
CtkSourceSpaceDrawer *
ctk_source_space_drawer_new (void)
{
	return g_object_new (CTK_SOURCE_TYPE_SPACE_DRAWER, NULL);
}

/**
 * ctk_source_space_drawer_get_types_for_locations:
 * @drawer: a #CtkSourceSpaceDrawer.
 * @locations: one or several #CtkSourceSpaceLocationFlags.
 *
 * If only one location is specified, this function returns what kind of
 * white spaces are drawn at that location. The value is retrieved from the
 * #CtkSourceSpaceDrawer:matrix property.
 *
 * If several locations are specified, this function returns the logical AND for
 * those locations. Which means that if a certain kind of white space is present
 * in the return value, then that kind of white space is drawn at all the
 * specified @locations.
 *
 * Returns: a combination of #CtkSourceSpaceTypeFlags.
 * Since: 3.24
 */
CtkSourceSpaceTypeFlags
ctk_source_space_drawer_get_types_for_locations (CtkSourceSpaceDrawer        *drawer,
						 CtkSourceSpaceLocationFlags  locations)
{
	g_return_val_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer), CTK_SOURCE_SPACE_TYPE_NONE);

	return get_types_at_all_locations (drawer, locations);
}

/**
 * ctk_source_space_drawer_set_types_for_locations:
 * @drawer: a #CtkSourceSpaceDrawer.
 * @locations: one or several #CtkSourceSpaceLocationFlags.
 * @types: a combination of #CtkSourceSpaceTypeFlags.
 *
 * Modifies the #CtkSourceSpaceDrawer:matrix property at the specified
 * @locations.
 *
 * Since: 3.24
 */
void
ctk_source_space_drawer_set_types_for_locations (CtkSourceSpaceDrawer        *drawer,
						 CtkSourceSpaceLocationFlags  locations,
						 CtkSourceSpaceTypeFlags      types)
{
	gint index;
	gint num_locations;
	gboolean changed = FALSE;

	g_return_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer));

	index = 0;
	num_locations = get_number_of_locations ();

	while (locations != 0 && index < num_locations)
	{
		if ((locations & 1) == 1 &&
		    drawer->priv->matrix[index] != types)
		{
			drawer->priv->matrix[index] = types;
			changed = TRUE;
		}

		locations >>= 1;
		index++;
	}

	if (changed)
	{
		g_object_notify_by_pspec (G_OBJECT (drawer), properties[PROP_MATRIX]);
	}
}

/**
 * ctk_source_space_drawer_get_matrix:
 * @drawer: a #CtkSourceSpaceDrawer.
 *
 * Gets the value of the #CtkSourceSpaceDrawer:matrix property, as a #GVariant.
 * An empty array can be returned in case the matrix is a zero matrix.
 *
 * The ctk_source_space_drawer_get_types_for_locations() function may be more
 * convenient to use.
 *
 * Returns: the #CtkSourceSpaceDrawer:matrix value as a new floating #GVariant
 *   instance.
 * Since: 3.24
 */
GVariant *
ctk_source_space_drawer_get_matrix (CtkSourceSpaceDrawer *drawer)
{
	GVariantBuilder builder;
	gint num_locations;
	gint i;

	g_return_val_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer), NULL);

	if (is_zero_matrix (drawer))
	{
		return g_variant_new ("au", NULL);
	}

	g_variant_builder_init (&builder, G_VARIANT_TYPE ("au"));

	num_locations = get_number_of_locations ();

	for (i = 0; i < num_locations; i++)
	{
		GVariant *space_types;

		space_types = g_variant_new_uint32 (drawer->priv->matrix[i]);

		g_variant_builder_add_value (&builder, space_types);
	}

	return g_variant_builder_end (&builder);
}

/**
 * ctk_source_space_drawer_set_matrix:
 * @drawer: a #CtkSourceSpaceDrawer.
 * @matrix: (transfer floating) (nullable): the new matrix value, or %NULL.
 *
 * Sets a new value to the #CtkSourceSpaceDrawer:matrix property, as a
 * #GVariant. If @matrix is %NULL, then an empty array is set.
 *
 * If @matrix is floating, it is consumed.
 *
 * The ctk_source_space_drawer_set_types_for_locations() function may be more
 * convenient to use.
 *
 * Since: 3.24
 */
void
ctk_source_space_drawer_set_matrix (CtkSourceSpaceDrawer *drawer,
				    GVariant             *matrix)
{
	gint num_locations;
	gint index;
	GVariantIter iter;
	gboolean changed = FALSE;

	g_return_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer));

	if (matrix == NULL)
	{
		set_zero_matrix (drawer);
		return;
	}

	g_return_if_fail (g_variant_is_of_type (matrix, G_VARIANT_TYPE ("au")));

	g_variant_iter_init (&iter, matrix);

	num_locations = get_number_of_locations ();
	index = 0;
	while (index < num_locations)
	{
		GVariant *child;
		guint32 space_types;

		child = g_variant_iter_next_value (&iter);
		if (child == NULL)
		{
			break;
		}

		space_types = g_variant_get_uint32 (child);

		if (drawer->priv->matrix[index] != space_types)
		{
			drawer->priv->matrix[index] = space_types;
			changed = TRUE;
		}

		g_variant_unref (child);
		index++;
	}

	while (index < num_locations)
	{
		if (drawer->priv->matrix[index] != 0)
		{
			drawer->priv->matrix[index] = 0;
			changed = TRUE;
		}

		index++;
	}

	if (changed)
	{
		g_object_notify_by_pspec (G_OBJECT (drawer), properties[PROP_MATRIX]);
	}

	if (g_variant_is_floating (matrix))
	{
		g_variant_ref_sink (matrix);
		g_variant_unref (matrix);
	}
}

/**
 * ctk_source_space_drawer_get_enable_matrix:
 * @drawer: a #CtkSourceSpaceDrawer.
 *
 * Returns: whether the #CtkSourceSpaceDrawer:matrix property is enabled.
 * Since: 3.24
 */
gboolean
ctk_source_space_drawer_get_enable_matrix (CtkSourceSpaceDrawer *drawer)
{
	g_return_val_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer), FALSE);

	return drawer->priv->enable_matrix;
}

/**
 * ctk_source_space_drawer_set_enable_matrix:
 * @drawer: a #CtkSourceSpaceDrawer.
 * @enable_matrix: the new value.
 *
 * Sets whether the #CtkSourceSpaceDrawer:matrix property is enabled.
 *
 * Since: 3.24
 */
void
ctk_source_space_drawer_set_enable_matrix (CtkSourceSpaceDrawer *drawer,
					   gboolean              enable_matrix)
{
	g_return_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer));

	enable_matrix = enable_matrix != FALSE;

	if (drawer->priv->enable_matrix != enable_matrix)
	{
		drawer->priv->enable_matrix = enable_matrix;
		g_object_notify_by_pspec (G_OBJECT (drawer), properties[PROP_ENABLE_MATRIX]);
	}
}

static gboolean
matrix_get_mapping (GValue   *value,
		    GVariant *variant,
		    gpointer  user_data)
{
	g_value_set_variant (value, variant);
	return TRUE;
}

static GVariant *
matrix_set_mapping (const GValue       *value,
		    const GVariantType *expected_type,
		    gpointer            user_data)
{
	return g_value_dup_variant (value);
}

/**
 * ctk_source_space_drawer_bind_matrix_setting:
 * @drawer: a #CtkSourceSpaceDrawer object.
 * @settings: a #GSettings object.
 * @key: the @settings key to bind.
 * @flags: flags for the binding.
 *
 * Binds the #CtkSourceSpaceDrawer:matrix property to a #GSettings key.
 *
 * The #GSettings key must be of the same type as the
 * #CtkSourceSpaceDrawer:matrix property, that is, `"au"`.
 *
 * The g_settings_bind() function cannot be used, because the default GIO
 * mapping functions don't support #GVariant properties (maybe it will be
 * supported by a future GIO version, in which case this function can be
 * deprecated).
 *
 * Since: 3.24
 */
void
ctk_source_space_drawer_bind_matrix_setting (CtkSourceSpaceDrawer *drawer,
					     GSettings            *settings,
					     const gchar          *key,
					     GSettingsBindFlags    flags)
{
	GVariant *value;

	g_return_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer));
	g_return_if_fail (G_IS_SETTINGS (settings));
	g_return_if_fail (key != NULL);
	g_return_if_fail ((flags & G_SETTINGS_BIND_INVERT_BOOLEAN) == 0);

	value = g_settings_get_value (settings, key);
	if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("au")))
	{
		g_warning ("%s(): the GSettings key must be of type \"au\".", G_STRFUNC);
		g_variant_unref (value);
		return;
	}
	g_variant_unref (value);

	g_settings_bind_with_mapping (settings, key,
				      drawer, "matrix",
				      flags,
				      matrix_get_mapping,
				      matrix_set_mapping,
				      NULL, NULL);
}

void
_ctk_source_space_drawer_update_color (CtkSourceSpaceDrawer *drawer,
				       CtkSourceView        *view)
{
	CtkSourceBuffer *buffer;
	CtkSourceStyleScheme *style_scheme;

	g_return_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer));
	g_return_if_fail (CTK_SOURCE_IS_VIEW (view));

	if (drawer->priv->color != NULL)
	{
		cdk_rgba_free (drawer->priv->color);
		drawer->priv->color = NULL;
	}

	buffer = CTK_SOURCE_BUFFER (ctk_text_view_get_buffer (CTK_TEXT_VIEW (view)));
	style_scheme = ctk_source_buffer_get_style_scheme (buffer);

	if (style_scheme != NULL)
	{
		CtkSourceStyle *style;

		style = _ctk_source_style_scheme_get_draw_spaces_style (style_scheme);

		if (style != NULL)
		{
			gchar *color_str = NULL;
			gboolean color_set;
			CdkRGBA color;

			g_object_get (style,
				      "foreground", &color_str,
				      "foreground-set", &color_set,
				      NULL);

			if (color_set &&
			    color_str != NULL &&
			    cdk_rgba_parse (&color, color_str))
			{
				drawer->priv->color = cdk_rgba_copy (&color);
			}

			g_free (color_str);
		}
	}

	if (drawer->priv->color == NULL)
	{
		CtkStyleContext *context;
		CdkRGBA color;

		context = ctk_widget_get_style_context (CTK_WIDGET (view));
		ctk_style_context_save (context);
		ctk_style_context_set_state (context, CTK_STATE_FLAG_INSENSITIVE);
		ctk_style_context_get_color (context,
					     ctk_style_context_get_state (context),
					     &color);
		ctk_style_context_restore (context);

		drawer->priv->color = cdk_rgba_copy (&color);
	}
}

static inline gboolean
is_tab (gunichar ch)
{
	return ch == '\t';
}

static inline gboolean
is_nbsp (gunichar ch)
{
	return g_unichar_break_type (ch) == G_UNICODE_BREAK_NON_BREAKING_GLUE;
}

static inline gboolean
is_narrowed_nbsp (gunichar ch)
{
	return ch == 0x202F;
}

static inline gboolean
is_space (gunichar ch)
{
	return g_unichar_type (ch) == G_UNICODE_SPACE_SEPARATOR;
}

static gboolean
is_newline (const CtkTextIter *iter)
{
	if (ctk_text_iter_is_end (iter))
	{
		CtkSourceBuffer *buffer;

		buffer = CTK_SOURCE_BUFFER (ctk_text_iter_get_buffer (iter));

		return ctk_source_buffer_get_implicit_trailing_newline (buffer);
	}

	return ctk_text_iter_ends_line (iter);
}

static inline gboolean
is_whitespace (gunichar ch)
{
	return (g_unichar_isspace (ch) || is_nbsp (ch) || is_space (ch));
}

static void
draw_space_at_pos (cairo_t      *cr,
		   CdkRectangle  rect)
{
	gint x, y;
	gdouble w;

	x = rect.x;
	y = rect.y + rect.height * 2 / 3;

	w = rect.width;

	cairo_save (cr);
	cairo_move_to (cr, x + w * 0.5, y);
	cairo_arc (cr, x + w * 0.5, y, 0.8, 0, 2 * G_PI);
	cairo_stroke (cr);
	cairo_restore (cr);
}

static void
draw_tab_at_pos (cairo_t      *cr,
		 CdkRectangle  rect)
{
	gint x, y;
	gdouble w, h;

	x = rect.x;
	y = rect.y + rect.height * 2 / 3;

	w = rect.width;
	h = rect.height;

	cairo_save (cr);
	cairo_move_to (cr, x + h * 1 / 6, y);
	cairo_rel_line_to (cr, w - h * 2 / 6, 0);
	cairo_rel_line_to (cr, -h * 1 / 4, -h * 1 / 4);
	cairo_rel_move_to (cr, +h * 1 / 4, +h * 1 / 4);
	cairo_rel_line_to (cr, -h * 1 / 4, +h * 1 / 4);
	cairo_stroke (cr);
	cairo_restore (cr);
}

static void
draw_newline_at_pos (cairo_t      *cr,
		     CdkRectangle  rect)
{
	gint x, y;
	gdouble w, h;

	x = rect.x;
	y = rect.y + rect.height / 3;

	w = 2 * rect.width;
	h = rect.height;

	cairo_save (cr);

	if (ctk_widget_get_default_direction () == CTK_TEXT_DIR_LTR)
	{
		cairo_move_to (cr, x + w * 7 / 8, y);
		cairo_rel_line_to (cr, 0, h * 1 / 3);
		cairo_rel_line_to (cr, -w * 6 / 8, 0);
		cairo_rel_line_to (cr, +h * 1 / 4, -h * 1 / 4);
		cairo_rel_move_to (cr, -h * 1 / 4, +h * 1 / 4);
		cairo_rel_line_to (cr, +h * 1 / 4, +h * 1 / 4);
	}
	else
	{
		cairo_move_to (cr, x + w * 1 / 8, y);
		cairo_rel_line_to (cr, 0, h * 1 / 3);
		cairo_rel_line_to (cr, w * 6 / 8, 0);
		cairo_rel_line_to (cr, -h * 1 / 4, -h * 1 / 4);
		cairo_rel_move_to (cr, +h * 1 / 4, +h * 1 / 4);
		cairo_rel_line_to (cr, -h * 1 / 4, -h * 1 / 4);
	}

	cairo_stroke (cr);
	cairo_restore (cr);
}

static void
draw_nbsp_at_pos (cairo_t      *cr,
		  CdkRectangle  rect,
		  gboolean      narrowed)
{
	gint x, y;
	gdouble w, h;

	x = rect.x;
	y = rect.y + rect.height / 2;

	w = rect.width;
	h = rect.height;

	cairo_save (cr);
	cairo_move_to (cr, x + w * 1 / 6, y);
	cairo_rel_line_to (cr, w * 4 / 6, 0);
	cairo_rel_line_to (cr, -w * 2 / 6, +h * 1 / 4);
	cairo_rel_line_to (cr, -w * 2 / 6, -h * 1 / 4);

	if (narrowed)
	{
		cairo_fill (cr);
	}
	else
	{
		cairo_stroke (cr);
	}

	cairo_restore (cr);
}

static void
draw_whitespace_at_iter (CtkTextView *text_view,
			 CtkTextIter *iter,
			 cairo_t     *cr)
{
	gunichar ch;
	CdkRectangle rect;

	ctk_text_view_get_iter_location (text_view, iter, &rect);

	/* If the space is at a line-wrap position, or if the character is a
	 * newline, we get 0 width so we fallback to the height.
	 */
	if (rect.width == 0)
	{
		rect.width = rect.height;
	}

	ch = ctk_text_iter_get_char (iter);

	if (is_tab (ch))
	{
		draw_tab_at_pos (cr, rect);
	}
	else if (is_nbsp (ch))
	{
		draw_nbsp_at_pos (cr, rect, is_narrowed_nbsp (ch));
	}
	else if (is_space (ch))
	{
		draw_space_at_pos (cr, rect);
	}
	else if (is_newline (iter))
	{
		draw_newline_at_pos (cr, rect);
	}
}

static void
space_needs_drawing_according_to_tag (const CtkTextIter *iter,
				      gboolean          *has_tag,
				      gboolean          *needs_drawing)
{
	GSList *tags;
	GSList *l;

	*has_tag = FALSE;
	*needs_drawing = FALSE;

	tags = ctk_text_iter_get_tags (iter);
	tags = g_slist_reverse (tags);

	for (l = tags; l != NULL; l = l->next)
	{
		CtkTextTag *tag = l->data;

		if (CTK_SOURCE_IS_TAG (tag))
		{
			gboolean draw_spaces_set;
			gboolean draw_spaces;

			g_object_get (tag,
				      "draw-spaces-set", &draw_spaces_set,
				      "draw-spaces", &draw_spaces,
				      NULL);

			if (draw_spaces_set)
			{
				*has_tag = TRUE;
				*needs_drawing = draw_spaces;
				break;
			}
		}
	}

	g_slist_free (tags);
}

static CtkSourceSpaceLocationFlags
get_iter_locations (const CtkTextIter *iter,
		    const CtkTextIter *leading_end,
		    const CtkTextIter *trailing_start)
{
	CtkSourceSpaceLocationFlags iter_locations = CTK_SOURCE_SPACE_LOCATION_NONE;

	if (ctk_text_iter_compare (iter, leading_end) < 0)
	{
		iter_locations |= CTK_SOURCE_SPACE_LOCATION_LEADING;
	}

	if (ctk_text_iter_compare (trailing_start, iter) <= 0)
	{
		iter_locations |= CTK_SOURCE_SPACE_LOCATION_TRAILING;
	}

	/* Neither leading nor trailing, must be in text. */
	if (iter_locations == CTK_SOURCE_SPACE_LOCATION_NONE)
	{
		iter_locations = CTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT;
	}

	return iter_locations;
}

static CtkSourceSpaceTypeFlags
get_iter_space_type (const CtkTextIter *iter)
{
	gunichar ch;

	ch = ctk_text_iter_get_char (iter);

	if (is_tab (ch))
	{
		return CTK_SOURCE_SPACE_TYPE_TAB;
	}
	else if (is_nbsp (ch))
	{
		return CTK_SOURCE_SPACE_TYPE_NBSP;
	}
	else if (is_space (ch))
	{
		return CTK_SOURCE_SPACE_TYPE_SPACE;
	}
	else if (is_newline (iter))
	{
		return CTK_SOURCE_SPACE_TYPE_NEWLINE;
	}

	return CTK_SOURCE_SPACE_TYPE_NONE;
}

static gboolean
space_needs_drawing_according_to_matrix (CtkSourceSpaceDrawer *drawer,
					 const CtkTextIter    *iter,
					 const CtkTextIter    *leading_end,
					 const CtkTextIter    *trailing_start)
{
	CtkSourceSpaceLocationFlags iter_locations;
	CtkSourceSpaceTypeFlags iter_space_type;
	CtkSourceSpaceTypeFlags allowed_space_types;

	iter_locations = get_iter_locations (iter, leading_end, trailing_start);
	iter_space_type = get_iter_space_type (iter);
	allowed_space_types = get_types_at_any_locations (drawer, iter_locations);

	return (iter_space_type & allowed_space_types) != 0;
}

static gboolean
space_needs_drawing (CtkSourceSpaceDrawer *drawer,
		     const CtkTextIter    *iter,
		     const CtkTextIter    *leading_end,
		     const CtkTextIter    *trailing_start)
{
	gboolean has_tag;
	gboolean needs_drawing;

	/* Check the CtkSourceTag:draw-spaces property (higher priority) */
	space_needs_drawing_according_to_tag (iter, &has_tag, &needs_drawing);
	if (has_tag)
	{
		return needs_drawing;
	}

	/* Check the matrix */
	return (drawer->priv->enable_matrix &&
		space_needs_drawing_according_to_matrix (drawer, iter, leading_end, trailing_start));
}

static void
get_line_end (CtkTextView       *text_view,
	      const CtkTextIter *start_iter,
	      CtkTextIter       *line_end,
	      gint               max_x,
	      gint               max_y,
	      gboolean           is_wrapping)
{
	gint min;
	gint max;
	CdkRectangle rect;

	*line_end = *start_iter;
	if (!ctk_text_iter_ends_line (line_end))
	{
		ctk_text_iter_forward_to_line_end (line_end);
	}

	/* Check if line_end is inside the bounding box anyway. */
	ctk_text_view_get_iter_location (text_view, line_end, &rect);
	if (( is_wrapping && rect.y < max_y) ||
	    (!is_wrapping && rect.x < max_x))
	{
		return;
	}

	min = ctk_text_iter_get_line_offset (start_iter);
	max = ctk_text_iter_get_line_offset (line_end);

	while (max >= min)
	{
		gint i;

		i = (min + max) >> 1;
		ctk_text_iter_set_line_offset (line_end, i);
		ctk_text_view_get_iter_location (text_view, line_end, &rect);

		if (( is_wrapping && rect.y < max_y) ||
		    (!is_wrapping && rect.x < max_x))
		{
			min = i + 1;
		}
		else if (( is_wrapping && rect.y > max_y) ||
			 (!is_wrapping && rect.x > max_x))
		{
			max = i - 1;
		}
		else
		{
			break;
		}
	}
}

void
_ctk_source_space_drawer_draw (CtkSourceSpaceDrawer *drawer,
			       CtkSourceView        *view,
			       cairo_t              *cr)
{
	CtkTextView *text_view;
	CtkTextBuffer *buffer;
	CdkRectangle clip;
	gint min_x;
	gint min_y;
	gint max_x;
	gint max_y;
	CtkTextIter start;
	CtkTextIter end;
	CtkTextIter iter;
	CtkTextIter leading_end;
	CtkTextIter trailing_start;
	CtkTextIter line_end;
	gboolean is_wrapping;

#ifdef ENABLE_PROFILE
	static GTimer *timer = NULL;
	if (timer == NULL)
	{
		timer = g_timer_new ();
	}

	g_timer_start (timer);
#endif

	g_return_if_fail (CTK_SOURCE_IS_SPACE_DRAWER (drawer));
	g_return_if_fail (CTK_SOURCE_IS_VIEW (view));
	g_return_if_fail (cr != NULL);

	if (drawer->priv->color == NULL)
	{
		g_warning ("CtkSourceSpaceDrawer: color not set.");
		return;
	}

	text_view = CTK_TEXT_VIEW (view);
	buffer = ctk_text_view_get_buffer (text_view);

	if ((!drawer->priv->enable_matrix || is_zero_matrix (drawer)) &&
	    !_ctk_source_buffer_has_spaces_tag (CTK_SOURCE_BUFFER (buffer)))
	{
		return;
	}

	if (!cdk_cairo_get_clip_rectangle (cr, &clip))
	{
		return;
	}

	is_wrapping = ctk_text_view_get_wrap_mode (text_view) != CTK_WRAP_NONE;

	min_x = clip.x;
	min_y = clip.y;
	max_x = min_x + clip.width;
	max_y = min_y + clip.height;

	ctk_text_view_get_iter_at_location (text_view, &start, min_x, min_y);
	ctk_text_view_get_iter_at_location (text_view, &end, max_x, max_y);

	cairo_save (cr);
	cdk_cairo_set_source_rgba (cr, drawer->priv->color);
	cairo_set_line_width (cr, 0.8);
	cairo_translate (cr, -0.5, -0.5);

	iter = start;
	_ctk_source_iter_get_leading_spaces_end_boundary (&iter, &leading_end);
	_ctk_source_iter_get_trailing_spaces_start_boundary (&iter, &trailing_start);
	get_line_end (text_view, &iter, &line_end, max_x, max_y, is_wrapping);

	while (TRUE)
	{
		gunichar ch = ctk_text_iter_get_char (&iter);
		gint ly;

		/* Allow end iter, to draw implicit trailing newline. */
		if ((is_whitespace (ch) || ctk_text_iter_is_end (&iter)) &&
		    space_needs_drawing (drawer, &iter, &leading_end, &trailing_start))
		{
			draw_whitespace_at_iter (text_view, &iter, cr);
		}

		if (ctk_text_iter_is_end (&iter) ||
		    ctk_text_iter_compare (&iter, &end) >= 0)
		{
			break;
		}

		ctk_text_iter_forward_char (&iter);

		if (ctk_text_iter_compare (&iter, &line_end) > 0)
		{
			CtkTextIter next_iter = iter;

			/* Move to the first iter in the exposed area of the
			 * next line.
			 */
			if (!ctk_text_iter_starts_line (&next_iter))
			{
				/* We're trying to move forward on the last
				 * line of the buffer, so we can stop now.
				 */
				if (!ctk_text_iter_forward_line (&next_iter))
				{
					break;
				}
			}

			ctk_text_view_get_line_yrange (text_view, &next_iter, &ly, NULL);
			ctk_text_view_get_iter_at_location (text_view, &next_iter, min_x, ly);

			/* Move back one char otherwise tabs may not be redrawn. */
			if (!ctk_text_iter_starts_line (&next_iter))
			{
				ctk_text_iter_backward_char (&next_iter);
			}

			/* Ensure that we have actually advanced, since the
			 * above backward_char() is dangerous and can lead to
			 * infinite loops.
			 */
			if (ctk_text_iter_compare (&next_iter, &iter) > 0)
			{
				iter = next_iter;
			}

			_ctk_source_iter_get_leading_spaces_end_boundary (&iter, &leading_end);
			_ctk_source_iter_get_trailing_spaces_start_boundary (&iter, &trailing_start);
			get_line_end (text_view, &iter, &line_end, max_x, max_y, is_wrapping);
		}
	};

	cairo_restore (cr);

#ifdef ENABLE_PROFILE
	g_timer_stop (timer);

	/* Same indentation as similar features in ctksourceview.c. */
	g_print ("    %s time: %g (sec * 1000)\n",
		 G_STRFUNC,
		 g_timer_elapsed (timer, NULL) * 1000);
#endif
}