简体   繁体   English

我如何在canvas qml元素中使用FontLoader qml对象?

[英]How i can use FontLoader qml object in canvas qml element?

Hello i want to use the font set into Fontloader qml object in a Canvas element , where i have a curved text. 您好,我想在Canvas元素中使用设置为Fontloader qml对象的字体,其中我有一个弯曲的文本。 It's possible do it? 有可能吗? In general change font family of canvas text it's possible ? 在一般情况下更改画布文本的字体家族是可能的吗? This is my code: 这是我的代码:

Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")


        FontLoader { id: webFont;name: "OpenSans"; source: "qrc:/OpenSans-Bold.ttf" }

        Canvas{

            property string nameFont: webFont.name

            function drawTextAlongArc(context, str, centerX, centerY, radius, angle)
            {

                context.save();
                context.translate(centerX, centerY);
                context.rotate(-1 * angle / 2);
                context.rotate(-1 * (angle / str.length) / 2);
                for (var n = 0; n < str.length; n++) {
                    context.rotate(angle / str.length);
                    context.save();
                    context.translate(0, -1 * radius);
                    var char1 = str[n];
                    context.fillText(char1, 0, 0);
                    context.restore();
                }
                context.restore();

            }


          anchors.fill: parent
          onPaint: {
              var ctx = getContext("2d");
              ctx.fillStyle = Qt.rgba(1, 1, 1, 1);
              ctx.fillRect(0, 0, width, height);

              ctx.font='50px OpenSans'
              ctx.textAlign = "center";

              var centerX = width / 2;
              var centerY = height/2; //height - 30;
              var angle   = Math.PI * 0.8; // radians
              var radius  = 180;
              ctx.fillStyle="#000000"
              drawTextAlongArc(ctx, "Hello World", centerX, centerY, radius, angle);
          }
        }
    }

I want to use instead of Verdana font, that i setted with the instruction : 我想使用我用指令设置的Verdana字体代替:

ctx.font='50px Verdana' ctx.font ='50px Verdana'

the font set into FontLoader { id: webFont; 设置为FontLoader的字体{id:webFont; source: "qrc:/OpenSans-Bold.ttf" } 来源:“ qrc:/OpenSans-Bold.ttf”}

How i can do it ? 我该怎么办?

As you said in the comments, the problem is indeed the space in the font name. 正如您在评论中所说,问题确实是字体名称中的空格。

As mentioned in the doc : 文档中所述

Note: The font-size and font-family properties are mandatory and must be in the order they are shown in above. 注意:font-size和font-family属性是必需的,并且必须按照上面显示的顺序排列。 In addition, a font family with spaces in its name must be quoted. 另外,必须引用名称中带有空格的字体系列。

So the problem should be fixed by loading your font with 因此,应该通过加载字体来解决问题

FontLoader { id: webFont; source: "qrc:/OpenSans-Bold.ttf" }

and using 和使用

ctx.font='50px "Open Sans"'

or even better: 甚至更好:

ctx.font='50px "%1"'.arg(webFont.name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM