Line data Source code
1 : /* File: draw_line_breaker.inl; Copyright and License: see below */
2 :
3 : #include "utf8stringbuf/utf8codepointiterator.h"
4 : #include <assert.h>
5 :
6 3 : static inline void draw_line_breaker_init( draw_line_breaker_t *this_ )
7 : {
8 3 : }
9 :
10 3 : static inline void draw_line_breaker_destroy( draw_line_breaker_t *this_ )
11 : {
12 3 : }
13 :
14 256 : static inline u8_error_t draw_line_breaker_append( const draw_line_breaker_t *this_,
15 : const utf8stringview_t *in_text,
16 : utf8stream_writer_t *out_text )
17 : {
18 256 : u8_error_t err = U8_ERROR_NONE;
19 : utf8codepointiterator_t iter;
20 256 : utf8codepointiterator_init( &iter, in_text );
21 :
22 256 : const utf8codepoint_t space0width = utf8codepoint( 0x200B );
23 256 : uint32_t last = ' ';
24 4096 : while( utf8codepointiterator_has_next( &iter ) )
25 : {
26 3840 : utf8codepoint_t codepnt = utf8codepointiterator_next( &iter );
27 3840 : uint32_t current = utf8codepoint_get_char ( &codepnt );
28 3840 : const bool no_space = ( last != ' ' )&&( current != ' ' );
29 3840 : if ( no_space )
30 : {
31 3072 : const bool last_AZ = ( last >= 'A' )&&( last <= 'Z' );
32 3072 : const bool last_az = ( last >= 'a' )&&( last <= 'z' );
33 3072 : const bool last_09 = ( last >= '0' )&&( last <= '9' );
34 3072 : const bool last_other = ( last > '\x7f' );
35 3072 : const bool last_stick_right
36 3072 : = ( last == '(' )||( last == '[' )||( last == '{' )||( last == '\x60' );
37 3072 : const bool cur_AZ = ( current >= 'A' )&&( current <= 'Z' );
38 3072 : const bool cur_az = ( current >= 'a' )&&( current <= 'z' );
39 3072 : const bool cur_09 = ( current >= '0' )&&( current <= '9' );
40 3072 : const bool cur_other = ( current > '\x7f' );
41 3072 : const bool cur_stick_left
42 3072 : = ( last == ')' )||( last == ']' )||( last == '}' )||( last == ',' )||( last == ';' );
43 :
44 3072 : const bool same_token
45 0 : = ( last_AZ && ( cur_AZ || cur_az || cur_09 ) )
46 3072 : || ( last_az && ( cur_az || cur_09 ) )
47 0 : || ( last_09 && cur_09 )
48 6144 : || ( last_other && cur_other );
49 3072 : const bool no_break
50 0 : = ( ( last == '.' ) && cur_09 ) /* do not split numbers with dots */
51 3072 : || ( last_09 && ( current == '.' ) ) /* do not split numbers with dots */
52 3072 : || ( ( last == '-' ) && cur_09 ) /* do not split negative numbers */
53 3072 : || ( ( last == '+' ) && cur_09 ) /* do not split positive numbers */
54 8960 : || ( ( last == current ) /* do not split pairs, e.g. ++, --, ==, &&, ...*/
55 2816 : || ( ( last == '!' ) && ( current == '=' ) )
56 2816 : || last_stick_right
57 2816 : || cur_stick_left ); /* even if not the same token, here no break */
58 :
59 3072 : if (( ! same_token )&&( ! no_break ))
60 : {
61 0 : err |= utf8stream_writer_write_char( out_text, utf8codepoint_get_char( &space0width ) );
62 : }
63 : }
64 3840 : err |= utf8stream_writer_write_char( out_text, utf8codepoint_get_char( &codepnt ) );
65 3840 : last = current;
66 : }
67 :
68 256 : utf8codepointiterator_destroy( &iter );
69 256 : return err;
70 : }
71 :
72 :
73 : /*
74 : Copyright 2024-2025 Andreas Warnke
75 :
76 : Licensed under the Apache License, Version 2.0 (the "License");
77 : you may not use this file except in compliance with the License.
78 : You may obtain a copy of the License at
79 :
80 : http://www.apache.org/licenses/LICENSE-2.0
81 :
82 : Unless required by applicable law or agreed to in writing, software
83 : distributed under the License is distributed on an "AS IS" BASIS,
84 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
85 : See the License for the specific language governing permissions and
86 : limitations under the License.
87 : */
|