Line data Source code
1 : /* File: draw_diagram_label.c; Copyright and License: see below */
2 :
3 : #include "draw/draw_diagram_label.h"
4 : #include "u8/u8_trace.h"
5 : #include "data_classifier.h"
6 : #include "data_diagramelement.h"
7 : #include "u8/u8_f64.h"
8 : #include "utf8stringbuf/utf8stringbuf.h"
9 : #include "utf8stringbuf/utf8string.h"
10 : #include <pango/pangocairo.h>
11 : #include <stdio.h>
12 : #include <stdlib.h>
13 : #include <assert.h>
14 :
15 : static const int DRAW_DIAGRAM_PANGO_AUTO_DETECT_LENGTH = -1;
16 :
17 0 : void draw_diagram_label_get_type_and_name_dimensions ( const draw_diagram_label_t *this_,
18 : const data_diagram_t *diagram,
19 : const data_profile_part_t *profile,
20 : const geometry_dimensions_t *proposed_bounds,
21 : const pencil_size_t *pencil_size,
22 : PangoLayout *font_layout,
23 : geometry_dimensions_t *out_label_dim )
24 : {
25 0 : U8_TRACE_BEGIN();
26 0 : assert( NULL != diagram );
27 0 : assert( NULL != profile );
28 0 : assert( NULL != proposed_bounds );
29 0 : assert( NULL != pencil_size );
30 0 : assert( NULL != font_layout );
31 0 : assert( NULL != out_label_dim );
32 :
33 0 : if ( data_diagram_is_valid( diagram ) )
34 : {
35 : /* calc stereotype image bounds */
36 0 : const char *const diagram_stereotype = data_diagram_get_stereotype_const( diagram );
37 : const bool has_stereotype_image
38 0 : = draw_stereotype_image_exists( &((*this_).image_renderer), diagram_stereotype, profile );
39 0 : const geometry_dimensions_t icon_dim
40 : = has_stereotype_image
41 0 : ? draw_stereotype_image_get_dimensions( &((*this_).image_renderer), pencil_size )
42 0 : : (geometry_dimensions_t){ .width = 0.0, .height = 0.0 };
43 0 : const double icon_gap = has_stereotype_image ? pencil_size_get_standard_object_border( pencil_size ) : 0.0;
44 :
45 : /* calc name text dimensions */
46 : /* int proposed_pango_width = geometry_dimensions_get_width( proposed_bounds ); */
47 0 : int text2_height = 0;
48 0 : int text2_width = 0;
49 0 : if ( 0 != utf8string_get_length( data_diagram_get_name_const( diagram ) ))
50 : {
51 0 : pango_layout_set_font_description( font_layout, pencil_size_get_standard_font_description( pencil_size ) );
52 0 : pango_layout_set_text( font_layout,
53 : data_diagram_get_name_const( diagram ),
54 : DRAW_DIAGRAM_PANGO_AUTO_DETECT_LENGTH
55 : );
56 0 : pango_layout_get_pixel_size (font_layout, &text2_width, &text2_height);
57 0 : text2_height += PENCIL_SIZE_FONT_ALIGN_MARGIN; /* allow to align font with pixel border */
58 0 : text2_width += PENCIL_SIZE_FONT_ALIGN_MARGIN;
59 : }
60 :
61 0 : *out_label_dim = (geometry_dimensions_t) {
62 0 : .width = geometry_dimensions_get_width( &icon_dim ) + icon_gap + text2_width,
63 0 : .height = u8_f64_max2( geometry_dimensions_get_height( &icon_dim ), text2_height )
64 : };
65 : }
66 : else
67 : {
68 0 : U8_LOG_ERROR("invalid diagram in draw_diagram_label_get_type_and_name_dimensions()");
69 0 : *out_label_dim = (geometry_dimensions_t) { .width = 0.0, .height = 0.0 };
70 : }
71 0 : U8_TRACE_END();
72 0 : }
73 :
74 0 : void draw_diagram_label_draw_type_and_name ( const draw_diagram_label_t *this_,
75 : const data_diagram_t *diagram,
76 : const data_profile_part_t *profile,
77 : const GdkRGBA *color,
78 : const geometry_rectangle_t *label_box,
79 : const pencil_size_t *pencil_size,
80 : PangoLayout *font_layout,
81 : cairo_t *cr )
82 : {
83 0 : U8_TRACE_BEGIN();
84 0 : assert( NULL != diagram );
85 0 : assert( NULL != profile );
86 0 : assert( NULL != color );
87 0 : assert( NULL != label_box );
88 0 : assert( NULL != pencil_size );
89 0 : assert( NULL != font_layout );
90 0 : assert( NULL != cr );
91 :
92 : /* calc bounds of stereotype icon */
93 0 : const char *const diagram_stereotype = data_diagram_get_stereotype_const( diagram );
94 : const bool has_stereotype_image
95 0 : = draw_stereotype_image_exists( &((*this_).image_renderer), diagram_stereotype, profile );
96 0 : const geometry_rectangle_t stereotype_box
97 : = has_stereotype_image
98 0 : ? draw_stereotype_image_get_bounds( &((*this_).image_renderer),
99 : geometry_rectangle_get_left( label_box ),
100 : geometry_rectangle_get_top( label_box ),
101 : GEOMETRY_H_ALIGN_LEFT,
102 : GEOMETRY_V_ALIGN_TOP,
103 : pencil_size
104 : )
105 0 : : (geometry_rectangle_t){ .left = 0.0, .top = 0.0, .width = 0.0, .height = 0.0 };
106 0 : const double icon_gap = has_stereotype_image ? pencil_size_get_standard_object_border( pencil_size ) : 0.0;
107 :
108 : /* draw stereotype icon */
109 0 : if ( has_stereotype_image )
110 : {
111 : u8_error_info_t err_info;
112 : const u8_error_t stereotype_err
113 0 : = draw_stereotype_image_draw( &((*this_).image_renderer),
114 : diagram_stereotype,
115 : profile,
116 : color,
117 : &err_info,
118 : &stereotype_box,
119 : cr
120 : );
121 0 : if ( u8_error_info_is_error( &err_info ) )
122 : {
123 0 : U8_LOG_WARNING_INT( "stereotype image: unxpected token in svg path in line",
124 : u8_error_info_get_line( &err_info )
125 : );
126 : }
127 0 : else if ( stereotype_err != U8_ERROR_NONE )
128 : {
129 0 : U8_LOG_WARNING_HEX( "error at drawing stereotype image", stereotype_err );
130 : }
131 : }
132 :
133 : /* define names for input data */
134 0 : const double text_left
135 0 : = geometry_rectangle_get_left( label_box ) + geometry_rectangle_get_width( &stereotype_box ) + icon_gap;
136 0 : const double text_top = geometry_rectangle_get_top( label_box );
137 :
138 : /* draw name text */
139 0 : if ( 0 != utf8string_get_length( data_diagram_get_name_const( diagram ) ))
140 : {
141 0 : cairo_set_source_rgba( cr, color->red, color->green, color->blue, color->alpha );
142 0 : pango_layout_set_font_description (font_layout, pencil_size_get_standard_font_description(pencil_size) );
143 0 : pango_layout_set_text( font_layout,
144 : data_diagram_get_name_const( diagram ),
145 : DRAW_DIAGRAM_PANGO_AUTO_DETECT_LENGTH
146 : );
147 :
148 : /* draw text */
149 0 : cairo_move_to ( cr, ceil( text_left ), ceil( text_top ) ); /* align font with pixel border */
150 0 : pango_cairo_show_layout (cr, font_layout);
151 : }
152 :
153 0 : U8_TRACE_END();
154 0 : }
155 :
156 :
157 : /*
158 : Copyright 2017-2024 Andreas Warnke
159 : http://www.apache.org/licenses/LICENSE-2.0
160 :
161 : Licensed under the Apache License, Version 2.0 (the "License");
162 : you may not use this file except in compliance with the License.
163 : You may obtain a copy of the License at
164 :
165 :
166 : Unless required by applicable law or agreed to in writing, software
167 : distributed under the License is distributed on an "AS IS" BASIS,
168 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
169 : See the License for the specific language governing permissions and
170 : limitations under the License.
171 : */
|