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 512 : 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 512 : u8_error_t err = U8_ERROR_NONE;
19 : utf8codepointiterator_t iter;
20 512 : utf8codepointiterator_init( &iter, in_text );
21 :
22 512 : const utf8codepoint_t space0width = utf8codepoint( 0x200B );
23 512 : uint32_t last = ' ';
24 6912 : while( utf8codepointiterator_has_next( &iter ) )
25 : {
26 6400 : utf8codepoint_t codepnt = utf8codepointiterator_next( &iter );
27 6400 : uint32_t current = utf8codepoint_get_char ( &codepnt );
28 6400 : const bool no_space = ( last != ' ' )&&( current != ' ' );
29 6400 : if ( no_space )
30 : {
31 5376 : const bool last_AZ = ( last >= 'A' )&&( last <= 'Z' );
32 5376 : const bool last_az = ( last >= 'a' )&&( last <= 'z' );
33 5376 : const bool last_09 = ( last >= '0' )&&( last <= '9' );
34 5376 : const bool last_other = ( last > '\x7f' );
35 5376 : const bool last_stick_right
36 5376 : = ( last == '(' )||( last == '[' )||( last == '{' )||( last == '\x60' );
37 5376 : const bool cur_AZ = ( current >= 'A' )&&( current <= 'Z' );
38 5376 : const bool cur_az = ( current >= 'a' )&&( current <= 'z' );
39 5376 : const bool cur_09 = ( current >= '0' )&&( current <= '9' );
40 5376 : const bool cur_other = ( current > '\x7f' );
41 5376 : const bool cur_stick_left
42 5376 : = ( last == ')' )||( last == ']' )||( last == '}' )||( last == ',' )||( last == ';' );
43 :
44 5376 : const bool same_token
45 0 : = ( last_AZ && ( cur_AZ || cur_az || cur_09 ) )
46 5376 : || ( last_az && ( cur_az || cur_09 ) )
47 0 : || ( last_09 && cur_09 )
48 10752 : || ( last_other && cur_other );
49 5376 : const bool no_break
50 0 : = ( ( last == '.' ) && cur_09 ) /* do not split numbers with dots */
51 5376 : || ( last_09 && ( current == '.' ) ) /* do not split numbers with dots */
52 5376 : || ( ( last == '-' ) && cur_09 ) /* do not split negative numbers */
53 5376 : || ( ( last == '+' ) && cur_09 ) /* do not split positive numbers */
54 15872 : || ( ( last == current ) /* do not split pairs, e.g. ++, --, ==, &&, ...*/
55 5120 : || ( ( last == '!' ) && ( current == '=' ) )
56 5120 : || last_stick_right
57 5120 : || cur_stick_left ); /* even if not the same token, here no break */
58 :
59 5376 : if (( ! same_token )&&( ! no_break ))
60 : {
61 0 : err |= utf8stream_writer_write_char( out_text, utf8codepoint_get_char( &space0width ) );
62 : }
63 : }
64 6400 : err |= utf8stream_writer_write_char( out_text, utf8codepoint_get_char( &codepnt ) );
65 6400 : last = current;
66 : }
67 :
68 512 : utf8codepointiterator_destroy( &iter );
69 512 : return err;
70 : }
71 :
72 :
73 : /*
74 : Copyright 2024-2026 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 : */
|