Debian Codegraph @

pencil/include/draw/draw_line_breaker.inl

raw
1 /* File: draw_line_breaker.inl; Copyright and License: see below */
2
3 #include "utf8stringbuf/utf8codepointiterator.h"
4 #include <assert.h>
5
6 static inline void draw_line_breaker_init( draw_line_breaker_t *this_ )
7 {
8 }
9
10 static inline void draw_line_breaker_destroy( draw_line_breaker_t *this_ )
11 {
12 }
13
14 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 u8_error_t err = U8_ERROR_NONE;
19 utf8codepointiterator_t iter;
20 utf8codepointiterator_init( &iter, in_text );
21
22 const utf8codepoint_t space0width = utf8codepoint( 0x200B );
23 uint32_t last = ' ';
24 while( utf8codepointiterator_has_next( &iter ) )
25 {
26 utf8codepoint_t codepnt = utf8codepointiterator_next( &iter );
27 uint32_t current = utf8codepoint_get_char ( &codepnt );
28 const bool no_space = ( last != ' ' )&&( current != ' ' );
29 if ( no_space )
30 {
31 const bool last_AZ = ( last >= 'A' )&&( last <= 'Z' );
32 const bool last_az = ( last >= 'a' )&&( last <= 'z' );
33 const bool last_09 = ( last >= '0' )&&( last <= '9' );
34 const bool last_other = ( last > '\x7f' );
35 const bool last_stick_right
36 = ( last == '(' )||( last == '[' )||( last == '{' )||( last == '\x60' );
37 const bool cur_AZ = ( current >= 'A' )&&( current <= 'Z' );
38 const bool cur_az = ( current >= 'a' )&&( current <= 'z' );
39 const bool cur_09 = ( current >= '0' )&&( current <= '9' );
40 const bool cur_other = ( current > '\x7f' );
41 const bool cur_stick_left
42 = ( last == ')' )||( last == ']' )||( last == '}' )||( last == ',' )||( last == ';' );
43
44 const bool same_token
45 = ( last_AZ && ( cur_AZ || cur_az || cur_09 ) )
46 || ( last_az && ( cur_az || cur_09 ) )
47 || ( last_09 && cur_09 )
48 || ( last_other && cur_other );
49 const bool no_break
50 = ( ( last == '.' ) && cur_09 ) /* do not split numbers with dots */
51 || ( last_09 && ( current == '.' ) ) /* do not split numbers with dots */
52 || ( ( last == '-' ) && cur_09 ) /* do not split negative numbers */
53 || ( ( last == '+' ) && cur_09 ) /* do not split positive numbers */
54 || ( ( last == current ) /* do not split pairs, e.g. ++, --, ==, &&, ...*/
55 || ( ( last == '!' ) && ( current == '=' ) )
56 || last_stick_right
57 || cur_stick_left ); /* even if not the same token, here no break */
58
59 if (( ! same_token )&&( ! no_break ))
60 {
61 err |= utf8stream_writer_write_char( out_text, utf8codepoint_get_char( &space0width ) );
62 }
63 }
64 err |= utf8stream_writer_write_char( out_text, utf8codepoint_get_char( &codepnt ) );
65 last = current;
66 }
67
68 utf8codepointiterator_destroy( &iter );
69 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 */