-
Notifications
You must be signed in to change notification settings - Fork 62
/
raqm.c
2801 lines (2430 loc) · 67.4 KB
/
raqm.c
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
/*
* Copyright © 2015 Information Technology Authority (ITA) <foss@ita.gov.om>
* Copyright © 2016-2023 Khaled Hosny <khaled@aliftype.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <string.h>
#ifdef RAQM_SHEENBIDI
#include <SheenBidi.h>
#else
#include <fribidi.h>
#endif
#include <hb.h>
#include <hb-ft.h>
#include "raqm.h"
/**
* SECTION:raqm
* @title: Raqm
* @short_description: A library for complex text layout
* @include: raqm.h
*
* Raqm is a light weight text layout library with strong emphasis on
* supporting languages and writing systems that require complex text layout.
*
* The main object in Raqm API is #raqm_t, it stores all the states of the
* input text, its properties, and the output of the layout process.
*
* To start, you create a #raqm_t object, add text and font(s) to it, run the
* layout process, and finally query about the output. For example:
*
* |[<!-- language="C" -->
* #include "raqm.h"
*
* int
* main (int argc, char *argv[])
* {
* const char *fontfile;
* const char *text;
* const char *direction;
* const char *language;
* int ret = 1;
*
* FT_Library library = NULL;
* FT_Face face = NULL;
*
* if (argc < 5)
* {
* printf ("Usage: %s FONT_FILE TEXT DIRECTION LANG\n", argv[0]);
* return 1;
* }
*
* fontfile = argv[1];
* text = argv[2];
* direction = argv[3];
* language = argv[4];
*
* if (FT_Init_FreeType (&library) == 0)
* {
* if (FT_New_Face (library, fontfile, 0, &face) == 0)
* {
* if (FT_Set_Char_Size (face, face->units_per_EM, 0, 0, 0) == 0)
* {
* raqm_t *rq = raqm_create ();
* if (rq != NULL)
* {
* raqm_direction_t dir = RAQM_DIRECTION_DEFAULT;
*
* if (strcmp (direction, "r") == 0)
* dir = RAQM_DIRECTION_RTL;
* else if (strcmp (direction, "l") == 0)
* dir = RAQM_DIRECTION_LTR;
*
* if (raqm_set_text_utf8 (rq, text, strlen (text)) &&
* raqm_set_freetype_face (rq, face) &&
* raqm_set_par_direction (rq, dir) &&
* raqm_set_language (rq, language, 0, strlen (text)) &&
* raqm_layout (rq))
* {
* size_t count, i;
* raqm_glyph_t *glyphs = raqm_get_glyphs (rq, &count);
*
* ret = !(glyphs != NULL || count == 0);
*
* printf("glyph count: %zu\n", count);
* for (i = 0; i < count; i++)
* {
* printf ("gid#%d off: (%d, %d) adv: (%d, %d) idx: %d\n",
* glyphs[i].index,
* glyphs[i].x_offset,
* glyphs[i].y_offset,
* glyphs[i].x_advance,
* glyphs[i].y_advance,
* glyphs[i].cluster);
* }
* }
*
* raqm_destroy (rq);
* }
* }
*
* FT_Done_Face (face);
* }
*
* FT_Done_FreeType (library);
* }
*
* return ret;
* }
* ]|
* To compile this example:
* |[<prompt>
* cc -o test test.c `pkg-config --libs --cflags raqm`
* ]|
*/
/* For enabling debug mode */
/*#define RAQM_DEBUG 1*/
#ifdef RAQM_DEBUG
#define RAQM_DBG(...) fprintf (stderr, __VA_ARGS__)
#else
#define RAQM_DBG(...)
#endif
#ifdef RAQM_TESTING
# define RAQM_TEST(...) printf (__VA_ARGS__)
# define SCRIPT_TO_STRING(script) \
char buff[5]; \
hb_tag_to_string (hb_script_to_iso15924_tag (script), buff); \
buff[4] = '\0';
#else
# define RAQM_TEST(...)
#endif
#define RAQM_BIDI_LEVEL_IS_RTL(level) \
((level) & 1)
#ifdef RAQM_SHEENBIDI
typedef SBLevel _raqm_bidi_level_t;
#else
typedef FriBidiLevel _raqm_bidi_level_t;
#endif
typedef struct
{
FT_Face ftface;
int ftloadflags;
hb_language_t lang;
hb_script_t script;
int spacing_after;
} _raqm_text_info;
typedef struct _raqm_run raqm_run_t;
struct _raqm
{
int ref_count;
uint32_t *text;
uint16_t *text_utf16;
char *text_utf8;
size_t text_len;
size_t text_capacity_bytes;
_raqm_text_info *text_info;
raqm_direction_t base_dir;
raqm_direction_t resolved_dir;
hb_feature_t *features;
size_t features_len;
raqm_run_t *runs;
raqm_run_t *runs_pool;
raqm_glyph_t *glyphs;
size_t glyphs_capacity;
int invisible_glyph;
};
struct _raqm_run
{
uint32_t pos;
uint32_t len;
hb_direction_t direction;
hb_script_t script;
hb_font_t *font;
hb_buffer_t *buffer;
raqm_run_t *next;
};
static size_t
_raqm_encoding_to_u32_index (raqm_t *rq,
size_t index);
static bool
_raqm_allowed_grapheme_boundary (hb_codepoint_t l_char,
hb_codepoint_t r_char);
static void
_raqm_init_text_info (raqm_t *rq)
{
hb_language_t default_lang = hb_language_get_default ();
for (size_t i = 0; i < rq->text_len; i++)
{
rq->text_info[i].ftface = NULL;
rq->text_info[i].ftloadflags = -1;
rq->text_info[i].lang = default_lang;
rq->text_info[i].script = HB_SCRIPT_INVALID;
rq->text_info[i].spacing_after = 0;
}
}
static void
_raqm_release_text_info (raqm_t *rq)
{
if (!rq->text_info)
return;
for (size_t i = 0; i < rq->text_len; i++)
{
if (rq->text_info[i].ftface)
FT_Done_Face (rq->text_info[i].ftface);
}
}
static bool
_raqm_compare_text_info (_raqm_text_info a,
_raqm_text_info b)
{
if (a.ftface != b.ftface)
return false;
if (a.ftloadflags != b.ftloadflags)
return false;
if (a.lang != b.lang)
return false;
if (a.script != b.script)
return false;
/* Spacing shouldn't break runs, so we don't compare them here. */
return true;
}
static void
_raqm_free_text(raqm_t* rq)
{
free (rq->text);
rq->text = NULL;
rq->text_info = NULL;
rq->text_utf8 = NULL;
rq->text_utf16 = NULL;
rq->text_len = 0;
rq->text_capacity_bytes = 0;
}
static bool
_raqm_alloc_text(raqm_t *rq,
size_t len,
bool need_utf8,
bool need_utf16)
{
/* Allocate contiguous memory block for texts and text_info */
size_t mem_size = (sizeof (uint32_t) + sizeof (_raqm_text_info)) * len;
if (need_utf8)
mem_size += sizeof (char) * len;
else if (need_utf16)
mem_size += sizeof (uint16_t) * len;
if (mem_size > rq->text_capacity_bytes)
{
void* new_mem = realloc (rq->text, mem_size);
if (!new_mem)
{
_raqm_free_text (rq);
return false;
}
rq->text_capacity_bytes = mem_size;
rq->text = new_mem;
}
rq->text_info = (_raqm_text_info*)(rq->text + len);
rq->text_utf8 = need_utf8 ? (char*)(rq->text_info + len) : NULL;
rq->text_utf16 = need_utf16 ? (uint16_t*)(rq->text_info + len) : NULL;
return true;
}
static raqm_run_t*
_raqm_alloc_run (raqm_t *rq)
{
raqm_run_t *run = rq->runs_pool;
if (run)
{
rq->runs_pool = run->next;
}
else
{
run = malloc (sizeof (raqm_run_t));
run->font = NULL;
run->buffer = NULL;
}
run->pos = 0;
run->len = 0;
run->direction = HB_DIRECTION_INVALID;
run->script = HB_SCRIPT_INVALID;
run->next = NULL;
return run;
}
static void
_raqm_free_runs (raqm_run_t *runs)
{
while (runs)
{
raqm_run_t *run = runs;
runs = runs->next;
if (run->buffer)
hb_buffer_destroy (run->buffer);
if (run->font)
hb_font_destroy (run->font);
free (run);
}
}
/**
* raqm_create:
*
* Creates a new #raqm_t with all its internal states initialized to their
* defaults.
*
* Return value:
* A newly allocated #raqm_t with a reference count of 1. The initial reference
* count should be released with raqm_destroy() when you are done using the
* #raqm_t. Returns `NULL` in case of error.
*
* Since: 0.1
*/
raqm_t *
raqm_create (void)
{
raqm_t *rq;
rq = malloc (sizeof (raqm_t));
if (!rq)
return NULL;
rq->ref_count = 1;
rq->base_dir = RAQM_DIRECTION_DEFAULT;
rq->resolved_dir = RAQM_DIRECTION_DEFAULT;
rq->features = NULL;
rq->features_len = 0;
rq->invisible_glyph = 0;
rq->text = NULL;
rq->text_utf16 = NULL;
rq->text_utf8 = NULL;
rq->text_info = NULL;
rq->text_capacity_bytes = 0;
rq->text_len = 0;
rq->runs = NULL;
rq->runs_pool = NULL;
rq->glyphs = NULL;
rq->glyphs_capacity = 0;
return rq;
}
/**
* raqm_reference:
* @rq: a #raqm_t.
*
* Increases the reference count on @rq by one. This prevents @rq from being
* destroyed until a matching call to raqm_destroy() is made.
*
* Return value:
* The referenced #raqm_t.
*
* Since: 0.1
*/
raqm_t *
raqm_reference (raqm_t *rq)
{
if (rq)
rq->ref_count++;
return rq;
}
/**
* raqm_destroy:
* @rq: a #raqm_t.
*
* Decreases the reference count on @rq by one. If the result is zero, then @rq
* and all associated resources are freed.
* See raqm_reference().
*
* Since: 0.1
*/
void
raqm_destroy (raqm_t *rq)
{
if (!rq || --rq->ref_count != 0)
return;
_raqm_release_text_info (rq);
_raqm_free_text (rq);
_raqm_free_runs (rq->runs);
_raqm_free_runs (rq->runs_pool);
free (rq->glyphs);
free (rq->features);
free (rq);
}
/**
* raqm_clear_contents:
* @rq: a #raqm_t.
*
* Clears internal state of previously used raqm_t object, making it ready
* for reuse and keeping some of allocated memory to increase performance.
*
* Since: 0.9
*/
void
raqm_clear_contents (raqm_t *rq)
{
if (!rq)
return;
_raqm_release_text_info (rq);
/* Return allocated runs to the pool, keep hb buffers for reuse */
raqm_run_t *run = rq->runs;
while (run)
{
if (run->buffer)
hb_buffer_reset (run->buffer);
if (run->font)
{
hb_font_destroy (run->font);
run->font = NULL;
}
if (!run->next)
{
run->next = rq->runs_pool;
rq->runs_pool = rq->runs;
rq->runs = NULL;
break;
}
run = run->next;
}
rq->text_len = 0;
rq->resolved_dir = RAQM_DIRECTION_DEFAULT;
}
/**
* raqm_set_text:
* @rq: a #raqm_t.
* @text: a UTF-32 encoded text string.
* @len: the length of @text.
*
* Adds @text to @rq to be used for layout. It must be a valid UTF-32 text, any
* invalid character will be replaced with U+FFFD. The text should typically
* represent a full paragraph, since doing the layout of chunks of text
* separately can give improper output.
*
* Return value:
* `true` if no errors happened, `false` otherwise.
*
* Since: 0.1
*/
bool
raqm_set_text (raqm_t *rq,
const uint32_t *text,
size_t len)
{
if (!rq || !text)
return false;
/* Call raqm_clear_contents to reuse this raqm_t */
if (rq->text_len)
return false;
/* Empty string, don’t fail but do nothing */
if (!len)
return true;
if (!_raqm_alloc_text(rq, len, false, false))
return false;
rq->text_len = len;
memcpy (rq->text, text, sizeof (uint32_t) * len);
_raqm_init_text_info (rq);
return true;
}
static void *
_raqm_get_utf8_codepoint (const void *str,
uint32_t *out_codepoint)
{
const char *s = (const char *)str;
if (0xf0 == (0xf8 & s[0]))
{
*out_codepoint = ((0x07 & s[0]) << 18) | ((0x3f & s[1]) << 12) | ((0x3f & s[2]) << 6) | (0x3f & s[3]);
s += 4;
}
else if (0xe0 == (0xf0 & s[0]))
{
*out_codepoint = ((0x0f & s[0]) << 12) | ((0x3f & s[1]) << 6) | (0x3f & s[2]);
s += 3;
}
else if (0xc0 == (0xe0 & s[0]))
{
*out_codepoint = ((0x1f & s[0]) << 6) | (0x3f & s[1]);
s += 2;
}
else
{
*out_codepoint = s[0];
s += 1;
}
return (void *)s;
}
static size_t
_raqm_u8_to_u32 (const char *text, size_t len, uint32_t *unicode)
{
size_t in_len = 0;
uint32_t *out_utf32 = unicode;
const char *in_utf8 = text;
while ((*in_utf8 != '\0') && (in_len < len))
{
in_utf8 = _raqm_get_utf8_codepoint (in_utf8, out_utf32);
++out_utf32;
++in_len;
}
return (out_utf32 - unicode);
}
static void *
_raqm_get_utf16_codepoint (const void *str,
uint32_t *out_codepoint)
{
const uint16_t *s = (const uint16_t *)str;
if (s[0] >= 0xD800 && s[0] <= 0xDBFF)
{
if (s[1] >= 0xDC00 && s[1] <= 0xDFFF)
{
uint32_t X = ((s[0] & ((1 << 6) -1)) << 10) | (s[1] & ((1 << 10) -1));
uint32_t W = (s[0] >> 6) & ((1 << 5) - 1);
*out_codepoint = (W+1) << 16 | X;
s += 2;
}
else
{
/* A single high surrogate, this is an error. */
*out_codepoint = s[0];
s += 1;
}
}
else
{
*out_codepoint = s[0];
s += 1;
}
return (void *)s;
}
static size_t
_raqm_u16_to_u32 (const uint16_t *text, size_t len, uint32_t *unicode)
{
size_t in_len = 0;
uint32_t *out_utf32 = unicode;
const uint16_t *in_utf16 = text;
while ((*in_utf16 != '\0') && (in_len < len))
{
in_utf16 = _raqm_get_utf16_codepoint (in_utf16, out_utf32);
++out_utf32;
++in_len;
}
return (out_utf32 - unicode);
}
/**
* raqm_set_text_utf8:
* @rq: a #raqm_t.
* @text: a UTF-8 encoded text string.
* @len: the length of @text in UTF-8 bytes.
*
* Same as raqm_set_text(), but for text encoded in UTF-8 encoding.
*
* Return value:
* `true` if no errors happened, `false` otherwise.
*
* Since: 0.1
*/
bool
raqm_set_text_utf8 (raqm_t *rq,
const char *text,
size_t len)
{
if (!rq || !text)
return false;
/* Call raqm_clear_contents to reuse this raqm_t */
if (rq->text_len)
return false;
/* Empty string, don’t fail but do nothing */
if (!len)
return true;
if (!_raqm_alloc_text(rq, len, true, false))
return false;
rq->text_len = _raqm_u8_to_u32 (text, len, rq->text);
memcpy (rq->text_utf8, text, sizeof (char) * len);
_raqm_init_text_info (rq);
return true;
}
/**
* raqm_set_text_utf16:
* @rq: a #raqm_t.
* @text: a UTF-16 encoded text string.
* @len: the length of @text in UTF-16 shorts.
*
* Same as raqm_set_text(), but for text encoded in UTF-16 encoding.
*
* Return value:
* `true` if no errors happened, `false` otherwise.
*
* Since: 0.10
*/
bool
raqm_set_text_utf16 (raqm_t *rq,
const uint16_t *text,
size_t len)
{
if (!rq || !text)
return false;
/* Call raqm_clear_contents to reuse this raqm_t */
if (rq->text_len)
return false;
/* Empty string, don’t fail but do nothing */
if (!len)
return true;
if (!_raqm_alloc_text(rq, len, false, true))
return false;
rq->text_len = _raqm_u16_to_u32 (text, len, rq->text);
memcpy (rq->text_utf16, text, sizeof (uint16_t) * len);
_raqm_init_text_info (rq);
return true;
}
/**
* raqm_set_par_direction:
* @rq: a #raqm_t.
* @dir: the direction of the paragraph.
*
* Sets the paragraph direction, also known as block direction in CSS. For
* horizontal text, this controls the overall direction in the Unicode
* Bidirectional Algorithm, so when the text is mainly right-to-left (with or
* without some left-to-right) text, then the base direction should be set to
* #RAQM_DIRECTION_RTL and vice versa.
*
* The default is #RAQM_DIRECTION_DEFAULT, which determines the paragraph
* direction based on the first character with strong bidi type (see [rule
* P2](https://unicode.org/reports/tr9/#P2) in Unicode Bidirectional Algorithm),
* which can be good enough for many cases but has problems when a mainly
* right-to-left paragraph starts with a left-to-right character and vice versa
* as the detected paragraph direction will be the wrong one, or when text does
* not contain any characters with string bidi types (e.g. only punctuation or
* numbers) as this will default to left-to-right paragraph direction.
*
* For vertical, top-to-bottom text, #RAQM_DIRECTION_TTB should be used. Raqm,
* however, provides limited vertical text support and does not handle rotated
* horizontal text in vertical text, instead everything is treated as vertical
* text.
*
* Return value:
* `true` if no errors happened, `false` otherwise.
*
* Since: 0.1
*/
bool
raqm_set_par_direction (raqm_t *rq,
raqm_direction_t dir)
{
if (!rq)
return false;
rq->base_dir = dir;
return true;
}
/**
* raqm_set_language:
* @rq: a #raqm_t.
* @lang: a BCP47 language code.
* @start: index of first character that should use @face.
* @len: number of characters using @face.
*
* Sets a [BCP47 language
* code](https://www.w3.org/International/articles/language-tags/) to be used
* for @len-number of characters staring at @start. The @start and @len are
* input string array indices (i.e. counting bytes in UTF-8 and scaler values
* in UTF-32).
*
* This method can be used repeatedly to set different languages for different
* parts of the text.
*
* Return value:
* `true` if no errors happened, `false` otherwise.
*
* Stability:
* Unstable
*
* Since: 0.2
*/
bool
raqm_set_language (raqm_t *rq,
const char *lang,
size_t start,
size_t len)
{
hb_language_t language;
size_t end;
if (!rq)
return false;
if (!rq->text_len)
return true;
end = _raqm_encoding_to_u32_index (rq, start + len);
start = _raqm_encoding_to_u32_index (rq, start);
if (start >= rq->text_len || end > rq->text_len)
return false;
if (!rq->text_info)
return false;
language = hb_language_from_string (lang, -1);
for (size_t i = start; i < end; i++)
{
rq->text_info[i].lang = language;
}
return true;
}
static bool
_raqm_add_font_feature (raqm_t *rq,
hb_feature_t fea)
{
void* new_features;
if (!rq)
return false;
new_features = realloc (rq->features,
sizeof (hb_feature_t) * (rq->features_len + 1));
if (!new_features)
return false;
if (fea.start != HB_FEATURE_GLOBAL_START)
fea.start = _raqm_encoding_to_u32_index (rq, fea.start);
if (fea.end != HB_FEATURE_GLOBAL_END)
fea.end = _raqm_encoding_to_u32_index (rq, fea.end);
rq->features = new_features;
rq->features[rq->features_len] = fea;
rq->features_len++;
return true;
}
/**
* raqm_add_font_feature:
* @rq: a #raqm_t.
* @feature: (transfer none): a font feature string.
* @len: length of @feature, -1 for `NULL`-terminated.
*
* Adds a font feature to be used by the #raqm_t during text layout. This is
* usually used to turn on optional font features that are not enabled by
* default, for example `dlig` or `ss01`, but can be also used to turn off
* default font features.
*
* @feature is string representing a single font feature, in the syntax
* understood by hb_feature_from_string().
*
* This function can be called repeatedly, new features will be appended to the
* end of the features list and can potentially override previous features.
*
* Return value:
* `true` if parsing @feature succeeded, `false` otherwise.
*
* Since: 0.1
*/
bool
raqm_add_font_feature (raqm_t *rq,
const char *feature,
int len)
{
hb_bool_t ok;
hb_feature_t fea;
if (!rq)
return false;
ok = hb_feature_from_string (feature, len, &fea);
if (ok)
_raqm_add_font_feature (rq, fea);
return ok;
}
static hb_font_t *
_raqm_create_hb_font (raqm_t *rq,
FT_Face face,
int loadflags)
{
hb_font_t *font = hb_ft_font_create_referenced (face);
if (loadflags >= 0)
hb_ft_font_set_load_flags (font, loadflags);
return font;
}
static bool
_raqm_set_freetype_face (raqm_t *rq,
FT_Face face,
size_t start,
size_t end)
{
if (!rq)
return false;
if (!rq->text_len)
return true;
if (start >= rq->text_len || end > rq->text_len)
return false;
if (!rq->text_info)
return false;
for (size_t i = start; i < end; i++)
{
if (rq->text_info[i].ftface)
FT_Done_Face (rq->text_info[i].ftface);
rq->text_info[i].ftface = face;
FT_Reference_Face (face);
}
return true;
}
/**
* raqm_set_freetype_face:
* @rq: a #raqm_t.
* @face: an #FT_Face.
*
* Sets an #FT_Face to be used for all characters in @rq.
*
* See also raqm_set_freetype_face_range().
*
* Return value:
* `true` if no errors happened, `false` otherwise.
*
* Since: 0.1
*/
bool
raqm_set_freetype_face (raqm_t *rq,
FT_Face face)
{
return _raqm_set_freetype_face (rq, face, 0, rq->text_len);
}
/**
* raqm_set_freetype_face_range:
* @rq: a #raqm_t.
* @face: an #FT_Face.
* @start: index of first character that should use @face from the input string.
* @len: number of elements using @face.
*
* Sets an #FT_Face to be used for @len-number of characters staring at @start.
* The @start and @len are input string array indices, counting elements
* according to the underlying encoding. @start must always be aligned to the
* start of an encoded codepoint, and @len must always end at a codepoint's
* final element.
*
* This method can be used repeatedly to set different faces for different
* parts of the text. It is the responsibility of the client to make sure that
* face ranges cover the whole text, and is properly aligned.
*
* See also raqm_set_freetype_face().
*
* Return value:
* `true` if no errors happened, `false` otherwise.
*
* Since: 0.1
*/
bool
raqm_set_freetype_face_range (raqm_t *rq,
FT_Face face,
size_t start,
size_t len)
{
size_t end;
if (!rq)
return false;
if (!rq->text_len)
return true;
end = _raqm_encoding_to_u32_index (rq, start + len);
start = _raqm_encoding_to_u32_index (rq, start);
return _raqm_set_freetype_face (rq, face, start, end);
}
static bool
_raqm_set_freetype_load_flags (raqm_t *rq,
int flags,
size_t start,
size_t end)
{
if (!rq)
return false;
if (!rq->text_len)
return true;
if (start >= rq->text_len || end > rq->text_len)